gdb: Remove vec.{c,h} and update code to not include vec.h
[binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2019 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2read.h"
33 #include "dwarf-index-cache.h"
34 #include "dwarf-index-common.h"
35 #include "bfd.h"
36 #include "elf-bfd.h"
37 #include "symtab.h"
38 #include "gdbtypes.h"
39 #include "objfiles.h"
40 #include "dwarf2.h"
41 #include "buildsym.h"
42 #include "demangle.h"
43 #include "gdb-demangle.h"
44 #include "filenames.h" /* for DOSish file names */
45 #include "macrotab.h"
46 #include "language.h"
47 #include "complaints.h"
48 #include "dwarf2expr.h"
49 #include "dwarf2loc.h"
50 #include "cp-support.h"
51 #include "hashtab.h"
52 #include "command.h"
53 #include "gdbcmd.h"
54 #include "block.h"
55 #include "addrmap.h"
56 #include "typeprint.h"
57 #include "psympriv.h"
58 #include "c-lang.h"
59 #include "go-lang.h"
60 #include "valprint.h"
61 #include "gdbcore.h" /* for gnutarget */
62 #include "gdb/gdb-index.h"
63 #include "gdb_bfd.h"
64 #include "f-lang.h"
65 #include "source.h"
66 #include "build-id.h"
67 #include "namespace.h"
68 #include "gdbsupport/function-view.h"
69 #include "gdbsupport/gdb_optional.h"
70 #include "gdbsupport/underlying.h"
71 #include "gdbsupport/hash_enum.h"
72 #include "filename-seen-cache.h"
73 #include "producer.h"
74 #include <fcntl.h>
75 #include <algorithm>
76 #include <unordered_map>
77 #include "gdbsupport/selftest.h"
78 #include "rust-lang.h"
79 #include "gdbsupport/pathstuff.h"
80
81 /* When == 1, print basic high level tracing messages.
82 When > 1, be more verbose.
83 This is in contrast to the low level DIE reading of dwarf_die_debug. */
84 static unsigned int dwarf_read_debug = 0;
85
86 /* When non-zero, dump DIEs after they are read in. */
87 static unsigned int dwarf_die_debug = 0;
88
89 /* When non-zero, dump line number entries as they are read in. */
90 static unsigned int dwarf_line_debug = 0;
91
92 /* When true, cross-check physname against demangler. */
93 static bool check_physname = false;
94
95 /* When true, do not reject deprecated .gdb_index sections. */
96 static bool use_deprecated_index_sections = false;
97
98 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
99
100 /* The "aclass" indices for various kinds of computed DWARF symbols. */
101
102 static int dwarf2_locexpr_index;
103 static int dwarf2_loclist_index;
104 static int dwarf2_locexpr_block_index;
105 static int dwarf2_loclist_block_index;
106
107 /* An index into a (C++) symbol name component in a symbol name as
108 recorded in the mapped_index's symbol table. For each C++ symbol
109 in the symbol table, we record one entry for the start of each
110 component in the symbol in a table of name components, and then
111 sort the table, in order to be able to binary search symbol names,
112 ignoring leading namespaces, both completion and regular look up.
113 For example, for symbol "A::B::C", we'll have an entry that points
114 to "A::B::C", another that points to "B::C", and another for "C".
115 Note that function symbols in GDB index have no parameter
116 information, just the function/method names. You can convert a
117 name_component to a "const char *" using the
118 'mapped_index::symbol_name_at(offset_type)' method. */
119
120 struct name_component
121 {
122 /* Offset in the symbol name where the component starts. Stored as
123 a (32-bit) offset instead of a pointer to save memory and improve
124 locality on 64-bit architectures. */
125 offset_type name_offset;
126
127 /* The symbol's index in the symbol and constant pool tables of a
128 mapped_index. */
129 offset_type idx;
130 };
131
132 /* Base class containing bits shared by both .gdb_index and
133 .debug_name indexes. */
134
135 struct mapped_index_base
136 {
137 mapped_index_base () = default;
138 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
139
140 /* The name_component table (a sorted vector). See name_component's
141 description above. */
142 std::vector<name_component> name_components;
143
144 /* How NAME_COMPONENTS is sorted. */
145 enum case_sensitivity name_components_casing;
146
147 /* Return the number of names in the symbol table. */
148 virtual size_t symbol_name_count () const = 0;
149
150 /* Get the name of the symbol at IDX in the symbol table. */
151 virtual const char *symbol_name_at (offset_type idx) const = 0;
152
153 /* Return whether the name at IDX in the symbol table should be
154 ignored. */
155 virtual bool symbol_name_slot_invalid (offset_type idx) const
156 {
157 return false;
158 }
159
160 /* Build the symbol name component sorted vector, if we haven't
161 yet. */
162 void build_name_components ();
163
164 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
165 possible matches for LN_NO_PARAMS in the name component
166 vector. */
167 std::pair<std::vector<name_component>::const_iterator,
168 std::vector<name_component>::const_iterator>
169 find_name_components_bounds (const lookup_name_info &ln_no_params,
170 enum language lang) const;
171
172 /* Prevent deleting/destroying via a base class pointer. */
173 protected:
174 ~mapped_index_base() = default;
175 };
176
177 /* A description of the mapped index. The file format is described in
178 a comment by the code that writes the index. */
179 struct mapped_index final : public mapped_index_base
180 {
181 /* A slot/bucket in the symbol table hash. */
182 struct symbol_table_slot
183 {
184 const offset_type name;
185 const offset_type vec;
186 };
187
188 /* Index data format version. */
189 int version = 0;
190
191 /* The address table data. */
192 gdb::array_view<const gdb_byte> address_table;
193
194 /* The symbol table, implemented as a hash table. */
195 gdb::array_view<symbol_table_slot> symbol_table;
196
197 /* A pointer to the constant pool. */
198 const char *constant_pool = nullptr;
199
200 bool symbol_name_slot_invalid (offset_type idx) const override
201 {
202 const auto &bucket = this->symbol_table[idx];
203 return bucket.name == 0 && bucket.vec == 0;
204 }
205
206 /* Convenience method to get at the name of the symbol at IDX in the
207 symbol table. */
208 const char *symbol_name_at (offset_type idx) const override
209 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
210
211 size_t symbol_name_count () const override
212 { return this->symbol_table.size (); }
213 };
214
215 /* A description of the mapped .debug_names.
216 Uninitialized map has CU_COUNT 0. */
217 struct mapped_debug_names final : public mapped_index_base
218 {
219 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
220 : dwarf2_per_objfile (dwarf2_per_objfile_)
221 {}
222
223 struct dwarf2_per_objfile *dwarf2_per_objfile;
224 bfd_endian dwarf5_byte_order;
225 bool dwarf5_is_dwarf64;
226 bool augmentation_is_gdb;
227 uint8_t offset_size;
228 uint32_t cu_count = 0;
229 uint32_t tu_count, bucket_count, name_count;
230 const gdb_byte *cu_table_reordered, *tu_table_reordered;
231 const uint32_t *bucket_table_reordered, *hash_table_reordered;
232 const gdb_byte *name_table_string_offs_reordered;
233 const gdb_byte *name_table_entry_offs_reordered;
234 const gdb_byte *entry_pool;
235
236 struct index_val
237 {
238 ULONGEST dwarf_tag;
239 struct attr
240 {
241 /* Attribute name DW_IDX_*. */
242 ULONGEST dw_idx;
243
244 /* Attribute form DW_FORM_*. */
245 ULONGEST form;
246
247 /* Value if FORM is DW_FORM_implicit_const. */
248 LONGEST implicit_const;
249 };
250 std::vector<attr> attr_vec;
251 };
252
253 std::unordered_map<ULONGEST, index_val> abbrev_map;
254
255 const char *namei_to_name (uint32_t namei) const;
256
257 /* Implementation of the mapped_index_base virtual interface, for
258 the name_components cache. */
259
260 const char *symbol_name_at (offset_type idx) const override
261 { return namei_to_name (idx); }
262
263 size_t symbol_name_count () const override
264 { return this->name_count; }
265 };
266
267 /* See dwarf2read.h. */
268
269 dwarf2_per_objfile *
270 get_dwarf2_per_objfile (struct objfile *objfile)
271 {
272 return dwarf2_objfile_data_key.get (objfile);
273 }
274
275 /* Default names of the debugging sections. */
276
277 /* Note that if the debugging section has been compressed, it might
278 have a name like .zdebug_info. */
279
280 static const struct dwarf2_debug_sections dwarf2_elf_names =
281 {
282 { ".debug_info", ".zdebug_info" },
283 { ".debug_abbrev", ".zdebug_abbrev" },
284 { ".debug_line", ".zdebug_line" },
285 { ".debug_loc", ".zdebug_loc" },
286 { ".debug_loclists", ".zdebug_loclists" },
287 { ".debug_macinfo", ".zdebug_macinfo" },
288 { ".debug_macro", ".zdebug_macro" },
289 { ".debug_str", ".zdebug_str" },
290 { ".debug_line_str", ".zdebug_line_str" },
291 { ".debug_ranges", ".zdebug_ranges" },
292 { ".debug_rnglists", ".zdebug_rnglists" },
293 { ".debug_types", ".zdebug_types" },
294 { ".debug_addr", ".zdebug_addr" },
295 { ".debug_frame", ".zdebug_frame" },
296 { ".eh_frame", NULL },
297 { ".gdb_index", ".zgdb_index" },
298 { ".debug_names", ".zdebug_names" },
299 { ".debug_aranges", ".zdebug_aranges" },
300 23
301 };
302
303 /* List of DWO/DWP sections. */
304
305 static const struct dwop_section_names
306 {
307 struct dwarf2_section_names abbrev_dwo;
308 struct dwarf2_section_names info_dwo;
309 struct dwarf2_section_names line_dwo;
310 struct dwarf2_section_names loc_dwo;
311 struct dwarf2_section_names loclists_dwo;
312 struct dwarf2_section_names macinfo_dwo;
313 struct dwarf2_section_names macro_dwo;
314 struct dwarf2_section_names str_dwo;
315 struct dwarf2_section_names str_offsets_dwo;
316 struct dwarf2_section_names types_dwo;
317 struct dwarf2_section_names cu_index;
318 struct dwarf2_section_names tu_index;
319 }
320 dwop_section_names =
321 {
322 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
323 { ".debug_info.dwo", ".zdebug_info.dwo" },
324 { ".debug_line.dwo", ".zdebug_line.dwo" },
325 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
326 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
327 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
328 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
329 { ".debug_str.dwo", ".zdebug_str.dwo" },
330 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
331 { ".debug_types.dwo", ".zdebug_types.dwo" },
332 { ".debug_cu_index", ".zdebug_cu_index" },
333 { ".debug_tu_index", ".zdebug_tu_index" },
334 };
335
336 /* local data types */
337
338 /* The data in a compilation unit header, after target2host
339 translation, looks like this. */
340 struct comp_unit_head
341 {
342 unsigned int length;
343 short version;
344 unsigned char addr_size;
345 unsigned char signed_addr_p;
346 sect_offset abbrev_sect_off;
347
348 /* Size of file offsets; either 4 or 8. */
349 unsigned int offset_size;
350
351 /* Size of the length field; either 4 or 12. */
352 unsigned int initial_length_size;
353
354 enum dwarf_unit_type unit_type;
355
356 /* Offset to the first byte of this compilation unit header in the
357 .debug_info section, for resolving relative reference dies. */
358 sect_offset sect_off;
359
360 /* Offset to first die in this cu from the start of the cu.
361 This will be the first byte following the compilation unit header. */
362 cu_offset first_die_cu_offset;
363
364
365 /* 64-bit signature of this unit. For type units, it denotes the signature of
366 the type (DW_UT_type in DWARF 4, additionally DW_UT_split_type in DWARF 5).
367 Also used in DWARF 5, to denote the dwo id when the unit type is
368 DW_UT_skeleton or DW_UT_split_compile. */
369 ULONGEST signature;
370
371 /* For types, offset in the type's DIE of the type defined by this TU. */
372 cu_offset type_cu_offset_in_tu;
373 };
374
375 /* Type used for delaying computation of method physnames.
376 See comments for compute_delayed_physnames. */
377 struct delayed_method_info
378 {
379 /* The type to which the method is attached, i.e., its parent class. */
380 struct type *type;
381
382 /* The index of the method in the type's function fieldlists. */
383 int fnfield_index;
384
385 /* The index of the method in the fieldlist. */
386 int index;
387
388 /* The name of the DIE. */
389 const char *name;
390
391 /* The DIE associated with this method. */
392 struct die_info *die;
393 };
394
395 /* Internal state when decoding a particular compilation unit. */
396 struct dwarf2_cu
397 {
398 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
399 ~dwarf2_cu ();
400
401 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
402
403 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
404 Create the set of symtabs used by this TU, or if this TU is sharing
405 symtabs with another TU and the symtabs have already been created
406 then restore those symtabs in the line header.
407 We don't need the pc/line-number mapping for type units. */
408 void setup_type_unit_groups (struct die_info *die);
409
410 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
411 buildsym_compunit constructor. */
412 struct compunit_symtab *start_symtab (const char *name,
413 const char *comp_dir,
414 CORE_ADDR low_pc);
415
416 /* Reset the builder. */
417 void reset_builder () { m_builder.reset (); }
418
419 /* The header of the compilation unit. */
420 struct comp_unit_head header {};
421
422 /* Base address of this compilation unit. */
423 CORE_ADDR base_address = 0;
424
425 /* Non-zero if base_address has been set. */
426 int base_known = 0;
427
428 /* The language we are debugging. */
429 enum language language = language_unknown;
430 const struct language_defn *language_defn = nullptr;
431
432 const char *producer = nullptr;
433
434 private:
435 /* The symtab builder for this CU. This is only non-NULL when full
436 symbols are being read. */
437 std::unique_ptr<buildsym_compunit> m_builder;
438
439 public:
440 /* The generic symbol table building routines have separate lists for
441 file scope symbols and all all other scopes (local scopes). So
442 we need to select the right one to pass to add_symbol_to_list().
443 We do it by keeping a pointer to the correct list in list_in_scope.
444
445 FIXME: The original dwarf code just treated the file scope as the
446 first local scope, and all other local scopes as nested local
447 scopes, and worked fine. Check to see if we really need to
448 distinguish these in buildsym.c. */
449 struct pending **list_in_scope = nullptr;
450
451 /* Hash table holding all the loaded partial DIEs
452 with partial_die->offset.SECT_OFF as hash. */
453 htab_t partial_dies = nullptr;
454
455 /* Storage for things with the same lifetime as this read-in compilation
456 unit, including partial DIEs. */
457 auto_obstack comp_unit_obstack;
458
459 /* When multiple dwarf2_cu structures are living in memory, this field
460 chains them all together, so that they can be released efficiently.
461 We will probably also want a generation counter so that most-recently-used
462 compilation units are cached... */
463 struct dwarf2_per_cu_data *read_in_chain = nullptr;
464
465 /* Backlink to our per_cu entry. */
466 struct dwarf2_per_cu_data *per_cu;
467
468 /* How many compilation units ago was this CU last referenced? */
469 int last_used = 0;
470
471 /* A hash table of DIE cu_offset for following references with
472 die_info->offset.sect_off as hash. */
473 htab_t die_hash = nullptr;
474
475 /* Full DIEs if read in. */
476 struct die_info *dies = nullptr;
477
478 /* A set of pointers to dwarf2_per_cu_data objects for compilation
479 units referenced by this one. Only set during full symbol processing;
480 partial symbol tables do not have dependencies. */
481 htab_t dependencies = nullptr;
482
483 /* Header data from the line table, during full symbol processing. */
484 struct line_header *line_header = nullptr;
485 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
486 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
487 this is the DW_TAG_compile_unit die for this CU. We'll hold on
488 to the line header as long as this DIE is being processed. See
489 process_die_scope. */
490 die_info *line_header_die_owner = nullptr;
491
492 /* A list of methods which need to have physnames computed
493 after all type information has been read. */
494 std::vector<delayed_method_info> method_list;
495
496 /* To be copied to symtab->call_site_htab. */
497 htab_t call_site_htab = nullptr;
498
499 /* Non-NULL if this CU came from a DWO file.
500 There is an invariant here that is important to remember:
501 Except for attributes copied from the top level DIE in the "main"
502 (or "stub") file in preparation for reading the DWO file
503 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
504 Either there isn't a DWO file (in which case this is NULL and the point
505 is moot), or there is and either we're not going to read it (in which
506 case this is NULL) or there is and we are reading it (in which case this
507 is non-NULL). */
508 struct dwo_unit *dwo_unit = nullptr;
509
510 /* The DW_AT_addr_base attribute if present, zero otherwise
511 (zero is a valid value though).
512 Note this value comes from the Fission stub CU/TU's DIE. */
513 ULONGEST addr_base = 0;
514
515 /* The DW_AT_ranges_base attribute if present, zero otherwise
516 (zero is a valid value though).
517 Note this value comes from the Fission stub CU/TU's DIE.
518 Also note that the value is zero in the non-DWO case so this value can
519 be used without needing to know whether DWO files are in use or not.
520 N.B. This does not apply to DW_AT_ranges appearing in
521 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
522 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
523 DW_AT_ranges_base *would* have to be applied, and we'd have to care
524 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
525 ULONGEST ranges_base = 0;
526
527 /* When reading debug info generated by older versions of rustc, we
528 have to rewrite some union types to be struct types with a
529 variant part. This rewriting must be done after the CU is fully
530 read in, because otherwise at the point of rewriting some struct
531 type might not have been fully processed. So, we keep a list of
532 all such types here and process them after expansion. */
533 std::vector<struct type *> rust_unions;
534
535 /* Mark used when releasing cached dies. */
536 bool mark : 1;
537
538 /* This CU references .debug_loc. See the symtab->locations_valid field.
539 This test is imperfect as there may exist optimized debug code not using
540 any location list and still facing inlining issues if handled as
541 unoptimized code. For a future better test see GCC PR other/32998. */
542 bool has_loclist : 1;
543
544 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
545 if all the producer_is_* fields are valid. This information is cached
546 because profiling CU expansion showed excessive time spent in
547 producer_is_gxx_lt_4_6. */
548 bool checked_producer : 1;
549 bool producer_is_gxx_lt_4_6 : 1;
550 bool producer_is_gcc_lt_4_3 : 1;
551 bool producer_is_icc : 1;
552 bool producer_is_icc_lt_14 : 1;
553 bool producer_is_codewarrior : 1;
554
555 /* When true, the file that we're processing is known to have
556 debugging info for C++ namespaces. GCC 3.3.x did not produce
557 this information, but later versions do. */
558
559 bool processing_has_namespace_info : 1;
560
561 struct partial_die_info *find_partial_die (sect_offset sect_off);
562
563 /* If this CU was inherited by another CU (via specification,
564 abstract_origin, etc), this is the ancestor CU. */
565 dwarf2_cu *ancestor;
566
567 /* Get the buildsym_compunit for this CU. */
568 buildsym_compunit *get_builder ()
569 {
570 /* If this CU has a builder associated with it, use that. */
571 if (m_builder != nullptr)
572 return m_builder.get ();
573
574 /* Otherwise, search ancestors for a valid builder. */
575 if (ancestor != nullptr)
576 return ancestor->get_builder ();
577
578 return nullptr;
579 }
580 };
581
582 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
583 This includes type_unit_group and quick_file_names. */
584
585 struct stmt_list_hash
586 {
587 /* The DWO unit this table is from or NULL if there is none. */
588 struct dwo_unit *dwo_unit;
589
590 /* Offset in .debug_line or .debug_line.dwo. */
591 sect_offset line_sect_off;
592 };
593
594 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
595 an object of this type. */
596
597 struct type_unit_group
598 {
599 /* dwarf2read.c's main "handle" on a TU symtab.
600 To simplify things we create an artificial CU that "includes" all the
601 type units using this stmt_list so that the rest of the code still has
602 a "per_cu" handle on the symtab.
603 This PER_CU is recognized by having no section. */
604 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
605 struct dwarf2_per_cu_data per_cu;
606
607 /* The TUs that share this DW_AT_stmt_list entry.
608 This is added to while parsing type units to build partial symtabs,
609 and is deleted afterwards and not used again. */
610 std::vector<signatured_type *> *tus;
611
612 /* The compunit symtab.
613 Type units in a group needn't all be defined in the same source file,
614 so we create an essentially anonymous symtab as the compunit symtab. */
615 struct compunit_symtab *compunit_symtab;
616
617 /* The data used to construct the hash key. */
618 struct stmt_list_hash hash;
619
620 /* The number of symtabs from the line header.
621 The value here must match line_header.num_file_names. */
622 unsigned int num_symtabs;
623
624 /* The symbol tables for this TU (obtained from the files listed in
625 DW_AT_stmt_list).
626 WARNING: The order of entries here must match the order of entries
627 in the line header. After the first TU using this type_unit_group, the
628 line header for the subsequent TUs is recreated from this. This is done
629 because we need to use the same symtabs for each TU using the same
630 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
631 there's no guarantee the line header doesn't have duplicate entries. */
632 struct symtab **symtabs;
633 };
634
635 /* These sections are what may appear in a (real or virtual) DWO file. */
636
637 struct dwo_sections
638 {
639 struct dwarf2_section_info abbrev;
640 struct dwarf2_section_info line;
641 struct dwarf2_section_info loc;
642 struct dwarf2_section_info loclists;
643 struct dwarf2_section_info macinfo;
644 struct dwarf2_section_info macro;
645 struct dwarf2_section_info str;
646 struct dwarf2_section_info str_offsets;
647 /* In the case of a virtual DWO file, these two are unused. */
648 struct dwarf2_section_info info;
649 std::vector<dwarf2_section_info> types;
650 };
651
652 /* CUs/TUs in DWP/DWO files. */
653
654 struct dwo_unit
655 {
656 /* Backlink to the containing struct dwo_file. */
657 struct dwo_file *dwo_file;
658
659 /* The "id" that distinguishes this CU/TU.
660 .debug_info calls this "dwo_id", .debug_types calls this "signature".
661 Since signatures came first, we stick with it for consistency. */
662 ULONGEST signature;
663
664 /* The section this CU/TU lives in, in the DWO file. */
665 struct dwarf2_section_info *section;
666
667 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
668 sect_offset sect_off;
669 unsigned int length;
670
671 /* For types, offset in the type's DIE of the type defined by this TU. */
672 cu_offset type_offset_in_tu;
673 };
674
675 /* include/dwarf2.h defines the DWP section codes.
676 It defines a max value but it doesn't define a min value, which we
677 use for error checking, so provide one. */
678
679 enum dwp_v2_section_ids
680 {
681 DW_SECT_MIN = 1
682 };
683
684 /* Data for one DWO file.
685
686 This includes virtual DWO files (a virtual DWO file is a DWO file as it
687 appears in a DWP file). DWP files don't really have DWO files per se -
688 comdat folding of types "loses" the DWO file they came from, and from
689 a high level view DWP files appear to contain a mass of random types.
690 However, to maintain consistency with the non-DWP case we pretend DWP
691 files contain virtual DWO files, and we assign each TU with one virtual
692 DWO file (generally based on the line and abbrev section offsets -
693 a heuristic that seems to work in practice). */
694
695 struct dwo_file
696 {
697 dwo_file () = default;
698 DISABLE_COPY_AND_ASSIGN (dwo_file);
699
700 /* The DW_AT_GNU_dwo_name attribute.
701 For virtual DWO files the name is constructed from the section offsets
702 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
703 from related CU+TUs. */
704 const char *dwo_name = nullptr;
705
706 /* The DW_AT_comp_dir attribute. */
707 const char *comp_dir = nullptr;
708
709 /* The bfd, when the file is open. Otherwise this is NULL.
710 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
711 gdb_bfd_ref_ptr dbfd;
712
713 /* The sections that make up this DWO file.
714 Remember that for virtual DWO files in DWP V2, these are virtual
715 sections (for lack of a better name). */
716 struct dwo_sections sections {};
717
718 /* The CUs in the file.
719 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
720 an extension to handle LLVM's Link Time Optimization output (where
721 multiple source files may be compiled into a single object/dwo pair). */
722 htab_t cus {};
723
724 /* Table of TUs in the file.
725 Each element is a struct dwo_unit. */
726 htab_t tus {};
727 };
728
729 /* These sections are what may appear in a DWP file. */
730
731 struct dwp_sections
732 {
733 /* These are used by both DWP version 1 and 2. */
734 struct dwarf2_section_info str;
735 struct dwarf2_section_info cu_index;
736 struct dwarf2_section_info tu_index;
737
738 /* These are only used by DWP version 2 files.
739 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
740 sections are referenced by section number, and are not recorded here.
741 In DWP version 2 there is at most one copy of all these sections, each
742 section being (effectively) comprised of the concatenation of all of the
743 individual sections that exist in the version 1 format.
744 To keep the code simple we treat each of these concatenated pieces as a
745 section itself (a virtual section?). */
746 struct dwarf2_section_info abbrev;
747 struct dwarf2_section_info info;
748 struct dwarf2_section_info line;
749 struct dwarf2_section_info loc;
750 struct dwarf2_section_info macinfo;
751 struct dwarf2_section_info macro;
752 struct dwarf2_section_info str_offsets;
753 struct dwarf2_section_info types;
754 };
755
756 /* These sections are what may appear in a virtual DWO file in DWP version 1.
757 A virtual DWO file is a DWO file as it appears in a DWP file. */
758
759 struct virtual_v1_dwo_sections
760 {
761 struct dwarf2_section_info abbrev;
762 struct dwarf2_section_info line;
763 struct dwarf2_section_info loc;
764 struct dwarf2_section_info macinfo;
765 struct dwarf2_section_info macro;
766 struct dwarf2_section_info str_offsets;
767 /* Each DWP hash table entry records one CU or one TU.
768 That is recorded here, and copied to dwo_unit.section. */
769 struct dwarf2_section_info info_or_types;
770 };
771
772 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
773 In version 2, the sections of the DWO files are concatenated together
774 and stored in one section of that name. Thus each ELF section contains
775 several "virtual" sections. */
776
777 struct virtual_v2_dwo_sections
778 {
779 bfd_size_type abbrev_offset;
780 bfd_size_type abbrev_size;
781
782 bfd_size_type line_offset;
783 bfd_size_type line_size;
784
785 bfd_size_type loc_offset;
786 bfd_size_type loc_size;
787
788 bfd_size_type macinfo_offset;
789 bfd_size_type macinfo_size;
790
791 bfd_size_type macro_offset;
792 bfd_size_type macro_size;
793
794 bfd_size_type str_offsets_offset;
795 bfd_size_type str_offsets_size;
796
797 /* Each DWP hash table entry records one CU or one TU.
798 That is recorded here, and copied to dwo_unit.section. */
799 bfd_size_type info_or_types_offset;
800 bfd_size_type info_or_types_size;
801 };
802
803 /* Contents of DWP hash tables. */
804
805 struct dwp_hash_table
806 {
807 uint32_t version, nr_columns;
808 uint32_t nr_units, nr_slots;
809 const gdb_byte *hash_table, *unit_table;
810 union
811 {
812 struct
813 {
814 const gdb_byte *indices;
815 } v1;
816 struct
817 {
818 /* This is indexed by column number and gives the id of the section
819 in that column. */
820 #define MAX_NR_V2_DWO_SECTIONS \
821 (1 /* .debug_info or .debug_types */ \
822 + 1 /* .debug_abbrev */ \
823 + 1 /* .debug_line */ \
824 + 1 /* .debug_loc */ \
825 + 1 /* .debug_str_offsets */ \
826 + 1 /* .debug_macro or .debug_macinfo */)
827 int section_ids[MAX_NR_V2_DWO_SECTIONS];
828 const gdb_byte *offsets;
829 const gdb_byte *sizes;
830 } v2;
831 } section_pool;
832 };
833
834 /* Data for one DWP file. */
835
836 struct dwp_file
837 {
838 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
839 : name (name_),
840 dbfd (std::move (abfd))
841 {
842 }
843
844 /* Name of the file. */
845 const char *name;
846
847 /* File format version. */
848 int version = 0;
849
850 /* The bfd. */
851 gdb_bfd_ref_ptr dbfd;
852
853 /* Section info for this file. */
854 struct dwp_sections sections {};
855
856 /* Table of CUs in the file. */
857 const struct dwp_hash_table *cus = nullptr;
858
859 /* Table of TUs in the file. */
860 const struct dwp_hash_table *tus = nullptr;
861
862 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
863 htab_t loaded_cus {};
864 htab_t loaded_tus {};
865
866 /* Table to map ELF section numbers to their sections.
867 This is only needed for the DWP V1 file format. */
868 unsigned int num_sections = 0;
869 asection **elf_sections = nullptr;
870 };
871
872 /* Struct used to pass misc. parameters to read_die_and_children, et
873 al. which are used for both .debug_info and .debug_types dies.
874 All parameters here are unchanging for the life of the call. This
875 struct exists to abstract away the constant parameters of die reading. */
876
877 struct die_reader_specs
878 {
879 /* The bfd of die_section. */
880 bfd* abfd;
881
882 /* The CU of the DIE we are parsing. */
883 struct dwarf2_cu *cu;
884
885 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
886 struct dwo_file *dwo_file;
887
888 /* The section the die comes from.
889 This is either .debug_info or .debug_types, or the .dwo variants. */
890 struct dwarf2_section_info *die_section;
891
892 /* die_section->buffer. */
893 const gdb_byte *buffer;
894
895 /* The end of the buffer. */
896 const gdb_byte *buffer_end;
897
898 /* The value of the DW_AT_comp_dir attribute. */
899 const char *comp_dir;
900
901 /* The abbreviation table to use when reading the DIEs. */
902 struct abbrev_table *abbrev_table;
903 };
904
905 /* Type of function passed to init_cutu_and_read_dies, et.al. */
906 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
907 const gdb_byte *info_ptr,
908 struct die_info *comp_unit_die,
909 int has_children,
910 void *data);
911
912 /* A 1-based directory index. This is a strong typedef to prevent
913 accidentally using a directory index as a 0-based index into an
914 array/vector. */
915 enum class dir_index : unsigned int {};
916
917 /* Likewise, a 1-based file name index. */
918 enum class file_name_index : unsigned int {};
919
920 struct file_entry
921 {
922 file_entry () = default;
923
924 file_entry (const char *name_, dir_index d_index_,
925 unsigned int mod_time_, unsigned int length_)
926 : name (name_),
927 d_index (d_index_),
928 mod_time (mod_time_),
929 length (length_)
930 {}
931
932 /* Return the include directory at D_INDEX stored in LH. Returns
933 NULL if D_INDEX is out of bounds. */
934 const char *include_dir (const line_header *lh) const;
935
936 /* The file name. Note this is an observing pointer. The memory is
937 owned by debug_line_buffer. */
938 const char *name {};
939
940 /* The directory index (1-based). */
941 dir_index d_index {};
942
943 unsigned int mod_time {};
944
945 unsigned int length {};
946
947 /* True if referenced by the Line Number Program. */
948 bool included_p {};
949
950 /* The associated symbol table, if any. */
951 struct symtab *symtab {};
952 };
953
954 /* The line number information for a compilation unit (found in the
955 .debug_line section) begins with a "statement program header",
956 which contains the following information. */
957 struct line_header
958 {
959 line_header ()
960 : offset_in_dwz {}
961 {}
962
963 /* Add an entry to the include directory table. */
964 void add_include_dir (const char *include_dir);
965
966 /* Add an entry to the file name table. */
967 void add_file_name (const char *name, dir_index d_index,
968 unsigned int mod_time, unsigned int length);
969
970 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
971 is out of bounds. */
972 const char *include_dir_at (dir_index index) const
973 {
974 /* Convert directory index number (1-based) to vector index
975 (0-based). */
976 size_t vec_index = to_underlying (index) - 1;
977
978 if (vec_index >= include_dirs.size ())
979 return NULL;
980 return include_dirs[vec_index];
981 }
982
983 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
984 is out of bounds. */
985 file_entry *file_name_at (file_name_index index)
986 {
987 /* Convert file name index number (1-based) to vector index
988 (0-based). */
989 size_t vec_index = to_underlying (index) - 1;
990
991 if (vec_index >= file_names.size ())
992 return NULL;
993 return &file_names[vec_index];
994 }
995
996 /* Offset of line number information in .debug_line section. */
997 sect_offset sect_off {};
998
999 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1000 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1001
1002 unsigned int total_length {};
1003 unsigned short version {};
1004 unsigned int header_length {};
1005 unsigned char minimum_instruction_length {};
1006 unsigned char maximum_ops_per_instruction {};
1007 unsigned char default_is_stmt {};
1008 int line_base {};
1009 unsigned char line_range {};
1010 unsigned char opcode_base {};
1011
1012 /* standard_opcode_lengths[i] is the number of operands for the
1013 standard opcode whose value is i. This means that
1014 standard_opcode_lengths[0] is unused, and the last meaningful
1015 element is standard_opcode_lengths[opcode_base - 1]. */
1016 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1017
1018 /* The include_directories table. Note these are observing
1019 pointers. The memory is owned by debug_line_buffer. */
1020 std::vector<const char *> include_dirs;
1021
1022 /* The file_names table. */
1023 std::vector<file_entry> file_names;
1024
1025 /* The start and end of the statement program following this
1026 header. These point into dwarf2_per_objfile->line_buffer. */
1027 const gdb_byte *statement_program_start {}, *statement_program_end {};
1028 };
1029
1030 typedef std::unique_ptr<line_header> line_header_up;
1031
1032 const char *
1033 file_entry::include_dir (const line_header *lh) const
1034 {
1035 return lh->include_dir_at (d_index);
1036 }
1037
1038 /* When we construct a partial symbol table entry we only
1039 need this much information. */
1040 struct partial_die_info : public allocate_on_obstack
1041 {
1042 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1043
1044 /* Disable assign but still keep copy ctor, which is needed
1045 load_partial_dies. */
1046 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1047
1048 /* Adjust the partial die before generating a symbol for it. This
1049 function may set the is_external flag or change the DIE's
1050 name. */
1051 void fixup (struct dwarf2_cu *cu);
1052
1053 /* Read a minimal amount of information into the minimal die
1054 structure. */
1055 const gdb_byte *read (const struct die_reader_specs *reader,
1056 const struct abbrev_info &abbrev,
1057 const gdb_byte *info_ptr);
1058
1059 /* Offset of this DIE. */
1060 const sect_offset sect_off;
1061
1062 /* DWARF-2 tag for this DIE. */
1063 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1064
1065 /* Assorted flags describing the data found in this DIE. */
1066 const unsigned int has_children : 1;
1067
1068 unsigned int is_external : 1;
1069 unsigned int is_declaration : 1;
1070 unsigned int has_type : 1;
1071 unsigned int has_specification : 1;
1072 unsigned int has_pc_info : 1;
1073 unsigned int may_be_inlined : 1;
1074
1075 /* This DIE has been marked DW_AT_main_subprogram. */
1076 unsigned int main_subprogram : 1;
1077
1078 /* Flag set if the SCOPE field of this structure has been
1079 computed. */
1080 unsigned int scope_set : 1;
1081
1082 /* Flag set if the DIE has a byte_size attribute. */
1083 unsigned int has_byte_size : 1;
1084
1085 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1086 unsigned int has_const_value : 1;
1087
1088 /* Flag set if any of the DIE's children are template arguments. */
1089 unsigned int has_template_arguments : 1;
1090
1091 /* Flag set if fixup has been called on this die. */
1092 unsigned int fixup_called : 1;
1093
1094 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1095 unsigned int is_dwz : 1;
1096
1097 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1098 unsigned int spec_is_dwz : 1;
1099
1100 /* The name of this DIE. Normally the value of DW_AT_name, but
1101 sometimes a default name for unnamed DIEs. */
1102 const char *name = nullptr;
1103
1104 /* The linkage name, if present. */
1105 const char *linkage_name = nullptr;
1106
1107 /* The scope to prepend to our children. This is generally
1108 allocated on the comp_unit_obstack, so will disappear
1109 when this compilation unit leaves the cache. */
1110 const char *scope = nullptr;
1111
1112 /* Some data associated with the partial DIE. The tag determines
1113 which field is live. */
1114 union
1115 {
1116 /* The location description associated with this DIE, if any. */
1117 struct dwarf_block *locdesc;
1118 /* The offset of an import, for DW_TAG_imported_unit. */
1119 sect_offset sect_off;
1120 } d {};
1121
1122 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1123 CORE_ADDR lowpc = 0;
1124 CORE_ADDR highpc = 0;
1125
1126 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1127 DW_AT_sibling, if any. */
1128 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1129 could return DW_AT_sibling values to its caller load_partial_dies. */
1130 const gdb_byte *sibling = nullptr;
1131
1132 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1133 DW_AT_specification (or DW_AT_abstract_origin or
1134 DW_AT_extension). */
1135 sect_offset spec_offset {};
1136
1137 /* Pointers to this DIE's parent, first child, and next sibling,
1138 if any. */
1139 struct partial_die_info *die_parent = nullptr;
1140 struct partial_die_info *die_child = nullptr;
1141 struct partial_die_info *die_sibling = nullptr;
1142
1143 friend struct partial_die_info *
1144 dwarf2_cu::find_partial_die (sect_offset sect_off);
1145
1146 private:
1147 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1148 partial_die_info (sect_offset sect_off)
1149 : partial_die_info (sect_off, DW_TAG_padding, 0)
1150 {
1151 }
1152
1153 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1154 int has_children_)
1155 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1156 {
1157 is_external = 0;
1158 is_declaration = 0;
1159 has_type = 0;
1160 has_specification = 0;
1161 has_pc_info = 0;
1162 may_be_inlined = 0;
1163 main_subprogram = 0;
1164 scope_set = 0;
1165 has_byte_size = 0;
1166 has_const_value = 0;
1167 has_template_arguments = 0;
1168 fixup_called = 0;
1169 is_dwz = 0;
1170 spec_is_dwz = 0;
1171 }
1172 };
1173
1174 /* This data structure holds the information of an abbrev. */
1175 struct abbrev_info
1176 {
1177 unsigned int number; /* number identifying abbrev */
1178 enum dwarf_tag tag; /* dwarf tag */
1179 unsigned short has_children; /* boolean */
1180 unsigned short num_attrs; /* number of attributes */
1181 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1182 struct abbrev_info *next; /* next in chain */
1183 };
1184
1185 struct attr_abbrev
1186 {
1187 ENUM_BITFIELD(dwarf_attribute) name : 16;
1188 ENUM_BITFIELD(dwarf_form) form : 16;
1189
1190 /* It is valid only if FORM is DW_FORM_implicit_const. */
1191 LONGEST implicit_const;
1192 };
1193
1194 /* Size of abbrev_table.abbrev_hash_table. */
1195 #define ABBREV_HASH_SIZE 121
1196
1197 /* Top level data structure to contain an abbreviation table. */
1198
1199 struct abbrev_table
1200 {
1201 explicit abbrev_table (sect_offset off)
1202 : sect_off (off)
1203 {
1204 m_abbrevs =
1205 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1206 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1207 }
1208
1209 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1210
1211 /* Allocate space for a struct abbrev_info object in
1212 ABBREV_TABLE. */
1213 struct abbrev_info *alloc_abbrev ();
1214
1215 /* Add an abbreviation to the table. */
1216 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1217
1218 /* Look up an abbrev in the table.
1219 Returns NULL if the abbrev is not found. */
1220
1221 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1222
1223
1224 /* Where the abbrev table came from.
1225 This is used as a sanity check when the table is used. */
1226 const sect_offset sect_off;
1227
1228 /* Storage for the abbrev table. */
1229 auto_obstack abbrev_obstack;
1230
1231 private:
1232
1233 /* Hash table of abbrevs.
1234 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1235 It could be statically allocated, but the previous code didn't so we
1236 don't either. */
1237 struct abbrev_info **m_abbrevs;
1238 };
1239
1240 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1241
1242 /* Attributes have a name and a value. */
1243 struct attribute
1244 {
1245 ENUM_BITFIELD(dwarf_attribute) name : 16;
1246 ENUM_BITFIELD(dwarf_form) form : 15;
1247
1248 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1249 field should be in u.str (existing only for DW_STRING) but it is kept
1250 here for better struct attribute alignment. */
1251 unsigned int string_is_canonical : 1;
1252
1253 union
1254 {
1255 const char *str;
1256 struct dwarf_block *blk;
1257 ULONGEST unsnd;
1258 LONGEST snd;
1259 CORE_ADDR addr;
1260 ULONGEST signature;
1261 }
1262 u;
1263 };
1264
1265 /* This data structure holds a complete die structure. */
1266 struct die_info
1267 {
1268 /* DWARF-2 tag for this DIE. */
1269 ENUM_BITFIELD(dwarf_tag) tag : 16;
1270
1271 /* Number of attributes */
1272 unsigned char num_attrs;
1273
1274 /* True if we're presently building the full type name for the
1275 type derived from this DIE. */
1276 unsigned char building_fullname : 1;
1277
1278 /* True if this die is in process. PR 16581. */
1279 unsigned char in_process : 1;
1280
1281 /* Abbrev number */
1282 unsigned int abbrev;
1283
1284 /* Offset in .debug_info or .debug_types section. */
1285 sect_offset sect_off;
1286
1287 /* The dies in a compilation unit form an n-ary tree. PARENT
1288 points to this die's parent; CHILD points to the first child of
1289 this node; and all the children of a given node are chained
1290 together via their SIBLING fields. */
1291 struct die_info *child; /* Its first child, if any. */
1292 struct die_info *sibling; /* Its next sibling, if any. */
1293 struct die_info *parent; /* Its parent, if any. */
1294
1295 /* An array of attributes, with NUM_ATTRS elements. There may be
1296 zero, but it's not common and zero-sized arrays are not
1297 sufficiently portable C. */
1298 struct attribute attrs[1];
1299 };
1300
1301 /* Get at parts of an attribute structure. */
1302
1303 #define DW_STRING(attr) ((attr)->u.str)
1304 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1305 #define DW_UNSND(attr) ((attr)->u.unsnd)
1306 #define DW_BLOCK(attr) ((attr)->u.blk)
1307 #define DW_SND(attr) ((attr)->u.snd)
1308 #define DW_ADDR(attr) ((attr)->u.addr)
1309 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1310
1311 /* Blocks are a bunch of untyped bytes. */
1312 struct dwarf_block
1313 {
1314 size_t size;
1315
1316 /* Valid only if SIZE is not zero. */
1317 const gdb_byte *data;
1318 };
1319
1320 #ifndef ATTR_ALLOC_CHUNK
1321 #define ATTR_ALLOC_CHUNK 4
1322 #endif
1323
1324 /* Allocate fields for structs, unions and enums in this size. */
1325 #ifndef DW_FIELD_ALLOC_CHUNK
1326 #define DW_FIELD_ALLOC_CHUNK 4
1327 #endif
1328
1329 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1330 but this would require a corresponding change in unpack_field_as_long
1331 and friends. */
1332 static int bits_per_byte = 8;
1333
1334 /* When reading a variant or variant part, we track a bit more
1335 information about the field, and store it in an object of this
1336 type. */
1337
1338 struct variant_field
1339 {
1340 /* If we see a DW_TAG_variant, then this will be the discriminant
1341 value. */
1342 ULONGEST discriminant_value;
1343 /* If we see a DW_TAG_variant, then this will be set if this is the
1344 default branch. */
1345 bool default_branch;
1346 /* While reading a DW_TAG_variant_part, this will be set if this
1347 field is the discriminant. */
1348 bool is_discriminant;
1349 };
1350
1351 struct nextfield
1352 {
1353 int accessibility = 0;
1354 int virtuality = 0;
1355 /* Extra information to describe a variant or variant part. */
1356 struct variant_field variant {};
1357 struct field field {};
1358 };
1359
1360 struct fnfieldlist
1361 {
1362 const char *name = nullptr;
1363 std::vector<struct fn_field> fnfields;
1364 };
1365
1366 /* The routines that read and process dies for a C struct or C++ class
1367 pass lists of data member fields and lists of member function fields
1368 in an instance of a field_info structure, as defined below. */
1369 struct field_info
1370 {
1371 /* List of data member and baseclasses fields. */
1372 std::vector<struct nextfield> fields;
1373 std::vector<struct nextfield> baseclasses;
1374
1375 /* Number of fields (including baseclasses). */
1376 int nfields = 0;
1377
1378 /* Set if the accesibility of one of the fields is not public. */
1379 int non_public_fields = 0;
1380
1381 /* Member function fieldlist array, contains name of possibly overloaded
1382 member function, number of overloaded member functions and a pointer
1383 to the head of the member function field chain. */
1384 std::vector<struct fnfieldlist> fnfieldlists;
1385
1386 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1387 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1388 std::vector<struct decl_field> typedef_field_list;
1389
1390 /* Nested types defined by this class and the number of elements in this
1391 list. */
1392 std::vector<struct decl_field> nested_types_list;
1393 };
1394
1395 /* One item on the queue of compilation units to read in full symbols
1396 for. */
1397 struct dwarf2_queue_item
1398 {
1399 struct dwarf2_per_cu_data *per_cu;
1400 enum language pretend_language;
1401 struct dwarf2_queue_item *next;
1402 };
1403
1404 /* The current queue. */
1405 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1406
1407 /* Loaded secondary compilation units are kept in memory until they
1408 have not been referenced for the processing of this many
1409 compilation units. Set this to zero to disable caching. Cache
1410 sizes of up to at least twenty will improve startup time for
1411 typical inter-CU-reference binaries, at an obvious memory cost. */
1412 static int dwarf_max_cache_age = 5;
1413 static void
1414 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1415 struct cmd_list_element *c, const char *value)
1416 {
1417 fprintf_filtered (file, _("The upper bound on the age of cached "
1418 "DWARF compilation units is %s.\n"),
1419 value);
1420 }
1421 \f
1422 /* local function prototypes */
1423
1424 static const char *get_section_name (const struct dwarf2_section_info *);
1425
1426 static const char *get_section_file_name (const struct dwarf2_section_info *);
1427
1428 static void dwarf2_find_base_address (struct die_info *die,
1429 struct dwarf2_cu *cu);
1430
1431 static struct partial_symtab *create_partial_symtab
1432 (struct dwarf2_per_cu_data *per_cu, const char *name);
1433
1434 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1435 const gdb_byte *info_ptr,
1436 struct die_info *type_unit_die,
1437 int has_children, void *data);
1438
1439 static void dwarf2_build_psymtabs_hard
1440 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1441
1442 static void scan_partial_symbols (struct partial_die_info *,
1443 CORE_ADDR *, CORE_ADDR *,
1444 int, struct dwarf2_cu *);
1445
1446 static void add_partial_symbol (struct partial_die_info *,
1447 struct dwarf2_cu *);
1448
1449 static void add_partial_namespace (struct partial_die_info *pdi,
1450 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1451 int set_addrmap, struct dwarf2_cu *cu);
1452
1453 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1454 CORE_ADDR *highpc, int set_addrmap,
1455 struct dwarf2_cu *cu);
1456
1457 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1458 struct dwarf2_cu *cu);
1459
1460 static void add_partial_subprogram (struct partial_die_info *pdi,
1461 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1462 int need_pc, struct dwarf2_cu *cu);
1463
1464 static void dwarf2_read_symtab (struct partial_symtab *,
1465 struct objfile *);
1466
1467 static void psymtab_to_symtab_1 (struct partial_symtab *);
1468
1469 static abbrev_table_up abbrev_table_read_table
1470 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1471 sect_offset);
1472
1473 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1474
1475 static struct partial_die_info *load_partial_dies
1476 (const struct die_reader_specs *, const gdb_byte *, int);
1477
1478 /* A pair of partial_die_info and compilation unit. */
1479 struct cu_partial_die_info
1480 {
1481 /* The compilation unit of the partial_die_info. */
1482 struct dwarf2_cu *cu;
1483 /* A partial_die_info. */
1484 struct partial_die_info *pdi;
1485
1486 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1487 : cu (cu),
1488 pdi (pdi)
1489 { /* Nothhing. */ }
1490
1491 private:
1492 cu_partial_die_info () = delete;
1493 };
1494
1495 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1496 struct dwarf2_cu *);
1497
1498 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1499 struct attribute *, struct attr_abbrev *,
1500 const gdb_byte *);
1501
1502 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1503
1504 static int read_1_signed_byte (bfd *, const gdb_byte *);
1505
1506 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1507
1508 /* Read the next three bytes (little-endian order) as an unsigned integer. */
1509 static unsigned int read_3_bytes (bfd *, const gdb_byte *);
1510
1511 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1512
1513 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1514
1515 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1516 unsigned int *);
1517
1518 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1519
1520 static LONGEST read_checked_initial_length_and_offset
1521 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1522 unsigned int *, unsigned int *);
1523
1524 static LONGEST read_offset (bfd *, const gdb_byte *,
1525 const struct comp_unit_head *,
1526 unsigned int *);
1527
1528 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1529
1530 static sect_offset read_abbrev_offset
1531 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1532 struct dwarf2_section_info *, sect_offset);
1533
1534 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1535
1536 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1537
1538 static const char *read_indirect_string
1539 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1540 const struct comp_unit_head *, unsigned int *);
1541
1542 static const char *read_indirect_line_string
1543 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1544 const struct comp_unit_head *, unsigned int *);
1545
1546 static const char *read_indirect_string_at_offset
1547 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1548 LONGEST str_offset);
1549
1550 static const char *read_indirect_string_from_dwz
1551 (struct objfile *objfile, struct dwz_file *, LONGEST);
1552
1553 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1554
1555 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1556 const gdb_byte *,
1557 unsigned int *);
1558
1559 static const char *read_str_index (const struct die_reader_specs *reader,
1560 ULONGEST str_index);
1561
1562 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1563
1564 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1565 struct dwarf2_cu *);
1566
1567 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1568 unsigned int);
1569
1570 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1571 struct dwarf2_cu *cu);
1572
1573 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1574
1575 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1576 struct dwarf2_cu *cu);
1577
1578 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1579
1580 static struct die_info *die_specification (struct die_info *die,
1581 struct dwarf2_cu **);
1582
1583 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1584 struct dwarf2_cu *cu);
1585
1586 static void dwarf_decode_lines (struct line_header *, const char *,
1587 struct dwarf2_cu *, struct partial_symtab *,
1588 CORE_ADDR, int decode_mapping);
1589
1590 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1591 const char *);
1592
1593 static struct symbol *new_symbol (struct die_info *, struct type *,
1594 struct dwarf2_cu *, struct symbol * = NULL);
1595
1596 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1597 struct dwarf2_cu *);
1598
1599 static void dwarf2_const_value_attr (const struct attribute *attr,
1600 struct type *type,
1601 const char *name,
1602 struct obstack *obstack,
1603 struct dwarf2_cu *cu, LONGEST *value,
1604 const gdb_byte **bytes,
1605 struct dwarf2_locexpr_baton **baton);
1606
1607 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1608
1609 static int need_gnat_info (struct dwarf2_cu *);
1610
1611 static struct type *die_descriptive_type (struct die_info *,
1612 struct dwarf2_cu *);
1613
1614 static void set_descriptive_type (struct type *, struct die_info *,
1615 struct dwarf2_cu *);
1616
1617 static struct type *die_containing_type (struct die_info *,
1618 struct dwarf2_cu *);
1619
1620 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1621 struct dwarf2_cu *);
1622
1623 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1624
1625 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1626
1627 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1628
1629 static char *typename_concat (struct obstack *obs, const char *prefix,
1630 const char *suffix, int physname,
1631 struct dwarf2_cu *cu);
1632
1633 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1634
1635 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1636
1637 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1638
1639 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1640
1641 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1642
1643 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1644
1645 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1646 struct dwarf2_cu *, struct partial_symtab *);
1647
1648 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1649 values. Keep the items ordered with increasing constraints compliance. */
1650 enum pc_bounds_kind
1651 {
1652 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1653 PC_BOUNDS_NOT_PRESENT,
1654
1655 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1656 were present but they do not form a valid range of PC addresses. */
1657 PC_BOUNDS_INVALID,
1658
1659 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1660 PC_BOUNDS_RANGES,
1661
1662 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1663 PC_BOUNDS_HIGH_LOW,
1664 };
1665
1666 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1667 CORE_ADDR *, CORE_ADDR *,
1668 struct dwarf2_cu *,
1669 struct partial_symtab *);
1670
1671 static void get_scope_pc_bounds (struct die_info *,
1672 CORE_ADDR *, CORE_ADDR *,
1673 struct dwarf2_cu *);
1674
1675 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1676 CORE_ADDR, struct dwarf2_cu *);
1677
1678 static void dwarf2_add_field (struct field_info *, struct die_info *,
1679 struct dwarf2_cu *);
1680
1681 static void dwarf2_attach_fields_to_type (struct field_info *,
1682 struct type *, struct dwarf2_cu *);
1683
1684 static void dwarf2_add_member_fn (struct field_info *,
1685 struct die_info *, struct type *,
1686 struct dwarf2_cu *);
1687
1688 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1689 struct type *,
1690 struct dwarf2_cu *);
1691
1692 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1693
1694 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1695
1696 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1697
1698 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1699
1700 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1701
1702 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1703
1704 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1705
1706 static struct type *read_module_type (struct die_info *die,
1707 struct dwarf2_cu *cu);
1708
1709 static const char *namespace_name (struct die_info *die,
1710 int *is_anonymous, struct dwarf2_cu *);
1711
1712 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1713
1714 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1715
1716 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1717 struct dwarf2_cu *);
1718
1719 static struct die_info *read_die_and_siblings_1
1720 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1721 struct die_info *);
1722
1723 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1724 const gdb_byte *info_ptr,
1725 const gdb_byte **new_info_ptr,
1726 struct die_info *parent);
1727
1728 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1729 struct die_info **, const gdb_byte *,
1730 int *, int);
1731
1732 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1733 struct die_info **, const gdb_byte *,
1734 int *);
1735
1736 static void process_die (struct die_info *, struct dwarf2_cu *);
1737
1738 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1739 struct obstack *);
1740
1741 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1742
1743 static const char *dwarf2_full_name (const char *name,
1744 struct die_info *die,
1745 struct dwarf2_cu *cu);
1746
1747 static const char *dwarf2_physname (const char *name, struct die_info *die,
1748 struct dwarf2_cu *cu);
1749
1750 static struct die_info *dwarf2_extension (struct die_info *die,
1751 struct dwarf2_cu **);
1752
1753 static const char *dwarf_tag_name (unsigned int);
1754
1755 static const char *dwarf_attr_name (unsigned int);
1756
1757 static const char *dwarf_unit_type_name (int unit_type);
1758
1759 static const char *dwarf_form_name (unsigned int);
1760
1761 static const char *dwarf_bool_name (unsigned int);
1762
1763 static const char *dwarf_type_encoding_name (unsigned int);
1764
1765 static struct die_info *sibling_die (struct die_info *);
1766
1767 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1768
1769 static void dump_die_for_error (struct die_info *);
1770
1771 static void dump_die_1 (struct ui_file *, int level, int max_level,
1772 struct die_info *);
1773
1774 /*static*/ void dump_die (struct die_info *, int max_level);
1775
1776 static void store_in_ref_table (struct die_info *,
1777 struct dwarf2_cu *);
1778
1779 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1780
1781 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1782
1783 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1784 const struct attribute *,
1785 struct dwarf2_cu **);
1786
1787 static struct die_info *follow_die_ref (struct die_info *,
1788 const struct attribute *,
1789 struct dwarf2_cu **);
1790
1791 static struct die_info *follow_die_sig (struct die_info *,
1792 const struct attribute *,
1793 struct dwarf2_cu **);
1794
1795 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1796 struct dwarf2_cu *);
1797
1798 static struct type *get_DW_AT_signature_type (struct die_info *,
1799 const struct attribute *,
1800 struct dwarf2_cu *);
1801
1802 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1803
1804 static void read_signatured_type (struct signatured_type *);
1805
1806 static int attr_to_dynamic_prop (const struct attribute *attr,
1807 struct die_info *die, struct dwarf2_cu *cu,
1808 struct dynamic_prop *prop, struct type *type);
1809
1810 /* memory allocation interface */
1811
1812 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1813
1814 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1815
1816 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1817
1818 static int attr_form_is_block (const struct attribute *);
1819
1820 static int attr_form_is_section_offset (const struct attribute *);
1821
1822 static int attr_form_is_constant (const struct attribute *);
1823
1824 static int attr_form_is_ref (const struct attribute *);
1825
1826 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1827 struct dwarf2_loclist_baton *baton,
1828 const struct attribute *attr);
1829
1830 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1831 struct symbol *sym,
1832 struct dwarf2_cu *cu,
1833 int is_block);
1834
1835 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1836 const gdb_byte *info_ptr,
1837 struct abbrev_info *abbrev);
1838
1839 static hashval_t partial_die_hash (const void *item);
1840
1841 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1842
1843 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1844 (sect_offset sect_off, unsigned int offset_in_dwz,
1845 struct dwarf2_per_objfile *dwarf2_per_objfile);
1846
1847 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1848 struct die_info *comp_unit_die,
1849 enum language pretend_language);
1850
1851 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1852
1853 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1854
1855 static struct type *set_die_type (struct die_info *, struct type *,
1856 struct dwarf2_cu *);
1857
1858 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1859
1860 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1861
1862 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1863 enum language);
1864
1865 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1866 enum language);
1867
1868 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1869 enum language);
1870
1871 static void dwarf2_add_dependence (struct dwarf2_cu *,
1872 struct dwarf2_per_cu_data *);
1873
1874 static void dwarf2_mark (struct dwarf2_cu *);
1875
1876 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1877
1878 static struct type *get_die_type_at_offset (sect_offset,
1879 struct dwarf2_per_cu_data *);
1880
1881 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1882
1883 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1884 enum language pretend_language);
1885
1886 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1887
1888 static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1889 static struct type *dwarf2_per_cu_addr_sized_int_type
1890 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
1891
1892 /* Class, the destructor of which frees all allocated queue entries. This
1893 will only have work to do if an error was thrown while processing the
1894 dwarf. If no error was thrown then the queue entries should have all
1895 been processed, and freed, as we went along. */
1896
1897 class dwarf2_queue_guard
1898 {
1899 public:
1900 dwarf2_queue_guard () = default;
1901
1902 /* Free any entries remaining on the queue. There should only be
1903 entries left if we hit an error while processing the dwarf. */
1904 ~dwarf2_queue_guard ()
1905 {
1906 struct dwarf2_queue_item *item, *last;
1907
1908 item = dwarf2_queue;
1909 while (item)
1910 {
1911 /* Anything still marked queued is likely to be in an
1912 inconsistent state, so discard it. */
1913 if (item->per_cu->queued)
1914 {
1915 if (item->per_cu->cu != NULL)
1916 free_one_cached_comp_unit (item->per_cu);
1917 item->per_cu->queued = 0;
1918 }
1919
1920 last = item;
1921 item = item->next;
1922 xfree (last);
1923 }
1924
1925 dwarf2_queue = dwarf2_queue_tail = NULL;
1926 }
1927 };
1928
1929 /* The return type of find_file_and_directory. Note, the enclosed
1930 string pointers are only valid while this object is valid. */
1931
1932 struct file_and_directory
1933 {
1934 /* The filename. This is never NULL. */
1935 const char *name;
1936
1937 /* The compilation directory. NULL if not known. If we needed to
1938 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1939 points directly to the DW_AT_comp_dir string attribute owned by
1940 the obstack that owns the DIE. */
1941 const char *comp_dir;
1942
1943 /* If we needed to build a new string for comp_dir, this is what
1944 owns the storage. */
1945 std::string comp_dir_storage;
1946 };
1947
1948 static file_and_directory find_file_and_directory (struct die_info *die,
1949 struct dwarf2_cu *cu);
1950
1951 static char *file_full_name (int file, struct line_header *lh,
1952 const char *comp_dir);
1953
1954 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1955 enum class rcuh_kind { COMPILE, TYPE };
1956
1957 static const gdb_byte *read_and_check_comp_unit_head
1958 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1959 struct comp_unit_head *header,
1960 struct dwarf2_section_info *section,
1961 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1962 rcuh_kind section_kind);
1963
1964 static void init_cutu_and_read_dies
1965 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1966 int use_existing_cu, int keep, bool skip_partial,
1967 die_reader_func_ftype *die_reader_func, void *data);
1968
1969 static void init_cutu_and_read_dies_simple
1970 (struct dwarf2_per_cu_data *this_cu,
1971 die_reader_func_ftype *die_reader_func, void *data);
1972
1973 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1974
1975 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1976
1977 static struct dwo_unit *lookup_dwo_unit_in_dwp
1978 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1979 struct dwp_file *dwp_file, const char *comp_dir,
1980 ULONGEST signature, int is_debug_types);
1981
1982 static struct dwp_file *get_dwp_file
1983 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1984
1985 static struct dwo_unit *lookup_dwo_comp_unit
1986 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1987
1988 static struct dwo_unit *lookup_dwo_type_unit
1989 (struct signatured_type *, const char *, const char *);
1990
1991 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1992
1993 /* A unique pointer to a dwo_file. */
1994
1995 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1996
1997 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1998
1999 static void check_producer (struct dwarf2_cu *cu);
2000
2001 static void free_line_header_voidp (void *arg);
2002 \f
2003 /* Various complaints about symbol reading that don't abort the process. */
2004
2005 static void
2006 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2007 {
2008 complaint (_("statement list doesn't fit in .debug_line section"));
2009 }
2010
2011 static void
2012 dwarf2_debug_line_missing_file_complaint (void)
2013 {
2014 complaint (_(".debug_line section has line data without a file"));
2015 }
2016
2017 static void
2018 dwarf2_debug_line_missing_end_sequence_complaint (void)
2019 {
2020 complaint (_(".debug_line section has line "
2021 "program sequence without an end"));
2022 }
2023
2024 static void
2025 dwarf2_complex_location_expr_complaint (void)
2026 {
2027 complaint (_("location expression too complex"));
2028 }
2029
2030 static void
2031 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2032 int arg3)
2033 {
2034 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
2035 arg1, arg2, arg3);
2036 }
2037
2038 static void
2039 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2040 {
2041 complaint (_("debug info runs off end of %s section"
2042 " [in module %s]"),
2043 get_section_name (section),
2044 get_section_file_name (section));
2045 }
2046
2047 static void
2048 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2049 {
2050 complaint (_("macro debug info contains a "
2051 "malformed macro definition:\n`%s'"),
2052 arg1);
2053 }
2054
2055 static void
2056 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2057 {
2058 complaint (_("invalid attribute class or form for '%s' in '%s'"),
2059 arg1, arg2);
2060 }
2061
2062 /* Hash function for line_header_hash. */
2063
2064 static hashval_t
2065 line_header_hash (const struct line_header *ofs)
2066 {
2067 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2068 }
2069
2070 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2071
2072 static hashval_t
2073 line_header_hash_voidp (const void *item)
2074 {
2075 const struct line_header *ofs = (const struct line_header *) item;
2076
2077 return line_header_hash (ofs);
2078 }
2079
2080 /* Equality function for line_header_hash. */
2081
2082 static int
2083 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2084 {
2085 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2086 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2087
2088 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2089 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2090 }
2091
2092 \f
2093
2094 /* Read the given attribute value as an address, taking the attribute's
2095 form into account. */
2096
2097 static CORE_ADDR
2098 attr_value_as_address (struct attribute *attr)
2099 {
2100 CORE_ADDR addr;
2101
2102 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2103 && attr->form != DW_FORM_GNU_addr_index)
2104 {
2105 /* Aside from a few clearly defined exceptions, attributes that
2106 contain an address must always be in DW_FORM_addr form.
2107 Unfortunately, some compilers happen to be violating this
2108 requirement by encoding addresses using other forms, such
2109 as DW_FORM_data4 for example. For those broken compilers,
2110 we try to do our best, without any guarantee of success,
2111 to interpret the address correctly. It would also be nice
2112 to generate a complaint, but that would require us to maintain
2113 a list of legitimate cases where a non-address form is allowed,
2114 as well as update callers to pass in at least the CU's DWARF
2115 version. This is more overhead than what we're willing to
2116 expand for a pretty rare case. */
2117 addr = DW_UNSND (attr);
2118 }
2119 else
2120 addr = DW_ADDR (attr);
2121
2122 return addr;
2123 }
2124
2125 /* See declaration. */
2126
2127 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2128 const dwarf2_debug_sections *names,
2129 bool can_copy_)
2130 : objfile (objfile_),
2131 can_copy (can_copy_)
2132 {
2133 if (names == NULL)
2134 names = &dwarf2_elf_names;
2135
2136 bfd *obfd = objfile->obfd;
2137
2138 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2139 locate_sections (obfd, sec, *names);
2140 }
2141
2142 dwarf2_per_objfile::~dwarf2_per_objfile ()
2143 {
2144 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2145 free_cached_comp_units ();
2146
2147 if (quick_file_names_table)
2148 htab_delete (quick_file_names_table);
2149
2150 if (line_header_hash)
2151 htab_delete (line_header_hash);
2152
2153 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2154 per_cu->imported_symtabs_free ();
2155
2156 for (signatured_type *sig_type : all_type_units)
2157 sig_type->per_cu.imported_symtabs_free ();
2158
2159 /* Everything else should be on the objfile obstack. */
2160 }
2161
2162 /* See declaration. */
2163
2164 void
2165 dwarf2_per_objfile::free_cached_comp_units ()
2166 {
2167 dwarf2_per_cu_data *per_cu = read_in_chain;
2168 dwarf2_per_cu_data **last_chain = &read_in_chain;
2169 while (per_cu != NULL)
2170 {
2171 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2172
2173 delete per_cu->cu;
2174 *last_chain = next_cu;
2175 per_cu = next_cu;
2176 }
2177 }
2178
2179 /* A helper class that calls free_cached_comp_units on
2180 destruction. */
2181
2182 class free_cached_comp_units
2183 {
2184 public:
2185
2186 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2187 : m_per_objfile (per_objfile)
2188 {
2189 }
2190
2191 ~free_cached_comp_units ()
2192 {
2193 m_per_objfile->free_cached_comp_units ();
2194 }
2195
2196 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2197
2198 private:
2199
2200 dwarf2_per_objfile *m_per_objfile;
2201 };
2202
2203 /* Try to locate the sections we need for DWARF 2 debugging
2204 information and return true if we have enough to do something.
2205 NAMES points to the dwarf2 section names, or is NULL if the standard
2206 ELF names are used. CAN_COPY is true for formats where symbol
2207 interposition is possible and so symbol values must follow copy
2208 relocation rules. */
2209
2210 int
2211 dwarf2_has_info (struct objfile *objfile,
2212 const struct dwarf2_debug_sections *names,
2213 bool can_copy)
2214 {
2215 if (objfile->flags & OBJF_READNEVER)
2216 return 0;
2217
2218 struct dwarf2_per_objfile *dwarf2_per_objfile
2219 = get_dwarf2_per_objfile (objfile);
2220
2221 if (dwarf2_per_objfile == NULL)
2222 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
2223 names,
2224 can_copy);
2225
2226 return (!dwarf2_per_objfile->info.is_virtual
2227 && dwarf2_per_objfile->info.s.section != NULL
2228 && !dwarf2_per_objfile->abbrev.is_virtual
2229 && dwarf2_per_objfile->abbrev.s.section != NULL);
2230 }
2231
2232 /* Return the containing section of virtual section SECTION. */
2233
2234 static struct dwarf2_section_info *
2235 get_containing_section (const struct dwarf2_section_info *section)
2236 {
2237 gdb_assert (section->is_virtual);
2238 return section->s.containing_section;
2239 }
2240
2241 /* Return the bfd owner of SECTION. */
2242
2243 static struct bfd *
2244 get_section_bfd_owner (const struct dwarf2_section_info *section)
2245 {
2246 if (section->is_virtual)
2247 {
2248 section = get_containing_section (section);
2249 gdb_assert (!section->is_virtual);
2250 }
2251 return section->s.section->owner;
2252 }
2253
2254 /* Return the bfd section of SECTION.
2255 Returns NULL if the section is not present. */
2256
2257 static asection *
2258 get_section_bfd_section (const struct dwarf2_section_info *section)
2259 {
2260 if (section->is_virtual)
2261 {
2262 section = get_containing_section (section);
2263 gdb_assert (!section->is_virtual);
2264 }
2265 return section->s.section;
2266 }
2267
2268 /* Return the name of SECTION. */
2269
2270 static const char *
2271 get_section_name (const struct dwarf2_section_info *section)
2272 {
2273 asection *sectp = get_section_bfd_section (section);
2274
2275 gdb_assert (sectp != NULL);
2276 return bfd_section_name (sectp);
2277 }
2278
2279 /* Return the name of the file SECTION is in. */
2280
2281 static const char *
2282 get_section_file_name (const struct dwarf2_section_info *section)
2283 {
2284 bfd *abfd = get_section_bfd_owner (section);
2285
2286 return bfd_get_filename (abfd);
2287 }
2288
2289 /* Return the id of SECTION.
2290 Returns 0 if SECTION doesn't exist. */
2291
2292 static int
2293 get_section_id (const struct dwarf2_section_info *section)
2294 {
2295 asection *sectp = get_section_bfd_section (section);
2296
2297 if (sectp == NULL)
2298 return 0;
2299 return sectp->id;
2300 }
2301
2302 /* Return the flags of SECTION.
2303 SECTION (or containing section if this is a virtual section) must exist. */
2304
2305 static int
2306 get_section_flags (const struct dwarf2_section_info *section)
2307 {
2308 asection *sectp = get_section_bfd_section (section);
2309
2310 gdb_assert (sectp != NULL);
2311 return bfd_section_flags (sectp);
2312 }
2313
2314 /* When loading sections, we look either for uncompressed section or for
2315 compressed section names. */
2316
2317 static int
2318 section_is_p (const char *section_name,
2319 const struct dwarf2_section_names *names)
2320 {
2321 if (names->normal != NULL
2322 && strcmp (section_name, names->normal) == 0)
2323 return 1;
2324 if (names->compressed != NULL
2325 && strcmp (section_name, names->compressed) == 0)
2326 return 1;
2327 return 0;
2328 }
2329
2330 /* See declaration. */
2331
2332 void
2333 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2334 const dwarf2_debug_sections &names)
2335 {
2336 flagword aflag = bfd_section_flags (sectp);
2337
2338 if ((aflag & SEC_HAS_CONTENTS) == 0)
2339 {
2340 }
2341 else if (section_is_p (sectp->name, &names.info))
2342 {
2343 this->info.s.section = sectp;
2344 this->info.size = bfd_section_size (sectp);
2345 }
2346 else if (section_is_p (sectp->name, &names.abbrev))
2347 {
2348 this->abbrev.s.section = sectp;
2349 this->abbrev.size = bfd_section_size (sectp);
2350 }
2351 else if (section_is_p (sectp->name, &names.line))
2352 {
2353 this->line.s.section = sectp;
2354 this->line.size = bfd_section_size (sectp);
2355 }
2356 else if (section_is_p (sectp->name, &names.loc))
2357 {
2358 this->loc.s.section = sectp;
2359 this->loc.size = bfd_section_size (sectp);
2360 }
2361 else if (section_is_p (sectp->name, &names.loclists))
2362 {
2363 this->loclists.s.section = sectp;
2364 this->loclists.size = bfd_section_size (sectp);
2365 }
2366 else if (section_is_p (sectp->name, &names.macinfo))
2367 {
2368 this->macinfo.s.section = sectp;
2369 this->macinfo.size = bfd_section_size (sectp);
2370 }
2371 else if (section_is_p (sectp->name, &names.macro))
2372 {
2373 this->macro.s.section = sectp;
2374 this->macro.size = bfd_section_size (sectp);
2375 }
2376 else if (section_is_p (sectp->name, &names.str))
2377 {
2378 this->str.s.section = sectp;
2379 this->str.size = bfd_section_size (sectp);
2380 }
2381 else if (section_is_p (sectp->name, &names.line_str))
2382 {
2383 this->line_str.s.section = sectp;
2384 this->line_str.size = bfd_section_size (sectp);
2385 }
2386 else if (section_is_p (sectp->name, &names.addr))
2387 {
2388 this->addr.s.section = sectp;
2389 this->addr.size = bfd_section_size (sectp);
2390 }
2391 else if (section_is_p (sectp->name, &names.frame))
2392 {
2393 this->frame.s.section = sectp;
2394 this->frame.size = bfd_section_size (sectp);
2395 }
2396 else if (section_is_p (sectp->name, &names.eh_frame))
2397 {
2398 this->eh_frame.s.section = sectp;
2399 this->eh_frame.size = bfd_section_size (sectp);
2400 }
2401 else if (section_is_p (sectp->name, &names.ranges))
2402 {
2403 this->ranges.s.section = sectp;
2404 this->ranges.size = bfd_section_size (sectp);
2405 }
2406 else if (section_is_p (sectp->name, &names.rnglists))
2407 {
2408 this->rnglists.s.section = sectp;
2409 this->rnglists.size = bfd_section_size (sectp);
2410 }
2411 else if (section_is_p (sectp->name, &names.types))
2412 {
2413 struct dwarf2_section_info type_section;
2414
2415 memset (&type_section, 0, sizeof (type_section));
2416 type_section.s.section = sectp;
2417 type_section.size = bfd_section_size (sectp);
2418
2419 this->types.push_back (type_section);
2420 }
2421 else if (section_is_p (sectp->name, &names.gdb_index))
2422 {
2423 this->gdb_index.s.section = sectp;
2424 this->gdb_index.size = bfd_section_size (sectp);
2425 }
2426 else if (section_is_p (sectp->name, &names.debug_names))
2427 {
2428 this->debug_names.s.section = sectp;
2429 this->debug_names.size = bfd_section_size (sectp);
2430 }
2431 else if (section_is_p (sectp->name, &names.debug_aranges))
2432 {
2433 this->debug_aranges.s.section = sectp;
2434 this->debug_aranges.size = bfd_section_size (sectp);
2435 }
2436
2437 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2438 && bfd_section_vma (sectp) == 0)
2439 this->has_section_at_zero = true;
2440 }
2441
2442 /* A helper function that decides whether a section is empty,
2443 or not present. */
2444
2445 static int
2446 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2447 {
2448 if (section->is_virtual)
2449 return section->size == 0;
2450 return section->s.section == NULL || section->size == 0;
2451 }
2452
2453 /* See dwarf2read.h. */
2454
2455 void
2456 dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
2457 {
2458 asection *sectp;
2459 bfd *abfd;
2460 gdb_byte *buf, *retbuf;
2461
2462 if (info->readin)
2463 return;
2464 info->buffer = NULL;
2465 info->readin = true;
2466
2467 if (dwarf2_section_empty_p (info))
2468 return;
2469
2470 sectp = get_section_bfd_section (info);
2471
2472 /* If this is a virtual section we need to read in the real one first. */
2473 if (info->is_virtual)
2474 {
2475 struct dwarf2_section_info *containing_section =
2476 get_containing_section (info);
2477
2478 gdb_assert (sectp != NULL);
2479 if ((sectp->flags & SEC_RELOC) != 0)
2480 {
2481 error (_("Dwarf Error: DWP format V2 with relocations is not"
2482 " supported in section %s [in module %s]"),
2483 get_section_name (info), get_section_file_name (info));
2484 }
2485 dwarf2_read_section (objfile, containing_section);
2486 /* Other code should have already caught virtual sections that don't
2487 fit. */
2488 gdb_assert (info->virtual_offset + info->size
2489 <= containing_section->size);
2490 /* If the real section is empty or there was a problem reading the
2491 section we shouldn't get here. */
2492 gdb_assert (containing_section->buffer != NULL);
2493 info->buffer = containing_section->buffer + info->virtual_offset;
2494 return;
2495 }
2496
2497 /* If the section has relocations, we must read it ourselves.
2498 Otherwise we attach it to the BFD. */
2499 if ((sectp->flags & SEC_RELOC) == 0)
2500 {
2501 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2502 return;
2503 }
2504
2505 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2506 info->buffer = buf;
2507
2508 /* When debugging .o files, we may need to apply relocations; see
2509 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2510 We never compress sections in .o files, so we only need to
2511 try this when the section is not compressed. */
2512 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2513 if (retbuf != NULL)
2514 {
2515 info->buffer = retbuf;
2516 return;
2517 }
2518
2519 abfd = get_section_bfd_owner (info);
2520 gdb_assert (abfd != NULL);
2521
2522 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2523 || bfd_bread (buf, info->size, abfd) != info->size)
2524 {
2525 error (_("Dwarf Error: Can't read DWARF data"
2526 " in section %s [in module %s]"),
2527 bfd_section_name (sectp), bfd_get_filename (abfd));
2528 }
2529 }
2530
2531 /* A helper function that returns the size of a section in a safe way.
2532 If you are positive that the section has been read before using the
2533 size, then it is safe to refer to the dwarf2_section_info object's
2534 "size" field directly. In other cases, you must call this
2535 function, because for compressed sections the size field is not set
2536 correctly until the section has been read. */
2537
2538 static bfd_size_type
2539 dwarf2_section_size (struct objfile *objfile,
2540 struct dwarf2_section_info *info)
2541 {
2542 if (!info->readin)
2543 dwarf2_read_section (objfile, info);
2544 return info->size;
2545 }
2546
2547 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2548 SECTION_NAME. */
2549
2550 void
2551 dwarf2_get_section_info (struct objfile *objfile,
2552 enum dwarf2_section_enum sect,
2553 asection **sectp, const gdb_byte **bufp,
2554 bfd_size_type *sizep)
2555 {
2556 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2557 struct dwarf2_section_info *info;
2558
2559 /* We may see an objfile without any DWARF, in which case we just
2560 return nothing. */
2561 if (data == NULL)
2562 {
2563 *sectp = NULL;
2564 *bufp = NULL;
2565 *sizep = 0;
2566 return;
2567 }
2568 switch (sect)
2569 {
2570 case DWARF2_DEBUG_FRAME:
2571 info = &data->frame;
2572 break;
2573 case DWARF2_EH_FRAME:
2574 info = &data->eh_frame;
2575 break;
2576 default:
2577 gdb_assert_not_reached ("unexpected section");
2578 }
2579
2580 dwarf2_read_section (objfile, info);
2581
2582 *sectp = get_section_bfd_section (info);
2583 *bufp = info->buffer;
2584 *sizep = info->size;
2585 }
2586
2587 /* A helper function to find the sections for a .dwz file. */
2588
2589 static void
2590 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2591 {
2592 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2593
2594 /* Note that we only support the standard ELF names, because .dwz
2595 is ELF-only (at the time of writing). */
2596 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2597 {
2598 dwz_file->abbrev.s.section = sectp;
2599 dwz_file->abbrev.size = bfd_section_size (sectp);
2600 }
2601 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2602 {
2603 dwz_file->info.s.section = sectp;
2604 dwz_file->info.size = bfd_section_size (sectp);
2605 }
2606 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2607 {
2608 dwz_file->str.s.section = sectp;
2609 dwz_file->str.size = bfd_section_size (sectp);
2610 }
2611 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2612 {
2613 dwz_file->line.s.section = sectp;
2614 dwz_file->line.size = bfd_section_size (sectp);
2615 }
2616 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2617 {
2618 dwz_file->macro.s.section = sectp;
2619 dwz_file->macro.size = bfd_section_size (sectp);
2620 }
2621 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2622 {
2623 dwz_file->gdb_index.s.section = sectp;
2624 dwz_file->gdb_index.size = bfd_section_size (sectp);
2625 }
2626 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2627 {
2628 dwz_file->debug_names.s.section = sectp;
2629 dwz_file->debug_names.size = bfd_section_size (sectp);
2630 }
2631 }
2632
2633 /* See dwarf2read.h. */
2634
2635 struct dwz_file *
2636 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2637 {
2638 const char *filename;
2639 bfd_size_type buildid_len_arg;
2640 size_t buildid_len;
2641 bfd_byte *buildid;
2642
2643 if (dwarf2_per_objfile->dwz_file != NULL)
2644 return dwarf2_per_objfile->dwz_file.get ();
2645
2646 bfd_set_error (bfd_error_no_error);
2647 gdb::unique_xmalloc_ptr<char> data
2648 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2649 &buildid_len_arg, &buildid));
2650 if (data == NULL)
2651 {
2652 if (bfd_get_error () == bfd_error_no_error)
2653 return NULL;
2654 error (_("could not read '.gnu_debugaltlink' section: %s"),
2655 bfd_errmsg (bfd_get_error ()));
2656 }
2657
2658 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2659
2660 buildid_len = (size_t) buildid_len_arg;
2661
2662 filename = data.get ();
2663
2664 std::string abs_storage;
2665 if (!IS_ABSOLUTE_PATH (filename))
2666 {
2667 gdb::unique_xmalloc_ptr<char> abs
2668 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2669
2670 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2671 filename = abs_storage.c_str ();
2672 }
2673
2674 /* First try the file name given in the section. If that doesn't
2675 work, try to use the build-id instead. */
2676 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2677 if (dwz_bfd != NULL)
2678 {
2679 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2680 dwz_bfd.reset (nullptr);
2681 }
2682
2683 if (dwz_bfd == NULL)
2684 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2685
2686 if (dwz_bfd == NULL)
2687 error (_("could not find '.gnu_debugaltlink' file for %s"),
2688 objfile_name (dwarf2_per_objfile->objfile));
2689
2690 std::unique_ptr<struct dwz_file> result
2691 (new struct dwz_file (std::move (dwz_bfd)));
2692
2693 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2694 result.get ());
2695
2696 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2697 result->dwz_bfd.get ());
2698 dwarf2_per_objfile->dwz_file = std::move (result);
2699 return dwarf2_per_objfile->dwz_file.get ();
2700 }
2701 \f
2702 /* DWARF quick_symbols_functions support. */
2703
2704 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2705 unique line tables, so we maintain a separate table of all .debug_line
2706 derived entries to support the sharing.
2707 All the quick functions need is the list of file names. We discard the
2708 line_header when we're done and don't need to record it here. */
2709 struct quick_file_names
2710 {
2711 /* The data used to construct the hash key. */
2712 struct stmt_list_hash hash;
2713
2714 /* The number of entries in file_names, real_names. */
2715 unsigned int num_file_names;
2716
2717 /* The file names from the line table, after being run through
2718 file_full_name. */
2719 const char **file_names;
2720
2721 /* The file names from the line table after being run through
2722 gdb_realpath. These are computed lazily. */
2723 const char **real_names;
2724 };
2725
2726 /* When using the index (and thus not using psymtabs), each CU has an
2727 object of this type. This is used to hold information needed by
2728 the various "quick" methods. */
2729 struct dwarf2_per_cu_quick_data
2730 {
2731 /* The file table. This can be NULL if there was no file table
2732 or it's currently not read in.
2733 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2734 struct quick_file_names *file_names;
2735
2736 /* The corresponding symbol table. This is NULL if symbols for this
2737 CU have not yet been read. */
2738 struct compunit_symtab *compunit_symtab;
2739
2740 /* A temporary mark bit used when iterating over all CUs in
2741 expand_symtabs_matching. */
2742 unsigned int mark : 1;
2743
2744 /* True if we've tried to read the file table and found there isn't one.
2745 There will be no point in trying to read it again next time. */
2746 unsigned int no_file_data : 1;
2747 };
2748
2749 /* Utility hash function for a stmt_list_hash. */
2750
2751 static hashval_t
2752 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2753 {
2754 hashval_t v = 0;
2755
2756 if (stmt_list_hash->dwo_unit != NULL)
2757 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2758 v += to_underlying (stmt_list_hash->line_sect_off);
2759 return v;
2760 }
2761
2762 /* Utility equality function for a stmt_list_hash. */
2763
2764 static int
2765 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2766 const struct stmt_list_hash *rhs)
2767 {
2768 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2769 return 0;
2770 if (lhs->dwo_unit != NULL
2771 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2772 return 0;
2773
2774 return lhs->line_sect_off == rhs->line_sect_off;
2775 }
2776
2777 /* Hash function for a quick_file_names. */
2778
2779 static hashval_t
2780 hash_file_name_entry (const void *e)
2781 {
2782 const struct quick_file_names *file_data
2783 = (const struct quick_file_names *) e;
2784
2785 return hash_stmt_list_entry (&file_data->hash);
2786 }
2787
2788 /* Equality function for a quick_file_names. */
2789
2790 static int
2791 eq_file_name_entry (const void *a, const void *b)
2792 {
2793 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2794 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2795
2796 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2797 }
2798
2799 /* Delete function for a quick_file_names. */
2800
2801 static void
2802 delete_file_name_entry (void *e)
2803 {
2804 struct quick_file_names *file_data = (struct quick_file_names *) e;
2805 int i;
2806
2807 for (i = 0; i < file_data->num_file_names; ++i)
2808 {
2809 xfree ((void*) file_data->file_names[i]);
2810 if (file_data->real_names)
2811 xfree ((void*) file_data->real_names[i]);
2812 }
2813
2814 /* The space for the struct itself lives on objfile_obstack,
2815 so we don't free it here. */
2816 }
2817
2818 /* Create a quick_file_names hash table. */
2819
2820 static htab_t
2821 create_quick_file_names_table (unsigned int nr_initial_entries)
2822 {
2823 return htab_create_alloc (nr_initial_entries,
2824 hash_file_name_entry, eq_file_name_entry,
2825 delete_file_name_entry, xcalloc, xfree);
2826 }
2827
2828 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2829 have to be created afterwards. You should call age_cached_comp_units after
2830 processing PER_CU->CU. dw2_setup must have been already called. */
2831
2832 static void
2833 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2834 {
2835 if (per_cu->is_debug_types)
2836 load_full_type_unit (per_cu);
2837 else
2838 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2839
2840 if (per_cu->cu == NULL)
2841 return; /* Dummy CU. */
2842
2843 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2844 }
2845
2846 /* Read in the symbols for PER_CU. */
2847
2848 static void
2849 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2850 {
2851 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2852
2853 /* Skip type_unit_groups, reading the type units they contain
2854 is handled elsewhere. */
2855 if (IS_TYPE_UNIT_GROUP (per_cu))
2856 return;
2857
2858 /* The destructor of dwarf2_queue_guard frees any entries left on
2859 the queue. After this point we're guaranteed to leave this function
2860 with the dwarf queue empty. */
2861 dwarf2_queue_guard q_guard;
2862
2863 if (dwarf2_per_objfile->using_index
2864 ? per_cu->v.quick->compunit_symtab == NULL
2865 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2866 {
2867 queue_comp_unit (per_cu, language_minimal);
2868 load_cu (per_cu, skip_partial);
2869
2870 /* If we just loaded a CU from a DWO, and we're working with an index
2871 that may badly handle TUs, load all the TUs in that DWO as well.
2872 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2873 if (!per_cu->is_debug_types
2874 && per_cu->cu != NULL
2875 && per_cu->cu->dwo_unit != NULL
2876 && dwarf2_per_objfile->index_table != NULL
2877 && dwarf2_per_objfile->index_table->version <= 7
2878 /* DWP files aren't supported yet. */
2879 && get_dwp_file (dwarf2_per_objfile) == NULL)
2880 queue_and_load_all_dwo_tus (per_cu);
2881 }
2882
2883 process_queue (dwarf2_per_objfile);
2884
2885 /* Age the cache, releasing compilation units that have not
2886 been used recently. */
2887 age_cached_comp_units (dwarf2_per_objfile);
2888 }
2889
2890 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2891 the objfile from which this CU came. Returns the resulting symbol
2892 table. */
2893
2894 static struct compunit_symtab *
2895 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2896 {
2897 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2898
2899 gdb_assert (dwarf2_per_objfile->using_index);
2900 if (!per_cu->v.quick->compunit_symtab)
2901 {
2902 free_cached_comp_units freer (dwarf2_per_objfile);
2903 scoped_restore decrementer = increment_reading_symtab ();
2904 dw2_do_instantiate_symtab (per_cu, skip_partial);
2905 process_cu_includes (dwarf2_per_objfile);
2906 }
2907
2908 return per_cu->v.quick->compunit_symtab;
2909 }
2910
2911 /* See declaration. */
2912
2913 dwarf2_per_cu_data *
2914 dwarf2_per_objfile::get_cutu (int index)
2915 {
2916 if (index >= this->all_comp_units.size ())
2917 {
2918 index -= this->all_comp_units.size ();
2919 gdb_assert (index < this->all_type_units.size ());
2920 return &this->all_type_units[index]->per_cu;
2921 }
2922
2923 return this->all_comp_units[index];
2924 }
2925
2926 /* See declaration. */
2927
2928 dwarf2_per_cu_data *
2929 dwarf2_per_objfile::get_cu (int index)
2930 {
2931 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2932
2933 return this->all_comp_units[index];
2934 }
2935
2936 /* See declaration. */
2937
2938 signatured_type *
2939 dwarf2_per_objfile::get_tu (int index)
2940 {
2941 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2942
2943 return this->all_type_units[index];
2944 }
2945
2946 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2947 objfile_obstack, and constructed with the specified field
2948 values. */
2949
2950 static dwarf2_per_cu_data *
2951 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2952 struct dwarf2_section_info *section,
2953 int is_dwz,
2954 sect_offset sect_off, ULONGEST length)
2955 {
2956 struct objfile *objfile = dwarf2_per_objfile->objfile;
2957 dwarf2_per_cu_data *the_cu
2958 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2959 struct dwarf2_per_cu_data);
2960 the_cu->sect_off = sect_off;
2961 the_cu->length = length;
2962 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2963 the_cu->section = section;
2964 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2965 struct dwarf2_per_cu_quick_data);
2966 the_cu->is_dwz = is_dwz;
2967 return the_cu;
2968 }
2969
2970 /* A helper for create_cus_from_index that handles a given list of
2971 CUs. */
2972
2973 static void
2974 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2975 const gdb_byte *cu_list, offset_type n_elements,
2976 struct dwarf2_section_info *section,
2977 int is_dwz)
2978 {
2979 for (offset_type i = 0; i < n_elements; i += 2)
2980 {
2981 gdb_static_assert (sizeof (ULONGEST) >= 8);
2982
2983 sect_offset sect_off
2984 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2985 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2986 cu_list += 2 * 8;
2987
2988 dwarf2_per_cu_data *per_cu
2989 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2990 sect_off, length);
2991 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2992 }
2993 }
2994
2995 /* Read the CU list from the mapped index, and use it to create all
2996 the CU objects for this objfile. */
2997
2998 static void
2999 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3000 const gdb_byte *cu_list, offset_type cu_list_elements,
3001 const gdb_byte *dwz_list, offset_type dwz_elements)
3002 {
3003 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3004 dwarf2_per_objfile->all_comp_units.reserve
3005 ((cu_list_elements + dwz_elements) / 2);
3006
3007 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
3008 &dwarf2_per_objfile->info, 0);
3009
3010 if (dwz_elements == 0)
3011 return;
3012
3013 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3014 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
3015 &dwz->info, 1);
3016 }
3017
3018 /* Create the signatured type hash table from the index. */
3019
3020 static void
3021 create_signatured_type_table_from_index
3022 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3023 struct dwarf2_section_info *section,
3024 const gdb_byte *bytes,
3025 offset_type elements)
3026 {
3027 struct objfile *objfile = dwarf2_per_objfile->objfile;
3028
3029 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3030 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
3031
3032 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3033
3034 for (offset_type i = 0; i < elements; i += 3)
3035 {
3036 struct signatured_type *sig_type;
3037 ULONGEST signature;
3038 void **slot;
3039 cu_offset type_offset_in_tu;
3040
3041 gdb_static_assert (sizeof (ULONGEST) >= 8);
3042 sect_offset sect_off
3043 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3044 type_offset_in_tu
3045 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3046 BFD_ENDIAN_LITTLE);
3047 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3048 bytes += 3 * 8;
3049
3050 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3051 struct signatured_type);
3052 sig_type->signature = signature;
3053 sig_type->type_offset_in_tu = type_offset_in_tu;
3054 sig_type->per_cu.is_debug_types = 1;
3055 sig_type->per_cu.section = section;
3056 sig_type->per_cu.sect_off = sect_off;
3057 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3058 sig_type->per_cu.v.quick
3059 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3060 struct dwarf2_per_cu_quick_data);
3061
3062 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3063 *slot = sig_type;
3064
3065 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3066 }
3067
3068 dwarf2_per_objfile->signatured_types = sig_types_hash;
3069 }
3070
3071 /* Create the signatured type hash table from .debug_names. */
3072
3073 static void
3074 create_signatured_type_table_from_debug_names
3075 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3076 const mapped_debug_names &map,
3077 struct dwarf2_section_info *section,
3078 struct dwarf2_section_info *abbrev_section)
3079 {
3080 struct objfile *objfile = dwarf2_per_objfile->objfile;
3081
3082 dwarf2_read_section (objfile, section);
3083 dwarf2_read_section (objfile, abbrev_section);
3084
3085 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3086 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
3087
3088 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3089
3090 for (uint32_t i = 0; i < map.tu_count; ++i)
3091 {
3092 struct signatured_type *sig_type;
3093 void **slot;
3094
3095 sect_offset sect_off
3096 = (sect_offset) (extract_unsigned_integer
3097 (map.tu_table_reordered + i * map.offset_size,
3098 map.offset_size,
3099 map.dwarf5_byte_order));
3100
3101 comp_unit_head cu_header;
3102 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3103 abbrev_section,
3104 section->buffer + to_underlying (sect_off),
3105 rcuh_kind::TYPE);
3106
3107 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3108 struct signatured_type);
3109 sig_type->signature = cu_header.signature;
3110 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3111 sig_type->per_cu.is_debug_types = 1;
3112 sig_type->per_cu.section = section;
3113 sig_type->per_cu.sect_off = sect_off;
3114 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3115 sig_type->per_cu.v.quick
3116 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3117 struct dwarf2_per_cu_quick_data);
3118
3119 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3120 *slot = sig_type;
3121
3122 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3123 }
3124
3125 dwarf2_per_objfile->signatured_types = sig_types_hash;
3126 }
3127
3128 /* Read the address map data from the mapped index, and use it to
3129 populate the objfile's psymtabs_addrmap. */
3130
3131 static void
3132 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3133 struct mapped_index *index)
3134 {
3135 struct objfile *objfile = dwarf2_per_objfile->objfile;
3136 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3137 const gdb_byte *iter, *end;
3138 struct addrmap *mutable_map;
3139 CORE_ADDR baseaddr;
3140
3141 auto_obstack temp_obstack;
3142
3143 mutable_map = addrmap_create_mutable (&temp_obstack);
3144
3145 iter = index->address_table.data ();
3146 end = iter + index->address_table.size ();
3147
3148 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3149
3150 while (iter < end)
3151 {
3152 ULONGEST hi, lo, cu_index;
3153 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3154 iter += 8;
3155 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3156 iter += 8;
3157 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3158 iter += 4;
3159
3160 if (lo > hi)
3161 {
3162 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
3163 hex_string (lo), hex_string (hi));
3164 continue;
3165 }
3166
3167 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
3168 {
3169 complaint (_(".gdb_index address table has invalid CU number %u"),
3170 (unsigned) cu_index);
3171 continue;
3172 }
3173
3174 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3175 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
3176 addrmap_set_empty (mutable_map, lo, hi - 1,
3177 dwarf2_per_objfile->get_cu (cu_index));
3178 }
3179
3180 objfile->partial_symtabs->psymtabs_addrmap
3181 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3182 }
3183
3184 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3185 populate the objfile's psymtabs_addrmap. */
3186
3187 static void
3188 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3189 struct dwarf2_section_info *section)
3190 {
3191 struct objfile *objfile = dwarf2_per_objfile->objfile;
3192 bfd *abfd = objfile->obfd;
3193 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3194 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3195 SECT_OFF_TEXT (objfile));
3196
3197 auto_obstack temp_obstack;
3198 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3199
3200 std::unordered_map<sect_offset,
3201 dwarf2_per_cu_data *,
3202 gdb::hash_enum<sect_offset>>
3203 debug_info_offset_to_per_cu;
3204 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3205 {
3206 const auto insertpair
3207 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3208 if (!insertpair.second)
3209 {
3210 warning (_("Section .debug_aranges in %s has duplicate "
3211 "debug_info_offset %s, ignoring .debug_aranges."),
3212 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3213 return;
3214 }
3215 }
3216
3217 dwarf2_read_section (objfile, section);
3218
3219 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3220
3221 const gdb_byte *addr = section->buffer;
3222
3223 while (addr < section->buffer + section->size)
3224 {
3225 const gdb_byte *const entry_addr = addr;
3226 unsigned int bytes_read;
3227
3228 const LONGEST entry_length = read_initial_length (abfd, addr,
3229 &bytes_read);
3230 addr += bytes_read;
3231
3232 const gdb_byte *const entry_end = addr + entry_length;
3233 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3234 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3235 if (addr + entry_length > section->buffer + section->size)
3236 {
3237 warning (_("Section .debug_aranges in %s entry at offset %s "
3238 "length %s exceeds section length %s, "
3239 "ignoring .debug_aranges."),
3240 objfile_name (objfile),
3241 plongest (entry_addr - section->buffer),
3242 plongest (bytes_read + entry_length),
3243 pulongest (section->size));
3244 return;
3245 }
3246
3247 /* The version number. */
3248 const uint16_t version = read_2_bytes (abfd, addr);
3249 addr += 2;
3250 if (version != 2)
3251 {
3252 warning (_("Section .debug_aranges in %s entry at offset %s "
3253 "has unsupported version %d, ignoring .debug_aranges."),
3254 objfile_name (objfile),
3255 plongest (entry_addr - section->buffer), version);
3256 return;
3257 }
3258
3259 const uint64_t debug_info_offset
3260 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3261 addr += offset_size;
3262 const auto per_cu_it
3263 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3264 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3265 {
3266 warning (_("Section .debug_aranges in %s entry at offset %s "
3267 "debug_info_offset %s does not exists, "
3268 "ignoring .debug_aranges."),
3269 objfile_name (objfile),
3270 plongest (entry_addr - section->buffer),
3271 pulongest (debug_info_offset));
3272 return;
3273 }
3274 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3275
3276 const uint8_t address_size = *addr++;
3277 if (address_size < 1 || address_size > 8)
3278 {
3279 warning (_("Section .debug_aranges in %s entry at offset %s "
3280 "address_size %u is invalid, ignoring .debug_aranges."),
3281 objfile_name (objfile),
3282 plongest (entry_addr - section->buffer), address_size);
3283 return;
3284 }
3285
3286 const uint8_t segment_selector_size = *addr++;
3287 if (segment_selector_size != 0)
3288 {
3289 warning (_("Section .debug_aranges in %s entry at offset %s "
3290 "segment_selector_size %u is not supported, "
3291 "ignoring .debug_aranges."),
3292 objfile_name (objfile),
3293 plongest (entry_addr - section->buffer),
3294 segment_selector_size);
3295 return;
3296 }
3297
3298 /* Must pad to an alignment boundary that is twice the address
3299 size. It is undocumented by the DWARF standard but GCC does
3300 use it. */
3301 for (size_t padding = ((-(addr - section->buffer))
3302 & (2 * address_size - 1));
3303 padding > 0; padding--)
3304 if (*addr++ != 0)
3305 {
3306 warning (_("Section .debug_aranges in %s entry at offset %s "
3307 "padding is not zero, ignoring .debug_aranges."),
3308 objfile_name (objfile),
3309 plongest (entry_addr - section->buffer));
3310 return;
3311 }
3312
3313 for (;;)
3314 {
3315 if (addr + 2 * address_size > entry_end)
3316 {
3317 warning (_("Section .debug_aranges in %s entry at offset %s "
3318 "address list is not properly terminated, "
3319 "ignoring .debug_aranges."),
3320 objfile_name (objfile),
3321 plongest (entry_addr - section->buffer));
3322 return;
3323 }
3324 ULONGEST start = extract_unsigned_integer (addr, address_size,
3325 dwarf5_byte_order);
3326 addr += address_size;
3327 ULONGEST length = extract_unsigned_integer (addr, address_size,
3328 dwarf5_byte_order);
3329 addr += address_size;
3330 if (start == 0 && length == 0)
3331 break;
3332 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3333 {
3334 /* Symbol was eliminated due to a COMDAT group. */
3335 continue;
3336 }
3337 ULONGEST end = start + length;
3338 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3339 - baseaddr);
3340 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3341 - baseaddr);
3342 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3343 }
3344 }
3345
3346 objfile->partial_symtabs->psymtabs_addrmap
3347 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3348 }
3349
3350 /* Find a slot in the mapped index INDEX for the object named NAME.
3351 If NAME is found, set *VEC_OUT to point to the CU vector in the
3352 constant pool and return true. If NAME cannot be found, return
3353 false. */
3354
3355 static bool
3356 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3357 offset_type **vec_out)
3358 {
3359 offset_type hash;
3360 offset_type slot, step;
3361 int (*cmp) (const char *, const char *);
3362
3363 gdb::unique_xmalloc_ptr<char> without_params;
3364 if (current_language->la_language == language_cplus
3365 || current_language->la_language == language_fortran
3366 || current_language->la_language == language_d)
3367 {
3368 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3369 not contain any. */
3370
3371 if (strchr (name, '(') != NULL)
3372 {
3373 without_params = cp_remove_params (name);
3374
3375 if (without_params != NULL)
3376 name = without_params.get ();
3377 }
3378 }
3379
3380 /* Index version 4 did not support case insensitive searches. But the
3381 indices for case insensitive languages are built in lowercase, therefore
3382 simulate our NAME being searched is also lowercased. */
3383 hash = mapped_index_string_hash ((index->version == 4
3384 && case_sensitivity == case_sensitive_off
3385 ? 5 : index->version),
3386 name);
3387
3388 slot = hash & (index->symbol_table.size () - 1);
3389 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3390 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3391
3392 for (;;)
3393 {
3394 const char *str;
3395
3396 const auto &bucket = index->symbol_table[slot];
3397 if (bucket.name == 0 && bucket.vec == 0)
3398 return false;
3399
3400 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3401 if (!cmp (name, str))
3402 {
3403 *vec_out = (offset_type *) (index->constant_pool
3404 + MAYBE_SWAP (bucket.vec));
3405 return true;
3406 }
3407
3408 slot = (slot + step) & (index->symbol_table.size () - 1);
3409 }
3410 }
3411
3412 /* A helper function that reads the .gdb_index from BUFFER and fills
3413 in MAP. FILENAME is the name of the file containing the data;
3414 it is used for error reporting. DEPRECATED_OK is true if it is
3415 ok to use deprecated sections.
3416
3417 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3418 out parameters that are filled in with information about the CU and
3419 TU lists in the section.
3420
3421 Returns true if all went well, false otherwise. */
3422
3423 static bool
3424 read_gdb_index_from_buffer (struct objfile *objfile,
3425 const char *filename,
3426 bool deprecated_ok,
3427 gdb::array_view<const gdb_byte> buffer,
3428 struct mapped_index *map,
3429 const gdb_byte **cu_list,
3430 offset_type *cu_list_elements,
3431 const gdb_byte **types_list,
3432 offset_type *types_list_elements)
3433 {
3434 const gdb_byte *addr = &buffer[0];
3435
3436 /* Version check. */
3437 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3438 /* Versions earlier than 3 emitted every copy of a psymbol. This
3439 causes the index to behave very poorly for certain requests. Version 3
3440 contained incomplete addrmap. So, it seems better to just ignore such
3441 indices. */
3442 if (version < 4)
3443 {
3444 static int warning_printed = 0;
3445 if (!warning_printed)
3446 {
3447 warning (_("Skipping obsolete .gdb_index section in %s."),
3448 filename);
3449 warning_printed = 1;
3450 }
3451 return 0;
3452 }
3453 /* Index version 4 uses a different hash function than index version
3454 5 and later.
3455
3456 Versions earlier than 6 did not emit psymbols for inlined
3457 functions. Using these files will cause GDB not to be able to
3458 set breakpoints on inlined functions by name, so we ignore these
3459 indices unless the user has done
3460 "set use-deprecated-index-sections on". */
3461 if (version < 6 && !deprecated_ok)
3462 {
3463 static int warning_printed = 0;
3464 if (!warning_printed)
3465 {
3466 warning (_("\
3467 Skipping deprecated .gdb_index section in %s.\n\
3468 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3469 to use the section anyway."),
3470 filename);
3471 warning_printed = 1;
3472 }
3473 return 0;
3474 }
3475 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3476 of the TU (for symbols coming from TUs),
3477 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3478 Plus gold-generated indices can have duplicate entries for global symbols,
3479 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3480 These are just performance bugs, and we can't distinguish gdb-generated
3481 indices from gold-generated ones, so issue no warning here. */
3482
3483 /* Indexes with higher version than the one supported by GDB may be no
3484 longer backward compatible. */
3485 if (version > 8)
3486 return 0;
3487
3488 map->version = version;
3489
3490 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3491
3492 int i = 0;
3493 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3494 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3495 / 8);
3496 ++i;
3497
3498 *types_list = addr + MAYBE_SWAP (metadata[i]);
3499 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3500 - MAYBE_SWAP (metadata[i]))
3501 / 8);
3502 ++i;
3503
3504 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3505 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3506 map->address_table
3507 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3508 ++i;
3509
3510 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3511 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3512 map->symbol_table
3513 = gdb::array_view<mapped_index::symbol_table_slot>
3514 ((mapped_index::symbol_table_slot *) symbol_table,
3515 (mapped_index::symbol_table_slot *) symbol_table_end);
3516
3517 ++i;
3518 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3519
3520 return 1;
3521 }
3522
3523 /* Callback types for dwarf2_read_gdb_index. */
3524
3525 typedef gdb::function_view
3526 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3527 get_gdb_index_contents_ftype;
3528 typedef gdb::function_view
3529 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3530 get_gdb_index_contents_dwz_ftype;
3531
3532 /* Read .gdb_index. If everything went ok, initialize the "quick"
3533 elements of all the CUs and return 1. Otherwise, return 0. */
3534
3535 static int
3536 dwarf2_read_gdb_index
3537 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3538 get_gdb_index_contents_ftype get_gdb_index_contents,
3539 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3540 {
3541 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3542 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3543 struct dwz_file *dwz;
3544 struct objfile *objfile = dwarf2_per_objfile->objfile;
3545
3546 gdb::array_view<const gdb_byte> main_index_contents
3547 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3548
3549 if (main_index_contents.empty ())
3550 return 0;
3551
3552 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3553 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3554 use_deprecated_index_sections,
3555 main_index_contents, map.get (), &cu_list,
3556 &cu_list_elements, &types_list,
3557 &types_list_elements))
3558 return 0;
3559
3560 /* Don't use the index if it's empty. */
3561 if (map->symbol_table.empty ())
3562 return 0;
3563
3564 /* If there is a .dwz file, read it so we can get its CU list as
3565 well. */
3566 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3567 if (dwz != NULL)
3568 {
3569 struct mapped_index dwz_map;
3570 const gdb_byte *dwz_types_ignore;
3571 offset_type dwz_types_elements_ignore;
3572
3573 gdb::array_view<const gdb_byte> dwz_index_content
3574 = get_gdb_index_contents_dwz (objfile, dwz);
3575
3576 if (dwz_index_content.empty ())
3577 return 0;
3578
3579 if (!read_gdb_index_from_buffer (objfile,
3580 bfd_get_filename (dwz->dwz_bfd.get ()),
3581 1, dwz_index_content, &dwz_map,
3582 &dwz_list, &dwz_list_elements,
3583 &dwz_types_ignore,
3584 &dwz_types_elements_ignore))
3585 {
3586 warning (_("could not read '.gdb_index' section from %s; skipping"),
3587 bfd_get_filename (dwz->dwz_bfd.get ()));
3588 return 0;
3589 }
3590 }
3591
3592 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3593 dwz_list, dwz_list_elements);
3594
3595 if (types_list_elements)
3596 {
3597 /* We can only handle a single .debug_types when we have an
3598 index. */
3599 if (dwarf2_per_objfile->types.size () != 1)
3600 return 0;
3601
3602 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3603
3604 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3605 types_list, types_list_elements);
3606 }
3607
3608 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3609
3610 dwarf2_per_objfile->index_table = std::move (map);
3611 dwarf2_per_objfile->using_index = 1;
3612 dwarf2_per_objfile->quick_file_names_table =
3613 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3614
3615 return 1;
3616 }
3617
3618 /* die_reader_func for dw2_get_file_names. */
3619
3620 static void
3621 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3622 const gdb_byte *info_ptr,
3623 struct die_info *comp_unit_die,
3624 int has_children,
3625 void *data)
3626 {
3627 struct dwarf2_cu *cu = reader->cu;
3628 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3629 struct dwarf2_per_objfile *dwarf2_per_objfile
3630 = cu->per_cu->dwarf2_per_objfile;
3631 struct objfile *objfile = dwarf2_per_objfile->objfile;
3632 struct dwarf2_per_cu_data *lh_cu;
3633 struct attribute *attr;
3634 int i;
3635 void **slot;
3636 struct quick_file_names *qfn;
3637
3638 gdb_assert (! this_cu->is_debug_types);
3639
3640 /* Our callers never want to match partial units -- instead they
3641 will match the enclosing full CU. */
3642 if (comp_unit_die->tag == DW_TAG_partial_unit)
3643 {
3644 this_cu->v.quick->no_file_data = 1;
3645 return;
3646 }
3647
3648 lh_cu = this_cu;
3649 slot = NULL;
3650
3651 line_header_up lh;
3652 sect_offset line_offset {};
3653
3654 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3655 if (attr)
3656 {
3657 struct quick_file_names find_entry;
3658
3659 line_offset = (sect_offset) DW_UNSND (attr);
3660
3661 /* We may have already read in this line header (TU line header sharing).
3662 If we have we're done. */
3663 find_entry.hash.dwo_unit = cu->dwo_unit;
3664 find_entry.hash.line_sect_off = line_offset;
3665 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3666 &find_entry, INSERT);
3667 if (*slot != NULL)
3668 {
3669 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3670 return;
3671 }
3672
3673 lh = dwarf_decode_line_header (line_offset, cu);
3674 }
3675 if (lh == NULL)
3676 {
3677 lh_cu->v.quick->no_file_data = 1;
3678 return;
3679 }
3680
3681 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3682 qfn->hash.dwo_unit = cu->dwo_unit;
3683 qfn->hash.line_sect_off = line_offset;
3684 gdb_assert (slot != NULL);
3685 *slot = qfn;
3686
3687 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3688
3689 int offset = 0;
3690 if (strcmp (fnd.name, "<unknown>") != 0)
3691 ++offset;
3692
3693 qfn->num_file_names = offset + lh->file_names.size ();
3694 qfn->file_names =
3695 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3696 if (offset != 0)
3697 qfn->file_names[0] = xstrdup (fnd.name);
3698 for (i = 0; i < lh->file_names.size (); ++i)
3699 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3700 qfn->real_names = NULL;
3701
3702 lh_cu->v.quick->file_names = qfn;
3703 }
3704
3705 /* A helper for the "quick" functions which attempts to read the line
3706 table for THIS_CU. */
3707
3708 static struct quick_file_names *
3709 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3710 {
3711 /* This should never be called for TUs. */
3712 gdb_assert (! this_cu->is_debug_types);
3713 /* Nor type unit groups. */
3714 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3715
3716 if (this_cu->v.quick->file_names != NULL)
3717 return this_cu->v.quick->file_names;
3718 /* If we know there is no line data, no point in looking again. */
3719 if (this_cu->v.quick->no_file_data)
3720 return NULL;
3721
3722 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3723
3724 if (this_cu->v.quick->no_file_data)
3725 return NULL;
3726 return this_cu->v.quick->file_names;
3727 }
3728
3729 /* A helper for the "quick" functions which computes and caches the
3730 real path for a given file name from the line table. */
3731
3732 static const char *
3733 dw2_get_real_path (struct objfile *objfile,
3734 struct quick_file_names *qfn, int index)
3735 {
3736 if (qfn->real_names == NULL)
3737 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3738 qfn->num_file_names, const char *);
3739
3740 if (qfn->real_names[index] == NULL)
3741 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3742
3743 return qfn->real_names[index];
3744 }
3745
3746 static struct symtab *
3747 dw2_find_last_source_symtab (struct objfile *objfile)
3748 {
3749 struct dwarf2_per_objfile *dwarf2_per_objfile
3750 = get_dwarf2_per_objfile (objfile);
3751 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3752 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3753
3754 if (cust == NULL)
3755 return NULL;
3756
3757 return compunit_primary_filetab (cust);
3758 }
3759
3760 /* Traversal function for dw2_forget_cached_source_info. */
3761
3762 static int
3763 dw2_free_cached_file_names (void **slot, void *info)
3764 {
3765 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3766
3767 if (file_data->real_names)
3768 {
3769 int i;
3770
3771 for (i = 0; i < file_data->num_file_names; ++i)
3772 {
3773 xfree ((void*) file_data->real_names[i]);
3774 file_data->real_names[i] = NULL;
3775 }
3776 }
3777
3778 return 1;
3779 }
3780
3781 static void
3782 dw2_forget_cached_source_info (struct objfile *objfile)
3783 {
3784 struct dwarf2_per_objfile *dwarf2_per_objfile
3785 = get_dwarf2_per_objfile (objfile);
3786
3787 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3788 dw2_free_cached_file_names, NULL);
3789 }
3790
3791 /* Helper function for dw2_map_symtabs_matching_filename that expands
3792 the symtabs and calls the iterator. */
3793
3794 static int
3795 dw2_map_expand_apply (struct objfile *objfile,
3796 struct dwarf2_per_cu_data *per_cu,
3797 const char *name, const char *real_path,
3798 gdb::function_view<bool (symtab *)> callback)
3799 {
3800 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3801
3802 /* Don't visit already-expanded CUs. */
3803 if (per_cu->v.quick->compunit_symtab)
3804 return 0;
3805
3806 /* This may expand more than one symtab, and we want to iterate over
3807 all of them. */
3808 dw2_instantiate_symtab (per_cu, false);
3809
3810 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3811 last_made, callback);
3812 }
3813
3814 /* Implementation of the map_symtabs_matching_filename method. */
3815
3816 static bool
3817 dw2_map_symtabs_matching_filename
3818 (struct objfile *objfile, const char *name, const char *real_path,
3819 gdb::function_view<bool (symtab *)> callback)
3820 {
3821 const char *name_basename = lbasename (name);
3822 struct dwarf2_per_objfile *dwarf2_per_objfile
3823 = get_dwarf2_per_objfile (objfile);
3824
3825 /* The rule is CUs specify all the files, including those used by
3826 any TU, so there's no need to scan TUs here. */
3827
3828 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3829 {
3830 /* We only need to look at symtabs not already expanded. */
3831 if (per_cu->v.quick->compunit_symtab)
3832 continue;
3833
3834 quick_file_names *file_data = dw2_get_file_names (per_cu);
3835 if (file_data == NULL)
3836 continue;
3837
3838 for (int j = 0; j < file_data->num_file_names; ++j)
3839 {
3840 const char *this_name = file_data->file_names[j];
3841 const char *this_real_name;
3842
3843 if (compare_filenames_for_search (this_name, name))
3844 {
3845 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3846 callback))
3847 return true;
3848 continue;
3849 }
3850
3851 /* Before we invoke realpath, which can get expensive when many
3852 files are involved, do a quick comparison of the basenames. */
3853 if (! basenames_may_differ
3854 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3855 continue;
3856
3857 this_real_name = dw2_get_real_path (objfile, file_data, j);
3858 if (compare_filenames_for_search (this_real_name, name))
3859 {
3860 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3861 callback))
3862 return true;
3863 continue;
3864 }
3865
3866 if (real_path != NULL)
3867 {
3868 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3869 gdb_assert (IS_ABSOLUTE_PATH (name));
3870 if (this_real_name != NULL
3871 && FILENAME_CMP (real_path, this_real_name) == 0)
3872 {
3873 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3874 callback))
3875 return true;
3876 continue;
3877 }
3878 }
3879 }
3880 }
3881
3882 return false;
3883 }
3884
3885 /* Struct used to manage iterating over all CUs looking for a symbol. */
3886
3887 struct dw2_symtab_iterator
3888 {
3889 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3890 struct dwarf2_per_objfile *dwarf2_per_objfile;
3891 /* If set, only look for symbols that match that block. Valid values are
3892 GLOBAL_BLOCK and STATIC_BLOCK. */
3893 gdb::optional<block_enum> block_index;
3894 /* The kind of symbol we're looking for. */
3895 domain_enum domain;
3896 /* The list of CUs from the index entry of the symbol,
3897 or NULL if not found. */
3898 offset_type *vec;
3899 /* The next element in VEC to look at. */
3900 int next;
3901 /* The number of elements in VEC, or zero if there is no match. */
3902 int length;
3903 /* Have we seen a global version of the symbol?
3904 If so we can ignore all further global instances.
3905 This is to work around gold/15646, inefficient gold-generated
3906 indices. */
3907 int global_seen;
3908 };
3909
3910 /* Initialize the index symtab iterator ITER. */
3911
3912 static void
3913 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3914 struct dwarf2_per_objfile *dwarf2_per_objfile,
3915 gdb::optional<block_enum> block_index,
3916 domain_enum domain,
3917 const char *name)
3918 {
3919 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3920 iter->block_index = block_index;
3921 iter->domain = domain;
3922 iter->next = 0;
3923 iter->global_seen = 0;
3924
3925 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3926
3927 /* index is NULL if OBJF_READNOW. */
3928 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3929 iter->length = MAYBE_SWAP (*iter->vec);
3930 else
3931 {
3932 iter->vec = NULL;
3933 iter->length = 0;
3934 }
3935 }
3936
3937 /* Return the next matching CU or NULL if there are no more. */
3938
3939 static struct dwarf2_per_cu_data *
3940 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3941 {
3942 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3943
3944 for ( ; iter->next < iter->length; ++iter->next)
3945 {
3946 offset_type cu_index_and_attrs =
3947 MAYBE_SWAP (iter->vec[iter->next + 1]);
3948 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3949 gdb_index_symbol_kind symbol_kind =
3950 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3951 /* Only check the symbol attributes if they're present.
3952 Indices prior to version 7 don't record them,
3953 and indices >= 7 may elide them for certain symbols
3954 (gold does this). */
3955 int attrs_valid =
3956 (dwarf2_per_objfile->index_table->version >= 7
3957 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3958
3959 /* Don't crash on bad data. */
3960 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3961 + dwarf2_per_objfile->all_type_units.size ()))
3962 {
3963 complaint (_(".gdb_index entry has bad CU index"
3964 " [in module %s]"),
3965 objfile_name (dwarf2_per_objfile->objfile));
3966 continue;
3967 }
3968
3969 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3970
3971 /* Skip if already read in. */
3972 if (per_cu->v.quick->compunit_symtab)
3973 continue;
3974
3975 /* Check static vs global. */
3976 if (attrs_valid)
3977 {
3978 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3979
3980 if (iter->block_index.has_value ())
3981 {
3982 bool want_static = *iter->block_index == STATIC_BLOCK;
3983
3984 if (is_static != want_static)
3985 continue;
3986 }
3987
3988 /* Work around gold/15646. */
3989 if (!is_static && iter->global_seen)
3990 continue;
3991 if (!is_static)
3992 iter->global_seen = 1;
3993 }
3994
3995 /* Only check the symbol's kind if it has one. */
3996 if (attrs_valid)
3997 {
3998 switch (iter->domain)
3999 {
4000 case VAR_DOMAIN:
4001 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4002 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4003 /* Some types are also in VAR_DOMAIN. */
4004 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4005 continue;
4006 break;
4007 case STRUCT_DOMAIN:
4008 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4009 continue;
4010 break;
4011 case LABEL_DOMAIN:
4012 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4013 continue;
4014 break;
4015 default:
4016 break;
4017 }
4018 }
4019
4020 ++iter->next;
4021 return per_cu;
4022 }
4023
4024 return NULL;
4025 }
4026
4027 static struct compunit_symtab *
4028 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
4029 const char *name, domain_enum domain)
4030 {
4031 struct compunit_symtab *stab_best = NULL;
4032 struct dwarf2_per_objfile *dwarf2_per_objfile
4033 = get_dwarf2_per_objfile (objfile);
4034
4035 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4036
4037 struct dw2_symtab_iterator iter;
4038 struct dwarf2_per_cu_data *per_cu;
4039
4040 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
4041
4042 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4043 {
4044 struct symbol *sym, *with_opaque = NULL;
4045 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
4046 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4047 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4048
4049 sym = block_find_symbol (block, name, domain,
4050 block_find_non_opaque_type_preferred,
4051 &with_opaque);
4052
4053 /* Some caution must be observed with overloaded functions
4054 and methods, since the index will not contain any overload
4055 information (but NAME might contain it). */
4056
4057 if (sym != NULL
4058 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4059 return stab;
4060 if (with_opaque != NULL
4061 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4062 stab_best = stab;
4063
4064 /* Keep looking through other CUs. */
4065 }
4066
4067 return stab_best;
4068 }
4069
4070 static void
4071 dw2_print_stats (struct objfile *objfile)
4072 {
4073 struct dwarf2_per_objfile *dwarf2_per_objfile
4074 = get_dwarf2_per_objfile (objfile);
4075 int total = (dwarf2_per_objfile->all_comp_units.size ()
4076 + dwarf2_per_objfile->all_type_units.size ());
4077 int count = 0;
4078
4079 for (int i = 0; i < total; ++i)
4080 {
4081 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4082
4083 if (!per_cu->v.quick->compunit_symtab)
4084 ++count;
4085 }
4086 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4087 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4088 }
4089
4090 /* This dumps minimal information about the index.
4091 It is called via "mt print objfiles".
4092 One use is to verify .gdb_index has been loaded by the
4093 gdb.dwarf2/gdb-index.exp testcase. */
4094
4095 static void
4096 dw2_dump (struct objfile *objfile)
4097 {
4098 struct dwarf2_per_objfile *dwarf2_per_objfile
4099 = get_dwarf2_per_objfile (objfile);
4100
4101 gdb_assert (dwarf2_per_objfile->using_index);
4102 printf_filtered (".gdb_index:");
4103 if (dwarf2_per_objfile->index_table != NULL)
4104 {
4105 printf_filtered (" version %d\n",
4106 dwarf2_per_objfile->index_table->version);
4107 }
4108 else
4109 printf_filtered (" faked for \"readnow\"\n");
4110 printf_filtered ("\n");
4111 }
4112
4113 static void
4114 dw2_expand_symtabs_for_function (struct objfile *objfile,
4115 const char *func_name)
4116 {
4117 struct dwarf2_per_objfile *dwarf2_per_objfile
4118 = get_dwarf2_per_objfile (objfile);
4119
4120 struct dw2_symtab_iterator iter;
4121 struct dwarf2_per_cu_data *per_cu;
4122
4123 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
4124
4125 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4126 dw2_instantiate_symtab (per_cu, false);
4127
4128 }
4129
4130 static void
4131 dw2_expand_all_symtabs (struct objfile *objfile)
4132 {
4133 struct dwarf2_per_objfile *dwarf2_per_objfile
4134 = get_dwarf2_per_objfile (objfile);
4135 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
4136 + dwarf2_per_objfile->all_type_units.size ());
4137
4138 for (int i = 0; i < total_units; ++i)
4139 {
4140 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4141
4142 /* We don't want to directly expand a partial CU, because if we
4143 read it with the wrong language, then assertion failures can
4144 be triggered later on. See PR symtab/23010. So, tell
4145 dw2_instantiate_symtab to skip partial CUs -- any important
4146 partial CU will be read via DW_TAG_imported_unit anyway. */
4147 dw2_instantiate_symtab (per_cu, true);
4148 }
4149 }
4150
4151 static void
4152 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4153 const char *fullname)
4154 {
4155 struct dwarf2_per_objfile *dwarf2_per_objfile
4156 = get_dwarf2_per_objfile (objfile);
4157
4158 /* We don't need to consider type units here.
4159 This is only called for examining code, e.g. expand_line_sal.
4160 There can be an order of magnitude (or more) more type units
4161 than comp units, and we avoid them if we can. */
4162
4163 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4164 {
4165 /* We only need to look at symtabs not already expanded. */
4166 if (per_cu->v.quick->compunit_symtab)
4167 continue;
4168
4169 quick_file_names *file_data = dw2_get_file_names (per_cu);
4170 if (file_data == NULL)
4171 continue;
4172
4173 for (int j = 0; j < file_data->num_file_names; ++j)
4174 {
4175 const char *this_fullname = file_data->file_names[j];
4176
4177 if (filename_cmp (this_fullname, fullname) == 0)
4178 {
4179 dw2_instantiate_symtab (per_cu, false);
4180 break;
4181 }
4182 }
4183 }
4184 }
4185
4186 static void
4187 dw2_map_matching_symbols
4188 (struct objfile *objfile,
4189 const lookup_name_info &name, domain_enum domain,
4190 int global,
4191 gdb::function_view<symbol_found_callback_ftype> callback,
4192 symbol_compare_ftype *ordered_compare)
4193 {
4194 /* Currently unimplemented; used for Ada. The function can be called if the
4195 current language is Ada for a non-Ada objfile using GNU index. As Ada
4196 does not look for non-Ada symbols this function should just return. */
4197 }
4198
4199 /* Starting from a search name, return the string that finds the upper
4200 bound of all strings that start with SEARCH_NAME in a sorted name
4201 list. Returns the empty string to indicate that the upper bound is
4202 the end of the list. */
4203
4204 static std::string
4205 make_sort_after_prefix_name (const char *search_name)
4206 {
4207 /* When looking to complete "func", we find the upper bound of all
4208 symbols that start with "func" by looking for where we'd insert
4209 the closest string that would follow "func" in lexicographical
4210 order. Usually, that's "func"-with-last-character-incremented,
4211 i.e. "fund". Mind non-ASCII characters, though. Usually those
4212 will be UTF-8 multi-byte sequences, but we can't be certain.
4213 Especially mind the 0xff character, which is a valid character in
4214 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4215 rule out compilers allowing it in identifiers. Note that
4216 conveniently, strcmp/strcasecmp are specified to compare
4217 characters interpreted as unsigned char. So what we do is treat
4218 the whole string as a base 256 number composed of a sequence of
4219 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4220 to 0, and carries 1 to the following more-significant position.
4221 If the very first character in SEARCH_NAME ends up incremented
4222 and carries/overflows, then the upper bound is the end of the
4223 list. The string after the empty string is also the empty
4224 string.
4225
4226 Some examples of this operation:
4227
4228 SEARCH_NAME => "+1" RESULT
4229
4230 "abc" => "abd"
4231 "ab\xff" => "ac"
4232 "\xff" "a" "\xff" => "\xff" "b"
4233 "\xff" => ""
4234 "\xff\xff" => ""
4235 "" => ""
4236
4237 Then, with these symbols for example:
4238
4239 func
4240 func1
4241 fund
4242
4243 completing "func" looks for symbols between "func" and
4244 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4245 which finds "func" and "func1", but not "fund".
4246
4247 And with:
4248
4249 funcÿ (Latin1 'ÿ' [0xff])
4250 funcÿ1
4251 fund
4252
4253 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4254 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4255
4256 And with:
4257
4258 ÿÿ (Latin1 'ÿ' [0xff])
4259 ÿÿ1
4260
4261 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4262 the end of the list.
4263 */
4264 std::string after = search_name;
4265 while (!after.empty () && (unsigned char) after.back () == 0xff)
4266 after.pop_back ();
4267 if (!after.empty ())
4268 after.back () = (unsigned char) after.back () + 1;
4269 return after;
4270 }
4271
4272 /* See declaration. */
4273
4274 std::pair<std::vector<name_component>::const_iterator,
4275 std::vector<name_component>::const_iterator>
4276 mapped_index_base::find_name_components_bounds
4277 (const lookup_name_info &lookup_name_without_params, language lang) const
4278 {
4279 auto *name_cmp
4280 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4281
4282 const char *lang_name
4283 = lookup_name_without_params.language_lookup_name (lang).c_str ();
4284
4285 /* Comparison function object for lower_bound that matches against a
4286 given symbol name. */
4287 auto lookup_compare_lower = [&] (const name_component &elem,
4288 const char *name)
4289 {
4290 const char *elem_qualified = this->symbol_name_at (elem.idx);
4291 const char *elem_name = elem_qualified + elem.name_offset;
4292 return name_cmp (elem_name, name) < 0;
4293 };
4294
4295 /* Comparison function object for upper_bound that matches against a
4296 given symbol name. */
4297 auto lookup_compare_upper = [&] (const char *name,
4298 const name_component &elem)
4299 {
4300 const char *elem_qualified = this->symbol_name_at (elem.idx);
4301 const char *elem_name = elem_qualified + elem.name_offset;
4302 return name_cmp (name, elem_name) < 0;
4303 };
4304
4305 auto begin = this->name_components.begin ();
4306 auto end = this->name_components.end ();
4307
4308 /* Find the lower bound. */
4309 auto lower = [&] ()
4310 {
4311 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
4312 return begin;
4313 else
4314 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4315 } ();
4316
4317 /* Find the upper bound. */
4318 auto upper = [&] ()
4319 {
4320 if (lookup_name_without_params.completion_mode ())
4321 {
4322 /* In completion mode, we want UPPER to point past all
4323 symbols names that have the same prefix. I.e., with
4324 these symbols, and completing "func":
4325
4326 function << lower bound
4327 function1
4328 other_function << upper bound
4329
4330 We find the upper bound by looking for the insertion
4331 point of "func"-with-last-character-incremented,
4332 i.e. "fund". */
4333 std::string after = make_sort_after_prefix_name (lang_name);
4334 if (after.empty ())
4335 return end;
4336 return std::lower_bound (lower, end, after.c_str (),
4337 lookup_compare_lower);
4338 }
4339 else
4340 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4341 } ();
4342
4343 return {lower, upper};
4344 }
4345
4346 /* See declaration. */
4347
4348 void
4349 mapped_index_base::build_name_components ()
4350 {
4351 if (!this->name_components.empty ())
4352 return;
4353
4354 this->name_components_casing = case_sensitivity;
4355 auto *name_cmp
4356 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4357
4358 /* The code below only knows how to break apart components of C++
4359 symbol names (and other languages that use '::' as
4360 namespace/module separator) and Ada symbol names. */
4361 auto count = this->symbol_name_count ();
4362 for (offset_type idx = 0; idx < count; idx++)
4363 {
4364 if (this->symbol_name_slot_invalid (idx))
4365 continue;
4366
4367 const char *name = this->symbol_name_at (idx);
4368
4369 /* Add each name component to the name component table. */
4370 unsigned int previous_len = 0;
4371
4372 if (strstr (name, "::") != nullptr)
4373 {
4374 for (unsigned int current_len = cp_find_first_component (name);
4375 name[current_len] != '\0';
4376 current_len += cp_find_first_component (name + current_len))
4377 {
4378 gdb_assert (name[current_len] == ':');
4379 this->name_components.push_back ({previous_len, idx});
4380 /* Skip the '::'. */
4381 current_len += 2;
4382 previous_len = current_len;
4383 }
4384 }
4385 else
4386 {
4387 /* Handle the Ada encoded (aka mangled) form here. */
4388 for (const char *iter = strstr (name, "__");
4389 iter != nullptr;
4390 iter = strstr (iter, "__"))
4391 {
4392 this->name_components.push_back ({previous_len, idx});
4393 iter += 2;
4394 previous_len = iter - name;
4395 }
4396 }
4397
4398 this->name_components.push_back ({previous_len, idx});
4399 }
4400
4401 /* Sort name_components elements by name. */
4402 auto name_comp_compare = [&] (const name_component &left,
4403 const name_component &right)
4404 {
4405 const char *left_qualified = this->symbol_name_at (left.idx);
4406 const char *right_qualified = this->symbol_name_at (right.idx);
4407
4408 const char *left_name = left_qualified + left.name_offset;
4409 const char *right_name = right_qualified + right.name_offset;
4410
4411 return name_cmp (left_name, right_name) < 0;
4412 };
4413
4414 std::sort (this->name_components.begin (),
4415 this->name_components.end (),
4416 name_comp_compare);
4417 }
4418
4419 /* Helper for dw2_expand_symtabs_matching that works with a
4420 mapped_index_base instead of the containing objfile. This is split
4421 to a separate function in order to be able to unit test the
4422 name_components matching using a mock mapped_index_base. For each
4423 symbol name that matches, calls MATCH_CALLBACK, passing it the
4424 symbol's index in the mapped_index_base symbol table. */
4425
4426 static void
4427 dw2_expand_symtabs_matching_symbol
4428 (mapped_index_base &index,
4429 const lookup_name_info &lookup_name_in,
4430 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4431 enum search_domain kind,
4432 gdb::function_view<bool (offset_type)> match_callback)
4433 {
4434 lookup_name_info lookup_name_without_params
4435 = lookup_name_in.make_ignore_params ();
4436
4437 /* Build the symbol name component sorted vector, if we haven't
4438 yet. */
4439 index.build_name_components ();
4440
4441 /* The same symbol may appear more than once in the range though.
4442 E.g., if we're looking for symbols that complete "w", and we have
4443 a symbol named "w1::w2", we'll find the two name components for
4444 that same symbol in the range. To be sure we only call the
4445 callback once per symbol, we first collect the symbol name
4446 indexes that matched in a temporary vector and ignore
4447 duplicates. */
4448 std::vector<offset_type> matches;
4449
4450 struct name_and_matcher
4451 {
4452 symbol_name_matcher_ftype *matcher;
4453 const std::string &name;
4454
4455 bool operator== (const name_and_matcher &other) const
4456 {
4457 return matcher == other.matcher && name == other.name;
4458 }
4459 };
4460
4461 /* A vector holding all the different symbol name matchers, for all
4462 languages. */
4463 std::vector<name_and_matcher> matchers;
4464
4465 for (int i = 0; i < nr_languages; i++)
4466 {
4467 enum language lang_e = (enum language) i;
4468
4469 const language_defn *lang = language_def (lang_e);
4470 symbol_name_matcher_ftype *name_matcher
4471 = get_symbol_name_matcher (lang, lookup_name_without_params);
4472
4473 name_and_matcher key {
4474 name_matcher,
4475 lookup_name_without_params.language_lookup_name (lang_e)
4476 };
4477
4478 /* Don't insert the same comparison routine more than once.
4479 Note that we do this linear walk. This is not a problem in
4480 practice because the number of supported languages is
4481 low. */
4482 if (std::find (matchers.begin (), matchers.end (), key)
4483 != matchers.end ())
4484 continue;
4485 matchers.push_back (std::move (key));
4486
4487 auto bounds
4488 = index.find_name_components_bounds (lookup_name_without_params,
4489 lang_e);
4490
4491 /* Now for each symbol name in range, check to see if we have a name
4492 match, and if so, call the MATCH_CALLBACK callback. */
4493
4494 for (; bounds.first != bounds.second; ++bounds.first)
4495 {
4496 const char *qualified = index.symbol_name_at (bounds.first->idx);
4497
4498 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4499 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4500 continue;
4501
4502 matches.push_back (bounds.first->idx);
4503 }
4504 }
4505
4506 std::sort (matches.begin (), matches.end ());
4507
4508 /* Finally call the callback, once per match. */
4509 ULONGEST prev = -1;
4510 for (offset_type idx : matches)
4511 {
4512 if (prev != idx)
4513 {
4514 if (!match_callback (idx))
4515 break;
4516 prev = idx;
4517 }
4518 }
4519
4520 /* Above we use a type wider than idx's for 'prev', since 0 and
4521 (offset_type)-1 are both possible values. */
4522 static_assert (sizeof (prev) > sizeof (offset_type), "");
4523 }
4524
4525 #if GDB_SELF_TEST
4526
4527 namespace selftests { namespace dw2_expand_symtabs_matching {
4528
4529 /* A mock .gdb_index/.debug_names-like name index table, enough to
4530 exercise dw2_expand_symtabs_matching_symbol, which works with the
4531 mapped_index_base interface. Builds an index from the symbol list
4532 passed as parameter to the constructor. */
4533 class mock_mapped_index : public mapped_index_base
4534 {
4535 public:
4536 mock_mapped_index (gdb::array_view<const char *> symbols)
4537 : m_symbol_table (symbols)
4538 {}
4539
4540 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4541
4542 /* Return the number of names in the symbol table. */
4543 size_t symbol_name_count () const override
4544 {
4545 return m_symbol_table.size ();
4546 }
4547
4548 /* Get the name of the symbol at IDX in the symbol table. */
4549 const char *symbol_name_at (offset_type idx) const override
4550 {
4551 return m_symbol_table[idx];
4552 }
4553
4554 private:
4555 gdb::array_view<const char *> m_symbol_table;
4556 };
4557
4558 /* Convenience function that converts a NULL pointer to a "<null>"
4559 string, to pass to print routines. */
4560
4561 static const char *
4562 string_or_null (const char *str)
4563 {
4564 return str != NULL ? str : "<null>";
4565 }
4566
4567 /* Check if a lookup_name_info built from
4568 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4569 index. EXPECTED_LIST is the list of expected matches, in expected
4570 matching order. If no match expected, then an empty list is
4571 specified. Returns true on success. On failure prints a warning
4572 indicating the file:line that failed, and returns false. */
4573
4574 static bool
4575 check_match (const char *file, int line,
4576 mock_mapped_index &mock_index,
4577 const char *name, symbol_name_match_type match_type,
4578 bool completion_mode,
4579 std::initializer_list<const char *> expected_list)
4580 {
4581 lookup_name_info lookup_name (name, match_type, completion_mode);
4582
4583 bool matched = true;
4584
4585 auto mismatch = [&] (const char *expected_str,
4586 const char *got)
4587 {
4588 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4589 "expected=\"%s\", got=\"%s\"\n"),
4590 file, line,
4591 (match_type == symbol_name_match_type::FULL
4592 ? "FULL" : "WILD"),
4593 name, string_or_null (expected_str), string_or_null (got));
4594 matched = false;
4595 };
4596
4597 auto expected_it = expected_list.begin ();
4598 auto expected_end = expected_list.end ();
4599
4600 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4601 NULL, ALL_DOMAIN,
4602 [&] (offset_type idx)
4603 {
4604 const char *matched_name = mock_index.symbol_name_at (idx);
4605 const char *expected_str
4606 = expected_it == expected_end ? NULL : *expected_it++;
4607
4608 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4609 mismatch (expected_str, matched_name);
4610 return true;
4611 });
4612
4613 const char *expected_str
4614 = expected_it == expected_end ? NULL : *expected_it++;
4615 if (expected_str != NULL)
4616 mismatch (expected_str, NULL);
4617
4618 return matched;
4619 }
4620
4621 /* The symbols added to the mock mapped_index for testing (in
4622 canonical form). */
4623 static const char *test_symbols[] = {
4624 "function",
4625 "std::bar",
4626 "std::zfunction",
4627 "std::zfunction2",
4628 "w1::w2",
4629 "ns::foo<char*>",
4630 "ns::foo<int>",
4631 "ns::foo<long>",
4632 "ns2::tmpl<int>::foo2",
4633 "(anonymous namespace)::A::B::C",
4634
4635 /* These are used to check that the increment-last-char in the
4636 matching algorithm for completion doesn't match "t1_fund" when
4637 completing "t1_func". */
4638 "t1_func",
4639 "t1_func1",
4640 "t1_fund",
4641 "t1_fund1",
4642
4643 /* A UTF-8 name with multi-byte sequences to make sure that
4644 cp-name-parser understands this as a single identifier ("função"
4645 is "function" in PT). */
4646 u8"u8função",
4647
4648 /* \377 (0xff) is Latin1 'ÿ'. */
4649 "yfunc\377",
4650
4651 /* \377 (0xff) is Latin1 'ÿ'. */
4652 "\377",
4653 "\377\377123",
4654
4655 /* A name with all sorts of complications. Starts with "z" to make
4656 it easier for the completion tests below. */
4657 #define Z_SYM_NAME \
4658 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4659 "::tuple<(anonymous namespace)::ui*, " \
4660 "std::default_delete<(anonymous namespace)::ui>, void>"
4661
4662 Z_SYM_NAME
4663 };
4664
4665 /* Returns true if the mapped_index_base::find_name_component_bounds
4666 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4667 in completion mode. */
4668
4669 static bool
4670 check_find_bounds_finds (mapped_index_base &index,
4671 const char *search_name,
4672 gdb::array_view<const char *> expected_syms)
4673 {
4674 lookup_name_info lookup_name (search_name,
4675 symbol_name_match_type::FULL, true);
4676
4677 auto bounds = index.find_name_components_bounds (lookup_name,
4678 language_cplus);
4679
4680 size_t distance = std::distance (bounds.first, bounds.second);
4681 if (distance != expected_syms.size ())
4682 return false;
4683
4684 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4685 {
4686 auto nc_elem = bounds.first + exp_elem;
4687 const char *qualified = index.symbol_name_at (nc_elem->idx);
4688 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4689 return false;
4690 }
4691
4692 return true;
4693 }
4694
4695 /* Test the lower-level mapped_index::find_name_component_bounds
4696 method. */
4697
4698 static void
4699 test_mapped_index_find_name_component_bounds ()
4700 {
4701 mock_mapped_index mock_index (test_symbols);
4702
4703 mock_index.build_name_components ();
4704
4705 /* Test the lower-level mapped_index::find_name_component_bounds
4706 method in completion mode. */
4707 {
4708 static const char *expected_syms[] = {
4709 "t1_func",
4710 "t1_func1",
4711 };
4712
4713 SELF_CHECK (check_find_bounds_finds (mock_index,
4714 "t1_func", expected_syms));
4715 }
4716
4717 /* Check that the increment-last-char in the name matching algorithm
4718 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4719 {
4720 static const char *expected_syms1[] = {
4721 "\377",
4722 "\377\377123",
4723 };
4724 SELF_CHECK (check_find_bounds_finds (mock_index,
4725 "\377", expected_syms1));
4726
4727 static const char *expected_syms2[] = {
4728 "\377\377123",
4729 };
4730 SELF_CHECK (check_find_bounds_finds (mock_index,
4731 "\377\377", expected_syms2));
4732 }
4733 }
4734
4735 /* Test dw2_expand_symtabs_matching_symbol. */
4736
4737 static void
4738 test_dw2_expand_symtabs_matching_symbol ()
4739 {
4740 mock_mapped_index mock_index (test_symbols);
4741
4742 /* We let all tests run until the end even if some fails, for debug
4743 convenience. */
4744 bool any_mismatch = false;
4745
4746 /* Create the expected symbols list (an initializer_list). Needed
4747 because lists have commas, and we need to pass them to CHECK,
4748 which is a macro. */
4749 #define EXPECT(...) { __VA_ARGS__ }
4750
4751 /* Wrapper for check_match that passes down the current
4752 __FILE__/__LINE__. */
4753 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4754 any_mismatch |= !check_match (__FILE__, __LINE__, \
4755 mock_index, \
4756 NAME, MATCH_TYPE, COMPLETION_MODE, \
4757 EXPECTED_LIST)
4758
4759 /* Identity checks. */
4760 for (const char *sym : test_symbols)
4761 {
4762 /* Should be able to match all existing symbols. */
4763 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4764 EXPECT (sym));
4765
4766 /* Should be able to match all existing symbols with
4767 parameters. */
4768 std::string with_params = std::string (sym) + "(int)";
4769 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4770 EXPECT (sym));
4771
4772 /* Should be able to match all existing symbols with
4773 parameters and qualifiers. */
4774 with_params = std::string (sym) + " ( int ) const";
4775 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4776 EXPECT (sym));
4777
4778 /* This should really find sym, but cp-name-parser.y doesn't
4779 know about lvalue/rvalue qualifiers yet. */
4780 with_params = std::string (sym) + " ( int ) &&";
4781 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4782 {});
4783 }
4784
4785 /* Check that the name matching algorithm for completion doesn't get
4786 confused with Latin1 'ÿ' / 0xff. */
4787 {
4788 static const char str[] = "\377";
4789 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4790 EXPECT ("\377", "\377\377123"));
4791 }
4792
4793 /* Check that the increment-last-char in the matching algorithm for
4794 completion doesn't match "t1_fund" when completing "t1_func". */
4795 {
4796 static const char str[] = "t1_func";
4797 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4798 EXPECT ("t1_func", "t1_func1"));
4799 }
4800
4801 /* Check that completion mode works at each prefix of the expected
4802 symbol name. */
4803 {
4804 static const char str[] = "function(int)";
4805 size_t len = strlen (str);
4806 std::string lookup;
4807
4808 for (size_t i = 1; i < len; i++)
4809 {
4810 lookup.assign (str, i);
4811 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4812 EXPECT ("function"));
4813 }
4814 }
4815
4816 /* While "w" is a prefix of both components, the match function
4817 should still only be called once. */
4818 {
4819 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4820 EXPECT ("w1::w2"));
4821 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4822 EXPECT ("w1::w2"));
4823 }
4824
4825 /* Same, with a "complicated" symbol. */
4826 {
4827 static const char str[] = Z_SYM_NAME;
4828 size_t len = strlen (str);
4829 std::string lookup;
4830
4831 for (size_t i = 1; i < len; i++)
4832 {
4833 lookup.assign (str, i);
4834 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4835 EXPECT (Z_SYM_NAME));
4836 }
4837 }
4838
4839 /* In FULL mode, an incomplete symbol doesn't match. */
4840 {
4841 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4842 {});
4843 }
4844
4845 /* A complete symbol with parameters matches any overload, since the
4846 index has no overload info. */
4847 {
4848 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4849 EXPECT ("std::zfunction", "std::zfunction2"));
4850 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4851 EXPECT ("std::zfunction", "std::zfunction2"));
4852 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4853 EXPECT ("std::zfunction", "std::zfunction2"));
4854 }
4855
4856 /* Check that whitespace is ignored appropriately. A symbol with a
4857 template argument list. */
4858 {
4859 static const char expected[] = "ns::foo<int>";
4860 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4861 EXPECT (expected));
4862 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4863 EXPECT (expected));
4864 }
4865
4866 /* Check that whitespace is ignored appropriately. A symbol with a
4867 template argument list that includes a pointer. */
4868 {
4869 static const char expected[] = "ns::foo<char*>";
4870 /* Try both completion and non-completion modes. */
4871 static const bool completion_mode[2] = {false, true};
4872 for (size_t i = 0; i < 2; i++)
4873 {
4874 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4875 completion_mode[i], EXPECT (expected));
4876 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4877 completion_mode[i], EXPECT (expected));
4878
4879 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4880 completion_mode[i], EXPECT (expected));
4881 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4882 completion_mode[i], EXPECT (expected));
4883 }
4884 }
4885
4886 {
4887 /* Check method qualifiers are ignored. */
4888 static const char expected[] = "ns::foo<char*>";
4889 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4890 symbol_name_match_type::FULL, true, EXPECT (expected));
4891 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4892 symbol_name_match_type::FULL, true, EXPECT (expected));
4893 CHECK_MATCH ("foo < char * > ( int ) const",
4894 symbol_name_match_type::WILD, true, EXPECT (expected));
4895 CHECK_MATCH ("foo < char * > ( int ) &&",
4896 symbol_name_match_type::WILD, true, EXPECT (expected));
4897 }
4898
4899 /* Test lookup names that don't match anything. */
4900 {
4901 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4902 {});
4903
4904 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4905 {});
4906 }
4907
4908 /* Some wild matching tests, exercising "(anonymous namespace)",
4909 which should not be confused with a parameter list. */
4910 {
4911 static const char *syms[] = {
4912 "A::B::C",
4913 "B::C",
4914 "C",
4915 "A :: B :: C ( int )",
4916 "B :: C ( int )",
4917 "C ( int )",
4918 };
4919
4920 for (const char *s : syms)
4921 {
4922 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4923 EXPECT ("(anonymous namespace)::A::B::C"));
4924 }
4925 }
4926
4927 {
4928 static const char expected[] = "ns2::tmpl<int>::foo2";
4929 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4930 EXPECT (expected));
4931 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4932 EXPECT (expected));
4933 }
4934
4935 SELF_CHECK (!any_mismatch);
4936
4937 #undef EXPECT
4938 #undef CHECK_MATCH
4939 }
4940
4941 static void
4942 run_test ()
4943 {
4944 test_mapped_index_find_name_component_bounds ();
4945 test_dw2_expand_symtabs_matching_symbol ();
4946 }
4947
4948 }} // namespace selftests::dw2_expand_symtabs_matching
4949
4950 #endif /* GDB_SELF_TEST */
4951
4952 /* If FILE_MATCHER is NULL or if PER_CU has
4953 dwarf2_per_cu_quick_data::MARK set (see
4954 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4955 EXPANSION_NOTIFY on it. */
4956
4957 static void
4958 dw2_expand_symtabs_matching_one
4959 (struct dwarf2_per_cu_data *per_cu,
4960 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4961 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4962 {
4963 if (file_matcher == NULL || per_cu->v.quick->mark)
4964 {
4965 bool symtab_was_null
4966 = (per_cu->v.quick->compunit_symtab == NULL);
4967
4968 dw2_instantiate_symtab (per_cu, false);
4969
4970 if (expansion_notify != NULL
4971 && symtab_was_null
4972 && per_cu->v.quick->compunit_symtab != NULL)
4973 expansion_notify (per_cu->v.quick->compunit_symtab);
4974 }
4975 }
4976
4977 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4978 matched, to expand corresponding CUs that were marked. IDX is the
4979 index of the symbol name that matched. */
4980
4981 static void
4982 dw2_expand_marked_cus
4983 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4984 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4985 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4986 search_domain kind)
4987 {
4988 offset_type *vec, vec_len, vec_idx;
4989 bool global_seen = false;
4990 mapped_index &index = *dwarf2_per_objfile->index_table;
4991
4992 vec = (offset_type *) (index.constant_pool
4993 + MAYBE_SWAP (index.symbol_table[idx].vec));
4994 vec_len = MAYBE_SWAP (vec[0]);
4995 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4996 {
4997 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4998 /* This value is only valid for index versions >= 7. */
4999 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5000 gdb_index_symbol_kind symbol_kind =
5001 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5002 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5003 /* Only check the symbol attributes if they're present.
5004 Indices prior to version 7 don't record them,
5005 and indices >= 7 may elide them for certain symbols
5006 (gold does this). */
5007 int attrs_valid =
5008 (index.version >= 7
5009 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5010
5011 /* Work around gold/15646. */
5012 if (attrs_valid)
5013 {
5014 if (!is_static && global_seen)
5015 continue;
5016 if (!is_static)
5017 global_seen = true;
5018 }
5019
5020 /* Only check the symbol's kind if it has one. */
5021 if (attrs_valid)
5022 {
5023 switch (kind)
5024 {
5025 case VARIABLES_DOMAIN:
5026 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5027 continue;
5028 break;
5029 case FUNCTIONS_DOMAIN:
5030 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5031 continue;
5032 break;
5033 case TYPES_DOMAIN:
5034 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5035 continue;
5036 break;
5037 default:
5038 break;
5039 }
5040 }
5041
5042 /* Don't crash on bad data. */
5043 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
5044 + dwarf2_per_objfile->all_type_units.size ()))
5045 {
5046 complaint (_(".gdb_index entry has bad CU index"
5047 " [in module %s]"),
5048 objfile_name (dwarf2_per_objfile->objfile));
5049 continue;
5050 }
5051
5052 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
5053 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5054 expansion_notify);
5055 }
5056 }
5057
5058 /* If FILE_MATCHER is non-NULL, set all the
5059 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5060 that match FILE_MATCHER. */
5061
5062 static void
5063 dw_expand_symtabs_matching_file_matcher
5064 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5065 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5066 {
5067 if (file_matcher == NULL)
5068 return;
5069
5070 objfile *const objfile = dwarf2_per_objfile->objfile;
5071
5072 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5073 htab_eq_pointer,
5074 NULL, xcalloc, xfree));
5075 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5076 htab_eq_pointer,
5077 NULL, xcalloc, xfree));
5078
5079 /* The rule is CUs specify all the files, including those used by
5080 any TU, so there's no need to scan TUs here. */
5081
5082 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5083 {
5084 QUIT;
5085
5086 per_cu->v.quick->mark = 0;
5087
5088 /* We only need to look at symtabs not already expanded. */
5089 if (per_cu->v.quick->compunit_symtab)
5090 continue;
5091
5092 quick_file_names *file_data = dw2_get_file_names (per_cu);
5093 if (file_data == NULL)
5094 continue;
5095
5096 if (htab_find (visited_not_found.get (), file_data) != NULL)
5097 continue;
5098 else if (htab_find (visited_found.get (), file_data) != NULL)
5099 {
5100 per_cu->v.quick->mark = 1;
5101 continue;
5102 }
5103
5104 for (int j = 0; j < file_data->num_file_names; ++j)
5105 {
5106 const char *this_real_name;
5107
5108 if (file_matcher (file_data->file_names[j], false))
5109 {
5110 per_cu->v.quick->mark = 1;
5111 break;
5112 }
5113
5114 /* Before we invoke realpath, which can get expensive when many
5115 files are involved, do a quick comparison of the basenames. */
5116 if (!basenames_may_differ
5117 && !file_matcher (lbasename (file_data->file_names[j]),
5118 true))
5119 continue;
5120
5121 this_real_name = dw2_get_real_path (objfile, file_data, j);
5122 if (file_matcher (this_real_name, false))
5123 {
5124 per_cu->v.quick->mark = 1;
5125 break;
5126 }
5127 }
5128
5129 void **slot = htab_find_slot (per_cu->v.quick->mark
5130 ? visited_found.get ()
5131 : visited_not_found.get (),
5132 file_data, INSERT);
5133 *slot = file_data;
5134 }
5135 }
5136
5137 static void
5138 dw2_expand_symtabs_matching
5139 (struct objfile *objfile,
5140 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5141 const lookup_name_info &lookup_name,
5142 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5143 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5144 enum search_domain kind)
5145 {
5146 struct dwarf2_per_objfile *dwarf2_per_objfile
5147 = get_dwarf2_per_objfile (objfile);
5148
5149 /* index_table is NULL if OBJF_READNOW. */
5150 if (!dwarf2_per_objfile->index_table)
5151 return;
5152
5153 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5154
5155 mapped_index &index = *dwarf2_per_objfile->index_table;
5156
5157 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5158 symbol_matcher,
5159 kind, [&] (offset_type idx)
5160 {
5161 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5162 expansion_notify, kind);
5163 return true;
5164 });
5165 }
5166
5167 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5168 symtab. */
5169
5170 static struct compunit_symtab *
5171 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5172 CORE_ADDR pc)
5173 {
5174 int i;
5175
5176 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5177 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5178 return cust;
5179
5180 if (cust->includes == NULL)
5181 return NULL;
5182
5183 for (i = 0; cust->includes[i]; ++i)
5184 {
5185 struct compunit_symtab *s = cust->includes[i];
5186
5187 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5188 if (s != NULL)
5189 return s;
5190 }
5191
5192 return NULL;
5193 }
5194
5195 static struct compunit_symtab *
5196 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5197 struct bound_minimal_symbol msymbol,
5198 CORE_ADDR pc,
5199 struct obj_section *section,
5200 int warn_if_readin)
5201 {
5202 struct dwarf2_per_cu_data *data;
5203 struct compunit_symtab *result;
5204
5205 if (!objfile->partial_symtabs->psymtabs_addrmap)
5206 return NULL;
5207
5208 CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
5209 SECT_OFF_TEXT (objfile));
5210 data = (struct dwarf2_per_cu_data *) addrmap_find
5211 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
5212 if (!data)
5213 return NULL;
5214
5215 if (warn_if_readin && data->v.quick->compunit_symtab)
5216 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5217 paddress (get_objfile_arch (objfile), pc));
5218
5219 result
5220 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5221 false),
5222 pc);
5223 gdb_assert (result != NULL);
5224 return result;
5225 }
5226
5227 static void
5228 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5229 void *data, int need_fullname)
5230 {
5231 struct dwarf2_per_objfile *dwarf2_per_objfile
5232 = get_dwarf2_per_objfile (objfile);
5233
5234 if (!dwarf2_per_objfile->filenames_cache)
5235 {
5236 dwarf2_per_objfile->filenames_cache.emplace ();
5237
5238 htab_up visited (htab_create_alloc (10,
5239 htab_hash_pointer, htab_eq_pointer,
5240 NULL, xcalloc, xfree));
5241
5242 /* The rule is CUs specify all the files, including those used
5243 by any TU, so there's no need to scan TUs here. We can
5244 ignore file names coming from already-expanded CUs. */
5245
5246 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5247 {
5248 if (per_cu->v.quick->compunit_symtab)
5249 {
5250 void **slot = htab_find_slot (visited.get (),
5251 per_cu->v.quick->file_names,
5252 INSERT);
5253
5254 *slot = per_cu->v.quick->file_names;
5255 }
5256 }
5257
5258 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5259 {
5260 /* We only need to look at symtabs not already expanded. */
5261 if (per_cu->v.quick->compunit_symtab)
5262 continue;
5263
5264 quick_file_names *file_data = dw2_get_file_names (per_cu);
5265 if (file_data == NULL)
5266 continue;
5267
5268 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5269 if (*slot)
5270 {
5271 /* Already visited. */
5272 continue;
5273 }
5274 *slot = file_data;
5275
5276 for (int j = 0; j < file_data->num_file_names; ++j)
5277 {
5278 const char *filename = file_data->file_names[j];
5279 dwarf2_per_objfile->filenames_cache->seen (filename);
5280 }
5281 }
5282 }
5283
5284 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5285 {
5286 gdb::unique_xmalloc_ptr<char> this_real_name;
5287
5288 if (need_fullname)
5289 this_real_name = gdb_realpath (filename);
5290 (*fun) (filename, this_real_name.get (), data);
5291 });
5292 }
5293
5294 static int
5295 dw2_has_symbols (struct objfile *objfile)
5296 {
5297 return 1;
5298 }
5299
5300 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5301 {
5302 dw2_has_symbols,
5303 dw2_find_last_source_symtab,
5304 dw2_forget_cached_source_info,
5305 dw2_map_symtabs_matching_filename,
5306 dw2_lookup_symbol,
5307 dw2_print_stats,
5308 dw2_dump,
5309 dw2_expand_symtabs_for_function,
5310 dw2_expand_all_symtabs,
5311 dw2_expand_symtabs_with_fullname,
5312 dw2_map_matching_symbols,
5313 dw2_expand_symtabs_matching,
5314 dw2_find_pc_sect_compunit_symtab,
5315 NULL,
5316 dw2_map_symbol_filenames
5317 };
5318
5319 /* DWARF-5 debug_names reader. */
5320
5321 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5322 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5323
5324 /* A helper function that reads the .debug_names section in SECTION
5325 and fills in MAP. FILENAME is the name of the file containing the
5326 section; it is used for error reporting.
5327
5328 Returns true if all went well, false otherwise. */
5329
5330 static bool
5331 read_debug_names_from_section (struct objfile *objfile,
5332 const char *filename,
5333 struct dwarf2_section_info *section,
5334 mapped_debug_names &map)
5335 {
5336 if (dwarf2_section_empty_p (section))
5337 return false;
5338
5339 /* Older elfutils strip versions could keep the section in the main
5340 executable while splitting it for the separate debug info file. */
5341 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5342 return false;
5343
5344 dwarf2_read_section (objfile, section);
5345
5346 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5347
5348 const gdb_byte *addr = section->buffer;
5349
5350 bfd *const abfd = get_section_bfd_owner (section);
5351
5352 unsigned int bytes_read;
5353 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5354 addr += bytes_read;
5355
5356 map.dwarf5_is_dwarf64 = bytes_read != 4;
5357 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5358 if (bytes_read + length != section->size)
5359 {
5360 /* There may be multiple per-CU indices. */
5361 warning (_("Section .debug_names in %s length %s does not match "
5362 "section length %s, ignoring .debug_names."),
5363 filename, plongest (bytes_read + length),
5364 pulongest (section->size));
5365 return false;
5366 }
5367
5368 /* The version number. */
5369 uint16_t version = read_2_bytes (abfd, addr);
5370 addr += 2;
5371 if (version != 5)
5372 {
5373 warning (_("Section .debug_names in %s has unsupported version %d, "
5374 "ignoring .debug_names."),
5375 filename, version);
5376 return false;
5377 }
5378
5379 /* Padding. */
5380 uint16_t padding = read_2_bytes (abfd, addr);
5381 addr += 2;
5382 if (padding != 0)
5383 {
5384 warning (_("Section .debug_names in %s has unsupported padding %d, "
5385 "ignoring .debug_names."),
5386 filename, padding);
5387 return false;
5388 }
5389
5390 /* comp_unit_count - The number of CUs in the CU list. */
5391 map.cu_count = read_4_bytes (abfd, addr);
5392 addr += 4;
5393
5394 /* local_type_unit_count - The number of TUs in the local TU
5395 list. */
5396 map.tu_count = read_4_bytes (abfd, addr);
5397 addr += 4;
5398
5399 /* foreign_type_unit_count - The number of TUs in the foreign TU
5400 list. */
5401 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5402 addr += 4;
5403 if (foreign_tu_count != 0)
5404 {
5405 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5406 "ignoring .debug_names."),
5407 filename, static_cast<unsigned long> (foreign_tu_count));
5408 return false;
5409 }
5410
5411 /* bucket_count - The number of hash buckets in the hash lookup
5412 table. */
5413 map.bucket_count = read_4_bytes (abfd, addr);
5414 addr += 4;
5415
5416 /* name_count - The number of unique names in the index. */
5417 map.name_count = read_4_bytes (abfd, addr);
5418 addr += 4;
5419
5420 /* abbrev_table_size - The size in bytes of the abbreviations
5421 table. */
5422 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5423 addr += 4;
5424
5425 /* augmentation_string_size - The size in bytes of the augmentation
5426 string. This value is rounded up to a multiple of 4. */
5427 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5428 addr += 4;
5429 map.augmentation_is_gdb = ((augmentation_string_size
5430 == sizeof (dwarf5_augmentation))
5431 && memcmp (addr, dwarf5_augmentation,
5432 sizeof (dwarf5_augmentation)) == 0);
5433 augmentation_string_size += (-augmentation_string_size) & 3;
5434 addr += augmentation_string_size;
5435
5436 /* List of CUs */
5437 map.cu_table_reordered = addr;
5438 addr += map.cu_count * map.offset_size;
5439
5440 /* List of Local TUs */
5441 map.tu_table_reordered = addr;
5442 addr += map.tu_count * map.offset_size;
5443
5444 /* Hash Lookup Table */
5445 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5446 addr += map.bucket_count * 4;
5447 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5448 addr += map.name_count * 4;
5449
5450 /* Name Table */
5451 map.name_table_string_offs_reordered = addr;
5452 addr += map.name_count * map.offset_size;
5453 map.name_table_entry_offs_reordered = addr;
5454 addr += map.name_count * map.offset_size;
5455
5456 const gdb_byte *abbrev_table_start = addr;
5457 for (;;)
5458 {
5459 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5460 addr += bytes_read;
5461 if (index_num == 0)
5462 break;
5463
5464 const auto insertpair
5465 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5466 if (!insertpair.second)
5467 {
5468 warning (_("Section .debug_names in %s has duplicate index %s, "
5469 "ignoring .debug_names."),
5470 filename, pulongest (index_num));
5471 return false;
5472 }
5473 mapped_debug_names::index_val &indexval = insertpair.first->second;
5474 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5475 addr += bytes_read;
5476
5477 for (;;)
5478 {
5479 mapped_debug_names::index_val::attr attr;
5480 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5481 addr += bytes_read;
5482 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5483 addr += bytes_read;
5484 if (attr.form == DW_FORM_implicit_const)
5485 {
5486 attr.implicit_const = read_signed_leb128 (abfd, addr,
5487 &bytes_read);
5488 addr += bytes_read;
5489 }
5490 if (attr.dw_idx == 0 && attr.form == 0)
5491 break;
5492 indexval.attr_vec.push_back (std::move (attr));
5493 }
5494 }
5495 if (addr != abbrev_table_start + abbrev_table_size)
5496 {
5497 warning (_("Section .debug_names in %s has abbreviation_table "
5498 "of size %s vs. written as %u, ignoring .debug_names."),
5499 filename, plongest (addr - abbrev_table_start),
5500 abbrev_table_size);
5501 return false;
5502 }
5503 map.entry_pool = addr;
5504
5505 return true;
5506 }
5507
5508 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5509 list. */
5510
5511 static void
5512 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5513 const mapped_debug_names &map,
5514 dwarf2_section_info &section,
5515 bool is_dwz)
5516 {
5517 sect_offset sect_off_prev;
5518 for (uint32_t i = 0; i <= map.cu_count; ++i)
5519 {
5520 sect_offset sect_off_next;
5521 if (i < map.cu_count)
5522 {
5523 sect_off_next
5524 = (sect_offset) (extract_unsigned_integer
5525 (map.cu_table_reordered + i * map.offset_size,
5526 map.offset_size,
5527 map.dwarf5_byte_order));
5528 }
5529 else
5530 sect_off_next = (sect_offset) section.size;
5531 if (i >= 1)
5532 {
5533 const ULONGEST length = sect_off_next - sect_off_prev;
5534 dwarf2_per_cu_data *per_cu
5535 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5536 sect_off_prev, length);
5537 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5538 }
5539 sect_off_prev = sect_off_next;
5540 }
5541 }
5542
5543 /* Read the CU list from the mapped index, and use it to create all
5544 the CU objects for this dwarf2_per_objfile. */
5545
5546 static void
5547 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5548 const mapped_debug_names &map,
5549 const mapped_debug_names &dwz_map)
5550 {
5551 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5552 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5553
5554 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5555 dwarf2_per_objfile->info,
5556 false /* is_dwz */);
5557
5558 if (dwz_map.cu_count == 0)
5559 return;
5560
5561 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5562 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5563 true /* is_dwz */);
5564 }
5565
5566 /* Read .debug_names. If everything went ok, initialize the "quick"
5567 elements of all the CUs and return true. Otherwise, return false. */
5568
5569 static bool
5570 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5571 {
5572 std::unique_ptr<mapped_debug_names> map
5573 (new mapped_debug_names (dwarf2_per_objfile));
5574 mapped_debug_names dwz_map (dwarf2_per_objfile);
5575 struct objfile *objfile = dwarf2_per_objfile->objfile;
5576
5577 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5578 &dwarf2_per_objfile->debug_names,
5579 *map))
5580 return false;
5581
5582 /* Don't use the index if it's empty. */
5583 if (map->name_count == 0)
5584 return false;
5585
5586 /* If there is a .dwz file, read it so we can get its CU list as
5587 well. */
5588 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5589 if (dwz != NULL)
5590 {
5591 if (!read_debug_names_from_section (objfile,
5592 bfd_get_filename (dwz->dwz_bfd.get ()),
5593 &dwz->debug_names, dwz_map))
5594 {
5595 warning (_("could not read '.debug_names' section from %s; skipping"),
5596 bfd_get_filename (dwz->dwz_bfd.get ()));
5597 return false;
5598 }
5599 }
5600
5601 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5602
5603 if (map->tu_count != 0)
5604 {
5605 /* We can only handle a single .debug_types when we have an
5606 index. */
5607 if (dwarf2_per_objfile->types.size () != 1)
5608 return false;
5609
5610 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5611
5612 create_signatured_type_table_from_debug_names
5613 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5614 }
5615
5616 create_addrmap_from_aranges (dwarf2_per_objfile,
5617 &dwarf2_per_objfile->debug_aranges);
5618
5619 dwarf2_per_objfile->debug_names_table = std::move (map);
5620 dwarf2_per_objfile->using_index = 1;
5621 dwarf2_per_objfile->quick_file_names_table =
5622 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5623
5624 return true;
5625 }
5626
5627 /* Type used to manage iterating over all CUs looking for a symbol for
5628 .debug_names. */
5629
5630 class dw2_debug_names_iterator
5631 {
5632 public:
5633 dw2_debug_names_iterator (const mapped_debug_names &map,
5634 gdb::optional<block_enum> block_index,
5635 domain_enum domain,
5636 const char *name)
5637 : m_map (map), m_block_index (block_index), m_domain (domain),
5638 m_addr (find_vec_in_debug_names (map, name))
5639 {}
5640
5641 dw2_debug_names_iterator (const mapped_debug_names &map,
5642 search_domain search, uint32_t namei)
5643 : m_map (map),
5644 m_search (search),
5645 m_addr (find_vec_in_debug_names (map, namei))
5646 {}
5647
5648 dw2_debug_names_iterator (const mapped_debug_names &map,
5649 block_enum block_index, domain_enum domain,
5650 uint32_t namei)
5651 : m_map (map), m_block_index (block_index), m_domain (domain),
5652 m_addr (find_vec_in_debug_names (map, namei))
5653 {}
5654
5655 /* Return the next matching CU or NULL if there are no more. */
5656 dwarf2_per_cu_data *next ();
5657
5658 private:
5659 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5660 const char *name);
5661 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5662 uint32_t namei);
5663
5664 /* The internalized form of .debug_names. */
5665 const mapped_debug_names &m_map;
5666
5667 /* If set, only look for symbols that match that block. Valid values are
5668 GLOBAL_BLOCK and STATIC_BLOCK. */
5669 const gdb::optional<block_enum> m_block_index;
5670
5671 /* The kind of symbol we're looking for. */
5672 const domain_enum m_domain = UNDEF_DOMAIN;
5673 const search_domain m_search = ALL_DOMAIN;
5674
5675 /* The list of CUs from the index entry of the symbol, or NULL if
5676 not found. */
5677 const gdb_byte *m_addr;
5678 };
5679
5680 const char *
5681 mapped_debug_names::namei_to_name (uint32_t namei) const
5682 {
5683 const ULONGEST namei_string_offs
5684 = extract_unsigned_integer ((name_table_string_offs_reordered
5685 + namei * offset_size),
5686 offset_size,
5687 dwarf5_byte_order);
5688 return read_indirect_string_at_offset
5689 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5690 }
5691
5692 /* Find a slot in .debug_names for the object named NAME. If NAME is
5693 found, return pointer to its pool data. If NAME cannot be found,
5694 return NULL. */
5695
5696 const gdb_byte *
5697 dw2_debug_names_iterator::find_vec_in_debug_names
5698 (const mapped_debug_names &map, const char *name)
5699 {
5700 int (*cmp) (const char *, const char *);
5701
5702 gdb::unique_xmalloc_ptr<char> without_params;
5703 if (current_language->la_language == language_cplus
5704 || current_language->la_language == language_fortran
5705 || current_language->la_language == language_d)
5706 {
5707 /* NAME is already canonical. Drop any qualifiers as
5708 .debug_names does not contain any. */
5709
5710 if (strchr (name, '(') != NULL)
5711 {
5712 without_params = cp_remove_params (name);
5713 if (without_params != NULL)
5714 name = without_params.get ();
5715 }
5716 }
5717
5718 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5719
5720 const uint32_t full_hash = dwarf5_djb_hash (name);
5721 uint32_t namei
5722 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5723 (map.bucket_table_reordered
5724 + (full_hash % map.bucket_count)), 4,
5725 map.dwarf5_byte_order);
5726 if (namei == 0)
5727 return NULL;
5728 --namei;
5729 if (namei >= map.name_count)
5730 {
5731 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5732 "[in module %s]"),
5733 namei, map.name_count,
5734 objfile_name (map.dwarf2_per_objfile->objfile));
5735 return NULL;
5736 }
5737
5738 for (;;)
5739 {
5740 const uint32_t namei_full_hash
5741 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5742 (map.hash_table_reordered + namei), 4,
5743 map.dwarf5_byte_order);
5744 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5745 return NULL;
5746
5747 if (full_hash == namei_full_hash)
5748 {
5749 const char *const namei_string = map.namei_to_name (namei);
5750
5751 #if 0 /* An expensive sanity check. */
5752 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5753 {
5754 complaint (_("Wrong .debug_names hash for string at index %u "
5755 "[in module %s]"),
5756 namei, objfile_name (dwarf2_per_objfile->objfile));
5757 return NULL;
5758 }
5759 #endif
5760
5761 if (cmp (namei_string, name) == 0)
5762 {
5763 const ULONGEST namei_entry_offs
5764 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5765 + namei * map.offset_size),
5766 map.offset_size, map.dwarf5_byte_order);
5767 return map.entry_pool + namei_entry_offs;
5768 }
5769 }
5770
5771 ++namei;
5772 if (namei >= map.name_count)
5773 return NULL;
5774 }
5775 }
5776
5777 const gdb_byte *
5778 dw2_debug_names_iterator::find_vec_in_debug_names
5779 (const mapped_debug_names &map, uint32_t namei)
5780 {
5781 if (namei >= map.name_count)
5782 {
5783 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5784 "[in module %s]"),
5785 namei, map.name_count,
5786 objfile_name (map.dwarf2_per_objfile->objfile));
5787 return NULL;
5788 }
5789
5790 const ULONGEST namei_entry_offs
5791 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5792 + namei * map.offset_size),
5793 map.offset_size, map.dwarf5_byte_order);
5794 return map.entry_pool + namei_entry_offs;
5795 }
5796
5797 /* See dw2_debug_names_iterator. */
5798
5799 dwarf2_per_cu_data *
5800 dw2_debug_names_iterator::next ()
5801 {
5802 if (m_addr == NULL)
5803 return NULL;
5804
5805 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5806 struct objfile *objfile = dwarf2_per_objfile->objfile;
5807 bfd *const abfd = objfile->obfd;
5808
5809 again:
5810
5811 unsigned int bytes_read;
5812 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5813 m_addr += bytes_read;
5814 if (abbrev == 0)
5815 return NULL;
5816
5817 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5818 if (indexval_it == m_map.abbrev_map.cend ())
5819 {
5820 complaint (_("Wrong .debug_names undefined abbrev code %s "
5821 "[in module %s]"),
5822 pulongest (abbrev), objfile_name (objfile));
5823 return NULL;
5824 }
5825 const mapped_debug_names::index_val &indexval = indexval_it->second;
5826 enum class symbol_linkage {
5827 unknown,
5828 static_,
5829 extern_,
5830 } symbol_linkage_ = symbol_linkage::unknown;
5831 dwarf2_per_cu_data *per_cu = NULL;
5832 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5833 {
5834 ULONGEST ull;
5835 switch (attr.form)
5836 {
5837 case DW_FORM_implicit_const:
5838 ull = attr.implicit_const;
5839 break;
5840 case DW_FORM_flag_present:
5841 ull = 1;
5842 break;
5843 case DW_FORM_udata:
5844 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5845 m_addr += bytes_read;
5846 break;
5847 default:
5848 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5849 dwarf_form_name (attr.form),
5850 objfile_name (objfile));
5851 return NULL;
5852 }
5853 switch (attr.dw_idx)
5854 {
5855 case DW_IDX_compile_unit:
5856 /* Don't crash on bad data. */
5857 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5858 {
5859 complaint (_(".debug_names entry has bad CU index %s"
5860 " [in module %s]"),
5861 pulongest (ull),
5862 objfile_name (dwarf2_per_objfile->objfile));
5863 continue;
5864 }
5865 per_cu = dwarf2_per_objfile->get_cutu (ull);
5866 break;
5867 case DW_IDX_type_unit:
5868 /* Don't crash on bad data. */
5869 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5870 {
5871 complaint (_(".debug_names entry has bad TU index %s"
5872 " [in module %s]"),
5873 pulongest (ull),
5874 objfile_name (dwarf2_per_objfile->objfile));
5875 continue;
5876 }
5877 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5878 break;
5879 case DW_IDX_GNU_internal:
5880 if (!m_map.augmentation_is_gdb)
5881 break;
5882 symbol_linkage_ = symbol_linkage::static_;
5883 break;
5884 case DW_IDX_GNU_external:
5885 if (!m_map.augmentation_is_gdb)
5886 break;
5887 symbol_linkage_ = symbol_linkage::extern_;
5888 break;
5889 }
5890 }
5891
5892 /* Skip if already read in. */
5893 if (per_cu->v.quick->compunit_symtab)
5894 goto again;
5895
5896 /* Check static vs global. */
5897 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5898 {
5899 const bool want_static = *m_block_index == STATIC_BLOCK;
5900 const bool symbol_is_static =
5901 symbol_linkage_ == symbol_linkage::static_;
5902 if (want_static != symbol_is_static)
5903 goto again;
5904 }
5905
5906 /* Match dw2_symtab_iter_next, symbol_kind
5907 and debug_names::psymbol_tag. */
5908 switch (m_domain)
5909 {
5910 case VAR_DOMAIN:
5911 switch (indexval.dwarf_tag)
5912 {
5913 case DW_TAG_variable:
5914 case DW_TAG_subprogram:
5915 /* Some types are also in VAR_DOMAIN. */
5916 case DW_TAG_typedef:
5917 case DW_TAG_structure_type:
5918 break;
5919 default:
5920 goto again;
5921 }
5922 break;
5923 case STRUCT_DOMAIN:
5924 switch (indexval.dwarf_tag)
5925 {
5926 case DW_TAG_typedef:
5927 case DW_TAG_structure_type:
5928 break;
5929 default:
5930 goto again;
5931 }
5932 break;
5933 case LABEL_DOMAIN:
5934 switch (indexval.dwarf_tag)
5935 {
5936 case 0:
5937 case DW_TAG_variable:
5938 break;
5939 default:
5940 goto again;
5941 }
5942 break;
5943 default:
5944 break;
5945 }
5946
5947 /* Match dw2_expand_symtabs_matching, symbol_kind and
5948 debug_names::psymbol_tag. */
5949 switch (m_search)
5950 {
5951 case VARIABLES_DOMAIN:
5952 switch (indexval.dwarf_tag)
5953 {
5954 case DW_TAG_variable:
5955 break;
5956 default:
5957 goto again;
5958 }
5959 break;
5960 case FUNCTIONS_DOMAIN:
5961 switch (indexval.dwarf_tag)
5962 {
5963 case DW_TAG_subprogram:
5964 break;
5965 default:
5966 goto again;
5967 }
5968 break;
5969 case TYPES_DOMAIN:
5970 switch (indexval.dwarf_tag)
5971 {
5972 case DW_TAG_typedef:
5973 case DW_TAG_structure_type:
5974 break;
5975 default:
5976 goto again;
5977 }
5978 break;
5979 default:
5980 break;
5981 }
5982
5983 return per_cu;
5984 }
5985
5986 static struct compunit_symtab *
5987 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5988 const char *name, domain_enum domain)
5989 {
5990 struct dwarf2_per_objfile *dwarf2_per_objfile
5991 = get_dwarf2_per_objfile (objfile);
5992
5993 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5994 if (!mapp)
5995 {
5996 /* index is NULL if OBJF_READNOW. */
5997 return NULL;
5998 }
5999 const auto &map = *mapp;
6000
6001 dw2_debug_names_iterator iter (map, block_index, domain, name);
6002
6003 struct compunit_symtab *stab_best = NULL;
6004 struct dwarf2_per_cu_data *per_cu;
6005 while ((per_cu = iter.next ()) != NULL)
6006 {
6007 struct symbol *sym, *with_opaque = NULL;
6008 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
6009 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6010 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6011
6012 sym = block_find_symbol (block, name, domain,
6013 block_find_non_opaque_type_preferred,
6014 &with_opaque);
6015
6016 /* Some caution must be observed with overloaded functions and
6017 methods, since the index will not contain any overload
6018 information (but NAME might contain it). */
6019
6020 if (sym != NULL
6021 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6022 return stab;
6023 if (with_opaque != NULL
6024 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6025 stab_best = stab;
6026
6027 /* Keep looking through other CUs. */
6028 }
6029
6030 return stab_best;
6031 }
6032
6033 /* This dumps minimal information about .debug_names. It is called
6034 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6035 uses this to verify that .debug_names has been loaded. */
6036
6037 static void
6038 dw2_debug_names_dump (struct objfile *objfile)
6039 {
6040 struct dwarf2_per_objfile *dwarf2_per_objfile
6041 = get_dwarf2_per_objfile (objfile);
6042
6043 gdb_assert (dwarf2_per_objfile->using_index);
6044 printf_filtered (".debug_names:");
6045 if (dwarf2_per_objfile->debug_names_table)
6046 printf_filtered (" exists\n");
6047 else
6048 printf_filtered (" faked for \"readnow\"\n");
6049 printf_filtered ("\n");
6050 }
6051
6052 static void
6053 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6054 const char *func_name)
6055 {
6056 struct dwarf2_per_objfile *dwarf2_per_objfile
6057 = get_dwarf2_per_objfile (objfile);
6058
6059 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6060 if (dwarf2_per_objfile->debug_names_table)
6061 {
6062 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6063
6064 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
6065
6066 struct dwarf2_per_cu_data *per_cu;
6067 while ((per_cu = iter.next ()) != NULL)
6068 dw2_instantiate_symtab (per_cu, false);
6069 }
6070 }
6071
6072 static void
6073 dw2_debug_names_map_matching_symbols
6074 (struct objfile *objfile,
6075 const lookup_name_info &name, domain_enum domain,
6076 int global,
6077 gdb::function_view<symbol_found_callback_ftype> callback,
6078 symbol_compare_ftype *ordered_compare)
6079 {
6080 struct dwarf2_per_objfile *dwarf2_per_objfile
6081 = get_dwarf2_per_objfile (objfile);
6082
6083 /* debug_names_table is NULL if OBJF_READNOW. */
6084 if (!dwarf2_per_objfile->debug_names_table)
6085 return;
6086
6087 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6088 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
6089
6090 const char *match_name = name.ada ().lookup_name ().c_str ();
6091 auto matcher = [&] (const char *symname)
6092 {
6093 if (ordered_compare == nullptr)
6094 return true;
6095 return ordered_compare (symname, match_name) == 0;
6096 };
6097
6098 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
6099 [&] (offset_type namei)
6100 {
6101 /* The name was matched, now expand corresponding CUs that were
6102 marked. */
6103 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
6104
6105 struct dwarf2_per_cu_data *per_cu;
6106 while ((per_cu = iter.next ()) != NULL)
6107 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
6108 return true;
6109 });
6110
6111 /* It's a shame we couldn't do this inside the
6112 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6113 that have already been expanded. Instead, this loop matches what
6114 the psymtab code does. */
6115 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
6116 {
6117 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
6118 if (cust != nullptr)
6119 {
6120 const struct block *block
6121 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
6122 if (!iterate_over_symbols_terminated (block, name,
6123 domain, callback))
6124 break;
6125 }
6126 }
6127 }
6128
6129 static void
6130 dw2_debug_names_expand_symtabs_matching
6131 (struct objfile *objfile,
6132 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6133 const lookup_name_info &lookup_name,
6134 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6135 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6136 enum search_domain kind)
6137 {
6138 struct dwarf2_per_objfile *dwarf2_per_objfile
6139 = get_dwarf2_per_objfile (objfile);
6140
6141 /* debug_names_table is NULL if OBJF_READNOW. */
6142 if (!dwarf2_per_objfile->debug_names_table)
6143 return;
6144
6145 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6146
6147 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6148
6149 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6150 symbol_matcher,
6151 kind, [&] (offset_type namei)
6152 {
6153 /* The name was matched, now expand corresponding CUs that were
6154 marked. */
6155 dw2_debug_names_iterator iter (map, kind, namei);
6156
6157 struct dwarf2_per_cu_data *per_cu;
6158 while ((per_cu = iter.next ()) != NULL)
6159 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6160 expansion_notify);
6161 return true;
6162 });
6163 }
6164
6165 const struct quick_symbol_functions dwarf2_debug_names_functions =
6166 {
6167 dw2_has_symbols,
6168 dw2_find_last_source_symtab,
6169 dw2_forget_cached_source_info,
6170 dw2_map_symtabs_matching_filename,
6171 dw2_debug_names_lookup_symbol,
6172 dw2_print_stats,
6173 dw2_debug_names_dump,
6174 dw2_debug_names_expand_symtabs_for_function,
6175 dw2_expand_all_symtabs,
6176 dw2_expand_symtabs_with_fullname,
6177 dw2_debug_names_map_matching_symbols,
6178 dw2_debug_names_expand_symtabs_matching,
6179 dw2_find_pc_sect_compunit_symtab,
6180 NULL,
6181 dw2_map_symbol_filenames
6182 };
6183
6184 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6185 to either a dwarf2_per_objfile or dwz_file object. */
6186
6187 template <typename T>
6188 static gdb::array_view<const gdb_byte>
6189 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6190 {
6191 dwarf2_section_info *section = &section_owner->gdb_index;
6192
6193 if (dwarf2_section_empty_p (section))
6194 return {};
6195
6196 /* Older elfutils strip versions could keep the section in the main
6197 executable while splitting it for the separate debug info file. */
6198 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6199 return {};
6200
6201 dwarf2_read_section (obj, section);
6202
6203 /* dwarf2_section_info::size is a bfd_size_type, while
6204 gdb::array_view works with size_t. On 32-bit hosts, with
6205 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6206 is 32-bit. So we need an explicit narrowing conversion here.
6207 This is fine, because it's impossible to allocate or mmap an
6208 array/buffer larger than what size_t can represent. */
6209 return gdb::make_array_view (section->buffer, section->size);
6210 }
6211
6212 /* Lookup the index cache for the contents of the index associated to
6213 DWARF2_OBJ. */
6214
6215 static gdb::array_view<const gdb_byte>
6216 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6217 {
6218 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6219 if (build_id == nullptr)
6220 return {};
6221
6222 return global_index_cache.lookup_gdb_index (build_id,
6223 &dwarf2_obj->index_cache_res);
6224 }
6225
6226 /* Same as the above, but for DWZ. */
6227
6228 static gdb::array_view<const gdb_byte>
6229 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6230 {
6231 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6232 if (build_id == nullptr)
6233 return {};
6234
6235 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6236 }
6237
6238 /* See symfile.h. */
6239
6240 bool
6241 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6242 {
6243 struct dwarf2_per_objfile *dwarf2_per_objfile
6244 = get_dwarf2_per_objfile (objfile);
6245
6246 /* If we're about to read full symbols, don't bother with the
6247 indices. In this case we also don't care if some other debug
6248 format is making psymtabs, because they are all about to be
6249 expanded anyway. */
6250 if ((objfile->flags & OBJF_READNOW))
6251 {
6252 dwarf2_per_objfile->using_index = 1;
6253 create_all_comp_units (dwarf2_per_objfile);
6254 create_all_type_units (dwarf2_per_objfile);
6255 dwarf2_per_objfile->quick_file_names_table
6256 = create_quick_file_names_table
6257 (dwarf2_per_objfile->all_comp_units.size ());
6258
6259 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
6260 + dwarf2_per_objfile->all_type_units.size ()); ++i)
6261 {
6262 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6263
6264 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6265 struct dwarf2_per_cu_quick_data);
6266 }
6267
6268 /* Return 1 so that gdb sees the "quick" functions. However,
6269 these functions will be no-ops because we will have expanded
6270 all symtabs. */
6271 *index_kind = dw_index_kind::GDB_INDEX;
6272 return true;
6273 }
6274
6275 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6276 {
6277 *index_kind = dw_index_kind::DEBUG_NAMES;
6278 return true;
6279 }
6280
6281 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6282 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6283 get_gdb_index_contents_from_section<dwz_file>))
6284 {
6285 *index_kind = dw_index_kind::GDB_INDEX;
6286 return true;
6287 }
6288
6289 /* ... otherwise, try to find the index in the index cache. */
6290 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6291 get_gdb_index_contents_from_cache,
6292 get_gdb_index_contents_from_cache_dwz))
6293 {
6294 global_index_cache.hit ();
6295 *index_kind = dw_index_kind::GDB_INDEX;
6296 return true;
6297 }
6298
6299 global_index_cache.miss ();
6300 return false;
6301 }
6302
6303 \f
6304
6305 /* Build a partial symbol table. */
6306
6307 void
6308 dwarf2_build_psymtabs (struct objfile *objfile)
6309 {
6310 struct dwarf2_per_objfile *dwarf2_per_objfile
6311 = get_dwarf2_per_objfile (objfile);
6312
6313 init_psymbol_list (objfile, 1024);
6314
6315 try
6316 {
6317 /* This isn't really ideal: all the data we allocate on the
6318 objfile's obstack is still uselessly kept around. However,
6319 freeing it seems unsafe. */
6320 psymtab_discarder psymtabs (objfile);
6321 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6322 psymtabs.keep ();
6323
6324 /* (maybe) store an index in the cache. */
6325 global_index_cache.store (dwarf2_per_objfile);
6326 }
6327 catch (const gdb_exception_error &except)
6328 {
6329 exception_print (gdb_stderr, except);
6330 }
6331 }
6332
6333 /* Return the total length of the CU described by HEADER. */
6334
6335 static unsigned int
6336 get_cu_length (const struct comp_unit_head *header)
6337 {
6338 return header->initial_length_size + header->length;
6339 }
6340
6341 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6342
6343 static inline bool
6344 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6345 {
6346 sect_offset bottom = cu_header->sect_off;
6347 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6348
6349 return sect_off >= bottom && sect_off < top;
6350 }
6351
6352 /* Find the base address of the compilation unit for range lists and
6353 location lists. It will normally be specified by DW_AT_low_pc.
6354 In DWARF-3 draft 4, the base address could be overridden by
6355 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6356 compilation units with discontinuous ranges. */
6357
6358 static void
6359 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6360 {
6361 struct attribute *attr;
6362
6363 cu->base_known = 0;
6364 cu->base_address = 0;
6365
6366 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6367 if (attr)
6368 {
6369 cu->base_address = attr_value_as_address (attr);
6370 cu->base_known = 1;
6371 }
6372 else
6373 {
6374 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6375 if (attr)
6376 {
6377 cu->base_address = attr_value_as_address (attr);
6378 cu->base_known = 1;
6379 }
6380 }
6381 }
6382
6383 /* Read in the comp unit header information from the debug_info at info_ptr.
6384 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6385 NOTE: This leaves members offset, first_die_offset to be filled in
6386 by the caller. */
6387
6388 static const gdb_byte *
6389 read_comp_unit_head (struct comp_unit_head *cu_header,
6390 const gdb_byte *info_ptr,
6391 struct dwarf2_section_info *section,
6392 rcuh_kind section_kind)
6393 {
6394 int signed_addr;
6395 unsigned int bytes_read;
6396 const char *filename = get_section_file_name (section);
6397 bfd *abfd = get_section_bfd_owner (section);
6398
6399 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6400 cu_header->initial_length_size = bytes_read;
6401 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6402 info_ptr += bytes_read;
6403 cu_header->version = read_2_bytes (abfd, info_ptr);
6404 if (cu_header->version < 2 || cu_header->version > 5)
6405 error (_("Dwarf Error: wrong version in compilation unit header "
6406 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6407 cu_header->version, filename);
6408 info_ptr += 2;
6409 if (cu_header->version < 5)
6410 switch (section_kind)
6411 {
6412 case rcuh_kind::COMPILE:
6413 cu_header->unit_type = DW_UT_compile;
6414 break;
6415 case rcuh_kind::TYPE:
6416 cu_header->unit_type = DW_UT_type;
6417 break;
6418 default:
6419 internal_error (__FILE__, __LINE__,
6420 _("read_comp_unit_head: invalid section_kind"));
6421 }
6422 else
6423 {
6424 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6425 (read_1_byte (abfd, info_ptr));
6426 info_ptr += 1;
6427 switch (cu_header->unit_type)
6428 {
6429 case DW_UT_compile:
6430 case DW_UT_partial:
6431 case DW_UT_skeleton:
6432 case DW_UT_split_compile:
6433 if (section_kind != rcuh_kind::COMPILE)
6434 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6435 "(is %s, should be %s) [in module %s]"),
6436 dwarf_unit_type_name (cu_header->unit_type),
6437 dwarf_unit_type_name (DW_UT_type), filename);
6438 break;
6439 case DW_UT_type:
6440 case DW_UT_split_type:
6441 section_kind = rcuh_kind::TYPE;
6442 break;
6443 default:
6444 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6445 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6446 "[in module %s]"), cu_header->unit_type,
6447 dwarf_unit_type_name (DW_UT_compile),
6448 dwarf_unit_type_name (DW_UT_skeleton),
6449 dwarf_unit_type_name (DW_UT_split_compile),
6450 dwarf_unit_type_name (DW_UT_type),
6451 dwarf_unit_type_name (DW_UT_split_type), filename);
6452 }
6453
6454 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6455 info_ptr += 1;
6456 }
6457 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6458 cu_header,
6459 &bytes_read);
6460 info_ptr += bytes_read;
6461 if (cu_header->version < 5)
6462 {
6463 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6464 info_ptr += 1;
6465 }
6466 signed_addr = bfd_get_sign_extend_vma (abfd);
6467 if (signed_addr < 0)
6468 internal_error (__FILE__, __LINE__,
6469 _("read_comp_unit_head: dwarf from non elf file"));
6470 cu_header->signed_addr_p = signed_addr;
6471
6472 bool header_has_signature = section_kind == rcuh_kind::TYPE
6473 || cu_header->unit_type == DW_UT_skeleton
6474 || cu_header->unit_type == DW_UT_split_compile;
6475
6476 if (header_has_signature)
6477 {
6478 cu_header->signature = read_8_bytes (abfd, info_ptr);
6479 info_ptr += 8;
6480 }
6481
6482 if (section_kind == rcuh_kind::TYPE)
6483 {
6484 LONGEST type_offset;
6485 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6486 info_ptr += bytes_read;
6487 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6488 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6489 error (_("Dwarf Error: Too big type_offset in compilation unit "
6490 "header (is %s) [in module %s]"), plongest (type_offset),
6491 filename);
6492 }
6493
6494 return info_ptr;
6495 }
6496
6497 /* Helper function that returns the proper abbrev section for
6498 THIS_CU. */
6499
6500 static struct dwarf2_section_info *
6501 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6502 {
6503 struct dwarf2_section_info *abbrev;
6504 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6505
6506 if (this_cu->is_dwz)
6507 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6508 else
6509 abbrev = &dwarf2_per_objfile->abbrev;
6510
6511 return abbrev;
6512 }
6513
6514 /* Subroutine of read_and_check_comp_unit_head and
6515 read_and_check_type_unit_head to simplify them.
6516 Perform various error checking on the header. */
6517
6518 static void
6519 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6520 struct comp_unit_head *header,
6521 struct dwarf2_section_info *section,
6522 struct dwarf2_section_info *abbrev_section)
6523 {
6524 const char *filename = get_section_file_name (section);
6525
6526 if (to_underlying (header->abbrev_sect_off)
6527 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6528 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6529 "(offset %s + 6) [in module %s]"),
6530 sect_offset_str (header->abbrev_sect_off),
6531 sect_offset_str (header->sect_off),
6532 filename);
6533
6534 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6535 avoid potential 32-bit overflow. */
6536 if (((ULONGEST) header->sect_off + get_cu_length (header))
6537 > section->size)
6538 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6539 "(offset %s + 0) [in module %s]"),
6540 header->length, sect_offset_str (header->sect_off),
6541 filename);
6542 }
6543
6544 /* Read in a CU/TU header and perform some basic error checking.
6545 The contents of the header are stored in HEADER.
6546 The result is a pointer to the start of the first DIE. */
6547
6548 static const gdb_byte *
6549 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6550 struct comp_unit_head *header,
6551 struct dwarf2_section_info *section,
6552 struct dwarf2_section_info *abbrev_section,
6553 const gdb_byte *info_ptr,
6554 rcuh_kind section_kind)
6555 {
6556 const gdb_byte *beg_of_comp_unit = info_ptr;
6557
6558 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6559
6560 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6561
6562 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6563
6564 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6565 abbrev_section);
6566
6567 return info_ptr;
6568 }
6569
6570 /* Fetch the abbreviation table offset from a comp or type unit header. */
6571
6572 static sect_offset
6573 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6574 struct dwarf2_section_info *section,
6575 sect_offset sect_off)
6576 {
6577 bfd *abfd = get_section_bfd_owner (section);
6578 const gdb_byte *info_ptr;
6579 unsigned int initial_length_size, offset_size;
6580 uint16_t version;
6581
6582 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6583 info_ptr = section->buffer + to_underlying (sect_off);
6584 read_initial_length (abfd, info_ptr, &initial_length_size);
6585 offset_size = initial_length_size == 4 ? 4 : 8;
6586 info_ptr += initial_length_size;
6587
6588 version = read_2_bytes (abfd, info_ptr);
6589 info_ptr += 2;
6590 if (version >= 5)
6591 {
6592 /* Skip unit type and address size. */
6593 info_ptr += 2;
6594 }
6595
6596 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6597 }
6598
6599 /* Allocate a new partial symtab for file named NAME and mark this new
6600 partial symtab as being an include of PST. */
6601
6602 static void
6603 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6604 struct objfile *objfile)
6605 {
6606 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6607
6608 if (!IS_ABSOLUTE_PATH (subpst->filename))
6609 {
6610 /* It shares objfile->objfile_obstack. */
6611 subpst->dirname = pst->dirname;
6612 }
6613
6614 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6615 subpst->dependencies[0] = pst;
6616 subpst->number_of_dependencies = 1;
6617
6618 subpst->read_symtab = pst->read_symtab;
6619
6620 /* No private part is necessary for include psymtabs. This property
6621 can be used to differentiate between such include psymtabs and
6622 the regular ones. */
6623 subpst->read_symtab_private = NULL;
6624 }
6625
6626 /* Read the Line Number Program data and extract the list of files
6627 included by the source file represented by PST. Build an include
6628 partial symtab for each of these included files. */
6629
6630 static void
6631 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6632 struct die_info *die,
6633 struct partial_symtab *pst)
6634 {
6635 line_header_up lh;
6636 struct attribute *attr;
6637
6638 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6639 if (attr)
6640 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6641 if (lh == NULL)
6642 return; /* No linetable, so no includes. */
6643
6644 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6645 that we pass in the raw text_low here; that is ok because we're
6646 only decoding the line table to make include partial symtabs, and
6647 so the addresses aren't really used. */
6648 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6649 pst->raw_text_low (), 1);
6650 }
6651
6652 static hashval_t
6653 hash_signatured_type (const void *item)
6654 {
6655 const struct signatured_type *sig_type
6656 = (const struct signatured_type *) item;
6657
6658 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6659 return sig_type->signature;
6660 }
6661
6662 static int
6663 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6664 {
6665 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6666 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6667
6668 return lhs->signature == rhs->signature;
6669 }
6670
6671 /* Allocate a hash table for signatured types. */
6672
6673 static htab_t
6674 allocate_signatured_type_table (struct objfile *objfile)
6675 {
6676 return htab_create_alloc_ex (41,
6677 hash_signatured_type,
6678 eq_signatured_type,
6679 NULL,
6680 &objfile->objfile_obstack,
6681 hashtab_obstack_allocate,
6682 dummy_obstack_deallocate);
6683 }
6684
6685 /* A helper function to add a signatured type CU to a table. */
6686
6687 static int
6688 add_signatured_type_cu_to_table (void **slot, void *datum)
6689 {
6690 struct signatured_type *sigt = (struct signatured_type *) *slot;
6691 std::vector<signatured_type *> *all_type_units
6692 = (std::vector<signatured_type *> *) datum;
6693
6694 all_type_units->push_back (sigt);
6695
6696 return 1;
6697 }
6698
6699 /* A helper for create_debug_types_hash_table. Read types from SECTION
6700 and fill them into TYPES_HTAB. It will process only type units,
6701 therefore DW_UT_type. */
6702
6703 static void
6704 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6705 struct dwo_file *dwo_file,
6706 dwarf2_section_info *section, htab_t &types_htab,
6707 rcuh_kind section_kind)
6708 {
6709 struct objfile *objfile = dwarf2_per_objfile->objfile;
6710 struct dwarf2_section_info *abbrev_section;
6711 bfd *abfd;
6712 const gdb_byte *info_ptr, *end_ptr;
6713
6714 abbrev_section = (dwo_file != NULL
6715 ? &dwo_file->sections.abbrev
6716 : &dwarf2_per_objfile->abbrev);
6717
6718 if (dwarf_read_debug)
6719 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6720 get_section_name (section),
6721 get_section_file_name (abbrev_section));
6722
6723 dwarf2_read_section (objfile, section);
6724 info_ptr = section->buffer;
6725
6726 if (info_ptr == NULL)
6727 return;
6728
6729 /* We can't set abfd until now because the section may be empty or
6730 not present, in which case the bfd is unknown. */
6731 abfd = get_section_bfd_owner (section);
6732
6733 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6734 because we don't need to read any dies: the signature is in the
6735 header. */
6736
6737 end_ptr = info_ptr + section->size;
6738 while (info_ptr < end_ptr)
6739 {
6740 struct signatured_type *sig_type;
6741 struct dwo_unit *dwo_tu;
6742 void **slot;
6743 const gdb_byte *ptr = info_ptr;
6744 struct comp_unit_head header;
6745 unsigned int length;
6746
6747 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6748
6749 /* Initialize it due to a false compiler warning. */
6750 header.signature = -1;
6751 header.type_cu_offset_in_tu = (cu_offset) -1;
6752
6753 /* We need to read the type's signature in order to build the hash
6754 table, but we don't need anything else just yet. */
6755
6756 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6757 abbrev_section, ptr, section_kind);
6758
6759 length = get_cu_length (&header);
6760
6761 /* Skip dummy type units. */
6762 if (ptr >= info_ptr + length
6763 || peek_abbrev_code (abfd, ptr) == 0
6764 || header.unit_type != DW_UT_type)
6765 {
6766 info_ptr += length;
6767 continue;
6768 }
6769
6770 if (types_htab == NULL)
6771 {
6772 if (dwo_file)
6773 types_htab = allocate_dwo_unit_table (objfile);
6774 else
6775 types_htab = allocate_signatured_type_table (objfile);
6776 }
6777
6778 if (dwo_file)
6779 {
6780 sig_type = NULL;
6781 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6782 struct dwo_unit);
6783 dwo_tu->dwo_file = dwo_file;
6784 dwo_tu->signature = header.signature;
6785 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6786 dwo_tu->section = section;
6787 dwo_tu->sect_off = sect_off;
6788 dwo_tu->length = length;
6789 }
6790 else
6791 {
6792 /* N.B.: type_offset is not usable if this type uses a DWO file.
6793 The real type_offset is in the DWO file. */
6794 dwo_tu = NULL;
6795 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6796 struct signatured_type);
6797 sig_type->signature = header.signature;
6798 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6799 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6800 sig_type->per_cu.is_debug_types = 1;
6801 sig_type->per_cu.section = section;
6802 sig_type->per_cu.sect_off = sect_off;
6803 sig_type->per_cu.length = length;
6804 }
6805
6806 slot = htab_find_slot (types_htab,
6807 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6808 INSERT);
6809 gdb_assert (slot != NULL);
6810 if (*slot != NULL)
6811 {
6812 sect_offset dup_sect_off;
6813
6814 if (dwo_file)
6815 {
6816 const struct dwo_unit *dup_tu
6817 = (const struct dwo_unit *) *slot;
6818
6819 dup_sect_off = dup_tu->sect_off;
6820 }
6821 else
6822 {
6823 const struct signatured_type *dup_tu
6824 = (const struct signatured_type *) *slot;
6825
6826 dup_sect_off = dup_tu->per_cu.sect_off;
6827 }
6828
6829 complaint (_("debug type entry at offset %s is duplicate to"
6830 " the entry at offset %s, signature %s"),
6831 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6832 hex_string (header.signature));
6833 }
6834 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6835
6836 if (dwarf_read_debug > 1)
6837 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6838 sect_offset_str (sect_off),
6839 hex_string (header.signature));
6840
6841 info_ptr += length;
6842 }
6843 }
6844
6845 /* Create the hash table of all entries in the .debug_types
6846 (or .debug_types.dwo) section(s).
6847 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6848 otherwise it is NULL.
6849
6850 The result is a pointer to the hash table or NULL if there are no types.
6851
6852 Note: This function processes DWO files only, not DWP files. */
6853
6854 static void
6855 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6856 struct dwo_file *dwo_file,
6857 gdb::array_view<dwarf2_section_info> type_sections,
6858 htab_t &types_htab)
6859 {
6860 for (dwarf2_section_info &section : type_sections)
6861 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6862 types_htab, rcuh_kind::TYPE);
6863 }
6864
6865 /* Create the hash table of all entries in the .debug_types section,
6866 and initialize all_type_units.
6867 The result is zero if there is an error (e.g. missing .debug_types section),
6868 otherwise non-zero. */
6869
6870 static int
6871 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6872 {
6873 htab_t types_htab = NULL;
6874
6875 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6876 &dwarf2_per_objfile->info, types_htab,
6877 rcuh_kind::COMPILE);
6878 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6879 dwarf2_per_objfile->types, types_htab);
6880 if (types_htab == NULL)
6881 {
6882 dwarf2_per_objfile->signatured_types = NULL;
6883 return 0;
6884 }
6885
6886 dwarf2_per_objfile->signatured_types = types_htab;
6887
6888 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6889 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6890
6891 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6892 &dwarf2_per_objfile->all_type_units);
6893
6894 return 1;
6895 }
6896
6897 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6898 If SLOT is non-NULL, it is the entry to use in the hash table.
6899 Otherwise we find one. */
6900
6901 static struct signatured_type *
6902 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6903 void **slot)
6904 {
6905 struct objfile *objfile = dwarf2_per_objfile->objfile;
6906
6907 if (dwarf2_per_objfile->all_type_units.size ()
6908 == dwarf2_per_objfile->all_type_units.capacity ())
6909 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6910
6911 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6912 struct signatured_type);
6913
6914 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6915 sig_type->signature = sig;
6916 sig_type->per_cu.is_debug_types = 1;
6917 if (dwarf2_per_objfile->using_index)
6918 {
6919 sig_type->per_cu.v.quick =
6920 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6921 struct dwarf2_per_cu_quick_data);
6922 }
6923
6924 if (slot == NULL)
6925 {
6926 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6927 sig_type, INSERT);
6928 }
6929 gdb_assert (*slot == NULL);
6930 *slot = sig_type;
6931 /* The rest of sig_type must be filled in by the caller. */
6932 return sig_type;
6933 }
6934
6935 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6936 Fill in SIG_ENTRY with DWO_ENTRY. */
6937
6938 static void
6939 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6940 struct signatured_type *sig_entry,
6941 struct dwo_unit *dwo_entry)
6942 {
6943 /* Make sure we're not clobbering something we don't expect to. */
6944 gdb_assert (! sig_entry->per_cu.queued);
6945 gdb_assert (sig_entry->per_cu.cu == NULL);
6946 if (dwarf2_per_objfile->using_index)
6947 {
6948 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6949 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6950 }
6951 else
6952 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6953 gdb_assert (sig_entry->signature == dwo_entry->signature);
6954 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6955 gdb_assert (sig_entry->type_unit_group == NULL);
6956 gdb_assert (sig_entry->dwo_unit == NULL);
6957
6958 sig_entry->per_cu.section = dwo_entry->section;
6959 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6960 sig_entry->per_cu.length = dwo_entry->length;
6961 sig_entry->per_cu.reading_dwo_directly = 1;
6962 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6963 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6964 sig_entry->dwo_unit = dwo_entry;
6965 }
6966
6967 /* Subroutine of lookup_signatured_type.
6968 If we haven't read the TU yet, create the signatured_type data structure
6969 for a TU to be read in directly from a DWO file, bypassing the stub.
6970 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6971 using .gdb_index, then when reading a CU we want to stay in the DWO file
6972 containing that CU. Otherwise we could end up reading several other DWO
6973 files (due to comdat folding) to process the transitive closure of all the
6974 mentioned TUs, and that can be slow. The current DWO file will have every
6975 type signature that it needs.
6976 We only do this for .gdb_index because in the psymtab case we already have
6977 to read all the DWOs to build the type unit groups. */
6978
6979 static struct signatured_type *
6980 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6981 {
6982 struct dwarf2_per_objfile *dwarf2_per_objfile
6983 = cu->per_cu->dwarf2_per_objfile;
6984 struct objfile *objfile = dwarf2_per_objfile->objfile;
6985 struct dwo_file *dwo_file;
6986 struct dwo_unit find_dwo_entry, *dwo_entry;
6987 struct signatured_type find_sig_entry, *sig_entry;
6988 void **slot;
6989
6990 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6991
6992 /* If TU skeletons have been removed then we may not have read in any
6993 TUs yet. */
6994 if (dwarf2_per_objfile->signatured_types == NULL)
6995 {
6996 dwarf2_per_objfile->signatured_types
6997 = allocate_signatured_type_table (objfile);
6998 }
6999
7000 /* We only ever need to read in one copy of a signatured type.
7001 Use the global signatured_types array to do our own comdat-folding
7002 of types. If this is the first time we're reading this TU, and
7003 the TU has an entry in .gdb_index, replace the recorded data from
7004 .gdb_index with this TU. */
7005
7006 find_sig_entry.signature = sig;
7007 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7008 &find_sig_entry, INSERT);
7009 sig_entry = (struct signatured_type *) *slot;
7010
7011 /* We can get here with the TU already read, *or* in the process of being
7012 read. Don't reassign the global entry to point to this DWO if that's
7013 the case. Also note that if the TU is already being read, it may not
7014 have come from a DWO, the program may be a mix of Fission-compiled
7015 code and non-Fission-compiled code. */
7016
7017 /* Have we already tried to read this TU?
7018 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7019 needn't exist in the global table yet). */
7020 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7021 return sig_entry;
7022
7023 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7024 dwo_unit of the TU itself. */
7025 dwo_file = cu->dwo_unit->dwo_file;
7026
7027 /* Ok, this is the first time we're reading this TU. */
7028 if (dwo_file->tus == NULL)
7029 return NULL;
7030 find_dwo_entry.signature = sig;
7031 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7032 if (dwo_entry == NULL)
7033 return NULL;
7034
7035 /* If the global table doesn't have an entry for this TU, add one. */
7036 if (sig_entry == NULL)
7037 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7038
7039 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7040 sig_entry->per_cu.tu_read = 1;
7041 return sig_entry;
7042 }
7043
7044 /* Subroutine of lookup_signatured_type.
7045 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7046 then try the DWP file. If the TU stub (skeleton) has been removed then
7047 it won't be in .gdb_index. */
7048
7049 static struct signatured_type *
7050 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7051 {
7052 struct dwarf2_per_objfile *dwarf2_per_objfile
7053 = cu->per_cu->dwarf2_per_objfile;
7054 struct objfile *objfile = dwarf2_per_objfile->objfile;
7055 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7056 struct dwo_unit *dwo_entry;
7057 struct signatured_type find_sig_entry, *sig_entry;
7058 void **slot;
7059
7060 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7061 gdb_assert (dwp_file != NULL);
7062
7063 /* If TU skeletons have been removed then we may not have read in any
7064 TUs yet. */
7065 if (dwarf2_per_objfile->signatured_types == NULL)
7066 {
7067 dwarf2_per_objfile->signatured_types
7068 = allocate_signatured_type_table (objfile);
7069 }
7070
7071 find_sig_entry.signature = sig;
7072 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7073 &find_sig_entry, INSERT);
7074 sig_entry = (struct signatured_type *) *slot;
7075
7076 /* Have we already tried to read this TU?
7077 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7078 needn't exist in the global table yet). */
7079 if (sig_entry != NULL)
7080 return sig_entry;
7081
7082 if (dwp_file->tus == NULL)
7083 return NULL;
7084 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7085 sig, 1 /* is_debug_types */);
7086 if (dwo_entry == NULL)
7087 return NULL;
7088
7089 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7090 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7091
7092 return sig_entry;
7093 }
7094
7095 /* Lookup a signature based type for DW_FORM_ref_sig8.
7096 Returns NULL if signature SIG is not present in the table.
7097 It is up to the caller to complain about this. */
7098
7099 static struct signatured_type *
7100 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7101 {
7102 struct dwarf2_per_objfile *dwarf2_per_objfile
7103 = cu->per_cu->dwarf2_per_objfile;
7104
7105 if (cu->dwo_unit
7106 && dwarf2_per_objfile->using_index)
7107 {
7108 /* We're in a DWO/DWP file, and we're using .gdb_index.
7109 These cases require special processing. */
7110 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7111 return lookup_dwo_signatured_type (cu, sig);
7112 else
7113 return lookup_dwp_signatured_type (cu, sig);
7114 }
7115 else
7116 {
7117 struct signatured_type find_entry, *entry;
7118
7119 if (dwarf2_per_objfile->signatured_types == NULL)
7120 return NULL;
7121 find_entry.signature = sig;
7122 entry = ((struct signatured_type *)
7123 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7124 return entry;
7125 }
7126 }
7127 \f
7128 /* Low level DIE reading support. */
7129
7130 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7131
7132 static void
7133 init_cu_die_reader (struct die_reader_specs *reader,
7134 struct dwarf2_cu *cu,
7135 struct dwarf2_section_info *section,
7136 struct dwo_file *dwo_file,
7137 struct abbrev_table *abbrev_table)
7138 {
7139 gdb_assert (section->readin && section->buffer != NULL);
7140 reader->abfd = get_section_bfd_owner (section);
7141 reader->cu = cu;
7142 reader->dwo_file = dwo_file;
7143 reader->die_section = section;
7144 reader->buffer = section->buffer;
7145 reader->buffer_end = section->buffer + section->size;
7146 reader->comp_dir = NULL;
7147 reader->abbrev_table = abbrev_table;
7148 }
7149
7150 /* Subroutine of init_cutu_and_read_dies to simplify it.
7151 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7152 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7153 already.
7154
7155 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7156 from it to the DIE in the DWO. If NULL we are skipping the stub.
7157 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7158 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7159 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7160 STUB_COMP_DIR may be non-NULL.
7161 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7162 are filled in with the info of the DIE from the DWO file.
7163 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7164 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7165 kept around for at least as long as *RESULT_READER.
7166
7167 The result is non-zero if a valid (non-dummy) DIE was found. */
7168
7169 static int
7170 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7171 struct dwo_unit *dwo_unit,
7172 struct die_info *stub_comp_unit_die,
7173 const char *stub_comp_dir,
7174 struct die_reader_specs *result_reader,
7175 const gdb_byte **result_info_ptr,
7176 struct die_info **result_comp_unit_die,
7177 int *result_has_children,
7178 abbrev_table_up *result_dwo_abbrev_table)
7179 {
7180 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7181 struct objfile *objfile = dwarf2_per_objfile->objfile;
7182 struct dwarf2_cu *cu = this_cu->cu;
7183 bfd *abfd;
7184 const gdb_byte *begin_info_ptr, *info_ptr;
7185 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7186 int i,num_extra_attrs;
7187 struct dwarf2_section_info *dwo_abbrev_section;
7188 struct attribute *attr;
7189 struct die_info *comp_unit_die;
7190
7191 /* At most one of these may be provided. */
7192 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7193
7194 /* These attributes aren't processed until later:
7195 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7196 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7197 referenced later. However, these attributes are found in the stub
7198 which we won't have later. In order to not impose this complication
7199 on the rest of the code, we read them here and copy them to the
7200 DWO CU/TU die. */
7201
7202 stmt_list = NULL;
7203 low_pc = NULL;
7204 high_pc = NULL;
7205 ranges = NULL;
7206 comp_dir = NULL;
7207
7208 if (stub_comp_unit_die != NULL)
7209 {
7210 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7211 DWO file. */
7212 if (! this_cu->is_debug_types)
7213 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7214 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7215 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7216 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7217 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7218
7219 /* There should be a DW_AT_addr_base attribute here (if needed).
7220 We need the value before we can process DW_FORM_GNU_addr_index
7221 or DW_FORM_addrx. */
7222 cu->addr_base = 0;
7223 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7224 if (attr)
7225 cu->addr_base = DW_UNSND (attr);
7226
7227 /* There should be a DW_AT_ranges_base attribute here (if needed).
7228 We need the value before we can process DW_AT_ranges. */
7229 cu->ranges_base = 0;
7230 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7231 if (attr)
7232 cu->ranges_base = DW_UNSND (attr);
7233 }
7234 else if (stub_comp_dir != NULL)
7235 {
7236 /* Reconstruct the comp_dir attribute to simplify the code below. */
7237 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7238 comp_dir->name = DW_AT_comp_dir;
7239 comp_dir->form = DW_FORM_string;
7240 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7241 DW_STRING (comp_dir) = stub_comp_dir;
7242 }
7243
7244 /* Set up for reading the DWO CU/TU. */
7245 cu->dwo_unit = dwo_unit;
7246 dwarf2_section_info *section = dwo_unit->section;
7247 dwarf2_read_section (objfile, section);
7248 abfd = get_section_bfd_owner (section);
7249 begin_info_ptr = info_ptr = (section->buffer
7250 + to_underlying (dwo_unit->sect_off));
7251 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7252
7253 if (this_cu->is_debug_types)
7254 {
7255 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7256
7257 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7258 &cu->header, section,
7259 dwo_abbrev_section,
7260 info_ptr, rcuh_kind::TYPE);
7261 /* This is not an assert because it can be caused by bad debug info. */
7262 if (sig_type->signature != cu->header.signature)
7263 {
7264 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7265 " TU at offset %s [in module %s]"),
7266 hex_string (sig_type->signature),
7267 hex_string (cu->header.signature),
7268 sect_offset_str (dwo_unit->sect_off),
7269 bfd_get_filename (abfd));
7270 }
7271 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7272 /* For DWOs coming from DWP files, we don't know the CU length
7273 nor the type's offset in the TU until now. */
7274 dwo_unit->length = get_cu_length (&cu->header);
7275 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7276
7277 /* Establish the type offset that can be used to lookup the type.
7278 For DWO files, we don't know it until now. */
7279 sig_type->type_offset_in_section
7280 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7281 }
7282 else
7283 {
7284 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7285 &cu->header, section,
7286 dwo_abbrev_section,
7287 info_ptr, rcuh_kind::COMPILE);
7288 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7289 /* For DWOs coming from DWP files, we don't know the CU length
7290 until now. */
7291 dwo_unit->length = get_cu_length (&cu->header);
7292 }
7293
7294 *result_dwo_abbrev_table
7295 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7296 cu->header.abbrev_sect_off);
7297 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7298 result_dwo_abbrev_table->get ());
7299
7300 /* Read in the die, but leave space to copy over the attributes
7301 from the stub. This has the benefit of simplifying the rest of
7302 the code - all the work to maintain the illusion of a single
7303 DW_TAG_{compile,type}_unit DIE is done here. */
7304 num_extra_attrs = ((stmt_list != NULL)
7305 + (low_pc != NULL)
7306 + (high_pc != NULL)
7307 + (ranges != NULL)
7308 + (comp_dir != NULL));
7309 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7310 result_has_children, num_extra_attrs);
7311
7312 /* Copy over the attributes from the stub to the DIE we just read in. */
7313 comp_unit_die = *result_comp_unit_die;
7314 i = comp_unit_die->num_attrs;
7315 if (stmt_list != NULL)
7316 comp_unit_die->attrs[i++] = *stmt_list;
7317 if (low_pc != NULL)
7318 comp_unit_die->attrs[i++] = *low_pc;
7319 if (high_pc != NULL)
7320 comp_unit_die->attrs[i++] = *high_pc;
7321 if (ranges != NULL)
7322 comp_unit_die->attrs[i++] = *ranges;
7323 if (comp_dir != NULL)
7324 comp_unit_die->attrs[i++] = *comp_dir;
7325 comp_unit_die->num_attrs += num_extra_attrs;
7326
7327 if (dwarf_die_debug)
7328 {
7329 fprintf_unfiltered (gdb_stdlog,
7330 "Read die from %s@0x%x of %s:\n",
7331 get_section_name (section),
7332 (unsigned) (begin_info_ptr - section->buffer),
7333 bfd_get_filename (abfd));
7334 dump_die (comp_unit_die, dwarf_die_debug);
7335 }
7336
7337 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7338 TUs by skipping the stub and going directly to the entry in the DWO file.
7339 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7340 to get it via circuitous means. Blech. */
7341 if (comp_dir != NULL)
7342 result_reader->comp_dir = DW_STRING (comp_dir);
7343
7344 /* Skip dummy compilation units. */
7345 if (info_ptr >= begin_info_ptr + dwo_unit->length
7346 || peek_abbrev_code (abfd, info_ptr) == 0)
7347 return 0;
7348
7349 *result_info_ptr = info_ptr;
7350 return 1;
7351 }
7352
7353 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7354 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7355 signature is part of the header. */
7356 static gdb::optional<ULONGEST>
7357 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7358 {
7359 if (cu->header.version >= 5)
7360 return cu->header.signature;
7361 struct attribute *attr;
7362 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7363 if (attr == nullptr)
7364 return gdb::optional<ULONGEST> ();
7365 return DW_UNSND (attr);
7366 }
7367
7368 /* Subroutine of init_cutu_and_read_dies to simplify it.
7369 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7370 Returns NULL if the specified DWO unit cannot be found. */
7371
7372 static struct dwo_unit *
7373 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7374 struct die_info *comp_unit_die)
7375 {
7376 struct dwarf2_cu *cu = this_cu->cu;
7377 struct dwo_unit *dwo_unit;
7378 const char *comp_dir, *dwo_name;
7379
7380 gdb_assert (cu != NULL);
7381
7382 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7383 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7384 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7385
7386 if (this_cu->is_debug_types)
7387 {
7388 struct signatured_type *sig_type;
7389
7390 /* Since this_cu is the first member of struct signatured_type,
7391 we can go from a pointer to one to a pointer to the other. */
7392 sig_type = (struct signatured_type *) this_cu;
7393 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7394 }
7395 else
7396 {
7397 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7398 if (!signature.has_value ())
7399 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7400 " [in module %s]"),
7401 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7402 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7403 *signature);
7404 }
7405
7406 return dwo_unit;
7407 }
7408
7409 /* Subroutine of init_cutu_and_read_dies to simplify it.
7410 See it for a description of the parameters.
7411 Read a TU directly from a DWO file, bypassing the stub. */
7412
7413 static void
7414 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7415 int use_existing_cu, int keep,
7416 die_reader_func_ftype *die_reader_func,
7417 void *data)
7418 {
7419 std::unique_ptr<dwarf2_cu> new_cu;
7420 struct signatured_type *sig_type;
7421 struct die_reader_specs reader;
7422 const gdb_byte *info_ptr;
7423 struct die_info *comp_unit_die;
7424 int has_children;
7425 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7426
7427 /* Verify we can do the following downcast, and that we have the
7428 data we need. */
7429 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7430 sig_type = (struct signatured_type *) this_cu;
7431 gdb_assert (sig_type->dwo_unit != NULL);
7432
7433 if (use_existing_cu && this_cu->cu != NULL)
7434 {
7435 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7436 /* There's no need to do the rereading_dwo_cu handling that
7437 init_cutu_and_read_dies does since we don't read the stub. */
7438 }
7439 else
7440 {
7441 /* If !use_existing_cu, this_cu->cu must be NULL. */
7442 gdb_assert (this_cu->cu == NULL);
7443 new_cu.reset (new dwarf2_cu (this_cu));
7444 }
7445
7446 /* A future optimization, if needed, would be to use an existing
7447 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7448 could share abbrev tables. */
7449
7450 /* The abbreviation table used by READER, this must live at least as long as
7451 READER. */
7452 abbrev_table_up dwo_abbrev_table;
7453
7454 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7455 NULL /* stub_comp_unit_die */,
7456 sig_type->dwo_unit->dwo_file->comp_dir,
7457 &reader, &info_ptr,
7458 &comp_unit_die, &has_children,
7459 &dwo_abbrev_table) == 0)
7460 {
7461 /* Dummy die. */
7462 return;
7463 }
7464
7465 /* All the "real" work is done here. */
7466 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7467
7468 /* This duplicates the code in init_cutu_and_read_dies,
7469 but the alternative is making the latter more complex.
7470 This function is only for the special case of using DWO files directly:
7471 no point in overly complicating the general case just to handle this. */
7472 if (new_cu != NULL && keep)
7473 {
7474 /* Link this CU into read_in_chain. */
7475 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7476 dwarf2_per_objfile->read_in_chain = this_cu;
7477 /* The chain owns it now. */
7478 new_cu.release ();
7479 }
7480 }
7481
7482 /* Initialize a CU (or TU) and read its DIEs.
7483 If the CU defers to a DWO file, read the DWO file as well.
7484
7485 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7486 Otherwise the table specified in the comp unit header is read in and used.
7487 This is an optimization for when we already have the abbrev table.
7488
7489 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7490 Otherwise, a new CU is allocated with xmalloc.
7491
7492 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7493 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7494
7495 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7496 linker) then DIE_READER_FUNC will not get called. */
7497
7498 static void
7499 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7500 struct abbrev_table *abbrev_table,
7501 int use_existing_cu, int keep,
7502 bool skip_partial,
7503 die_reader_func_ftype *die_reader_func,
7504 void *data)
7505 {
7506 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7507 struct objfile *objfile = dwarf2_per_objfile->objfile;
7508 struct dwarf2_section_info *section = this_cu->section;
7509 bfd *abfd = get_section_bfd_owner (section);
7510 struct dwarf2_cu *cu;
7511 const gdb_byte *begin_info_ptr, *info_ptr;
7512 struct die_reader_specs reader;
7513 struct die_info *comp_unit_die;
7514 int has_children;
7515 struct signatured_type *sig_type = NULL;
7516 struct dwarf2_section_info *abbrev_section;
7517 /* Non-zero if CU currently points to a DWO file and we need to
7518 reread it. When this happens we need to reread the skeleton die
7519 before we can reread the DWO file (this only applies to CUs, not TUs). */
7520 int rereading_dwo_cu = 0;
7521
7522 if (dwarf_die_debug)
7523 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7524 this_cu->is_debug_types ? "type" : "comp",
7525 sect_offset_str (this_cu->sect_off));
7526
7527 if (use_existing_cu)
7528 gdb_assert (keep);
7529
7530 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7531 file (instead of going through the stub), short-circuit all of this. */
7532 if (this_cu->reading_dwo_directly)
7533 {
7534 /* Narrow down the scope of possibilities to have to understand. */
7535 gdb_assert (this_cu->is_debug_types);
7536 gdb_assert (abbrev_table == NULL);
7537 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7538 die_reader_func, data);
7539 return;
7540 }
7541
7542 /* This is cheap if the section is already read in. */
7543 dwarf2_read_section (objfile, section);
7544
7545 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7546
7547 abbrev_section = get_abbrev_section_for_cu (this_cu);
7548
7549 std::unique_ptr<dwarf2_cu> new_cu;
7550 if (use_existing_cu && this_cu->cu != NULL)
7551 {
7552 cu = this_cu->cu;
7553 /* If this CU is from a DWO file we need to start over, we need to
7554 refetch the attributes from the skeleton CU.
7555 This could be optimized by retrieving those attributes from when we
7556 were here the first time: the previous comp_unit_die was stored in
7557 comp_unit_obstack. But there's no data yet that we need this
7558 optimization. */
7559 if (cu->dwo_unit != NULL)
7560 rereading_dwo_cu = 1;
7561 }
7562 else
7563 {
7564 /* If !use_existing_cu, this_cu->cu must be NULL. */
7565 gdb_assert (this_cu->cu == NULL);
7566 new_cu.reset (new dwarf2_cu (this_cu));
7567 cu = new_cu.get ();
7568 }
7569
7570 /* Get the header. */
7571 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7572 {
7573 /* We already have the header, there's no need to read it in again. */
7574 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7575 }
7576 else
7577 {
7578 if (this_cu->is_debug_types)
7579 {
7580 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7581 &cu->header, section,
7582 abbrev_section, info_ptr,
7583 rcuh_kind::TYPE);
7584
7585 /* Since per_cu is the first member of struct signatured_type,
7586 we can go from a pointer to one to a pointer to the other. */
7587 sig_type = (struct signatured_type *) this_cu;
7588 gdb_assert (sig_type->signature == cu->header.signature);
7589 gdb_assert (sig_type->type_offset_in_tu
7590 == cu->header.type_cu_offset_in_tu);
7591 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7592
7593 /* LENGTH has not been set yet for type units if we're
7594 using .gdb_index. */
7595 this_cu->length = get_cu_length (&cu->header);
7596
7597 /* Establish the type offset that can be used to lookup the type. */
7598 sig_type->type_offset_in_section =
7599 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7600
7601 this_cu->dwarf_version = cu->header.version;
7602 }
7603 else
7604 {
7605 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7606 &cu->header, section,
7607 abbrev_section,
7608 info_ptr,
7609 rcuh_kind::COMPILE);
7610
7611 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7612 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7613 this_cu->dwarf_version = cu->header.version;
7614 }
7615 }
7616
7617 /* Skip dummy compilation units. */
7618 if (info_ptr >= begin_info_ptr + this_cu->length
7619 || peek_abbrev_code (abfd, info_ptr) == 0)
7620 return;
7621
7622 /* If we don't have them yet, read the abbrevs for this compilation unit.
7623 And if we need to read them now, make sure they're freed when we're
7624 done (own the table through ABBREV_TABLE_HOLDER). */
7625 abbrev_table_up abbrev_table_holder;
7626 if (abbrev_table != NULL)
7627 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7628 else
7629 {
7630 abbrev_table_holder
7631 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7632 cu->header.abbrev_sect_off);
7633 abbrev_table = abbrev_table_holder.get ();
7634 }
7635
7636 /* Read the top level CU/TU die. */
7637 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
7638 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7639
7640 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7641 return;
7642
7643 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7644 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7645 table from the DWO file and pass the ownership over to us. It will be
7646 referenced from READER, so we must make sure to free it after we're done
7647 with READER.
7648
7649 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7650 DWO CU, that this test will fail (the attribute will not be present). */
7651 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7652 abbrev_table_up dwo_abbrev_table;
7653 if (dwo_name != nullptr)
7654 {
7655 struct dwo_unit *dwo_unit;
7656 struct die_info *dwo_comp_unit_die;
7657
7658 if (has_children)
7659 {
7660 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7661 " has children (offset %s) [in module %s]"),
7662 sect_offset_str (this_cu->sect_off),
7663 bfd_get_filename (abfd));
7664 }
7665 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7666 if (dwo_unit != NULL)
7667 {
7668 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7669 comp_unit_die, NULL,
7670 &reader, &info_ptr,
7671 &dwo_comp_unit_die, &has_children,
7672 &dwo_abbrev_table) == 0)
7673 {
7674 /* Dummy die. */
7675 return;
7676 }
7677 comp_unit_die = dwo_comp_unit_die;
7678 }
7679 else
7680 {
7681 /* Yikes, we couldn't find the rest of the DIE, we only have
7682 the stub. A complaint has already been logged. There's
7683 not much more we can do except pass on the stub DIE to
7684 die_reader_func. We don't want to throw an error on bad
7685 debug info. */
7686 }
7687 }
7688
7689 /* All of the above is setup for this call. Yikes. */
7690 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7691
7692 /* Done, clean up. */
7693 if (new_cu != NULL && keep)
7694 {
7695 /* Link this CU into read_in_chain. */
7696 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7697 dwarf2_per_objfile->read_in_chain = this_cu;
7698 /* The chain owns it now. */
7699 new_cu.release ();
7700 }
7701 }
7702
7703 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7704 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7705 to have already done the lookup to find the DWO file).
7706
7707 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7708 THIS_CU->is_debug_types, but nothing else.
7709
7710 We fill in THIS_CU->length.
7711
7712 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7713 linker) then DIE_READER_FUNC will not get called.
7714
7715 THIS_CU->cu is always freed when done.
7716 This is done in order to not leave THIS_CU->cu in a state where we have
7717 to care whether it refers to the "main" CU or the DWO CU. */
7718
7719 static void
7720 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
7721 struct dwo_file *dwo_file,
7722 die_reader_func_ftype *die_reader_func,
7723 void *data)
7724 {
7725 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7726 struct objfile *objfile = dwarf2_per_objfile->objfile;
7727 struct dwarf2_section_info *section = this_cu->section;
7728 bfd *abfd = get_section_bfd_owner (section);
7729 struct dwarf2_section_info *abbrev_section;
7730 const gdb_byte *begin_info_ptr, *info_ptr;
7731 struct die_reader_specs reader;
7732 struct die_info *comp_unit_die;
7733 int has_children;
7734
7735 if (dwarf_die_debug)
7736 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7737 this_cu->is_debug_types ? "type" : "comp",
7738 sect_offset_str (this_cu->sect_off));
7739
7740 gdb_assert (this_cu->cu == NULL);
7741
7742 abbrev_section = (dwo_file != NULL
7743 ? &dwo_file->sections.abbrev
7744 : get_abbrev_section_for_cu (this_cu));
7745
7746 /* This is cheap if the section is already read in. */
7747 dwarf2_read_section (objfile, section);
7748
7749 struct dwarf2_cu cu (this_cu);
7750
7751 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7752 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7753 &cu.header, section,
7754 abbrev_section, info_ptr,
7755 (this_cu->is_debug_types
7756 ? rcuh_kind::TYPE
7757 : rcuh_kind::COMPILE));
7758
7759 this_cu->length = get_cu_length (&cu.header);
7760
7761 /* Skip dummy compilation units. */
7762 if (info_ptr >= begin_info_ptr + this_cu->length
7763 || peek_abbrev_code (abfd, info_ptr) == 0)
7764 return;
7765
7766 abbrev_table_up abbrev_table
7767 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7768 cu.header.abbrev_sect_off);
7769
7770 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
7771 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7772
7773 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7774 }
7775
7776 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7777 does not lookup the specified DWO file.
7778 This cannot be used to read DWO files.
7779
7780 THIS_CU->cu is always freed when done.
7781 This is done in order to not leave THIS_CU->cu in a state where we have
7782 to care whether it refers to the "main" CU or the DWO CU.
7783 We can revisit this if the data shows there's a performance issue. */
7784
7785 static void
7786 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7787 die_reader_func_ftype *die_reader_func,
7788 void *data)
7789 {
7790 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
7791 }
7792 \f
7793 /* Type Unit Groups.
7794
7795 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7796 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7797 so that all types coming from the same compilation (.o file) are grouped
7798 together. A future step could be to put the types in the same symtab as
7799 the CU the types ultimately came from. */
7800
7801 static hashval_t
7802 hash_type_unit_group (const void *item)
7803 {
7804 const struct type_unit_group *tu_group
7805 = (const struct type_unit_group *) item;
7806
7807 return hash_stmt_list_entry (&tu_group->hash);
7808 }
7809
7810 static int
7811 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7812 {
7813 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7814 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7815
7816 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7817 }
7818
7819 /* Allocate a hash table for type unit groups. */
7820
7821 static htab_t
7822 allocate_type_unit_groups_table (struct objfile *objfile)
7823 {
7824 return htab_create_alloc_ex (3,
7825 hash_type_unit_group,
7826 eq_type_unit_group,
7827 NULL,
7828 &objfile->objfile_obstack,
7829 hashtab_obstack_allocate,
7830 dummy_obstack_deallocate);
7831 }
7832
7833 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7834 partial symtabs. We combine several TUs per psymtab to not let the size
7835 of any one psymtab grow too big. */
7836 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7837 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7838
7839 /* Helper routine for get_type_unit_group.
7840 Create the type_unit_group object used to hold one or more TUs. */
7841
7842 static struct type_unit_group *
7843 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7844 {
7845 struct dwarf2_per_objfile *dwarf2_per_objfile
7846 = cu->per_cu->dwarf2_per_objfile;
7847 struct objfile *objfile = dwarf2_per_objfile->objfile;
7848 struct dwarf2_per_cu_data *per_cu;
7849 struct type_unit_group *tu_group;
7850
7851 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7852 struct type_unit_group);
7853 per_cu = &tu_group->per_cu;
7854 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7855
7856 if (dwarf2_per_objfile->using_index)
7857 {
7858 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7859 struct dwarf2_per_cu_quick_data);
7860 }
7861 else
7862 {
7863 unsigned int line_offset = to_underlying (line_offset_struct);
7864 struct partial_symtab *pst;
7865 std::string name;
7866
7867 /* Give the symtab a useful name for debug purposes. */
7868 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7869 name = string_printf ("<type_units_%d>",
7870 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7871 else
7872 name = string_printf ("<type_units_at_0x%x>", line_offset);
7873
7874 pst = create_partial_symtab (per_cu, name.c_str ());
7875 pst->anonymous = 1;
7876 }
7877
7878 tu_group->hash.dwo_unit = cu->dwo_unit;
7879 tu_group->hash.line_sect_off = line_offset_struct;
7880
7881 return tu_group;
7882 }
7883
7884 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7885 STMT_LIST is a DW_AT_stmt_list attribute. */
7886
7887 static struct type_unit_group *
7888 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7889 {
7890 struct dwarf2_per_objfile *dwarf2_per_objfile
7891 = cu->per_cu->dwarf2_per_objfile;
7892 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7893 struct type_unit_group *tu_group;
7894 void **slot;
7895 unsigned int line_offset;
7896 struct type_unit_group type_unit_group_for_lookup;
7897
7898 if (dwarf2_per_objfile->type_unit_groups == NULL)
7899 {
7900 dwarf2_per_objfile->type_unit_groups =
7901 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7902 }
7903
7904 /* Do we need to create a new group, or can we use an existing one? */
7905
7906 if (stmt_list)
7907 {
7908 line_offset = DW_UNSND (stmt_list);
7909 ++tu_stats->nr_symtab_sharers;
7910 }
7911 else
7912 {
7913 /* Ugh, no stmt_list. Rare, but we have to handle it.
7914 We can do various things here like create one group per TU or
7915 spread them over multiple groups to split up the expansion work.
7916 To avoid worst case scenarios (too many groups or too large groups)
7917 we, umm, group them in bunches. */
7918 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7919 | (tu_stats->nr_stmt_less_type_units
7920 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7921 ++tu_stats->nr_stmt_less_type_units;
7922 }
7923
7924 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7925 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7926 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7927 &type_unit_group_for_lookup, INSERT);
7928 if (*slot != NULL)
7929 {
7930 tu_group = (struct type_unit_group *) *slot;
7931 gdb_assert (tu_group != NULL);
7932 }
7933 else
7934 {
7935 sect_offset line_offset_struct = (sect_offset) line_offset;
7936 tu_group = create_type_unit_group (cu, line_offset_struct);
7937 *slot = tu_group;
7938 ++tu_stats->nr_symtabs;
7939 }
7940
7941 return tu_group;
7942 }
7943 \f
7944 /* Partial symbol tables. */
7945
7946 /* Create a psymtab named NAME and assign it to PER_CU.
7947
7948 The caller must fill in the following details:
7949 dirname, textlow, texthigh. */
7950
7951 static struct partial_symtab *
7952 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7953 {
7954 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7955 struct partial_symtab *pst;
7956
7957 pst = start_psymtab_common (objfile, name, 0);
7958
7959 pst->psymtabs_addrmap_supported = 1;
7960
7961 /* This is the glue that links PST into GDB's symbol API. */
7962 pst->read_symtab_private = per_cu;
7963 pst->read_symtab = dwarf2_read_symtab;
7964 per_cu->v.psymtab = pst;
7965
7966 return pst;
7967 }
7968
7969 /* The DATA object passed to process_psymtab_comp_unit_reader has this
7970 type. */
7971
7972 struct process_psymtab_comp_unit_data
7973 {
7974 /* True if we are reading a DW_TAG_partial_unit. */
7975
7976 int want_partial_unit;
7977
7978 /* The "pretend" language that is used if the CU doesn't declare a
7979 language. */
7980
7981 enum language pretend_language;
7982 };
7983
7984 /* die_reader_func for process_psymtab_comp_unit. */
7985
7986 static void
7987 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7988 const gdb_byte *info_ptr,
7989 struct die_info *comp_unit_die,
7990 int has_children,
7991 void *data)
7992 {
7993 struct dwarf2_cu *cu = reader->cu;
7994 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7995 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7996 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7997 CORE_ADDR baseaddr;
7998 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7999 struct partial_symtab *pst;
8000 enum pc_bounds_kind cu_bounds_kind;
8001 const char *filename;
8002 struct process_psymtab_comp_unit_data *info
8003 = (struct process_psymtab_comp_unit_data *) data;
8004
8005 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
8006 return;
8007
8008 gdb_assert (! per_cu->is_debug_types);
8009
8010 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
8011
8012 /* Allocate a new partial symbol table structure. */
8013 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8014 if (filename == NULL)
8015 filename = "";
8016
8017 pst = create_partial_symtab (per_cu, filename);
8018
8019 /* This must be done before calling dwarf2_build_include_psymtabs. */
8020 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8021
8022 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8023
8024 dwarf2_find_base_address (comp_unit_die, cu);
8025
8026 /* Possibly set the default values of LOWPC and HIGHPC from
8027 `DW_AT_ranges'. */
8028 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8029 &best_highpc, cu, pst);
8030 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8031 {
8032 CORE_ADDR low
8033 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8034 - baseaddr);
8035 CORE_ADDR high
8036 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8037 - baseaddr - 1);
8038 /* Store the contiguous range if it is not empty; it can be
8039 empty for CUs with no code. */
8040 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8041 low, high, pst);
8042 }
8043
8044 /* Check if comp unit has_children.
8045 If so, read the rest of the partial symbols from this comp unit.
8046 If not, there's no more debug_info for this comp unit. */
8047 if (has_children)
8048 {
8049 struct partial_die_info *first_die;
8050 CORE_ADDR lowpc, highpc;
8051
8052 lowpc = ((CORE_ADDR) -1);
8053 highpc = ((CORE_ADDR) 0);
8054
8055 first_die = load_partial_dies (reader, info_ptr, 1);
8056
8057 scan_partial_symbols (first_die, &lowpc, &highpc,
8058 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8059
8060 /* If we didn't find a lowpc, set it to highpc to avoid
8061 complaints from `maint check'. */
8062 if (lowpc == ((CORE_ADDR) -1))
8063 lowpc = highpc;
8064
8065 /* If the compilation unit didn't have an explicit address range,
8066 then use the information extracted from its child dies. */
8067 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8068 {
8069 best_lowpc = lowpc;
8070 best_highpc = highpc;
8071 }
8072 }
8073 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
8074 best_lowpc + baseaddr)
8075 - baseaddr);
8076 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
8077 best_highpc + baseaddr)
8078 - baseaddr);
8079
8080 end_psymtab_common (objfile, pst);
8081
8082 if (!cu->per_cu->imported_symtabs_empty ())
8083 {
8084 int i;
8085 int len = cu->per_cu->imported_symtabs_size ();
8086
8087 /* Fill in 'dependencies' here; we fill in 'users' in a
8088 post-pass. */
8089 pst->number_of_dependencies = len;
8090 pst->dependencies
8091 = objfile->partial_symtabs->allocate_dependencies (len);
8092 for (i = 0; i < len; ++i)
8093 {
8094 pst->dependencies[i]
8095 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
8096 }
8097
8098 cu->per_cu->imported_symtabs_free ();
8099 }
8100
8101 /* Get the list of files included in the current compilation unit,
8102 and build a psymtab for each of them. */
8103 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8104
8105 if (dwarf_read_debug)
8106 fprintf_unfiltered (gdb_stdlog,
8107 "Psymtab for %s unit @%s: %s - %s"
8108 ", %d global, %d static syms\n",
8109 per_cu->is_debug_types ? "type" : "comp",
8110 sect_offset_str (per_cu->sect_off),
8111 paddress (gdbarch, pst->text_low (objfile)),
8112 paddress (gdbarch, pst->text_high (objfile)),
8113 pst->n_global_syms, pst->n_static_syms);
8114 }
8115
8116 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8117 Process compilation unit THIS_CU for a psymtab. */
8118
8119 static void
8120 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8121 int want_partial_unit,
8122 enum language pretend_language)
8123 {
8124 /* If this compilation unit was already read in, free the
8125 cached copy in order to read it in again. This is
8126 necessary because we skipped some symbols when we first
8127 read in the compilation unit (see load_partial_dies).
8128 This problem could be avoided, but the benefit is unclear. */
8129 if (this_cu->cu != NULL)
8130 free_one_cached_comp_unit (this_cu);
8131
8132 if (this_cu->is_debug_types)
8133 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8134 build_type_psymtabs_reader, NULL);
8135 else
8136 {
8137 process_psymtab_comp_unit_data info;
8138 info.want_partial_unit = want_partial_unit;
8139 info.pretend_language = pretend_language;
8140 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8141 process_psymtab_comp_unit_reader, &info);
8142 }
8143
8144 /* Age out any secondary CUs. */
8145 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8146 }
8147
8148 /* Reader function for build_type_psymtabs. */
8149
8150 static void
8151 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8152 const gdb_byte *info_ptr,
8153 struct die_info *type_unit_die,
8154 int has_children,
8155 void *data)
8156 {
8157 struct dwarf2_per_objfile *dwarf2_per_objfile
8158 = reader->cu->per_cu->dwarf2_per_objfile;
8159 struct objfile *objfile = dwarf2_per_objfile->objfile;
8160 struct dwarf2_cu *cu = reader->cu;
8161 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8162 struct signatured_type *sig_type;
8163 struct type_unit_group *tu_group;
8164 struct attribute *attr;
8165 struct partial_die_info *first_die;
8166 CORE_ADDR lowpc, highpc;
8167 struct partial_symtab *pst;
8168
8169 gdb_assert (data == NULL);
8170 gdb_assert (per_cu->is_debug_types);
8171 sig_type = (struct signatured_type *) per_cu;
8172
8173 if (! has_children)
8174 return;
8175
8176 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8177 tu_group = get_type_unit_group (cu, attr);
8178
8179 if (tu_group->tus == nullptr)
8180 tu_group->tus = new std::vector<signatured_type *>;
8181 tu_group->tus->push_back (sig_type);
8182
8183 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8184 pst = create_partial_symtab (per_cu, "");
8185 pst->anonymous = 1;
8186
8187 first_die = load_partial_dies (reader, info_ptr, 1);
8188
8189 lowpc = (CORE_ADDR) -1;
8190 highpc = (CORE_ADDR) 0;
8191 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8192
8193 end_psymtab_common (objfile, pst);
8194 }
8195
8196 /* Struct used to sort TUs by their abbreviation table offset. */
8197
8198 struct tu_abbrev_offset
8199 {
8200 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8201 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8202 {}
8203
8204 signatured_type *sig_type;
8205 sect_offset abbrev_offset;
8206 };
8207
8208 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8209
8210 static bool
8211 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8212 const struct tu_abbrev_offset &b)
8213 {
8214 return a.abbrev_offset < b.abbrev_offset;
8215 }
8216
8217 /* Efficiently read all the type units.
8218 This does the bulk of the work for build_type_psymtabs.
8219
8220 The efficiency is because we sort TUs by the abbrev table they use and
8221 only read each abbrev table once. In one program there are 200K TUs
8222 sharing 8K abbrev tables.
8223
8224 The main purpose of this function is to support building the
8225 dwarf2_per_objfile->type_unit_groups table.
8226 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8227 can collapse the search space by grouping them by stmt_list.
8228 The savings can be significant, in the same program from above the 200K TUs
8229 share 8K stmt_list tables.
8230
8231 FUNC is expected to call get_type_unit_group, which will create the
8232 struct type_unit_group if necessary and add it to
8233 dwarf2_per_objfile->type_unit_groups. */
8234
8235 static void
8236 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8237 {
8238 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8239 abbrev_table_up abbrev_table;
8240 sect_offset abbrev_offset;
8241
8242 /* It's up to the caller to not call us multiple times. */
8243 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8244
8245 if (dwarf2_per_objfile->all_type_units.empty ())
8246 return;
8247
8248 /* TUs typically share abbrev tables, and there can be way more TUs than
8249 abbrev tables. Sort by abbrev table to reduce the number of times we
8250 read each abbrev table in.
8251 Alternatives are to punt or to maintain a cache of abbrev tables.
8252 This is simpler and efficient enough for now.
8253
8254 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8255 symtab to use). Typically TUs with the same abbrev offset have the same
8256 stmt_list value too so in practice this should work well.
8257
8258 The basic algorithm here is:
8259
8260 sort TUs by abbrev table
8261 for each TU with same abbrev table:
8262 read abbrev table if first user
8263 read TU top level DIE
8264 [IWBN if DWO skeletons had DW_AT_stmt_list]
8265 call FUNC */
8266
8267 if (dwarf_read_debug)
8268 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8269
8270 /* Sort in a separate table to maintain the order of all_type_units
8271 for .gdb_index: TU indices directly index all_type_units. */
8272 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8273 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8274
8275 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8276 sorted_by_abbrev.emplace_back
8277 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8278 sig_type->per_cu.section,
8279 sig_type->per_cu.sect_off));
8280
8281 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8282 sort_tu_by_abbrev_offset);
8283
8284 abbrev_offset = (sect_offset) ~(unsigned) 0;
8285
8286 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
8287 {
8288 /* Switch to the next abbrev table if necessary. */
8289 if (abbrev_table == NULL
8290 || tu.abbrev_offset != abbrev_offset)
8291 {
8292 abbrev_offset = tu.abbrev_offset;
8293 abbrev_table =
8294 abbrev_table_read_table (dwarf2_per_objfile,
8295 &dwarf2_per_objfile->abbrev,
8296 abbrev_offset);
8297 ++tu_stats->nr_uniq_abbrev_tables;
8298 }
8299
8300 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
8301 0, 0, false, build_type_psymtabs_reader, NULL);
8302 }
8303 }
8304
8305 /* Print collected type unit statistics. */
8306
8307 static void
8308 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8309 {
8310 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8311
8312 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8313 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8314 dwarf2_per_objfile->all_type_units.size ());
8315 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8316 tu_stats->nr_uniq_abbrev_tables);
8317 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8318 tu_stats->nr_symtabs);
8319 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8320 tu_stats->nr_symtab_sharers);
8321 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8322 tu_stats->nr_stmt_less_type_units);
8323 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8324 tu_stats->nr_all_type_units_reallocs);
8325 }
8326
8327 /* Traversal function for build_type_psymtabs. */
8328
8329 static int
8330 build_type_psymtab_dependencies (void **slot, void *info)
8331 {
8332 struct dwarf2_per_objfile *dwarf2_per_objfile
8333 = (struct dwarf2_per_objfile *) info;
8334 struct objfile *objfile = dwarf2_per_objfile->objfile;
8335 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8336 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8337 struct partial_symtab *pst = per_cu->v.psymtab;
8338 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
8339 int i;
8340
8341 gdb_assert (len > 0);
8342 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8343
8344 pst->number_of_dependencies = len;
8345 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
8346 for (i = 0; i < len; ++i)
8347 {
8348 struct signatured_type *iter = tu_group->tus->at (i);
8349 gdb_assert (iter->per_cu.is_debug_types);
8350 pst->dependencies[i] = iter->per_cu.v.psymtab;
8351 iter->type_unit_group = tu_group;
8352 }
8353
8354 delete tu_group->tus;
8355 tu_group->tus = nullptr;
8356
8357 return 1;
8358 }
8359
8360 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8361 Build partial symbol tables for the .debug_types comp-units. */
8362
8363 static void
8364 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8365 {
8366 if (! create_all_type_units (dwarf2_per_objfile))
8367 return;
8368
8369 build_type_psymtabs_1 (dwarf2_per_objfile);
8370 }
8371
8372 /* Traversal function for process_skeletonless_type_unit.
8373 Read a TU in a DWO file and build partial symbols for it. */
8374
8375 static int
8376 process_skeletonless_type_unit (void **slot, void *info)
8377 {
8378 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8379 struct dwarf2_per_objfile *dwarf2_per_objfile
8380 = (struct dwarf2_per_objfile *) info;
8381 struct signatured_type find_entry, *entry;
8382
8383 /* If this TU doesn't exist in the global table, add it and read it in. */
8384
8385 if (dwarf2_per_objfile->signatured_types == NULL)
8386 {
8387 dwarf2_per_objfile->signatured_types
8388 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8389 }
8390
8391 find_entry.signature = dwo_unit->signature;
8392 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8393 INSERT);
8394 /* If we've already seen this type there's nothing to do. What's happening
8395 is we're doing our own version of comdat-folding here. */
8396 if (*slot != NULL)
8397 return 1;
8398
8399 /* This does the job that create_all_type_units would have done for
8400 this TU. */
8401 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8402 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8403 *slot = entry;
8404
8405 /* This does the job that build_type_psymtabs_1 would have done. */
8406 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
8407 build_type_psymtabs_reader, NULL);
8408
8409 return 1;
8410 }
8411
8412 /* Traversal function for process_skeletonless_type_units. */
8413
8414 static int
8415 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8416 {
8417 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8418
8419 if (dwo_file->tus != NULL)
8420 {
8421 htab_traverse_noresize (dwo_file->tus,
8422 process_skeletonless_type_unit, info);
8423 }
8424
8425 return 1;
8426 }
8427
8428 /* Scan all TUs of DWO files, verifying we've processed them.
8429 This is needed in case a TU was emitted without its skeleton.
8430 Note: This can't be done until we know what all the DWO files are. */
8431
8432 static void
8433 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8434 {
8435 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8436 if (get_dwp_file (dwarf2_per_objfile) == NULL
8437 && dwarf2_per_objfile->dwo_files != NULL)
8438 {
8439 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
8440 process_dwo_file_for_skeletonless_type_units,
8441 dwarf2_per_objfile);
8442 }
8443 }
8444
8445 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8446
8447 static void
8448 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8449 {
8450 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8451 {
8452 struct partial_symtab *pst = per_cu->v.psymtab;
8453
8454 if (pst == NULL)
8455 continue;
8456
8457 for (int j = 0; j < pst->number_of_dependencies; ++j)
8458 {
8459 /* Set the 'user' field only if it is not already set. */
8460 if (pst->dependencies[j]->user == NULL)
8461 pst->dependencies[j]->user = pst;
8462 }
8463 }
8464 }
8465
8466 /* Build the partial symbol table by doing a quick pass through the
8467 .debug_info and .debug_abbrev sections. */
8468
8469 static void
8470 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8471 {
8472 struct objfile *objfile = dwarf2_per_objfile->objfile;
8473
8474 if (dwarf_read_debug)
8475 {
8476 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8477 objfile_name (objfile));
8478 }
8479
8480 dwarf2_per_objfile->reading_partial_symbols = 1;
8481
8482 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8483
8484 /* Any cached compilation units will be linked by the per-objfile
8485 read_in_chain. Make sure to free them when we're done. */
8486 free_cached_comp_units freer (dwarf2_per_objfile);
8487
8488 build_type_psymtabs (dwarf2_per_objfile);
8489
8490 create_all_comp_units (dwarf2_per_objfile);
8491
8492 /* Create a temporary address map on a temporary obstack. We later
8493 copy this to the final obstack. */
8494 auto_obstack temp_obstack;
8495
8496 scoped_restore save_psymtabs_addrmap
8497 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8498 addrmap_create_mutable (&temp_obstack));
8499
8500 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8501 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8502
8503 /* This has to wait until we read the CUs, we need the list of DWOs. */
8504 process_skeletonless_type_units (dwarf2_per_objfile);
8505
8506 /* Now that all TUs have been processed we can fill in the dependencies. */
8507 if (dwarf2_per_objfile->type_unit_groups != NULL)
8508 {
8509 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8510 build_type_psymtab_dependencies, dwarf2_per_objfile);
8511 }
8512
8513 if (dwarf_read_debug)
8514 print_tu_stats (dwarf2_per_objfile);
8515
8516 set_partial_user (dwarf2_per_objfile);
8517
8518 objfile->partial_symtabs->psymtabs_addrmap
8519 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8520 objfile->partial_symtabs->obstack ());
8521 /* At this point we want to keep the address map. */
8522 save_psymtabs_addrmap.release ();
8523
8524 if (dwarf_read_debug)
8525 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8526 objfile_name (objfile));
8527 }
8528
8529 /* die_reader_func for load_partial_comp_unit. */
8530
8531 static void
8532 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8533 const gdb_byte *info_ptr,
8534 struct die_info *comp_unit_die,
8535 int has_children,
8536 void *data)
8537 {
8538 struct dwarf2_cu *cu = reader->cu;
8539
8540 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8541
8542 /* Check if comp unit has_children.
8543 If so, read the rest of the partial symbols from this comp unit.
8544 If not, there's no more debug_info for this comp unit. */
8545 if (has_children)
8546 load_partial_dies (reader, info_ptr, 0);
8547 }
8548
8549 /* Load the partial DIEs for a secondary CU into memory.
8550 This is also used when rereading a primary CU with load_all_dies. */
8551
8552 static void
8553 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8554 {
8555 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
8556 load_partial_comp_unit_reader, NULL);
8557 }
8558
8559 static void
8560 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8561 struct dwarf2_section_info *section,
8562 struct dwarf2_section_info *abbrev_section,
8563 unsigned int is_dwz)
8564 {
8565 const gdb_byte *info_ptr;
8566 struct objfile *objfile = dwarf2_per_objfile->objfile;
8567
8568 if (dwarf_read_debug)
8569 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8570 get_section_name (section),
8571 get_section_file_name (section));
8572
8573 dwarf2_read_section (objfile, section);
8574
8575 info_ptr = section->buffer;
8576
8577 while (info_ptr < section->buffer + section->size)
8578 {
8579 struct dwarf2_per_cu_data *this_cu;
8580
8581 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8582
8583 comp_unit_head cu_header;
8584 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8585 abbrev_section, info_ptr,
8586 rcuh_kind::COMPILE);
8587
8588 /* Save the compilation unit for later lookup. */
8589 if (cu_header.unit_type != DW_UT_type)
8590 {
8591 this_cu = XOBNEW (&objfile->objfile_obstack,
8592 struct dwarf2_per_cu_data);
8593 memset (this_cu, 0, sizeof (*this_cu));
8594 }
8595 else
8596 {
8597 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8598 struct signatured_type);
8599 memset (sig_type, 0, sizeof (*sig_type));
8600 sig_type->signature = cu_header.signature;
8601 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8602 this_cu = &sig_type->per_cu;
8603 }
8604 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8605 this_cu->sect_off = sect_off;
8606 this_cu->length = cu_header.length + cu_header.initial_length_size;
8607 this_cu->is_dwz = is_dwz;
8608 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8609 this_cu->section = section;
8610
8611 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
8612
8613 info_ptr = info_ptr + this_cu->length;
8614 }
8615 }
8616
8617 /* Create a list of all compilation units in OBJFILE.
8618 This is only done for -readnow and building partial symtabs. */
8619
8620 static void
8621 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8622 {
8623 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
8624 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8625 &dwarf2_per_objfile->abbrev, 0);
8626
8627 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8628 if (dwz != NULL)
8629 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8630 1);
8631 }
8632
8633 /* Process all loaded DIEs for compilation unit CU, starting at
8634 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8635 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8636 DW_AT_ranges). See the comments of add_partial_subprogram on how
8637 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8638
8639 static void
8640 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8641 CORE_ADDR *highpc, int set_addrmap,
8642 struct dwarf2_cu *cu)
8643 {
8644 struct partial_die_info *pdi;
8645
8646 /* Now, march along the PDI's, descending into ones which have
8647 interesting children but skipping the children of the other ones,
8648 until we reach the end of the compilation unit. */
8649
8650 pdi = first_die;
8651
8652 while (pdi != NULL)
8653 {
8654 pdi->fixup (cu);
8655
8656 /* Anonymous namespaces or modules have no name but have interesting
8657 children, so we need to look at them. Ditto for anonymous
8658 enums. */
8659
8660 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8661 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8662 || pdi->tag == DW_TAG_imported_unit
8663 || pdi->tag == DW_TAG_inlined_subroutine)
8664 {
8665 switch (pdi->tag)
8666 {
8667 case DW_TAG_subprogram:
8668 case DW_TAG_inlined_subroutine:
8669 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8670 break;
8671 case DW_TAG_constant:
8672 case DW_TAG_variable:
8673 case DW_TAG_typedef:
8674 case DW_TAG_union_type:
8675 if (!pdi->is_declaration)
8676 {
8677 add_partial_symbol (pdi, cu);
8678 }
8679 break;
8680 case DW_TAG_class_type:
8681 case DW_TAG_interface_type:
8682 case DW_TAG_structure_type:
8683 if (!pdi->is_declaration)
8684 {
8685 add_partial_symbol (pdi, cu);
8686 }
8687 if ((cu->language == language_rust
8688 || cu->language == language_cplus) && pdi->has_children)
8689 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8690 set_addrmap, cu);
8691 break;
8692 case DW_TAG_enumeration_type:
8693 if (!pdi->is_declaration)
8694 add_partial_enumeration (pdi, cu);
8695 break;
8696 case DW_TAG_base_type:
8697 case DW_TAG_subrange_type:
8698 /* File scope base type definitions are added to the partial
8699 symbol table. */
8700 add_partial_symbol (pdi, cu);
8701 break;
8702 case DW_TAG_namespace:
8703 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8704 break;
8705 case DW_TAG_module:
8706 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8707 break;
8708 case DW_TAG_imported_unit:
8709 {
8710 struct dwarf2_per_cu_data *per_cu;
8711
8712 /* For now we don't handle imported units in type units. */
8713 if (cu->per_cu->is_debug_types)
8714 {
8715 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8716 " supported in type units [in module %s]"),
8717 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8718 }
8719
8720 per_cu = dwarf2_find_containing_comp_unit
8721 (pdi->d.sect_off, pdi->is_dwz,
8722 cu->per_cu->dwarf2_per_objfile);
8723
8724 /* Go read the partial unit, if needed. */
8725 if (per_cu->v.psymtab == NULL)
8726 process_psymtab_comp_unit (per_cu, 1, cu->language);
8727
8728 cu->per_cu->imported_symtabs_push (per_cu);
8729 }
8730 break;
8731 case DW_TAG_imported_declaration:
8732 add_partial_symbol (pdi, cu);
8733 break;
8734 default:
8735 break;
8736 }
8737 }
8738
8739 /* If the die has a sibling, skip to the sibling. */
8740
8741 pdi = pdi->die_sibling;
8742 }
8743 }
8744
8745 /* Functions used to compute the fully scoped name of a partial DIE.
8746
8747 Normally, this is simple. For C++, the parent DIE's fully scoped
8748 name is concatenated with "::" and the partial DIE's name.
8749 Enumerators are an exception; they use the scope of their parent
8750 enumeration type, i.e. the name of the enumeration type is not
8751 prepended to the enumerator.
8752
8753 There are two complexities. One is DW_AT_specification; in this
8754 case "parent" means the parent of the target of the specification,
8755 instead of the direct parent of the DIE. The other is compilers
8756 which do not emit DW_TAG_namespace; in this case we try to guess
8757 the fully qualified name of structure types from their members'
8758 linkage names. This must be done using the DIE's children rather
8759 than the children of any DW_AT_specification target. We only need
8760 to do this for structures at the top level, i.e. if the target of
8761 any DW_AT_specification (if any; otherwise the DIE itself) does not
8762 have a parent. */
8763
8764 /* Compute the scope prefix associated with PDI's parent, in
8765 compilation unit CU. The result will be allocated on CU's
8766 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8767 field. NULL is returned if no prefix is necessary. */
8768 static const char *
8769 partial_die_parent_scope (struct partial_die_info *pdi,
8770 struct dwarf2_cu *cu)
8771 {
8772 const char *grandparent_scope;
8773 struct partial_die_info *parent, *real_pdi;
8774
8775 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8776 then this means the parent of the specification DIE. */
8777
8778 real_pdi = pdi;
8779 while (real_pdi->has_specification)
8780 {
8781 auto res = find_partial_die (real_pdi->spec_offset,
8782 real_pdi->spec_is_dwz, cu);
8783 real_pdi = res.pdi;
8784 cu = res.cu;
8785 }
8786
8787 parent = real_pdi->die_parent;
8788 if (parent == NULL)
8789 return NULL;
8790
8791 if (parent->scope_set)
8792 return parent->scope;
8793
8794 parent->fixup (cu);
8795
8796 grandparent_scope = partial_die_parent_scope (parent, cu);
8797
8798 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8799 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8800 Work around this problem here. */
8801 if (cu->language == language_cplus
8802 && parent->tag == DW_TAG_namespace
8803 && strcmp (parent->name, "::") == 0
8804 && grandparent_scope == NULL)
8805 {
8806 parent->scope = NULL;
8807 parent->scope_set = 1;
8808 return NULL;
8809 }
8810
8811 /* Nested subroutines in Fortran get a prefix. */
8812 if (pdi->tag == DW_TAG_enumerator)
8813 /* Enumerators should not get the name of the enumeration as a prefix. */
8814 parent->scope = grandparent_scope;
8815 else if (parent->tag == DW_TAG_namespace
8816 || parent->tag == DW_TAG_module
8817 || parent->tag == DW_TAG_structure_type
8818 || parent->tag == DW_TAG_class_type
8819 || parent->tag == DW_TAG_interface_type
8820 || parent->tag == DW_TAG_union_type
8821 || parent->tag == DW_TAG_enumeration_type
8822 || (cu->language == language_fortran
8823 && parent->tag == DW_TAG_subprogram
8824 && pdi->tag == DW_TAG_subprogram))
8825 {
8826 if (grandparent_scope == NULL)
8827 parent->scope = parent->name;
8828 else
8829 parent->scope = typename_concat (&cu->comp_unit_obstack,
8830 grandparent_scope,
8831 parent->name, 0, cu);
8832 }
8833 else
8834 {
8835 /* FIXME drow/2004-04-01: What should we be doing with
8836 function-local names? For partial symbols, we should probably be
8837 ignoring them. */
8838 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8839 dwarf_tag_name (parent->tag),
8840 sect_offset_str (pdi->sect_off));
8841 parent->scope = grandparent_scope;
8842 }
8843
8844 parent->scope_set = 1;
8845 return parent->scope;
8846 }
8847
8848 /* Return the fully scoped name associated with PDI, from compilation unit
8849 CU. The result will be allocated with malloc. */
8850
8851 static char *
8852 partial_die_full_name (struct partial_die_info *pdi,
8853 struct dwarf2_cu *cu)
8854 {
8855 const char *parent_scope;
8856
8857 /* If this is a template instantiation, we can not work out the
8858 template arguments from partial DIEs. So, unfortunately, we have
8859 to go through the full DIEs. At least any work we do building
8860 types here will be reused if full symbols are loaded later. */
8861 if (pdi->has_template_arguments)
8862 {
8863 pdi->fixup (cu);
8864
8865 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8866 {
8867 struct die_info *die;
8868 struct attribute attr;
8869 struct dwarf2_cu *ref_cu = cu;
8870
8871 /* DW_FORM_ref_addr is using section offset. */
8872 attr.name = (enum dwarf_attribute) 0;
8873 attr.form = DW_FORM_ref_addr;
8874 attr.u.unsnd = to_underlying (pdi->sect_off);
8875 die = follow_die_ref (NULL, &attr, &ref_cu);
8876
8877 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8878 }
8879 }
8880
8881 parent_scope = partial_die_parent_scope (pdi, cu);
8882 if (parent_scope == NULL)
8883 return NULL;
8884 else
8885 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
8886 }
8887
8888 static void
8889 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8890 {
8891 struct dwarf2_per_objfile *dwarf2_per_objfile
8892 = cu->per_cu->dwarf2_per_objfile;
8893 struct objfile *objfile = dwarf2_per_objfile->objfile;
8894 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8895 CORE_ADDR addr = 0;
8896 const char *actual_name = NULL;
8897 CORE_ADDR baseaddr;
8898 char *built_actual_name;
8899
8900 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8901
8902 built_actual_name = partial_die_full_name (pdi, cu);
8903 if (built_actual_name != NULL)
8904 actual_name = built_actual_name;
8905
8906 if (actual_name == NULL)
8907 actual_name = pdi->name;
8908
8909 switch (pdi->tag)
8910 {
8911 case DW_TAG_inlined_subroutine:
8912 case DW_TAG_subprogram:
8913 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8914 - baseaddr);
8915 if (pdi->is_external
8916 || cu->language == language_ada
8917 || (cu->language == language_fortran
8918 && pdi->die_parent != NULL
8919 && pdi->die_parent->tag == DW_TAG_subprogram))
8920 {
8921 /* Normally, only "external" DIEs are part of the global scope.
8922 But in Ada and Fortran, we want to be able to access nested
8923 procedures globally. So all Ada and Fortran subprograms are
8924 stored in the global scope. */
8925 add_psymbol_to_list (actual_name, strlen (actual_name),
8926 built_actual_name != NULL,
8927 VAR_DOMAIN, LOC_BLOCK,
8928 SECT_OFF_TEXT (objfile),
8929 psymbol_placement::GLOBAL,
8930 addr,
8931 cu->language, objfile);
8932 }
8933 else
8934 {
8935 add_psymbol_to_list (actual_name, strlen (actual_name),
8936 built_actual_name != NULL,
8937 VAR_DOMAIN, LOC_BLOCK,
8938 SECT_OFF_TEXT (objfile),
8939 psymbol_placement::STATIC,
8940 addr, cu->language, objfile);
8941 }
8942
8943 if (pdi->main_subprogram && actual_name != NULL)
8944 set_objfile_main_name (objfile, actual_name, cu->language);
8945 break;
8946 case DW_TAG_constant:
8947 add_psymbol_to_list (actual_name, strlen (actual_name),
8948 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8949 -1, (pdi->is_external
8950 ? psymbol_placement::GLOBAL
8951 : psymbol_placement::STATIC),
8952 0, cu->language, objfile);
8953 break;
8954 case DW_TAG_variable:
8955 if (pdi->d.locdesc)
8956 addr = decode_locdesc (pdi->d.locdesc, cu);
8957
8958 if (pdi->d.locdesc
8959 && addr == 0
8960 && !dwarf2_per_objfile->has_section_at_zero)
8961 {
8962 /* A global or static variable may also have been stripped
8963 out by the linker if unused, in which case its address
8964 will be nullified; do not add such variables into partial
8965 symbol table then. */
8966 }
8967 else if (pdi->is_external)
8968 {
8969 /* Global Variable.
8970 Don't enter into the minimal symbol tables as there is
8971 a minimal symbol table entry from the ELF symbols already.
8972 Enter into partial symbol table if it has a location
8973 descriptor or a type.
8974 If the location descriptor is missing, new_symbol will create
8975 a LOC_UNRESOLVED symbol, the address of the variable will then
8976 be determined from the minimal symbol table whenever the variable
8977 is referenced.
8978 The address for the partial symbol table entry is not
8979 used by GDB, but it comes in handy for debugging partial symbol
8980 table building. */
8981
8982 if (pdi->d.locdesc || pdi->has_type)
8983 add_psymbol_to_list (actual_name, strlen (actual_name),
8984 built_actual_name != NULL,
8985 VAR_DOMAIN, LOC_STATIC,
8986 SECT_OFF_TEXT (objfile),
8987 psymbol_placement::GLOBAL,
8988 addr, cu->language, objfile);
8989 }
8990 else
8991 {
8992 int has_loc = pdi->d.locdesc != NULL;
8993
8994 /* Static Variable. Skip symbols whose value we cannot know (those
8995 without location descriptors or constant values). */
8996 if (!has_loc && !pdi->has_const_value)
8997 {
8998 xfree (built_actual_name);
8999 return;
9000 }
9001
9002 add_psymbol_to_list (actual_name, strlen (actual_name),
9003 built_actual_name != NULL,
9004 VAR_DOMAIN, LOC_STATIC,
9005 SECT_OFF_TEXT (objfile),
9006 psymbol_placement::STATIC,
9007 has_loc ? addr : 0,
9008 cu->language, objfile);
9009 }
9010 break;
9011 case DW_TAG_typedef:
9012 case DW_TAG_base_type:
9013 case DW_TAG_subrange_type:
9014 add_psymbol_to_list (actual_name, strlen (actual_name),
9015 built_actual_name != NULL,
9016 VAR_DOMAIN, LOC_TYPEDEF, -1,
9017 psymbol_placement::STATIC,
9018 0, cu->language, objfile);
9019 break;
9020 case DW_TAG_imported_declaration:
9021 case DW_TAG_namespace:
9022 add_psymbol_to_list (actual_name, strlen (actual_name),
9023 built_actual_name != NULL,
9024 VAR_DOMAIN, LOC_TYPEDEF, -1,
9025 psymbol_placement::GLOBAL,
9026 0, cu->language, objfile);
9027 break;
9028 case DW_TAG_module:
9029 /* With Fortran 77 there might be a "BLOCK DATA" module
9030 available without any name. If so, we skip the module as it
9031 doesn't bring any value. */
9032 if (actual_name != nullptr)
9033 add_psymbol_to_list (actual_name, strlen (actual_name),
9034 built_actual_name != NULL,
9035 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9036 psymbol_placement::GLOBAL,
9037 0, cu->language, objfile);
9038 break;
9039 case DW_TAG_class_type:
9040 case DW_TAG_interface_type:
9041 case DW_TAG_structure_type:
9042 case DW_TAG_union_type:
9043 case DW_TAG_enumeration_type:
9044 /* Skip external references. The DWARF standard says in the section
9045 about "Structure, Union, and Class Type Entries": "An incomplete
9046 structure, union or class type is represented by a structure,
9047 union or class entry that does not have a byte size attribute
9048 and that has a DW_AT_declaration attribute." */
9049 if (!pdi->has_byte_size && pdi->is_declaration)
9050 {
9051 xfree (built_actual_name);
9052 return;
9053 }
9054
9055 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9056 static vs. global. */
9057 add_psymbol_to_list (actual_name, strlen (actual_name),
9058 built_actual_name != NULL,
9059 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9060 cu->language == language_cplus
9061 ? psymbol_placement::GLOBAL
9062 : psymbol_placement::STATIC,
9063 0, cu->language, objfile);
9064
9065 break;
9066 case DW_TAG_enumerator:
9067 add_psymbol_to_list (actual_name, strlen (actual_name),
9068 built_actual_name != NULL,
9069 VAR_DOMAIN, LOC_CONST, -1,
9070 cu->language == language_cplus
9071 ? psymbol_placement::GLOBAL
9072 : psymbol_placement::STATIC,
9073 0, cu->language, objfile);
9074 break;
9075 default:
9076 break;
9077 }
9078
9079 xfree (built_actual_name);
9080 }
9081
9082 /* Read a partial die corresponding to a namespace; also, add a symbol
9083 corresponding to that namespace to the symbol table. NAMESPACE is
9084 the name of the enclosing namespace. */
9085
9086 static void
9087 add_partial_namespace (struct partial_die_info *pdi,
9088 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9089 int set_addrmap, struct dwarf2_cu *cu)
9090 {
9091 /* Add a symbol for the namespace. */
9092
9093 add_partial_symbol (pdi, cu);
9094
9095 /* Now scan partial symbols in that namespace. */
9096
9097 if (pdi->has_children)
9098 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9099 }
9100
9101 /* Read a partial die corresponding to a Fortran module. */
9102
9103 static void
9104 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9105 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9106 {
9107 /* Add a symbol for the namespace. */
9108
9109 add_partial_symbol (pdi, cu);
9110
9111 /* Now scan partial symbols in that module. */
9112
9113 if (pdi->has_children)
9114 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9115 }
9116
9117 /* Read a partial die corresponding to a subprogram or an inlined
9118 subprogram and create a partial symbol for that subprogram.
9119 When the CU language allows it, this routine also defines a partial
9120 symbol for each nested subprogram that this subprogram contains.
9121 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9122 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9123
9124 PDI may also be a lexical block, in which case we simply search
9125 recursively for subprograms defined inside that lexical block.
9126 Again, this is only performed when the CU language allows this
9127 type of definitions. */
9128
9129 static void
9130 add_partial_subprogram (struct partial_die_info *pdi,
9131 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9132 int set_addrmap, struct dwarf2_cu *cu)
9133 {
9134 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9135 {
9136 if (pdi->has_pc_info)
9137 {
9138 if (pdi->lowpc < *lowpc)
9139 *lowpc = pdi->lowpc;
9140 if (pdi->highpc > *highpc)
9141 *highpc = pdi->highpc;
9142 if (set_addrmap)
9143 {
9144 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9145 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9146 CORE_ADDR baseaddr;
9147 CORE_ADDR this_highpc;
9148 CORE_ADDR this_lowpc;
9149
9150 baseaddr = ANOFFSET (objfile->section_offsets,
9151 SECT_OFF_TEXT (objfile));
9152 this_lowpc
9153 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9154 pdi->lowpc + baseaddr)
9155 - baseaddr);
9156 this_highpc
9157 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9158 pdi->highpc + baseaddr)
9159 - baseaddr);
9160 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
9161 this_lowpc, this_highpc - 1,
9162 cu->per_cu->v.psymtab);
9163 }
9164 }
9165
9166 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9167 {
9168 if (!pdi->is_declaration)
9169 /* Ignore subprogram DIEs that do not have a name, they are
9170 illegal. Do not emit a complaint at this point, we will
9171 do so when we convert this psymtab into a symtab. */
9172 if (pdi->name)
9173 add_partial_symbol (pdi, cu);
9174 }
9175 }
9176
9177 if (! pdi->has_children)
9178 return;
9179
9180 if (cu->language == language_ada || cu->language == language_fortran)
9181 {
9182 pdi = pdi->die_child;
9183 while (pdi != NULL)
9184 {
9185 pdi->fixup (cu);
9186 if (pdi->tag == DW_TAG_subprogram
9187 || pdi->tag == DW_TAG_inlined_subroutine
9188 || pdi->tag == DW_TAG_lexical_block)
9189 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9190 pdi = pdi->die_sibling;
9191 }
9192 }
9193 }
9194
9195 /* Read a partial die corresponding to an enumeration type. */
9196
9197 static void
9198 add_partial_enumeration (struct partial_die_info *enum_pdi,
9199 struct dwarf2_cu *cu)
9200 {
9201 struct partial_die_info *pdi;
9202
9203 if (enum_pdi->name != NULL)
9204 add_partial_symbol (enum_pdi, cu);
9205
9206 pdi = enum_pdi->die_child;
9207 while (pdi)
9208 {
9209 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9210 complaint (_("malformed enumerator DIE ignored"));
9211 else
9212 add_partial_symbol (pdi, cu);
9213 pdi = pdi->die_sibling;
9214 }
9215 }
9216
9217 /* Return the initial uleb128 in the die at INFO_PTR. */
9218
9219 static unsigned int
9220 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9221 {
9222 unsigned int bytes_read;
9223
9224 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9225 }
9226
9227 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9228 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9229
9230 Return the corresponding abbrev, or NULL if the number is zero (indicating
9231 an empty DIE). In either case *BYTES_READ will be set to the length of
9232 the initial number. */
9233
9234 static struct abbrev_info *
9235 peek_die_abbrev (const die_reader_specs &reader,
9236 const gdb_byte *info_ptr, unsigned int *bytes_read)
9237 {
9238 dwarf2_cu *cu = reader.cu;
9239 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9240 unsigned int abbrev_number
9241 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9242
9243 if (abbrev_number == 0)
9244 return NULL;
9245
9246 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9247 if (!abbrev)
9248 {
9249 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9250 " at offset %s [in module %s]"),
9251 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9252 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9253 }
9254
9255 return abbrev;
9256 }
9257
9258 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9259 Returns a pointer to the end of a series of DIEs, terminated by an empty
9260 DIE. Any children of the skipped DIEs will also be skipped. */
9261
9262 static const gdb_byte *
9263 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9264 {
9265 while (1)
9266 {
9267 unsigned int bytes_read;
9268 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9269
9270 if (abbrev == NULL)
9271 return info_ptr + bytes_read;
9272 else
9273 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9274 }
9275 }
9276
9277 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9278 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9279 abbrev corresponding to that skipped uleb128 should be passed in
9280 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9281 children. */
9282
9283 static const gdb_byte *
9284 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9285 struct abbrev_info *abbrev)
9286 {
9287 unsigned int bytes_read;
9288 struct attribute attr;
9289 bfd *abfd = reader->abfd;
9290 struct dwarf2_cu *cu = reader->cu;
9291 const gdb_byte *buffer = reader->buffer;
9292 const gdb_byte *buffer_end = reader->buffer_end;
9293 unsigned int form, i;
9294
9295 for (i = 0; i < abbrev->num_attrs; i++)
9296 {
9297 /* The only abbrev we care about is DW_AT_sibling. */
9298 if (abbrev->attrs[i].name == DW_AT_sibling)
9299 {
9300 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9301 if (attr.form == DW_FORM_ref_addr)
9302 complaint (_("ignoring absolute DW_AT_sibling"));
9303 else
9304 {
9305 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9306 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9307
9308 if (sibling_ptr < info_ptr)
9309 complaint (_("DW_AT_sibling points backwards"));
9310 else if (sibling_ptr > reader->buffer_end)
9311 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9312 else
9313 return sibling_ptr;
9314 }
9315 }
9316
9317 /* If it isn't DW_AT_sibling, skip this attribute. */
9318 form = abbrev->attrs[i].form;
9319 skip_attribute:
9320 switch (form)
9321 {
9322 case DW_FORM_ref_addr:
9323 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9324 and later it is offset sized. */
9325 if (cu->header.version == 2)
9326 info_ptr += cu->header.addr_size;
9327 else
9328 info_ptr += cu->header.offset_size;
9329 break;
9330 case DW_FORM_GNU_ref_alt:
9331 info_ptr += cu->header.offset_size;
9332 break;
9333 case DW_FORM_addr:
9334 info_ptr += cu->header.addr_size;
9335 break;
9336 case DW_FORM_data1:
9337 case DW_FORM_ref1:
9338 case DW_FORM_flag:
9339 case DW_FORM_strx1:
9340 info_ptr += 1;
9341 break;
9342 case DW_FORM_flag_present:
9343 case DW_FORM_implicit_const:
9344 break;
9345 case DW_FORM_data2:
9346 case DW_FORM_ref2:
9347 case DW_FORM_strx2:
9348 info_ptr += 2;
9349 break;
9350 case DW_FORM_strx3:
9351 info_ptr += 3;
9352 break;
9353 case DW_FORM_data4:
9354 case DW_FORM_ref4:
9355 case DW_FORM_strx4:
9356 info_ptr += 4;
9357 break;
9358 case DW_FORM_data8:
9359 case DW_FORM_ref8:
9360 case DW_FORM_ref_sig8:
9361 info_ptr += 8;
9362 break;
9363 case DW_FORM_data16:
9364 info_ptr += 16;
9365 break;
9366 case DW_FORM_string:
9367 read_direct_string (abfd, info_ptr, &bytes_read);
9368 info_ptr += bytes_read;
9369 break;
9370 case DW_FORM_sec_offset:
9371 case DW_FORM_strp:
9372 case DW_FORM_GNU_strp_alt:
9373 info_ptr += cu->header.offset_size;
9374 break;
9375 case DW_FORM_exprloc:
9376 case DW_FORM_block:
9377 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9378 info_ptr += bytes_read;
9379 break;
9380 case DW_FORM_block1:
9381 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9382 break;
9383 case DW_FORM_block2:
9384 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9385 break;
9386 case DW_FORM_block4:
9387 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9388 break;
9389 case DW_FORM_addrx:
9390 case DW_FORM_strx:
9391 case DW_FORM_sdata:
9392 case DW_FORM_udata:
9393 case DW_FORM_ref_udata:
9394 case DW_FORM_GNU_addr_index:
9395 case DW_FORM_GNU_str_index:
9396 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9397 break;
9398 case DW_FORM_indirect:
9399 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9400 info_ptr += bytes_read;
9401 /* We need to continue parsing from here, so just go back to
9402 the top. */
9403 goto skip_attribute;
9404
9405 default:
9406 error (_("Dwarf Error: Cannot handle %s "
9407 "in DWARF reader [in module %s]"),
9408 dwarf_form_name (form),
9409 bfd_get_filename (abfd));
9410 }
9411 }
9412
9413 if (abbrev->has_children)
9414 return skip_children (reader, info_ptr);
9415 else
9416 return info_ptr;
9417 }
9418
9419 /* Locate ORIG_PDI's sibling.
9420 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9421
9422 static const gdb_byte *
9423 locate_pdi_sibling (const struct die_reader_specs *reader,
9424 struct partial_die_info *orig_pdi,
9425 const gdb_byte *info_ptr)
9426 {
9427 /* Do we know the sibling already? */
9428
9429 if (orig_pdi->sibling)
9430 return orig_pdi->sibling;
9431
9432 /* Are there any children to deal with? */
9433
9434 if (!orig_pdi->has_children)
9435 return info_ptr;
9436
9437 /* Skip the children the long way. */
9438
9439 return skip_children (reader, info_ptr);
9440 }
9441
9442 /* Expand this partial symbol table into a full symbol table. SELF is
9443 not NULL. */
9444
9445 static void
9446 dwarf2_read_symtab (struct partial_symtab *self,
9447 struct objfile *objfile)
9448 {
9449 struct dwarf2_per_objfile *dwarf2_per_objfile
9450 = get_dwarf2_per_objfile (objfile);
9451
9452 if (self->readin)
9453 {
9454 warning (_("bug: psymtab for %s is already read in."),
9455 self->filename);
9456 }
9457 else
9458 {
9459 if (info_verbose)
9460 {
9461 printf_filtered (_("Reading in symbols for %s..."),
9462 self->filename);
9463 gdb_flush (gdb_stdout);
9464 }
9465
9466 /* If this psymtab is constructed from a debug-only objfile, the
9467 has_section_at_zero flag will not necessarily be correct. We
9468 can get the correct value for this flag by looking at the data
9469 associated with the (presumably stripped) associated objfile. */
9470 if (objfile->separate_debug_objfile_backlink)
9471 {
9472 struct dwarf2_per_objfile *dpo_backlink
9473 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9474
9475 dwarf2_per_objfile->has_section_at_zero
9476 = dpo_backlink->has_section_at_zero;
9477 }
9478
9479 dwarf2_per_objfile->reading_partial_symbols = 0;
9480
9481 psymtab_to_symtab_1 (self);
9482
9483 /* Finish up the debug error message. */
9484 if (info_verbose)
9485 printf_filtered (_("done.\n"));
9486 }
9487
9488 process_cu_includes (dwarf2_per_objfile);
9489 }
9490 \f
9491 /* Reading in full CUs. */
9492
9493 /* Add PER_CU to the queue. */
9494
9495 static void
9496 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9497 enum language pretend_language)
9498 {
9499 struct dwarf2_queue_item *item;
9500
9501 per_cu->queued = 1;
9502 item = XNEW (struct dwarf2_queue_item);
9503 item->per_cu = per_cu;
9504 item->pretend_language = pretend_language;
9505 item->next = NULL;
9506
9507 if (dwarf2_queue == NULL)
9508 dwarf2_queue = item;
9509 else
9510 dwarf2_queue_tail->next = item;
9511
9512 dwarf2_queue_tail = item;
9513 }
9514
9515 /* If PER_CU is not yet queued, add it to the queue.
9516 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9517 dependency.
9518 The result is non-zero if PER_CU was queued, otherwise the result is zero
9519 meaning either PER_CU is already queued or it is already loaded.
9520
9521 N.B. There is an invariant here that if a CU is queued then it is loaded.
9522 The caller is required to load PER_CU if we return non-zero. */
9523
9524 static int
9525 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9526 struct dwarf2_per_cu_data *per_cu,
9527 enum language pretend_language)
9528 {
9529 /* We may arrive here during partial symbol reading, if we need full
9530 DIEs to process an unusual case (e.g. template arguments). Do
9531 not queue PER_CU, just tell our caller to load its DIEs. */
9532 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9533 {
9534 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9535 return 1;
9536 return 0;
9537 }
9538
9539 /* Mark the dependence relation so that we don't flush PER_CU
9540 too early. */
9541 if (dependent_cu != NULL)
9542 dwarf2_add_dependence (dependent_cu, per_cu);
9543
9544 /* If it's already on the queue, we have nothing to do. */
9545 if (per_cu->queued)
9546 return 0;
9547
9548 /* If the compilation unit is already loaded, just mark it as
9549 used. */
9550 if (per_cu->cu != NULL)
9551 {
9552 per_cu->cu->last_used = 0;
9553 return 0;
9554 }
9555
9556 /* Add it to the queue. */
9557 queue_comp_unit (per_cu, pretend_language);
9558
9559 return 1;
9560 }
9561
9562 /* Process the queue. */
9563
9564 static void
9565 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9566 {
9567 struct dwarf2_queue_item *item, *next_item;
9568
9569 if (dwarf_read_debug)
9570 {
9571 fprintf_unfiltered (gdb_stdlog,
9572 "Expanding one or more symtabs of objfile %s ...\n",
9573 objfile_name (dwarf2_per_objfile->objfile));
9574 }
9575
9576 /* The queue starts out with one item, but following a DIE reference
9577 may load a new CU, adding it to the end of the queue. */
9578 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9579 {
9580 if ((dwarf2_per_objfile->using_index
9581 ? !item->per_cu->v.quick->compunit_symtab
9582 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9583 /* Skip dummy CUs. */
9584 && item->per_cu->cu != NULL)
9585 {
9586 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9587 unsigned int debug_print_threshold;
9588 char buf[100];
9589
9590 if (per_cu->is_debug_types)
9591 {
9592 struct signatured_type *sig_type =
9593 (struct signatured_type *) per_cu;
9594
9595 sprintf (buf, "TU %s at offset %s",
9596 hex_string (sig_type->signature),
9597 sect_offset_str (per_cu->sect_off));
9598 /* There can be 100s of TUs.
9599 Only print them in verbose mode. */
9600 debug_print_threshold = 2;
9601 }
9602 else
9603 {
9604 sprintf (buf, "CU at offset %s",
9605 sect_offset_str (per_cu->sect_off));
9606 debug_print_threshold = 1;
9607 }
9608
9609 if (dwarf_read_debug >= debug_print_threshold)
9610 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9611
9612 if (per_cu->is_debug_types)
9613 process_full_type_unit (per_cu, item->pretend_language);
9614 else
9615 process_full_comp_unit (per_cu, item->pretend_language);
9616
9617 if (dwarf_read_debug >= debug_print_threshold)
9618 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9619 }
9620
9621 item->per_cu->queued = 0;
9622 next_item = item->next;
9623 xfree (item);
9624 }
9625
9626 dwarf2_queue_tail = NULL;
9627
9628 if (dwarf_read_debug)
9629 {
9630 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9631 objfile_name (dwarf2_per_objfile->objfile));
9632 }
9633 }
9634
9635 /* Read in full symbols for PST, and anything it depends on. */
9636
9637 static void
9638 psymtab_to_symtab_1 (struct partial_symtab *pst)
9639 {
9640 struct dwarf2_per_cu_data *per_cu;
9641 int i;
9642
9643 if (pst->readin)
9644 return;
9645
9646 for (i = 0; i < pst->number_of_dependencies; i++)
9647 if (!pst->dependencies[i]->readin
9648 && pst->dependencies[i]->user == NULL)
9649 {
9650 /* Inform about additional files that need to be read in. */
9651 if (info_verbose)
9652 {
9653 /* FIXME: i18n: Need to make this a single string. */
9654 fputs_filtered (" ", gdb_stdout);
9655 wrap_here ("");
9656 fputs_filtered ("and ", gdb_stdout);
9657 wrap_here ("");
9658 printf_filtered ("%s...", pst->dependencies[i]->filename);
9659 wrap_here (""); /* Flush output. */
9660 gdb_flush (gdb_stdout);
9661 }
9662 psymtab_to_symtab_1 (pst->dependencies[i]);
9663 }
9664
9665 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
9666
9667 if (per_cu == NULL)
9668 {
9669 /* It's an include file, no symbols to read for it.
9670 Everything is in the parent symtab. */
9671 pst->readin = 1;
9672 return;
9673 }
9674
9675 dw2_do_instantiate_symtab (per_cu, false);
9676 }
9677
9678 /* Trivial hash function for die_info: the hash value of a DIE
9679 is its offset in .debug_info for this objfile. */
9680
9681 static hashval_t
9682 die_hash (const void *item)
9683 {
9684 const struct die_info *die = (const struct die_info *) item;
9685
9686 return to_underlying (die->sect_off);
9687 }
9688
9689 /* Trivial comparison function for die_info structures: two DIEs
9690 are equal if they have the same offset. */
9691
9692 static int
9693 die_eq (const void *item_lhs, const void *item_rhs)
9694 {
9695 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9696 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9697
9698 return die_lhs->sect_off == die_rhs->sect_off;
9699 }
9700
9701 /* die_reader_func for load_full_comp_unit.
9702 This is identical to read_signatured_type_reader,
9703 but is kept separate for now. */
9704
9705 static void
9706 load_full_comp_unit_reader (const struct die_reader_specs *reader,
9707 const gdb_byte *info_ptr,
9708 struct die_info *comp_unit_die,
9709 int has_children,
9710 void *data)
9711 {
9712 struct dwarf2_cu *cu = reader->cu;
9713 enum language *language_ptr = (enum language *) data;
9714
9715 gdb_assert (cu->die_hash == NULL);
9716 cu->die_hash =
9717 htab_create_alloc_ex (cu->header.length / 12,
9718 die_hash,
9719 die_eq,
9720 NULL,
9721 &cu->comp_unit_obstack,
9722 hashtab_obstack_allocate,
9723 dummy_obstack_deallocate);
9724
9725 if (has_children)
9726 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9727 &info_ptr, comp_unit_die);
9728 cu->dies = comp_unit_die;
9729 /* comp_unit_die is not stored in die_hash, no need. */
9730
9731 /* We try not to read any attributes in this function, because not
9732 all CUs needed for references have been loaded yet, and symbol
9733 table processing isn't initialized. But we have to set the CU language,
9734 or we won't be able to build types correctly.
9735 Similarly, if we do not read the producer, we can not apply
9736 producer-specific interpretation. */
9737 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
9738 }
9739
9740 /* Load the DIEs associated with PER_CU into memory. */
9741
9742 static void
9743 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9744 bool skip_partial,
9745 enum language pretend_language)
9746 {
9747 gdb_assert (! this_cu->is_debug_types);
9748
9749 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
9750 load_full_comp_unit_reader, &pretend_language);
9751 }
9752
9753 /* Add a DIE to the delayed physname list. */
9754
9755 static void
9756 add_to_method_list (struct type *type, int fnfield_index, int index,
9757 const char *name, struct die_info *die,
9758 struct dwarf2_cu *cu)
9759 {
9760 struct delayed_method_info mi;
9761 mi.type = type;
9762 mi.fnfield_index = fnfield_index;
9763 mi.index = index;
9764 mi.name = name;
9765 mi.die = die;
9766 cu->method_list.push_back (mi);
9767 }
9768
9769 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9770 "const" / "volatile". If so, decrements LEN by the length of the
9771 modifier and return true. Otherwise return false. */
9772
9773 template<size_t N>
9774 static bool
9775 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9776 {
9777 size_t mod_len = sizeof (mod) - 1;
9778 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9779 {
9780 len -= mod_len;
9781 return true;
9782 }
9783 return false;
9784 }
9785
9786 /* Compute the physnames of any methods on the CU's method list.
9787
9788 The computation of method physnames is delayed in order to avoid the
9789 (bad) condition that one of the method's formal parameters is of an as yet
9790 incomplete type. */
9791
9792 static void
9793 compute_delayed_physnames (struct dwarf2_cu *cu)
9794 {
9795 /* Only C++ delays computing physnames. */
9796 if (cu->method_list.empty ())
9797 return;
9798 gdb_assert (cu->language == language_cplus);
9799
9800 for (const delayed_method_info &mi : cu->method_list)
9801 {
9802 const char *physname;
9803 struct fn_fieldlist *fn_flp
9804 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9805 physname = dwarf2_physname (mi.name, mi.die, cu);
9806 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9807 = physname ? physname : "";
9808
9809 /* Since there's no tag to indicate whether a method is a
9810 const/volatile overload, extract that information out of the
9811 demangled name. */
9812 if (physname != NULL)
9813 {
9814 size_t len = strlen (physname);
9815
9816 while (1)
9817 {
9818 if (physname[len] == ')') /* shortcut */
9819 break;
9820 else if (check_modifier (physname, len, " const"))
9821 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9822 else if (check_modifier (physname, len, " volatile"))
9823 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9824 else
9825 break;
9826 }
9827 }
9828 }
9829
9830 /* The list is no longer needed. */
9831 cu->method_list.clear ();
9832 }
9833
9834 /* Go objects should be embedded in a DW_TAG_module DIE,
9835 and it's not clear if/how imported objects will appear.
9836 To keep Go support simple until that's worked out,
9837 go back through what we've read and create something usable.
9838 We could do this while processing each DIE, and feels kinda cleaner,
9839 but that way is more invasive.
9840 This is to, for example, allow the user to type "p var" or "b main"
9841 without having to specify the package name, and allow lookups
9842 of module.object to work in contexts that use the expression
9843 parser. */
9844
9845 static void
9846 fixup_go_packaging (struct dwarf2_cu *cu)
9847 {
9848 char *package_name = NULL;
9849 struct pending *list;
9850 int i;
9851
9852 for (list = *cu->get_builder ()->get_global_symbols ();
9853 list != NULL;
9854 list = list->next)
9855 {
9856 for (i = 0; i < list->nsyms; ++i)
9857 {
9858 struct symbol *sym = list->symbol[i];
9859
9860 if (SYMBOL_LANGUAGE (sym) == language_go
9861 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9862 {
9863 char *this_package_name = go_symbol_package_name (sym);
9864
9865 if (this_package_name == NULL)
9866 continue;
9867 if (package_name == NULL)
9868 package_name = this_package_name;
9869 else
9870 {
9871 struct objfile *objfile
9872 = cu->per_cu->dwarf2_per_objfile->objfile;
9873 if (strcmp (package_name, this_package_name) != 0)
9874 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9875 (symbol_symtab (sym) != NULL
9876 ? symtab_to_filename_for_display
9877 (symbol_symtab (sym))
9878 : objfile_name (objfile)),
9879 this_package_name, package_name);
9880 xfree (this_package_name);
9881 }
9882 }
9883 }
9884 }
9885
9886 if (package_name != NULL)
9887 {
9888 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9889 const char *saved_package_name
9890 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name);
9891 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9892 saved_package_name);
9893 struct symbol *sym;
9894
9895 sym = allocate_symbol (objfile);
9896 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
9897 SYMBOL_SET_NAMES (sym, saved_package_name,
9898 strlen (saved_package_name), 0, objfile);
9899 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9900 e.g., "main" finds the "main" module and not C's main(). */
9901 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9902 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9903 SYMBOL_TYPE (sym) = type;
9904
9905 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9906
9907 xfree (package_name);
9908 }
9909 }
9910
9911 /* Allocate a fully-qualified name consisting of the two parts on the
9912 obstack. */
9913
9914 static const char *
9915 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9916 {
9917 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9918 }
9919
9920 /* A helper that allocates a struct discriminant_info to attach to a
9921 union type. */
9922
9923 static struct discriminant_info *
9924 alloc_discriminant_info (struct type *type, int discriminant_index,
9925 int default_index)
9926 {
9927 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9928 gdb_assert (discriminant_index == -1
9929 || (discriminant_index >= 0
9930 && discriminant_index < TYPE_NFIELDS (type)));
9931 gdb_assert (default_index == -1
9932 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9933
9934 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9935
9936 struct discriminant_info *disc
9937 = ((struct discriminant_info *)
9938 TYPE_ZALLOC (type,
9939 offsetof (struct discriminant_info, discriminants)
9940 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9941 disc->default_index = default_index;
9942 disc->discriminant_index = discriminant_index;
9943
9944 struct dynamic_prop prop;
9945 prop.kind = PROP_UNDEFINED;
9946 prop.data.baton = disc;
9947
9948 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9949
9950 return disc;
9951 }
9952
9953 /* Some versions of rustc emitted enums in an unusual way.
9954
9955 Ordinary enums were emitted as unions. The first element of each
9956 structure in the union was named "RUST$ENUM$DISR". This element
9957 held the discriminant.
9958
9959 These versions of Rust also implemented the "non-zero"
9960 optimization. When the enum had two values, and one is empty and
9961 the other holds a pointer that cannot be zero, the pointer is used
9962 as the discriminant, with a zero value meaning the empty variant.
9963 Here, the union's first member is of the form
9964 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9965 where the fieldnos are the indices of the fields that should be
9966 traversed in order to find the field (which may be several fields deep)
9967 and the variantname is the name of the variant of the case when the
9968 field is zero.
9969
9970 This function recognizes whether TYPE is of one of these forms,
9971 and, if so, smashes it to be a variant type. */
9972
9973 static void
9974 quirk_rust_enum (struct type *type, struct objfile *objfile)
9975 {
9976 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9977
9978 /* We don't need to deal with empty enums. */
9979 if (TYPE_NFIELDS (type) == 0)
9980 return;
9981
9982 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9983 if (TYPE_NFIELDS (type) == 1
9984 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9985 {
9986 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9987
9988 /* Decode the field name to find the offset of the
9989 discriminant. */
9990 ULONGEST bit_offset = 0;
9991 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9992 while (name[0] >= '0' && name[0] <= '9')
9993 {
9994 char *tail;
9995 unsigned long index = strtoul (name, &tail, 10);
9996 name = tail;
9997 if (*name != '$'
9998 || index >= TYPE_NFIELDS (field_type)
9999 || (TYPE_FIELD_LOC_KIND (field_type, index)
10000 != FIELD_LOC_KIND_BITPOS))
10001 {
10002 complaint (_("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 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10022
10023 /* Put the discriminant must at index 0. */
10024 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10025 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10026 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10027 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10028
10029 /* The order of fields doesn't really matter, so put the real
10030 field at index 1 and the data-less field at index 2. */
10031 struct discriminant_info *disc
10032 = alloc_discriminant_info (union_type, 0, 1);
10033 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10034 TYPE_FIELD_NAME (union_type, 1)
10035 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10036 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10037 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10038 TYPE_FIELD_NAME (union_type, 1));
10039
10040 const char *dataless_name
10041 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10042 name);
10043 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10044 dataless_name);
10045 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10046 /* NAME points into the original discriminant name, which
10047 already has the correct lifetime. */
10048 TYPE_FIELD_NAME (union_type, 2) = name;
10049 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10050 disc->discriminants[2] = 0;
10051
10052 /* Smash this type to be a structure type. We have to do this
10053 because the type has already been recorded. */
10054 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10055 TYPE_NFIELDS (type) = 1;
10056 TYPE_FIELDS (type)
10057 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10058
10059 /* Install the variant part. */
10060 TYPE_FIELD_TYPE (type, 0) = union_type;
10061 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10062 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10063 }
10064 /* A union with a single anonymous field is probably an old-style
10065 univariant enum. */
10066 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
10067 {
10068 /* Smash this type to be a structure type. We have to do this
10069 because the type has already been recorded. */
10070 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10071
10072 /* Make a union to hold the variants. */
10073 struct type *union_type = alloc_type (objfile);
10074 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10075 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10076 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10077 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10078 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10079
10080 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10081 const char *variant_name
10082 = rust_last_path_segment (TYPE_NAME (field_type));
10083 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10084 TYPE_NAME (field_type)
10085 = rust_fully_qualify (&objfile->objfile_obstack,
10086 TYPE_NAME (type), variant_name);
10087
10088 /* Install the union in the outer struct type. */
10089 TYPE_NFIELDS (type) = 1;
10090 TYPE_FIELDS (type)
10091 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10092 TYPE_FIELD_TYPE (type, 0) = union_type;
10093 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10094 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10095
10096 alloc_discriminant_info (union_type, -1, 0);
10097 }
10098 else
10099 {
10100 struct type *disr_type = nullptr;
10101 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10102 {
10103 disr_type = TYPE_FIELD_TYPE (type, i);
10104
10105 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10106 {
10107 /* All fields of a true enum will be structs. */
10108 return;
10109 }
10110 else if (TYPE_NFIELDS (disr_type) == 0)
10111 {
10112 /* Could be data-less variant, so keep going. */
10113 disr_type = nullptr;
10114 }
10115 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10116 "RUST$ENUM$DISR") != 0)
10117 {
10118 /* Not a Rust enum. */
10119 return;
10120 }
10121 else
10122 {
10123 /* Found one. */
10124 break;
10125 }
10126 }
10127
10128 /* If we got here without a discriminant, then it's probably
10129 just a union. */
10130 if (disr_type == nullptr)
10131 return;
10132
10133 /* Smash this type to be a structure type. We have to do this
10134 because the type has already been recorded. */
10135 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10136
10137 /* Make a union to hold the variants. */
10138 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10139 struct type *union_type = alloc_type (objfile);
10140 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10141 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10142 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10143 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10144 TYPE_FIELDS (union_type)
10145 = (struct field *) TYPE_ZALLOC (union_type,
10146 (TYPE_NFIELDS (union_type)
10147 * sizeof (struct field)));
10148
10149 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10150 TYPE_NFIELDS (type) * sizeof (struct field));
10151
10152 /* Install the discriminant at index 0 in the union. */
10153 TYPE_FIELD (union_type, 0) = *disr_field;
10154 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10155 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10156
10157 /* Install the union in the outer struct type. */
10158 TYPE_FIELD_TYPE (type, 0) = union_type;
10159 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10160 TYPE_NFIELDS (type) = 1;
10161
10162 /* Set the size and offset of the union type. */
10163 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10164
10165 /* We need a way to find the correct discriminant given a
10166 variant name. For convenience we build a map here. */
10167 struct type *enum_type = FIELD_TYPE (*disr_field);
10168 std::unordered_map<std::string, ULONGEST> discriminant_map;
10169 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10170 {
10171 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10172 {
10173 const char *name
10174 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10175 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10176 }
10177 }
10178
10179 int n_fields = TYPE_NFIELDS (union_type);
10180 struct discriminant_info *disc
10181 = alloc_discriminant_info (union_type, 0, -1);
10182 /* Skip the discriminant here. */
10183 for (int i = 1; i < n_fields; ++i)
10184 {
10185 /* Find the final word in the name of this variant's type.
10186 That name can be used to look up the correct
10187 discriminant. */
10188 const char *variant_name
10189 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10190 i)));
10191
10192 auto iter = discriminant_map.find (variant_name);
10193 if (iter != discriminant_map.end ())
10194 disc->discriminants[i] = iter->second;
10195
10196 /* Remove the discriminant field, if it exists. */
10197 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10198 if (TYPE_NFIELDS (sub_type) > 0)
10199 {
10200 --TYPE_NFIELDS (sub_type);
10201 ++TYPE_FIELDS (sub_type);
10202 }
10203 TYPE_FIELD_NAME (union_type, i) = variant_name;
10204 TYPE_NAME (sub_type)
10205 = rust_fully_qualify (&objfile->objfile_obstack,
10206 TYPE_NAME (type), variant_name);
10207 }
10208 }
10209 }
10210
10211 /* Rewrite some Rust unions to be structures with variants parts. */
10212
10213 static void
10214 rust_union_quirks (struct dwarf2_cu *cu)
10215 {
10216 gdb_assert (cu->language == language_rust);
10217 for (type *type_ : cu->rust_unions)
10218 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
10219 /* We don't need this any more. */
10220 cu->rust_unions.clear ();
10221 }
10222
10223 /* Return the symtab for PER_CU. This works properly regardless of
10224 whether we're using the index or psymtabs. */
10225
10226 static struct compunit_symtab *
10227 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10228 {
10229 return (per_cu->dwarf2_per_objfile->using_index
10230 ? per_cu->v.quick->compunit_symtab
10231 : per_cu->v.psymtab->compunit_symtab);
10232 }
10233
10234 /* A helper function for computing the list of all symbol tables
10235 included by PER_CU. */
10236
10237 static void
10238 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
10239 htab_t all_children, htab_t all_type_symtabs,
10240 struct dwarf2_per_cu_data *per_cu,
10241 struct compunit_symtab *immediate_parent)
10242 {
10243 void **slot;
10244 struct compunit_symtab *cust;
10245
10246 slot = htab_find_slot (all_children, per_cu, INSERT);
10247 if (*slot != NULL)
10248 {
10249 /* This inclusion and its children have been processed. */
10250 return;
10251 }
10252
10253 *slot = per_cu;
10254 /* Only add a CU if it has a symbol table. */
10255 cust = get_compunit_symtab (per_cu);
10256 if (cust != NULL)
10257 {
10258 /* If this is a type unit only add its symbol table if we haven't
10259 seen it yet (type unit per_cu's can share symtabs). */
10260 if (per_cu->is_debug_types)
10261 {
10262 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10263 if (*slot == NULL)
10264 {
10265 *slot = cust;
10266 result->push_back (cust);
10267 if (cust->user == NULL)
10268 cust->user = immediate_parent;
10269 }
10270 }
10271 else
10272 {
10273 result->push_back (cust);
10274 if (cust->user == NULL)
10275 cust->user = immediate_parent;
10276 }
10277 }
10278
10279 if (!per_cu->imported_symtabs_empty ())
10280 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10281 {
10282 recursively_compute_inclusions (result, all_children,
10283 all_type_symtabs, ptr, cust);
10284 }
10285 }
10286
10287 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10288 PER_CU. */
10289
10290 static void
10291 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10292 {
10293 gdb_assert (! per_cu->is_debug_types);
10294
10295 if (!per_cu->imported_symtabs_empty ())
10296 {
10297 int len;
10298 std::vector<compunit_symtab *> result_symtabs;
10299 htab_t all_children, all_type_symtabs;
10300 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10301
10302 /* If we don't have a symtab, we can just skip this case. */
10303 if (cust == NULL)
10304 return;
10305
10306 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10307 NULL, xcalloc, xfree);
10308 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10309 NULL, xcalloc, xfree);
10310
10311 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10312 {
10313 recursively_compute_inclusions (&result_symtabs, all_children,
10314 all_type_symtabs, ptr, cust);
10315 }
10316
10317 /* Now we have a transitive closure of all the included symtabs. */
10318 len = result_symtabs.size ();
10319 cust->includes
10320 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10321 struct compunit_symtab *, len + 1);
10322 memcpy (cust->includes, result_symtabs.data (),
10323 len * sizeof (compunit_symtab *));
10324 cust->includes[len] = NULL;
10325
10326 htab_delete (all_children);
10327 htab_delete (all_type_symtabs);
10328 }
10329 }
10330
10331 /* Compute the 'includes' field for the symtabs of all the CUs we just
10332 read. */
10333
10334 static void
10335 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10336 {
10337 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
10338 {
10339 if (! iter->is_debug_types)
10340 compute_compunit_symtab_includes (iter);
10341 }
10342
10343 dwarf2_per_objfile->just_read_cus.clear ();
10344 }
10345
10346 /* Generate full symbol information for PER_CU, whose DIEs have
10347 already been loaded into memory. */
10348
10349 static void
10350 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10351 enum language pretend_language)
10352 {
10353 struct dwarf2_cu *cu = per_cu->cu;
10354 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10355 struct objfile *objfile = dwarf2_per_objfile->objfile;
10356 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10357 CORE_ADDR lowpc, highpc;
10358 struct compunit_symtab *cust;
10359 CORE_ADDR baseaddr;
10360 struct block *static_block;
10361 CORE_ADDR addr;
10362
10363 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10364
10365 /* Clear the list here in case something was left over. */
10366 cu->method_list.clear ();
10367
10368 cu->language = pretend_language;
10369 cu->language_defn = language_def (cu->language);
10370
10371 /* Do line number decoding in read_file_scope () */
10372 process_die (cu->dies, cu);
10373
10374 /* For now fudge the Go package. */
10375 if (cu->language == language_go)
10376 fixup_go_packaging (cu);
10377
10378 /* Now that we have processed all the DIEs in the CU, all the types
10379 should be complete, and it should now be safe to compute all of the
10380 physnames. */
10381 compute_delayed_physnames (cu);
10382
10383 if (cu->language == language_rust)
10384 rust_union_quirks (cu);
10385
10386 /* Some compilers don't define a DW_AT_high_pc attribute for the
10387 compilation unit. If the DW_AT_high_pc is missing, synthesize
10388 it, by scanning the DIE's below the compilation unit. */
10389 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10390
10391 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10392 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10393
10394 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10395 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10396 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10397 addrmap to help ensure it has an accurate map of pc values belonging to
10398 this comp unit. */
10399 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10400
10401 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10402 SECT_OFF_TEXT (objfile),
10403 0);
10404
10405 if (cust != NULL)
10406 {
10407 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10408
10409 /* Set symtab language to language from DW_AT_language. If the
10410 compilation is from a C file generated by language preprocessors, do
10411 not set the language if it was already deduced by start_subfile. */
10412 if (!(cu->language == language_c
10413 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10414 COMPUNIT_FILETABS (cust)->language = cu->language;
10415
10416 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10417 produce DW_AT_location with location lists but it can be possibly
10418 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10419 there were bugs in prologue debug info, fixed later in GCC-4.5
10420 by "unwind info for epilogues" patch (which is not directly related).
10421
10422 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10423 needed, it would be wrong due to missing DW_AT_producer there.
10424
10425 Still one can confuse GDB by using non-standard GCC compilation
10426 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10427 */
10428 if (cu->has_loclist && gcc_4_minor >= 5)
10429 cust->locations_valid = 1;
10430
10431 if (gcc_4_minor >= 5)
10432 cust->epilogue_unwind_valid = 1;
10433
10434 cust->call_site_htab = cu->call_site_htab;
10435 }
10436
10437 if (dwarf2_per_objfile->using_index)
10438 per_cu->v.quick->compunit_symtab = cust;
10439 else
10440 {
10441 struct partial_symtab *pst = per_cu->v.psymtab;
10442 pst->compunit_symtab = cust;
10443 pst->readin = 1;
10444 }
10445
10446 /* Push it for inclusion processing later. */
10447 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
10448
10449 /* Not needed any more. */
10450 cu->reset_builder ();
10451 }
10452
10453 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10454 already been loaded into memory. */
10455
10456 static void
10457 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10458 enum language pretend_language)
10459 {
10460 struct dwarf2_cu *cu = per_cu->cu;
10461 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10462 struct objfile *objfile = dwarf2_per_objfile->objfile;
10463 struct compunit_symtab *cust;
10464 struct signatured_type *sig_type;
10465
10466 gdb_assert (per_cu->is_debug_types);
10467 sig_type = (struct signatured_type *) per_cu;
10468
10469 /* Clear the list here in case something was left over. */
10470 cu->method_list.clear ();
10471
10472 cu->language = pretend_language;
10473 cu->language_defn = language_def (cu->language);
10474
10475 /* The symbol tables are set up in read_type_unit_scope. */
10476 process_die (cu->dies, cu);
10477
10478 /* For now fudge the Go package. */
10479 if (cu->language == language_go)
10480 fixup_go_packaging (cu);
10481
10482 /* Now that we have processed all the DIEs in the CU, all the types
10483 should be complete, and it should now be safe to compute all of the
10484 physnames. */
10485 compute_delayed_physnames (cu);
10486
10487 if (cu->language == language_rust)
10488 rust_union_quirks (cu);
10489
10490 /* TUs share symbol tables.
10491 If this is the first TU to use this symtab, complete the construction
10492 of it with end_expandable_symtab. Otherwise, complete the addition of
10493 this TU's symbols to the existing symtab. */
10494 if (sig_type->type_unit_group->compunit_symtab == NULL)
10495 {
10496 buildsym_compunit *builder = cu->get_builder ();
10497 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10498 sig_type->type_unit_group->compunit_symtab = cust;
10499
10500 if (cust != NULL)
10501 {
10502 /* Set symtab language to language from DW_AT_language. If the
10503 compilation is from a C file generated by language preprocessors,
10504 do not set the language if it was already deduced by
10505 start_subfile. */
10506 if (!(cu->language == language_c
10507 && COMPUNIT_FILETABS (cust)->language != language_c))
10508 COMPUNIT_FILETABS (cust)->language = cu->language;
10509 }
10510 }
10511 else
10512 {
10513 cu->get_builder ()->augment_type_symtab ();
10514 cust = sig_type->type_unit_group->compunit_symtab;
10515 }
10516
10517 if (dwarf2_per_objfile->using_index)
10518 per_cu->v.quick->compunit_symtab = cust;
10519 else
10520 {
10521 struct partial_symtab *pst = per_cu->v.psymtab;
10522 pst->compunit_symtab = cust;
10523 pst->readin = 1;
10524 }
10525
10526 /* Not needed any more. */
10527 cu->reset_builder ();
10528 }
10529
10530 /* Process an imported unit DIE. */
10531
10532 static void
10533 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10534 {
10535 struct attribute *attr;
10536
10537 /* For now we don't handle imported units in type units. */
10538 if (cu->per_cu->is_debug_types)
10539 {
10540 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10541 " supported in type units [in module %s]"),
10542 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10543 }
10544
10545 attr = dwarf2_attr (die, DW_AT_import, cu);
10546 if (attr != NULL)
10547 {
10548 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10549 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10550 dwarf2_per_cu_data *per_cu
10551 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10552 cu->per_cu->dwarf2_per_objfile);
10553
10554 /* If necessary, add it to the queue and load its DIEs. */
10555 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10556 load_full_comp_unit (per_cu, false, cu->language);
10557
10558 cu->per_cu->imported_symtabs_push (per_cu);
10559 }
10560 }
10561
10562 /* RAII object that represents a process_die scope: i.e.,
10563 starts/finishes processing a DIE. */
10564 class process_die_scope
10565 {
10566 public:
10567 process_die_scope (die_info *die, dwarf2_cu *cu)
10568 : m_die (die), m_cu (cu)
10569 {
10570 /* We should only be processing DIEs not already in process. */
10571 gdb_assert (!m_die->in_process);
10572 m_die->in_process = true;
10573 }
10574
10575 ~process_die_scope ()
10576 {
10577 m_die->in_process = false;
10578
10579 /* If we're done processing the DIE for the CU that owns the line
10580 header, we don't need the line header anymore. */
10581 if (m_cu->line_header_die_owner == m_die)
10582 {
10583 delete m_cu->line_header;
10584 m_cu->line_header = NULL;
10585 m_cu->line_header_die_owner = NULL;
10586 }
10587 }
10588
10589 private:
10590 die_info *m_die;
10591 dwarf2_cu *m_cu;
10592 };
10593
10594 /* Process a die and its children. */
10595
10596 static void
10597 process_die (struct die_info *die, struct dwarf2_cu *cu)
10598 {
10599 process_die_scope scope (die, cu);
10600
10601 switch (die->tag)
10602 {
10603 case DW_TAG_padding:
10604 break;
10605 case DW_TAG_compile_unit:
10606 case DW_TAG_partial_unit:
10607 read_file_scope (die, cu);
10608 break;
10609 case DW_TAG_type_unit:
10610 read_type_unit_scope (die, cu);
10611 break;
10612 case DW_TAG_subprogram:
10613 /* Nested subprograms in Fortran get a prefix. */
10614 if (cu->language == language_fortran
10615 && die->parent != NULL
10616 && die->parent->tag == DW_TAG_subprogram)
10617 cu->processing_has_namespace_info = true;
10618 /* Fall through. */
10619 case DW_TAG_inlined_subroutine:
10620 read_func_scope (die, cu);
10621 break;
10622 case DW_TAG_lexical_block:
10623 case DW_TAG_try_block:
10624 case DW_TAG_catch_block:
10625 read_lexical_block_scope (die, cu);
10626 break;
10627 case DW_TAG_call_site:
10628 case DW_TAG_GNU_call_site:
10629 read_call_site_scope (die, cu);
10630 break;
10631 case DW_TAG_class_type:
10632 case DW_TAG_interface_type:
10633 case DW_TAG_structure_type:
10634 case DW_TAG_union_type:
10635 process_structure_scope (die, cu);
10636 break;
10637 case DW_TAG_enumeration_type:
10638 process_enumeration_scope (die, cu);
10639 break;
10640
10641 /* These dies have a type, but processing them does not create
10642 a symbol or recurse to process the children. Therefore we can
10643 read them on-demand through read_type_die. */
10644 case DW_TAG_subroutine_type:
10645 case DW_TAG_set_type:
10646 case DW_TAG_array_type:
10647 case DW_TAG_pointer_type:
10648 case DW_TAG_ptr_to_member_type:
10649 case DW_TAG_reference_type:
10650 case DW_TAG_rvalue_reference_type:
10651 case DW_TAG_string_type:
10652 break;
10653
10654 case DW_TAG_base_type:
10655 case DW_TAG_subrange_type:
10656 case DW_TAG_typedef:
10657 /* Add a typedef symbol for the type definition, if it has a
10658 DW_AT_name. */
10659 new_symbol (die, read_type_die (die, cu), cu);
10660 break;
10661 case DW_TAG_common_block:
10662 read_common_block (die, cu);
10663 break;
10664 case DW_TAG_common_inclusion:
10665 break;
10666 case DW_TAG_namespace:
10667 cu->processing_has_namespace_info = true;
10668 read_namespace (die, cu);
10669 break;
10670 case DW_TAG_module:
10671 cu->processing_has_namespace_info = true;
10672 read_module (die, cu);
10673 break;
10674 case DW_TAG_imported_declaration:
10675 cu->processing_has_namespace_info = true;
10676 if (read_namespace_alias (die, cu))
10677 break;
10678 /* The declaration is not a global namespace alias. */
10679 /* Fall through. */
10680 case DW_TAG_imported_module:
10681 cu->processing_has_namespace_info = true;
10682 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10683 || cu->language != language_fortran))
10684 complaint (_("Tag '%s' has unexpected children"),
10685 dwarf_tag_name (die->tag));
10686 read_import_statement (die, cu);
10687 break;
10688
10689 case DW_TAG_imported_unit:
10690 process_imported_unit_die (die, cu);
10691 break;
10692
10693 case DW_TAG_variable:
10694 read_variable (die, cu);
10695 break;
10696
10697 default:
10698 new_symbol (die, NULL, cu);
10699 break;
10700 }
10701 }
10702 \f
10703 /* DWARF name computation. */
10704
10705 /* A helper function for dwarf2_compute_name which determines whether DIE
10706 needs to have the name of the scope prepended to the name listed in the
10707 die. */
10708
10709 static int
10710 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10711 {
10712 struct attribute *attr;
10713
10714 switch (die->tag)
10715 {
10716 case DW_TAG_namespace:
10717 case DW_TAG_typedef:
10718 case DW_TAG_class_type:
10719 case DW_TAG_interface_type:
10720 case DW_TAG_structure_type:
10721 case DW_TAG_union_type:
10722 case DW_TAG_enumeration_type:
10723 case DW_TAG_enumerator:
10724 case DW_TAG_subprogram:
10725 case DW_TAG_inlined_subroutine:
10726 case DW_TAG_member:
10727 case DW_TAG_imported_declaration:
10728 return 1;
10729
10730 case DW_TAG_variable:
10731 case DW_TAG_constant:
10732 /* We only need to prefix "globally" visible variables. These include
10733 any variable marked with DW_AT_external or any variable that
10734 lives in a namespace. [Variables in anonymous namespaces
10735 require prefixing, but they are not DW_AT_external.] */
10736
10737 if (dwarf2_attr (die, DW_AT_specification, cu))
10738 {
10739 struct dwarf2_cu *spec_cu = cu;
10740
10741 return die_needs_namespace (die_specification (die, &spec_cu),
10742 spec_cu);
10743 }
10744
10745 attr = dwarf2_attr (die, DW_AT_external, cu);
10746 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10747 && die->parent->tag != DW_TAG_module)
10748 return 0;
10749 /* A variable in a lexical block of some kind does not need a
10750 namespace, even though in C++ such variables may be external
10751 and have a mangled name. */
10752 if (die->parent->tag == DW_TAG_lexical_block
10753 || die->parent->tag == DW_TAG_try_block
10754 || die->parent->tag == DW_TAG_catch_block
10755 || die->parent->tag == DW_TAG_subprogram)
10756 return 0;
10757 return 1;
10758
10759 default:
10760 return 0;
10761 }
10762 }
10763
10764 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10765 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10766 defined for the given DIE. */
10767
10768 static struct attribute *
10769 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10770 {
10771 struct attribute *attr;
10772
10773 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10774 if (attr == NULL)
10775 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10776
10777 return attr;
10778 }
10779
10780 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10781 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10782 defined for the given DIE. */
10783
10784 static const char *
10785 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10786 {
10787 const char *linkage_name;
10788
10789 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10790 if (linkage_name == NULL)
10791 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10792
10793 return linkage_name;
10794 }
10795
10796 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10797 compute the physname for the object, which include a method's:
10798 - formal parameters (C++),
10799 - receiver type (Go),
10800
10801 The term "physname" is a bit confusing.
10802 For C++, for example, it is the demangled name.
10803 For Go, for example, it's the mangled name.
10804
10805 For Ada, return the DIE's linkage name rather than the fully qualified
10806 name. PHYSNAME is ignored..
10807
10808 The result is allocated on the objfile_obstack and canonicalized. */
10809
10810 static const char *
10811 dwarf2_compute_name (const char *name,
10812 struct die_info *die, struct dwarf2_cu *cu,
10813 int physname)
10814 {
10815 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10816
10817 if (name == NULL)
10818 name = dwarf2_name (die, cu);
10819
10820 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10821 but otherwise compute it by typename_concat inside GDB.
10822 FIXME: Actually this is not really true, or at least not always true.
10823 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10824 Fortran names because there is no mangling standard. So new_symbol
10825 will set the demangled name to the result of dwarf2_full_name, and it is
10826 the demangled name that GDB uses if it exists. */
10827 if (cu->language == language_ada
10828 || (cu->language == language_fortran && physname))
10829 {
10830 /* For Ada unit, we prefer the linkage name over the name, as
10831 the former contains the exported name, which the user expects
10832 to be able to reference. Ideally, we want the user to be able
10833 to reference this entity using either natural or linkage name,
10834 but we haven't started looking at this enhancement yet. */
10835 const char *linkage_name = dw2_linkage_name (die, cu);
10836
10837 if (linkage_name != NULL)
10838 return linkage_name;
10839 }
10840
10841 /* These are the only languages we know how to qualify names in. */
10842 if (name != NULL
10843 && (cu->language == language_cplus
10844 || cu->language == language_fortran || cu->language == language_d
10845 || cu->language == language_rust))
10846 {
10847 if (die_needs_namespace (die, cu))
10848 {
10849 const char *prefix;
10850 const char *canonical_name = NULL;
10851
10852 string_file buf;
10853
10854 prefix = determine_prefix (die, cu);
10855 if (*prefix != '\0')
10856 {
10857 char *prefixed_name = typename_concat (NULL, prefix, name,
10858 physname, cu);
10859
10860 buf.puts (prefixed_name);
10861 xfree (prefixed_name);
10862 }
10863 else
10864 buf.puts (name);
10865
10866 /* Template parameters may be specified in the DIE's DW_AT_name, or
10867 as children with DW_TAG_template_type_param or
10868 DW_TAG_value_type_param. If the latter, add them to the name
10869 here. If the name already has template parameters, then
10870 skip this step; some versions of GCC emit both, and
10871 it is more efficient to use the pre-computed name.
10872
10873 Something to keep in mind about this process: it is very
10874 unlikely, or in some cases downright impossible, to produce
10875 something that will match the mangled name of a function.
10876 If the definition of the function has the same debug info,
10877 we should be able to match up with it anyway. But fallbacks
10878 using the minimal symbol, for instance to find a method
10879 implemented in a stripped copy of libstdc++, will not work.
10880 If we do not have debug info for the definition, we will have to
10881 match them up some other way.
10882
10883 When we do name matching there is a related problem with function
10884 templates; two instantiated function templates are allowed to
10885 differ only by their return types, which we do not add here. */
10886
10887 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10888 {
10889 struct attribute *attr;
10890 struct die_info *child;
10891 int first = 1;
10892
10893 die->building_fullname = 1;
10894
10895 for (child = die->child; child != NULL; child = child->sibling)
10896 {
10897 struct type *type;
10898 LONGEST value;
10899 const gdb_byte *bytes;
10900 struct dwarf2_locexpr_baton *baton;
10901 struct value *v;
10902
10903 if (child->tag != DW_TAG_template_type_param
10904 && child->tag != DW_TAG_template_value_param)
10905 continue;
10906
10907 if (first)
10908 {
10909 buf.puts ("<");
10910 first = 0;
10911 }
10912 else
10913 buf.puts (", ");
10914
10915 attr = dwarf2_attr (child, DW_AT_type, cu);
10916 if (attr == NULL)
10917 {
10918 complaint (_("template parameter missing DW_AT_type"));
10919 buf.puts ("UNKNOWN_TYPE");
10920 continue;
10921 }
10922 type = die_type (child, cu);
10923
10924 if (child->tag == DW_TAG_template_type_param)
10925 {
10926 c_print_type (type, "", &buf, -1, 0, cu->language,
10927 &type_print_raw_options);
10928 continue;
10929 }
10930
10931 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10932 if (attr == NULL)
10933 {
10934 complaint (_("template parameter missing "
10935 "DW_AT_const_value"));
10936 buf.puts ("UNKNOWN_VALUE");
10937 continue;
10938 }
10939
10940 dwarf2_const_value_attr (attr, type, name,
10941 &cu->comp_unit_obstack, cu,
10942 &value, &bytes, &baton);
10943
10944 if (TYPE_NOSIGN (type))
10945 /* GDB prints characters as NUMBER 'CHAR'. If that's
10946 changed, this can use value_print instead. */
10947 c_printchar (value, type, &buf);
10948 else
10949 {
10950 struct value_print_options opts;
10951
10952 if (baton != NULL)
10953 v = dwarf2_evaluate_loc_desc (type, NULL,
10954 baton->data,
10955 baton->size,
10956 baton->per_cu);
10957 else if (bytes != NULL)
10958 {
10959 v = allocate_value (type);
10960 memcpy (value_contents_writeable (v), bytes,
10961 TYPE_LENGTH (type));
10962 }
10963 else
10964 v = value_from_longest (type, value);
10965
10966 /* Specify decimal so that we do not depend on
10967 the radix. */
10968 get_formatted_print_options (&opts, 'd');
10969 opts.raw = 1;
10970 value_print (v, &buf, &opts);
10971 release_value (v);
10972 }
10973 }
10974
10975 die->building_fullname = 0;
10976
10977 if (!first)
10978 {
10979 /* Close the argument list, with a space if necessary
10980 (nested templates). */
10981 if (!buf.empty () && buf.string ().back () == '>')
10982 buf.puts (" >");
10983 else
10984 buf.puts (">");
10985 }
10986 }
10987
10988 /* For C++ methods, append formal parameter type
10989 information, if PHYSNAME. */
10990
10991 if (physname && die->tag == DW_TAG_subprogram
10992 && cu->language == language_cplus)
10993 {
10994 struct type *type = read_type_die (die, cu);
10995
10996 c_type_print_args (type, &buf, 1, cu->language,
10997 &type_print_raw_options);
10998
10999 if (cu->language == language_cplus)
11000 {
11001 /* Assume that an artificial first parameter is
11002 "this", but do not crash if it is not. RealView
11003 marks unnamed (and thus unused) parameters as
11004 artificial; there is no way to differentiate
11005 the two cases. */
11006 if (TYPE_NFIELDS (type) > 0
11007 && TYPE_FIELD_ARTIFICIAL (type, 0)
11008 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11009 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11010 0))))
11011 buf.puts (" const");
11012 }
11013 }
11014
11015 const std::string &intermediate_name = buf.string ();
11016
11017 if (cu->language == language_cplus)
11018 canonical_name
11019 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11020 &objfile->per_bfd->storage_obstack);
11021
11022 /* If we only computed INTERMEDIATE_NAME, or if
11023 INTERMEDIATE_NAME is already canonical, then we need to
11024 copy it to the appropriate obstack. */
11025 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11026 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11027 intermediate_name);
11028 else
11029 name = canonical_name;
11030 }
11031 }
11032
11033 return name;
11034 }
11035
11036 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11037 If scope qualifiers are appropriate they will be added. The result
11038 will be allocated on the storage_obstack, or NULL if the DIE does
11039 not have a name. NAME may either be from a previous call to
11040 dwarf2_name or NULL.
11041
11042 The output string will be canonicalized (if C++). */
11043
11044 static const char *
11045 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11046 {
11047 return dwarf2_compute_name (name, die, cu, 0);
11048 }
11049
11050 /* Construct a physname for the given DIE in CU. NAME may either be
11051 from a previous call to dwarf2_name or NULL. The result will be
11052 allocated on the objfile_objstack or NULL if the DIE does not have a
11053 name.
11054
11055 The output string will be canonicalized (if C++). */
11056
11057 static const char *
11058 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11059 {
11060 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11061 const char *retval, *mangled = NULL, *canon = NULL;
11062 int need_copy = 1;
11063
11064 /* In this case dwarf2_compute_name is just a shortcut not building anything
11065 on its own. */
11066 if (!die_needs_namespace (die, cu))
11067 return dwarf2_compute_name (name, die, cu, 1);
11068
11069 mangled = dw2_linkage_name (die, cu);
11070
11071 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11072 See https://github.com/rust-lang/rust/issues/32925. */
11073 if (cu->language == language_rust && mangled != NULL
11074 && strchr (mangled, '{') != NULL)
11075 mangled = NULL;
11076
11077 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11078 has computed. */
11079 gdb::unique_xmalloc_ptr<char> demangled;
11080 if (mangled != NULL)
11081 {
11082
11083 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11084 {
11085 /* Do nothing (do not demangle the symbol name). */
11086 }
11087 else if (cu->language == language_go)
11088 {
11089 /* This is a lie, but we already lie to the caller new_symbol.
11090 new_symbol assumes we return the mangled name.
11091 This just undoes that lie until things are cleaned up. */
11092 }
11093 else
11094 {
11095 /* Use DMGL_RET_DROP for C++ template functions to suppress
11096 their return type. It is easier for GDB users to search
11097 for such functions as `name(params)' than `long name(params)'.
11098 In such case the minimal symbol names do not match the full
11099 symbol names but for template functions there is never a need
11100 to look up their definition from their declaration so
11101 the only disadvantage remains the minimal symbol variant
11102 `long name(params)' does not have the proper inferior type. */
11103 demangled.reset (gdb_demangle (mangled,
11104 (DMGL_PARAMS | DMGL_ANSI
11105 | DMGL_RET_DROP)));
11106 }
11107 if (demangled)
11108 canon = demangled.get ();
11109 else
11110 {
11111 canon = mangled;
11112 need_copy = 0;
11113 }
11114 }
11115
11116 if (canon == NULL || check_physname)
11117 {
11118 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11119
11120 if (canon != NULL && strcmp (physname, canon) != 0)
11121 {
11122 /* It may not mean a bug in GDB. The compiler could also
11123 compute DW_AT_linkage_name incorrectly. But in such case
11124 GDB would need to be bug-to-bug compatible. */
11125
11126 complaint (_("Computed physname <%s> does not match demangled <%s> "
11127 "(from linkage <%s>) - DIE at %s [in module %s]"),
11128 physname, canon, mangled, sect_offset_str (die->sect_off),
11129 objfile_name (objfile));
11130
11131 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11132 is available here - over computed PHYSNAME. It is safer
11133 against both buggy GDB and buggy compilers. */
11134
11135 retval = canon;
11136 }
11137 else
11138 {
11139 retval = physname;
11140 need_copy = 0;
11141 }
11142 }
11143 else
11144 retval = canon;
11145
11146 if (need_copy)
11147 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
11148
11149 return retval;
11150 }
11151
11152 /* Inspect DIE in CU for a namespace alias. If one exists, record
11153 a new symbol for it.
11154
11155 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11156
11157 static int
11158 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11159 {
11160 struct attribute *attr;
11161
11162 /* If the die does not have a name, this is not a namespace
11163 alias. */
11164 attr = dwarf2_attr (die, DW_AT_name, cu);
11165 if (attr != NULL)
11166 {
11167 int num;
11168 struct die_info *d = die;
11169 struct dwarf2_cu *imported_cu = cu;
11170
11171 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11172 keep inspecting DIEs until we hit the underlying import. */
11173 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11174 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11175 {
11176 attr = dwarf2_attr (d, DW_AT_import, cu);
11177 if (attr == NULL)
11178 break;
11179
11180 d = follow_die_ref (d, attr, &imported_cu);
11181 if (d->tag != DW_TAG_imported_declaration)
11182 break;
11183 }
11184
11185 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11186 {
11187 complaint (_("DIE at %s has too many recursively imported "
11188 "declarations"), sect_offset_str (d->sect_off));
11189 return 0;
11190 }
11191
11192 if (attr != NULL)
11193 {
11194 struct type *type;
11195 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11196
11197 type = get_die_type_at_offset (sect_off, cu->per_cu);
11198 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11199 {
11200 /* This declaration is a global namespace alias. Add
11201 a symbol for it whose type is the aliased namespace. */
11202 new_symbol (die, type, cu);
11203 return 1;
11204 }
11205 }
11206 }
11207
11208 return 0;
11209 }
11210
11211 /* Return the using directives repository (global or local?) to use in the
11212 current context for CU.
11213
11214 For Ada, imported declarations can materialize renamings, which *may* be
11215 global. However it is impossible (for now?) in DWARF to distinguish
11216 "external" imported declarations and "static" ones. As all imported
11217 declarations seem to be static in all other languages, make them all CU-wide
11218 global only in Ada. */
11219
11220 static struct using_direct **
11221 using_directives (struct dwarf2_cu *cu)
11222 {
11223 if (cu->language == language_ada
11224 && cu->get_builder ()->outermost_context_p ())
11225 return cu->get_builder ()->get_global_using_directives ();
11226 else
11227 return cu->get_builder ()->get_local_using_directives ();
11228 }
11229
11230 /* Read the import statement specified by the given die and record it. */
11231
11232 static void
11233 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11234 {
11235 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11236 struct attribute *import_attr;
11237 struct die_info *imported_die, *child_die;
11238 struct dwarf2_cu *imported_cu;
11239 const char *imported_name;
11240 const char *imported_name_prefix;
11241 const char *canonical_name;
11242 const char *import_alias;
11243 const char *imported_declaration = NULL;
11244 const char *import_prefix;
11245 std::vector<const char *> excludes;
11246
11247 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11248 if (import_attr == NULL)
11249 {
11250 complaint (_("Tag '%s' has no DW_AT_import"),
11251 dwarf_tag_name (die->tag));
11252 return;
11253 }
11254
11255 imported_cu = cu;
11256 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11257 imported_name = dwarf2_name (imported_die, imported_cu);
11258 if (imported_name == NULL)
11259 {
11260 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11261
11262 The import in the following code:
11263 namespace A
11264 {
11265 typedef int B;
11266 }
11267
11268 int main ()
11269 {
11270 using A::B;
11271 B b;
11272 return b;
11273 }
11274
11275 ...
11276 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11277 <52> DW_AT_decl_file : 1
11278 <53> DW_AT_decl_line : 6
11279 <54> DW_AT_import : <0x75>
11280 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11281 <59> DW_AT_name : B
11282 <5b> DW_AT_decl_file : 1
11283 <5c> DW_AT_decl_line : 2
11284 <5d> DW_AT_type : <0x6e>
11285 ...
11286 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11287 <76> DW_AT_byte_size : 4
11288 <77> DW_AT_encoding : 5 (signed)
11289
11290 imports the wrong die ( 0x75 instead of 0x58 ).
11291 This case will be ignored until the gcc bug is fixed. */
11292 return;
11293 }
11294
11295 /* Figure out the local name after import. */
11296 import_alias = dwarf2_name (die, cu);
11297
11298 /* Figure out where the statement is being imported to. */
11299 import_prefix = determine_prefix (die, cu);
11300
11301 /* Figure out what the scope of the imported die is and prepend it
11302 to the name of the imported die. */
11303 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11304
11305 if (imported_die->tag != DW_TAG_namespace
11306 && imported_die->tag != DW_TAG_module)
11307 {
11308 imported_declaration = imported_name;
11309 canonical_name = imported_name_prefix;
11310 }
11311 else if (strlen (imported_name_prefix) > 0)
11312 canonical_name = obconcat (&objfile->objfile_obstack,
11313 imported_name_prefix,
11314 (cu->language == language_d ? "." : "::"),
11315 imported_name, (char *) NULL);
11316 else
11317 canonical_name = imported_name;
11318
11319 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11320 for (child_die = die->child; child_die && child_die->tag;
11321 child_die = sibling_die (child_die))
11322 {
11323 /* DWARF-4: A Fortran use statement with a “rename list” may be
11324 represented by an imported module entry with an import attribute
11325 referring to the module and owned entries corresponding to those
11326 entities that are renamed as part of being imported. */
11327
11328 if (child_die->tag != DW_TAG_imported_declaration)
11329 {
11330 complaint (_("child DW_TAG_imported_declaration expected "
11331 "- DIE at %s [in module %s]"),
11332 sect_offset_str (child_die->sect_off),
11333 objfile_name (objfile));
11334 continue;
11335 }
11336
11337 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11338 if (import_attr == NULL)
11339 {
11340 complaint (_("Tag '%s' has no DW_AT_import"),
11341 dwarf_tag_name (child_die->tag));
11342 continue;
11343 }
11344
11345 imported_cu = cu;
11346 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11347 &imported_cu);
11348 imported_name = dwarf2_name (imported_die, imported_cu);
11349 if (imported_name == NULL)
11350 {
11351 complaint (_("child DW_TAG_imported_declaration has unknown "
11352 "imported name - DIE at %s [in module %s]"),
11353 sect_offset_str (child_die->sect_off),
11354 objfile_name (objfile));
11355 continue;
11356 }
11357
11358 excludes.push_back (imported_name);
11359
11360 process_die (child_die, cu);
11361 }
11362
11363 add_using_directive (using_directives (cu),
11364 import_prefix,
11365 canonical_name,
11366 import_alias,
11367 imported_declaration,
11368 excludes,
11369 0,
11370 &objfile->objfile_obstack);
11371 }
11372
11373 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11374 types, but gives them a size of zero. Starting with version 14,
11375 ICC is compatible with GCC. */
11376
11377 static bool
11378 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11379 {
11380 if (!cu->checked_producer)
11381 check_producer (cu);
11382
11383 return cu->producer_is_icc_lt_14;
11384 }
11385
11386 /* ICC generates a DW_AT_type for C void functions. This was observed on
11387 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11388 which says that void functions should not have a DW_AT_type. */
11389
11390 static bool
11391 producer_is_icc (struct dwarf2_cu *cu)
11392 {
11393 if (!cu->checked_producer)
11394 check_producer (cu);
11395
11396 return cu->producer_is_icc;
11397 }
11398
11399 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11400 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11401 this, it was first present in GCC release 4.3.0. */
11402
11403 static bool
11404 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11405 {
11406 if (!cu->checked_producer)
11407 check_producer (cu);
11408
11409 return cu->producer_is_gcc_lt_4_3;
11410 }
11411
11412 static file_and_directory
11413 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11414 {
11415 file_and_directory res;
11416
11417 /* Find the filename. Do not use dwarf2_name here, since the filename
11418 is not a source language identifier. */
11419 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11420 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11421
11422 if (res.comp_dir == NULL
11423 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11424 && IS_ABSOLUTE_PATH (res.name))
11425 {
11426 res.comp_dir_storage = ldirname (res.name);
11427 if (!res.comp_dir_storage.empty ())
11428 res.comp_dir = res.comp_dir_storage.c_str ();
11429 }
11430 if (res.comp_dir != NULL)
11431 {
11432 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11433 directory, get rid of it. */
11434 const char *cp = strchr (res.comp_dir, ':');
11435
11436 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11437 res.comp_dir = cp + 1;
11438 }
11439
11440 if (res.name == NULL)
11441 res.name = "<unknown>";
11442
11443 return res;
11444 }
11445
11446 /* Handle DW_AT_stmt_list for a compilation unit.
11447 DIE is the DW_TAG_compile_unit die for CU.
11448 COMP_DIR is the compilation directory. LOWPC is passed to
11449 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11450
11451 static void
11452 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11453 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11454 {
11455 struct dwarf2_per_objfile *dwarf2_per_objfile
11456 = cu->per_cu->dwarf2_per_objfile;
11457 struct objfile *objfile = dwarf2_per_objfile->objfile;
11458 struct attribute *attr;
11459 struct line_header line_header_local;
11460 hashval_t line_header_local_hash;
11461 void **slot;
11462 int decode_mapping;
11463
11464 gdb_assert (! cu->per_cu->is_debug_types);
11465
11466 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11467 if (attr == NULL)
11468 return;
11469
11470 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11471
11472 /* The line header hash table is only created if needed (it exists to
11473 prevent redundant reading of the line table for partial_units).
11474 If we're given a partial_unit, we'll need it. If we're given a
11475 compile_unit, then use the line header hash table if it's already
11476 created, but don't create one just yet. */
11477
11478 if (dwarf2_per_objfile->line_header_hash == NULL
11479 && die->tag == DW_TAG_partial_unit)
11480 {
11481 dwarf2_per_objfile->line_header_hash
11482 = htab_create_alloc_ex (127, line_header_hash_voidp,
11483 line_header_eq_voidp,
11484 free_line_header_voidp,
11485 &objfile->objfile_obstack,
11486 hashtab_obstack_allocate,
11487 dummy_obstack_deallocate);
11488 }
11489
11490 line_header_local.sect_off = line_offset;
11491 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11492 line_header_local_hash = line_header_hash (&line_header_local);
11493 if (dwarf2_per_objfile->line_header_hash != NULL)
11494 {
11495 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11496 &line_header_local,
11497 line_header_local_hash, NO_INSERT);
11498
11499 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11500 is not present in *SLOT (since if there is something in *SLOT then
11501 it will be for a partial_unit). */
11502 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11503 {
11504 gdb_assert (*slot != NULL);
11505 cu->line_header = (struct line_header *) *slot;
11506 return;
11507 }
11508 }
11509
11510 /* dwarf_decode_line_header does not yet provide sufficient information.
11511 We always have to call also dwarf_decode_lines for it. */
11512 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11513 if (lh == NULL)
11514 return;
11515
11516 cu->line_header = lh.release ();
11517 cu->line_header_die_owner = die;
11518
11519 if (dwarf2_per_objfile->line_header_hash == NULL)
11520 slot = NULL;
11521 else
11522 {
11523 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11524 &line_header_local,
11525 line_header_local_hash, INSERT);
11526 gdb_assert (slot != NULL);
11527 }
11528 if (slot != NULL && *slot == NULL)
11529 {
11530 /* This newly decoded line number information unit will be owned
11531 by line_header_hash hash table. */
11532 *slot = cu->line_header;
11533 cu->line_header_die_owner = NULL;
11534 }
11535 else
11536 {
11537 /* We cannot free any current entry in (*slot) as that struct line_header
11538 may be already used by multiple CUs. Create only temporary decoded
11539 line_header for this CU - it may happen at most once for each line
11540 number information unit. And if we're not using line_header_hash
11541 then this is what we want as well. */
11542 gdb_assert (die->tag != DW_TAG_partial_unit);
11543 }
11544 decode_mapping = (die->tag != DW_TAG_partial_unit);
11545 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11546 decode_mapping);
11547
11548 }
11549
11550 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11551
11552 static void
11553 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11554 {
11555 struct dwarf2_per_objfile *dwarf2_per_objfile
11556 = cu->per_cu->dwarf2_per_objfile;
11557 struct objfile *objfile = dwarf2_per_objfile->objfile;
11558 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11559 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11560 CORE_ADDR highpc = ((CORE_ADDR) 0);
11561 struct attribute *attr;
11562 struct die_info *child_die;
11563 CORE_ADDR baseaddr;
11564
11565 prepare_one_comp_unit (cu, die, cu->language);
11566 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11567
11568 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11569
11570 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11571 from finish_block. */
11572 if (lowpc == ((CORE_ADDR) -1))
11573 lowpc = highpc;
11574 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11575
11576 file_and_directory fnd = find_file_and_directory (die, cu);
11577
11578 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11579 standardised yet. As a workaround for the language detection we fall
11580 back to the DW_AT_producer string. */
11581 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11582 cu->language = language_opencl;
11583
11584 /* Similar hack for Go. */
11585 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11586 set_cu_language (DW_LANG_Go, cu);
11587
11588 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11589
11590 /* Decode line number information if present. We do this before
11591 processing child DIEs, so that the line header table is available
11592 for DW_AT_decl_file. */
11593 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11594
11595 /* Process all dies in compilation unit. */
11596 if (die->child != NULL)
11597 {
11598 child_die = die->child;
11599 while (child_die && child_die->tag)
11600 {
11601 process_die (child_die, cu);
11602 child_die = sibling_die (child_die);
11603 }
11604 }
11605
11606 /* Decode macro information, if present. Dwarf 2 macro information
11607 refers to information in the line number info statement program
11608 header, so we can only read it if we've read the header
11609 successfully. */
11610 attr = dwarf2_attr (die, DW_AT_macros, cu);
11611 if (attr == NULL)
11612 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11613 if (attr && cu->line_header)
11614 {
11615 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11616 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11617
11618 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11619 }
11620 else
11621 {
11622 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11623 if (attr && cu->line_header)
11624 {
11625 unsigned int macro_offset = DW_UNSND (attr);
11626
11627 dwarf_decode_macros (cu, macro_offset, 0);
11628 }
11629 }
11630 }
11631
11632 void
11633 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11634 {
11635 struct type_unit_group *tu_group;
11636 int first_time;
11637 struct attribute *attr;
11638 unsigned int i;
11639 struct signatured_type *sig_type;
11640
11641 gdb_assert (per_cu->is_debug_types);
11642 sig_type = (struct signatured_type *) per_cu;
11643
11644 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11645
11646 /* If we're using .gdb_index (includes -readnow) then
11647 per_cu->type_unit_group may not have been set up yet. */
11648 if (sig_type->type_unit_group == NULL)
11649 sig_type->type_unit_group = get_type_unit_group (this, attr);
11650 tu_group = sig_type->type_unit_group;
11651
11652 /* If we've already processed this stmt_list there's no real need to
11653 do it again, we could fake it and just recreate the part we need
11654 (file name,index -> symtab mapping). If data shows this optimization
11655 is useful we can do it then. */
11656 first_time = tu_group->compunit_symtab == NULL;
11657
11658 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11659 debug info. */
11660 line_header_up lh;
11661 if (attr != NULL)
11662 {
11663 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11664 lh = dwarf_decode_line_header (line_offset, this);
11665 }
11666 if (lh == NULL)
11667 {
11668 if (first_time)
11669 start_symtab ("", NULL, 0);
11670 else
11671 {
11672 gdb_assert (tu_group->symtabs == NULL);
11673 gdb_assert (m_builder == nullptr);
11674 struct compunit_symtab *cust = tu_group->compunit_symtab;
11675 m_builder.reset (new struct buildsym_compunit
11676 (COMPUNIT_OBJFILE (cust), "",
11677 COMPUNIT_DIRNAME (cust),
11678 compunit_language (cust),
11679 0, cust));
11680 }
11681 return;
11682 }
11683
11684 line_header = lh.release ();
11685 line_header_die_owner = die;
11686
11687 if (first_time)
11688 {
11689 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11690
11691 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11692 still initializing it, and our caller (a few levels up)
11693 process_full_type_unit still needs to know if this is the first
11694 time. */
11695
11696 tu_group->num_symtabs = line_header->file_names.size ();
11697 tu_group->symtabs = XNEWVEC (struct symtab *,
11698 line_header->file_names.size ());
11699
11700 for (i = 0; i < line_header->file_names.size (); ++i)
11701 {
11702 file_entry &fe = line_header->file_names[i];
11703
11704 dwarf2_start_subfile (this, fe.name,
11705 fe.include_dir (line_header));
11706 buildsym_compunit *b = get_builder ();
11707 if (b->get_current_subfile ()->symtab == NULL)
11708 {
11709 /* NOTE: start_subfile will recognize when it's been
11710 passed a file it has already seen. So we can't
11711 assume there's a simple mapping from
11712 cu->line_header->file_names to subfiles, plus
11713 cu->line_header->file_names may contain dups. */
11714 b->get_current_subfile ()->symtab
11715 = allocate_symtab (cust, b->get_current_subfile ()->name);
11716 }
11717
11718 fe.symtab = b->get_current_subfile ()->symtab;
11719 tu_group->symtabs[i] = fe.symtab;
11720 }
11721 }
11722 else
11723 {
11724 gdb_assert (m_builder == nullptr);
11725 struct compunit_symtab *cust = tu_group->compunit_symtab;
11726 m_builder.reset (new struct buildsym_compunit
11727 (COMPUNIT_OBJFILE (cust), "",
11728 COMPUNIT_DIRNAME (cust),
11729 compunit_language (cust),
11730 0, cust));
11731
11732 for (i = 0; i < line_header->file_names.size (); ++i)
11733 {
11734 file_entry &fe = line_header->file_names[i];
11735
11736 fe.symtab = tu_group->symtabs[i];
11737 }
11738 }
11739
11740 /* The main symtab is allocated last. Type units don't have DW_AT_name
11741 so they don't have a "real" (so to speak) symtab anyway.
11742 There is later code that will assign the main symtab to all symbols
11743 that don't have one. We need to handle the case of a symbol with a
11744 missing symtab (DW_AT_decl_file) anyway. */
11745 }
11746
11747 /* Process DW_TAG_type_unit.
11748 For TUs we want to skip the first top level sibling if it's not the
11749 actual type being defined by this TU. In this case the first top
11750 level sibling is there to provide context only. */
11751
11752 static void
11753 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11754 {
11755 struct die_info *child_die;
11756
11757 prepare_one_comp_unit (cu, die, language_minimal);
11758
11759 /* Initialize (or reinitialize) the machinery for building symtabs.
11760 We do this before processing child DIEs, so that the line header table
11761 is available for DW_AT_decl_file. */
11762 cu->setup_type_unit_groups (die);
11763
11764 if (die->child != NULL)
11765 {
11766 child_die = die->child;
11767 while (child_die && child_die->tag)
11768 {
11769 process_die (child_die, cu);
11770 child_die = sibling_die (child_die);
11771 }
11772 }
11773 }
11774 \f
11775 /* DWO/DWP files.
11776
11777 http://gcc.gnu.org/wiki/DebugFission
11778 http://gcc.gnu.org/wiki/DebugFissionDWP
11779
11780 To simplify handling of both DWO files ("object" files with the DWARF info)
11781 and DWP files (a file with the DWOs packaged up into one file), we treat
11782 DWP files as having a collection of virtual DWO files. */
11783
11784 static hashval_t
11785 hash_dwo_file (const void *item)
11786 {
11787 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11788 hashval_t hash;
11789
11790 hash = htab_hash_string (dwo_file->dwo_name);
11791 if (dwo_file->comp_dir != NULL)
11792 hash += htab_hash_string (dwo_file->comp_dir);
11793 return hash;
11794 }
11795
11796 static int
11797 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11798 {
11799 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11800 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11801
11802 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11803 return 0;
11804 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11805 return lhs->comp_dir == rhs->comp_dir;
11806 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11807 }
11808
11809 /* Allocate a hash table for DWO files. */
11810
11811 static htab_up
11812 allocate_dwo_file_hash_table (struct objfile *objfile)
11813 {
11814 auto delete_dwo_file = [] (void *item)
11815 {
11816 struct dwo_file *dwo_file = (struct dwo_file *) item;
11817
11818 delete dwo_file;
11819 };
11820
11821 return htab_up (htab_create_alloc_ex (41,
11822 hash_dwo_file,
11823 eq_dwo_file,
11824 delete_dwo_file,
11825 &objfile->objfile_obstack,
11826 hashtab_obstack_allocate,
11827 dummy_obstack_deallocate));
11828 }
11829
11830 /* Lookup DWO file DWO_NAME. */
11831
11832 static void **
11833 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11834 const char *dwo_name,
11835 const char *comp_dir)
11836 {
11837 struct dwo_file find_entry;
11838 void **slot;
11839
11840 if (dwarf2_per_objfile->dwo_files == NULL)
11841 dwarf2_per_objfile->dwo_files
11842 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11843
11844 find_entry.dwo_name = dwo_name;
11845 find_entry.comp_dir = comp_dir;
11846 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11847 INSERT);
11848
11849 return slot;
11850 }
11851
11852 static hashval_t
11853 hash_dwo_unit (const void *item)
11854 {
11855 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11856
11857 /* This drops the top 32 bits of the id, but is ok for a hash. */
11858 return dwo_unit->signature;
11859 }
11860
11861 static int
11862 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11863 {
11864 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11865 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11866
11867 /* The signature is assumed to be unique within the DWO file.
11868 So while object file CU dwo_id's always have the value zero,
11869 that's OK, assuming each object file DWO file has only one CU,
11870 and that's the rule for now. */
11871 return lhs->signature == rhs->signature;
11872 }
11873
11874 /* Allocate a hash table for DWO CUs,TUs.
11875 There is one of these tables for each of CUs,TUs for each DWO file. */
11876
11877 static htab_t
11878 allocate_dwo_unit_table (struct objfile *objfile)
11879 {
11880 /* Start out with a pretty small number.
11881 Generally DWO files contain only one CU and maybe some TUs. */
11882 return htab_create_alloc_ex (3,
11883 hash_dwo_unit,
11884 eq_dwo_unit,
11885 NULL,
11886 &objfile->objfile_obstack,
11887 hashtab_obstack_allocate,
11888 dummy_obstack_deallocate);
11889 }
11890
11891 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11892
11893 struct create_dwo_cu_data
11894 {
11895 struct dwo_file *dwo_file;
11896 struct dwo_unit dwo_unit;
11897 };
11898
11899 /* die_reader_func for create_dwo_cu. */
11900
11901 static void
11902 create_dwo_cu_reader (const struct die_reader_specs *reader,
11903 const gdb_byte *info_ptr,
11904 struct die_info *comp_unit_die,
11905 int has_children,
11906 void *datap)
11907 {
11908 struct dwarf2_cu *cu = reader->cu;
11909 sect_offset sect_off = cu->per_cu->sect_off;
11910 struct dwarf2_section_info *section = cu->per_cu->section;
11911 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11912 struct dwo_file *dwo_file = data->dwo_file;
11913 struct dwo_unit *dwo_unit = &data->dwo_unit;
11914
11915 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11916 if (!signature.has_value ())
11917 {
11918 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11919 " its dwo_id [in module %s]"),
11920 sect_offset_str (sect_off), dwo_file->dwo_name);
11921 return;
11922 }
11923
11924 dwo_unit->dwo_file = dwo_file;
11925 dwo_unit->signature = *signature;
11926 dwo_unit->section = section;
11927 dwo_unit->sect_off = sect_off;
11928 dwo_unit->length = cu->per_cu->length;
11929
11930 if (dwarf_read_debug)
11931 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11932 sect_offset_str (sect_off),
11933 hex_string (dwo_unit->signature));
11934 }
11935
11936 /* Create the dwo_units for the CUs in a DWO_FILE.
11937 Note: This function processes DWO files only, not DWP files. */
11938
11939 static void
11940 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11941 struct dwo_file &dwo_file, dwarf2_section_info &section,
11942 htab_t &cus_htab)
11943 {
11944 struct objfile *objfile = dwarf2_per_objfile->objfile;
11945 const gdb_byte *info_ptr, *end_ptr;
11946
11947 dwarf2_read_section (objfile, &section);
11948 info_ptr = section.buffer;
11949
11950 if (info_ptr == NULL)
11951 return;
11952
11953 if (dwarf_read_debug)
11954 {
11955 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11956 get_section_name (&section),
11957 get_section_file_name (&section));
11958 }
11959
11960 end_ptr = info_ptr + section.size;
11961 while (info_ptr < end_ptr)
11962 {
11963 struct dwarf2_per_cu_data per_cu;
11964 struct create_dwo_cu_data create_dwo_cu_data;
11965 struct dwo_unit *dwo_unit;
11966 void **slot;
11967 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11968
11969 memset (&create_dwo_cu_data.dwo_unit, 0,
11970 sizeof (create_dwo_cu_data.dwo_unit));
11971 memset (&per_cu, 0, sizeof (per_cu));
11972 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11973 per_cu.is_debug_types = 0;
11974 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11975 per_cu.section = &section;
11976 create_dwo_cu_data.dwo_file = &dwo_file;
11977
11978 init_cutu_and_read_dies_no_follow (
11979 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11980 info_ptr += per_cu.length;
11981
11982 // If the unit could not be parsed, skip it.
11983 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11984 continue;
11985
11986 if (cus_htab == NULL)
11987 cus_htab = allocate_dwo_unit_table (objfile);
11988
11989 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11990 *dwo_unit = create_dwo_cu_data.dwo_unit;
11991 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11992 gdb_assert (slot != NULL);
11993 if (*slot != NULL)
11994 {
11995 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11996 sect_offset dup_sect_off = dup_cu->sect_off;
11997
11998 complaint (_("debug cu entry at offset %s is duplicate to"
11999 " the entry at offset %s, signature %s"),
12000 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
12001 hex_string (dwo_unit->signature));
12002 }
12003 *slot = (void *)dwo_unit;
12004 }
12005 }
12006
12007 /* DWP file .debug_{cu,tu}_index section format:
12008 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12009
12010 DWP Version 1:
12011
12012 Both index sections have the same format, and serve to map a 64-bit
12013 signature to a set of section numbers. Each section begins with a header,
12014 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12015 indexes, and a pool of 32-bit section numbers. The index sections will be
12016 aligned at 8-byte boundaries in the file.
12017
12018 The index section header consists of:
12019
12020 V, 32 bit version number
12021 -, 32 bits unused
12022 N, 32 bit number of compilation units or type units in the index
12023 M, 32 bit number of slots in the hash table
12024
12025 Numbers are recorded using the byte order of the application binary.
12026
12027 The hash table begins at offset 16 in the section, and consists of an array
12028 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12029 order of the application binary). Unused slots in the hash table are 0.
12030 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12031
12032 The parallel table begins immediately after the hash table
12033 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12034 array of 32-bit indexes (using the byte order of the application binary),
12035 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12036 table contains a 32-bit index into the pool of section numbers. For unused
12037 hash table slots, the corresponding entry in the parallel table will be 0.
12038
12039 The pool of section numbers begins immediately following the hash table
12040 (at offset 16 + 12 * M from the beginning of the section). The pool of
12041 section numbers consists of an array of 32-bit words (using the byte order
12042 of the application binary). Each item in the array is indexed starting
12043 from 0. The hash table entry provides the index of the first section
12044 number in the set. Additional section numbers in the set follow, and the
12045 set is terminated by a 0 entry (section number 0 is not used in ELF).
12046
12047 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12048 section must be the first entry in the set, and the .debug_abbrev.dwo must
12049 be the second entry. Other members of the set may follow in any order.
12050
12051 ---
12052
12053 DWP Version 2:
12054
12055 DWP Version 2 combines all the .debug_info, etc. sections into one,
12056 and the entries in the index tables are now offsets into these sections.
12057 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12058 section.
12059
12060 Index Section Contents:
12061 Header
12062 Hash Table of Signatures dwp_hash_table.hash_table
12063 Parallel Table of Indices dwp_hash_table.unit_table
12064 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12065 Table of Section Sizes dwp_hash_table.v2.sizes
12066
12067 The index section header consists of:
12068
12069 V, 32 bit version number
12070 L, 32 bit number of columns in the table of section offsets
12071 N, 32 bit number of compilation units or type units in the index
12072 M, 32 bit number of slots in the hash table
12073
12074 Numbers are recorded using the byte order of the application binary.
12075
12076 The hash table has the same format as version 1.
12077 The parallel table of indices has the same format as version 1,
12078 except that the entries are origin-1 indices into the table of sections
12079 offsets and the table of section sizes.
12080
12081 The table of offsets begins immediately following the parallel table
12082 (at offset 16 + 12 * M from the beginning of the section). The table is
12083 a two-dimensional array of 32-bit words (using the byte order of the
12084 application binary), with L columns and N+1 rows, in row-major order.
12085 Each row in the array is indexed starting from 0. The first row provides
12086 a key to the remaining rows: each column in this row provides an identifier
12087 for a debug section, and the offsets in the same column of subsequent rows
12088 refer to that section. The section identifiers are:
12089
12090 DW_SECT_INFO 1 .debug_info.dwo
12091 DW_SECT_TYPES 2 .debug_types.dwo
12092 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12093 DW_SECT_LINE 4 .debug_line.dwo
12094 DW_SECT_LOC 5 .debug_loc.dwo
12095 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12096 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12097 DW_SECT_MACRO 8 .debug_macro.dwo
12098
12099 The offsets provided by the CU and TU index sections are the base offsets
12100 for the contributions made by each CU or TU to the corresponding section
12101 in the package file. Each CU and TU header contains an abbrev_offset
12102 field, used to find the abbreviations table for that CU or TU within the
12103 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12104 be interpreted as relative to the base offset given in the index section.
12105 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12106 should be interpreted as relative to the base offset for .debug_line.dwo,
12107 and offsets into other debug sections obtained from DWARF attributes should
12108 also be interpreted as relative to the corresponding base offset.
12109
12110 The table of sizes begins immediately following the table of offsets.
12111 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12112 with L columns and N rows, in row-major order. Each row in the array is
12113 indexed starting from 1 (row 0 is shared by the two tables).
12114
12115 ---
12116
12117 Hash table lookup is handled the same in version 1 and 2:
12118
12119 We assume that N and M will not exceed 2^32 - 1.
12120 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12121
12122 Given a 64-bit compilation unit signature or a type signature S, an entry
12123 in the hash table is located as follows:
12124
12125 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12126 the low-order k bits all set to 1.
12127
12128 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12129
12130 3) If the hash table entry at index H matches the signature, use that
12131 entry. If the hash table entry at index H is unused (all zeroes),
12132 terminate the search: the signature is not present in the table.
12133
12134 4) Let H = (H + H') modulo M. Repeat at Step 3.
12135
12136 Because M > N and H' and M are relatively prime, the search is guaranteed
12137 to stop at an unused slot or find the match. */
12138
12139 /* Create a hash table to map DWO IDs to their CU/TU entry in
12140 .debug_{info,types}.dwo in DWP_FILE.
12141 Returns NULL if there isn't one.
12142 Note: This function processes DWP files only, not DWO files. */
12143
12144 static struct dwp_hash_table *
12145 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12146 struct dwp_file *dwp_file, int is_debug_types)
12147 {
12148 struct objfile *objfile = dwarf2_per_objfile->objfile;
12149 bfd *dbfd = dwp_file->dbfd.get ();
12150 const gdb_byte *index_ptr, *index_end;
12151 struct dwarf2_section_info *index;
12152 uint32_t version, nr_columns, nr_units, nr_slots;
12153 struct dwp_hash_table *htab;
12154
12155 if (is_debug_types)
12156 index = &dwp_file->sections.tu_index;
12157 else
12158 index = &dwp_file->sections.cu_index;
12159
12160 if (dwarf2_section_empty_p (index))
12161 return NULL;
12162 dwarf2_read_section (objfile, index);
12163
12164 index_ptr = index->buffer;
12165 index_end = index_ptr + index->size;
12166
12167 version = read_4_bytes (dbfd, index_ptr);
12168 index_ptr += 4;
12169 if (version == 2)
12170 nr_columns = read_4_bytes (dbfd, index_ptr);
12171 else
12172 nr_columns = 0;
12173 index_ptr += 4;
12174 nr_units = read_4_bytes (dbfd, index_ptr);
12175 index_ptr += 4;
12176 nr_slots = read_4_bytes (dbfd, index_ptr);
12177 index_ptr += 4;
12178
12179 if (version != 1 && version != 2)
12180 {
12181 error (_("Dwarf Error: unsupported DWP file version (%s)"
12182 " [in module %s]"),
12183 pulongest (version), dwp_file->name);
12184 }
12185 if (nr_slots != (nr_slots & -nr_slots))
12186 {
12187 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12188 " is not power of 2 [in module %s]"),
12189 pulongest (nr_slots), dwp_file->name);
12190 }
12191
12192 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12193 htab->version = version;
12194 htab->nr_columns = nr_columns;
12195 htab->nr_units = nr_units;
12196 htab->nr_slots = nr_slots;
12197 htab->hash_table = index_ptr;
12198 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12199
12200 /* Exit early if the table is empty. */
12201 if (nr_slots == 0 || nr_units == 0
12202 || (version == 2 && nr_columns == 0))
12203 {
12204 /* All must be zero. */
12205 if (nr_slots != 0 || nr_units != 0
12206 || (version == 2 && nr_columns != 0))
12207 {
12208 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
12209 " all zero [in modules %s]"),
12210 dwp_file->name);
12211 }
12212 return htab;
12213 }
12214
12215 if (version == 1)
12216 {
12217 htab->section_pool.v1.indices =
12218 htab->unit_table + sizeof (uint32_t) * nr_slots;
12219 /* It's harder to decide whether the section is too small in v1.
12220 V1 is deprecated anyway so we punt. */
12221 }
12222 else
12223 {
12224 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12225 int *ids = htab->section_pool.v2.section_ids;
12226 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
12227 /* Reverse map for error checking. */
12228 int ids_seen[DW_SECT_MAX + 1];
12229 int i;
12230
12231 if (nr_columns < 2)
12232 {
12233 error (_("Dwarf Error: bad DWP hash table, too few columns"
12234 " in section table [in module %s]"),
12235 dwp_file->name);
12236 }
12237 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12238 {
12239 error (_("Dwarf Error: bad DWP hash table, too many columns"
12240 " in section table [in module %s]"),
12241 dwp_file->name);
12242 }
12243 memset (ids, 255, sizeof_ids);
12244 memset (ids_seen, 255, sizeof (ids_seen));
12245 for (i = 0; i < nr_columns; ++i)
12246 {
12247 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12248
12249 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12250 {
12251 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12252 " in section table [in module %s]"),
12253 id, dwp_file->name);
12254 }
12255 if (ids_seen[id] != -1)
12256 {
12257 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12258 " id %d in section table [in module %s]"),
12259 id, dwp_file->name);
12260 }
12261 ids_seen[id] = i;
12262 ids[i] = id;
12263 }
12264 /* Must have exactly one info or types section. */
12265 if (((ids_seen[DW_SECT_INFO] != -1)
12266 + (ids_seen[DW_SECT_TYPES] != -1))
12267 != 1)
12268 {
12269 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12270 " DWO info/types section [in module %s]"),
12271 dwp_file->name);
12272 }
12273 /* Must have an abbrev section. */
12274 if (ids_seen[DW_SECT_ABBREV] == -1)
12275 {
12276 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12277 " section [in module %s]"),
12278 dwp_file->name);
12279 }
12280 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12281 htab->section_pool.v2.sizes =
12282 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12283 * nr_units * nr_columns);
12284 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12285 * nr_units * nr_columns))
12286 > index_end)
12287 {
12288 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12289 " [in module %s]"),
12290 dwp_file->name);
12291 }
12292 }
12293
12294 return htab;
12295 }
12296
12297 /* Update SECTIONS with the data from SECTP.
12298
12299 This function is like the other "locate" section routines that are
12300 passed to bfd_map_over_sections, but in this context the sections to
12301 read comes from the DWP V1 hash table, not the full ELF section table.
12302
12303 The result is non-zero for success, or zero if an error was found. */
12304
12305 static int
12306 locate_v1_virtual_dwo_sections (asection *sectp,
12307 struct virtual_v1_dwo_sections *sections)
12308 {
12309 const struct dwop_section_names *names = &dwop_section_names;
12310
12311 if (section_is_p (sectp->name, &names->abbrev_dwo))
12312 {
12313 /* There can be only one. */
12314 if (sections->abbrev.s.section != NULL)
12315 return 0;
12316 sections->abbrev.s.section = sectp;
12317 sections->abbrev.size = bfd_section_size (sectp);
12318 }
12319 else if (section_is_p (sectp->name, &names->info_dwo)
12320 || section_is_p (sectp->name, &names->types_dwo))
12321 {
12322 /* There can be only one. */
12323 if (sections->info_or_types.s.section != NULL)
12324 return 0;
12325 sections->info_or_types.s.section = sectp;
12326 sections->info_or_types.size = bfd_section_size (sectp);
12327 }
12328 else if (section_is_p (sectp->name, &names->line_dwo))
12329 {
12330 /* There can be only one. */
12331 if (sections->line.s.section != NULL)
12332 return 0;
12333 sections->line.s.section = sectp;
12334 sections->line.size = bfd_section_size (sectp);
12335 }
12336 else if (section_is_p (sectp->name, &names->loc_dwo))
12337 {
12338 /* There can be only one. */
12339 if (sections->loc.s.section != NULL)
12340 return 0;
12341 sections->loc.s.section = sectp;
12342 sections->loc.size = bfd_section_size (sectp);
12343 }
12344 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12345 {
12346 /* There can be only one. */
12347 if (sections->macinfo.s.section != NULL)
12348 return 0;
12349 sections->macinfo.s.section = sectp;
12350 sections->macinfo.size = bfd_section_size (sectp);
12351 }
12352 else if (section_is_p (sectp->name, &names->macro_dwo))
12353 {
12354 /* There can be only one. */
12355 if (sections->macro.s.section != NULL)
12356 return 0;
12357 sections->macro.s.section = sectp;
12358 sections->macro.size = bfd_section_size (sectp);
12359 }
12360 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12361 {
12362 /* There can be only one. */
12363 if (sections->str_offsets.s.section != NULL)
12364 return 0;
12365 sections->str_offsets.s.section = sectp;
12366 sections->str_offsets.size = bfd_section_size (sectp);
12367 }
12368 else
12369 {
12370 /* No other kind of section is valid. */
12371 return 0;
12372 }
12373
12374 return 1;
12375 }
12376
12377 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12378 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12379 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12380 This is for DWP version 1 files. */
12381
12382 static struct dwo_unit *
12383 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12384 struct dwp_file *dwp_file,
12385 uint32_t unit_index,
12386 const char *comp_dir,
12387 ULONGEST signature, int is_debug_types)
12388 {
12389 struct objfile *objfile = dwarf2_per_objfile->objfile;
12390 const struct dwp_hash_table *dwp_htab =
12391 is_debug_types ? dwp_file->tus : dwp_file->cus;
12392 bfd *dbfd = dwp_file->dbfd.get ();
12393 const char *kind = is_debug_types ? "TU" : "CU";
12394 struct dwo_file *dwo_file;
12395 struct dwo_unit *dwo_unit;
12396 struct virtual_v1_dwo_sections sections;
12397 void **dwo_file_slot;
12398 int i;
12399
12400 gdb_assert (dwp_file->version == 1);
12401
12402 if (dwarf_read_debug)
12403 {
12404 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12405 kind,
12406 pulongest (unit_index), hex_string (signature),
12407 dwp_file->name);
12408 }
12409
12410 /* Fetch the sections of this DWO unit.
12411 Put a limit on the number of sections we look for so that bad data
12412 doesn't cause us to loop forever. */
12413
12414 #define MAX_NR_V1_DWO_SECTIONS \
12415 (1 /* .debug_info or .debug_types */ \
12416 + 1 /* .debug_abbrev */ \
12417 + 1 /* .debug_line */ \
12418 + 1 /* .debug_loc */ \
12419 + 1 /* .debug_str_offsets */ \
12420 + 1 /* .debug_macro or .debug_macinfo */ \
12421 + 1 /* trailing zero */)
12422
12423 memset (&sections, 0, sizeof (sections));
12424
12425 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12426 {
12427 asection *sectp;
12428 uint32_t section_nr =
12429 read_4_bytes (dbfd,
12430 dwp_htab->section_pool.v1.indices
12431 + (unit_index + i) * sizeof (uint32_t));
12432
12433 if (section_nr == 0)
12434 break;
12435 if (section_nr >= dwp_file->num_sections)
12436 {
12437 error (_("Dwarf Error: bad DWP hash table, section number too large"
12438 " [in module %s]"),
12439 dwp_file->name);
12440 }
12441
12442 sectp = dwp_file->elf_sections[section_nr];
12443 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12444 {
12445 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12446 " [in module %s]"),
12447 dwp_file->name);
12448 }
12449 }
12450
12451 if (i < 2
12452 || dwarf2_section_empty_p (&sections.info_or_types)
12453 || dwarf2_section_empty_p (&sections.abbrev))
12454 {
12455 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12456 " [in module %s]"),
12457 dwp_file->name);
12458 }
12459 if (i == MAX_NR_V1_DWO_SECTIONS)
12460 {
12461 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12462 " [in module %s]"),
12463 dwp_file->name);
12464 }
12465
12466 /* It's easier for the rest of the code if we fake a struct dwo_file and
12467 have dwo_unit "live" in that. At least for now.
12468
12469 The DWP file can be made up of a random collection of CUs and TUs.
12470 However, for each CU + set of TUs that came from the same original DWO
12471 file, we can combine them back into a virtual DWO file to save space
12472 (fewer struct dwo_file objects to allocate). Remember that for really
12473 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12474
12475 std::string virtual_dwo_name =
12476 string_printf ("virtual-dwo/%d-%d-%d-%d",
12477 get_section_id (&sections.abbrev),
12478 get_section_id (&sections.line),
12479 get_section_id (&sections.loc),
12480 get_section_id (&sections.str_offsets));
12481 /* Can we use an existing virtual DWO file? */
12482 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12483 virtual_dwo_name.c_str (),
12484 comp_dir);
12485 /* Create one if necessary. */
12486 if (*dwo_file_slot == NULL)
12487 {
12488 if (dwarf_read_debug)
12489 {
12490 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12491 virtual_dwo_name.c_str ());
12492 }
12493 dwo_file = new struct dwo_file;
12494 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12495 virtual_dwo_name);
12496 dwo_file->comp_dir = comp_dir;
12497 dwo_file->sections.abbrev = sections.abbrev;
12498 dwo_file->sections.line = sections.line;
12499 dwo_file->sections.loc = sections.loc;
12500 dwo_file->sections.macinfo = sections.macinfo;
12501 dwo_file->sections.macro = sections.macro;
12502 dwo_file->sections.str_offsets = sections.str_offsets;
12503 /* The "str" section is global to the entire DWP file. */
12504 dwo_file->sections.str = dwp_file->sections.str;
12505 /* The info or types section is assigned below to dwo_unit,
12506 there's no need to record it in dwo_file.
12507 Also, we can't simply record type sections in dwo_file because
12508 we record a pointer into the vector in dwo_unit. As we collect more
12509 types we'll grow the vector and eventually have to reallocate space
12510 for it, invalidating all copies of pointers into the previous
12511 contents. */
12512 *dwo_file_slot = dwo_file;
12513 }
12514 else
12515 {
12516 if (dwarf_read_debug)
12517 {
12518 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12519 virtual_dwo_name.c_str ());
12520 }
12521 dwo_file = (struct dwo_file *) *dwo_file_slot;
12522 }
12523
12524 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12525 dwo_unit->dwo_file = dwo_file;
12526 dwo_unit->signature = signature;
12527 dwo_unit->section =
12528 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12529 *dwo_unit->section = sections.info_or_types;
12530 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12531
12532 return dwo_unit;
12533 }
12534
12535 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12536 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12537 piece within that section used by a TU/CU, return a virtual section
12538 of just that piece. */
12539
12540 static struct dwarf2_section_info
12541 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12542 struct dwarf2_section_info *section,
12543 bfd_size_type offset, bfd_size_type size)
12544 {
12545 struct dwarf2_section_info result;
12546 asection *sectp;
12547
12548 gdb_assert (section != NULL);
12549 gdb_assert (!section->is_virtual);
12550
12551 memset (&result, 0, sizeof (result));
12552 result.s.containing_section = section;
12553 result.is_virtual = true;
12554
12555 if (size == 0)
12556 return result;
12557
12558 sectp = get_section_bfd_section (section);
12559
12560 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12561 bounds of the real section. This is a pretty-rare event, so just
12562 flag an error (easier) instead of a warning and trying to cope. */
12563 if (sectp == NULL
12564 || offset + size > bfd_section_size (sectp))
12565 {
12566 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12567 " in section %s [in module %s]"),
12568 sectp ? bfd_section_name (sectp) : "<unknown>",
12569 objfile_name (dwarf2_per_objfile->objfile));
12570 }
12571
12572 result.virtual_offset = offset;
12573 result.size = size;
12574 return result;
12575 }
12576
12577 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12578 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12579 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12580 This is for DWP version 2 files. */
12581
12582 static struct dwo_unit *
12583 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12584 struct dwp_file *dwp_file,
12585 uint32_t unit_index,
12586 const char *comp_dir,
12587 ULONGEST signature, int is_debug_types)
12588 {
12589 struct objfile *objfile = dwarf2_per_objfile->objfile;
12590 const struct dwp_hash_table *dwp_htab =
12591 is_debug_types ? dwp_file->tus : dwp_file->cus;
12592 bfd *dbfd = dwp_file->dbfd.get ();
12593 const char *kind = is_debug_types ? "TU" : "CU";
12594 struct dwo_file *dwo_file;
12595 struct dwo_unit *dwo_unit;
12596 struct virtual_v2_dwo_sections sections;
12597 void **dwo_file_slot;
12598 int i;
12599
12600 gdb_assert (dwp_file->version == 2);
12601
12602 if (dwarf_read_debug)
12603 {
12604 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12605 kind,
12606 pulongest (unit_index), hex_string (signature),
12607 dwp_file->name);
12608 }
12609
12610 /* Fetch the section offsets of this DWO unit. */
12611
12612 memset (&sections, 0, sizeof (sections));
12613
12614 for (i = 0; i < dwp_htab->nr_columns; ++i)
12615 {
12616 uint32_t offset = read_4_bytes (dbfd,
12617 dwp_htab->section_pool.v2.offsets
12618 + (((unit_index - 1) * dwp_htab->nr_columns
12619 + i)
12620 * sizeof (uint32_t)));
12621 uint32_t size = read_4_bytes (dbfd,
12622 dwp_htab->section_pool.v2.sizes
12623 + (((unit_index - 1) * dwp_htab->nr_columns
12624 + i)
12625 * sizeof (uint32_t)));
12626
12627 switch (dwp_htab->section_pool.v2.section_ids[i])
12628 {
12629 case DW_SECT_INFO:
12630 case DW_SECT_TYPES:
12631 sections.info_or_types_offset = offset;
12632 sections.info_or_types_size = size;
12633 break;
12634 case DW_SECT_ABBREV:
12635 sections.abbrev_offset = offset;
12636 sections.abbrev_size = size;
12637 break;
12638 case DW_SECT_LINE:
12639 sections.line_offset = offset;
12640 sections.line_size = size;
12641 break;
12642 case DW_SECT_LOC:
12643 sections.loc_offset = offset;
12644 sections.loc_size = size;
12645 break;
12646 case DW_SECT_STR_OFFSETS:
12647 sections.str_offsets_offset = offset;
12648 sections.str_offsets_size = size;
12649 break;
12650 case DW_SECT_MACINFO:
12651 sections.macinfo_offset = offset;
12652 sections.macinfo_size = size;
12653 break;
12654 case DW_SECT_MACRO:
12655 sections.macro_offset = offset;
12656 sections.macro_size = size;
12657 break;
12658 }
12659 }
12660
12661 /* It's easier for the rest of the code if we fake a struct dwo_file and
12662 have dwo_unit "live" in that. At least for now.
12663
12664 The DWP file can be made up of a random collection of CUs and TUs.
12665 However, for each CU + set of TUs that came from the same original DWO
12666 file, we can combine them back into a virtual DWO file to save space
12667 (fewer struct dwo_file objects to allocate). Remember that for really
12668 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12669
12670 std::string virtual_dwo_name =
12671 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12672 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12673 (long) (sections.line_size ? sections.line_offset : 0),
12674 (long) (sections.loc_size ? sections.loc_offset : 0),
12675 (long) (sections.str_offsets_size
12676 ? sections.str_offsets_offset : 0));
12677 /* Can we use an existing virtual DWO file? */
12678 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12679 virtual_dwo_name.c_str (),
12680 comp_dir);
12681 /* Create one if necessary. */
12682 if (*dwo_file_slot == NULL)
12683 {
12684 if (dwarf_read_debug)
12685 {
12686 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12687 virtual_dwo_name.c_str ());
12688 }
12689 dwo_file = new struct dwo_file;
12690 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12691 virtual_dwo_name);
12692 dwo_file->comp_dir = comp_dir;
12693 dwo_file->sections.abbrev =
12694 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12695 sections.abbrev_offset, sections.abbrev_size);
12696 dwo_file->sections.line =
12697 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12698 sections.line_offset, sections.line_size);
12699 dwo_file->sections.loc =
12700 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12701 sections.loc_offset, sections.loc_size);
12702 dwo_file->sections.macinfo =
12703 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12704 sections.macinfo_offset, sections.macinfo_size);
12705 dwo_file->sections.macro =
12706 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12707 sections.macro_offset, sections.macro_size);
12708 dwo_file->sections.str_offsets =
12709 create_dwp_v2_section (dwarf2_per_objfile,
12710 &dwp_file->sections.str_offsets,
12711 sections.str_offsets_offset,
12712 sections.str_offsets_size);
12713 /* The "str" section is global to the entire DWP file. */
12714 dwo_file->sections.str = dwp_file->sections.str;
12715 /* The info or types section is assigned below to dwo_unit,
12716 there's no need to record it in dwo_file.
12717 Also, we can't simply record type sections in dwo_file because
12718 we record a pointer into the vector in dwo_unit. As we collect more
12719 types we'll grow the vector and eventually have to reallocate space
12720 for it, invalidating all copies of pointers into the previous
12721 contents. */
12722 *dwo_file_slot = dwo_file;
12723 }
12724 else
12725 {
12726 if (dwarf_read_debug)
12727 {
12728 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12729 virtual_dwo_name.c_str ());
12730 }
12731 dwo_file = (struct dwo_file *) *dwo_file_slot;
12732 }
12733
12734 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12735 dwo_unit->dwo_file = dwo_file;
12736 dwo_unit->signature = signature;
12737 dwo_unit->section =
12738 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12739 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12740 is_debug_types
12741 ? &dwp_file->sections.types
12742 : &dwp_file->sections.info,
12743 sections.info_or_types_offset,
12744 sections.info_or_types_size);
12745 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12746
12747 return dwo_unit;
12748 }
12749
12750 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12751 Returns NULL if the signature isn't found. */
12752
12753 static struct dwo_unit *
12754 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12755 struct dwp_file *dwp_file, const char *comp_dir,
12756 ULONGEST signature, int is_debug_types)
12757 {
12758 const struct dwp_hash_table *dwp_htab =
12759 is_debug_types ? dwp_file->tus : dwp_file->cus;
12760 bfd *dbfd = dwp_file->dbfd.get ();
12761 uint32_t mask = dwp_htab->nr_slots - 1;
12762 uint32_t hash = signature & mask;
12763 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12764 unsigned int i;
12765 void **slot;
12766 struct dwo_unit find_dwo_cu;
12767
12768 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12769 find_dwo_cu.signature = signature;
12770 slot = htab_find_slot (is_debug_types
12771 ? dwp_file->loaded_tus
12772 : dwp_file->loaded_cus,
12773 &find_dwo_cu, INSERT);
12774
12775 if (*slot != NULL)
12776 return (struct dwo_unit *) *slot;
12777
12778 /* Use a for loop so that we don't loop forever on bad debug info. */
12779 for (i = 0; i < dwp_htab->nr_slots; ++i)
12780 {
12781 ULONGEST signature_in_table;
12782
12783 signature_in_table =
12784 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12785 if (signature_in_table == signature)
12786 {
12787 uint32_t unit_index =
12788 read_4_bytes (dbfd,
12789 dwp_htab->unit_table + hash * sizeof (uint32_t));
12790
12791 if (dwp_file->version == 1)
12792 {
12793 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12794 dwp_file, unit_index,
12795 comp_dir, signature,
12796 is_debug_types);
12797 }
12798 else
12799 {
12800 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12801 dwp_file, unit_index,
12802 comp_dir, signature,
12803 is_debug_types);
12804 }
12805 return (struct dwo_unit *) *slot;
12806 }
12807 if (signature_in_table == 0)
12808 return NULL;
12809 hash = (hash + hash2) & mask;
12810 }
12811
12812 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12813 " [in module %s]"),
12814 dwp_file->name);
12815 }
12816
12817 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12818 Open the file specified by FILE_NAME and hand it off to BFD for
12819 preliminary analysis. Return a newly initialized bfd *, which
12820 includes a canonicalized copy of FILE_NAME.
12821 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12822 SEARCH_CWD is true if the current directory is to be searched.
12823 It will be searched before debug-file-directory.
12824 If successful, the file is added to the bfd include table of the
12825 objfile's bfd (see gdb_bfd_record_inclusion).
12826 If unable to find/open the file, return NULL.
12827 NOTE: This function is derived from symfile_bfd_open. */
12828
12829 static gdb_bfd_ref_ptr
12830 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12831 const char *file_name, int is_dwp, int search_cwd)
12832 {
12833 int desc;
12834 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12835 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12836 to debug_file_directory. */
12837 const char *search_path;
12838 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12839
12840 gdb::unique_xmalloc_ptr<char> search_path_holder;
12841 if (search_cwd)
12842 {
12843 if (*debug_file_directory != '\0')
12844 {
12845 search_path_holder.reset (concat (".", dirname_separator_string,
12846 debug_file_directory,
12847 (char *) NULL));
12848 search_path = search_path_holder.get ();
12849 }
12850 else
12851 search_path = ".";
12852 }
12853 else
12854 search_path = debug_file_directory;
12855
12856 openp_flags flags = OPF_RETURN_REALPATH;
12857 if (is_dwp)
12858 flags |= OPF_SEARCH_IN_PATH;
12859
12860 gdb::unique_xmalloc_ptr<char> absolute_name;
12861 desc = openp (search_path, flags, file_name,
12862 O_RDONLY | O_BINARY, &absolute_name);
12863 if (desc < 0)
12864 return NULL;
12865
12866 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12867 gnutarget, desc));
12868 if (sym_bfd == NULL)
12869 return NULL;
12870 bfd_set_cacheable (sym_bfd.get (), 1);
12871
12872 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12873 return NULL;
12874
12875 /* Success. Record the bfd as having been included by the objfile's bfd.
12876 This is important because things like demangled_names_hash lives in the
12877 objfile's per_bfd space and may have references to things like symbol
12878 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12879 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12880
12881 return sym_bfd;
12882 }
12883
12884 /* Try to open DWO file FILE_NAME.
12885 COMP_DIR is the DW_AT_comp_dir attribute.
12886 The result is the bfd handle of the file.
12887 If there is a problem finding or opening the file, return NULL.
12888 Upon success, the canonicalized path of the file is stored in the bfd,
12889 same as symfile_bfd_open. */
12890
12891 static gdb_bfd_ref_ptr
12892 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12893 const char *file_name, const char *comp_dir)
12894 {
12895 if (IS_ABSOLUTE_PATH (file_name))
12896 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12897 0 /*is_dwp*/, 0 /*search_cwd*/);
12898
12899 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12900
12901 if (comp_dir != NULL)
12902 {
12903 char *path_to_try = concat (comp_dir, SLASH_STRING,
12904 file_name, (char *) NULL);
12905
12906 /* NOTE: If comp_dir is a relative path, this will also try the
12907 search path, which seems useful. */
12908 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12909 path_to_try,
12910 0 /*is_dwp*/,
12911 1 /*search_cwd*/));
12912 xfree (path_to_try);
12913 if (abfd != NULL)
12914 return abfd;
12915 }
12916
12917 /* That didn't work, try debug-file-directory, which, despite its name,
12918 is a list of paths. */
12919
12920 if (*debug_file_directory == '\0')
12921 return NULL;
12922
12923 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12924 0 /*is_dwp*/, 1 /*search_cwd*/);
12925 }
12926
12927 /* This function is mapped across the sections and remembers the offset and
12928 size of each of the DWO debugging sections we are interested in. */
12929
12930 static void
12931 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12932 {
12933 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12934 const struct dwop_section_names *names = &dwop_section_names;
12935
12936 if (section_is_p (sectp->name, &names->abbrev_dwo))
12937 {
12938 dwo_sections->abbrev.s.section = sectp;
12939 dwo_sections->abbrev.size = bfd_section_size (sectp);
12940 }
12941 else if (section_is_p (sectp->name, &names->info_dwo))
12942 {
12943 dwo_sections->info.s.section = sectp;
12944 dwo_sections->info.size = bfd_section_size (sectp);
12945 }
12946 else if (section_is_p (sectp->name, &names->line_dwo))
12947 {
12948 dwo_sections->line.s.section = sectp;
12949 dwo_sections->line.size = bfd_section_size (sectp);
12950 }
12951 else if (section_is_p (sectp->name, &names->loc_dwo))
12952 {
12953 dwo_sections->loc.s.section = sectp;
12954 dwo_sections->loc.size = bfd_section_size (sectp);
12955 }
12956 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12957 {
12958 dwo_sections->macinfo.s.section = sectp;
12959 dwo_sections->macinfo.size = bfd_section_size (sectp);
12960 }
12961 else if (section_is_p (sectp->name, &names->macro_dwo))
12962 {
12963 dwo_sections->macro.s.section = sectp;
12964 dwo_sections->macro.size = bfd_section_size (sectp);
12965 }
12966 else if (section_is_p (sectp->name, &names->str_dwo))
12967 {
12968 dwo_sections->str.s.section = sectp;
12969 dwo_sections->str.size = bfd_section_size (sectp);
12970 }
12971 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12972 {
12973 dwo_sections->str_offsets.s.section = sectp;
12974 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12975 }
12976 else if (section_is_p (sectp->name, &names->types_dwo))
12977 {
12978 struct dwarf2_section_info type_section;
12979
12980 memset (&type_section, 0, sizeof (type_section));
12981 type_section.s.section = sectp;
12982 type_section.size = bfd_section_size (sectp);
12983 dwo_sections->types.push_back (type_section);
12984 }
12985 }
12986
12987 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12988 by PER_CU. This is for the non-DWP case.
12989 The result is NULL if DWO_NAME can't be found. */
12990
12991 static struct dwo_file *
12992 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12993 const char *dwo_name, const char *comp_dir)
12994 {
12995 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12996
12997 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12998 if (dbfd == NULL)
12999 {
13000 if (dwarf_read_debug)
13001 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13002 return NULL;
13003 }
13004
13005 dwo_file_up dwo_file (new struct dwo_file);
13006 dwo_file->dwo_name = dwo_name;
13007 dwo_file->comp_dir = comp_dir;
13008 dwo_file->dbfd = std::move (dbfd);
13009
13010 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
13011 &dwo_file->sections);
13012
13013 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13014 dwo_file->cus);
13015
13016 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
13017 dwo_file->sections.types, dwo_file->tus);
13018
13019 if (dwarf_read_debug)
13020 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13021
13022 return dwo_file.release ();
13023 }
13024
13025 /* This function is mapped across the sections and remembers the offset and
13026 size of each of the DWP debugging sections common to version 1 and 2 that
13027 we are interested in. */
13028
13029 static void
13030 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13031 void *dwp_file_ptr)
13032 {
13033 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13034 const struct dwop_section_names *names = &dwop_section_names;
13035 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13036
13037 /* Record the ELF section number for later lookup: this is what the
13038 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13039 gdb_assert (elf_section_nr < dwp_file->num_sections);
13040 dwp_file->elf_sections[elf_section_nr] = sectp;
13041
13042 /* Look for specific sections that we need. */
13043 if (section_is_p (sectp->name, &names->str_dwo))
13044 {
13045 dwp_file->sections.str.s.section = sectp;
13046 dwp_file->sections.str.size = bfd_section_size (sectp);
13047 }
13048 else if (section_is_p (sectp->name, &names->cu_index))
13049 {
13050 dwp_file->sections.cu_index.s.section = sectp;
13051 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
13052 }
13053 else if (section_is_p (sectp->name, &names->tu_index))
13054 {
13055 dwp_file->sections.tu_index.s.section = sectp;
13056 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
13057 }
13058 }
13059
13060 /* This function is mapped across the sections and remembers the offset and
13061 size of each of the DWP version 2 debugging sections that we are interested
13062 in. This is split into a separate function because we don't know if we
13063 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13064
13065 static void
13066 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13067 {
13068 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13069 const struct dwop_section_names *names = &dwop_section_names;
13070 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13071
13072 /* Record the ELF section number for later lookup: this is what the
13073 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13074 gdb_assert (elf_section_nr < dwp_file->num_sections);
13075 dwp_file->elf_sections[elf_section_nr] = sectp;
13076
13077 /* Look for specific sections that we need. */
13078 if (section_is_p (sectp->name, &names->abbrev_dwo))
13079 {
13080 dwp_file->sections.abbrev.s.section = sectp;
13081 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13082 }
13083 else if (section_is_p (sectp->name, &names->info_dwo))
13084 {
13085 dwp_file->sections.info.s.section = sectp;
13086 dwp_file->sections.info.size = bfd_section_size (sectp);
13087 }
13088 else if (section_is_p (sectp->name, &names->line_dwo))
13089 {
13090 dwp_file->sections.line.s.section = sectp;
13091 dwp_file->sections.line.size = bfd_section_size (sectp);
13092 }
13093 else if (section_is_p (sectp->name, &names->loc_dwo))
13094 {
13095 dwp_file->sections.loc.s.section = sectp;
13096 dwp_file->sections.loc.size = bfd_section_size (sectp);
13097 }
13098 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13099 {
13100 dwp_file->sections.macinfo.s.section = sectp;
13101 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
13102 }
13103 else if (section_is_p (sectp->name, &names->macro_dwo))
13104 {
13105 dwp_file->sections.macro.s.section = sectp;
13106 dwp_file->sections.macro.size = bfd_section_size (sectp);
13107 }
13108 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13109 {
13110 dwp_file->sections.str_offsets.s.section = sectp;
13111 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13112 }
13113 else if (section_is_p (sectp->name, &names->types_dwo))
13114 {
13115 dwp_file->sections.types.s.section = sectp;
13116 dwp_file->sections.types.size = bfd_section_size (sectp);
13117 }
13118 }
13119
13120 /* Hash function for dwp_file loaded CUs/TUs. */
13121
13122 static hashval_t
13123 hash_dwp_loaded_cutus (const void *item)
13124 {
13125 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13126
13127 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13128 return dwo_unit->signature;
13129 }
13130
13131 /* Equality function for dwp_file loaded CUs/TUs. */
13132
13133 static int
13134 eq_dwp_loaded_cutus (const void *a, const void *b)
13135 {
13136 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13137 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13138
13139 return dua->signature == dub->signature;
13140 }
13141
13142 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13143
13144 static htab_t
13145 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13146 {
13147 return htab_create_alloc_ex (3,
13148 hash_dwp_loaded_cutus,
13149 eq_dwp_loaded_cutus,
13150 NULL,
13151 &objfile->objfile_obstack,
13152 hashtab_obstack_allocate,
13153 dummy_obstack_deallocate);
13154 }
13155
13156 /* Try to open DWP file FILE_NAME.
13157 The result is the bfd handle of the file.
13158 If there is a problem finding or opening the file, return NULL.
13159 Upon success, the canonicalized path of the file is stored in the bfd,
13160 same as symfile_bfd_open. */
13161
13162 static gdb_bfd_ref_ptr
13163 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13164 const char *file_name)
13165 {
13166 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13167 1 /*is_dwp*/,
13168 1 /*search_cwd*/));
13169 if (abfd != NULL)
13170 return abfd;
13171
13172 /* Work around upstream bug 15652.
13173 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13174 [Whether that's a "bug" is debatable, but it is getting in our way.]
13175 We have no real idea where the dwp file is, because gdb's realpath-ing
13176 of the executable's path may have discarded the needed info.
13177 [IWBN if the dwp file name was recorded in the executable, akin to
13178 .gnu_debuglink, but that doesn't exist yet.]
13179 Strip the directory from FILE_NAME and search again. */
13180 if (*debug_file_directory != '\0')
13181 {
13182 /* Don't implicitly search the current directory here.
13183 If the user wants to search "." to handle this case,
13184 it must be added to debug-file-directory. */
13185 return try_open_dwop_file (dwarf2_per_objfile,
13186 lbasename (file_name), 1 /*is_dwp*/,
13187 0 /*search_cwd*/);
13188 }
13189
13190 return NULL;
13191 }
13192
13193 /* Initialize the use of the DWP file for the current objfile.
13194 By convention the name of the DWP file is ${objfile}.dwp.
13195 The result is NULL if it can't be found. */
13196
13197 static std::unique_ptr<struct dwp_file>
13198 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13199 {
13200 struct objfile *objfile = dwarf2_per_objfile->objfile;
13201
13202 /* Try to find first .dwp for the binary file before any symbolic links
13203 resolving. */
13204
13205 /* If the objfile is a debug file, find the name of the real binary
13206 file and get the name of dwp file from there. */
13207 std::string dwp_name;
13208 if (objfile->separate_debug_objfile_backlink != NULL)
13209 {
13210 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13211 const char *backlink_basename = lbasename (backlink->original_name);
13212
13213 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13214 }
13215 else
13216 dwp_name = objfile->original_name;
13217
13218 dwp_name += ".dwp";
13219
13220 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13221 if (dbfd == NULL
13222 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13223 {
13224 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13225 dwp_name = objfile_name (objfile);
13226 dwp_name += ".dwp";
13227 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13228 }
13229
13230 if (dbfd == NULL)
13231 {
13232 if (dwarf_read_debug)
13233 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13234 return std::unique_ptr<dwp_file> ();
13235 }
13236
13237 const char *name = bfd_get_filename (dbfd.get ());
13238 std::unique_ptr<struct dwp_file> dwp_file
13239 (new struct dwp_file (name, std::move (dbfd)));
13240
13241 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13242 dwp_file->elf_sections =
13243 OBSTACK_CALLOC (&objfile->objfile_obstack,
13244 dwp_file->num_sections, asection *);
13245
13246 bfd_map_over_sections (dwp_file->dbfd.get (),
13247 dwarf2_locate_common_dwp_sections,
13248 dwp_file.get ());
13249
13250 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13251 0);
13252
13253 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13254 1);
13255
13256 /* The DWP file version is stored in the hash table. Oh well. */
13257 if (dwp_file->cus && dwp_file->tus
13258 && dwp_file->cus->version != dwp_file->tus->version)
13259 {
13260 /* Technically speaking, we should try to limp along, but this is
13261 pretty bizarre. We use pulongest here because that's the established
13262 portability solution (e.g, we cannot use %u for uint32_t). */
13263 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13264 " TU version %s [in DWP file %s]"),
13265 pulongest (dwp_file->cus->version),
13266 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13267 }
13268
13269 if (dwp_file->cus)
13270 dwp_file->version = dwp_file->cus->version;
13271 else if (dwp_file->tus)
13272 dwp_file->version = dwp_file->tus->version;
13273 else
13274 dwp_file->version = 2;
13275
13276 if (dwp_file->version == 2)
13277 bfd_map_over_sections (dwp_file->dbfd.get (),
13278 dwarf2_locate_v2_dwp_sections,
13279 dwp_file.get ());
13280
13281 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13282 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13283
13284 if (dwarf_read_debug)
13285 {
13286 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13287 fprintf_unfiltered (gdb_stdlog,
13288 " %s CUs, %s TUs\n",
13289 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13290 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13291 }
13292
13293 return dwp_file;
13294 }
13295
13296 /* Wrapper around open_and_init_dwp_file, only open it once. */
13297
13298 static struct dwp_file *
13299 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13300 {
13301 if (! dwarf2_per_objfile->dwp_checked)
13302 {
13303 dwarf2_per_objfile->dwp_file
13304 = open_and_init_dwp_file (dwarf2_per_objfile);
13305 dwarf2_per_objfile->dwp_checked = 1;
13306 }
13307 return dwarf2_per_objfile->dwp_file.get ();
13308 }
13309
13310 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13311 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13312 or in the DWP file for the objfile, referenced by THIS_UNIT.
13313 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13314 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13315
13316 This is called, for example, when wanting to read a variable with a
13317 complex location. Therefore we don't want to do file i/o for every call.
13318 Therefore we don't want to look for a DWO file on every call.
13319 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13320 then we check if we've already seen DWO_NAME, and only THEN do we check
13321 for a DWO file.
13322
13323 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13324 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13325
13326 static struct dwo_unit *
13327 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13328 const char *dwo_name, const char *comp_dir,
13329 ULONGEST signature, int is_debug_types)
13330 {
13331 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13332 struct objfile *objfile = dwarf2_per_objfile->objfile;
13333 const char *kind = is_debug_types ? "TU" : "CU";
13334 void **dwo_file_slot;
13335 struct dwo_file *dwo_file;
13336 struct dwp_file *dwp_file;
13337
13338 /* First see if there's a DWP file.
13339 If we have a DWP file but didn't find the DWO inside it, don't
13340 look for the original DWO file. It makes gdb behave differently
13341 depending on whether one is debugging in the build tree. */
13342
13343 dwp_file = get_dwp_file (dwarf2_per_objfile);
13344 if (dwp_file != NULL)
13345 {
13346 const struct dwp_hash_table *dwp_htab =
13347 is_debug_types ? dwp_file->tus : dwp_file->cus;
13348
13349 if (dwp_htab != NULL)
13350 {
13351 struct dwo_unit *dwo_cutu =
13352 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13353 signature, is_debug_types);
13354
13355 if (dwo_cutu != NULL)
13356 {
13357 if (dwarf_read_debug)
13358 {
13359 fprintf_unfiltered (gdb_stdlog,
13360 "Virtual DWO %s %s found: @%s\n",
13361 kind, hex_string (signature),
13362 host_address_to_string (dwo_cutu));
13363 }
13364 return dwo_cutu;
13365 }
13366 }
13367 }
13368 else
13369 {
13370 /* No DWP file, look for the DWO file. */
13371
13372 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13373 dwo_name, comp_dir);
13374 if (*dwo_file_slot == NULL)
13375 {
13376 /* Read in the file and build a table of the CUs/TUs it contains. */
13377 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13378 }
13379 /* NOTE: This will be NULL if unable to open the file. */
13380 dwo_file = (struct dwo_file *) *dwo_file_slot;
13381
13382 if (dwo_file != NULL)
13383 {
13384 struct dwo_unit *dwo_cutu = NULL;
13385
13386 if (is_debug_types && dwo_file->tus)
13387 {
13388 struct dwo_unit find_dwo_cutu;
13389
13390 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13391 find_dwo_cutu.signature = signature;
13392 dwo_cutu
13393 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13394 }
13395 else if (!is_debug_types && dwo_file->cus)
13396 {
13397 struct dwo_unit find_dwo_cutu;
13398
13399 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13400 find_dwo_cutu.signature = signature;
13401 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13402 &find_dwo_cutu);
13403 }
13404
13405 if (dwo_cutu != NULL)
13406 {
13407 if (dwarf_read_debug)
13408 {
13409 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13410 kind, dwo_name, hex_string (signature),
13411 host_address_to_string (dwo_cutu));
13412 }
13413 return dwo_cutu;
13414 }
13415 }
13416 }
13417
13418 /* We didn't find it. This could mean a dwo_id mismatch, or
13419 someone deleted the DWO/DWP file, or the search path isn't set up
13420 correctly to find the file. */
13421
13422 if (dwarf_read_debug)
13423 {
13424 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13425 kind, dwo_name, hex_string (signature));
13426 }
13427
13428 /* This is a warning and not a complaint because it can be caused by
13429 pilot error (e.g., user accidentally deleting the DWO). */
13430 {
13431 /* Print the name of the DWP file if we looked there, helps the user
13432 better diagnose the problem. */
13433 std::string dwp_text;
13434
13435 if (dwp_file != NULL)
13436 dwp_text = string_printf (" [in DWP file %s]",
13437 lbasename (dwp_file->name));
13438
13439 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13440 " [in module %s]"),
13441 kind, dwo_name, hex_string (signature),
13442 dwp_text.c_str (),
13443 this_unit->is_debug_types ? "TU" : "CU",
13444 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13445 }
13446 return NULL;
13447 }
13448
13449 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13450 See lookup_dwo_cutu_unit for details. */
13451
13452 static struct dwo_unit *
13453 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13454 const char *dwo_name, const char *comp_dir,
13455 ULONGEST signature)
13456 {
13457 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13458 }
13459
13460 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13461 See lookup_dwo_cutu_unit for details. */
13462
13463 static struct dwo_unit *
13464 lookup_dwo_type_unit (struct signatured_type *this_tu,
13465 const char *dwo_name, const char *comp_dir)
13466 {
13467 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13468 }
13469
13470 /* Traversal function for queue_and_load_all_dwo_tus. */
13471
13472 static int
13473 queue_and_load_dwo_tu (void **slot, void *info)
13474 {
13475 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13476 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13477 ULONGEST signature = dwo_unit->signature;
13478 struct signatured_type *sig_type =
13479 lookup_dwo_signatured_type (per_cu->cu, signature);
13480
13481 if (sig_type != NULL)
13482 {
13483 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13484
13485 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13486 a real dependency of PER_CU on SIG_TYPE. That is detected later
13487 while processing PER_CU. */
13488 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13489 load_full_type_unit (sig_cu);
13490 per_cu->imported_symtabs_push (sig_cu);
13491 }
13492
13493 return 1;
13494 }
13495
13496 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13497 The DWO may have the only definition of the type, though it may not be
13498 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13499 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13500
13501 static void
13502 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13503 {
13504 struct dwo_unit *dwo_unit;
13505 struct dwo_file *dwo_file;
13506
13507 gdb_assert (!per_cu->is_debug_types);
13508 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13509 gdb_assert (per_cu->cu != NULL);
13510
13511 dwo_unit = per_cu->cu->dwo_unit;
13512 gdb_assert (dwo_unit != NULL);
13513
13514 dwo_file = dwo_unit->dwo_file;
13515 if (dwo_file->tus != NULL)
13516 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13517 }
13518
13519 /* Read in various DIEs. */
13520
13521 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13522 Inherit only the children of the DW_AT_abstract_origin DIE not being
13523 already referenced by DW_AT_abstract_origin from the children of the
13524 current DIE. */
13525
13526 static void
13527 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13528 {
13529 struct die_info *child_die;
13530 sect_offset *offsetp;
13531 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13532 struct die_info *origin_die;
13533 /* Iterator of the ORIGIN_DIE children. */
13534 struct die_info *origin_child_die;
13535 struct attribute *attr;
13536 struct dwarf2_cu *origin_cu;
13537 struct pending **origin_previous_list_in_scope;
13538
13539 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13540 if (!attr)
13541 return;
13542
13543 /* Note that following die references may follow to a die in a
13544 different cu. */
13545
13546 origin_cu = cu;
13547 origin_die = follow_die_ref (die, attr, &origin_cu);
13548
13549 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13550 symbols in. */
13551 origin_previous_list_in_scope = origin_cu->list_in_scope;
13552 origin_cu->list_in_scope = cu->list_in_scope;
13553
13554 if (die->tag != origin_die->tag
13555 && !(die->tag == DW_TAG_inlined_subroutine
13556 && origin_die->tag == DW_TAG_subprogram))
13557 complaint (_("DIE %s and its abstract origin %s have different tags"),
13558 sect_offset_str (die->sect_off),
13559 sect_offset_str (origin_die->sect_off));
13560
13561 std::vector<sect_offset> offsets;
13562
13563 for (child_die = die->child;
13564 child_die && child_die->tag;
13565 child_die = sibling_die (child_die))
13566 {
13567 struct die_info *child_origin_die;
13568 struct dwarf2_cu *child_origin_cu;
13569
13570 /* We are trying to process concrete instance entries:
13571 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13572 it's not relevant to our analysis here. i.e. detecting DIEs that are
13573 present in the abstract instance but not referenced in the concrete
13574 one. */
13575 if (child_die->tag == DW_TAG_call_site
13576 || child_die->tag == DW_TAG_GNU_call_site)
13577 continue;
13578
13579 /* For each CHILD_DIE, find the corresponding child of
13580 ORIGIN_DIE. If there is more than one layer of
13581 DW_AT_abstract_origin, follow them all; there shouldn't be,
13582 but GCC versions at least through 4.4 generate this (GCC PR
13583 40573). */
13584 child_origin_die = child_die;
13585 child_origin_cu = cu;
13586 while (1)
13587 {
13588 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13589 child_origin_cu);
13590 if (attr == NULL)
13591 break;
13592 child_origin_die = follow_die_ref (child_origin_die, attr,
13593 &child_origin_cu);
13594 }
13595
13596 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13597 counterpart may exist. */
13598 if (child_origin_die != child_die)
13599 {
13600 if (child_die->tag != child_origin_die->tag
13601 && !(child_die->tag == DW_TAG_inlined_subroutine
13602 && child_origin_die->tag == DW_TAG_subprogram))
13603 complaint (_("Child DIE %s and its abstract origin %s have "
13604 "different tags"),
13605 sect_offset_str (child_die->sect_off),
13606 sect_offset_str (child_origin_die->sect_off));
13607 if (child_origin_die->parent != origin_die)
13608 complaint (_("Child DIE %s and its abstract origin %s have "
13609 "different parents"),
13610 sect_offset_str (child_die->sect_off),
13611 sect_offset_str (child_origin_die->sect_off));
13612 else
13613 offsets.push_back (child_origin_die->sect_off);
13614 }
13615 }
13616 std::sort (offsets.begin (), offsets.end ());
13617 sect_offset *offsets_end = offsets.data () + offsets.size ();
13618 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13619 if (offsetp[-1] == *offsetp)
13620 complaint (_("Multiple children of DIE %s refer "
13621 "to DIE %s as their abstract origin"),
13622 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13623
13624 offsetp = offsets.data ();
13625 origin_child_die = origin_die->child;
13626 while (origin_child_die && origin_child_die->tag)
13627 {
13628 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13629 while (offsetp < offsets_end
13630 && *offsetp < origin_child_die->sect_off)
13631 offsetp++;
13632 if (offsetp >= offsets_end
13633 || *offsetp > origin_child_die->sect_off)
13634 {
13635 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13636 Check whether we're already processing ORIGIN_CHILD_DIE.
13637 This can happen with mutually referenced abstract_origins.
13638 PR 16581. */
13639 if (!origin_child_die->in_process)
13640 process_die (origin_child_die, origin_cu);
13641 }
13642 origin_child_die = sibling_die (origin_child_die);
13643 }
13644 origin_cu->list_in_scope = origin_previous_list_in_scope;
13645 }
13646
13647 static void
13648 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13649 {
13650 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13651 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13652 struct context_stack *newobj;
13653 CORE_ADDR lowpc;
13654 CORE_ADDR highpc;
13655 struct die_info *child_die;
13656 struct attribute *attr, *call_line, *call_file;
13657 const char *name;
13658 CORE_ADDR baseaddr;
13659 struct block *block;
13660 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13661 std::vector<struct symbol *> template_args;
13662 struct template_symbol *templ_func = NULL;
13663
13664 if (inlined_func)
13665 {
13666 /* If we do not have call site information, we can't show the
13667 caller of this inlined function. That's too confusing, so
13668 only use the scope for local variables. */
13669 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13670 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13671 if (call_line == NULL || call_file == NULL)
13672 {
13673 read_lexical_block_scope (die, cu);
13674 return;
13675 }
13676 }
13677
13678 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13679
13680 name = dwarf2_name (die, cu);
13681
13682 /* Ignore functions with missing or empty names. These are actually
13683 illegal according to the DWARF standard. */
13684 if (name == NULL)
13685 {
13686 complaint (_("missing name for subprogram DIE at %s"),
13687 sect_offset_str (die->sect_off));
13688 return;
13689 }
13690
13691 /* Ignore functions with missing or invalid low and high pc attributes. */
13692 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13693 <= PC_BOUNDS_INVALID)
13694 {
13695 attr = dwarf2_attr (die, DW_AT_external, cu);
13696 if (!attr || !DW_UNSND (attr))
13697 complaint (_("cannot get low and high bounds "
13698 "for subprogram DIE at %s"),
13699 sect_offset_str (die->sect_off));
13700 return;
13701 }
13702
13703 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13704 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13705
13706 /* If we have any template arguments, then we must allocate a
13707 different sort of symbol. */
13708 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13709 {
13710 if (child_die->tag == DW_TAG_template_type_param
13711 || child_die->tag == DW_TAG_template_value_param)
13712 {
13713 templ_func = allocate_template_symbol (objfile);
13714 templ_func->subclass = SYMBOL_TEMPLATE;
13715 break;
13716 }
13717 }
13718
13719 newobj = cu->get_builder ()->push_context (0, lowpc);
13720 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13721 (struct symbol *) templ_func);
13722
13723 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13724 set_objfile_main_name (objfile, SYMBOL_LINKAGE_NAME (newobj->name),
13725 cu->language);
13726
13727 /* If there is a location expression for DW_AT_frame_base, record
13728 it. */
13729 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13730 if (attr)
13731 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13732
13733 /* If there is a location for the static link, record it. */
13734 newobj->static_link = NULL;
13735 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13736 if (attr)
13737 {
13738 newobj->static_link
13739 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13740 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13741 dwarf2_per_cu_addr_type (cu->per_cu));
13742 }
13743
13744 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13745
13746 if (die->child != NULL)
13747 {
13748 child_die = die->child;
13749 while (child_die && child_die->tag)
13750 {
13751 if (child_die->tag == DW_TAG_template_type_param
13752 || child_die->tag == DW_TAG_template_value_param)
13753 {
13754 struct symbol *arg = new_symbol (child_die, NULL, cu);
13755
13756 if (arg != NULL)
13757 template_args.push_back (arg);
13758 }
13759 else
13760 process_die (child_die, cu);
13761 child_die = sibling_die (child_die);
13762 }
13763 }
13764
13765 inherit_abstract_dies (die, cu);
13766
13767 /* If we have a DW_AT_specification, we might need to import using
13768 directives from the context of the specification DIE. See the
13769 comment in determine_prefix. */
13770 if (cu->language == language_cplus
13771 && dwarf2_attr (die, DW_AT_specification, cu))
13772 {
13773 struct dwarf2_cu *spec_cu = cu;
13774 struct die_info *spec_die = die_specification (die, &spec_cu);
13775
13776 while (spec_die)
13777 {
13778 child_die = spec_die->child;
13779 while (child_die && child_die->tag)
13780 {
13781 if (child_die->tag == DW_TAG_imported_module)
13782 process_die (child_die, spec_cu);
13783 child_die = sibling_die (child_die);
13784 }
13785
13786 /* In some cases, GCC generates specification DIEs that
13787 themselves contain DW_AT_specification attributes. */
13788 spec_die = die_specification (spec_die, &spec_cu);
13789 }
13790 }
13791
13792 struct context_stack cstk = cu->get_builder ()->pop_context ();
13793 /* Make a block for the local symbols within. */
13794 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13795 cstk.static_link, lowpc, highpc);
13796
13797 /* For C++, set the block's scope. */
13798 if ((cu->language == language_cplus
13799 || cu->language == language_fortran
13800 || cu->language == language_d
13801 || cu->language == language_rust)
13802 && cu->processing_has_namespace_info)
13803 block_set_scope (block, determine_prefix (die, cu),
13804 &objfile->objfile_obstack);
13805
13806 /* If we have address ranges, record them. */
13807 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13808
13809 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13810
13811 /* Attach template arguments to function. */
13812 if (!template_args.empty ())
13813 {
13814 gdb_assert (templ_func != NULL);
13815
13816 templ_func->n_template_arguments = template_args.size ();
13817 templ_func->template_arguments
13818 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13819 templ_func->n_template_arguments);
13820 memcpy (templ_func->template_arguments,
13821 template_args.data (),
13822 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13823
13824 /* Make sure that the symtab is set on the new symbols. Even
13825 though they don't appear in this symtab directly, other parts
13826 of gdb assume that symbols do, and this is reasonably
13827 true. */
13828 for (symbol *sym : template_args)
13829 symbol_set_symtab (sym, symbol_symtab (templ_func));
13830 }
13831
13832 /* In C++, we can have functions nested inside functions (e.g., when
13833 a function declares a class that has methods). This means that
13834 when we finish processing a function scope, we may need to go
13835 back to building a containing block's symbol lists. */
13836 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13837 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13838
13839 /* If we've finished processing a top-level function, subsequent
13840 symbols go in the file symbol list. */
13841 if (cu->get_builder ()->outermost_context_p ())
13842 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13843 }
13844
13845 /* Process all the DIES contained within a lexical block scope. Start
13846 a new scope, process the dies, and then close the scope. */
13847
13848 static void
13849 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13850 {
13851 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13852 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13853 CORE_ADDR lowpc, highpc;
13854 struct die_info *child_die;
13855 CORE_ADDR baseaddr;
13856
13857 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13858
13859 /* Ignore blocks with missing or invalid low and high pc attributes. */
13860 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13861 as multiple lexical blocks? Handling children in a sane way would
13862 be nasty. Might be easier to properly extend generic blocks to
13863 describe ranges. */
13864 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13865 {
13866 case PC_BOUNDS_NOT_PRESENT:
13867 /* DW_TAG_lexical_block has no attributes, process its children as if
13868 there was no wrapping by that DW_TAG_lexical_block.
13869 GCC does no longer produces such DWARF since GCC r224161. */
13870 for (child_die = die->child;
13871 child_die != NULL && child_die->tag;
13872 child_die = sibling_die (child_die))
13873 process_die (child_die, cu);
13874 return;
13875 case PC_BOUNDS_INVALID:
13876 return;
13877 }
13878 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13879 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13880
13881 cu->get_builder ()->push_context (0, lowpc);
13882 if (die->child != NULL)
13883 {
13884 child_die = die->child;
13885 while (child_die && child_die->tag)
13886 {
13887 process_die (child_die, cu);
13888 child_die = sibling_die (child_die);
13889 }
13890 }
13891 inherit_abstract_dies (die, cu);
13892 struct context_stack cstk = cu->get_builder ()->pop_context ();
13893
13894 if (*cu->get_builder ()->get_local_symbols () != NULL
13895 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13896 {
13897 struct block *block
13898 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13899 cstk.start_addr, highpc);
13900
13901 /* Note that recording ranges after traversing children, as we
13902 do here, means that recording a parent's ranges entails
13903 walking across all its children's ranges as they appear in
13904 the address map, which is quadratic behavior.
13905
13906 It would be nicer to record the parent's ranges before
13907 traversing its children, simply overriding whatever you find
13908 there. But since we don't even decide whether to create a
13909 block until after we've traversed its children, that's hard
13910 to do. */
13911 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13912 }
13913 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13914 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13915 }
13916
13917 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13918
13919 static void
13920 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13921 {
13922 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13923 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13924 CORE_ADDR pc, baseaddr;
13925 struct attribute *attr;
13926 struct call_site *call_site, call_site_local;
13927 void **slot;
13928 int nparams;
13929 struct die_info *child_die;
13930
13931 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13932
13933 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13934 if (attr == NULL)
13935 {
13936 /* This was a pre-DWARF-5 GNU extension alias
13937 for DW_AT_call_return_pc. */
13938 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13939 }
13940 if (!attr)
13941 {
13942 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13943 "DIE %s [in module %s]"),
13944 sect_offset_str (die->sect_off), objfile_name (objfile));
13945 return;
13946 }
13947 pc = attr_value_as_address (attr) + baseaddr;
13948 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13949
13950 if (cu->call_site_htab == NULL)
13951 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13952 NULL, &objfile->objfile_obstack,
13953 hashtab_obstack_allocate, NULL);
13954 call_site_local.pc = pc;
13955 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13956 if (*slot != NULL)
13957 {
13958 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13959 "DIE %s [in module %s]"),
13960 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13961 objfile_name (objfile));
13962 return;
13963 }
13964
13965 /* Count parameters at the caller. */
13966
13967 nparams = 0;
13968 for (child_die = die->child; child_die && child_die->tag;
13969 child_die = sibling_die (child_die))
13970 {
13971 if (child_die->tag != DW_TAG_call_site_parameter
13972 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13973 {
13974 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13975 "DW_TAG_call_site child DIE %s [in module %s]"),
13976 child_die->tag, sect_offset_str (child_die->sect_off),
13977 objfile_name (objfile));
13978 continue;
13979 }
13980
13981 nparams++;
13982 }
13983
13984 call_site
13985 = ((struct call_site *)
13986 obstack_alloc (&objfile->objfile_obstack,
13987 sizeof (*call_site)
13988 + (sizeof (*call_site->parameter) * (nparams - 1))));
13989 *slot = call_site;
13990 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13991 call_site->pc = pc;
13992
13993 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13994 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13995 {
13996 struct die_info *func_die;
13997
13998 /* Skip also over DW_TAG_inlined_subroutine. */
13999 for (func_die = die->parent;
14000 func_die && func_die->tag != DW_TAG_subprogram
14001 && func_die->tag != DW_TAG_subroutine_type;
14002 func_die = func_die->parent);
14003
14004 /* DW_AT_call_all_calls is a superset
14005 of DW_AT_call_all_tail_calls. */
14006 if (func_die
14007 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14008 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14009 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14010 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14011 {
14012 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14013 not complete. But keep CALL_SITE for look ups via call_site_htab,
14014 both the initial caller containing the real return address PC and
14015 the final callee containing the current PC of a chain of tail
14016 calls do not need to have the tail call list complete. But any
14017 function candidate for a virtual tail call frame searched via
14018 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14019 determined unambiguously. */
14020 }
14021 else
14022 {
14023 struct type *func_type = NULL;
14024
14025 if (func_die)
14026 func_type = get_die_type (func_die, cu);
14027 if (func_type != NULL)
14028 {
14029 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14030
14031 /* Enlist this call site to the function. */
14032 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14033 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14034 }
14035 else
14036 complaint (_("Cannot find function owning DW_TAG_call_site "
14037 "DIE %s [in module %s]"),
14038 sect_offset_str (die->sect_off), objfile_name (objfile));
14039 }
14040 }
14041
14042 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14043 if (attr == NULL)
14044 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14045 if (attr == NULL)
14046 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14047 if (attr == NULL)
14048 {
14049 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14050 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14051 }
14052 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14053 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14054 /* Keep NULL DWARF_BLOCK. */;
14055 else if (attr_form_is_block (attr))
14056 {
14057 struct dwarf2_locexpr_baton *dlbaton;
14058
14059 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14060 dlbaton->data = DW_BLOCK (attr)->data;
14061 dlbaton->size = DW_BLOCK (attr)->size;
14062 dlbaton->per_cu = cu->per_cu;
14063
14064 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14065 }
14066 else if (attr_form_is_ref (attr))
14067 {
14068 struct dwarf2_cu *target_cu = cu;
14069 struct die_info *target_die;
14070
14071 target_die = follow_die_ref (die, attr, &target_cu);
14072 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14073 if (die_is_declaration (target_die, target_cu))
14074 {
14075 const char *target_physname;
14076
14077 /* Prefer the mangled name; otherwise compute the demangled one. */
14078 target_physname = dw2_linkage_name (target_die, target_cu);
14079 if (target_physname == NULL)
14080 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14081 if (target_physname == NULL)
14082 complaint (_("DW_AT_call_target target DIE has invalid "
14083 "physname, for referencing DIE %s [in module %s]"),
14084 sect_offset_str (die->sect_off), objfile_name (objfile));
14085 else
14086 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14087 }
14088 else
14089 {
14090 CORE_ADDR lowpc;
14091
14092 /* DW_AT_entry_pc should be preferred. */
14093 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14094 <= PC_BOUNDS_INVALID)
14095 complaint (_("DW_AT_call_target target DIE has invalid "
14096 "low pc, for referencing DIE %s [in module %s]"),
14097 sect_offset_str (die->sect_off), objfile_name (objfile));
14098 else
14099 {
14100 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14101 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14102 }
14103 }
14104 }
14105 else
14106 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14107 "block nor reference, for DIE %s [in module %s]"),
14108 sect_offset_str (die->sect_off), objfile_name (objfile));
14109
14110 call_site->per_cu = cu->per_cu;
14111
14112 for (child_die = die->child;
14113 child_die && child_die->tag;
14114 child_die = sibling_die (child_die))
14115 {
14116 struct call_site_parameter *parameter;
14117 struct attribute *loc, *origin;
14118
14119 if (child_die->tag != DW_TAG_call_site_parameter
14120 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14121 {
14122 /* Already printed the complaint above. */
14123 continue;
14124 }
14125
14126 gdb_assert (call_site->parameter_count < nparams);
14127 parameter = &call_site->parameter[call_site->parameter_count];
14128
14129 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14130 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14131 register is contained in DW_AT_call_value. */
14132
14133 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14134 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14135 if (origin == NULL)
14136 {
14137 /* This was a pre-DWARF-5 GNU extension alias
14138 for DW_AT_call_parameter. */
14139 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14140 }
14141 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14142 {
14143 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14144
14145 sect_offset sect_off
14146 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14147 if (!offset_in_cu_p (&cu->header, sect_off))
14148 {
14149 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14150 binding can be done only inside one CU. Such referenced DIE
14151 therefore cannot be even moved to DW_TAG_partial_unit. */
14152 complaint (_("DW_AT_call_parameter offset is not in CU for "
14153 "DW_TAG_call_site child DIE %s [in module %s]"),
14154 sect_offset_str (child_die->sect_off),
14155 objfile_name (objfile));
14156 continue;
14157 }
14158 parameter->u.param_cu_off
14159 = (cu_offset) (sect_off - cu->header.sect_off);
14160 }
14161 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14162 {
14163 complaint (_("No DW_FORM_block* DW_AT_location for "
14164 "DW_TAG_call_site child DIE %s [in module %s]"),
14165 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14166 continue;
14167 }
14168 else
14169 {
14170 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14171 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14172 if (parameter->u.dwarf_reg != -1)
14173 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14174 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14175 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14176 &parameter->u.fb_offset))
14177 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14178 else
14179 {
14180 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14181 "for DW_FORM_block* DW_AT_location is supported for "
14182 "DW_TAG_call_site child DIE %s "
14183 "[in module %s]"),
14184 sect_offset_str (child_die->sect_off),
14185 objfile_name (objfile));
14186 continue;
14187 }
14188 }
14189
14190 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14191 if (attr == NULL)
14192 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14193 if (!attr_form_is_block (attr))
14194 {
14195 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14196 "DW_TAG_call_site child DIE %s [in module %s]"),
14197 sect_offset_str (child_die->sect_off),
14198 objfile_name (objfile));
14199 continue;
14200 }
14201 parameter->value = DW_BLOCK (attr)->data;
14202 parameter->value_size = DW_BLOCK (attr)->size;
14203
14204 /* Parameters are not pre-cleared by memset above. */
14205 parameter->data_value = NULL;
14206 parameter->data_value_size = 0;
14207 call_site->parameter_count++;
14208
14209 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14210 if (attr == NULL)
14211 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14212 if (attr)
14213 {
14214 if (!attr_form_is_block (attr))
14215 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14216 "DW_TAG_call_site child DIE %s [in module %s]"),
14217 sect_offset_str (child_die->sect_off),
14218 objfile_name (objfile));
14219 else
14220 {
14221 parameter->data_value = DW_BLOCK (attr)->data;
14222 parameter->data_value_size = DW_BLOCK (attr)->size;
14223 }
14224 }
14225 }
14226 }
14227
14228 /* Helper function for read_variable. If DIE represents a virtual
14229 table, then return the type of the concrete object that is
14230 associated with the virtual table. Otherwise, return NULL. */
14231
14232 static struct type *
14233 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14234 {
14235 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14236 if (attr == NULL)
14237 return NULL;
14238
14239 /* Find the type DIE. */
14240 struct die_info *type_die = NULL;
14241 struct dwarf2_cu *type_cu = cu;
14242
14243 if (attr_form_is_ref (attr))
14244 type_die = follow_die_ref (die, attr, &type_cu);
14245 if (type_die == NULL)
14246 return NULL;
14247
14248 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14249 return NULL;
14250 return die_containing_type (type_die, type_cu);
14251 }
14252
14253 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14254
14255 static void
14256 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14257 {
14258 struct rust_vtable_symbol *storage = NULL;
14259
14260 if (cu->language == language_rust)
14261 {
14262 struct type *containing_type = rust_containing_type (die, cu);
14263
14264 if (containing_type != NULL)
14265 {
14266 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14267
14268 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14269 struct rust_vtable_symbol);
14270 initialize_objfile_symbol (storage);
14271 storage->concrete_type = containing_type;
14272 storage->subclass = SYMBOL_RUST_VTABLE;
14273 }
14274 }
14275
14276 struct symbol *res = new_symbol (die, NULL, cu, storage);
14277 struct attribute *abstract_origin
14278 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14279 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14280 if (res == NULL && loc && abstract_origin)
14281 {
14282 /* We have a variable without a name, but with a location and an abstract
14283 origin. This may be a concrete instance of an abstract variable
14284 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14285 later. */
14286 struct dwarf2_cu *origin_cu = cu;
14287 struct die_info *origin_die
14288 = follow_die_ref (die, abstract_origin, &origin_cu);
14289 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
14290 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
14291 }
14292 }
14293
14294 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14295 reading .debug_rnglists.
14296 Callback's type should be:
14297 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14298 Return true if the attributes are present and valid, otherwise,
14299 return false. */
14300
14301 template <typename Callback>
14302 static bool
14303 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14304 Callback &&callback)
14305 {
14306 struct dwarf2_per_objfile *dwarf2_per_objfile
14307 = cu->per_cu->dwarf2_per_objfile;
14308 struct objfile *objfile = dwarf2_per_objfile->objfile;
14309 bfd *obfd = objfile->obfd;
14310 /* Base address selection entry. */
14311 CORE_ADDR base;
14312 int found_base;
14313 const gdb_byte *buffer;
14314 CORE_ADDR baseaddr;
14315 bool overflow = false;
14316
14317 found_base = cu->base_known;
14318 base = cu->base_address;
14319
14320 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14321 if (offset >= dwarf2_per_objfile->rnglists.size)
14322 {
14323 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14324 offset);
14325 return false;
14326 }
14327 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14328
14329 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14330
14331 while (1)
14332 {
14333 /* Initialize it due to a false compiler warning. */
14334 CORE_ADDR range_beginning = 0, range_end = 0;
14335 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14336 + dwarf2_per_objfile->rnglists.size);
14337 unsigned int bytes_read;
14338
14339 if (buffer == buf_end)
14340 {
14341 overflow = true;
14342 break;
14343 }
14344 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14345 switch (rlet)
14346 {
14347 case DW_RLE_end_of_list:
14348 break;
14349 case DW_RLE_base_address:
14350 if (buffer + cu->header.addr_size > buf_end)
14351 {
14352 overflow = true;
14353 break;
14354 }
14355 base = read_address (obfd, buffer, cu, &bytes_read);
14356 found_base = 1;
14357 buffer += bytes_read;
14358 break;
14359 case DW_RLE_start_length:
14360 if (buffer + cu->header.addr_size > buf_end)
14361 {
14362 overflow = true;
14363 break;
14364 }
14365 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14366 buffer += bytes_read;
14367 range_end = (range_beginning
14368 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14369 buffer += bytes_read;
14370 if (buffer > buf_end)
14371 {
14372 overflow = true;
14373 break;
14374 }
14375 break;
14376 case DW_RLE_offset_pair:
14377 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14378 buffer += bytes_read;
14379 if (buffer > buf_end)
14380 {
14381 overflow = true;
14382 break;
14383 }
14384 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14385 buffer += bytes_read;
14386 if (buffer > buf_end)
14387 {
14388 overflow = true;
14389 break;
14390 }
14391 break;
14392 case DW_RLE_start_end:
14393 if (buffer + 2 * cu->header.addr_size > buf_end)
14394 {
14395 overflow = true;
14396 break;
14397 }
14398 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14399 buffer += bytes_read;
14400 range_end = read_address (obfd, buffer, cu, &bytes_read);
14401 buffer += bytes_read;
14402 break;
14403 default:
14404 complaint (_("Invalid .debug_rnglists data (no base address)"));
14405 return false;
14406 }
14407 if (rlet == DW_RLE_end_of_list || overflow)
14408 break;
14409 if (rlet == DW_RLE_base_address)
14410 continue;
14411
14412 if (!found_base)
14413 {
14414 /* We have no valid base address for the ranges
14415 data. */
14416 complaint (_("Invalid .debug_rnglists data (no base address)"));
14417 return false;
14418 }
14419
14420 if (range_beginning > range_end)
14421 {
14422 /* Inverted range entries are invalid. */
14423 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14424 return false;
14425 }
14426
14427 /* Empty range entries have no effect. */
14428 if (range_beginning == range_end)
14429 continue;
14430
14431 range_beginning += base;
14432 range_end += base;
14433
14434 /* A not-uncommon case of bad debug info.
14435 Don't pollute the addrmap with bad data. */
14436 if (range_beginning + baseaddr == 0
14437 && !dwarf2_per_objfile->has_section_at_zero)
14438 {
14439 complaint (_(".debug_rnglists entry has start address of zero"
14440 " [in module %s]"), objfile_name (objfile));
14441 continue;
14442 }
14443
14444 callback (range_beginning, range_end);
14445 }
14446
14447 if (overflow)
14448 {
14449 complaint (_("Offset %d is not terminated "
14450 "for DW_AT_ranges attribute"),
14451 offset);
14452 return false;
14453 }
14454
14455 return true;
14456 }
14457
14458 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14459 Callback's type should be:
14460 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14461 Return 1 if the attributes are present and valid, otherwise, return 0. */
14462
14463 template <typename Callback>
14464 static int
14465 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14466 Callback &&callback)
14467 {
14468 struct dwarf2_per_objfile *dwarf2_per_objfile
14469 = cu->per_cu->dwarf2_per_objfile;
14470 struct objfile *objfile = dwarf2_per_objfile->objfile;
14471 struct comp_unit_head *cu_header = &cu->header;
14472 bfd *obfd = objfile->obfd;
14473 unsigned int addr_size = cu_header->addr_size;
14474 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14475 /* Base address selection entry. */
14476 CORE_ADDR base;
14477 int found_base;
14478 unsigned int dummy;
14479 const gdb_byte *buffer;
14480 CORE_ADDR baseaddr;
14481
14482 if (cu_header->version >= 5)
14483 return dwarf2_rnglists_process (offset, cu, callback);
14484
14485 found_base = cu->base_known;
14486 base = cu->base_address;
14487
14488 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14489 if (offset >= dwarf2_per_objfile->ranges.size)
14490 {
14491 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14492 offset);
14493 return 0;
14494 }
14495 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14496
14497 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14498
14499 while (1)
14500 {
14501 CORE_ADDR range_beginning, range_end;
14502
14503 range_beginning = read_address (obfd, buffer, cu, &dummy);
14504 buffer += addr_size;
14505 range_end = read_address (obfd, buffer, cu, &dummy);
14506 buffer += addr_size;
14507 offset += 2 * addr_size;
14508
14509 /* An end of list marker is a pair of zero addresses. */
14510 if (range_beginning == 0 && range_end == 0)
14511 /* Found the end of list entry. */
14512 break;
14513
14514 /* Each base address selection entry is a pair of 2 values.
14515 The first is the largest possible address, the second is
14516 the base address. Check for a base address here. */
14517 if ((range_beginning & mask) == mask)
14518 {
14519 /* If we found the largest possible address, then we already
14520 have the base address in range_end. */
14521 base = range_end;
14522 found_base = 1;
14523 continue;
14524 }
14525
14526 if (!found_base)
14527 {
14528 /* We have no valid base address for the ranges
14529 data. */
14530 complaint (_("Invalid .debug_ranges data (no base address)"));
14531 return 0;
14532 }
14533
14534 if (range_beginning > range_end)
14535 {
14536 /* Inverted range entries are invalid. */
14537 complaint (_("Invalid .debug_ranges data (inverted range)"));
14538 return 0;
14539 }
14540
14541 /* Empty range entries have no effect. */
14542 if (range_beginning == range_end)
14543 continue;
14544
14545 range_beginning += base;
14546 range_end += base;
14547
14548 /* A not-uncommon case of bad debug info.
14549 Don't pollute the addrmap with bad data. */
14550 if (range_beginning + baseaddr == 0
14551 && !dwarf2_per_objfile->has_section_at_zero)
14552 {
14553 complaint (_(".debug_ranges entry has start address of zero"
14554 " [in module %s]"), objfile_name (objfile));
14555 continue;
14556 }
14557
14558 callback (range_beginning, range_end);
14559 }
14560
14561 return 1;
14562 }
14563
14564 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14565 Return 1 if the attributes are present and valid, otherwise, return 0.
14566 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14567
14568 static int
14569 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14570 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14571 struct partial_symtab *ranges_pst)
14572 {
14573 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14574 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14575 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14576 SECT_OFF_TEXT (objfile));
14577 int low_set = 0;
14578 CORE_ADDR low = 0;
14579 CORE_ADDR high = 0;
14580 int retval;
14581
14582 retval = dwarf2_ranges_process (offset, cu,
14583 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14584 {
14585 if (ranges_pst != NULL)
14586 {
14587 CORE_ADDR lowpc;
14588 CORE_ADDR highpc;
14589
14590 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14591 range_beginning + baseaddr)
14592 - baseaddr);
14593 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14594 range_end + baseaddr)
14595 - baseaddr);
14596 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14597 lowpc, highpc - 1, ranges_pst);
14598 }
14599
14600 /* FIXME: This is recording everything as a low-high
14601 segment of consecutive addresses. We should have a
14602 data structure for discontiguous block ranges
14603 instead. */
14604 if (! low_set)
14605 {
14606 low = range_beginning;
14607 high = range_end;
14608 low_set = 1;
14609 }
14610 else
14611 {
14612 if (range_beginning < low)
14613 low = range_beginning;
14614 if (range_end > high)
14615 high = range_end;
14616 }
14617 });
14618 if (!retval)
14619 return 0;
14620
14621 if (! low_set)
14622 /* If the first entry is an end-of-list marker, the range
14623 describes an empty scope, i.e. no instructions. */
14624 return 0;
14625
14626 if (low_return)
14627 *low_return = low;
14628 if (high_return)
14629 *high_return = high;
14630 return 1;
14631 }
14632
14633 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14634 definition for the return value. *LOWPC and *HIGHPC are set iff
14635 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14636
14637 static enum pc_bounds_kind
14638 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14639 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14640 struct partial_symtab *pst)
14641 {
14642 struct dwarf2_per_objfile *dwarf2_per_objfile
14643 = cu->per_cu->dwarf2_per_objfile;
14644 struct attribute *attr;
14645 struct attribute *attr_high;
14646 CORE_ADDR low = 0;
14647 CORE_ADDR high = 0;
14648 enum pc_bounds_kind ret;
14649
14650 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14651 if (attr_high)
14652 {
14653 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14654 if (attr)
14655 {
14656 low = attr_value_as_address (attr);
14657 high = attr_value_as_address (attr_high);
14658 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14659 high += low;
14660 }
14661 else
14662 /* Found high w/o low attribute. */
14663 return PC_BOUNDS_INVALID;
14664
14665 /* Found consecutive range of addresses. */
14666 ret = PC_BOUNDS_HIGH_LOW;
14667 }
14668 else
14669 {
14670 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14671 if (attr != NULL)
14672 {
14673 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14674 We take advantage of the fact that DW_AT_ranges does not appear
14675 in DW_TAG_compile_unit of DWO files. */
14676 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14677 unsigned int ranges_offset = (DW_UNSND (attr)
14678 + (need_ranges_base
14679 ? cu->ranges_base
14680 : 0));
14681
14682 /* Value of the DW_AT_ranges attribute is the offset in the
14683 .debug_ranges section. */
14684 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14685 return PC_BOUNDS_INVALID;
14686 /* Found discontinuous range of addresses. */
14687 ret = PC_BOUNDS_RANGES;
14688 }
14689 else
14690 return PC_BOUNDS_NOT_PRESENT;
14691 }
14692
14693 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14694 if (high <= low)
14695 return PC_BOUNDS_INVALID;
14696
14697 /* When using the GNU linker, .gnu.linkonce. sections are used to
14698 eliminate duplicate copies of functions and vtables and such.
14699 The linker will arbitrarily choose one and discard the others.
14700 The AT_*_pc values for such functions refer to local labels in
14701 these sections. If the section from that file was discarded, the
14702 labels are not in the output, so the relocs get a value of 0.
14703 If this is a discarded function, mark the pc bounds as invalid,
14704 so that GDB will ignore it. */
14705 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14706 return PC_BOUNDS_INVALID;
14707
14708 *lowpc = low;
14709 if (highpc)
14710 *highpc = high;
14711 return ret;
14712 }
14713
14714 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14715 its low and high PC addresses. Do nothing if these addresses could not
14716 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14717 and HIGHPC to the high address if greater than HIGHPC. */
14718
14719 static void
14720 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14721 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14722 struct dwarf2_cu *cu)
14723 {
14724 CORE_ADDR low, high;
14725 struct die_info *child = die->child;
14726
14727 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14728 {
14729 *lowpc = std::min (*lowpc, low);
14730 *highpc = std::max (*highpc, high);
14731 }
14732
14733 /* If the language does not allow nested subprograms (either inside
14734 subprograms or lexical blocks), we're done. */
14735 if (cu->language != language_ada)
14736 return;
14737
14738 /* Check all the children of the given DIE. If it contains nested
14739 subprograms, then check their pc bounds. Likewise, we need to
14740 check lexical blocks as well, as they may also contain subprogram
14741 definitions. */
14742 while (child && child->tag)
14743 {
14744 if (child->tag == DW_TAG_subprogram
14745 || child->tag == DW_TAG_lexical_block)
14746 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14747 child = sibling_die (child);
14748 }
14749 }
14750
14751 /* Get the low and high pc's represented by the scope DIE, and store
14752 them in *LOWPC and *HIGHPC. If the correct values can't be
14753 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14754
14755 static void
14756 get_scope_pc_bounds (struct die_info *die,
14757 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14758 struct dwarf2_cu *cu)
14759 {
14760 CORE_ADDR best_low = (CORE_ADDR) -1;
14761 CORE_ADDR best_high = (CORE_ADDR) 0;
14762 CORE_ADDR current_low, current_high;
14763
14764 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14765 >= PC_BOUNDS_RANGES)
14766 {
14767 best_low = current_low;
14768 best_high = current_high;
14769 }
14770 else
14771 {
14772 struct die_info *child = die->child;
14773
14774 while (child && child->tag)
14775 {
14776 switch (child->tag) {
14777 case DW_TAG_subprogram:
14778 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14779 break;
14780 case DW_TAG_namespace:
14781 case DW_TAG_module:
14782 /* FIXME: carlton/2004-01-16: Should we do this for
14783 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14784 that current GCC's always emit the DIEs corresponding
14785 to definitions of methods of classes as children of a
14786 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14787 the DIEs giving the declarations, which could be
14788 anywhere). But I don't see any reason why the
14789 standards says that they have to be there. */
14790 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14791
14792 if (current_low != ((CORE_ADDR) -1))
14793 {
14794 best_low = std::min (best_low, current_low);
14795 best_high = std::max (best_high, current_high);
14796 }
14797 break;
14798 default:
14799 /* Ignore. */
14800 break;
14801 }
14802
14803 child = sibling_die (child);
14804 }
14805 }
14806
14807 *lowpc = best_low;
14808 *highpc = best_high;
14809 }
14810
14811 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14812 in DIE. */
14813
14814 static void
14815 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14816 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14817 {
14818 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14819 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14820 struct attribute *attr;
14821 struct attribute *attr_high;
14822
14823 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14824 if (attr_high)
14825 {
14826 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14827 if (attr)
14828 {
14829 CORE_ADDR low = attr_value_as_address (attr);
14830 CORE_ADDR high = attr_value_as_address (attr_high);
14831
14832 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14833 high += low;
14834
14835 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14836 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14837 cu->get_builder ()->record_block_range (block, low, high - 1);
14838 }
14839 }
14840
14841 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14842 if (attr)
14843 {
14844 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14845 We take advantage of the fact that DW_AT_ranges does not appear
14846 in DW_TAG_compile_unit of DWO files. */
14847 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14848
14849 /* The value of the DW_AT_ranges attribute is the offset of the
14850 address range list in the .debug_ranges section. */
14851 unsigned long offset = (DW_UNSND (attr)
14852 + (need_ranges_base ? cu->ranges_base : 0));
14853
14854 std::vector<blockrange> blockvec;
14855 dwarf2_ranges_process (offset, cu,
14856 [&] (CORE_ADDR start, CORE_ADDR end)
14857 {
14858 start += baseaddr;
14859 end += baseaddr;
14860 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14861 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14862 cu->get_builder ()->record_block_range (block, start, end - 1);
14863 blockvec.emplace_back (start, end);
14864 });
14865
14866 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14867 }
14868 }
14869
14870 /* Check whether the producer field indicates either of GCC < 4.6, or the
14871 Intel C/C++ compiler, and cache the result in CU. */
14872
14873 static void
14874 check_producer (struct dwarf2_cu *cu)
14875 {
14876 int major, minor;
14877
14878 if (cu->producer == NULL)
14879 {
14880 /* For unknown compilers expect their behavior is DWARF version
14881 compliant.
14882
14883 GCC started to support .debug_types sections by -gdwarf-4 since
14884 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14885 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14886 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14887 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14888 }
14889 else if (producer_is_gcc (cu->producer, &major, &minor))
14890 {
14891 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14892 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14893 }
14894 else if (producer_is_icc (cu->producer, &major, &minor))
14895 {
14896 cu->producer_is_icc = true;
14897 cu->producer_is_icc_lt_14 = major < 14;
14898 }
14899 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14900 cu->producer_is_codewarrior = true;
14901 else
14902 {
14903 /* For other non-GCC compilers, expect their behavior is DWARF version
14904 compliant. */
14905 }
14906
14907 cu->checked_producer = true;
14908 }
14909
14910 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14911 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14912 during 4.6.0 experimental. */
14913
14914 static bool
14915 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14916 {
14917 if (!cu->checked_producer)
14918 check_producer (cu);
14919
14920 return cu->producer_is_gxx_lt_4_6;
14921 }
14922
14923
14924 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14925 with incorrect is_stmt attributes. */
14926
14927 static bool
14928 producer_is_codewarrior (struct dwarf2_cu *cu)
14929 {
14930 if (!cu->checked_producer)
14931 check_producer (cu);
14932
14933 return cu->producer_is_codewarrior;
14934 }
14935
14936 /* Return the default accessibility type if it is not overriden by
14937 DW_AT_accessibility. */
14938
14939 static enum dwarf_access_attribute
14940 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14941 {
14942 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14943 {
14944 /* The default DWARF 2 accessibility for members is public, the default
14945 accessibility for inheritance is private. */
14946
14947 if (die->tag != DW_TAG_inheritance)
14948 return DW_ACCESS_public;
14949 else
14950 return DW_ACCESS_private;
14951 }
14952 else
14953 {
14954 /* DWARF 3+ defines the default accessibility a different way. The same
14955 rules apply now for DW_TAG_inheritance as for the members and it only
14956 depends on the container kind. */
14957
14958 if (die->parent->tag == DW_TAG_class_type)
14959 return DW_ACCESS_private;
14960 else
14961 return DW_ACCESS_public;
14962 }
14963 }
14964
14965 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14966 offset. If the attribute was not found return 0, otherwise return
14967 1. If it was found but could not properly be handled, set *OFFSET
14968 to 0. */
14969
14970 static int
14971 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14972 LONGEST *offset)
14973 {
14974 struct attribute *attr;
14975
14976 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14977 if (attr != NULL)
14978 {
14979 *offset = 0;
14980
14981 /* Note that we do not check for a section offset first here.
14982 This is because DW_AT_data_member_location is new in DWARF 4,
14983 so if we see it, we can assume that a constant form is really
14984 a constant and not a section offset. */
14985 if (attr_form_is_constant (attr))
14986 *offset = dwarf2_get_attr_constant_value (attr, 0);
14987 else if (attr_form_is_section_offset (attr))
14988 dwarf2_complex_location_expr_complaint ();
14989 else if (attr_form_is_block (attr))
14990 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14991 else
14992 dwarf2_complex_location_expr_complaint ();
14993
14994 return 1;
14995 }
14996
14997 return 0;
14998 }
14999
15000 /* Add an aggregate field to the field list. */
15001
15002 static void
15003 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15004 struct dwarf2_cu *cu)
15005 {
15006 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15007 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15008 struct nextfield *new_field;
15009 struct attribute *attr;
15010 struct field *fp;
15011 const char *fieldname = "";
15012
15013 if (die->tag == DW_TAG_inheritance)
15014 {
15015 fip->baseclasses.emplace_back ();
15016 new_field = &fip->baseclasses.back ();
15017 }
15018 else
15019 {
15020 fip->fields.emplace_back ();
15021 new_field = &fip->fields.back ();
15022 }
15023
15024 fip->nfields++;
15025
15026 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15027 if (attr)
15028 new_field->accessibility = DW_UNSND (attr);
15029 else
15030 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15031 if (new_field->accessibility != DW_ACCESS_public)
15032 fip->non_public_fields = 1;
15033
15034 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15035 if (attr)
15036 new_field->virtuality = DW_UNSND (attr);
15037 else
15038 new_field->virtuality = DW_VIRTUALITY_none;
15039
15040 fp = &new_field->field;
15041
15042 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15043 {
15044 LONGEST offset;
15045
15046 /* Data member other than a C++ static data member. */
15047
15048 /* Get type of field. */
15049 fp->type = die_type (die, cu);
15050
15051 SET_FIELD_BITPOS (*fp, 0);
15052
15053 /* Get bit size of field (zero if none). */
15054 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15055 if (attr)
15056 {
15057 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15058 }
15059 else
15060 {
15061 FIELD_BITSIZE (*fp) = 0;
15062 }
15063
15064 /* Get bit offset of field. */
15065 if (handle_data_member_location (die, cu, &offset))
15066 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15067 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15068 if (attr)
15069 {
15070 if (gdbarch_bits_big_endian (gdbarch))
15071 {
15072 /* For big endian bits, the DW_AT_bit_offset gives the
15073 additional bit offset from the MSB of the containing
15074 anonymous object to the MSB of the field. We don't
15075 have to do anything special since we don't need to
15076 know the size of the anonymous object. */
15077 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15078 }
15079 else
15080 {
15081 /* For little endian bits, compute the bit offset to the
15082 MSB of the anonymous object, subtract off the number of
15083 bits from the MSB of the field to the MSB of the
15084 object, and then subtract off the number of bits of
15085 the field itself. The result is the bit offset of
15086 the LSB of the field. */
15087 int anonymous_size;
15088 int bit_offset = DW_UNSND (attr);
15089
15090 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15091 if (attr)
15092 {
15093 /* The size of the anonymous object containing
15094 the bit field is explicit, so use the
15095 indicated size (in bytes). */
15096 anonymous_size = DW_UNSND (attr);
15097 }
15098 else
15099 {
15100 /* The size of the anonymous object containing
15101 the bit field must be inferred from the type
15102 attribute of the data member containing the
15103 bit field. */
15104 anonymous_size = TYPE_LENGTH (fp->type);
15105 }
15106 SET_FIELD_BITPOS (*fp,
15107 (FIELD_BITPOS (*fp)
15108 + anonymous_size * bits_per_byte
15109 - bit_offset - FIELD_BITSIZE (*fp)));
15110 }
15111 }
15112 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15113 if (attr != NULL)
15114 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15115 + dwarf2_get_attr_constant_value (attr, 0)));
15116
15117 /* Get name of field. */
15118 fieldname = dwarf2_name (die, cu);
15119 if (fieldname == NULL)
15120 fieldname = "";
15121
15122 /* The name is already allocated along with this objfile, so we don't
15123 need to duplicate it for the type. */
15124 fp->name = fieldname;
15125
15126 /* Change accessibility for artificial fields (e.g. virtual table
15127 pointer or virtual base class pointer) to private. */
15128 if (dwarf2_attr (die, DW_AT_artificial, cu))
15129 {
15130 FIELD_ARTIFICIAL (*fp) = 1;
15131 new_field->accessibility = DW_ACCESS_private;
15132 fip->non_public_fields = 1;
15133 }
15134 }
15135 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15136 {
15137 /* C++ static member. */
15138
15139 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15140 is a declaration, but all versions of G++ as of this writing
15141 (so through at least 3.2.1) incorrectly generate
15142 DW_TAG_variable tags. */
15143
15144 const char *physname;
15145
15146 /* Get name of field. */
15147 fieldname = dwarf2_name (die, cu);
15148 if (fieldname == NULL)
15149 return;
15150
15151 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15152 if (attr
15153 /* Only create a symbol if this is an external value.
15154 new_symbol checks this and puts the value in the global symbol
15155 table, which we want. If it is not external, new_symbol
15156 will try to put the value in cu->list_in_scope which is wrong. */
15157 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15158 {
15159 /* A static const member, not much different than an enum as far as
15160 we're concerned, except that we can support more types. */
15161 new_symbol (die, NULL, cu);
15162 }
15163
15164 /* Get physical name. */
15165 physname = dwarf2_physname (fieldname, die, cu);
15166
15167 /* The name is already allocated along with this objfile, so we don't
15168 need to duplicate it for the type. */
15169 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15170 FIELD_TYPE (*fp) = die_type (die, cu);
15171 FIELD_NAME (*fp) = fieldname;
15172 }
15173 else if (die->tag == DW_TAG_inheritance)
15174 {
15175 LONGEST offset;
15176
15177 /* C++ base class field. */
15178 if (handle_data_member_location (die, cu, &offset))
15179 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15180 FIELD_BITSIZE (*fp) = 0;
15181 FIELD_TYPE (*fp) = die_type (die, cu);
15182 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
15183 }
15184 else if (die->tag == DW_TAG_variant_part)
15185 {
15186 /* process_structure_scope will treat this DIE as a union. */
15187 process_structure_scope (die, cu);
15188
15189 /* The variant part is relative to the start of the enclosing
15190 structure. */
15191 SET_FIELD_BITPOS (*fp, 0);
15192 fp->type = get_die_type (die, cu);
15193 fp->artificial = 1;
15194 fp->name = "<<variant>>";
15195
15196 /* Normally a DW_TAG_variant_part won't have a size, but our
15197 representation requires one, so set it to the maximum of the
15198 child sizes. */
15199 if (TYPE_LENGTH (fp->type) == 0)
15200 {
15201 unsigned max = 0;
15202 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
15203 if (TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)) > max)
15204 max = TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i));
15205 TYPE_LENGTH (fp->type) = max;
15206 }
15207 }
15208 else
15209 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15210 }
15211
15212 /* Can the type given by DIE define another type? */
15213
15214 static bool
15215 type_can_define_types (const struct die_info *die)
15216 {
15217 switch (die->tag)
15218 {
15219 case DW_TAG_typedef:
15220 case DW_TAG_class_type:
15221 case DW_TAG_structure_type:
15222 case DW_TAG_union_type:
15223 case DW_TAG_enumeration_type:
15224 return true;
15225
15226 default:
15227 return false;
15228 }
15229 }
15230
15231 /* Add a type definition defined in the scope of the FIP's class. */
15232
15233 static void
15234 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15235 struct dwarf2_cu *cu)
15236 {
15237 struct decl_field fp;
15238 memset (&fp, 0, sizeof (fp));
15239
15240 gdb_assert (type_can_define_types (die));
15241
15242 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15243 fp.name = dwarf2_name (die, cu);
15244 fp.type = read_type_die (die, cu);
15245
15246 /* Save accessibility. */
15247 enum dwarf_access_attribute accessibility;
15248 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15249 if (attr != NULL)
15250 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15251 else
15252 accessibility = dwarf2_default_access_attribute (die, cu);
15253 switch (accessibility)
15254 {
15255 case DW_ACCESS_public:
15256 /* The assumed value if neither private nor protected. */
15257 break;
15258 case DW_ACCESS_private:
15259 fp.is_private = 1;
15260 break;
15261 case DW_ACCESS_protected:
15262 fp.is_protected = 1;
15263 break;
15264 default:
15265 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15266 }
15267
15268 if (die->tag == DW_TAG_typedef)
15269 fip->typedef_field_list.push_back (fp);
15270 else
15271 fip->nested_types_list.push_back (fp);
15272 }
15273
15274 /* Create the vector of fields, and attach it to the type. */
15275
15276 static void
15277 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15278 struct dwarf2_cu *cu)
15279 {
15280 int nfields = fip->nfields;
15281
15282 /* Record the field count, allocate space for the array of fields,
15283 and create blank accessibility bitfields if necessary. */
15284 TYPE_NFIELDS (type) = nfields;
15285 TYPE_FIELDS (type) = (struct field *)
15286 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15287
15288 if (fip->non_public_fields && cu->language != language_ada)
15289 {
15290 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15291
15292 TYPE_FIELD_PRIVATE_BITS (type) =
15293 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15294 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15295
15296 TYPE_FIELD_PROTECTED_BITS (type) =
15297 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15298 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15299
15300 TYPE_FIELD_IGNORE_BITS (type) =
15301 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15302 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15303 }
15304
15305 /* If the type has baseclasses, allocate and clear a bit vector for
15306 TYPE_FIELD_VIRTUAL_BITS. */
15307 if (!fip->baseclasses.empty () && cu->language != language_ada)
15308 {
15309 int num_bytes = B_BYTES (fip->baseclasses.size ());
15310 unsigned char *pointer;
15311
15312 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15313 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15314 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15315 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15316 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15317 }
15318
15319 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15320 {
15321 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15322
15323 for (int index = 0; index < nfields; ++index)
15324 {
15325 struct nextfield &field = fip->fields[index];
15326
15327 if (field.variant.is_discriminant)
15328 di->discriminant_index = index;
15329 else if (field.variant.default_branch)
15330 di->default_index = index;
15331 else
15332 di->discriminants[index] = field.variant.discriminant_value;
15333 }
15334 }
15335
15336 /* Copy the saved-up fields into the field vector. */
15337 for (int i = 0; i < nfields; ++i)
15338 {
15339 struct nextfield &field
15340 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15341 : fip->fields[i - fip->baseclasses.size ()]);
15342
15343 TYPE_FIELD (type, i) = field.field;
15344 switch (field.accessibility)
15345 {
15346 case DW_ACCESS_private:
15347 if (cu->language != language_ada)
15348 SET_TYPE_FIELD_PRIVATE (type, i);
15349 break;
15350
15351 case DW_ACCESS_protected:
15352 if (cu->language != language_ada)
15353 SET_TYPE_FIELD_PROTECTED (type, i);
15354 break;
15355
15356 case DW_ACCESS_public:
15357 break;
15358
15359 default:
15360 /* Unknown accessibility. Complain and treat it as public. */
15361 {
15362 complaint (_("unsupported accessibility %d"),
15363 field.accessibility);
15364 }
15365 break;
15366 }
15367 if (i < fip->baseclasses.size ())
15368 {
15369 switch (field.virtuality)
15370 {
15371 case DW_VIRTUALITY_virtual:
15372 case DW_VIRTUALITY_pure_virtual:
15373 if (cu->language == language_ada)
15374 error (_("unexpected virtuality in component of Ada type"));
15375 SET_TYPE_FIELD_VIRTUAL (type, i);
15376 break;
15377 }
15378 }
15379 }
15380 }
15381
15382 /* Return true if this member function is a constructor, false
15383 otherwise. */
15384
15385 static int
15386 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15387 {
15388 const char *fieldname;
15389 const char *type_name;
15390 int len;
15391
15392 if (die->parent == NULL)
15393 return 0;
15394
15395 if (die->parent->tag != DW_TAG_structure_type
15396 && die->parent->tag != DW_TAG_union_type
15397 && die->parent->tag != DW_TAG_class_type)
15398 return 0;
15399
15400 fieldname = dwarf2_name (die, cu);
15401 type_name = dwarf2_name (die->parent, cu);
15402 if (fieldname == NULL || type_name == NULL)
15403 return 0;
15404
15405 len = strlen (fieldname);
15406 return (strncmp (fieldname, type_name, len) == 0
15407 && (type_name[len] == '\0' || type_name[len] == '<'));
15408 }
15409
15410 /* Add a member function to the proper fieldlist. */
15411
15412 static void
15413 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15414 struct type *type, struct dwarf2_cu *cu)
15415 {
15416 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15417 struct attribute *attr;
15418 int i;
15419 struct fnfieldlist *flp = nullptr;
15420 struct fn_field *fnp;
15421 const char *fieldname;
15422 struct type *this_type;
15423 enum dwarf_access_attribute accessibility;
15424
15425 if (cu->language == language_ada)
15426 error (_("unexpected member function in Ada type"));
15427
15428 /* Get name of member function. */
15429 fieldname = dwarf2_name (die, cu);
15430 if (fieldname == NULL)
15431 return;
15432
15433 /* Look up member function name in fieldlist. */
15434 for (i = 0; i < fip->fnfieldlists.size (); i++)
15435 {
15436 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15437 {
15438 flp = &fip->fnfieldlists[i];
15439 break;
15440 }
15441 }
15442
15443 /* Create a new fnfieldlist if necessary. */
15444 if (flp == nullptr)
15445 {
15446 fip->fnfieldlists.emplace_back ();
15447 flp = &fip->fnfieldlists.back ();
15448 flp->name = fieldname;
15449 i = fip->fnfieldlists.size () - 1;
15450 }
15451
15452 /* Create a new member function field and add it to the vector of
15453 fnfieldlists. */
15454 flp->fnfields.emplace_back ();
15455 fnp = &flp->fnfields.back ();
15456
15457 /* Delay processing of the physname until later. */
15458 if (cu->language == language_cplus)
15459 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15460 die, cu);
15461 else
15462 {
15463 const char *physname = dwarf2_physname (fieldname, die, cu);
15464 fnp->physname = physname ? physname : "";
15465 }
15466
15467 fnp->type = alloc_type (objfile);
15468 this_type = read_type_die (die, cu);
15469 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15470 {
15471 int nparams = TYPE_NFIELDS (this_type);
15472
15473 /* TYPE is the domain of this method, and THIS_TYPE is the type
15474 of the method itself (TYPE_CODE_METHOD). */
15475 smash_to_method_type (fnp->type, type,
15476 TYPE_TARGET_TYPE (this_type),
15477 TYPE_FIELDS (this_type),
15478 TYPE_NFIELDS (this_type),
15479 TYPE_VARARGS (this_type));
15480
15481 /* Handle static member functions.
15482 Dwarf2 has no clean way to discern C++ static and non-static
15483 member functions. G++ helps GDB by marking the first
15484 parameter for non-static member functions (which is the this
15485 pointer) as artificial. We obtain this information from
15486 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15487 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15488 fnp->voffset = VOFFSET_STATIC;
15489 }
15490 else
15491 complaint (_("member function type missing for '%s'"),
15492 dwarf2_full_name (fieldname, die, cu));
15493
15494 /* Get fcontext from DW_AT_containing_type if present. */
15495 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15496 fnp->fcontext = die_containing_type (die, cu);
15497
15498 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15499 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15500
15501 /* Get accessibility. */
15502 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15503 if (attr)
15504 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15505 else
15506 accessibility = dwarf2_default_access_attribute (die, cu);
15507 switch (accessibility)
15508 {
15509 case DW_ACCESS_private:
15510 fnp->is_private = 1;
15511 break;
15512 case DW_ACCESS_protected:
15513 fnp->is_protected = 1;
15514 break;
15515 }
15516
15517 /* Check for artificial methods. */
15518 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15519 if (attr && DW_UNSND (attr) != 0)
15520 fnp->is_artificial = 1;
15521
15522 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15523
15524 /* Get index in virtual function table if it is a virtual member
15525 function. For older versions of GCC, this is an offset in the
15526 appropriate virtual table, as specified by DW_AT_containing_type.
15527 For everyone else, it is an expression to be evaluated relative
15528 to the object address. */
15529
15530 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15531 if (attr)
15532 {
15533 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15534 {
15535 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15536 {
15537 /* Old-style GCC. */
15538 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15539 }
15540 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15541 || (DW_BLOCK (attr)->size > 1
15542 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15543 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15544 {
15545 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15546 if ((fnp->voffset % cu->header.addr_size) != 0)
15547 dwarf2_complex_location_expr_complaint ();
15548 else
15549 fnp->voffset /= cu->header.addr_size;
15550 fnp->voffset += 2;
15551 }
15552 else
15553 dwarf2_complex_location_expr_complaint ();
15554
15555 if (!fnp->fcontext)
15556 {
15557 /* If there is no `this' field and no DW_AT_containing_type,
15558 we cannot actually find a base class context for the
15559 vtable! */
15560 if (TYPE_NFIELDS (this_type) == 0
15561 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15562 {
15563 complaint (_("cannot determine context for virtual member "
15564 "function \"%s\" (offset %s)"),
15565 fieldname, sect_offset_str (die->sect_off));
15566 }
15567 else
15568 {
15569 fnp->fcontext
15570 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15571 }
15572 }
15573 }
15574 else if (attr_form_is_section_offset (attr))
15575 {
15576 dwarf2_complex_location_expr_complaint ();
15577 }
15578 else
15579 {
15580 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15581 fieldname);
15582 }
15583 }
15584 else
15585 {
15586 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15587 if (attr && DW_UNSND (attr))
15588 {
15589 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15590 complaint (_("Member function \"%s\" (offset %s) is virtual "
15591 "but the vtable offset is not specified"),
15592 fieldname, sect_offset_str (die->sect_off));
15593 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15594 TYPE_CPLUS_DYNAMIC (type) = 1;
15595 }
15596 }
15597 }
15598
15599 /* Create the vector of member function fields, and attach it to the type. */
15600
15601 static void
15602 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15603 struct dwarf2_cu *cu)
15604 {
15605 if (cu->language == language_ada)
15606 error (_("unexpected member functions in Ada type"));
15607
15608 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15609 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15610 TYPE_ALLOC (type,
15611 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15612
15613 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15614 {
15615 struct fnfieldlist &nf = fip->fnfieldlists[i];
15616 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15617
15618 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15619 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15620 fn_flp->fn_fields = (struct fn_field *)
15621 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15622
15623 for (int k = 0; k < nf.fnfields.size (); ++k)
15624 fn_flp->fn_fields[k] = nf.fnfields[k];
15625 }
15626
15627 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15628 }
15629
15630 /* Returns non-zero if NAME is the name of a vtable member in CU's
15631 language, zero otherwise. */
15632 static int
15633 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15634 {
15635 static const char vptr[] = "_vptr";
15636
15637 /* Look for the C++ form of the vtable. */
15638 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15639 return 1;
15640
15641 return 0;
15642 }
15643
15644 /* GCC outputs unnamed structures that are really pointers to member
15645 functions, with the ABI-specified layout. If TYPE describes
15646 such a structure, smash it into a member function type.
15647
15648 GCC shouldn't do this; it should just output pointer to member DIEs.
15649 This is GCC PR debug/28767. */
15650
15651 static void
15652 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15653 {
15654 struct type *pfn_type, *self_type, *new_type;
15655
15656 /* Check for a structure with no name and two children. */
15657 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15658 return;
15659
15660 /* Check for __pfn and __delta members. */
15661 if (TYPE_FIELD_NAME (type, 0) == NULL
15662 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15663 || TYPE_FIELD_NAME (type, 1) == NULL
15664 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15665 return;
15666
15667 /* Find the type of the method. */
15668 pfn_type = TYPE_FIELD_TYPE (type, 0);
15669 if (pfn_type == NULL
15670 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15671 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15672 return;
15673
15674 /* Look for the "this" argument. */
15675 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15676 if (TYPE_NFIELDS (pfn_type) == 0
15677 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15678 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15679 return;
15680
15681 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15682 new_type = alloc_type (objfile);
15683 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15684 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15685 TYPE_VARARGS (pfn_type));
15686 smash_to_methodptr_type (type, new_type);
15687 }
15688
15689 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15690 appropriate error checking and issuing complaints if there is a
15691 problem. */
15692
15693 static ULONGEST
15694 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15695 {
15696 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15697
15698 if (attr == nullptr)
15699 return 0;
15700
15701 if (!attr_form_is_constant (attr))
15702 {
15703 complaint (_("DW_AT_alignment must have constant form"
15704 " - DIE at %s [in module %s]"),
15705 sect_offset_str (die->sect_off),
15706 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15707 return 0;
15708 }
15709
15710 ULONGEST align;
15711 if (attr->form == DW_FORM_sdata)
15712 {
15713 LONGEST val = DW_SND (attr);
15714 if (val < 0)
15715 {
15716 complaint (_("DW_AT_alignment value must not be negative"
15717 " - DIE at %s [in module %s]"),
15718 sect_offset_str (die->sect_off),
15719 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15720 return 0;
15721 }
15722 align = val;
15723 }
15724 else
15725 align = DW_UNSND (attr);
15726
15727 if (align == 0)
15728 {
15729 complaint (_("DW_AT_alignment value must not be zero"
15730 " - DIE at %s [in module %s]"),
15731 sect_offset_str (die->sect_off),
15732 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15733 return 0;
15734 }
15735 if ((align & (align - 1)) != 0)
15736 {
15737 complaint (_("DW_AT_alignment value must be a power of 2"
15738 " - DIE at %s [in module %s]"),
15739 sect_offset_str (die->sect_off),
15740 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15741 return 0;
15742 }
15743
15744 return align;
15745 }
15746
15747 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15748 the alignment for TYPE. */
15749
15750 static void
15751 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15752 struct type *type)
15753 {
15754 if (!set_type_align (type, get_alignment (cu, die)))
15755 complaint (_("DW_AT_alignment value too large"
15756 " - DIE at %s [in module %s]"),
15757 sect_offset_str (die->sect_off),
15758 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15759 }
15760
15761 /* Called when we find the DIE that starts a structure or union scope
15762 (definition) to create a type for the structure or union. Fill in
15763 the type's name and general properties; the members will not be
15764 processed until process_structure_scope. A symbol table entry for
15765 the type will also not be done until process_structure_scope (assuming
15766 the type has a name).
15767
15768 NOTE: we need to call these functions regardless of whether or not the
15769 DIE has a DW_AT_name attribute, since it might be an anonymous
15770 structure or union. This gets the type entered into our set of
15771 user defined types. */
15772
15773 static struct type *
15774 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15775 {
15776 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15777 struct type *type;
15778 struct attribute *attr;
15779 const char *name;
15780
15781 /* If the definition of this type lives in .debug_types, read that type.
15782 Don't follow DW_AT_specification though, that will take us back up
15783 the chain and we want to go down. */
15784 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15785 if (attr)
15786 {
15787 type = get_DW_AT_signature_type (die, attr, cu);
15788
15789 /* The type's CU may not be the same as CU.
15790 Ensure TYPE is recorded with CU in die_type_hash. */
15791 return set_die_type (die, type, cu);
15792 }
15793
15794 type = alloc_type (objfile);
15795 INIT_CPLUS_SPECIFIC (type);
15796
15797 name = dwarf2_name (die, cu);
15798 if (name != NULL)
15799 {
15800 if (cu->language == language_cplus
15801 || cu->language == language_d
15802 || cu->language == language_rust)
15803 {
15804 const char *full_name = dwarf2_full_name (name, die, cu);
15805
15806 /* dwarf2_full_name might have already finished building the DIE's
15807 type. If so, there is no need to continue. */
15808 if (get_die_type (die, cu) != NULL)
15809 return get_die_type (die, cu);
15810
15811 TYPE_NAME (type) = full_name;
15812 }
15813 else
15814 {
15815 /* The name is already allocated along with this objfile, so
15816 we don't need to duplicate it for the type. */
15817 TYPE_NAME (type) = name;
15818 }
15819 }
15820
15821 if (die->tag == DW_TAG_structure_type)
15822 {
15823 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15824 }
15825 else if (die->tag == DW_TAG_union_type)
15826 {
15827 TYPE_CODE (type) = TYPE_CODE_UNION;
15828 }
15829 else if (die->tag == DW_TAG_variant_part)
15830 {
15831 TYPE_CODE (type) = TYPE_CODE_UNION;
15832 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15833 }
15834 else
15835 {
15836 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15837 }
15838
15839 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15840 TYPE_DECLARED_CLASS (type) = 1;
15841
15842 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15843 if (attr)
15844 {
15845 if (attr_form_is_constant (attr))
15846 TYPE_LENGTH (type) = DW_UNSND (attr);
15847 else
15848 {
15849 /* For the moment, dynamic type sizes are not supported
15850 by GDB's struct type. The actual size is determined
15851 on-demand when resolving the type of a given object,
15852 so set the type's length to zero for now. Otherwise,
15853 we record an expression as the length, and that expression
15854 could lead to a very large value, which could eventually
15855 lead to us trying to allocate that much memory when creating
15856 a value of that type. */
15857 TYPE_LENGTH (type) = 0;
15858 }
15859 }
15860 else
15861 {
15862 TYPE_LENGTH (type) = 0;
15863 }
15864
15865 maybe_set_alignment (cu, die, type);
15866
15867 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15868 {
15869 /* ICC<14 does not output the required DW_AT_declaration on
15870 incomplete types, but gives them a size of zero. */
15871 TYPE_STUB (type) = 1;
15872 }
15873 else
15874 TYPE_STUB_SUPPORTED (type) = 1;
15875
15876 if (die_is_declaration (die, cu))
15877 TYPE_STUB (type) = 1;
15878 else if (attr == NULL && die->child == NULL
15879 && producer_is_realview (cu->producer))
15880 /* RealView does not output the required DW_AT_declaration
15881 on incomplete types. */
15882 TYPE_STUB (type) = 1;
15883
15884 /* We need to add the type field to the die immediately so we don't
15885 infinitely recurse when dealing with pointers to the structure
15886 type within the structure itself. */
15887 set_die_type (die, type, cu);
15888
15889 /* set_die_type should be already done. */
15890 set_descriptive_type (type, die, cu);
15891
15892 return type;
15893 }
15894
15895 /* A helper for process_structure_scope that handles a single member
15896 DIE. */
15897
15898 static void
15899 handle_struct_member_die (struct die_info *child_die, struct type *type,
15900 struct field_info *fi,
15901 std::vector<struct symbol *> *template_args,
15902 struct dwarf2_cu *cu)
15903 {
15904 if (child_die->tag == DW_TAG_member
15905 || child_die->tag == DW_TAG_variable
15906 || child_die->tag == DW_TAG_variant_part)
15907 {
15908 /* NOTE: carlton/2002-11-05: A C++ static data member
15909 should be a DW_TAG_member that is a declaration, but
15910 all versions of G++ as of this writing (so through at
15911 least 3.2.1) incorrectly generate DW_TAG_variable
15912 tags for them instead. */
15913 dwarf2_add_field (fi, child_die, cu);
15914 }
15915 else if (child_die->tag == DW_TAG_subprogram)
15916 {
15917 /* Rust doesn't have member functions in the C++ sense.
15918 However, it does emit ordinary functions as children
15919 of a struct DIE. */
15920 if (cu->language == language_rust)
15921 read_func_scope (child_die, cu);
15922 else
15923 {
15924 /* C++ member function. */
15925 dwarf2_add_member_fn (fi, child_die, type, cu);
15926 }
15927 }
15928 else if (child_die->tag == DW_TAG_inheritance)
15929 {
15930 /* C++ base class field. */
15931 dwarf2_add_field (fi, child_die, cu);
15932 }
15933 else if (type_can_define_types (child_die))
15934 dwarf2_add_type_defn (fi, child_die, cu);
15935 else if (child_die->tag == DW_TAG_template_type_param
15936 || child_die->tag == DW_TAG_template_value_param)
15937 {
15938 struct symbol *arg = new_symbol (child_die, NULL, cu);
15939
15940 if (arg != NULL)
15941 template_args->push_back (arg);
15942 }
15943 else if (child_die->tag == DW_TAG_variant)
15944 {
15945 /* In a variant we want to get the discriminant and also add a
15946 field for our sole member child. */
15947 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15948
15949 for (die_info *variant_child = child_die->child;
15950 variant_child != NULL;
15951 variant_child = sibling_die (variant_child))
15952 {
15953 if (variant_child->tag == DW_TAG_member)
15954 {
15955 handle_struct_member_die (variant_child, type, fi,
15956 template_args, cu);
15957 /* Only handle the one. */
15958 break;
15959 }
15960 }
15961
15962 /* We don't handle this but we might as well report it if we see
15963 it. */
15964 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15965 complaint (_("DW_AT_discr_list is not supported yet"
15966 " - DIE at %s [in module %s]"),
15967 sect_offset_str (child_die->sect_off),
15968 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15969
15970 /* The first field was just added, so we can stash the
15971 discriminant there. */
15972 gdb_assert (!fi->fields.empty ());
15973 if (discr == NULL)
15974 fi->fields.back ().variant.default_branch = true;
15975 else
15976 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15977 }
15978 }
15979
15980 /* Finish creating a structure or union type, including filling in
15981 its members and creating a symbol for it. */
15982
15983 static void
15984 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15985 {
15986 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15987 struct die_info *child_die;
15988 struct type *type;
15989
15990 type = get_die_type (die, cu);
15991 if (type == NULL)
15992 type = read_structure_type (die, cu);
15993
15994 /* When reading a DW_TAG_variant_part, we need to notice when we
15995 read the discriminant member, so we can record it later in the
15996 discriminant_info. */
15997 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15998 sect_offset discr_offset;
15999 bool has_template_parameters = false;
16000
16001 if (is_variant_part)
16002 {
16003 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16004 if (discr == NULL)
16005 {
16006 /* Maybe it's a univariant form, an extension we support.
16007 In this case arrange not to check the offset. */
16008 is_variant_part = false;
16009 }
16010 else if (attr_form_is_ref (discr))
16011 {
16012 struct dwarf2_cu *target_cu = cu;
16013 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16014
16015 discr_offset = target_die->sect_off;
16016 }
16017 else
16018 {
16019 complaint (_("DW_AT_discr does not have DIE reference form"
16020 " - DIE at %s [in module %s]"),
16021 sect_offset_str (die->sect_off),
16022 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16023 is_variant_part = false;
16024 }
16025 }
16026
16027 if (die->child != NULL && ! die_is_declaration (die, cu))
16028 {
16029 struct field_info fi;
16030 std::vector<struct symbol *> template_args;
16031
16032 child_die = die->child;
16033
16034 while (child_die && child_die->tag)
16035 {
16036 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16037
16038 if (is_variant_part && discr_offset == child_die->sect_off)
16039 fi.fields.back ().variant.is_discriminant = true;
16040
16041 child_die = sibling_die (child_die);
16042 }
16043
16044 /* Attach template arguments to type. */
16045 if (!template_args.empty ())
16046 {
16047 has_template_parameters = true;
16048 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16049 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16050 TYPE_TEMPLATE_ARGUMENTS (type)
16051 = XOBNEWVEC (&objfile->objfile_obstack,
16052 struct symbol *,
16053 TYPE_N_TEMPLATE_ARGUMENTS (type));
16054 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16055 template_args.data (),
16056 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16057 * sizeof (struct symbol *)));
16058 }
16059
16060 /* Attach fields and member functions to the type. */
16061 if (fi.nfields)
16062 dwarf2_attach_fields_to_type (&fi, type, cu);
16063 if (!fi.fnfieldlists.empty ())
16064 {
16065 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16066
16067 /* Get the type which refers to the base class (possibly this
16068 class itself) which contains the vtable pointer for the current
16069 class from the DW_AT_containing_type attribute. This use of
16070 DW_AT_containing_type is a GNU extension. */
16071
16072 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16073 {
16074 struct type *t = die_containing_type (die, cu);
16075
16076 set_type_vptr_basetype (type, t);
16077 if (type == t)
16078 {
16079 int i;
16080
16081 /* Our own class provides vtbl ptr. */
16082 for (i = TYPE_NFIELDS (t) - 1;
16083 i >= TYPE_N_BASECLASSES (t);
16084 --i)
16085 {
16086 const char *fieldname = TYPE_FIELD_NAME (t, i);
16087
16088 if (is_vtable_name (fieldname, cu))
16089 {
16090 set_type_vptr_fieldno (type, i);
16091 break;
16092 }
16093 }
16094
16095 /* Complain if virtual function table field not found. */
16096 if (i < TYPE_N_BASECLASSES (t))
16097 complaint (_("virtual function table pointer "
16098 "not found when defining class '%s'"),
16099 TYPE_NAME (type) ? TYPE_NAME (type) : "");
16100 }
16101 else
16102 {
16103 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16104 }
16105 }
16106 else if (cu->producer
16107 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16108 {
16109 /* The IBM XLC compiler does not provide direct indication
16110 of the containing type, but the vtable pointer is
16111 always named __vfp. */
16112
16113 int i;
16114
16115 for (i = TYPE_NFIELDS (type) - 1;
16116 i >= TYPE_N_BASECLASSES (type);
16117 --i)
16118 {
16119 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16120 {
16121 set_type_vptr_fieldno (type, i);
16122 set_type_vptr_basetype (type, type);
16123 break;
16124 }
16125 }
16126 }
16127 }
16128
16129 /* Copy fi.typedef_field_list linked list elements content into the
16130 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16131 if (!fi.typedef_field_list.empty ())
16132 {
16133 int count = fi.typedef_field_list.size ();
16134
16135 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16136 TYPE_TYPEDEF_FIELD_ARRAY (type)
16137 = ((struct decl_field *)
16138 TYPE_ALLOC (type,
16139 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16140 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16141
16142 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16143 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16144 }
16145
16146 /* Copy fi.nested_types_list linked list elements content into the
16147 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16148 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16149 {
16150 int count = fi.nested_types_list.size ();
16151
16152 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16153 TYPE_NESTED_TYPES_ARRAY (type)
16154 = ((struct decl_field *)
16155 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16156 TYPE_NESTED_TYPES_COUNT (type) = count;
16157
16158 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16159 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16160 }
16161 }
16162
16163 quirk_gcc_member_function_pointer (type, objfile);
16164 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16165 cu->rust_unions.push_back (type);
16166
16167 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16168 snapshots) has been known to create a die giving a declaration
16169 for a class that has, as a child, a die giving a definition for a
16170 nested class. So we have to process our children even if the
16171 current die is a declaration. Normally, of course, a declaration
16172 won't have any children at all. */
16173
16174 child_die = die->child;
16175
16176 while (child_die != NULL && child_die->tag)
16177 {
16178 if (child_die->tag == DW_TAG_member
16179 || child_die->tag == DW_TAG_variable
16180 || child_die->tag == DW_TAG_inheritance
16181 || child_die->tag == DW_TAG_template_value_param
16182 || child_die->tag == DW_TAG_template_type_param)
16183 {
16184 /* Do nothing. */
16185 }
16186 else
16187 process_die (child_die, cu);
16188
16189 child_die = sibling_die (child_die);
16190 }
16191
16192 /* Do not consider external references. According to the DWARF standard,
16193 these DIEs are identified by the fact that they have no byte_size
16194 attribute, and a declaration attribute. */
16195 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16196 || !die_is_declaration (die, cu))
16197 {
16198 struct symbol *sym = new_symbol (die, type, cu);
16199
16200 if (has_template_parameters)
16201 {
16202 struct symtab *symtab;
16203 if (sym != nullptr)
16204 symtab = symbol_symtab (sym);
16205 else if (cu->line_header != nullptr)
16206 {
16207 /* Any related symtab will do. */
16208 symtab
16209 = cu->line_header->file_name_at (file_name_index (1))->symtab;
16210 }
16211 else
16212 {
16213 symtab = nullptr;
16214 complaint (_("could not find suitable "
16215 "symtab for template parameter"
16216 " - DIE at %s [in module %s]"),
16217 sect_offset_str (die->sect_off),
16218 objfile_name (objfile));
16219 }
16220
16221 if (symtab != nullptr)
16222 {
16223 /* Make sure that the symtab is set on the new symbols.
16224 Even though they don't appear in this symtab directly,
16225 other parts of gdb assume that symbols do, and this is
16226 reasonably true. */
16227 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16228 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16229 }
16230 }
16231 }
16232 }
16233
16234 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16235 update TYPE using some information only available in DIE's children. */
16236
16237 static void
16238 update_enumeration_type_from_children (struct die_info *die,
16239 struct type *type,
16240 struct dwarf2_cu *cu)
16241 {
16242 struct die_info *child_die;
16243 int unsigned_enum = 1;
16244 int flag_enum = 1;
16245 ULONGEST mask = 0;
16246
16247 auto_obstack obstack;
16248
16249 for (child_die = die->child;
16250 child_die != NULL && child_die->tag;
16251 child_die = sibling_die (child_die))
16252 {
16253 struct attribute *attr;
16254 LONGEST value;
16255 const gdb_byte *bytes;
16256 struct dwarf2_locexpr_baton *baton;
16257 const char *name;
16258
16259 if (child_die->tag != DW_TAG_enumerator)
16260 continue;
16261
16262 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16263 if (attr == NULL)
16264 continue;
16265
16266 name = dwarf2_name (child_die, cu);
16267 if (name == NULL)
16268 name = "<anonymous enumerator>";
16269
16270 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16271 &value, &bytes, &baton);
16272 if (value < 0)
16273 {
16274 unsigned_enum = 0;
16275 flag_enum = 0;
16276 }
16277 else if ((mask & value) != 0)
16278 flag_enum = 0;
16279 else
16280 mask |= value;
16281
16282 /* If we already know that the enum type is neither unsigned, nor
16283 a flag type, no need to look at the rest of the enumerates. */
16284 if (!unsigned_enum && !flag_enum)
16285 break;
16286 }
16287
16288 if (unsigned_enum)
16289 TYPE_UNSIGNED (type) = 1;
16290 if (flag_enum)
16291 TYPE_FLAG_ENUM (type) = 1;
16292 }
16293
16294 /* Given a DW_AT_enumeration_type die, set its type. We do not
16295 complete the type's fields yet, or create any symbols. */
16296
16297 static struct type *
16298 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16299 {
16300 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16301 struct type *type;
16302 struct attribute *attr;
16303 const char *name;
16304
16305 /* If the definition of this type lives in .debug_types, read that type.
16306 Don't follow DW_AT_specification though, that will take us back up
16307 the chain and we want to go down. */
16308 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16309 if (attr)
16310 {
16311 type = get_DW_AT_signature_type (die, attr, cu);
16312
16313 /* The type's CU may not be the same as CU.
16314 Ensure TYPE is recorded with CU in die_type_hash. */
16315 return set_die_type (die, type, cu);
16316 }
16317
16318 type = alloc_type (objfile);
16319
16320 TYPE_CODE (type) = TYPE_CODE_ENUM;
16321 name = dwarf2_full_name (NULL, die, cu);
16322 if (name != NULL)
16323 TYPE_NAME (type) = name;
16324
16325 attr = dwarf2_attr (die, DW_AT_type, cu);
16326 if (attr != NULL)
16327 {
16328 struct type *underlying_type = die_type (die, cu);
16329
16330 TYPE_TARGET_TYPE (type) = underlying_type;
16331 }
16332
16333 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16334 if (attr)
16335 {
16336 TYPE_LENGTH (type) = DW_UNSND (attr);
16337 }
16338 else
16339 {
16340 TYPE_LENGTH (type) = 0;
16341 }
16342
16343 maybe_set_alignment (cu, die, type);
16344
16345 /* The enumeration DIE can be incomplete. In Ada, any type can be
16346 declared as private in the package spec, and then defined only
16347 inside the package body. Such types are known as Taft Amendment
16348 Types. When another package uses such a type, an incomplete DIE
16349 may be generated by the compiler. */
16350 if (die_is_declaration (die, cu))
16351 TYPE_STUB (type) = 1;
16352
16353 /* Finish the creation of this type by using the enum's children.
16354 We must call this even when the underlying type has been provided
16355 so that we can determine if we're looking at a "flag" enum. */
16356 update_enumeration_type_from_children (die, type, cu);
16357
16358 /* If this type has an underlying type that is not a stub, then we
16359 may use its attributes. We always use the "unsigned" attribute
16360 in this situation, because ordinarily we guess whether the type
16361 is unsigned -- but the guess can be wrong and the underlying type
16362 can tell us the reality. However, we defer to a local size
16363 attribute if one exists, because this lets the compiler override
16364 the underlying type if needed. */
16365 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16366 {
16367 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16368 if (TYPE_LENGTH (type) == 0)
16369 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16370 if (TYPE_RAW_ALIGN (type) == 0
16371 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16372 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
16373 }
16374
16375 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16376
16377 return set_die_type (die, type, cu);
16378 }
16379
16380 /* Given a pointer to a die which begins an enumeration, process all
16381 the dies that define the members of the enumeration, and create the
16382 symbol for the enumeration type.
16383
16384 NOTE: We reverse the order of the element list. */
16385
16386 static void
16387 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16388 {
16389 struct type *this_type;
16390
16391 this_type = get_die_type (die, cu);
16392 if (this_type == NULL)
16393 this_type = read_enumeration_type (die, cu);
16394
16395 if (die->child != NULL)
16396 {
16397 struct die_info *child_die;
16398 struct symbol *sym;
16399 struct field *fields = NULL;
16400 int num_fields = 0;
16401 const char *name;
16402
16403 child_die = die->child;
16404 while (child_die && child_die->tag)
16405 {
16406 if (child_die->tag != DW_TAG_enumerator)
16407 {
16408 process_die (child_die, cu);
16409 }
16410 else
16411 {
16412 name = dwarf2_name (child_die, cu);
16413 if (name)
16414 {
16415 sym = new_symbol (child_die, this_type, cu);
16416
16417 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16418 {
16419 fields = (struct field *)
16420 xrealloc (fields,
16421 (num_fields + DW_FIELD_ALLOC_CHUNK)
16422 * sizeof (struct field));
16423 }
16424
16425 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16426 FIELD_TYPE (fields[num_fields]) = NULL;
16427 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16428 FIELD_BITSIZE (fields[num_fields]) = 0;
16429
16430 num_fields++;
16431 }
16432 }
16433
16434 child_die = sibling_die (child_die);
16435 }
16436
16437 if (num_fields)
16438 {
16439 TYPE_NFIELDS (this_type) = num_fields;
16440 TYPE_FIELDS (this_type) = (struct field *)
16441 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16442 memcpy (TYPE_FIELDS (this_type), fields,
16443 sizeof (struct field) * num_fields);
16444 xfree (fields);
16445 }
16446 }
16447
16448 /* If we are reading an enum from a .debug_types unit, and the enum
16449 is a declaration, and the enum is not the signatured type in the
16450 unit, then we do not want to add a symbol for it. Adding a
16451 symbol would in some cases obscure the true definition of the
16452 enum, giving users an incomplete type when the definition is
16453 actually available. Note that we do not want to do this for all
16454 enums which are just declarations, because C++0x allows forward
16455 enum declarations. */
16456 if (cu->per_cu->is_debug_types
16457 && die_is_declaration (die, cu))
16458 {
16459 struct signatured_type *sig_type;
16460
16461 sig_type = (struct signatured_type *) cu->per_cu;
16462 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16463 if (sig_type->type_offset_in_section != die->sect_off)
16464 return;
16465 }
16466
16467 new_symbol (die, this_type, cu);
16468 }
16469
16470 /* Extract all information from a DW_TAG_array_type DIE and put it in
16471 the DIE's type field. For now, this only handles one dimensional
16472 arrays. */
16473
16474 static struct type *
16475 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16476 {
16477 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16478 struct die_info *child_die;
16479 struct type *type;
16480 struct type *element_type, *range_type, *index_type;
16481 struct attribute *attr;
16482 const char *name;
16483 struct dynamic_prop *byte_stride_prop = NULL;
16484 unsigned int bit_stride = 0;
16485
16486 element_type = die_type (die, cu);
16487
16488 /* The die_type call above may have already set the type for this DIE. */
16489 type = get_die_type (die, cu);
16490 if (type)
16491 return type;
16492
16493 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16494 if (attr != NULL)
16495 {
16496 int stride_ok;
16497 struct type *prop_type
16498 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
16499
16500 byte_stride_prop
16501 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16502 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16503 prop_type);
16504 if (!stride_ok)
16505 {
16506 complaint (_("unable to read array DW_AT_byte_stride "
16507 " - DIE at %s [in module %s]"),
16508 sect_offset_str (die->sect_off),
16509 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16510 /* Ignore this attribute. We will likely not be able to print
16511 arrays of this type correctly, but there is little we can do
16512 to help if we cannot read the attribute's value. */
16513 byte_stride_prop = NULL;
16514 }
16515 }
16516
16517 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16518 if (attr != NULL)
16519 bit_stride = DW_UNSND (attr);
16520
16521 /* Irix 6.2 native cc creates array types without children for
16522 arrays with unspecified length. */
16523 if (die->child == NULL)
16524 {
16525 index_type = objfile_type (objfile)->builtin_int;
16526 range_type = create_static_range_type (NULL, index_type, 0, -1);
16527 type = create_array_type_with_stride (NULL, element_type, range_type,
16528 byte_stride_prop, bit_stride);
16529 return set_die_type (die, type, cu);
16530 }
16531
16532 std::vector<struct type *> range_types;
16533 child_die = die->child;
16534 while (child_die && child_die->tag)
16535 {
16536 if (child_die->tag == DW_TAG_subrange_type)
16537 {
16538 struct type *child_type = read_type_die (child_die, cu);
16539
16540 if (child_type != NULL)
16541 {
16542 /* The range type was succesfully read. Save it for the
16543 array type creation. */
16544 range_types.push_back (child_type);
16545 }
16546 }
16547 child_die = sibling_die (child_die);
16548 }
16549
16550 /* Dwarf2 dimensions are output from left to right, create the
16551 necessary array types in backwards order. */
16552
16553 type = element_type;
16554
16555 if (read_array_order (die, cu) == DW_ORD_col_major)
16556 {
16557 int i = 0;
16558
16559 while (i < range_types.size ())
16560 type = create_array_type_with_stride (NULL, type, range_types[i++],
16561 byte_stride_prop, bit_stride);
16562 }
16563 else
16564 {
16565 size_t ndim = range_types.size ();
16566 while (ndim-- > 0)
16567 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16568 byte_stride_prop, bit_stride);
16569 }
16570
16571 /* Understand Dwarf2 support for vector types (like they occur on
16572 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16573 array type. This is not part of the Dwarf2/3 standard yet, but a
16574 custom vendor extension. The main difference between a regular
16575 array and the vector variant is that vectors are passed by value
16576 to functions. */
16577 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16578 if (attr)
16579 make_vector_type (type);
16580
16581 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16582 implementation may choose to implement triple vectors using this
16583 attribute. */
16584 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16585 if (attr)
16586 {
16587 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16588 TYPE_LENGTH (type) = DW_UNSND (attr);
16589 else
16590 complaint (_("DW_AT_byte_size for array type smaller "
16591 "than the total size of elements"));
16592 }
16593
16594 name = dwarf2_name (die, cu);
16595 if (name)
16596 TYPE_NAME (type) = name;
16597
16598 maybe_set_alignment (cu, die, type);
16599
16600 /* Install the type in the die. */
16601 set_die_type (die, type, cu);
16602
16603 /* set_die_type should be already done. */
16604 set_descriptive_type (type, die, cu);
16605
16606 return type;
16607 }
16608
16609 static enum dwarf_array_dim_ordering
16610 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16611 {
16612 struct attribute *attr;
16613
16614 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16615
16616 if (attr)
16617 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16618
16619 /* GNU F77 is a special case, as at 08/2004 array type info is the
16620 opposite order to the dwarf2 specification, but data is still
16621 laid out as per normal fortran.
16622
16623 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16624 version checking. */
16625
16626 if (cu->language == language_fortran
16627 && cu->producer && strstr (cu->producer, "GNU F77"))
16628 {
16629 return DW_ORD_row_major;
16630 }
16631
16632 switch (cu->language_defn->la_array_ordering)
16633 {
16634 case array_column_major:
16635 return DW_ORD_col_major;
16636 case array_row_major:
16637 default:
16638 return DW_ORD_row_major;
16639 };
16640 }
16641
16642 /* Extract all information from a DW_TAG_set_type DIE and put it in
16643 the DIE's type field. */
16644
16645 static struct type *
16646 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16647 {
16648 struct type *domain_type, *set_type;
16649 struct attribute *attr;
16650
16651 domain_type = die_type (die, cu);
16652
16653 /* The die_type call above may have already set the type for this DIE. */
16654 set_type = get_die_type (die, cu);
16655 if (set_type)
16656 return set_type;
16657
16658 set_type = create_set_type (NULL, domain_type);
16659
16660 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16661 if (attr)
16662 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16663
16664 maybe_set_alignment (cu, die, set_type);
16665
16666 return set_die_type (die, set_type, cu);
16667 }
16668
16669 /* A helper for read_common_block that creates a locexpr baton.
16670 SYM is the symbol which we are marking as computed.
16671 COMMON_DIE is the DIE for the common block.
16672 COMMON_LOC is the location expression attribute for the common
16673 block itself.
16674 MEMBER_LOC is the location expression attribute for the particular
16675 member of the common block that we are processing.
16676 CU is the CU from which the above come. */
16677
16678 static void
16679 mark_common_block_symbol_computed (struct symbol *sym,
16680 struct die_info *common_die,
16681 struct attribute *common_loc,
16682 struct attribute *member_loc,
16683 struct dwarf2_cu *cu)
16684 {
16685 struct dwarf2_per_objfile *dwarf2_per_objfile
16686 = cu->per_cu->dwarf2_per_objfile;
16687 struct objfile *objfile = dwarf2_per_objfile->objfile;
16688 struct dwarf2_locexpr_baton *baton;
16689 gdb_byte *ptr;
16690 unsigned int cu_off;
16691 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16692 LONGEST offset = 0;
16693
16694 gdb_assert (common_loc && member_loc);
16695 gdb_assert (attr_form_is_block (common_loc));
16696 gdb_assert (attr_form_is_block (member_loc)
16697 || attr_form_is_constant (member_loc));
16698
16699 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16700 baton->per_cu = cu->per_cu;
16701 gdb_assert (baton->per_cu);
16702
16703 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16704
16705 if (attr_form_is_constant (member_loc))
16706 {
16707 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16708 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16709 }
16710 else
16711 baton->size += DW_BLOCK (member_loc)->size;
16712
16713 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16714 baton->data = ptr;
16715
16716 *ptr++ = DW_OP_call4;
16717 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16718 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16719 ptr += 4;
16720
16721 if (attr_form_is_constant (member_loc))
16722 {
16723 *ptr++ = DW_OP_addr;
16724 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16725 ptr += cu->header.addr_size;
16726 }
16727 else
16728 {
16729 /* We have to copy the data here, because DW_OP_call4 will only
16730 use a DW_AT_location attribute. */
16731 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16732 ptr += DW_BLOCK (member_loc)->size;
16733 }
16734
16735 *ptr++ = DW_OP_plus;
16736 gdb_assert (ptr - baton->data == baton->size);
16737
16738 SYMBOL_LOCATION_BATON (sym) = baton;
16739 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16740 }
16741
16742 /* Create appropriate locally-scoped variables for all the
16743 DW_TAG_common_block entries. Also create a struct common_block
16744 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16745 is used to sepate the common blocks name namespace from regular
16746 variable names. */
16747
16748 static void
16749 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16750 {
16751 struct attribute *attr;
16752
16753 attr = dwarf2_attr (die, DW_AT_location, cu);
16754 if (attr)
16755 {
16756 /* Support the .debug_loc offsets. */
16757 if (attr_form_is_block (attr))
16758 {
16759 /* Ok. */
16760 }
16761 else if (attr_form_is_section_offset (attr))
16762 {
16763 dwarf2_complex_location_expr_complaint ();
16764 attr = NULL;
16765 }
16766 else
16767 {
16768 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16769 "common block member");
16770 attr = NULL;
16771 }
16772 }
16773
16774 if (die->child != NULL)
16775 {
16776 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16777 struct die_info *child_die;
16778 size_t n_entries = 0, size;
16779 struct common_block *common_block;
16780 struct symbol *sym;
16781
16782 for (child_die = die->child;
16783 child_die && child_die->tag;
16784 child_die = sibling_die (child_die))
16785 ++n_entries;
16786
16787 size = (sizeof (struct common_block)
16788 + (n_entries - 1) * sizeof (struct symbol *));
16789 common_block
16790 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16791 size);
16792 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16793 common_block->n_entries = 0;
16794
16795 for (child_die = die->child;
16796 child_die && child_die->tag;
16797 child_die = sibling_die (child_die))
16798 {
16799 /* Create the symbol in the DW_TAG_common_block block in the current
16800 symbol scope. */
16801 sym = new_symbol (child_die, NULL, cu);
16802 if (sym != NULL)
16803 {
16804 struct attribute *member_loc;
16805
16806 common_block->contents[common_block->n_entries++] = sym;
16807
16808 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16809 cu);
16810 if (member_loc)
16811 {
16812 /* GDB has handled this for a long time, but it is
16813 not specified by DWARF. It seems to have been
16814 emitted by gfortran at least as recently as:
16815 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16816 complaint (_("Variable in common block has "
16817 "DW_AT_data_member_location "
16818 "- DIE at %s [in module %s]"),
16819 sect_offset_str (child_die->sect_off),
16820 objfile_name (objfile));
16821
16822 if (attr_form_is_section_offset (member_loc))
16823 dwarf2_complex_location_expr_complaint ();
16824 else if (attr_form_is_constant (member_loc)
16825 || attr_form_is_block (member_loc))
16826 {
16827 if (attr)
16828 mark_common_block_symbol_computed (sym, die, attr,
16829 member_loc, cu);
16830 }
16831 else
16832 dwarf2_complex_location_expr_complaint ();
16833 }
16834 }
16835 }
16836
16837 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16838 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16839 }
16840 }
16841
16842 /* Create a type for a C++ namespace. */
16843
16844 static struct type *
16845 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16846 {
16847 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16848 const char *previous_prefix, *name;
16849 int is_anonymous;
16850 struct type *type;
16851
16852 /* For extensions, reuse the type of the original namespace. */
16853 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16854 {
16855 struct die_info *ext_die;
16856 struct dwarf2_cu *ext_cu = cu;
16857
16858 ext_die = dwarf2_extension (die, &ext_cu);
16859 type = read_type_die (ext_die, ext_cu);
16860
16861 /* EXT_CU may not be the same as CU.
16862 Ensure TYPE is recorded with CU in die_type_hash. */
16863 return set_die_type (die, type, cu);
16864 }
16865
16866 name = namespace_name (die, &is_anonymous, cu);
16867
16868 /* Now build the name of the current namespace. */
16869
16870 previous_prefix = determine_prefix (die, cu);
16871 if (previous_prefix[0] != '\0')
16872 name = typename_concat (&objfile->objfile_obstack,
16873 previous_prefix, name, 0, cu);
16874
16875 /* Create the type. */
16876 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16877
16878 return set_die_type (die, type, cu);
16879 }
16880
16881 /* Read a namespace scope. */
16882
16883 static void
16884 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16885 {
16886 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16887 int is_anonymous;
16888
16889 /* Add a symbol associated to this if we haven't seen the namespace
16890 before. Also, add a using directive if it's an anonymous
16891 namespace. */
16892
16893 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16894 {
16895 struct type *type;
16896
16897 type = read_type_die (die, cu);
16898 new_symbol (die, type, cu);
16899
16900 namespace_name (die, &is_anonymous, cu);
16901 if (is_anonymous)
16902 {
16903 const char *previous_prefix = determine_prefix (die, cu);
16904
16905 std::vector<const char *> excludes;
16906 add_using_directive (using_directives (cu),
16907 previous_prefix, TYPE_NAME (type), NULL,
16908 NULL, excludes, 0, &objfile->objfile_obstack);
16909 }
16910 }
16911
16912 if (die->child != NULL)
16913 {
16914 struct die_info *child_die = die->child;
16915
16916 while (child_die && child_die->tag)
16917 {
16918 process_die (child_die, cu);
16919 child_die = sibling_die (child_die);
16920 }
16921 }
16922 }
16923
16924 /* Read a Fortran module as type. This DIE can be only a declaration used for
16925 imported module. Still we need that type as local Fortran "use ... only"
16926 declaration imports depend on the created type in determine_prefix. */
16927
16928 static struct type *
16929 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16930 {
16931 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16932 const char *module_name;
16933 struct type *type;
16934
16935 module_name = dwarf2_name (die, cu);
16936 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16937
16938 return set_die_type (die, type, cu);
16939 }
16940
16941 /* Read a Fortran module. */
16942
16943 static void
16944 read_module (struct die_info *die, struct dwarf2_cu *cu)
16945 {
16946 struct die_info *child_die = die->child;
16947 struct type *type;
16948
16949 type = read_type_die (die, cu);
16950 new_symbol (die, type, cu);
16951
16952 while (child_die && child_die->tag)
16953 {
16954 process_die (child_die, cu);
16955 child_die = sibling_die (child_die);
16956 }
16957 }
16958
16959 /* Return the name of the namespace represented by DIE. Set
16960 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16961 namespace. */
16962
16963 static const char *
16964 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16965 {
16966 struct die_info *current_die;
16967 const char *name = NULL;
16968
16969 /* Loop through the extensions until we find a name. */
16970
16971 for (current_die = die;
16972 current_die != NULL;
16973 current_die = dwarf2_extension (die, &cu))
16974 {
16975 /* We don't use dwarf2_name here so that we can detect the absence
16976 of a name -> anonymous namespace. */
16977 name = dwarf2_string_attr (die, DW_AT_name, cu);
16978
16979 if (name != NULL)
16980 break;
16981 }
16982
16983 /* Is it an anonymous namespace? */
16984
16985 *is_anonymous = (name == NULL);
16986 if (*is_anonymous)
16987 name = CP_ANONYMOUS_NAMESPACE_STR;
16988
16989 return name;
16990 }
16991
16992 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16993 the user defined type vector. */
16994
16995 static struct type *
16996 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16997 {
16998 struct gdbarch *gdbarch
16999 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
17000 struct comp_unit_head *cu_header = &cu->header;
17001 struct type *type;
17002 struct attribute *attr_byte_size;
17003 struct attribute *attr_address_class;
17004 int byte_size, addr_class;
17005 struct type *target_type;
17006
17007 target_type = die_type (die, cu);
17008
17009 /* The die_type call above may have already set the type for this DIE. */
17010 type = get_die_type (die, cu);
17011 if (type)
17012 return type;
17013
17014 type = lookup_pointer_type (target_type);
17015
17016 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17017 if (attr_byte_size)
17018 byte_size = DW_UNSND (attr_byte_size);
17019 else
17020 byte_size = cu_header->addr_size;
17021
17022 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17023 if (attr_address_class)
17024 addr_class = DW_UNSND (attr_address_class);
17025 else
17026 addr_class = DW_ADDR_none;
17027
17028 ULONGEST alignment = get_alignment (cu, die);
17029
17030 /* If the pointer size, alignment, or address class is different
17031 than the default, create a type variant marked as such and set
17032 the length accordingly. */
17033 if (TYPE_LENGTH (type) != byte_size
17034 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17035 && alignment != TYPE_RAW_ALIGN (type))
17036 || addr_class != DW_ADDR_none)
17037 {
17038 if (gdbarch_address_class_type_flags_p (gdbarch))
17039 {
17040 int type_flags;
17041
17042 type_flags = gdbarch_address_class_type_flags
17043 (gdbarch, byte_size, addr_class);
17044 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17045 == 0);
17046 type = make_type_with_address_space (type, type_flags);
17047 }
17048 else if (TYPE_LENGTH (type) != byte_size)
17049 {
17050 complaint (_("invalid pointer size %d"), byte_size);
17051 }
17052 else if (TYPE_RAW_ALIGN (type) != alignment)
17053 {
17054 complaint (_("Invalid DW_AT_alignment"
17055 " - DIE at %s [in module %s]"),
17056 sect_offset_str (die->sect_off),
17057 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17058 }
17059 else
17060 {
17061 /* Should we also complain about unhandled address classes? */
17062 }
17063 }
17064
17065 TYPE_LENGTH (type) = byte_size;
17066 set_type_align (type, alignment);
17067 return set_die_type (die, type, cu);
17068 }
17069
17070 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17071 the user defined type vector. */
17072
17073 static struct type *
17074 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17075 {
17076 struct type *type;
17077 struct type *to_type;
17078 struct type *domain;
17079
17080 to_type = die_type (die, cu);
17081 domain = die_containing_type (die, cu);
17082
17083 /* The calls above may have already set the type for this DIE. */
17084 type = get_die_type (die, cu);
17085 if (type)
17086 return type;
17087
17088 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17089 type = lookup_methodptr_type (to_type);
17090 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17091 {
17092 struct type *new_type
17093 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
17094
17095 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17096 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17097 TYPE_VARARGS (to_type));
17098 type = lookup_methodptr_type (new_type);
17099 }
17100 else
17101 type = lookup_memberptr_type (to_type, domain);
17102
17103 return set_die_type (die, type, cu);
17104 }
17105
17106 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17107 the user defined type vector. */
17108
17109 static struct type *
17110 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17111 enum type_code refcode)
17112 {
17113 struct comp_unit_head *cu_header = &cu->header;
17114 struct type *type, *target_type;
17115 struct attribute *attr;
17116
17117 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17118
17119 target_type = die_type (die, cu);
17120
17121 /* The die_type call above may have already set the type for this DIE. */
17122 type = get_die_type (die, cu);
17123 if (type)
17124 return type;
17125
17126 type = lookup_reference_type (target_type, refcode);
17127 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17128 if (attr)
17129 {
17130 TYPE_LENGTH (type) = DW_UNSND (attr);
17131 }
17132 else
17133 {
17134 TYPE_LENGTH (type) = cu_header->addr_size;
17135 }
17136 maybe_set_alignment (cu, die, type);
17137 return set_die_type (die, type, cu);
17138 }
17139
17140 /* Add the given cv-qualifiers to the element type of the array. GCC
17141 outputs DWARF type qualifiers that apply to an array, not the
17142 element type. But GDB relies on the array element type to carry
17143 the cv-qualifiers. This mimics section 6.7.3 of the C99
17144 specification. */
17145
17146 static struct type *
17147 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17148 struct type *base_type, int cnst, int voltl)
17149 {
17150 struct type *el_type, *inner_array;
17151
17152 base_type = copy_type (base_type);
17153 inner_array = base_type;
17154
17155 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17156 {
17157 TYPE_TARGET_TYPE (inner_array) =
17158 copy_type (TYPE_TARGET_TYPE (inner_array));
17159 inner_array = TYPE_TARGET_TYPE (inner_array);
17160 }
17161
17162 el_type = TYPE_TARGET_TYPE (inner_array);
17163 cnst |= TYPE_CONST (el_type);
17164 voltl |= TYPE_VOLATILE (el_type);
17165 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17166
17167 return set_die_type (die, base_type, cu);
17168 }
17169
17170 static struct type *
17171 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17172 {
17173 struct type *base_type, *cv_type;
17174
17175 base_type = die_type (die, cu);
17176
17177 /* The die_type call above may have already set the type for this DIE. */
17178 cv_type = get_die_type (die, cu);
17179 if (cv_type)
17180 return cv_type;
17181
17182 /* In case the const qualifier is applied to an array type, the element type
17183 is so qualified, not the array type (section 6.7.3 of C99). */
17184 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17185 return add_array_cv_type (die, cu, base_type, 1, 0);
17186
17187 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17188 return set_die_type (die, cv_type, cu);
17189 }
17190
17191 static struct type *
17192 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17193 {
17194 struct type *base_type, *cv_type;
17195
17196 base_type = die_type (die, cu);
17197
17198 /* The die_type call above may have already set the type for this DIE. */
17199 cv_type = get_die_type (die, cu);
17200 if (cv_type)
17201 return cv_type;
17202
17203 /* In case the volatile qualifier is applied to an array type, the
17204 element type is so qualified, not the array type (section 6.7.3
17205 of C99). */
17206 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17207 return add_array_cv_type (die, cu, base_type, 0, 1);
17208
17209 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17210 return set_die_type (die, cv_type, cu);
17211 }
17212
17213 /* Handle DW_TAG_restrict_type. */
17214
17215 static struct type *
17216 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17217 {
17218 struct type *base_type, *cv_type;
17219
17220 base_type = die_type (die, cu);
17221
17222 /* The die_type call above may have already set the type for this DIE. */
17223 cv_type = get_die_type (die, cu);
17224 if (cv_type)
17225 return cv_type;
17226
17227 cv_type = make_restrict_type (base_type);
17228 return set_die_type (die, cv_type, cu);
17229 }
17230
17231 /* Handle DW_TAG_atomic_type. */
17232
17233 static struct type *
17234 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17235 {
17236 struct type *base_type, *cv_type;
17237
17238 base_type = die_type (die, cu);
17239
17240 /* The die_type call above may have already set the type for this DIE. */
17241 cv_type = get_die_type (die, cu);
17242 if (cv_type)
17243 return cv_type;
17244
17245 cv_type = make_atomic_type (base_type);
17246 return set_die_type (die, cv_type, cu);
17247 }
17248
17249 /* Extract all information from a DW_TAG_string_type DIE and add to
17250 the user defined type vector. It isn't really a user defined type,
17251 but it behaves like one, with other DIE's using an AT_user_def_type
17252 attribute to reference it. */
17253
17254 static struct type *
17255 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17256 {
17257 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17258 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17259 struct type *type, *range_type, *index_type, *char_type;
17260 struct attribute *attr;
17261 unsigned int length;
17262
17263 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17264 if (attr)
17265 {
17266 length = DW_UNSND (attr);
17267 }
17268 else
17269 {
17270 /* Check for the DW_AT_byte_size attribute. */
17271 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17272 if (attr)
17273 {
17274 length = DW_UNSND (attr);
17275 }
17276 else
17277 {
17278 length = 1;
17279 }
17280 }
17281
17282 index_type = objfile_type (objfile)->builtin_int;
17283 range_type = create_static_range_type (NULL, index_type, 1, length);
17284 char_type = language_string_char_type (cu->language_defn, gdbarch);
17285 type = create_string_type (NULL, char_type, range_type);
17286
17287 return set_die_type (die, type, cu);
17288 }
17289
17290 /* Assuming that DIE corresponds to a function, returns nonzero
17291 if the function is prototyped. */
17292
17293 static int
17294 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17295 {
17296 struct attribute *attr;
17297
17298 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17299 if (attr && (DW_UNSND (attr) != 0))
17300 return 1;
17301
17302 /* The DWARF standard implies that the DW_AT_prototyped attribute
17303 is only meaninful for C, but the concept also extends to other
17304 languages that allow unprototyped functions (Eg: Objective C).
17305 For all other languages, assume that functions are always
17306 prototyped. */
17307 if (cu->language != language_c
17308 && cu->language != language_objc
17309 && cu->language != language_opencl)
17310 return 1;
17311
17312 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17313 prototyped and unprototyped functions; default to prototyped,
17314 since that is more common in modern code (and RealView warns
17315 about unprototyped functions). */
17316 if (producer_is_realview (cu->producer))
17317 return 1;
17318
17319 return 0;
17320 }
17321
17322 /* Handle DIES due to C code like:
17323
17324 struct foo
17325 {
17326 int (*funcp)(int a, long l);
17327 int b;
17328 };
17329
17330 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17331
17332 static struct type *
17333 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17334 {
17335 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17336 struct type *type; /* Type that this function returns. */
17337 struct type *ftype; /* Function that returns above type. */
17338 struct attribute *attr;
17339
17340 type = die_type (die, cu);
17341
17342 /* The die_type call above may have already set the type for this DIE. */
17343 ftype = get_die_type (die, cu);
17344 if (ftype)
17345 return ftype;
17346
17347 ftype = lookup_function_type (type);
17348
17349 if (prototyped_function_p (die, cu))
17350 TYPE_PROTOTYPED (ftype) = 1;
17351
17352 /* Store the calling convention in the type if it's available in
17353 the subroutine die. Otherwise set the calling convention to
17354 the default value DW_CC_normal. */
17355 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17356 if (attr)
17357 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17358 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17359 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17360 else
17361 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17362
17363 /* Record whether the function returns normally to its caller or not
17364 if the DWARF producer set that information. */
17365 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17366 if (attr && (DW_UNSND (attr) != 0))
17367 TYPE_NO_RETURN (ftype) = 1;
17368
17369 /* We need to add the subroutine type to the die immediately so
17370 we don't infinitely recurse when dealing with parameters
17371 declared as the same subroutine type. */
17372 set_die_type (die, ftype, cu);
17373
17374 if (die->child != NULL)
17375 {
17376 struct type *void_type = objfile_type (objfile)->builtin_void;
17377 struct die_info *child_die;
17378 int nparams, iparams;
17379
17380 /* Count the number of parameters.
17381 FIXME: GDB currently ignores vararg functions, but knows about
17382 vararg member functions. */
17383 nparams = 0;
17384 child_die = die->child;
17385 while (child_die && child_die->tag)
17386 {
17387 if (child_die->tag == DW_TAG_formal_parameter)
17388 nparams++;
17389 else if (child_die->tag == DW_TAG_unspecified_parameters)
17390 TYPE_VARARGS (ftype) = 1;
17391 child_die = sibling_die (child_die);
17392 }
17393
17394 /* Allocate storage for parameters and fill them in. */
17395 TYPE_NFIELDS (ftype) = nparams;
17396 TYPE_FIELDS (ftype) = (struct field *)
17397 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17398
17399 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17400 even if we error out during the parameters reading below. */
17401 for (iparams = 0; iparams < nparams; iparams++)
17402 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17403
17404 iparams = 0;
17405 child_die = die->child;
17406 while (child_die && child_die->tag)
17407 {
17408 if (child_die->tag == DW_TAG_formal_parameter)
17409 {
17410 struct type *arg_type;
17411
17412 /* DWARF version 2 has no clean way to discern C++
17413 static and non-static member functions. G++ helps
17414 GDB by marking the first parameter for non-static
17415 member functions (which is the this pointer) as
17416 artificial. We pass this information to
17417 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17418
17419 DWARF version 3 added DW_AT_object_pointer, which GCC
17420 4.5 does not yet generate. */
17421 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17422 if (attr)
17423 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17424 else
17425 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17426 arg_type = die_type (child_die, cu);
17427
17428 /* RealView does not mark THIS as const, which the testsuite
17429 expects. GCC marks THIS as const in method definitions,
17430 but not in the class specifications (GCC PR 43053). */
17431 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17432 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17433 {
17434 int is_this = 0;
17435 struct dwarf2_cu *arg_cu = cu;
17436 const char *name = dwarf2_name (child_die, cu);
17437
17438 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17439 if (attr)
17440 {
17441 /* If the compiler emits this, use it. */
17442 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17443 is_this = 1;
17444 }
17445 else if (name && strcmp (name, "this") == 0)
17446 /* Function definitions will have the argument names. */
17447 is_this = 1;
17448 else if (name == NULL && iparams == 0)
17449 /* Declarations may not have the names, so like
17450 elsewhere in GDB, assume an artificial first
17451 argument is "this". */
17452 is_this = 1;
17453
17454 if (is_this)
17455 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17456 arg_type, 0);
17457 }
17458
17459 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17460 iparams++;
17461 }
17462 child_die = sibling_die (child_die);
17463 }
17464 }
17465
17466 return ftype;
17467 }
17468
17469 static struct type *
17470 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17471 {
17472 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17473 const char *name = NULL;
17474 struct type *this_type, *target_type;
17475
17476 name = dwarf2_full_name (NULL, die, cu);
17477 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17478 TYPE_TARGET_STUB (this_type) = 1;
17479 set_die_type (die, this_type, cu);
17480 target_type = die_type (die, cu);
17481 if (target_type != this_type)
17482 TYPE_TARGET_TYPE (this_type) = target_type;
17483 else
17484 {
17485 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17486 spec and cause infinite loops in GDB. */
17487 complaint (_("Self-referential DW_TAG_typedef "
17488 "- DIE at %s [in module %s]"),
17489 sect_offset_str (die->sect_off), objfile_name (objfile));
17490 TYPE_TARGET_TYPE (this_type) = NULL;
17491 }
17492 return this_type;
17493 }
17494
17495 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17496 (which may be different from NAME) to the architecture back-end to allow
17497 it to guess the correct format if necessary. */
17498
17499 static struct type *
17500 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17501 const char *name_hint)
17502 {
17503 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17504 const struct floatformat **format;
17505 struct type *type;
17506
17507 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17508 if (format)
17509 type = init_float_type (objfile, bits, name, format);
17510 else
17511 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17512
17513 return type;
17514 }
17515
17516 /* Allocate an integer type of size BITS and name NAME. */
17517
17518 static struct type *
17519 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17520 int bits, int unsigned_p, const char *name)
17521 {
17522 struct type *type;
17523
17524 /* Versions of Intel's C Compiler generate an integer type called "void"
17525 instead of using DW_TAG_unspecified_type. This has been seen on
17526 at least versions 14, 17, and 18. */
17527 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17528 && strcmp (name, "void") == 0)
17529 type = objfile_type (objfile)->builtin_void;
17530 else
17531 type = init_integer_type (objfile, bits, unsigned_p, name);
17532
17533 return type;
17534 }
17535
17536 /* Initialise and return a floating point type of size BITS suitable for
17537 use as a component of a complex number. The NAME_HINT is passed through
17538 when initialising the floating point type and is the name of the complex
17539 type.
17540
17541 As DWARF doesn't currently provide an explicit name for the components
17542 of a complex number, but it can be helpful to have these components
17543 named, we try to select a suitable name based on the size of the
17544 component. */
17545 static struct type *
17546 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17547 struct objfile *objfile,
17548 int bits, const char *name_hint)
17549 {
17550 gdbarch *gdbarch = get_objfile_arch (objfile);
17551 struct type *tt = nullptr;
17552
17553 /* Try to find a suitable floating point builtin type of size BITS.
17554 We're going to use the name of this type as the name for the complex
17555 target type that we are about to create. */
17556 switch (cu->language)
17557 {
17558 case language_fortran:
17559 switch (bits)
17560 {
17561 case 32:
17562 tt = builtin_f_type (gdbarch)->builtin_real;
17563 break;
17564 case 64:
17565 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17566 break;
17567 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17568 case 128:
17569 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17570 break;
17571 }
17572 break;
17573 default:
17574 switch (bits)
17575 {
17576 case 32:
17577 tt = builtin_type (gdbarch)->builtin_float;
17578 break;
17579 case 64:
17580 tt = builtin_type (gdbarch)->builtin_double;
17581 break;
17582 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17583 case 128:
17584 tt = builtin_type (gdbarch)->builtin_long_double;
17585 break;
17586 }
17587 break;
17588 }
17589
17590 /* If the type we found doesn't match the size we were looking for, then
17591 pretend we didn't find a type at all, the complex target type we
17592 create will then be nameless. */
17593 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17594 tt = nullptr;
17595
17596 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17597 return dwarf2_init_float_type (objfile, bits, name, name_hint);
17598 }
17599
17600 /* Find a representation of a given base type and install
17601 it in the TYPE field of the die. */
17602
17603 static struct type *
17604 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17605 {
17606 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17607 struct type *type;
17608 struct attribute *attr;
17609 int encoding = 0, bits = 0;
17610 const char *name;
17611
17612 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17613 if (attr)
17614 {
17615 encoding = DW_UNSND (attr);
17616 }
17617 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17618 if (attr)
17619 {
17620 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17621 }
17622 name = dwarf2_name (die, cu);
17623 if (!name)
17624 {
17625 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17626 }
17627
17628 switch (encoding)
17629 {
17630 case DW_ATE_address:
17631 /* Turn DW_ATE_address into a void * pointer. */
17632 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17633 type = init_pointer_type (objfile, bits, name, type);
17634 break;
17635 case DW_ATE_boolean:
17636 type = init_boolean_type (objfile, bits, 1, name);
17637 break;
17638 case DW_ATE_complex_float:
17639 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name);
17640 type = init_complex_type (objfile, name, type);
17641 break;
17642 case DW_ATE_decimal_float:
17643 type = init_decfloat_type (objfile, bits, name);
17644 break;
17645 case DW_ATE_float:
17646 type = dwarf2_init_float_type (objfile, bits, name, name);
17647 break;
17648 case DW_ATE_signed:
17649 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17650 break;
17651 case DW_ATE_unsigned:
17652 if (cu->language == language_fortran
17653 && name
17654 && startswith (name, "character("))
17655 type = init_character_type (objfile, bits, 1, name);
17656 else
17657 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17658 break;
17659 case DW_ATE_signed_char:
17660 if (cu->language == language_ada || cu->language == language_m2
17661 || cu->language == language_pascal
17662 || cu->language == language_fortran)
17663 type = init_character_type (objfile, bits, 0, name);
17664 else
17665 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17666 break;
17667 case DW_ATE_unsigned_char:
17668 if (cu->language == language_ada || cu->language == language_m2
17669 || cu->language == language_pascal
17670 || cu->language == language_fortran
17671 || cu->language == language_rust)
17672 type = init_character_type (objfile, bits, 1, name);
17673 else
17674 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17675 break;
17676 case DW_ATE_UTF:
17677 {
17678 gdbarch *arch = get_objfile_arch (objfile);
17679
17680 if (bits == 16)
17681 type = builtin_type (arch)->builtin_char16;
17682 else if (bits == 32)
17683 type = builtin_type (arch)->builtin_char32;
17684 else
17685 {
17686 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17687 bits);
17688 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17689 }
17690 return set_die_type (die, type, cu);
17691 }
17692 break;
17693
17694 default:
17695 complaint (_("unsupported DW_AT_encoding: '%s'"),
17696 dwarf_type_encoding_name (encoding));
17697 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17698 break;
17699 }
17700
17701 if (name && strcmp (name, "char") == 0)
17702 TYPE_NOSIGN (type) = 1;
17703
17704 maybe_set_alignment (cu, die, type);
17705
17706 return set_die_type (die, type, cu);
17707 }
17708
17709 /* Parse dwarf attribute if it's a block, reference or constant and put the
17710 resulting value of the attribute into struct bound_prop.
17711 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17712
17713 static int
17714 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17715 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17716 struct type *default_type)
17717 {
17718 struct dwarf2_property_baton *baton;
17719 struct obstack *obstack
17720 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17721
17722 gdb_assert (default_type != NULL);
17723
17724 if (attr == NULL || prop == NULL)
17725 return 0;
17726
17727 if (attr_form_is_block (attr))
17728 {
17729 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17730 baton->property_type = default_type;
17731 baton->locexpr.per_cu = cu->per_cu;
17732 baton->locexpr.size = DW_BLOCK (attr)->size;
17733 baton->locexpr.data = DW_BLOCK (attr)->data;
17734 baton->locexpr.is_reference = false;
17735 prop->data.baton = baton;
17736 prop->kind = PROP_LOCEXPR;
17737 gdb_assert (prop->data.baton != NULL);
17738 }
17739 else if (attr_form_is_ref (attr))
17740 {
17741 struct dwarf2_cu *target_cu = cu;
17742 struct die_info *target_die;
17743 struct attribute *target_attr;
17744
17745 target_die = follow_die_ref (die, attr, &target_cu);
17746 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17747 if (target_attr == NULL)
17748 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17749 target_cu);
17750 if (target_attr == NULL)
17751 return 0;
17752
17753 switch (target_attr->name)
17754 {
17755 case DW_AT_location:
17756 if (attr_form_is_section_offset (target_attr))
17757 {
17758 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17759 baton->property_type = die_type (target_die, target_cu);
17760 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17761 prop->data.baton = baton;
17762 prop->kind = PROP_LOCLIST;
17763 gdb_assert (prop->data.baton != NULL);
17764 }
17765 else if (attr_form_is_block (target_attr))
17766 {
17767 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17768 baton->property_type = die_type (target_die, target_cu);
17769 baton->locexpr.per_cu = cu->per_cu;
17770 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17771 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17772 baton->locexpr.is_reference = true;
17773 prop->data.baton = baton;
17774 prop->kind = PROP_LOCEXPR;
17775 gdb_assert (prop->data.baton != NULL);
17776 }
17777 else
17778 {
17779 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17780 "dynamic property");
17781 return 0;
17782 }
17783 break;
17784 case DW_AT_data_member_location:
17785 {
17786 LONGEST offset;
17787
17788 if (!handle_data_member_location (target_die, target_cu,
17789 &offset))
17790 return 0;
17791
17792 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17793 baton->property_type = read_type_die (target_die->parent,
17794 target_cu);
17795 baton->offset_info.offset = offset;
17796 baton->offset_info.type = die_type (target_die, target_cu);
17797 prop->data.baton = baton;
17798 prop->kind = PROP_ADDR_OFFSET;
17799 break;
17800 }
17801 }
17802 }
17803 else if (attr_form_is_constant (attr))
17804 {
17805 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17806 prop->kind = PROP_CONST;
17807 }
17808 else
17809 {
17810 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17811 dwarf2_name (die, cu));
17812 return 0;
17813 }
17814
17815 return 1;
17816 }
17817
17818 /* Find an integer type the same size as the address size given in the
17819 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
17820 is unsigned or not. */
17821
17822 static struct type *
17823 dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
17824 bool unsigned_p)
17825 {
17826 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
17827 int addr_size = dwarf2_per_cu_addr_size (per_cu);
17828 struct type *int_type;
17829
17830 /* Helper macro to examine the various builtin types. */
17831 #define TRY_TYPE(F) \
17832 int_type = (unsigned_p \
17833 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17834 : objfile_type (objfile)->builtin_ ## F); \
17835 if (int_type != NULL && TYPE_LENGTH (int_type) == addr_size) \
17836 return int_type
17837
17838 TRY_TYPE (char);
17839 TRY_TYPE (short);
17840 TRY_TYPE (int);
17841 TRY_TYPE (long);
17842 TRY_TYPE (long_long);
17843
17844 #undef TRY_TYPE
17845
17846 gdb_assert_not_reached ("unable to find suitable integer type");
17847 }
17848
17849 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17850 present (which is valid) then compute the default type based on the
17851 compilation units address size. */
17852
17853 static struct type *
17854 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17855 {
17856 struct type *index_type = die_type (die, cu);
17857
17858 /* Dwarf-2 specifications explicitly allows to create subrange types
17859 without specifying a base type.
17860 In that case, the base type must be set to the type of
17861 the lower bound, upper bound or count, in that order, if any of these
17862 three attributes references an object that has a type.
17863 If no base type is found, the Dwarf-2 specifications say that
17864 a signed integer type of size equal to the size of an address should
17865 be used.
17866 For the following C code: `extern char gdb_int [];'
17867 GCC produces an empty range DIE.
17868 FIXME: muller/2010-05-28: Possible references to object for low bound,
17869 high bound or count are not yet handled by this code. */
17870 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17871 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
17872
17873 return index_type;
17874 }
17875
17876 /* Read the given DW_AT_subrange DIE. */
17877
17878 static struct type *
17879 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17880 {
17881 struct type *base_type, *orig_base_type;
17882 struct type *range_type;
17883 struct attribute *attr;
17884 struct dynamic_prop low, high;
17885 int low_default_is_valid;
17886 int high_bound_is_count = 0;
17887 const char *name;
17888 ULONGEST negative_mask;
17889
17890 orig_base_type = read_subrange_index_type (die, cu);
17891
17892 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17893 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17894 creating the range type, but we use the result of check_typedef
17895 when examining properties of the type. */
17896 base_type = check_typedef (orig_base_type);
17897
17898 /* The die_type call above may have already set the type for this DIE. */
17899 range_type = get_die_type (die, cu);
17900 if (range_type)
17901 return range_type;
17902
17903 low.kind = PROP_CONST;
17904 high.kind = PROP_CONST;
17905 high.data.const_val = 0;
17906
17907 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17908 omitting DW_AT_lower_bound. */
17909 switch (cu->language)
17910 {
17911 case language_c:
17912 case language_cplus:
17913 low.data.const_val = 0;
17914 low_default_is_valid = 1;
17915 break;
17916 case language_fortran:
17917 low.data.const_val = 1;
17918 low_default_is_valid = 1;
17919 break;
17920 case language_d:
17921 case language_objc:
17922 case language_rust:
17923 low.data.const_val = 0;
17924 low_default_is_valid = (cu->header.version >= 4);
17925 break;
17926 case language_ada:
17927 case language_m2:
17928 case language_pascal:
17929 low.data.const_val = 1;
17930 low_default_is_valid = (cu->header.version >= 4);
17931 break;
17932 default:
17933 low.data.const_val = 0;
17934 low_default_is_valid = 0;
17935 break;
17936 }
17937
17938 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17939 if (attr)
17940 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17941 else if (!low_default_is_valid)
17942 complaint (_("Missing DW_AT_lower_bound "
17943 "- DIE at %s [in module %s]"),
17944 sect_offset_str (die->sect_off),
17945 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17946
17947 struct attribute *attr_ub, *attr_count;
17948 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17949 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17950 {
17951 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17952 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17953 {
17954 /* If bounds are constant do the final calculation here. */
17955 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17956 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17957 else
17958 high_bound_is_count = 1;
17959 }
17960 else
17961 {
17962 if (attr_ub != NULL)
17963 complaint (_("Unresolved DW_AT_upper_bound "
17964 "- DIE at %s [in module %s]"),
17965 sect_offset_str (die->sect_off),
17966 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17967 if (attr_count != NULL)
17968 complaint (_("Unresolved DW_AT_count "
17969 "- DIE at %s [in module %s]"),
17970 sect_offset_str (die->sect_off),
17971 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17972 }
17973 }
17974
17975 LONGEST bias = 0;
17976 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17977 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
17978 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17979
17980 /* Normally, the DWARF producers are expected to use a signed
17981 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17982 But this is unfortunately not always the case, as witnessed
17983 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17984 is used instead. To work around that ambiguity, we treat
17985 the bounds as signed, and thus sign-extend their values, when
17986 the base type is signed. */
17987 negative_mask =
17988 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17989 if (low.kind == PROP_CONST
17990 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17991 low.data.const_val |= negative_mask;
17992 if (high.kind == PROP_CONST
17993 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17994 high.data.const_val |= negative_mask;
17995
17996 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17997
17998 if (high_bound_is_count)
17999 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18000
18001 /* Ada expects an empty array on no boundary attributes. */
18002 if (attr == NULL && cu->language != language_ada)
18003 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
18004
18005 name = dwarf2_name (die, cu);
18006 if (name)
18007 TYPE_NAME (range_type) = name;
18008
18009 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18010 if (attr)
18011 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18012
18013 maybe_set_alignment (cu, die, range_type);
18014
18015 set_die_type (die, range_type, cu);
18016
18017 /* set_die_type should be already done. */
18018 set_descriptive_type (range_type, die, cu);
18019
18020 return range_type;
18021 }
18022
18023 static struct type *
18024 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18025 {
18026 struct type *type;
18027
18028 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18029 NULL);
18030 TYPE_NAME (type) = dwarf2_name (die, cu);
18031
18032 /* In Ada, an unspecified type is typically used when the description
18033 of the type is defered to a different unit. When encountering
18034 such a type, we treat it as a stub, and try to resolve it later on,
18035 when needed. */
18036 if (cu->language == language_ada)
18037 TYPE_STUB (type) = 1;
18038
18039 return set_die_type (die, type, cu);
18040 }
18041
18042 /* Read a single die and all its descendents. Set the die's sibling
18043 field to NULL; set other fields in the die correctly, and set all
18044 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18045 location of the info_ptr after reading all of those dies. PARENT
18046 is the parent of the die in question. */
18047
18048 static struct die_info *
18049 read_die_and_children (const struct die_reader_specs *reader,
18050 const gdb_byte *info_ptr,
18051 const gdb_byte **new_info_ptr,
18052 struct die_info *parent)
18053 {
18054 struct die_info *die;
18055 const gdb_byte *cur_ptr;
18056 int has_children;
18057
18058 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
18059 if (die == NULL)
18060 {
18061 *new_info_ptr = cur_ptr;
18062 return NULL;
18063 }
18064 store_in_ref_table (die, reader->cu);
18065
18066 if (has_children)
18067 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18068 else
18069 {
18070 die->child = NULL;
18071 *new_info_ptr = cur_ptr;
18072 }
18073
18074 die->sibling = NULL;
18075 die->parent = parent;
18076 return die;
18077 }
18078
18079 /* Read a die, all of its descendents, and all of its siblings; set
18080 all of the fields of all of the dies correctly. Arguments are as
18081 in read_die_and_children. */
18082
18083 static struct die_info *
18084 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18085 const gdb_byte *info_ptr,
18086 const gdb_byte **new_info_ptr,
18087 struct die_info *parent)
18088 {
18089 struct die_info *first_die, *last_sibling;
18090 const gdb_byte *cur_ptr;
18091
18092 cur_ptr = info_ptr;
18093 first_die = last_sibling = NULL;
18094
18095 while (1)
18096 {
18097 struct die_info *die
18098 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18099
18100 if (die == NULL)
18101 {
18102 *new_info_ptr = cur_ptr;
18103 return first_die;
18104 }
18105
18106 if (!first_die)
18107 first_die = die;
18108 else
18109 last_sibling->sibling = die;
18110
18111 last_sibling = die;
18112 }
18113 }
18114
18115 /* Read a die, all of its descendents, and all of its siblings; set
18116 all of the fields of all of the dies correctly. Arguments are as
18117 in read_die_and_children.
18118 This the main entry point for reading a DIE and all its children. */
18119
18120 static struct die_info *
18121 read_die_and_siblings (const struct die_reader_specs *reader,
18122 const gdb_byte *info_ptr,
18123 const gdb_byte **new_info_ptr,
18124 struct die_info *parent)
18125 {
18126 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18127 new_info_ptr, parent);
18128
18129 if (dwarf_die_debug)
18130 {
18131 fprintf_unfiltered (gdb_stdlog,
18132 "Read die from %s@0x%x of %s:\n",
18133 get_section_name (reader->die_section),
18134 (unsigned) (info_ptr - reader->die_section->buffer),
18135 bfd_get_filename (reader->abfd));
18136 dump_die (die, dwarf_die_debug);
18137 }
18138
18139 return die;
18140 }
18141
18142 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18143 attributes.
18144 The caller is responsible for filling in the extra attributes
18145 and updating (*DIEP)->num_attrs.
18146 Set DIEP to point to a newly allocated die with its information,
18147 except for its child, sibling, and parent fields.
18148 Set HAS_CHILDREN to tell whether the die has children or not. */
18149
18150 static const gdb_byte *
18151 read_full_die_1 (const struct die_reader_specs *reader,
18152 struct die_info **diep, const gdb_byte *info_ptr,
18153 int *has_children, int num_extra_attrs)
18154 {
18155 unsigned int abbrev_number, bytes_read, i;
18156 struct abbrev_info *abbrev;
18157 struct die_info *die;
18158 struct dwarf2_cu *cu = reader->cu;
18159 bfd *abfd = reader->abfd;
18160
18161 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18162 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18163 info_ptr += bytes_read;
18164 if (!abbrev_number)
18165 {
18166 *diep = NULL;
18167 *has_children = 0;
18168 return info_ptr;
18169 }
18170
18171 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18172 if (!abbrev)
18173 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18174 abbrev_number,
18175 bfd_get_filename (abfd));
18176
18177 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18178 die->sect_off = sect_off;
18179 die->tag = abbrev->tag;
18180 die->abbrev = abbrev_number;
18181
18182 /* Make the result usable.
18183 The caller needs to update num_attrs after adding the extra
18184 attributes. */
18185 die->num_attrs = abbrev->num_attrs;
18186
18187 for (i = 0; i < abbrev->num_attrs; ++i)
18188 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18189 info_ptr);
18190
18191 *diep = die;
18192 *has_children = abbrev->has_children;
18193 return info_ptr;
18194 }
18195
18196 /* Read a die and all its attributes.
18197 Set DIEP to point to a newly allocated die with its information,
18198 except for its child, sibling, and parent fields.
18199 Set HAS_CHILDREN to tell whether the die has children or not. */
18200
18201 static const gdb_byte *
18202 read_full_die (const struct die_reader_specs *reader,
18203 struct die_info **diep, const gdb_byte *info_ptr,
18204 int *has_children)
18205 {
18206 const gdb_byte *result;
18207
18208 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18209
18210 if (dwarf_die_debug)
18211 {
18212 fprintf_unfiltered (gdb_stdlog,
18213 "Read die from %s@0x%x of %s:\n",
18214 get_section_name (reader->die_section),
18215 (unsigned) (info_ptr - reader->die_section->buffer),
18216 bfd_get_filename (reader->abfd));
18217 dump_die (*diep, dwarf_die_debug);
18218 }
18219
18220 return result;
18221 }
18222 \f
18223 /* Abbreviation tables.
18224
18225 In DWARF version 2, the description of the debugging information is
18226 stored in a separate .debug_abbrev section. Before we read any
18227 dies from a section we read in all abbreviations and install them
18228 in a hash table. */
18229
18230 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18231
18232 struct abbrev_info *
18233 abbrev_table::alloc_abbrev ()
18234 {
18235 struct abbrev_info *abbrev;
18236
18237 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18238 memset (abbrev, 0, sizeof (struct abbrev_info));
18239
18240 return abbrev;
18241 }
18242
18243 /* Add an abbreviation to the table. */
18244
18245 void
18246 abbrev_table::add_abbrev (unsigned int abbrev_number,
18247 struct abbrev_info *abbrev)
18248 {
18249 unsigned int hash_number;
18250
18251 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18252 abbrev->next = m_abbrevs[hash_number];
18253 m_abbrevs[hash_number] = abbrev;
18254 }
18255
18256 /* Look up an abbrev in the table.
18257 Returns NULL if the abbrev is not found. */
18258
18259 struct abbrev_info *
18260 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18261 {
18262 unsigned int hash_number;
18263 struct abbrev_info *abbrev;
18264
18265 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18266 abbrev = m_abbrevs[hash_number];
18267
18268 while (abbrev)
18269 {
18270 if (abbrev->number == abbrev_number)
18271 return abbrev;
18272 abbrev = abbrev->next;
18273 }
18274 return NULL;
18275 }
18276
18277 /* Read in an abbrev table. */
18278
18279 static abbrev_table_up
18280 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18281 struct dwarf2_section_info *section,
18282 sect_offset sect_off)
18283 {
18284 struct objfile *objfile = dwarf2_per_objfile->objfile;
18285 bfd *abfd = get_section_bfd_owner (section);
18286 const gdb_byte *abbrev_ptr;
18287 struct abbrev_info *cur_abbrev;
18288 unsigned int abbrev_number, bytes_read, abbrev_name;
18289 unsigned int abbrev_form;
18290 struct attr_abbrev *cur_attrs;
18291 unsigned int allocated_attrs;
18292
18293 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18294
18295 dwarf2_read_section (objfile, section);
18296 abbrev_ptr = section->buffer + to_underlying (sect_off);
18297 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18298 abbrev_ptr += bytes_read;
18299
18300 allocated_attrs = ATTR_ALLOC_CHUNK;
18301 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18302
18303 /* Loop until we reach an abbrev number of 0. */
18304 while (abbrev_number)
18305 {
18306 cur_abbrev = abbrev_table->alloc_abbrev ();
18307
18308 /* read in abbrev header */
18309 cur_abbrev->number = abbrev_number;
18310 cur_abbrev->tag
18311 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18312 abbrev_ptr += bytes_read;
18313 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18314 abbrev_ptr += 1;
18315
18316 /* now read in declarations */
18317 for (;;)
18318 {
18319 LONGEST implicit_const;
18320
18321 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18322 abbrev_ptr += bytes_read;
18323 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18324 abbrev_ptr += bytes_read;
18325 if (abbrev_form == DW_FORM_implicit_const)
18326 {
18327 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18328 &bytes_read);
18329 abbrev_ptr += bytes_read;
18330 }
18331 else
18332 {
18333 /* Initialize it due to a false compiler warning. */
18334 implicit_const = -1;
18335 }
18336
18337 if (abbrev_name == 0)
18338 break;
18339
18340 if (cur_abbrev->num_attrs == allocated_attrs)
18341 {
18342 allocated_attrs += ATTR_ALLOC_CHUNK;
18343 cur_attrs
18344 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18345 }
18346
18347 cur_attrs[cur_abbrev->num_attrs].name
18348 = (enum dwarf_attribute) abbrev_name;
18349 cur_attrs[cur_abbrev->num_attrs].form
18350 = (enum dwarf_form) abbrev_form;
18351 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18352 ++cur_abbrev->num_attrs;
18353 }
18354
18355 cur_abbrev->attrs =
18356 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18357 cur_abbrev->num_attrs);
18358 memcpy (cur_abbrev->attrs, cur_attrs,
18359 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18360
18361 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18362
18363 /* Get next abbreviation.
18364 Under Irix6 the abbreviations for a compilation unit are not
18365 always properly terminated with an abbrev number of 0.
18366 Exit loop if we encounter an abbreviation which we have
18367 already read (which means we are about to read the abbreviations
18368 for the next compile unit) or if the end of the abbreviation
18369 table is reached. */
18370 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18371 break;
18372 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18373 abbrev_ptr += bytes_read;
18374 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18375 break;
18376 }
18377
18378 xfree (cur_attrs);
18379 return abbrev_table;
18380 }
18381
18382 /* Returns nonzero if TAG represents a type that we might generate a partial
18383 symbol for. */
18384
18385 static int
18386 is_type_tag_for_partial (int tag)
18387 {
18388 switch (tag)
18389 {
18390 #if 0
18391 /* Some types that would be reasonable to generate partial symbols for,
18392 that we don't at present. */
18393 case DW_TAG_array_type:
18394 case DW_TAG_file_type:
18395 case DW_TAG_ptr_to_member_type:
18396 case DW_TAG_set_type:
18397 case DW_TAG_string_type:
18398 case DW_TAG_subroutine_type:
18399 #endif
18400 case DW_TAG_base_type:
18401 case DW_TAG_class_type:
18402 case DW_TAG_interface_type:
18403 case DW_TAG_enumeration_type:
18404 case DW_TAG_structure_type:
18405 case DW_TAG_subrange_type:
18406 case DW_TAG_typedef:
18407 case DW_TAG_union_type:
18408 return 1;
18409 default:
18410 return 0;
18411 }
18412 }
18413
18414 /* Load all DIEs that are interesting for partial symbols into memory. */
18415
18416 static struct partial_die_info *
18417 load_partial_dies (const struct die_reader_specs *reader,
18418 const gdb_byte *info_ptr, int building_psymtab)
18419 {
18420 struct dwarf2_cu *cu = reader->cu;
18421 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18422 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18423 unsigned int bytes_read;
18424 unsigned int load_all = 0;
18425 int nesting_level = 1;
18426
18427 parent_die = NULL;
18428 last_die = NULL;
18429
18430 gdb_assert (cu->per_cu != NULL);
18431 if (cu->per_cu->load_all_dies)
18432 load_all = 1;
18433
18434 cu->partial_dies
18435 = htab_create_alloc_ex (cu->header.length / 12,
18436 partial_die_hash,
18437 partial_die_eq,
18438 NULL,
18439 &cu->comp_unit_obstack,
18440 hashtab_obstack_allocate,
18441 dummy_obstack_deallocate);
18442
18443 while (1)
18444 {
18445 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18446
18447 /* A NULL abbrev means the end of a series of children. */
18448 if (abbrev == NULL)
18449 {
18450 if (--nesting_level == 0)
18451 return first_die;
18452
18453 info_ptr += bytes_read;
18454 last_die = parent_die;
18455 parent_die = parent_die->die_parent;
18456 continue;
18457 }
18458
18459 /* Check for template arguments. We never save these; if
18460 they're seen, we just mark the parent, and go on our way. */
18461 if (parent_die != NULL
18462 && cu->language == language_cplus
18463 && (abbrev->tag == DW_TAG_template_type_param
18464 || abbrev->tag == DW_TAG_template_value_param))
18465 {
18466 parent_die->has_template_arguments = 1;
18467
18468 if (!load_all)
18469 {
18470 /* We don't need a partial DIE for the template argument. */
18471 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18472 continue;
18473 }
18474 }
18475
18476 /* We only recurse into c++ subprograms looking for template arguments.
18477 Skip their other children. */
18478 if (!load_all
18479 && cu->language == language_cplus
18480 && parent_die != NULL
18481 && parent_die->tag == DW_TAG_subprogram)
18482 {
18483 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18484 continue;
18485 }
18486
18487 /* Check whether this DIE is interesting enough to save. Normally
18488 we would not be interested in members here, but there may be
18489 later variables referencing them via DW_AT_specification (for
18490 static members). */
18491 if (!load_all
18492 && !is_type_tag_for_partial (abbrev->tag)
18493 && abbrev->tag != DW_TAG_constant
18494 && abbrev->tag != DW_TAG_enumerator
18495 && abbrev->tag != DW_TAG_subprogram
18496 && abbrev->tag != DW_TAG_inlined_subroutine
18497 && abbrev->tag != DW_TAG_lexical_block
18498 && abbrev->tag != DW_TAG_variable
18499 && abbrev->tag != DW_TAG_namespace
18500 && abbrev->tag != DW_TAG_module
18501 && abbrev->tag != DW_TAG_member
18502 && abbrev->tag != DW_TAG_imported_unit
18503 && abbrev->tag != DW_TAG_imported_declaration)
18504 {
18505 /* Otherwise we skip to the next sibling, if any. */
18506 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18507 continue;
18508 }
18509
18510 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18511 abbrev);
18512
18513 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18514
18515 /* This two-pass algorithm for processing partial symbols has a
18516 high cost in cache pressure. Thus, handle some simple cases
18517 here which cover the majority of C partial symbols. DIEs
18518 which neither have specification tags in them, nor could have
18519 specification tags elsewhere pointing at them, can simply be
18520 processed and discarded.
18521
18522 This segment is also optional; scan_partial_symbols and
18523 add_partial_symbol will handle these DIEs if we chain
18524 them in normally. When compilers which do not emit large
18525 quantities of duplicate debug information are more common,
18526 this code can probably be removed. */
18527
18528 /* Any complete simple types at the top level (pretty much all
18529 of them, for a language without namespaces), can be processed
18530 directly. */
18531 if (parent_die == NULL
18532 && pdi.has_specification == 0
18533 && pdi.is_declaration == 0
18534 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18535 || pdi.tag == DW_TAG_base_type
18536 || pdi.tag == DW_TAG_subrange_type))
18537 {
18538 if (building_psymtab && pdi.name != NULL)
18539 add_psymbol_to_list (pdi.name, strlen (pdi.name), false,
18540 VAR_DOMAIN, LOC_TYPEDEF, -1,
18541 psymbol_placement::STATIC,
18542 0, cu->language, objfile);
18543 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18544 continue;
18545 }
18546
18547 /* The exception for DW_TAG_typedef with has_children above is
18548 a workaround of GCC PR debug/47510. In the case of this complaint
18549 type_name_or_error will error on such types later.
18550
18551 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18552 it could not find the child DIEs referenced later, this is checked
18553 above. In correct DWARF DW_TAG_typedef should have no children. */
18554
18555 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18556 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18557 "- DIE at %s [in module %s]"),
18558 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18559
18560 /* If we're at the second level, and we're an enumerator, and
18561 our parent has no specification (meaning possibly lives in a
18562 namespace elsewhere), then we can add the partial symbol now
18563 instead of queueing it. */
18564 if (pdi.tag == DW_TAG_enumerator
18565 && parent_die != NULL
18566 && parent_die->die_parent == NULL
18567 && parent_die->tag == DW_TAG_enumeration_type
18568 && parent_die->has_specification == 0)
18569 {
18570 if (pdi.name == NULL)
18571 complaint (_("malformed enumerator DIE ignored"));
18572 else if (building_psymtab)
18573 add_psymbol_to_list (pdi.name, strlen (pdi.name), false,
18574 VAR_DOMAIN, LOC_CONST, -1,
18575 cu->language == language_cplus
18576 ? psymbol_placement::GLOBAL
18577 : psymbol_placement::STATIC,
18578 0, cu->language, objfile);
18579
18580 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18581 continue;
18582 }
18583
18584 struct partial_die_info *part_die
18585 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18586
18587 /* We'll save this DIE so link it in. */
18588 part_die->die_parent = parent_die;
18589 part_die->die_sibling = NULL;
18590 part_die->die_child = NULL;
18591
18592 if (last_die && last_die == parent_die)
18593 last_die->die_child = part_die;
18594 else if (last_die)
18595 last_die->die_sibling = part_die;
18596
18597 last_die = part_die;
18598
18599 if (first_die == NULL)
18600 first_die = part_die;
18601
18602 /* Maybe add the DIE to the hash table. Not all DIEs that we
18603 find interesting need to be in the hash table, because we
18604 also have the parent/sibling/child chains; only those that we
18605 might refer to by offset later during partial symbol reading.
18606
18607 For now this means things that might have be the target of a
18608 DW_AT_specification, DW_AT_abstract_origin, or
18609 DW_AT_extension. DW_AT_extension will refer only to
18610 namespaces; DW_AT_abstract_origin refers to functions (and
18611 many things under the function DIE, but we do not recurse
18612 into function DIEs during partial symbol reading) and
18613 possibly variables as well; DW_AT_specification refers to
18614 declarations. Declarations ought to have the DW_AT_declaration
18615 flag. It happens that GCC forgets to put it in sometimes, but
18616 only for functions, not for types.
18617
18618 Adding more things than necessary to the hash table is harmless
18619 except for the performance cost. Adding too few will result in
18620 wasted time in find_partial_die, when we reread the compilation
18621 unit with load_all_dies set. */
18622
18623 if (load_all
18624 || abbrev->tag == DW_TAG_constant
18625 || abbrev->tag == DW_TAG_subprogram
18626 || abbrev->tag == DW_TAG_variable
18627 || abbrev->tag == DW_TAG_namespace
18628 || part_die->is_declaration)
18629 {
18630 void **slot;
18631
18632 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18633 to_underlying (part_die->sect_off),
18634 INSERT);
18635 *slot = part_die;
18636 }
18637
18638 /* For some DIEs we want to follow their children (if any). For C
18639 we have no reason to follow the children of structures; for other
18640 languages we have to, so that we can get at method physnames
18641 to infer fully qualified class names, for DW_AT_specification,
18642 and for C++ template arguments. For C++, we also look one level
18643 inside functions to find template arguments (if the name of the
18644 function does not already contain the template arguments).
18645
18646 For Ada and Fortran, we need to scan the children of subprograms
18647 and lexical blocks as well because these languages allow the
18648 definition of nested entities that could be interesting for the
18649 debugger, such as nested subprograms for instance. */
18650 if (last_die->has_children
18651 && (load_all
18652 || last_die->tag == DW_TAG_namespace
18653 || last_die->tag == DW_TAG_module
18654 || last_die->tag == DW_TAG_enumeration_type
18655 || (cu->language == language_cplus
18656 && last_die->tag == DW_TAG_subprogram
18657 && (last_die->name == NULL
18658 || strchr (last_die->name, '<') == NULL))
18659 || (cu->language != language_c
18660 && (last_die->tag == DW_TAG_class_type
18661 || last_die->tag == DW_TAG_interface_type
18662 || last_die->tag == DW_TAG_structure_type
18663 || last_die->tag == DW_TAG_union_type))
18664 || ((cu->language == language_ada
18665 || cu->language == language_fortran)
18666 && (last_die->tag == DW_TAG_subprogram
18667 || last_die->tag == DW_TAG_lexical_block))))
18668 {
18669 nesting_level++;
18670 parent_die = last_die;
18671 continue;
18672 }
18673
18674 /* Otherwise we skip to the next sibling, if any. */
18675 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18676
18677 /* Back to the top, do it again. */
18678 }
18679 }
18680
18681 partial_die_info::partial_die_info (sect_offset sect_off_,
18682 struct abbrev_info *abbrev)
18683 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18684 {
18685 }
18686
18687 /* Read a minimal amount of information into the minimal die structure.
18688 INFO_PTR should point just after the initial uleb128 of a DIE. */
18689
18690 const gdb_byte *
18691 partial_die_info::read (const struct die_reader_specs *reader,
18692 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18693 {
18694 struct dwarf2_cu *cu = reader->cu;
18695 struct dwarf2_per_objfile *dwarf2_per_objfile
18696 = cu->per_cu->dwarf2_per_objfile;
18697 unsigned int i;
18698 int has_low_pc_attr = 0;
18699 int has_high_pc_attr = 0;
18700 int high_pc_relative = 0;
18701
18702 for (i = 0; i < abbrev.num_attrs; ++i)
18703 {
18704 struct attribute attr;
18705
18706 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18707
18708 /* Store the data if it is of an attribute we want to keep in a
18709 partial symbol table. */
18710 switch (attr.name)
18711 {
18712 case DW_AT_name:
18713 switch (tag)
18714 {
18715 case DW_TAG_compile_unit:
18716 case DW_TAG_partial_unit:
18717 case DW_TAG_type_unit:
18718 /* Compilation units have a DW_AT_name that is a filename, not
18719 a source language identifier. */
18720 case DW_TAG_enumeration_type:
18721 case DW_TAG_enumerator:
18722 /* These tags always have simple identifiers already; no need
18723 to canonicalize them. */
18724 name = DW_STRING (&attr);
18725 break;
18726 default:
18727 {
18728 struct objfile *objfile = dwarf2_per_objfile->objfile;
18729
18730 name
18731 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18732 &objfile->per_bfd->storage_obstack);
18733 }
18734 break;
18735 }
18736 break;
18737 case DW_AT_linkage_name:
18738 case DW_AT_MIPS_linkage_name:
18739 /* Note that both forms of linkage name might appear. We
18740 assume they will be the same, and we only store the last
18741 one we see. */
18742 linkage_name = DW_STRING (&attr);
18743 break;
18744 case DW_AT_low_pc:
18745 has_low_pc_attr = 1;
18746 lowpc = attr_value_as_address (&attr);
18747 break;
18748 case DW_AT_high_pc:
18749 has_high_pc_attr = 1;
18750 highpc = attr_value_as_address (&attr);
18751 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18752 high_pc_relative = 1;
18753 break;
18754 case DW_AT_location:
18755 /* Support the .debug_loc offsets. */
18756 if (attr_form_is_block (&attr))
18757 {
18758 d.locdesc = DW_BLOCK (&attr);
18759 }
18760 else if (attr_form_is_section_offset (&attr))
18761 {
18762 dwarf2_complex_location_expr_complaint ();
18763 }
18764 else
18765 {
18766 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18767 "partial symbol information");
18768 }
18769 break;
18770 case DW_AT_external:
18771 is_external = DW_UNSND (&attr);
18772 break;
18773 case DW_AT_declaration:
18774 is_declaration = DW_UNSND (&attr);
18775 break;
18776 case DW_AT_type:
18777 has_type = 1;
18778 break;
18779 case DW_AT_abstract_origin:
18780 case DW_AT_specification:
18781 case DW_AT_extension:
18782 has_specification = 1;
18783 spec_offset = dwarf2_get_ref_die_offset (&attr);
18784 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18785 || cu->per_cu->is_dwz);
18786 break;
18787 case DW_AT_sibling:
18788 /* Ignore absolute siblings, they might point outside of
18789 the current compile unit. */
18790 if (attr.form == DW_FORM_ref_addr)
18791 complaint (_("ignoring absolute DW_AT_sibling"));
18792 else
18793 {
18794 const gdb_byte *buffer = reader->buffer;
18795 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18796 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18797
18798 if (sibling_ptr < info_ptr)
18799 complaint (_("DW_AT_sibling points backwards"));
18800 else if (sibling_ptr > reader->buffer_end)
18801 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18802 else
18803 sibling = sibling_ptr;
18804 }
18805 break;
18806 case DW_AT_byte_size:
18807 has_byte_size = 1;
18808 break;
18809 case DW_AT_const_value:
18810 has_const_value = 1;
18811 break;
18812 case DW_AT_calling_convention:
18813 /* DWARF doesn't provide a way to identify a program's source-level
18814 entry point. DW_AT_calling_convention attributes are only meant
18815 to describe functions' calling conventions.
18816
18817 However, because it's a necessary piece of information in
18818 Fortran, and before DWARF 4 DW_CC_program was the only
18819 piece of debugging information whose definition refers to
18820 a 'main program' at all, several compilers marked Fortran
18821 main programs with DW_CC_program --- even when those
18822 functions use the standard calling conventions.
18823
18824 Although DWARF now specifies a way to provide this
18825 information, we support this practice for backward
18826 compatibility. */
18827 if (DW_UNSND (&attr) == DW_CC_program
18828 && cu->language == language_fortran)
18829 main_subprogram = 1;
18830 break;
18831 case DW_AT_inline:
18832 if (DW_UNSND (&attr) == DW_INL_inlined
18833 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18834 may_be_inlined = 1;
18835 break;
18836
18837 case DW_AT_import:
18838 if (tag == DW_TAG_imported_unit)
18839 {
18840 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18841 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18842 || cu->per_cu->is_dwz);
18843 }
18844 break;
18845
18846 case DW_AT_main_subprogram:
18847 main_subprogram = DW_UNSND (&attr);
18848 break;
18849
18850 case DW_AT_ranges:
18851 {
18852 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18853 but that requires a full DIE, so instead we just
18854 reimplement it. */
18855 int need_ranges_base = tag != DW_TAG_compile_unit;
18856 unsigned int ranges_offset = (DW_UNSND (&attr)
18857 + (need_ranges_base
18858 ? cu->ranges_base
18859 : 0));
18860
18861 /* Value of the DW_AT_ranges attribute is the offset in the
18862 .debug_ranges section. */
18863 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18864 nullptr))
18865 has_pc_info = 1;
18866 }
18867 break;
18868
18869 default:
18870 break;
18871 }
18872 }
18873
18874 /* For Ada, if both the name and the linkage name appear, we prefer
18875 the latter. This lets "catch exception" work better, regardless
18876 of the order in which the name and linkage name were emitted.
18877 Really, though, this is just a workaround for the fact that gdb
18878 doesn't store both the name and the linkage name. */
18879 if (cu->language == language_ada && linkage_name != nullptr)
18880 name = linkage_name;
18881
18882 if (high_pc_relative)
18883 highpc += lowpc;
18884
18885 if (has_low_pc_attr && has_high_pc_attr)
18886 {
18887 /* When using the GNU linker, .gnu.linkonce. sections are used to
18888 eliminate duplicate copies of functions and vtables and such.
18889 The linker will arbitrarily choose one and discard the others.
18890 The AT_*_pc values for such functions refer to local labels in
18891 these sections. If the section from that file was discarded, the
18892 labels are not in the output, so the relocs get a value of 0.
18893 If this is a discarded function, mark the pc bounds as invalid,
18894 so that GDB will ignore it. */
18895 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18896 {
18897 struct objfile *objfile = dwarf2_per_objfile->objfile;
18898 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18899
18900 complaint (_("DW_AT_low_pc %s is zero "
18901 "for DIE at %s [in module %s]"),
18902 paddress (gdbarch, lowpc),
18903 sect_offset_str (sect_off),
18904 objfile_name (objfile));
18905 }
18906 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18907 else if (lowpc >= highpc)
18908 {
18909 struct objfile *objfile = dwarf2_per_objfile->objfile;
18910 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18911
18912 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18913 "for DIE at %s [in module %s]"),
18914 paddress (gdbarch, lowpc),
18915 paddress (gdbarch, highpc),
18916 sect_offset_str (sect_off),
18917 objfile_name (objfile));
18918 }
18919 else
18920 has_pc_info = 1;
18921 }
18922
18923 return info_ptr;
18924 }
18925
18926 /* Find a cached partial DIE at OFFSET in CU. */
18927
18928 struct partial_die_info *
18929 dwarf2_cu::find_partial_die (sect_offset sect_off)
18930 {
18931 struct partial_die_info *lookup_die = NULL;
18932 struct partial_die_info part_die (sect_off);
18933
18934 lookup_die = ((struct partial_die_info *)
18935 htab_find_with_hash (partial_dies, &part_die,
18936 to_underlying (sect_off)));
18937
18938 return lookup_die;
18939 }
18940
18941 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18942 except in the case of .debug_types DIEs which do not reference
18943 outside their CU (they do however referencing other types via
18944 DW_FORM_ref_sig8). */
18945
18946 static const struct cu_partial_die_info
18947 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18948 {
18949 struct dwarf2_per_objfile *dwarf2_per_objfile
18950 = cu->per_cu->dwarf2_per_objfile;
18951 struct objfile *objfile = dwarf2_per_objfile->objfile;
18952 struct dwarf2_per_cu_data *per_cu = NULL;
18953 struct partial_die_info *pd = NULL;
18954
18955 if (offset_in_dwz == cu->per_cu->is_dwz
18956 && offset_in_cu_p (&cu->header, sect_off))
18957 {
18958 pd = cu->find_partial_die (sect_off);
18959 if (pd != NULL)
18960 return { cu, pd };
18961 /* We missed recording what we needed.
18962 Load all dies and try again. */
18963 per_cu = cu->per_cu;
18964 }
18965 else
18966 {
18967 /* TUs don't reference other CUs/TUs (except via type signatures). */
18968 if (cu->per_cu->is_debug_types)
18969 {
18970 error (_("Dwarf Error: Type Unit at offset %s contains"
18971 " external reference to offset %s [in module %s].\n"),
18972 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18973 bfd_get_filename (objfile->obfd));
18974 }
18975 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18976 dwarf2_per_objfile);
18977
18978 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18979 load_partial_comp_unit (per_cu);
18980
18981 per_cu->cu->last_used = 0;
18982 pd = per_cu->cu->find_partial_die (sect_off);
18983 }
18984
18985 /* If we didn't find it, and not all dies have been loaded,
18986 load them all and try again. */
18987
18988 if (pd == NULL && per_cu->load_all_dies == 0)
18989 {
18990 per_cu->load_all_dies = 1;
18991
18992 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18993 THIS_CU->cu may already be in use. So we can't just free it and
18994 replace its DIEs with the ones we read in. Instead, we leave those
18995 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18996 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18997 set. */
18998 load_partial_comp_unit (per_cu);
18999
19000 pd = per_cu->cu->find_partial_die (sect_off);
19001 }
19002
19003 if (pd == NULL)
19004 internal_error (__FILE__, __LINE__,
19005 _("could not find partial DIE %s "
19006 "in cache [from module %s]\n"),
19007 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19008 return { per_cu->cu, pd };
19009 }
19010
19011 /* See if we can figure out if the class lives in a namespace. We do
19012 this by looking for a member function; its demangled name will
19013 contain namespace info, if there is any. */
19014
19015 static void
19016 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19017 struct dwarf2_cu *cu)
19018 {
19019 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19020 what template types look like, because the demangler
19021 frequently doesn't give the same name as the debug info. We
19022 could fix this by only using the demangled name to get the
19023 prefix (but see comment in read_structure_type). */
19024
19025 struct partial_die_info *real_pdi;
19026 struct partial_die_info *child_pdi;
19027
19028 /* If this DIE (this DIE's specification, if any) has a parent, then
19029 we should not do this. We'll prepend the parent's fully qualified
19030 name when we create the partial symbol. */
19031
19032 real_pdi = struct_pdi;
19033 while (real_pdi->has_specification)
19034 {
19035 auto res = find_partial_die (real_pdi->spec_offset,
19036 real_pdi->spec_is_dwz, cu);
19037 real_pdi = res.pdi;
19038 cu = res.cu;
19039 }
19040
19041 if (real_pdi->die_parent != NULL)
19042 return;
19043
19044 for (child_pdi = struct_pdi->die_child;
19045 child_pdi != NULL;
19046 child_pdi = child_pdi->die_sibling)
19047 {
19048 if (child_pdi->tag == DW_TAG_subprogram
19049 && child_pdi->linkage_name != NULL)
19050 {
19051 char *actual_class_name
19052 = language_class_name_from_physname (cu->language_defn,
19053 child_pdi->linkage_name);
19054 if (actual_class_name != NULL)
19055 {
19056 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19057 struct_pdi->name
19058 = obstack_strdup (&objfile->per_bfd->storage_obstack,
19059 actual_class_name);
19060 xfree (actual_class_name);
19061 }
19062 break;
19063 }
19064 }
19065 }
19066
19067 void
19068 partial_die_info::fixup (struct dwarf2_cu *cu)
19069 {
19070 /* Once we've fixed up a die, there's no point in doing so again.
19071 This also avoids a memory leak if we were to call
19072 guess_partial_die_structure_name multiple times. */
19073 if (fixup_called)
19074 return;
19075
19076 /* If we found a reference attribute and the DIE has no name, try
19077 to find a name in the referred to DIE. */
19078
19079 if (name == NULL && has_specification)
19080 {
19081 struct partial_die_info *spec_die;
19082
19083 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19084 spec_die = res.pdi;
19085 cu = res.cu;
19086
19087 spec_die->fixup (cu);
19088
19089 if (spec_die->name)
19090 {
19091 name = spec_die->name;
19092
19093 /* Copy DW_AT_external attribute if it is set. */
19094 if (spec_die->is_external)
19095 is_external = spec_die->is_external;
19096 }
19097 }
19098
19099 /* Set default names for some unnamed DIEs. */
19100
19101 if (name == NULL && tag == DW_TAG_namespace)
19102 name = CP_ANONYMOUS_NAMESPACE_STR;
19103
19104 /* If there is no parent die to provide a namespace, and there are
19105 children, see if we can determine the namespace from their linkage
19106 name. */
19107 if (cu->language == language_cplus
19108 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
19109 && die_parent == NULL
19110 && has_children
19111 && (tag == DW_TAG_class_type
19112 || tag == DW_TAG_structure_type
19113 || tag == DW_TAG_union_type))
19114 guess_partial_die_structure_name (this, cu);
19115
19116 /* GCC might emit a nameless struct or union that has a linkage
19117 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19118 if (name == NULL
19119 && (tag == DW_TAG_class_type
19120 || tag == DW_TAG_interface_type
19121 || tag == DW_TAG_structure_type
19122 || tag == DW_TAG_union_type)
19123 && linkage_name != NULL)
19124 {
19125 char *demangled;
19126
19127 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
19128 if (demangled)
19129 {
19130 const char *base;
19131
19132 /* Strip any leading namespaces/classes, keep only the base name.
19133 DW_AT_name for named DIEs does not contain the prefixes. */
19134 base = strrchr (demangled, ':');
19135 if (base && base > demangled && base[-1] == ':')
19136 base++;
19137 else
19138 base = demangled;
19139
19140 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19141 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
19142 xfree (demangled);
19143 }
19144 }
19145
19146 fixup_called = 1;
19147 }
19148
19149 /* Read an attribute value described by an attribute form. */
19150
19151 static const gdb_byte *
19152 read_attribute_value (const struct die_reader_specs *reader,
19153 struct attribute *attr, unsigned form,
19154 LONGEST implicit_const, const gdb_byte *info_ptr)
19155 {
19156 struct dwarf2_cu *cu = reader->cu;
19157 struct dwarf2_per_objfile *dwarf2_per_objfile
19158 = cu->per_cu->dwarf2_per_objfile;
19159 struct objfile *objfile = dwarf2_per_objfile->objfile;
19160 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19161 bfd *abfd = reader->abfd;
19162 struct comp_unit_head *cu_header = &cu->header;
19163 unsigned int bytes_read;
19164 struct dwarf_block *blk;
19165
19166 attr->form = (enum dwarf_form) form;
19167 switch (form)
19168 {
19169 case DW_FORM_ref_addr:
19170 if (cu->header.version == 2)
19171 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19172 else
19173 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19174 &cu->header, &bytes_read);
19175 info_ptr += bytes_read;
19176 break;
19177 case DW_FORM_GNU_ref_alt:
19178 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19179 info_ptr += bytes_read;
19180 break;
19181 case DW_FORM_addr:
19182 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19183 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19184 info_ptr += bytes_read;
19185 break;
19186 case DW_FORM_block2:
19187 blk = dwarf_alloc_block (cu);
19188 blk->size = read_2_bytes (abfd, info_ptr);
19189 info_ptr += 2;
19190 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19191 info_ptr += blk->size;
19192 DW_BLOCK (attr) = blk;
19193 break;
19194 case DW_FORM_block4:
19195 blk = dwarf_alloc_block (cu);
19196 blk->size = read_4_bytes (abfd, info_ptr);
19197 info_ptr += 4;
19198 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19199 info_ptr += blk->size;
19200 DW_BLOCK (attr) = blk;
19201 break;
19202 case DW_FORM_data2:
19203 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19204 info_ptr += 2;
19205 break;
19206 case DW_FORM_data4:
19207 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19208 info_ptr += 4;
19209 break;
19210 case DW_FORM_data8:
19211 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19212 info_ptr += 8;
19213 break;
19214 case DW_FORM_data16:
19215 blk = dwarf_alloc_block (cu);
19216 blk->size = 16;
19217 blk->data = read_n_bytes (abfd, info_ptr, 16);
19218 info_ptr += 16;
19219 DW_BLOCK (attr) = blk;
19220 break;
19221 case DW_FORM_sec_offset:
19222 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19223 info_ptr += bytes_read;
19224 break;
19225 case DW_FORM_string:
19226 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19227 DW_STRING_IS_CANONICAL (attr) = 0;
19228 info_ptr += bytes_read;
19229 break;
19230 case DW_FORM_strp:
19231 if (!cu->per_cu->is_dwz)
19232 {
19233 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19234 abfd, info_ptr, cu_header,
19235 &bytes_read);
19236 DW_STRING_IS_CANONICAL (attr) = 0;
19237 info_ptr += bytes_read;
19238 break;
19239 }
19240 /* FALLTHROUGH */
19241 case DW_FORM_line_strp:
19242 if (!cu->per_cu->is_dwz)
19243 {
19244 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19245 abfd, info_ptr,
19246 cu_header, &bytes_read);
19247 DW_STRING_IS_CANONICAL (attr) = 0;
19248 info_ptr += bytes_read;
19249 break;
19250 }
19251 /* FALLTHROUGH */
19252 case DW_FORM_GNU_strp_alt:
19253 {
19254 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19255 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19256 &bytes_read);
19257
19258 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19259 dwz, str_offset);
19260 DW_STRING_IS_CANONICAL (attr) = 0;
19261 info_ptr += bytes_read;
19262 }
19263 break;
19264 case DW_FORM_exprloc:
19265 case DW_FORM_block:
19266 blk = dwarf_alloc_block (cu);
19267 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19268 info_ptr += bytes_read;
19269 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19270 info_ptr += blk->size;
19271 DW_BLOCK (attr) = blk;
19272 break;
19273 case DW_FORM_block1:
19274 blk = dwarf_alloc_block (cu);
19275 blk->size = read_1_byte (abfd, info_ptr);
19276 info_ptr += 1;
19277 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19278 info_ptr += blk->size;
19279 DW_BLOCK (attr) = blk;
19280 break;
19281 case DW_FORM_data1:
19282 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19283 info_ptr += 1;
19284 break;
19285 case DW_FORM_flag:
19286 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19287 info_ptr += 1;
19288 break;
19289 case DW_FORM_flag_present:
19290 DW_UNSND (attr) = 1;
19291 break;
19292 case DW_FORM_sdata:
19293 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19294 info_ptr += bytes_read;
19295 break;
19296 case DW_FORM_udata:
19297 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19298 info_ptr += bytes_read;
19299 break;
19300 case DW_FORM_ref1:
19301 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19302 + read_1_byte (abfd, info_ptr));
19303 info_ptr += 1;
19304 break;
19305 case DW_FORM_ref2:
19306 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19307 + read_2_bytes (abfd, info_ptr));
19308 info_ptr += 2;
19309 break;
19310 case DW_FORM_ref4:
19311 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19312 + read_4_bytes (abfd, info_ptr));
19313 info_ptr += 4;
19314 break;
19315 case DW_FORM_ref8:
19316 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19317 + read_8_bytes (abfd, info_ptr));
19318 info_ptr += 8;
19319 break;
19320 case DW_FORM_ref_sig8:
19321 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19322 info_ptr += 8;
19323 break;
19324 case DW_FORM_ref_udata:
19325 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19326 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19327 info_ptr += bytes_read;
19328 break;
19329 case DW_FORM_indirect:
19330 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19331 info_ptr += bytes_read;
19332 if (form == DW_FORM_implicit_const)
19333 {
19334 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19335 info_ptr += bytes_read;
19336 }
19337 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19338 info_ptr);
19339 break;
19340 case DW_FORM_implicit_const:
19341 DW_SND (attr) = implicit_const;
19342 break;
19343 case DW_FORM_addrx:
19344 case DW_FORM_GNU_addr_index:
19345 if (reader->dwo_file == NULL)
19346 {
19347 /* For now flag a hard error.
19348 Later we can turn this into a complaint. */
19349 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19350 dwarf_form_name (form),
19351 bfd_get_filename (abfd));
19352 }
19353 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19354 info_ptr += bytes_read;
19355 break;
19356 case DW_FORM_strx:
19357 case DW_FORM_strx1:
19358 case DW_FORM_strx2:
19359 case DW_FORM_strx3:
19360 case DW_FORM_strx4:
19361 case DW_FORM_GNU_str_index:
19362 if (reader->dwo_file == NULL)
19363 {
19364 /* For now flag a hard error.
19365 Later we can turn this into a complaint if warranted. */
19366 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19367 dwarf_form_name (form),
19368 bfd_get_filename (abfd));
19369 }
19370 {
19371 ULONGEST str_index;
19372 if (form == DW_FORM_strx1)
19373 {
19374 str_index = read_1_byte (abfd, info_ptr);
19375 info_ptr += 1;
19376 }
19377 else if (form == DW_FORM_strx2)
19378 {
19379 str_index = read_2_bytes (abfd, info_ptr);
19380 info_ptr += 2;
19381 }
19382 else if (form == DW_FORM_strx3)
19383 {
19384 str_index = read_3_bytes (abfd, info_ptr);
19385 info_ptr += 3;
19386 }
19387 else if (form == DW_FORM_strx4)
19388 {
19389 str_index = read_4_bytes (abfd, info_ptr);
19390 info_ptr += 4;
19391 }
19392 else
19393 {
19394 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19395 info_ptr += bytes_read;
19396 }
19397 DW_STRING (attr) = read_str_index (reader, str_index);
19398 DW_STRING_IS_CANONICAL (attr) = 0;
19399 }
19400 break;
19401 default:
19402 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19403 dwarf_form_name (form),
19404 bfd_get_filename (abfd));
19405 }
19406
19407 /* Super hack. */
19408 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19409 attr->form = DW_FORM_GNU_ref_alt;
19410
19411 /* We have seen instances where the compiler tried to emit a byte
19412 size attribute of -1 which ended up being encoded as an unsigned
19413 0xffffffff. Although 0xffffffff is technically a valid size value,
19414 an object of this size seems pretty unlikely so we can relatively
19415 safely treat these cases as if the size attribute was invalid and
19416 treat them as zero by default. */
19417 if (attr->name == DW_AT_byte_size
19418 && form == DW_FORM_data4
19419 && DW_UNSND (attr) >= 0xffffffff)
19420 {
19421 complaint
19422 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19423 hex_string (DW_UNSND (attr)));
19424 DW_UNSND (attr) = 0;
19425 }
19426
19427 return info_ptr;
19428 }
19429
19430 /* Read an attribute described by an abbreviated attribute. */
19431
19432 static const gdb_byte *
19433 read_attribute (const struct die_reader_specs *reader,
19434 struct attribute *attr, struct attr_abbrev *abbrev,
19435 const gdb_byte *info_ptr)
19436 {
19437 attr->name = abbrev->name;
19438 return read_attribute_value (reader, attr, abbrev->form,
19439 abbrev->implicit_const, info_ptr);
19440 }
19441
19442 /* Read dwarf information from a buffer. */
19443
19444 static unsigned int
19445 read_1_byte (bfd *abfd, const gdb_byte *buf)
19446 {
19447 return bfd_get_8 (abfd, buf);
19448 }
19449
19450 static int
19451 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19452 {
19453 return bfd_get_signed_8 (abfd, buf);
19454 }
19455
19456 static unsigned int
19457 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19458 {
19459 return bfd_get_16 (abfd, buf);
19460 }
19461
19462 static int
19463 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19464 {
19465 return bfd_get_signed_16 (abfd, buf);
19466 }
19467
19468 static unsigned int
19469 read_3_bytes (bfd *abfd, const gdb_byte *buf)
19470 {
19471 unsigned int result = 0;
19472 for (int i = 0; i < 3; ++i)
19473 {
19474 unsigned char byte = bfd_get_8 (abfd, buf);
19475 buf++;
19476 result |= ((unsigned int) byte << (i * 8));
19477 }
19478 return result;
19479 }
19480
19481 static unsigned int
19482 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19483 {
19484 return bfd_get_32 (abfd, buf);
19485 }
19486
19487 static int
19488 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19489 {
19490 return bfd_get_signed_32 (abfd, buf);
19491 }
19492
19493 static ULONGEST
19494 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19495 {
19496 return bfd_get_64 (abfd, buf);
19497 }
19498
19499 static CORE_ADDR
19500 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19501 unsigned int *bytes_read)
19502 {
19503 struct comp_unit_head *cu_header = &cu->header;
19504 CORE_ADDR retval = 0;
19505
19506 if (cu_header->signed_addr_p)
19507 {
19508 switch (cu_header->addr_size)
19509 {
19510 case 2:
19511 retval = bfd_get_signed_16 (abfd, buf);
19512 break;
19513 case 4:
19514 retval = bfd_get_signed_32 (abfd, buf);
19515 break;
19516 case 8:
19517 retval = bfd_get_signed_64 (abfd, buf);
19518 break;
19519 default:
19520 internal_error (__FILE__, __LINE__,
19521 _("read_address: bad switch, signed [in module %s]"),
19522 bfd_get_filename (abfd));
19523 }
19524 }
19525 else
19526 {
19527 switch (cu_header->addr_size)
19528 {
19529 case 2:
19530 retval = bfd_get_16 (abfd, buf);
19531 break;
19532 case 4:
19533 retval = bfd_get_32 (abfd, buf);
19534 break;
19535 case 8:
19536 retval = bfd_get_64 (abfd, buf);
19537 break;
19538 default:
19539 internal_error (__FILE__, __LINE__,
19540 _("read_address: bad switch, "
19541 "unsigned [in module %s]"),
19542 bfd_get_filename (abfd));
19543 }
19544 }
19545
19546 *bytes_read = cu_header->addr_size;
19547 return retval;
19548 }
19549
19550 /* Read the initial length from a section. The (draft) DWARF 3
19551 specification allows the initial length to take up either 4 bytes
19552 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19553 bytes describe the length and all offsets will be 8 bytes in length
19554 instead of 4.
19555
19556 An older, non-standard 64-bit format is also handled by this
19557 function. The older format in question stores the initial length
19558 as an 8-byte quantity without an escape value. Lengths greater
19559 than 2^32 aren't very common which means that the initial 4 bytes
19560 is almost always zero. Since a length value of zero doesn't make
19561 sense for the 32-bit format, this initial zero can be considered to
19562 be an escape value which indicates the presence of the older 64-bit
19563 format. As written, the code can't detect (old format) lengths
19564 greater than 4GB. If it becomes necessary to handle lengths
19565 somewhat larger than 4GB, we could allow other small values (such
19566 as the non-sensical values of 1, 2, and 3) to also be used as
19567 escape values indicating the presence of the old format.
19568
19569 The value returned via bytes_read should be used to increment the
19570 relevant pointer after calling read_initial_length().
19571
19572 [ Note: read_initial_length() and read_offset() are based on the
19573 document entitled "DWARF Debugging Information Format", revision
19574 3, draft 8, dated November 19, 2001. This document was obtained
19575 from:
19576
19577 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19578
19579 This document is only a draft and is subject to change. (So beware.)
19580
19581 Details regarding the older, non-standard 64-bit format were
19582 determined empirically by examining 64-bit ELF files produced by
19583 the SGI toolchain on an IRIX 6.5 machine.
19584
19585 - Kevin, July 16, 2002
19586 ] */
19587
19588 static LONGEST
19589 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19590 {
19591 LONGEST length = bfd_get_32 (abfd, buf);
19592
19593 if (length == 0xffffffff)
19594 {
19595 length = bfd_get_64 (abfd, buf + 4);
19596 *bytes_read = 12;
19597 }
19598 else if (length == 0)
19599 {
19600 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19601 length = bfd_get_64 (abfd, buf);
19602 *bytes_read = 8;
19603 }
19604 else
19605 {
19606 *bytes_read = 4;
19607 }
19608
19609 return length;
19610 }
19611
19612 /* Cover function for read_initial_length.
19613 Returns the length of the object at BUF, and stores the size of the
19614 initial length in *BYTES_READ and stores the size that offsets will be in
19615 *OFFSET_SIZE.
19616 If the initial length size is not equivalent to that specified in
19617 CU_HEADER then issue a complaint.
19618 This is useful when reading non-comp-unit headers. */
19619
19620 static LONGEST
19621 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19622 const struct comp_unit_head *cu_header,
19623 unsigned int *bytes_read,
19624 unsigned int *offset_size)
19625 {
19626 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19627
19628 gdb_assert (cu_header->initial_length_size == 4
19629 || cu_header->initial_length_size == 8
19630 || cu_header->initial_length_size == 12);
19631
19632 if (cu_header->initial_length_size != *bytes_read)
19633 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
19634
19635 *offset_size = (*bytes_read == 4) ? 4 : 8;
19636 return length;
19637 }
19638
19639 /* Read an offset from the data stream. The size of the offset is
19640 given by cu_header->offset_size. */
19641
19642 static LONGEST
19643 read_offset (bfd *abfd, const gdb_byte *buf,
19644 const struct comp_unit_head *cu_header,
19645 unsigned int *bytes_read)
19646 {
19647 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19648
19649 *bytes_read = cu_header->offset_size;
19650 return offset;
19651 }
19652
19653 /* Read an offset from the data stream. */
19654
19655 static LONGEST
19656 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19657 {
19658 LONGEST retval = 0;
19659
19660 switch (offset_size)
19661 {
19662 case 4:
19663 retval = bfd_get_32 (abfd, buf);
19664 break;
19665 case 8:
19666 retval = bfd_get_64 (abfd, buf);
19667 break;
19668 default:
19669 internal_error (__FILE__, __LINE__,
19670 _("read_offset_1: bad switch [in module %s]"),
19671 bfd_get_filename (abfd));
19672 }
19673
19674 return retval;
19675 }
19676
19677 static const gdb_byte *
19678 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19679 {
19680 /* If the size of a host char is 8 bits, we can return a pointer
19681 to the buffer, otherwise we have to copy the data to a buffer
19682 allocated on the temporary obstack. */
19683 gdb_assert (HOST_CHAR_BIT == 8);
19684 return buf;
19685 }
19686
19687 static const char *
19688 read_direct_string (bfd *abfd, const gdb_byte *buf,
19689 unsigned int *bytes_read_ptr)
19690 {
19691 /* If the size of a host char is 8 bits, we can return a pointer
19692 to the string, otherwise we have to copy the string to a buffer
19693 allocated on the temporary obstack. */
19694 gdb_assert (HOST_CHAR_BIT == 8);
19695 if (*buf == '\0')
19696 {
19697 *bytes_read_ptr = 1;
19698 return NULL;
19699 }
19700 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19701 return (const char *) buf;
19702 }
19703
19704 /* Return pointer to string at section SECT offset STR_OFFSET with error
19705 reporting strings FORM_NAME and SECT_NAME. */
19706
19707 static const char *
19708 read_indirect_string_at_offset_from (struct objfile *objfile,
19709 bfd *abfd, LONGEST str_offset,
19710 struct dwarf2_section_info *sect,
19711 const char *form_name,
19712 const char *sect_name)
19713 {
19714 dwarf2_read_section (objfile, sect);
19715 if (sect->buffer == NULL)
19716 error (_("%s used without %s section [in module %s]"),
19717 form_name, sect_name, bfd_get_filename (abfd));
19718 if (str_offset >= sect->size)
19719 error (_("%s pointing outside of %s section [in module %s]"),
19720 form_name, sect_name, bfd_get_filename (abfd));
19721 gdb_assert (HOST_CHAR_BIT == 8);
19722 if (sect->buffer[str_offset] == '\0')
19723 return NULL;
19724 return (const char *) (sect->buffer + str_offset);
19725 }
19726
19727 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19728
19729 static const char *
19730 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19731 bfd *abfd, LONGEST str_offset)
19732 {
19733 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19734 abfd, str_offset,
19735 &dwarf2_per_objfile->str,
19736 "DW_FORM_strp", ".debug_str");
19737 }
19738
19739 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19740
19741 static const char *
19742 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19743 bfd *abfd, LONGEST str_offset)
19744 {
19745 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19746 abfd, str_offset,
19747 &dwarf2_per_objfile->line_str,
19748 "DW_FORM_line_strp",
19749 ".debug_line_str");
19750 }
19751
19752 /* Read a string at offset STR_OFFSET in the .debug_str section from
19753 the .dwz file DWZ. Throw an error if the offset is too large. If
19754 the string consists of a single NUL byte, return NULL; otherwise
19755 return a pointer to the string. */
19756
19757 static const char *
19758 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19759 LONGEST str_offset)
19760 {
19761 dwarf2_read_section (objfile, &dwz->str);
19762
19763 if (dwz->str.buffer == NULL)
19764 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19765 "section [in module %s]"),
19766 bfd_get_filename (dwz->dwz_bfd.get ()));
19767 if (str_offset >= dwz->str.size)
19768 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19769 ".debug_str section [in module %s]"),
19770 bfd_get_filename (dwz->dwz_bfd.get ()));
19771 gdb_assert (HOST_CHAR_BIT == 8);
19772 if (dwz->str.buffer[str_offset] == '\0')
19773 return NULL;
19774 return (const char *) (dwz->str.buffer + str_offset);
19775 }
19776
19777 /* Return pointer to string at .debug_str offset as read from BUF.
19778 BUF is assumed to be in a compilation unit described by CU_HEADER.
19779 Return *BYTES_READ_PTR count of bytes read from BUF. */
19780
19781 static const char *
19782 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19783 const gdb_byte *buf,
19784 const struct comp_unit_head *cu_header,
19785 unsigned int *bytes_read_ptr)
19786 {
19787 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19788
19789 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19790 }
19791
19792 /* Return pointer to string at .debug_line_str offset as read from BUF.
19793 BUF is assumed to be in a compilation unit described by CU_HEADER.
19794 Return *BYTES_READ_PTR count of bytes read from BUF. */
19795
19796 static const char *
19797 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19798 bfd *abfd, const gdb_byte *buf,
19799 const struct comp_unit_head *cu_header,
19800 unsigned int *bytes_read_ptr)
19801 {
19802 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19803
19804 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19805 str_offset);
19806 }
19807
19808 ULONGEST
19809 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19810 unsigned int *bytes_read_ptr)
19811 {
19812 ULONGEST result;
19813 unsigned int num_read;
19814 int shift;
19815 unsigned char byte;
19816
19817 result = 0;
19818 shift = 0;
19819 num_read = 0;
19820 while (1)
19821 {
19822 byte = bfd_get_8 (abfd, buf);
19823 buf++;
19824 num_read++;
19825 result |= ((ULONGEST) (byte & 127) << shift);
19826 if ((byte & 128) == 0)
19827 {
19828 break;
19829 }
19830 shift += 7;
19831 }
19832 *bytes_read_ptr = num_read;
19833 return result;
19834 }
19835
19836 static LONGEST
19837 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19838 unsigned int *bytes_read_ptr)
19839 {
19840 ULONGEST result;
19841 int shift, num_read;
19842 unsigned char byte;
19843
19844 result = 0;
19845 shift = 0;
19846 num_read = 0;
19847 while (1)
19848 {
19849 byte = bfd_get_8 (abfd, buf);
19850 buf++;
19851 num_read++;
19852 result |= ((ULONGEST) (byte & 127) << shift);
19853 shift += 7;
19854 if ((byte & 128) == 0)
19855 {
19856 break;
19857 }
19858 }
19859 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19860 result |= -(((ULONGEST) 1) << shift);
19861 *bytes_read_ptr = num_read;
19862 return result;
19863 }
19864
19865 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19866 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19867 ADDR_SIZE is the size of addresses from the CU header. */
19868
19869 static CORE_ADDR
19870 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19871 unsigned int addr_index, ULONGEST addr_base, int addr_size)
19872 {
19873 struct objfile *objfile = dwarf2_per_objfile->objfile;
19874 bfd *abfd = objfile->obfd;
19875 const gdb_byte *info_ptr;
19876
19877 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19878 if (dwarf2_per_objfile->addr.buffer == NULL)
19879 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19880 objfile_name (objfile));
19881 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19882 error (_("DW_FORM_addr_index pointing outside of "
19883 ".debug_addr section [in module %s]"),
19884 objfile_name (objfile));
19885 info_ptr = (dwarf2_per_objfile->addr.buffer
19886 + addr_base + addr_index * addr_size);
19887 if (addr_size == 4)
19888 return bfd_get_32 (abfd, info_ptr);
19889 else
19890 return bfd_get_64 (abfd, info_ptr);
19891 }
19892
19893 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19894
19895 static CORE_ADDR
19896 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19897 {
19898 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19899 cu->addr_base, cu->header.addr_size);
19900 }
19901
19902 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19903
19904 static CORE_ADDR
19905 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19906 unsigned int *bytes_read)
19907 {
19908 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19909 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19910
19911 return read_addr_index (cu, addr_index);
19912 }
19913
19914 /* Data structure to pass results from dwarf2_read_addr_index_reader
19915 back to dwarf2_read_addr_index. */
19916
19917 struct dwarf2_read_addr_index_data
19918 {
19919 ULONGEST addr_base;
19920 int addr_size;
19921 };
19922
19923 /* die_reader_func for dwarf2_read_addr_index. */
19924
19925 static void
19926 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
19927 const gdb_byte *info_ptr,
19928 struct die_info *comp_unit_die,
19929 int has_children,
19930 void *data)
19931 {
19932 struct dwarf2_cu *cu = reader->cu;
19933 struct dwarf2_read_addr_index_data *aidata =
19934 (struct dwarf2_read_addr_index_data *) data;
19935
19936 aidata->addr_base = cu->addr_base;
19937 aidata->addr_size = cu->header.addr_size;
19938 }
19939
19940 /* Given an index in .debug_addr, fetch the value.
19941 NOTE: This can be called during dwarf expression evaluation,
19942 long after the debug information has been read, and thus per_cu->cu
19943 may no longer exist. */
19944
19945 CORE_ADDR
19946 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19947 unsigned int addr_index)
19948 {
19949 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19950 struct dwarf2_cu *cu = per_cu->cu;
19951 ULONGEST addr_base;
19952 int addr_size;
19953
19954 /* We need addr_base and addr_size.
19955 If we don't have PER_CU->cu, we have to get it.
19956 Nasty, but the alternative is storing the needed info in PER_CU,
19957 which at this point doesn't seem justified: it's not clear how frequently
19958 it would get used and it would increase the size of every PER_CU.
19959 Entry points like dwarf2_per_cu_addr_size do a similar thing
19960 so we're not in uncharted territory here.
19961 Alas we need to be a bit more complicated as addr_base is contained
19962 in the DIE.
19963
19964 We don't need to read the entire CU(/TU).
19965 We just need the header and top level die.
19966
19967 IWBN to use the aging mechanism to let us lazily later discard the CU.
19968 For now we skip this optimization. */
19969
19970 if (cu != NULL)
19971 {
19972 addr_base = cu->addr_base;
19973 addr_size = cu->header.addr_size;
19974 }
19975 else
19976 {
19977 struct dwarf2_read_addr_index_data aidata;
19978
19979 /* Note: We can't use init_cutu_and_read_dies_simple here,
19980 we need addr_base. */
19981 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
19982 dwarf2_read_addr_index_reader, &aidata);
19983 addr_base = aidata.addr_base;
19984 addr_size = aidata.addr_size;
19985 }
19986
19987 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19988 addr_size);
19989 }
19990
19991 /* Given a DW_FORM_GNU_str_index or DW_FORM_strx, fetch the string.
19992 This is only used by the Fission support. */
19993
19994 static const char *
19995 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19996 {
19997 struct dwarf2_cu *cu = reader->cu;
19998 struct dwarf2_per_objfile *dwarf2_per_objfile
19999 = cu->per_cu->dwarf2_per_objfile;
20000 struct objfile *objfile = dwarf2_per_objfile->objfile;
20001 const char *objf_name = objfile_name (objfile);
20002 bfd *abfd = objfile->obfd;
20003 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
20004 struct dwarf2_section_info *str_offsets_section =
20005 &reader->dwo_file->sections.str_offsets;
20006 const gdb_byte *info_ptr;
20007 ULONGEST str_offset;
20008 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20009
20010 dwarf2_read_section (objfile, str_section);
20011 dwarf2_read_section (objfile, str_offsets_section);
20012 if (str_section->buffer == NULL)
20013 error (_("%s used without .debug_str.dwo section"
20014 " in CU at offset %s [in module %s]"),
20015 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20016 if (str_offsets_section->buffer == NULL)
20017 error (_("%s used without .debug_str_offsets.dwo section"
20018 " in CU at offset %s [in module %s]"),
20019 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20020 if (str_index * cu->header.offset_size >= str_offsets_section->size)
20021 error (_("%s pointing outside of .debug_str_offsets.dwo"
20022 " section in CU at offset %s [in module %s]"),
20023 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20024 info_ptr = (str_offsets_section->buffer
20025 + str_index * cu->header.offset_size);
20026 if (cu->header.offset_size == 4)
20027 str_offset = bfd_get_32 (abfd, info_ptr);
20028 else
20029 str_offset = bfd_get_64 (abfd, info_ptr);
20030 if (str_offset >= str_section->size)
20031 error (_("Offset from %s pointing outside of"
20032 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20033 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20034 return (const char *) (str_section->buffer + str_offset);
20035 }
20036
20037 /* Return the length of an LEB128 number in BUF. */
20038
20039 static int
20040 leb128_size (const gdb_byte *buf)
20041 {
20042 const gdb_byte *begin = buf;
20043 gdb_byte byte;
20044
20045 while (1)
20046 {
20047 byte = *buf++;
20048 if ((byte & 128) == 0)
20049 return buf - begin;
20050 }
20051 }
20052
20053 static void
20054 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20055 {
20056 switch (lang)
20057 {
20058 case DW_LANG_C89:
20059 case DW_LANG_C99:
20060 case DW_LANG_C11:
20061 case DW_LANG_C:
20062 case DW_LANG_UPC:
20063 cu->language = language_c;
20064 break;
20065 case DW_LANG_Java:
20066 case DW_LANG_C_plus_plus:
20067 case DW_LANG_C_plus_plus_11:
20068 case DW_LANG_C_plus_plus_14:
20069 cu->language = language_cplus;
20070 break;
20071 case DW_LANG_D:
20072 cu->language = language_d;
20073 break;
20074 case DW_LANG_Fortran77:
20075 case DW_LANG_Fortran90:
20076 case DW_LANG_Fortran95:
20077 case DW_LANG_Fortran03:
20078 case DW_LANG_Fortran08:
20079 cu->language = language_fortran;
20080 break;
20081 case DW_LANG_Go:
20082 cu->language = language_go;
20083 break;
20084 case DW_LANG_Mips_Assembler:
20085 cu->language = language_asm;
20086 break;
20087 case DW_LANG_Ada83:
20088 case DW_LANG_Ada95:
20089 cu->language = language_ada;
20090 break;
20091 case DW_LANG_Modula2:
20092 cu->language = language_m2;
20093 break;
20094 case DW_LANG_Pascal83:
20095 cu->language = language_pascal;
20096 break;
20097 case DW_LANG_ObjC:
20098 cu->language = language_objc;
20099 break;
20100 case DW_LANG_Rust:
20101 case DW_LANG_Rust_old:
20102 cu->language = language_rust;
20103 break;
20104 case DW_LANG_Cobol74:
20105 case DW_LANG_Cobol85:
20106 default:
20107 cu->language = language_minimal;
20108 break;
20109 }
20110 cu->language_defn = language_def (cu->language);
20111 }
20112
20113 /* Return the named attribute or NULL if not there. */
20114
20115 static struct attribute *
20116 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20117 {
20118 for (;;)
20119 {
20120 unsigned int i;
20121 struct attribute *spec = NULL;
20122
20123 for (i = 0; i < die->num_attrs; ++i)
20124 {
20125 if (die->attrs[i].name == name)
20126 return &die->attrs[i];
20127 if (die->attrs[i].name == DW_AT_specification
20128 || die->attrs[i].name == DW_AT_abstract_origin)
20129 spec = &die->attrs[i];
20130 }
20131
20132 if (!spec)
20133 break;
20134
20135 die = follow_die_ref (die, spec, &cu);
20136 }
20137
20138 return NULL;
20139 }
20140
20141 /* Return the named attribute or NULL if not there,
20142 but do not follow DW_AT_specification, etc.
20143 This is for use in contexts where we're reading .debug_types dies.
20144 Following DW_AT_specification, DW_AT_abstract_origin will take us
20145 back up the chain, and we want to go down. */
20146
20147 static struct attribute *
20148 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
20149 {
20150 unsigned int i;
20151
20152 for (i = 0; i < die->num_attrs; ++i)
20153 if (die->attrs[i].name == name)
20154 return &die->attrs[i];
20155
20156 return NULL;
20157 }
20158
20159 /* Return the string associated with a string-typed attribute, or NULL if it
20160 is either not found or is of an incorrect type. */
20161
20162 static const char *
20163 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20164 {
20165 struct attribute *attr;
20166 const char *str = NULL;
20167
20168 attr = dwarf2_attr (die, name, cu);
20169
20170 if (attr != NULL)
20171 {
20172 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
20173 || attr->form == DW_FORM_string
20174 || attr->form == DW_FORM_strx
20175 || attr->form == DW_FORM_strx1
20176 || attr->form == DW_FORM_strx2
20177 || attr->form == DW_FORM_strx3
20178 || attr->form == DW_FORM_strx4
20179 || attr->form == DW_FORM_GNU_str_index
20180 || attr->form == DW_FORM_GNU_strp_alt)
20181 str = DW_STRING (attr);
20182 else
20183 complaint (_("string type expected for attribute %s for "
20184 "DIE at %s in module %s"),
20185 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20186 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
20187 }
20188
20189 return str;
20190 }
20191
20192 /* Return the dwo name or NULL if not present. If present, it is in either
20193 DW_AT_GNU_dwo_name or DW_AT_dwo_name atrribute. */
20194 static const char *
20195 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20196 {
20197 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20198 if (dwo_name == nullptr)
20199 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20200 return dwo_name;
20201 }
20202
20203 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20204 and holds a non-zero value. This function should only be used for
20205 DW_FORM_flag or DW_FORM_flag_present attributes. */
20206
20207 static int
20208 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20209 {
20210 struct attribute *attr = dwarf2_attr (die, name, cu);
20211
20212 return (attr && DW_UNSND (attr));
20213 }
20214
20215 static int
20216 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20217 {
20218 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20219 which value is non-zero. However, we have to be careful with
20220 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20221 (via dwarf2_flag_true_p) follows this attribute. So we may
20222 end up accidently finding a declaration attribute that belongs
20223 to a different DIE referenced by the specification attribute,
20224 even though the given DIE does not have a declaration attribute. */
20225 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20226 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20227 }
20228
20229 /* Return the die giving the specification for DIE, if there is
20230 one. *SPEC_CU is the CU containing DIE on input, and the CU
20231 containing the return value on output. If there is no
20232 specification, but there is an abstract origin, that is
20233 returned. */
20234
20235 static struct die_info *
20236 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20237 {
20238 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20239 *spec_cu);
20240
20241 if (spec_attr == NULL)
20242 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20243
20244 if (spec_attr == NULL)
20245 return NULL;
20246 else
20247 return follow_die_ref (die, spec_attr, spec_cu);
20248 }
20249
20250 /* Stub for free_line_header to match void * callback types. */
20251
20252 static void
20253 free_line_header_voidp (void *arg)
20254 {
20255 struct line_header *lh = (struct line_header *) arg;
20256
20257 delete lh;
20258 }
20259
20260 void
20261 line_header::add_include_dir (const char *include_dir)
20262 {
20263 if (dwarf_line_debug >= 2)
20264 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20265 include_dirs.size () + 1, include_dir);
20266
20267 include_dirs.push_back (include_dir);
20268 }
20269
20270 void
20271 line_header::add_file_name (const char *name,
20272 dir_index d_index,
20273 unsigned int mod_time,
20274 unsigned int length)
20275 {
20276 if (dwarf_line_debug >= 2)
20277 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
20278 (unsigned) file_names.size () + 1, name);
20279
20280 file_names.emplace_back (name, d_index, mod_time, length);
20281 }
20282
20283 /* A convenience function to find the proper .debug_line section for a CU. */
20284
20285 static struct dwarf2_section_info *
20286 get_debug_line_section (struct dwarf2_cu *cu)
20287 {
20288 struct dwarf2_section_info *section;
20289 struct dwarf2_per_objfile *dwarf2_per_objfile
20290 = cu->per_cu->dwarf2_per_objfile;
20291
20292 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20293 DWO file. */
20294 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20295 section = &cu->dwo_unit->dwo_file->sections.line;
20296 else if (cu->per_cu->is_dwz)
20297 {
20298 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20299
20300 section = &dwz->line;
20301 }
20302 else
20303 section = &dwarf2_per_objfile->line;
20304
20305 return section;
20306 }
20307
20308 /* Read directory or file name entry format, starting with byte of
20309 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20310 entries count and the entries themselves in the described entry
20311 format. */
20312
20313 static void
20314 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20315 bfd *abfd, const gdb_byte **bufp,
20316 struct line_header *lh,
20317 const struct comp_unit_head *cu_header,
20318 void (*callback) (struct line_header *lh,
20319 const char *name,
20320 dir_index d_index,
20321 unsigned int mod_time,
20322 unsigned int length))
20323 {
20324 gdb_byte format_count, formati;
20325 ULONGEST data_count, datai;
20326 const gdb_byte *buf = *bufp;
20327 const gdb_byte *format_header_data;
20328 unsigned int bytes_read;
20329
20330 format_count = read_1_byte (abfd, buf);
20331 buf += 1;
20332 format_header_data = buf;
20333 for (formati = 0; formati < format_count; formati++)
20334 {
20335 read_unsigned_leb128 (abfd, buf, &bytes_read);
20336 buf += bytes_read;
20337 read_unsigned_leb128 (abfd, buf, &bytes_read);
20338 buf += bytes_read;
20339 }
20340
20341 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20342 buf += bytes_read;
20343 for (datai = 0; datai < data_count; datai++)
20344 {
20345 const gdb_byte *format = format_header_data;
20346 struct file_entry fe;
20347
20348 for (formati = 0; formati < format_count; formati++)
20349 {
20350 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20351 format += bytes_read;
20352
20353 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20354 format += bytes_read;
20355
20356 gdb::optional<const char *> string;
20357 gdb::optional<unsigned int> uint;
20358
20359 switch (form)
20360 {
20361 case DW_FORM_string:
20362 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20363 buf += bytes_read;
20364 break;
20365
20366 case DW_FORM_line_strp:
20367 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20368 abfd, buf,
20369 cu_header,
20370 &bytes_read));
20371 buf += bytes_read;
20372 break;
20373
20374 case DW_FORM_data1:
20375 uint.emplace (read_1_byte (abfd, buf));
20376 buf += 1;
20377 break;
20378
20379 case DW_FORM_data2:
20380 uint.emplace (read_2_bytes (abfd, buf));
20381 buf += 2;
20382 break;
20383
20384 case DW_FORM_data4:
20385 uint.emplace (read_4_bytes (abfd, buf));
20386 buf += 4;
20387 break;
20388
20389 case DW_FORM_data8:
20390 uint.emplace (read_8_bytes (abfd, buf));
20391 buf += 8;
20392 break;
20393
20394 case DW_FORM_udata:
20395 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20396 buf += bytes_read;
20397 break;
20398
20399 case DW_FORM_block:
20400 /* It is valid only for DW_LNCT_timestamp which is ignored by
20401 current GDB. */
20402 break;
20403 }
20404
20405 switch (content_type)
20406 {
20407 case DW_LNCT_path:
20408 if (string.has_value ())
20409 fe.name = *string;
20410 break;
20411 case DW_LNCT_directory_index:
20412 if (uint.has_value ())
20413 fe.d_index = (dir_index) *uint;
20414 break;
20415 case DW_LNCT_timestamp:
20416 if (uint.has_value ())
20417 fe.mod_time = *uint;
20418 break;
20419 case DW_LNCT_size:
20420 if (uint.has_value ())
20421 fe.length = *uint;
20422 break;
20423 case DW_LNCT_MD5:
20424 break;
20425 default:
20426 complaint (_("Unknown format content type %s"),
20427 pulongest (content_type));
20428 }
20429 }
20430
20431 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20432 }
20433
20434 *bufp = buf;
20435 }
20436
20437 /* Read the statement program header starting at OFFSET in
20438 .debug_line, or .debug_line.dwo. Return a pointer
20439 to a struct line_header, allocated using xmalloc.
20440 Returns NULL if there is a problem reading the header, e.g., if it
20441 has a version we don't understand.
20442
20443 NOTE: the strings in the include directory and file name tables of
20444 the returned object point into the dwarf line section buffer,
20445 and must not be freed. */
20446
20447 static line_header_up
20448 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20449 {
20450 const gdb_byte *line_ptr;
20451 unsigned int bytes_read, offset_size;
20452 int i;
20453 const char *cur_dir, *cur_file;
20454 struct dwarf2_section_info *section;
20455 bfd *abfd;
20456 struct dwarf2_per_objfile *dwarf2_per_objfile
20457 = cu->per_cu->dwarf2_per_objfile;
20458
20459 section = get_debug_line_section (cu);
20460 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20461 if (section->buffer == NULL)
20462 {
20463 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20464 complaint (_("missing .debug_line.dwo section"));
20465 else
20466 complaint (_("missing .debug_line section"));
20467 return 0;
20468 }
20469
20470 /* We can't do this until we know the section is non-empty.
20471 Only then do we know we have such a section. */
20472 abfd = get_section_bfd_owner (section);
20473
20474 /* Make sure that at least there's room for the total_length field.
20475 That could be 12 bytes long, but we're just going to fudge that. */
20476 if (to_underlying (sect_off) + 4 >= section->size)
20477 {
20478 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20479 return 0;
20480 }
20481
20482 line_header_up lh (new line_header ());
20483
20484 lh->sect_off = sect_off;
20485 lh->offset_in_dwz = cu->per_cu->is_dwz;
20486
20487 line_ptr = section->buffer + to_underlying (sect_off);
20488
20489 /* Read in the header. */
20490 lh->total_length =
20491 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20492 &bytes_read, &offset_size);
20493 line_ptr += bytes_read;
20494 if (line_ptr + lh->total_length > (section->buffer + section->size))
20495 {
20496 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20497 return 0;
20498 }
20499 lh->statement_program_end = line_ptr + lh->total_length;
20500 lh->version = read_2_bytes (abfd, line_ptr);
20501 line_ptr += 2;
20502 if (lh->version > 5)
20503 {
20504 /* This is a version we don't understand. The format could have
20505 changed in ways we don't handle properly so just punt. */
20506 complaint (_("unsupported version in .debug_line section"));
20507 return NULL;
20508 }
20509 if (lh->version >= 5)
20510 {
20511 gdb_byte segment_selector_size;
20512
20513 /* Skip address size. */
20514 read_1_byte (abfd, line_ptr);
20515 line_ptr += 1;
20516
20517 segment_selector_size = read_1_byte (abfd, line_ptr);
20518 line_ptr += 1;
20519 if (segment_selector_size != 0)
20520 {
20521 complaint (_("unsupported segment selector size %u "
20522 "in .debug_line section"),
20523 segment_selector_size);
20524 return NULL;
20525 }
20526 }
20527 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20528 line_ptr += offset_size;
20529 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20530 line_ptr += 1;
20531 if (lh->version >= 4)
20532 {
20533 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20534 line_ptr += 1;
20535 }
20536 else
20537 lh->maximum_ops_per_instruction = 1;
20538
20539 if (lh->maximum_ops_per_instruction == 0)
20540 {
20541 lh->maximum_ops_per_instruction = 1;
20542 complaint (_("invalid maximum_ops_per_instruction "
20543 "in `.debug_line' section"));
20544 }
20545
20546 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20547 line_ptr += 1;
20548 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20549 line_ptr += 1;
20550 lh->line_range = read_1_byte (abfd, line_ptr);
20551 line_ptr += 1;
20552 lh->opcode_base = read_1_byte (abfd, line_ptr);
20553 line_ptr += 1;
20554 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20555
20556 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20557 for (i = 1; i < lh->opcode_base; ++i)
20558 {
20559 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20560 line_ptr += 1;
20561 }
20562
20563 if (lh->version >= 5)
20564 {
20565 /* Read directory table. */
20566 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20567 &cu->header,
20568 [] (struct line_header *header, const char *name,
20569 dir_index d_index, unsigned int mod_time,
20570 unsigned int length)
20571 {
20572 header->add_include_dir (name);
20573 });
20574
20575 /* Read file name table. */
20576 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20577 &cu->header,
20578 [] (struct line_header *header, const char *name,
20579 dir_index d_index, unsigned int mod_time,
20580 unsigned int length)
20581 {
20582 header->add_file_name (name, d_index, mod_time, length);
20583 });
20584 }
20585 else
20586 {
20587 /* Read directory table. */
20588 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20589 {
20590 line_ptr += bytes_read;
20591 lh->add_include_dir (cur_dir);
20592 }
20593 line_ptr += bytes_read;
20594
20595 /* Read file name table. */
20596 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20597 {
20598 unsigned int mod_time, length;
20599 dir_index d_index;
20600
20601 line_ptr += bytes_read;
20602 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20603 line_ptr += bytes_read;
20604 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20605 line_ptr += bytes_read;
20606 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20607 line_ptr += bytes_read;
20608
20609 lh->add_file_name (cur_file, d_index, mod_time, length);
20610 }
20611 line_ptr += bytes_read;
20612 }
20613 lh->statement_program_start = line_ptr;
20614
20615 if (line_ptr > (section->buffer + section->size))
20616 complaint (_("line number info header doesn't "
20617 "fit in `.debug_line' section"));
20618
20619 return lh;
20620 }
20621
20622 /* Subroutine of dwarf_decode_lines to simplify it.
20623 Return the file name of the psymtab for included file FILE_INDEX
20624 in line header LH of PST.
20625 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20626 If space for the result is malloc'd, *NAME_HOLDER will be set.
20627 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20628
20629 static const char *
20630 psymtab_include_file_name (const struct line_header *lh, int file_index,
20631 const struct partial_symtab *pst,
20632 const char *comp_dir,
20633 gdb::unique_xmalloc_ptr<char> *name_holder)
20634 {
20635 const file_entry &fe = lh->file_names[file_index];
20636 const char *include_name = fe.name;
20637 const char *include_name_to_compare = include_name;
20638 const char *pst_filename;
20639 int file_is_pst;
20640
20641 const char *dir_name = fe.include_dir (lh);
20642
20643 gdb::unique_xmalloc_ptr<char> hold_compare;
20644 if (!IS_ABSOLUTE_PATH (include_name)
20645 && (dir_name != NULL || comp_dir != NULL))
20646 {
20647 /* Avoid creating a duplicate psymtab for PST.
20648 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20649 Before we do the comparison, however, we need to account
20650 for DIR_NAME and COMP_DIR.
20651 First prepend dir_name (if non-NULL). If we still don't
20652 have an absolute path prepend comp_dir (if non-NULL).
20653 However, the directory we record in the include-file's
20654 psymtab does not contain COMP_DIR (to match the
20655 corresponding symtab(s)).
20656
20657 Example:
20658
20659 bash$ cd /tmp
20660 bash$ gcc -g ./hello.c
20661 include_name = "hello.c"
20662 dir_name = "."
20663 DW_AT_comp_dir = comp_dir = "/tmp"
20664 DW_AT_name = "./hello.c"
20665
20666 */
20667
20668 if (dir_name != NULL)
20669 {
20670 name_holder->reset (concat (dir_name, SLASH_STRING,
20671 include_name, (char *) NULL));
20672 include_name = name_holder->get ();
20673 include_name_to_compare = include_name;
20674 }
20675 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20676 {
20677 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20678 include_name, (char *) NULL));
20679 include_name_to_compare = hold_compare.get ();
20680 }
20681 }
20682
20683 pst_filename = pst->filename;
20684 gdb::unique_xmalloc_ptr<char> copied_name;
20685 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20686 {
20687 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20688 pst_filename, (char *) NULL));
20689 pst_filename = copied_name.get ();
20690 }
20691
20692 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20693
20694 if (file_is_pst)
20695 return NULL;
20696 return include_name;
20697 }
20698
20699 /* State machine to track the state of the line number program. */
20700
20701 class lnp_state_machine
20702 {
20703 public:
20704 /* Initialize a machine state for the start of a line number
20705 program. */
20706 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20707 bool record_lines_p);
20708
20709 file_entry *current_file ()
20710 {
20711 /* lh->file_names is 0-based, but the file name numbers in the
20712 statement program are 1-based. */
20713 return m_line_header->file_name_at (m_file);
20714 }
20715
20716 /* Record the line in the state machine. END_SEQUENCE is true if
20717 we're processing the end of a sequence. */
20718 void record_line (bool end_sequence);
20719
20720 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20721 nop-out rest of the lines in this sequence. */
20722 void check_line_address (struct dwarf2_cu *cu,
20723 const gdb_byte *line_ptr,
20724 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20725
20726 void handle_set_discriminator (unsigned int discriminator)
20727 {
20728 m_discriminator = discriminator;
20729 m_line_has_non_zero_discriminator |= discriminator != 0;
20730 }
20731
20732 /* Handle DW_LNE_set_address. */
20733 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20734 {
20735 m_op_index = 0;
20736 address += baseaddr;
20737 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20738 }
20739
20740 /* Handle DW_LNS_advance_pc. */
20741 void handle_advance_pc (CORE_ADDR adjust);
20742
20743 /* Handle a special opcode. */
20744 void handle_special_opcode (unsigned char op_code);
20745
20746 /* Handle DW_LNS_advance_line. */
20747 void handle_advance_line (int line_delta)
20748 {
20749 advance_line (line_delta);
20750 }
20751
20752 /* Handle DW_LNS_set_file. */
20753 void handle_set_file (file_name_index file);
20754
20755 /* Handle DW_LNS_negate_stmt. */
20756 void handle_negate_stmt ()
20757 {
20758 m_is_stmt = !m_is_stmt;
20759 }
20760
20761 /* Handle DW_LNS_const_add_pc. */
20762 void handle_const_add_pc ();
20763
20764 /* Handle DW_LNS_fixed_advance_pc. */
20765 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20766 {
20767 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20768 m_op_index = 0;
20769 }
20770
20771 /* Handle DW_LNS_copy. */
20772 void handle_copy ()
20773 {
20774 record_line (false);
20775 m_discriminator = 0;
20776 }
20777
20778 /* Handle DW_LNE_end_sequence. */
20779 void handle_end_sequence ()
20780 {
20781 m_currently_recording_lines = true;
20782 }
20783
20784 private:
20785 /* Advance the line by LINE_DELTA. */
20786 void advance_line (int line_delta)
20787 {
20788 m_line += line_delta;
20789
20790 if (line_delta != 0)
20791 m_line_has_non_zero_discriminator = m_discriminator != 0;
20792 }
20793
20794 struct dwarf2_cu *m_cu;
20795
20796 gdbarch *m_gdbarch;
20797
20798 /* True if we're recording lines.
20799 Otherwise we're building partial symtabs and are just interested in
20800 finding include files mentioned by the line number program. */
20801 bool m_record_lines_p;
20802
20803 /* The line number header. */
20804 line_header *m_line_header;
20805
20806 /* These are part of the standard DWARF line number state machine,
20807 and initialized according to the DWARF spec. */
20808
20809 unsigned char m_op_index = 0;
20810 /* The line table index (1-based) of the current file. */
20811 file_name_index m_file = (file_name_index) 1;
20812 unsigned int m_line = 1;
20813
20814 /* These are initialized in the constructor. */
20815
20816 CORE_ADDR m_address;
20817 bool m_is_stmt;
20818 unsigned int m_discriminator;
20819
20820 /* Additional bits of state we need to track. */
20821
20822 /* The last file that we called dwarf2_start_subfile for.
20823 This is only used for TLLs. */
20824 unsigned int m_last_file = 0;
20825 /* The last file a line number was recorded for. */
20826 struct subfile *m_last_subfile = NULL;
20827
20828 /* When true, record the lines we decode. */
20829 bool m_currently_recording_lines = false;
20830
20831 /* The last line number that was recorded, used to coalesce
20832 consecutive entries for the same line. This can happen, for
20833 example, when discriminators are present. PR 17276. */
20834 unsigned int m_last_line = 0;
20835 bool m_line_has_non_zero_discriminator = false;
20836 };
20837
20838 void
20839 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20840 {
20841 CORE_ADDR addr_adj = (((m_op_index + adjust)
20842 / m_line_header->maximum_ops_per_instruction)
20843 * m_line_header->minimum_instruction_length);
20844 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20845 m_op_index = ((m_op_index + adjust)
20846 % m_line_header->maximum_ops_per_instruction);
20847 }
20848
20849 void
20850 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20851 {
20852 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20853 CORE_ADDR addr_adj = (((m_op_index
20854 + (adj_opcode / m_line_header->line_range))
20855 / m_line_header->maximum_ops_per_instruction)
20856 * m_line_header->minimum_instruction_length);
20857 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20858 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20859 % m_line_header->maximum_ops_per_instruction);
20860
20861 int line_delta = (m_line_header->line_base
20862 + (adj_opcode % m_line_header->line_range));
20863 advance_line (line_delta);
20864 record_line (false);
20865 m_discriminator = 0;
20866 }
20867
20868 void
20869 lnp_state_machine::handle_set_file (file_name_index file)
20870 {
20871 m_file = file;
20872
20873 const file_entry *fe = current_file ();
20874 if (fe == NULL)
20875 dwarf2_debug_line_missing_file_complaint ();
20876 else if (m_record_lines_p)
20877 {
20878 const char *dir = fe->include_dir (m_line_header);
20879
20880 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20881 m_line_has_non_zero_discriminator = m_discriminator != 0;
20882 dwarf2_start_subfile (m_cu, fe->name, dir);
20883 }
20884 }
20885
20886 void
20887 lnp_state_machine::handle_const_add_pc ()
20888 {
20889 CORE_ADDR adjust
20890 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20891
20892 CORE_ADDR addr_adj
20893 = (((m_op_index + adjust)
20894 / m_line_header->maximum_ops_per_instruction)
20895 * m_line_header->minimum_instruction_length);
20896
20897 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20898 m_op_index = ((m_op_index + adjust)
20899 % m_line_header->maximum_ops_per_instruction);
20900 }
20901
20902 /* Return non-zero if we should add LINE to the line number table.
20903 LINE is the line to add, LAST_LINE is the last line that was added,
20904 LAST_SUBFILE is the subfile for LAST_LINE.
20905 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20906 had a non-zero discriminator.
20907
20908 We have to be careful in the presence of discriminators.
20909 E.g., for this line:
20910
20911 for (i = 0; i < 100000; i++);
20912
20913 clang can emit four line number entries for that one line,
20914 each with a different discriminator.
20915 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20916
20917 However, we want gdb to coalesce all four entries into one.
20918 Otherwise the user could stepi into the middle of the line and
20919 gdb would get confused about whether the pc really was in the
20920 middle of the line.
20921
20922 Things are further complicated by the fact that two consecutive
20923 line number entries for the same line is a heuristic used by gcc
20924 to denote the end of the prologue. So we can't just discard duplicate
20925 entries, we have to be selective about it. The heuristic we use is
20926 that we only collapse consecutive entries for the same line if at least
20927 one of those entries has a non-zero discriminator. PR 17276.
20928
20929 Note: Addresses in the line number state machine can never go backwards
20930 within one sequence, thus this coalescing is ok. */
20931
20932 static int
20933 dwarf_record_line_p (struct dwarf2_cu *cu,
20934 unsigned int line, unsigned int last_line,
20935 int line_has_non_zero_discriminator,
20936 struct subfile *last_subfile)
20937 {
20938 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20939 return 1;
20940 if (line != last_line)
20941 return 1;
20942 /* Same line for the same file that we've seen already.
20943 As a last check, for pr 17276, only record the line if the line
20944 has never had a non-zero discriminator. */
20945 if (!line_has_non_zero_discriminator)
20946 return 1;
20947 return 0;
20948 }
20949
20950 /* Use the CU's builder to record line number LINE beginning at
20951 address ADDRESS in the line table of subfile SUBFILE. */
20952
20953 static void
20954 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20955 unsigned int line, CORE_ADDR address,
20956 struct dwarf2_cu *cu)
20957 {
20958 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20959
20960 if (dwarf_line_debug)
20961 {
20962 fprintf_unfiltered (gdb_stdlog,
20963 "Recording line %u, file %s, address %s\n",
20964 line, lbasename (subfile->name),
20965 paddress (gdbarch, address));
20966 }
20967
20968 if (cu != nullptr)
20969 cu->get_builder ()->record_line (subfile, line, addr);
20970 }
20971
20972 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20973 Mark the end of a set of line number records.
20974 The arguments are the same as for dwarf_record_line_1.
20975 If SUBFILE is NULL the request is ignored. */
20976
20977 static void
20978 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20979 CORE_ADDR address, struct dwarf2_cu *cu)
20980 {
20981 if (subfile == NULL)
20982 return;
20983
20984 if (dwarf_line_debug)
20985 {
20986 fprintf_unfiltered (gdb_stdlog,
20987 "Finishing current line, file %s, address %s\n",
20988 lbasename (subfile->name),
20989 paddress (gdbarch, address));
20990 }
20991
20992 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
20993 }
20994
20995 void
20996 lnp_state_machine::record_line (bool end_sequence)
20997 {
20998 if (dwarf_line_debug)
20999 {
21000 fprintf_unfiltered (gdb_stdlog,
21001 "Processing actual line %u: file %u,"
21002 " address %s, is_stmt %u, discrim %u\n",
21003 m_line, to_underlying (m_file),
21004 paddress (m_gdbarch, m_address),
21005 m_is_stmt, m_discriminator);
21006 }
21007
21008 file_entry *fe = current_file ();
21009
21010 if (fe == NULL)
21011 dwarf2_debug_line_missing_file_complaint ();
21012 /* For now we ignore lines not starting on an instruction boundary.
21013 But not when processing end_sequence for compatibility with the
21014 previous version of the code. */
21015 else if (m_op_index == 0 || end_sequence)
21016 {
21017 fe->included_p = 1;
21018 if (m_record_lines_p && (producer_is_codewarrior (m_cu) || m_is_stmt))
21019 {
21020 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
21021 || end_sequence)
21022 {
21023 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21024 m_currently_recording_lines ? m_cu : nullptr);
21025 }
21026
21027 if (!end_sequence)
21028 {
21029 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21030 m_line_has_non_zero_discriminator,
21031 m_last_subfile))
21032 {
21033 buildsym_compunit *builder = m_cu->get_builder ();
21034 dwarf_record_line_1 (m_gdbarch,
21035 builder->get_current_subfile (),
21036 m_line, m_address,
21037 m_currently_recording_lines ? m_cu : nullptr);
21038 }
21039 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21040 m_last_line = m_line;
21041 }
21042 }
21043 }
21044 }
21045
21046 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21047 line_header *lh, bool record_lines_p)
21048 {
21049 m_cu = cu;
21050 m_gdbarch = arch;
21051 m_record_lines_p = record_lines_p;
21052 m_line_header = lh;
21053
21054 m_currently_recording_lines = true;
21055
21056 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21057 was a line entry for it so that the backend has a chance to adjust it
21058 and also record it in case it needs it. This is currently used by MIPS
21059 code, cf. `mips_adjust_dwarf2_line'. */
21060 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21061 m_is_stmt = lh->default_is_stmt;
21062 m_discriminator = 0;
21063 }
21064
21065 void
21066 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21067 const gdb_byte *line_ptr,
21068 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21069 {
21070 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21071 the pc range of the CU. However, we restrict the test to only ADDRESS
21072 values of zero to preserve GDB's previous behaviour which is to handle
21073 the specific case of a function being GC'd by the linker. */
21074
21075 if (address == 0 && address < unrelocated_lowpc)
21076 {
21077 /* This line table is for a function which has been
21078 GCd by the linker. Ignore it. PR gdb/12528 */
21079
21080 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21081 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21082
21083 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21084 line_offset, objfile_name (objfile));
21085 m_currently_recording_lines = false;
21086 /* Note: m_currently_recording_lines is left as false until we see
21087 DW_LNE_end_sequence. */
21088 }
21089 }
21090
21091 /* Subroutine of dwarf_decode_lines to simplify it.
21092 Process the line number information in LH.
21093 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21094 program in order to set included_p for every referenced header. */
21095
21096 static void
21097 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21098 const int decode_for_pst_p, CORE_ADDR lowpc)
21099 {
21100 const gdb_byte *line_ptr, *extended_end;
21101 const gdb_byte *line_end;
21102 unsigned int bytes_read, extended_len;
21103 unsigned char op_code, extended_op;
21104 CORE_ADDR baseaddr;
21105 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21106 bfd *abfd = objfile->obfd;
21107 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21108 /* True if we're recording line info (as opposed to building partial
21109 symtabs and just interested in finding include files mentioned by
21110 the line number program). */
21111 bool record_lines_p = !decode_for_pst_p;
21112
21113 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21114
21115 line_ptr = lh->statement_program_start;
21116 line_end = lh->statement_program_end;
21117
21118 /* Read the statement sequences until there's nothing left. */
21119 while (line_ptr < line_end)
21120 {
21121 /* The DWARF line number program state machine. Reset the state
21122 machine at the start of each sequence. */
21123 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21124 bool end_sequence = false;
21125
21126 if (record_lines_p)
21127 {
21128 /* Start a subfile for the current file of the state
21129 machine. */
21130 const file_entry *fe = state_machine.current_file ();
21131
21132 if (fe != NULL)
21133 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21134 }
21135
21136 /* Decode the table. */
21137 while (line_ptr < line_end && !end_sequence)
21138 {
21139 op_code = read_1_byte (abfd, line_ptr);
21140 line_ptr += 1;
21141
21142 if (op_code >= lh->opcode_base)
21143 {
21144 /* Special opcode. */
21145 state_machine.handle_special_opcode (op_code);
21146 }
21147 else switch (op_code)
21148 {
21149 case DW_LNS_extended_op:
21150 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21151 &bytes_read);
21152 line_ptr += bytes_read;
21153 extended_end = line_ptr + extended_len;
21154 extended_op = read_1_byte (abfd, line_ptr);
21155 line_ptr += 1;
21156 switch (extended_op)
21157 {
21158 case DW_LNE_end_sequence:
21159 state_machine.handle_end_sequence ();
21160 end_sequence = true;
21161 break;
21162 case DW_LNE_set_address:
21163 {
21164 CORE_ADDR address
21165 = read_address (abfd, line_ptr, cu, &bytes_read);
21166 line_ptr += bytes_read;
21167
21168 state_machine.check_line_address (cu, line_ptr,
21169 lowpc - baseaddr, address);
21170 state_machine.handle_set_address (baseaddr, address);
21171 }
21172 break;
21173 case DW_LNE_define_file:
21174 {
21175 const char *cur_file;
21176 unsigned int mod_time, length;
21177 dir_index dindex;
21178
21179 cur_file = read_direct_string (abfd, line_ptr,
21180 &bytes_read);
21181 line_ptr += bytes_read;
21182 dindex = (dir_index)
21183 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21184 line_ptr += bytes_read;
21185 mod_time =
21186 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21187 line_ptr += bytes_read;
21188 length =
21189 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21190 line_ptr += bytes_read;
21191 lh->add_file_name (cur_file, dindex, mod_time, length);
21192 }
21193 break;
21194 case DW_LNE_set_discriminator:
21195 {
21196 /* The discriminator is not interesting to the
21197 debugger; just ignore it. We still need to
21198 check its value though:
21199 if there are consecutive entries for the same
21200 (non-prologue) line we want to coalesce them.
21201 PR 17276. */
21202 unsigned int discr
21203 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21204 line_ptr += bytes_read;
21205
21206 state_machine.handle_set_discriminator (discr);
21207 }
21208 break;
21209 default:
21210 complaint (_("mangled .debug_line section"));
21211 return;
21212 }
21213 /* Make sure that we parsed the extended op correctly. If e.g.
21214 we expected a different address size than the producer used,
21215 we may have read the wrong number of bytes. */
21216 if (line_ptr != extended_end)
21217 {
21218 complaint (_("mangled .debug_line section"));
21219 return;
21220 }
21221 break;
21222 case DW_LNS_copy:
21223 state_machine.handle_copy ();
21224 break;
21225 case DW_LNS_advance_pc:
21226 {
21227 CORE_ADDR adjust
21228 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21229 line_ptr += bytes_read;
21230
21231 state_machine.handle_advance_pc (adjust);
21232 }
21233 break;
21234 case DW_LNS_advance_line:
21235 {
21236 int line_delta
21237 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21238 line_ptr += bytes_read;
21239
21240 state_machine.handle_advance_line (line_delta);
21241 }
21242 break;
21243 case DW_LNS_set_file:
21244 {
21245 file_name_index file
21246 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21247 &bytes_read);
21248 line_ptr += bytes_read;
21249
21250 state_machine.handle_set_file (file);
21251 }
21252 break;
21253 case DW_LNS_set_column:
21254 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21255 line_ptr += bytes_read;
21256 break;
21257 case DW_LNS_negate_stmt:
21258 state_machine.handle_negate_stmt ();
21259 break;
21260 case DW_LNS_set_basic_block:
21261 break;
21262 /* Add to the address register of the state machine the
21263 address increment value corresponding to special opcode
21264 255. I.e., this value is scaled by the minimum
21265 instruction length since special opcode 255 would have
21266 scaled the increment. */
21267 case DW_LNS_const_add_pc:
21268 state_machine.handle_const_add_pc ();
21269 break;
21270 case DW_LNS_fixed_advance_pc:
21271 {
21272 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21273 line_ptr += 2;
21274
21275 state_machine.handle_fixed_advance_pc (addr_adj);
21276 }
21277 break;
21278 default:
21279 {
21280 /* Unknown standard opcode, ignore it. */
21281 int i;
21282
21283 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21284 {
21285 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21286 line_ptr += bytes_read;
21287 }
21288 }
21289 }
21290 }
21291
21292 if (!end_sequence)
21293 dwarf2_debug_line_missing_end_sequence_complaint ();
21294
21295 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21296 in which case we still finish recording the last line). */
21297 state_machine.record_line (true);
21298 }
21299 }
21300
21301 /* Decode the Line Number Program (LNP) for the given line_header
21302 structure and CU. The actual information extracted and the type
21303 of structures created from the LNP depends on the value of PST.
21304
21305 1. If PST is NULL, then this procedure uses the data from the program
21306 to create all necessary symbol tables, and their linetables.
21307
21308 2. If PST is not NULL, this procedure reads the program to determine
21309 the list of files included by the unit represented by PST, and
21310 builds all the associated partial symbol tables.
21311
21312 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21313 It is used for relative paths in the line table.
21314 NOTE: When processing partial symtabs (pst != NULL),
21315 comp_dir == pst->dirname.
21316
21317 NOTE: It is important that psymtabs have the same file name (via strcmp)
21318 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21319 symtab we don't use it in the name of the psymtabs we create.
21320 E.g. expand_line_sal requires this when finding psymtabs to expand.
21321 A good testcase for this is mb-inline.exp.
21322
21323 LOWPC is the lowest address in CU (or 0 if not known).
21324
21325 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21326 for its PC<->lines mapping information. Otherwise only the filename
21327 table is read in. */
21328
21329 static void
21330 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21331 struct dwarf2_cu *cu, struct partial_symtab *pst,
21332 CORE_ADDR lowpc, int decode_mapping)
21333 {
21334 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21335 const int decode_for_pst_p = (pst != NULL);
21336
21337 if (decode_mapping)
21338 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21339
21340 if (decode_for_pst_p)
21341 {
21342 int file_index;
21343
21344 /* Now that we're done scanning the Line Header Program, we can
21345 create the psymtab of each included file. */
21346 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
21347 if (lh->file_names[file_index].included_p == 1)
21348 {
21349 gdb::unique_xmalloc_ptr<char> name_holder;
21350 const char *include_name =
21351 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21352 &name_holder);
21353 if (include_name != NULL)
21354 dwarf2_create_include_psymtab (include_name, pst, objfile);
21355 }
21356 }
21357 else
21358 {
21359 /* Make sure a symtab is created for every file, even files
21360 which contain only variables (i.e. no code with associated
21361 line numbers). */
21362 buildsym_compunit *builder = cu->get_builder ();
21363 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21364 int i;
21365
21366 for (i = 0; i < lh->file_names.size (); i++)
21367 {
21368 file_entry &fe = lh->file_names[i];
21369
21370 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21371
21372 if (builder->get_current_subfile ()->symtab == NULL)
21373 {
21374 builder->get_current_subfile ()->symtab
21375 = allocate_symtab (cust,
21376 builder->get_current_subfile ()->name);
21377 }
21378 fe.symtab = builder->get_current_subfile ()->symtab;
21379 }
21380 }
21381 }
21382
21383 /* Start a subfile for DWARF. FILENAME is the name of the file and
21384 DIRNAME the name of the source directory which contains FILENAME
21385 or NULL if not known.
21386 This routine tries to keep line numbers from identical absolute and
21387 relative file names in a common subfile.
21388
21389 Using the `list' example from the GDB testsuite, which resides in
21390 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21391 of /srcdir/list0.c yields the following debugging information for list0.c:
21392
21393 DW_AT_name: /srcdir/list0.c
21394 DW_AT_comp_dir: /compdir
21395 files.files[0].name: list0.h
21396 files.files[0].dir: /srcdir
21397 files.files[1].name: list0.c
21398 files.files[1].dir: /srcdir
21399
21400 The line number information for list0.c has to end up in a single
21401 subfile, so that `break /srcdir/list0.c:1' works as expected.
21402 start_subfile will ensure that this happens provided that we pass the
21403 concatenation of files.files[1].dir and files.files[1].name as the
21404 subfile's name. */
21405
21406 static void
21407 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21408 const char *dirname)
21409 {
21410 char *copy = NULL;
21411
21412 /* In order not to lose the line information directory,
21413 we concatenate it to the filename when it makes sense.
21414 Note that the Dwarf3 standard says (speaking of filenames in line
21415 information): ``The directory index is ignored for file names
21416 that represent full path names''. Thus ignoring dirname in the
21417 `else' branch below isn't an issue. */
21418
21419 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21420 {
21421 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21422 filename = copy;
21423 }
21424
21425 cu->get_builder ()->start_subfile (filename);
21426
21427 if (copy != NULL)
21428 xfree (copy);
21429 }
21430
21431 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21432 buildsym_compunit constructor. */
21433
21434 struct compunit_symtab *
21435 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21436 CORE_ADDR low_pc)
21437 {
21438 gdb_assert (m_builder == nullptr);
21439
21440 m_builder.reset (new struct buildsym_compunit
21441 (per_cu->dwarf2_per_objfile->objfile,
21442 name, comp_dir, language, low_pc));
21443
21444 list_in_scope = get_builder ()->get_file_symbols ();
21445
21446 get_builder ()->record_debugformat ("DWARF 2");
21447 get_builder ()->record_producer (producer);
21448
21449 processing_has_namespace_info = false;
21450
21451 return get_builder ()->get_compunit_symtab ();
21452 }
21453
21454 static void
21455 var_decode_location (struct attribute *attr, struct symbol *sym,
21456 struct dwarf2_cu *cu)
21457 {
21458 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21459 struct comp_unit_head *cu_header = &cu->header;
21460
21461 /* NOTE drow/2003-01-30: There used to be a comment and some special
21462 code here to turn a symbol with DW_AT_external and a
21463 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21464 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21465 with some versions of binutils) where shared libraries could have
21466 relocations against symbols in their debug information - the
21467 minimal symbol would have the right address, but the debug info
21468 would not. It's no longer necessary, because we will explicitly
21469 apply relocations when we read in the debug information now. */
21470
21471 /* A DW_AT_location attribute with no contents indicates that a
21472 variable has been optimized away. */
21473 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21474 {
21475 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21476 return;
21477 }
21478
21479 /* Handle one degenerate form of location expression specially, to
21480 preserve GDB's previous behavior when section offsets are
21481 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21482 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21483
21484 if (attr_form_is_block (attr)
21485 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21486 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21487 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21488 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
21489 && (DW_BLOCK (attr)->size
21490 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21491 {
21492 unsigned int dummy;
21493
21494 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21495 SET_SYMBOL_VALUE_ADDRESS (sym,
21496 read_address (objfile->obfd,
21497 DW_BLOCK (attr)->data + 1,
21498 cu, &dummy));
21499 else
21500 SET_SYMBOL_VALUE_ADDRESS
21501 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21502 &dummy));
21503 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21504 fixup_symbol_section (sym, objfile);
21505 SET_SYMBOL_VALUE_ADDRESS (sym,
21506 SYMBOL_VALUE_ADDRESS (sym)
21507 + ANOFFSET (objfile->section_offsets,
21508 SYMBOL_SECTION (sym)));
21509 return;
21510 }
21511
21512 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21513 expression evaluator, and use LOC_COMPUTED only when necessary
21514 (i.e. when the value of a register or memory location is
21515 referenced, or a thread-local block, etc.). Then again, it might
21516 not be worthwhile. I'm assuming that it isn't unless performance
21517 or memory numbers show me otherwise. */
21518
21519 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21520
21521 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21522 cu->has_loclist = true;
21523 }
21524
21525 /* Given a pointer to a DWARF information entry, figure out if we need
21526 to make a symbol table entry for it, and if so, create a new entry
21527 and return a pointer to it.
21528 If TYPE is NULL, determine symbol type from the die, otherwise
21529 used the passed type.
21530 If SPACE is not NULL, use it to hold the new symbol. If it is
21531 NULL, allocate a new symbol on the objfile's obstack. */
21532
21533 static struct symbol *
21534 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21535 struct symbol *space)
21536 {
21537 struct dwarf2_per_objfile *dwarf2_per_objfile
21538 = cu->per_cu->dwarf2_per_objfile;
21539 struct objfile *objfile = dwarf2_per_objfile->objfile;
21540 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21541 struct symbol *sym = NULL;
21542 const char *name;
21543 struct attribute *attr = NULL;
21544 struct attribute *attr2 = NULL;
21545 CORE_ADDR baseaddr;
21546 struct pending **list_to_add = NULL;
21547
21548 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21549
21550 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21551
21552 name = dwarf2_name (die, cu);
21553 if (name)
21554 {
21555 const char *linkagename;
21556 int suppress_add = 0;
21557
21558 if (space)
21559 sym = space;
21560 else
21561 sym = allocate_symbol (objfile);
21562 OBJSTAT (objfile, n_syms++);
21563
21564 /* Cache this symbol's name and the name's demangled form (if any). */
21565 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21566 linkagename = dwarf2_physname (name, die, cu);
21567 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21568
21569 /* Fortran does not have mangling standard and the mangling does differ
21570 between gfortran, iFort etc. */
21571 if (cu->language == language_fortran
21572 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21573 symbol_set_demangled_name (&(sym->ginfo),
21574 dwarf2_full_name (name, die, cu),
21575 NULL);
21576
21577 /* Default assumptions.
21578 Use the passed type or decode it from the die. */
21579 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21580 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21581 if (type != NULL)
21582 SYMBOL_TYPE (sym) = type;
21583 else
21584 SYMBOL_TYPE (sym) = die_type (die, cu);
21585 attr = dwarf2_attr (die,
21586 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21587 cu);
21588 if (attr)
21589 {
21590 SYMBOL_LINE (sym) = DW_UNSND (attr);
21591 }
21592
21593 attr = dwarf2_attr (die,
21594 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21595 cu);
21596 if (attr)
21597 {
21598 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21599 struct file_entry *fe;
21600
21601 if (cu->line_header != NULL)
21602 fe = cu->line_header->file_name_at (file_index);
21603 else
21604 fe = NULL;
21605
21606 if (fe == NULL)
21607 complaint (_("file index out of range"));
21608 else
21609 symbol_set_symtab (sym, fe->symtab);
21610 }
21611
21612 switch (die->tag)
21613 {
21614 case DW_TAG_label:
21615 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21616 if (attr)
21617 {
21618 CORE_ADDR addr;
21619
21620 addr = attr_value_as_address (attr);
21621 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21622 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21623 }
21624 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21625 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21626 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21627 add_symbol_to_list (sym, cu->list_in_scope);
21628 break;
21629 case DW_TAG_subprogram:
21630 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21631 finish_block. */
21632 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21633 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21634 if ((attr2 && (DW_UNSND (attr2) != 0))
21635 || cu->language == language_ada
21636 || cu->language == language_fortran)
21637 {
21638 /* Subprograms marked external are stored as a global symbol.
21639 Ada and Fortran subprograms, whether marked external or
21640 not, are always stored as a global symbol, because we want
21641 to be able to access them globally. For instance, we want
21642 to be able to break on a nested subprogram without having
21643 to specify the context. */
21644 list_to_add = cu->get_builder ()->get_global_symbols ();
21645 }
21646 else
21647 {
21648 list_to_add = cu->list_in_scope;
21649 }
21650 break;
21651 case DW_TAG_inlined_subroutine:
21652 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21653 finish_block. */
21654 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21655 SYMBOL_INLINED (sym) = 1;
21656 list_to_add = cu->list_in_scope;
21657 break;
21658 case DW_TAG_template_value_param:
21659 suppress_add = 1;
21660 /* Fall through. */
21661 case DW_TAG_constant:
21662 case DW_TAG_variable:
21663 case DW_TAG_member:
21664 /* Compilation with minimal debug info may result in
21665 variables with missing type entries. Change the
21666 misleading `void' type to something sensible. */
21667 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21668 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21669
21670 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21671 /* In the case of DW_TAG_member, we should only be called for
21672 static const members. */
21673 if (die->tag == DW_TAG_member)
21674 {
21675 /* dwarf2_add_field uses die_is_declaration,
21676 so we do the same. */
21677 gdb_assert (die_is_declaration (die, cu));
21678 gdb_assert (attr);
21679 }
21680 if (attr)
21681 {
21682 dwarf2_const_value (attr, sym, cu);
21683 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21684 if (!suppress_add)
21685 {
21686 if (attr2 && (DW_UNSND (attr2) != 0))
21687 list_to_add = cu->get_builder ()->get_global_symbols ();
21688 else
21689 list_to_add = cu->list_in_scope;
21690 }
21691 break;
21692 }
21693 attr = dwarf2_attr (die, DW_AT_location, cu);
21694 if (attr)
21695 {
21696 var_decode_location (attr, sym, cu);
21697 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21698
21699 /* Fortran explicitly imports any global symbols to the local
21700 scope by DW_TAG_common_block. */
21701 if (cu->language == language_fortran && die->parent
21702 && die->parent->tag == DW_TAG_common_block)
21703 attr2 = NULL;
21704
21705 if (SYMBOL_CLASS (sym) == LOC_STATIC
21706 && SYMBOL_VALUE_ADDRESS (sym) == 0
21707 && !dwarf2_per_objfile->has_section_at_zero)
21708 {
21709 /* When a static variable is eliminated by the linker,
21710 the corresponding debug information is not stripped
21711 out, but the variable address is set to null;
21712 do not add such variables into symbol table. */
21713 }
21714 else if (attr2 && (DW_UNSND (attr2) != 0))
21715 {
21716 if (SYMBOL_CLASS (sym) == LOC_STATIC
21717 && (objfile->flags & OBJF_MAINLINE) == 0
21718 && dwarf2_per_objfile->can_copy)
21719 {
21720 /* A global static variable might be subject to
21721 copy relocation. We first check for a local
21722 minsym, though, because maybe the symbol was
21723 marked hidden, in which case this would not
21724 apply. */
21725 bound_minimal_symbol found
21726 = (lookup_minimal_symbol_linkage
21727 (SYMBOL_LINKAGE_NAME (sym), objfile));
21728 if (found.minsym != nullptr)
21729 sym->maybe_copied = 1;
21730 }
21731
21732 /* A variable with DW_AT_external is never static,
21733 but it may be block-scoped. */
21734 list_to_add
21735 = ((cu->list_in_scope
21736 == cu->get_builder ()->get_file_symbols ())
21737 ? cu->get_builder ()->get_global_symbols ()
21738 : cu->list_in_scope);
21739 }
21740 else
21741 list_to_add = cu->list_in_scope;
21742 }
21743 else
21744 {
21745 /* We do not know the address of this symbol.
21746 If it is an external symbol and we have type information
21747 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21748 The address of the variable will then be determined from
21749 the minimal symbol table whenever the variable is
21750 referenced. */
21751 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21752
21753 /* Fortran explicitly imports any global symbols to the local
21754 scope by DW_TAG_common_block. */
21755 if (cu->language == language_fortran && die->parent
21756 && die->parent->tag == DW_TAG_common_block)
21757 {
21758 /* SYMBOL_CLASS doesn't matter here because
21759 read_common_block is going to reset it. */
21760 if (!suppress_add)
21761 list_to_add = cu->list_in_scope;
21762 }
21763 else if (attr2 && (DW_UNSND (attr2) != 0)
21764 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21765 {
21766 /* A variable with DW_AT_external is never static, but it
21767 may be block-scoped. */
21768 list_to_add
21769 = ((cu->list_in_scope
21770 == cu->get_builder ()->get_file_symbols ())
21771 ? cu->get_builder ()->get_global_symbols ()
21772 : cu->list_in_scope);
21773
21774 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21775 }
21776 else if (!die_is_declaration (die, cu))
21777 {
21778 /* Use the default LOC_OPTIMIZED_OUT class. */
21779 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21780 if (!suppress_add)
21781 list_to_add = cu->list_in_scope;
21782 }
21783 }
21784 break;
21785 case DW_TAG_formal_parameter:
21786 {
21787 /* If we are inside a function, mark this as an argument. If
21788 not, we might be looking at an argument to an inlined function
21789 when we do not have enough information to show inlined frames;
21790 pretend it's a local variable in that case so that the user can
21791 still see it. */
21792 struct context_stack *curr
21793 = cu->get_builder ()->get_current_context_stack ();
21794 if (curr != nullptr && curr->name != nullptr)
21795 SYMBOL_IS_ARGUMENT (sym) = 1;
21796 attr = dwarf2_attr (die, DW_AT_location, cu);
21797 if (attr)
21798 {
21799 var_decode_location (attr, sym, cu);
21800 }
21801 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21802 if (attr)
21803 {
21804 dwarf2_const_value (attr, sym, cu);
21805 }
21806
21807 list_to_add = cu->list_in_scope;
21808 }
21809 break;
21810 case DW_TAG_unspecified_parameters:
21811 /* From varargs functions; gdb doesn't seem to have any
21812 interest in this information, so just ignore it for now.
21813 (FIXME?) */
21814 break;
21815 case DW_TAG_template_type_param:
21816 suppress_add = 1;
21817 /* Fall through. */
21818 case DW_TAG_class_type:
21819 case DW_TAG_interface_type:
21820 case DW_TAG_structure_type:
21821 case DW_TAG_union_type:
21822 case DW_TAG_set_type:
21823 case DW_TAG_enumeration_type:
21824 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21825 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21826
21827 {
21828 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21829 really ever be static objects: otherwise, if you try
21830 to, say, break of a class's method and you're in a file
21831 which doesn't mention that class, it won't work unless
21832 the check for all static symbols in lookup_symbol_aux
21833 saves you. See the OtherFileClass tests in
21834 gdb.c++/namespace.exp. */
21835
21836 if (!suppress_add)
21837 {
21838 buildsym_compunit *builder = cu->get_builder ();
21839 list_to_add
21840 = (cu->list_in_scope == builder->get_file_symbols ()
21841 && cu->language == language_cplus
21842 ? builder->get_global_symbols ()
21843 : cu->list_in_scope);
21844
21845 /* The semantics of C++ state that "struct foo {
21846 ... }" also defines a typedef for "foo". */
21847 if (cu->language == language_cplus
21848 || cu->language == language_ada
21849 || cu->language == language_d
21850 || cu->language == language_rust)
21851 {
21852 /* The symbol's name is already allocated along
21853 with this objfile, so we don't need to
21854 duplicate it for the type. */
21855 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21856 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21857 }
21858 }
21859 }
21860 break;
21861 case DW_TAG_typedef:
21862 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21863 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21864 list_to_add = cu->list_in_scope;
21865 break;
21866 case DW_TAG_base_type:
21867 case DW_TAG_subrange_type:
21868 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21869 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21870 list_to_add = cu->list_in_scope;
21871 break;
21872 case DW_TAG_enumerator:
21873 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21874 if (attr)
21875 {
21876 dwarf2_const_value (attr, sym, cu);
21877 }
21878 {
21879 /* NOTE: carlton/2003-11-10: See comment above in the
21880 DW_TAG_class_type, etc. block. */
21881
21882 list_to_add
21883 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21884 && cu->language == language_cplus
21885 ? cu->get_builder ()->get_global_symbols ()
21886 : cu->list_in_scope);
21887 }
21888 break;
21889 case DW_TAG_imported_declaration:
21890 case DW_TAG_namespace:
21891 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21892 list_to_add = cu->get_builder ()->get_global_symbols ();
21893 break;
21894 case DW_TAG_module:
21895 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21896 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21897 list_to_add = cu->get_builder ()->get_global_symbols ();
21898 break;
21899 case DW_TAG_common_block:
21900 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21901 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21902 add_symbol_to_list (sym, cu->list_in_scope);
21903 break;
21904 default:
21905 /* Not a tag we recognize. Hopefully we aren't processing
21906 trash data, but since we must specifically ignore things
21907 we don't recognize, there is nothing else we should do at
21908 this point. */
21909 complaint (_("unsupported tag: '%s'"),
21910 dwarf_tag_name (die->tag));
21911 break;
21912 }
21913
21914 if (suppress_add)
21915 {
21916 sym->hash_next = objfile->template_symbols;
21917 objfile->template_symbols = sym;
21918 list_to_add = NULL;
21919 }
21920
21921 if (list_to_add != NULL)
21922 add_symbol_to_list (sym, list_to_add);
21923
21924 /* For the benefit of old versions of GCC, check for anonymous
21925 namespaces based on the demangled name. */
21926 if (!cu->processing_has_namespace_info
21927 && cu->language == language_cplus)
21928 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21929 }
21930 return (sym);
21931 }
21932
21933 /* Given an attr with a DW_FORM_dataN value in host byte order,
21934 zero-extend it as appropriate for the symbol's type. The DWARF
21935 standard (v4) is not entirely clear about the meaning of using
21936 DW_FORM_dataN for a constant with a signed type, where the type is
21937 wider than the data. The conclusion of a discussion on the DWARF
21938 list was that this is unspecified. We choose to always zero-extend
21939 because that is the interpretation long in use by GCC. */
21940
21941 static gdb_byte *
21942 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21943 struct dwarf2_cu *cu, LONGEST *value, int bits)
21944 {
21945 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21946 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21947 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21948 LONGEST l = DW_UNSND (attr);
21949
21950 if (bits < sizeof (*value) * 8)
21951 {
21952 l &= ((LONGEST) 1 << bits) - 1;
21953 *value = l;
21954 }
21955 else if (bits == sizeof (*value) * 8)
21956 *value = l;
21957 else
21958 {
21959 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21960 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21961 return bytes;
21962 }
21963
21964 return NULL;
21965 }
21966
21967 /* Read a constant value from an attribute. Either set *VALUE, or if
21968 the value does not fit in *VALUE, set *BYTES - either already
21969 allocated on the objfile obstack, or newly allocated on OBSTACK,
21970 or, set *BATON, if we translated the constant to a location
21971 expression. */
21972
21973 static void
21974 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21975 const char *name, struct obstack *obstack,
21976 struct dwarf2_cu *cu,
21977 LONGEST *value, const gdb_byte **bytes,
21978 struct dwarf2_locexpr_baton **baton)
21979 {
21980 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21981 struct comp_unit_head *cu_header = &cu->header;
21982 struct dwarf_block *blk;
21983 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21984 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21985
21986 *value = 0;
21987 *bytes = NULL;
21988 *baton = NULL;
21989
21990 switch (attr->form)
21991 {
21992 case DW_FORM_addr:
21993 case DW_FORM_addrx:
21994 case DW_FORM_GNU_addr_index:
21995 {
21996 gdb_byte *data;
21997
21998 if (TYPE_LENGTH (type) != cu_header->addr_size)
21999 dwarf2_const_value_length_mismatch_complaint (name,
22000 cu_header->addr_size,
22001 TYPE_LENGTH (type));
22002 /* Symbols of this form are reasonably rare, so we just
22003 piggyback on the existing location code rather than writing
22004 a new implementation of symbol_computed_ops. */
22005 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22006 (*baton)->per_cu = cu->per_cu;
22007 gdb_assert ((*baton)->per_cu);
22008
22009 (*baton)->size = 2 + cu_header->addr_size;
22010 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22011 (*baton)->data = data;
22012
22013 data[0] = DW_OP_addr;
22014 store_unsigned_integer (&data[1], cu_header->addr_size,
22015 byte_order, DW_ADDR (attr));
22016 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22017 }
22018 break;
22019 case DW_FORM_string:
22020 case DW_FORM_strp:
22021 case DW_FORM_strx:
22022 case DW_FORM_GNU_str_index:
22023 case DW_FORM_GNU_strp_alt:
22024 /* DW_STRING is already allocated on the objfile obstack, point
22025 directly to it. */
22026 *bytes = (const gdb_byte *) DW_STRING (attr);
22027 break;
22028 case DW_FORM_block1:
22029 case DW_FORM_block2:
22030 case DW_FORM_block4:
22031 case DW_FORM_block:
22032 case DW_FORM_exprloc:
22033 case DW_FORM_data16:
22034 blk = DW_BLOCK (attr);
22035 if (TYPE_LENGTH (type) != blk->size)
22036 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22037 TYPE_LENGTH (type));
22038 *bytes = blk->data;
22039 break;
22040
22041 /* The DW_AT_const_value attributes are supposed to carry the
22042 symbol's value "represented as it would be on the target
22043 architecture." By the time we get here, it's already been
22044 converted to host endianness, so we just need to sign- or
22045 zero-extend it as appropriate. */
22046 case DW_FORM_data1:
22047 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22048 break;
22049 case DW_FORM_data2:
22050 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22051 break;
22052 case DW_FORM_data4:
22053 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22054 break;
22055 case DW_FORM_data8:
22056 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22057 break;
22058
22059 case DW_FORM_sdata:
22060 case DW_FORM_implicit_const:
22061 *value = DW_SND (attr);
22062 break;
22063
22064 case DW_FORM_udata:
22065 *value = DW_UNSND (attr);
22066 break;
22067
22068 default:
22069 complaint (_("unsupported const value attribute form: '%s'"),
22070 dwarf_form_name (attr->form));
22071 *value = 0;
22072 break;
22073 }
22074 }
22075
22076
22077 /* Copy constant value from an attribute to a symbol. */
22078
22079 static void
22080 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22081 struct dwarf2_cu *cu)
22082 {
22083 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22084 LONGEST value;
22085 const gdb_byte *bytes;
22086 struct dwarf2_locexpr_baton *baton;
22087
22088 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22089 SYMBOL_PRINT_NAME (sym),
22090 &objfile->objfile_obstack, cu,
22091 &value, &bytes, &baton);
22092
22093 if (baton != NULL)
22094 {
22095 SYMBOL_LOCATION_BATON (sym) = baton;
22096 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22097 }
22098 else if (bytes != NULL)
22099 {
22100 SYMBOL_VALUE_BYTES (sym) = bytes;
22101 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22102 }
22103 else
22104 {
22105 SYMBOL_VALUE (sym) = value;
22106 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22107 }
22108 }
22109
22110 /* Return the type of the die in question using its DW_AT_type attribute. */
22111
22112 static struct type *
22113 die_type (struct die_info *die, struct dwarf2_cu *cu)
22114 {
22115 struct attribute *type_attr;
22116
22117 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22118 if (!type_attr)
22119 {
22120 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22121 /* A missing DW_AT_type represents a void type. */
22122 return objfile_type (objfile)->builtin_void;
22123 }
22124
22125 return lookup_die_type (die, type_attr, cu);
22126 }
22127
22128 /* True iff CU's producer generates GNAT Ada auxiliary information
22129 that allows to find parallel types through that information instead
22130 of having to do expensive parallel lookups by type name. */
22131
22132 static int
22133 need_gnat_info (struct dwarf2_cu *cu)
22134 {
22135 /* Assume that the Ada compiler was GNAT, which always produces
22136 the auxiliary information. */
22137 return (cu->language == language_ada);
22138 }
22139
22140 /* Return the auxiliary type of the die in question using its
22141 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22142 attribute is not present. */
22143
22144 static struct type *
22145 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22146 {
22147 struct attribute *type_attr;
22148
22149 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22150 if (!type_attr)
22151 return NULL;
22152
22153 return lookup_die_type (die, type_attr, cu);
22154 }
22155
22156 /* If DIE has a descriptive_type attribute, then set the TYPE's
22157 descriptive type accordingly. */
22158
22159 static void
22160 set_descriptive_type (struct type *type, struct die_info *die,
22161 struct dwarf2_cu *cu)
22162 {
22163 struct type *descriptive_type = die_descriptive_type (die, cu);
22164
22165 if (descriptive_type)
22166 {
22167 ALLOCATE_GNAT_AUX_TYPE (type);
22168 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22169 }
22170 }
22171
22172 /* Return the containing type of the die in question using its
22173 DW_AT_containing_type attribute. */
22174
22175 static struct type *
22176 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22177 {
22178 struct attribute *type_attr;
22179 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22180
22181 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22182 if (!type_attr)
22183 error (_("Dwarf Error: Problem turning containing type into gdb type "
22184 "[in module %s]"), objfile_name (objfile));
22185
22186 return lookup_die_type (die, type_attr, cu);
22187 }
22188
22189 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22190
22191 static struct type *
22192 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22193 {
22194 struct dwarf2_per_objfile *dwarf2_per_objfile
22195 = cu->per_cu->dwarf2_per_objfile;
22196 struct objfile *objfile = dwarf2_per_objfile->objfile;
22197 char *saved;
22198
22199 std::string message
22200 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22201 objfile_name (objfile),
22202 sect_offset_str (cu->header.sect_off),
22203 sect_offset_str (die->sect_off));
22204 saved = obstack_strdup (&objfile->objfile_obstack, message);
22205
22206 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22207 }
22208
22209 /* Look up the type of DIE in CU using its type attribute ATTR.
22210 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22211 DW_AT_containing_type.
22212 If there is no type substitute an error marker. */
22213
22214 static struct type *
22215 lookup_die_type (struct die_info *die, const struct attribute *attr,
22216 struct dwarf2_cu *cu)
22217 {
22218 struct dwarf2_per_objfile *dwarf2_per_objfile
22219 = cu->per_cu->dwarf2_per_objfile;
22220 struct objfile *objfile = dwarf2_per_objfile->objfile;
22221 struct type *this_type;
22222
22223 gdb_assert (attr->name == DW_AT_type
22224 || attr->name == DW_AT_GNAT_descriptive_type
22225 || attr->name == DW_AT_containing_type);
22226
22227 /* First see if we have it cached. */
22228
22229 if (attr->form == DW_FORM_GNU_ref_alt)
22230 {
22231 struct dwarf2_per_cu_data *per_cu;
22232 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22233
22234 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22235 dwarf2_per_objfile);
22236 this_type = get_die_type_at_offset (sect_off, per_cu);
22237 }
22238 else if (attr_form_is_ref (attr))
22239 {
22240 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22241
22242 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
22243 }
22244 else if (attr->form == DW_FORM_ref_sig8)
22245 {
22246 ULONGEST signature = DW_SIGNATURE (attr);
22247
22248 return get_signatured_type (die, signature, cu);
22249 }
22250 else
22251 {
22252 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22253 " at %s [in module %s]"),
22254 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22255 objfile_name (objfile));
22256 return build_error_marker_type (cu, die);
22257 }
22258
22259 /* If not cached we need to read it in. */
22260
22261 if (this_type == NULL)
22262 {
22263 struct die_info *type_die = NULL;
22264 struct dwarf2_cu *type_cu = cu;
22265
22266 if (attr_form_is_ref (attr))
22267 type_die = follow_die_ref (die, attr, &type_cu);
22268 if (type_die == NULL)
22269 return build_error_marker_type (cu, die);
22270 /* If we find the type now, it's probably because the type came
22271 from an inter-CU reference and the type's CU got expanded before
22272 ours. */
22273 this_type = read_type_die (type_die, type_cu);
22274 }
22275
22276 /* If we still don't have a type use an error marker. */
22277
22278 if (this_type == NULL)
22279 return build_error_marker_type (cu, die);
22280
22281 return this_type;
22282 }
22283
22284 /* Return the type in DIE, CU.
22285 Returns NULL for invalid types.
22286
22287 This first does a lookup in die_type_hash,
22288 and only reads the die in if necessary.
22289
22290 NOTE: This can be called when reading in partial or full symbols. */
22291
22292 static struct type *
22293 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22294 {
22295 struct type *this_type;
22296
22297 this_type = get_die_type (die, cu);
22298 if (this_type)
22299 return this_type;
22300
22301 return read_type_die_1 (die, cu);
22302 }
22303
22304 /* Read the type in DIE, CU.
22305 Returns NULL for invalid types. */
22306
22307 static struct type *
22308 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22309 {
22310 struct type *this_type = NULL;
22311
22312 switch (die->tag)
22313 {
22314 case DW_TAG_class_type:
22315 case DW_TAG_interface_type:
22316 case DW_TAG_structure_type:
22317 case DW_TAG_union_type:
22318 this_type = read_structure_type (die, cu);
22319 break;
22320 case DW_TAG_enumeration_type:
22321 this_type = read_enumeration_type (die, cu);
22322 break;
22323 case DW_TAG_subprogram:
22324 case DW_TAG_subroutine_type:
22325 case DW_TAG_inlined_subroutine:
22326 this_type = read_subroutine_type (die, cu);
22327 break;
22328 case DW_TAG_array_type:
22329 this_type = read_array_type (die, cu);
22330 break;
22331 case DW_TAG_set_type:
22332 this_type = read_set_type (die, cu);
22333 break;
22334 case DW_TAG_pointer_type:
22335 this_type = read_tag_pointer_type (die, cu);
22336 break;
22337 case DW_TAG_ptr_to_member_type:
22338 this_type = read_tag_ptr_to_member_type (die, cu);
22339 break;
22340 case DW_TAG_reference_type:
22341 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22342 break;
22343 case DW_TAG_rvalue_reference_type:
22344 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22345 break;
22346 case DW_TAG_const_type:
22347 this_type = read_tag_const_type (die, cu);
22348 break;
22349 case DW_TAG_volatile_type:
22350 this_type = read_tag_volatile_type (die, cu);
22351 break;
22352 case DW_TAG_restrict_type:
22353 this_type = read_tag_restrict_type (die, cu);
22354 break;
22355 case DW_TAG_string_type:
22356 this_type = read_tag_string_type (die, cu);
22357 break;
22358 case DW_TAG_typedef:
22359 this_type = read_typedef (die, cu);
22360 break;
22361 case DW_TAG_subrange_type:
22362 this_type = read_subrange_type (die, cu);
22363 break;
22364 case DW_TAG_base_type:
22365 this_type = read_base_type (die, cu);
22366 break;
22367 case DW_TAG_unspecified_type:
22368 this_type = read_unspecified_type (die, cu);
22369 break;
22370 case DW_TAG_namespace:
22371 this_type = read_namespace_type (die, cu);
22372 break;
22373 case DW_TAG_module:
22374 this_type = read_module_type (die, cu);
22375 break;
22376 case DW_TAG_atomic_type:
22377 this_type = read_tag_atomic_type (die, cu);
22378 break;
22379 default:
22380 complaint (_("unexpected tag in read_type_die: '%s'"),
22381 dwarf_tag_name (die->tag));
22382 break;
22383 }
22384
22385 return this_type;
22386 }
22387
22388 /* See if we can figure out if the class lives in a namespace. We do
22389 this by looking for a member function; its demangled name will
22390 contain namespace info, if there is any.
22391 Return the computed name or NULL.
22392 Space for the result is allocated on the objfile's obstack.
22393 This is the full-die version of guess_partial_die_structure_name.
22394 In this case we know DIE has no useful parent. */
22395
22396 static char *
22397 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22398 {
22399 struct die_info *spec_die;
22400 struct dwarf2_cu *spec_cu;
22401 struct die_info *child;
22402 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22403
22404 spec_cu = cu;
22405 spec_die = die_specification (die, &spec_cu);
22406 if (spec_die != NULL)
22407 {
22408 die = spec_die;
22409 cu = spec_cu;
22410 }
22411
22412 for (child = die->child;
22413 child != NULL;
22414 child = child->sibling)
22415 {
22416 if (child->tag == DW_TAG_subprogram)
22417 {
22418 const char *linkage_name = dw2_linkage_name (child, cu);
22419
22420 if (linkage_name != NULL)
22421 {
22422 char *actual_name
22423 = language_class_name_from_physname (cu->language_defn,
22424 linkage_name);
22425 char *name = NULL;
22426
22427 if (actual_name != NULL)
22428 {
22429 const char *die_name = dwarf2_name (die, cu);
22430
22431 if (die_name != NULL
22432 && strcmp (die_name, actual_name) != 0)
22433 {
22434 /* Strip off the class name from the full name.
22435 We want the prefix. */
22436 int die_name_len = strlen (die_name);
22437 int actual_name_len = strlen (actual_name);
22438
22439 /* Test for '::' as a sanity check. */
22440 if (actual_name_len > die_name_len + 2
22441 && actual_name[actual_name_len
22442 - die_name_len - 1] == ':')
22443 name = obstack_strndup (
22444 &objfile->per_bfd->storage_obstack,
22445 actual_name, actual_name_len - die_name_len - 2);
22446 }
22447 }
22448 xfree (actual_name);
22449 return name;
22450 }
22451 }
22452 }
22453
22454 return NULL;
22455 }
22456
22457 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22458 prefix part in such case. See
22459 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22460
22461 static const char *
22462 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22463 {
22464 struct attribute *attr;
22465 const char *base;
22466
22467 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22468 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22469 return NULL;
22470
22471 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22472 return NULL;
22473
22474 attr = dw2_linkage_name_attr (die, cu);
22475 if (attr == NULL || DW_STRING (attr) == NULL)
22476 return NULL;
22477
22478 /* dwarf2_name had to be already called. */
22479 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22480
22481 /* Strip the base name, keep any leading namespaces/classes. */
22482 base = strrchr (DW_STRING (attr), ':');
22483 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22484 return "";
22485
22486 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22487 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22488 DW_STRING (attr),
22489 &base[-1] - DW_STRING (attr));
22490 }
22491
22492 /* Return the name of the namespace/class that DIE is defined within,
22493 or "" if we can't tell. The caller should not xfree the result.
22494
22495 For example, if we're within the method foo() in the following
22496 code:
22497
22498 namespace N {
22499 class C {
22500 void foo () {
22501 }
22502 };
22503 }
22504
22505 then determine_prefix on foo's die will return "N::C". */
22506
22507 static const char *
22508 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22509 {
22510 struct dwarf2_per_objfile *dwarf2_per_objfile
22511 = cu->per_cu->dwarf2_per_objfile;
22512 struct die_info *parent, *spec_die;
22513 struct dwarf2_cu *spec_cu;
22514 struct type *parent_type;
22515 const char *retval;
22516
22517 if (cu->language != language_cplus
22518 && cu->language != language_fortran && cu->language != language_d
22519 && cu->language != language_rust)
22520 return "";
22521
22522 retval = anonymous_struct_prefix (die, cu);
22523 if (retval)
22524 return retval;
22525
22526 /* We have to be careful in the presence of DW_AT_specification.
22527 For example, with GCC 3.4, given the code
22528
22529 namespace N {
22530 void foo() {
22531 // Definition of N::foo.
22532 }
22533 }
22534
22535 then we'll have a tree of DIEs like this:
22536
22537 1: DW_TAG_compile_unit
22538 2: DW_TAG_namespace // N
22539 3: DW_TAG_subprogram // declaration of N::foo
22540 4: DW_TAG_subprogram // definition of N::foo
22541 DW_AT_specification // refers to die #3
22542
22543 Thus, when processing die #4, we have to pretend that we're in
22544 the context of its DW_AT_specification, namely the contex of die
22545 #3. */
22546 spec_cu = cu;
22547 spec_die = die_specification (die, &spec_cu);
22548 if (spec_die == NULL)
22549 parent = die->parent;
22550 else
22551 {
22552 parent = spec_die->parent;
22553 cu = spec_cu;
22554 }
22555
22556 if (parent == NULL)
22557 return "";
22558 else if (parent->building_fullname)
22559 {
22560 const char *name;
22561 const char *parent_name;
22562
22563 /* It has been seen on RealView 2.2 built binaries,
22564 DW_TAG_template_type_param types actually _defined_ as
22565 children of the parent class:
22566
22567 enum E {};
22568 template class <class Enum> Class{};
22569 Class<enum E> class_e;
22570
22571 1: DW_TAG_class_type (Class)
22572 2: DW_TAG_enumeration_type (E)
22573 3: DW_TAG_enumerator (enum1:0)
22574 3: DW_TAG_enumerator (enum2:1)
22575 ...
22576 2: DW_TAG_template_type_param
22577 DW_AT_type DW_FORM_ref_udata (E)
22578
22579 Besides being broken debug info, it can put GDB into an
22580 infinite loop. Consider:
22581
22582 When we're building the full name for Class<E>, we'll start
22583 at Class, and go look over its template type parameters,
22584 finding E. We'll then try to build the full name of E, and
22585 reach here. We're now trying to build the full name of E,
22586 and look over the parent DIE for containing scope. In the
22587 broken case, if we followed the parent DIE of E, we'd again
22588 find Class, and once again go look at its template type
22589 arguments, etc., etc. Simply don't consider such parent die
22590 as source-level parent of this die (it can't be, the language
22591 doesn't allow it), and break the loop here. */
22592 name = dwarf2_name (die, cu);
22593 parent_name = dwarf2_name (parent, cu);
22594 complaint (_("template param type '%s' defined within parent '%s'"),
22595 name ? name : "<unknown>",
22596 parent_name ? parent_name : "<unknown>");
22597 return "";
22598 }
22599 else
22600 switch (parent->tag)
22601 {
22602 case DW_TAG_namespace:
22603 parent_type = read_type_die (parent, cu);
22604 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22605 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22606 Work around this problem here. */
22607 if (cu->language == language_cplus
22608 && strcmp (TYPE_NAME (parent_type), "::") == 0)
22609 return "";
22610 /* We give a name to even anonymous namespaces. */
22611 return TYPE_NAME (parent_type);
22612 case DW_TAG_class_type:
22613 case DW_TAG_interface_type:
22614 case DW_TAG_structure_type:
22615 case DW_TAG_union_type:
22616 case DW_TAG_module:
22617 parent_type = read_type_die (parent, cu);
22618 if (TYPE_NAME (parent_type) != NULL)
22619 return TYPE_NAME (parent_type);
22620 else
22621 /* An anonymous structure is only allowed non-static data
22622 members; no typedefs, no member functions, et cetera.
22623 So it does not need a prefix. */
22624 return "";
22625 case DW_TAG_compile_unit:
22626 case DW_TAG_partial_unit:
22627 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22628 if (cu->language == language_cplus
22629 && !dwarf2_per_objfile->types.empty ()
22630 && die->child != NULL
22631 && (die->tag == DW_TAG_class_type
22632 || die->tag == DW_TAG_structure_type
22633 || die->tag == DW_TAG_union_type))
22634 {
22635 char *name = guess_full_die_structure_name (die, cu);
22636 if (name != NULL)
22637 return name;
22638 }
22639 return "";
22640 case DW_TAG_subprogram:
22641 /* Nested subroutines in Fortran get a prefix with the name
22642 of the parent's subroutine. */
22643 if (cu->language == language_fortran)
22644 {
22645 if ((die->tag == DW_TAG_subprogram)
22646 && (dwarf2_name (parent, cu) != NULL))
22647 return dwarf2_name (parent, cu);
22648 }
22649 return determine_prefix (parent, cu);
22650 case DW_TAG_enumeration_type:
22651 parent_type = read_type_die (parent, cu);
22652 if (TYPE_DECLARED_CLASS (parent_type))
22653 {
22654 if (TYPE_NAME (parent_type) != NULL)
22655 return TYPE_NAME (parent_type);
22656 return "";
22657 }
22658 /* Fall through. */
22659 default:
22660 return determine_prefix (parent, cu);
22661 }
22662 }
22663
22664 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22665 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22666 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22667 an obconcat, otherwise allocate storage for the result. The CU argument is
22668 used to determine the language and hence, the appropriate separator. */
22669
22670 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22671
22672 static char *
22673 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22674 int physname, struct dwarf2_cu *cu)
22675 {
22676 const char *lead = "";
22677 const char *sep;
22678
22679 if (suffix == NULL || suffix[0] == '\0'
22680 || prefix == NULL || prefix[0] == '\0')
22681 sep = "";
22682 else if (cu->language == language_d)
22683 {
22684 /* For D, the 'main' function could be defined in any module, but it
22685 should never be prefixed. */
22686 if (strcmp (suffix, "D main") == 0)
22687 {
22688 prefix = "";
22689 sep = "";
22690 }
22691 else
22692 sep = ".";
22693 }
22694 else if (cu->language == language_fortran && physname)
22695 {
22696 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22697 DW_AT_MIPS_linkage_name is preferred and used instead. */
22698
22699 lead = "__";
22700 sep = "_MOD_";
22701 }
22702 else
22703 sep = "::";
22704
22705 if (prefix == NULL)
22706 prefix = "";
22707 if (suffix == NULL)
22708 suffix = "";
22709
22710 if (obs == NULL)
22711 {
22712 char *retval
22713 = ((char *)
22714 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22715
22716 strcpy (retval, lead);
22717 strcat (retval, prefix);
22718 strcat (retval, sep);
22719 strcat (retval, suffix);
22720 return retval;
22721 }
22722 else
22723 {
22724 /* We have an obstack. */
22725 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22726 }
22727 }
22728
22729 /* Return sibling of die, NULL if no sibling. */
22730
22731 static struct die_info *
22732 sibling_die (struct die_info *die)
22733 {
22734 return die->sibling;
22735 }
22736
22737 /* Get name of a die, return NULL if not found. */
22738
22739 static const char *
22740 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22741 struct obstack *obstack)
22742 {
22743 if (name && cu->language == language_cplus)
22744 {
22745 std::string canon_name = cp_canonicalize_string (name);
22746
22747 if (!canon_name.empty ())
22748 {
22749 if (canon_name != name)
22750 name = obstack_strdup (obstack, canon_name);
22751 }
22752 }
22753
22754 return name;
22755 }
22756
22757 /* Get name of a die, return NULL if not found.
22758 Anonymous namespaces are converted to their magic string. */
22759
22760 static const char *
22761 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22762 {
22763 struct attribute *attr;
22764 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22765
22766 attr = dwarf2_attr (die, DW_AT_name, cu);
22767 if ((!attr || !DW_STRING (attr))
22768 && die->tag != DW_TAG_namespace
22769 && die->tag != DW_TAG_class_type
22770 && die->tag != DW_TAG_interface_type
22771 && die->tag != DW_TAG_structure_type
22772 && die->tag != DW_TAG_union_type)
22773 return NULL;
22774
22775 switch (die->tag)
22776 {
22777 case DW_TAG_compile_unit:
22778 case DW_TAG_partial_unit:
22779 /* Compilation units have a DW_AT_name that is a filename, not
22780 a source language identifier. */
22781 case DW_TAG_enumeration_type:
22782 case DW_TAG_enumerator:
22783 /* These tags always have simple identifiers already; no need
22784 to canonicalize them. */
22785 return DW_STRING (attr);
22786
22787 case DW_TAG_namespace:
22788 if (attr != NULL && DW_STRING (attr) != NULL)
22789 return DW_STRING (attr);
22790 return CP_ANONYMOUS_NAMESPACE_STR;
22791
22792 case DW_TAG_class_type:
22793 case DW_TAG_interface_type:
22794 case DW_TAG_structure_type:
22795 case DW_TAG_union_type:
22796 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22797 structures or unions. These were of the form "._%d" in GCC 4.1,
22798 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22799 and GCC 4.4. We work around this problem by ignoring these. */
22800 if (attr && DW_STRING (attr)
22801 && (startswith (DW_STRING (attr), "._")
22802 || startswith (DW_STRING (attr), "<anonymous")))
22803 return NULL;
22804
22805 /* GCC might emit a nameless typedef that has a linkage name. See
22806 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22807 if (!attr || DW_STRING (attr) == NULL)
22808 {
22809 char *demangled = NULL;
22810
22811 attr = dw2_linkage_name_attr (die, cu);
22812 if (attr == NULL || DW_STRING (attr) == NULL)
22813 return NULL;
22814
22815 /* Avoid demangling DW_STRING (attr) the second time on a second
22816 call for the same DIE. */
22817 if (!DW_STRING_IS_CANONICAL (attr))
22818 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22819
22820 if (demangled)
22821 {
22822 const char *base;
22823
22824 /* FIXME: we already did this for the partial symbol... */
22825 DW_STRING (attr)
22826 = obstack_strdup (&objfile->per_bfd->storage_obstack,
22827 demangled);
22828 DW_STRING_IS_CANONICAL (attr) = 1;
22829 xfree (demangled);
22830
22831 /* Strip any leading namespaces/classes, keep only the base name.
22832 DW_AT_name for named DIEs does not contain the prefixes. */
22833 base = strrchr (DW_STRING (attr), ':');
22834 if (base && base > DW_STRING (attr) && base[-1] == ':')
22835 return &base[1];
22836 else
22837 return DW_STRING (attr);
22838 }
22839 }
22840 break;
22841
22842 default:
22843 break;
22844 }
22845
22846 if (!DW_STRING_IS_CANONICAL (attr))
22847 {
22848 DW_STRING (attr)
22849 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22850 &objfile->per_bfd->storage_obstack);
22851 DW_STRING_IS_CANONICAL (attr) = 1;
22852 }
22853 return DW_STRING (attr);
22854 }
22855
22856 /* Return the die that this die in an extension of, or NULL if there
22857 is none. *EXT_CU is the CU containing DIE on input, and the CU
22858 containing the return value on output. */
22859
22860 static struct die_info *
22861 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22862 {
22863 struct attribute *attr;
22864
22865 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22866 if (attr == NULL)
22867 return NULL;
22868
22869 return follow_die_ref (die, attr, ext_cu);
22870 }
22871
22872 /* A convenience function that returns an "unknown" DWARF name,
22873 including the value of V. STR is the name of the entity being
22874 printed, e.g., "TAG". */
22875
22876 static const char *
22877 dwarf_unknown (const char *str, unsigned v)
22878 {
22879 char *cell = get_print_cell ();
22880 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
22881 return cell;
22882 }
22883
22884 /* Convert a DIE tag into its string name. */
22885
22886 static const char *
22887 dwarf_tag_name (unsigned tag)
22888 {
22889 const char *name = get_DW_TAG_name (tag);
22890
22891 if (name == NULL)
22892 return dwarf_unknown ("TAG", tag);
22893
22894 return name;
22895 }
22896
22897 /* Convert a DWARF attribute code into its string name. */
22898
22899 static const char *
22900 dwarf_attr_name (unsigned attr)
22901 {
22902 const char *name;
22903
22904 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22905 if (attr == DW_AT_MIPS_fde)
22906 return "DW_AT_MIPS_fde";
22907 #else
22908 if (attr == DW_AT_HP_block_index)
22909 return "DW_AT_HP_block_index";
22910 #endif
22911
22912 name = get_DW_AT_name (attr);
22913
22914 if (name == NULL)
22915 return dwarf_unknown ("AT", attr);
22916
22917 return name;
22918 }
22919
22920 /* Convert a unit type to corresponding DW_UT name. */
22921
22922 static const char *
22923 dwarf_unit_type_name (int unit_type) {
22924 switch (unit_type)
22925 {
22926 case 0x01:
22927 return "DW_UT_compile (0x01)";
22928 case 0x02:
22929 return "DW_UT_type (0x02)";
22930 case 0x03:
22931 return "DW_UT_partial (0x03)";
22932 case 0x04:
22933 return "DW_UT_skeleton (0x04)";
22934 case 0x05:
22935 return "DW_UT_split_compile (0x05)";
22936 case 0x06:
22937 return "DW_UT_split_type (0x06)";
22938 case 0x80:
22939 return "DW_UT_lo_user (0x80)";
22940 case 0xff:
22941 return "DW_UT_hi_user (0xff)";
22942 default:
22943 return nullptr;
22944 }
22945 }
22946
22947 /* Convert a DWARF value form code into its string name. */
22948
22949 static const char *
22950 dwarf_form_name (unsigned form)
22951 {
22952 const char *name = get_DW_FORM_name (form);
22953
22954 if (name == NULL)
22955 return dwarf_unknown ("FORM", form);
22956
22957 return name;
22958 }
22959
22960 static const char *
22961 dwarf_bool_name (unsigned mybool)
22962 {
22963 if (mybool)
22964 return "TRUE";
22965 else
22966 return "FALSE";
22967 }
22968
22969 /* Convert a DWARF type code into its string name. */
22970
22971 static const char *
22972 dwarf_type_encoding_name (unsigned enc)
22973 {
22974 const char *name = get_DW_ATE_name (enc);
22975
22976 if (name == NULL)
22977 return dwarf_unknown ("ATE", enc);
22978
22979 return name;
22980 }
22981
22982 static void
22983 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22984 {
22985 unsigned int i;
22986
22987 print_spaces (indent, f);
22988 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22989 dwarf_tag_name (die->tag), die->abbrev,
22990 sect_offset_str (die->sect_off));
22991
22992 if (die->parent != NULL)
22993 {
22994 print_spaces (indent, f);
22995 fprintf_unfiltered (f, " parent at offset: %s\n",
22996 sect_offset_str (die->parent->sect_off));
22997 }
22998
22999 print_spaces (indent, f);
23000 fprintf_unfiltered (f, " has children: %s\n",
23001 dwarf_bool_name (die->child != NULL));
23002
23003 print_spaces (indent, f);
23004 fprintf_unfiltered (f, " attributes:\n");
23005
23006 for (i = 0; i < die->num_attrs; ++i)
23007 {
23008 print_spaces (indent, f);
23009 fprintf_unfiltered (f, " %s (%s) ",
23010 dwarf_attr_name (die->attrs[i].name),
23011 dwarf_form_name (die->attrs[i].form));
23012
23013 switch (die->attrs[i].form)
23014 {
23015 case DW_FORM_addr:
23016 case DW_FORM_addrx:
23017 case DW_FORM_GNU_addr_index:
23018 fprintf_unfiltered (f, "address: ");
23019 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
23020 break;
23021 case DW_FORM_block2:
23022 case DW_FORM_block4:
23023 case DW_FORM_block:
23024 case DW_FORM_block1:
23025 fprintf_unfiltered (f, "block: size %s",
23026 pulongest (DW_BLOCK (&die->attrs[i])->size));
23027 break;
23028 case DW_FORM_exprloc:
23029 fprintf_unfiltered (f, "expression: size %s",
23030 pulongest (DW_BLOCK (&die->attrs[i])->size));
23031 break;
23032 case DW_FORM_data16:
23033 fprintf_unfiltered (f, "constant of 16 bytes");
23034 break;
23035 case DW_FORM_ref_addr:
23036 fprintf_unfiltered (f, "ref address: ");
23037 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23038 break;
23039 case DW_FORM_GNU_ref_alt:
23040 fprintf_unfiltered (f, "alt ref address: ");
23041 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23042 break;
23043 case DW_FORM_ref1:
23044 case DW_FORM_ref2:
23045 case DW_FORM_ref4:
23046 case DW_FORM_ref8:
23047 case DW_FORM_ref_udata:
23048 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23049 (long) (DW_UNSND (&die->attrs[i])));
23050 break;
23051 case DW_FORM_data1:
23052 case DW_FORM_data2:
23053 case DW_FORM_data4:
23054 case DW_FORM_data8:
23055 case DW_FORM_udata:
23056 case DW_FORM_sdata:
23057 fprintf_unfiltered (f, "constant: %s",
23058 pulongest (DW_UNSND (&die->attrs[i])));
23059 break;
23060 case DW_FORM_sec_offset:
23061 fprintf_unfiltered (f, "section offset: %s",
23062 pulongest (DW_UNSND (&die->attrs[i])));
23063 break;
23064 case DW_FORM_ref_sig8:
23065 fprintf_unfiltered (f, "signature: %s",
23066 hex_string (DW_SIGNATURE (&die->attrs[i])));
23067 break;
23068 case DW_FORM_string:
23069 case DW_FORM_strp:
23070 case DW_FORM_line_strp:
23071 case DW_FORM_strx:
23072 case DW_FORM_GNU_str_index:
23073 case DW_FORM_GNU_strp_alt:
23074 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23075 DW_STRING (&die->attrs[i])
23076 ? DW_STRING (&die->attrs[i]) : "",
23077 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
23078 break;
23079 case DW_FORM_flag:
23080 if (DW_UNSND (&die->attrs[i]))
23081 fprintf_unfiltered (f, "flag: TRUE");
23082 else
23083 fprintf_unfiltered (f, "flag: FALSE");
23084 break;
23085 case DW_FORM_flag_present:
23086 fprintf_unfiltered (f, "flag: TRUE");
23087 break;
23088 case DW_FORM_indirect:
23089 /* The reader will have reduced the indirect form to
23090 the "base form" so this form should not occur. */
23091 fprintf_unfiltered (f,
23092 "unexpected attribute form: DW_FORM_indirect");
23093 break;
23094 case DW_FORM_implicit_const:
23095 fprintf_unfiltered (f, "constant: %s",
23096 plongest (DW_SND (&die->attrs[i])));
23097 break;
23098 default:
23099 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23100 die->attrs[i].form);
23101 break;
23102 }
23103 fprintf_unfiltered (f, "\n");
23104 }
23105 }
23106
23107 static void
23108 dump_die_for_error (struct die_info *die)
23109 {
23110 dump_die_shallow (gdb_stderr, 0, die);
23111 }
23112
23113 static void
23114 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23115 {
23116 int indent = level * 4;
23117
23118 gdb_assert (die != NULL);
23119
23120 if (level >= max_level)
23121 return;
23122
23123 dump_die_shallow (f, indent, die);
23124
23125 if (die->child != NULL)
23126 {
23127 print_spaces (indent, f);
23128 fprintf_unfiltered (f, " Children:");
23129 if (level + 1 < max_level)
23130 {
23131 fprintf_unfiltered (f, "\n");
23132 dump_die_1 (f, level + 1, max_level, die->child);
23133 }
23134 else
23135 {
23136 fprintf_unfiltered (f,
23137 " [not printed, max nesting level reached]\n");
23138 }
23139 }
23140
23141 if (die->sibling != NULL && level > 0)
23142 {
23143 dump_die_1 (f, level, max_level, die->sibling);
23144 }
23145 }
23146
23147 /* This is called from the pdie macro in gdbinit.in.
23148 It's not static so gcc will keep a copy callable from gdb. */
23149
23150 void
23151 dump_die (struct die_info *die, int max_level)
23152 {
23153 dump_die_1 (gdb_stdlog, 0, max_level, die);
23154 }
23155
23156 static void
23157 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23158 {
23159 void **slot;
23160
23161 slot = htab_find_slot_with_hash (cu->die_hash, die,
23162 to_underlying (die->sect_off),
23163 INSERT);
23164
23165 *slot = die;
23166 }
23167
23168 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23169 required kind. */
23170
23171 static sect_offset
23172 dwarf2_get_ref_die_offset (const struct attribute *attr)
23173 {
23174 if (attr_form_is_ref (attr))
23175 return (sect_offset) DW_UNSND (attr);
23176
23177 complaint (_("unsupported die ref attribute form: '%s'"),
23178 dwarf_form_name (attr->form));
23179 return {};
23180 }
23181
23182 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23183 * the value held by the attribute is not constant. */
23184
23185 static LONGEST
23186 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
23187 {
23188 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
23189 return DW_SND (attr);
23190 else if (attr->form == DW_FORM_udata
23191 || attr->form == DW_FORM_data1
23192 || attr->form == DW_FORM_data2
23193 || attr->form == DW_FORM_data4
23194 || attr->form == DW_FORM_data8)
23195 return DW_UNSND (attr);
23196 else
23197 {
23198 /* For DW_FORM_data16 see attr_form_is_constant. */
23199 complaint (_("Attribute value is not a constant (%s)"),
23200 dwarf_form_name (attr->form));
23201 return default_value;
23202 }
23203 }
23204
23205 /* Follow reference or signature attribute ATTR of SRC_DIE.
23206 On entry *REF_CU is the CU of SRC_DIE.
23207 On exit *REF_CU is the CU of the result. */
23208
23209 static struct die_info *
23210 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23211 struct dwarf2_cu **ref_cu)
23212 {
23213 struct die_info *die;
23214
23215 if (attr_form_is_ref (attr))
23216 die = follow_die_ref (src_die, attr, ref_cu);
23217 else if (attr->form == DW_FORM_ref_sig8)
23218 die = follow_die_sig (src_die, attr, ref_cu);
23219 else
23220 {
23221 dump_die_for_error (src_die);
23222 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23223 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23224 }
23225
23226 return die;
23227 }
23228
23229 /* Follow reference OFFSET.
23230 On entry *REF_CU is the CU of the source die referencing OFFSET.
23231 On exit *REF_CU is the CU of the result.
23232 Returns NULL if OFFSET is invalid. */
23233
23234 static struct die_info *
23235 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23236 struct dwarf2_cu **ref_cu)
23237 {
23238 struct die_info temp_die;
23239 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23240 struct dwarf2_per_objfile *dwarf2_per_objfile
23241 = cu->per_cu->dwarf2_per_objfile;
23242
23243 gdb_assert (cu->per_cu != NULL);
23244
23245 target_cu = cu;
23246
23247 if (cu->per_cu->is_debug_types)
23248 {
23249 /* .debug_types CUs cannot reference anything outside their CU.
23250 If they need to, they have to reference a signatured type via
23251 DW_FORM_ref_sig8. */
23252 if (!offset_in_cu_p (&cu->header, sect_off))
23253 return NULL;
23254 }
23255 else if (offset_in_dwz != cu->per_cu->is_dwz
23256 || !offset_in_cu_p (&cu->header, sect_off))
23257 {
23258 struct dwarf2_per_cu_data *per_cu;
23259
23260 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23261 dwarf2_per_objfile);
23262
23263 /* If necessary, add it to the queue and load its DIEs. */
23264 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23265 load_full_comp_unit (per_cu, false, cu->language);
23266
23267 target_cu = per_cu->cu;
23268 }
23269 else if (cu->dies == NULL)
23270 {
23271 /* We're loading full DIEs during partial symbol reading. */
23272 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
23273 load_full_comp_unit (cu->per_cu, false, language_minimal);
23274 }
23275
23276 *ref_cu = target_cu;
23277 temp_die.sect_off = sect_off;
23278
23279 if (target_cu != cu)
23280 target_cu->ancestor = cu;
23281
23282 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23283 &temp_die,
23284 to_underlying (sect_off));
23285 }
23286
23287 /* Follow reference attribute ATTR of SRC_DIE.
23288 On entry *REF_CU is the CU of SRC_DIE.
23289 On exit *REF_CU is the CU of the result. */
23290
23291 static struct die_info *
23292 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23293 struct dwarf2_cu **ref_cu)
23294 {
23295 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
23296 struct dwarf2_cu *cu = *ref_cu;
23297 struct die_info *die;
23298
23299 die = follow_die_offset (sect_off,
23300 (attr->form == DW_FORM_GNU_ref_alt
23301 || cu->per_cu->is_dwz),
23302 ref_cu);
23303 if (!die)
23304 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23305 "at %s [in module %s]"),
23306 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23307 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
23308
23309 return die;
23310 }
23311
23312 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
23313 Returned value is intended for DW_OP_call*. Returned
23314 dwarf2_locexpr_baton->data has lifetime of
23315 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
23316
23317 struct dwarf2_locexpr_baton
23318 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23319 struct dwarf2_per_cu_data *per_cu,
23320 CORE_ADDR (*get_frame_pc) (void *baton),
23321 void *baton, bool resolve_abstract_p)
23322 {
23323 struct dwarf2_cu *cu;
23324 struct die_info *die;
23325 struct attribute *attr;
23326 struct dwarf2_locexpr_baton retval;
23327 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23328 struct objfile *objfile = dwarf2_per_objfile->objfile;
23329
23330 if (per_cu->cu == NULL)
23331 load_cu (per_cu, false);
23332 cu = per_cu->cu;
23333 if (cu == NULL)
23334 {
23335 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23336 Instead just throw an error, not much else we can do. */
23337 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23338 sect_offset_str (sect_off), objfile_name (objfile));
23339 }
23340
23341 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23342 if (!die)
23343 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23344 sect_offset_str (sect_off), objfile_name (objfile));
23345
23346 attr = dwarf2_attr (die, DW_AT_location, cu);
23347 if (!attr && resolve_abstract_p
23348 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
23349 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23350 {
23351 CORE_ADDR pc = (*get_frame_pc) (baton);
23352 CORE_ADDR baseaddr
23353 = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23354 struct gdbarch *gdbarch = get_objfile_arch (objfile);
23355
23356 for (const auto &cand_off
23357 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
23358 {
23359 struct dwarf2_cu *cand_cu = cu;
23360 struct die_info *cand
23361 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23362 if (!cand
23363 || !cand->parent
23364 || cand->parent->tag != DW_TAG_subprogram)
23365 continue;
23366
23367 CORE_ADDR pc_low, pc_high;
23368 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23369 if (pc_low == ((CORE_ADDR) -1))
23370 continue;
23371 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23372 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23373 if (!(pc_low <= pc && pc < pc_high))
23374 continue;
23375
23376 die = cand;
23377 attr = dwarf2_attr (die, DW_AT_location, cu);
23378 break;
23379 }
23380 }
23381
23382 if (!attr)
23383 {
23384 /* DWARF: "If there is no such attribute, then there is no effect.".
23385 DATA is ignored if SIZE is 0. */
23386
23387 retval.data = NULL;
23388 retval.size = 0;
23389 }
23390 else if (attr_form_is_section_offset (attr))
23391 {
23392 struct dwarf2_loclist_baton loclist_baton;
23393 CORE_ADDR pc = (*get_frame_pc) (baton);
23394 size_t size;
23395
23396 fill_in_loclist_baton (cu, &loclist_baton, attr);
23397
23398 retval.data = dwarf2_find_location_expression (&loclist_baton,
23399 &size, pc);
23400 retval.size = size;
23401 }
23402 else
23403 {
23404 if (!attr_form_is_block (attr))
23405 error (_("Dwarf Error: DIE at %s referenced in module %s "
23406 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23407 sect_offset_str (sect_off), objfile_name (objfile));
23408
23409 retval.data = DW_BLOCK (attr)->data;
23410 retval.size = DW_BLOCK (attr)->size;
23411 }
23412 retval.per_cu = cu->per_cu;
23413
23414 age_cached_comp_units (dwarf2_per_objfile);
23415
23416 return retval;
23417 }
23418
23419 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23420 offset. */
23421
23422 struct dwarf2_locexpr_baton
23423 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23424 struct dwarf2_per_cu_data *per_cu,
23425 CORE_ADDR (*get_frame_pc) (void *baton),
23426 void *baton)
23427 {
23428 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23429
23430 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23431 }
23432
23433 /* Write a constant of a given type as target-ordered bytes into
23434 OBSTACK. */
23435
23436 static const gdb_byte *
23437 write_constant_as_bytes (struct obstack *obstack,
23438 enum bfd_endian byte_order,
23439 struct type *type,
23440 ULONGEST value,
23441 LONGEST *len)
23442 {
23443 gdb_byte *result;
23444
23445 *len = TYPE_LENGTH (type);
23446 result = (gdb_byte *) obstack_alloc (obstack, *len);
23447 store_unsigned_integer (result, *len, byte_order, value);
23448
23449 return result;
23450 }
23451
23452 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23453 pointer to the constant bytes and set LEN to the length of the
23454 data. If memory is needed, allocate it on OBSTACK. If the DIE
23455 does not have a DW_AT_const_value, return NULL. */
23456
23457 const gdb_byte *
23458 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23459 struct dwarf2_per_cu_data *per_cu,
23460 struct obstack *obstack,
23461 LONGEST *len)
23462 {
23463 struct dwarf2_cu *cu;
23464 struct die_info *die;
23465 struct attribute *attr;
23466 const gdb_byte *result = NULL;
23467 struct type *type;
23468 LONGEST value;
23469 enum bfd_endian byte_order;
23470 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23471
23472 if (per_cu->cu == NULL)
23473 load_cu (per_cu, false);
23474 cu = per_cu->cu;
23475 if (cu == NULL)
23476 {
23477 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23478 Instead just throw an error, not much else we can do. */
23479 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23480 sect_offset_str (sect_off), objfile_name (objfile));
23481 }
23482
23483 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23484 if (!die)
23485 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23486 sect_offset_str (sect_off), objfile_name (objfile));
23487
23488 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23489 if (attr == NULL)
23490 return NULL;
23491
23492 byte_order = (bfd_big_endian (objfile->obfd)
23493 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23494
23495 switch (attr->form)
23496 {
23497 case DW_FORM_addr:
23498 case DW_FORM_addrx:
23499 case DW_FORM_GNU_addr_index:
23500 {
23501 gdb_byte *tem;
23502
23503 *len = cu->header.addr_size;
23504 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23505 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23506 result = tem;
23507 }
23508 break;
23509 case DW_FORM_string:
23510 case DW_FORM_strp:
23511 case DW_FORM_strx:
23512 case DW_FORM_GNU_str_index:
23513 case DW_FORM_GNU_strp_alt:
23514 /* DW_STRING is already allocated on the objfile obstack, point
23515 directly to it. */
23516 result = (const gdb_byte *) DW_STRING (attr);
23517 *len = strlen (DW_STRING (attr));
23518 break;
23519 case DW_FORM_block1:
23520 case DW_FORM_block2:
23521 case DW_FORM_block4:
23522 case DW_FORM_block:
23523 case DW_FORM_exprloc:
23524 case DW_FORM_data16:
23525 result = DW_BLOCK (attr)->data;
23526 *len = DW_BLOCK (attr)->size;
23527 break;
23528
23529 /* The DW_AT_const_value attributes are supposed to carry the
23530 symbol's value "represented as it would be on the target
23531 architecture." By the time we get here, it's already been
23532 converted to host endianness, so we just need to sign- or
23533 zero-extend it as appropriate. */
23534 case DW_FORM_data1:
23535 type = die_type (die, cu);
23536 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23537 if (result == NULL)
23538 result = write_constant_as_bytes (obstack, byte_order,
23539 type, value, len);
23540 break;
23541 case DW_FORM_data2:
23542 type = die_type (die, cu);
23543 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23544 if (result == NULL)
23545 result = write_constant_as_bytes (obstack, byte_order,
23546 type, value, len);
23547 break;
23548 case DW_FORM_data4:
23549 type = die_type (die, cu);
23550 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23551 if (result == NULL)
23552 result = write_constant_as_bytes (obstack, byte_order,
23553 type, value, len);
23554 break;
23555 case DW_FORM_data8:
23556 type = die_type (die, cu);
23557 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23558 if (result == NULL)
23559 result = write_constant_as_bytes (obstack, byte_order,
23560 type, value, len);
23561 break;
23562
23563 case DW_FORM_sdata:
23564 case DW_FORM_implicit_const:
23565 type = die_type (die, cu);
23566 result = write_constant_as_bytes (obstack, byte_order,
23567 type, DW_SND (attr), len);
23568 break;
23569
23570 case DW_FORM_udata:
23571 type = die_type (die, cu);
23572 result = write_constant_as_bytes (obstack, byte_order,
23573 type, DW_UNSND (attr), len);
23574 break;
23575
23576 default:
23577 complaint (_("unsupported const value attribute form: '%s'"),
23578 dwarf_form_name (attr->form));
23579 break;
23580 }
23581
23582 return result;
23583 }
23584
23585 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23586 valid type for this die is found. */
23587
23588 struct type *
23589 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23590 struct dwarf2_per_cu_data *per_cu)
23591 {
23592 struct dwarf2_cu *cu;
23593 struct die_info *die;
23594
23595 if (per_cu->cu == NULL)
23596 load_cu (per_cu, false);
23597 cu = per_cu->cu;
23598 if (!cu)
23599 return NULL;
23600
23601 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23602 if (!die)
23603 return NULL;
23604
23605 return die_type (die, cu);
23606 }
23607
23608 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23609 PER_CU. */
23610
23611 struct type *
23612 dwarf2_get_die_type (cu_offset die_offset,
23613 struct dwarf2_per_cu_data *per_cu)
23614 {
23615 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23616 return get_die_type_at_offset (die_offset_sect, per_cu);
23617 }
23618
23619 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23620 On entry *REF_CU is the CU of SRC_DIE.
23621 On exit *REF_CU is the CU of the result.
23622 Returns NULL if the referenced DIE isn't found. */
23623
23624 static struct die_info *
23625 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23626 struct dwarf2_cu **ref_cu)
23627 {
23628 struct die_info temp_die;
23629 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23630 struct die_info *die;
23631
23632 /* While it might be nice to assert sig_type->type == NULL here,
23633 we can get here for DW_AT_imported_declaration where we need
23634 the DIE not the type. */
23635
23636 /* If necessary, add it to the queue and load its DIEs. */
23637
23638 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23639 read_signatured_type (sig_type);
23640
23641 sig_cu = sig_type->per_cu.cu;
23642 gdb_assert (sig_cu != NULL);
23643 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23644 temp_die.sect_off = sig_type->type_offset_in_section;
23645 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23646 to_underlying (temp_die.sect_off));
23647 if (die)
23648 {
23649 struct dwarf2_per_objfile *dwarf2_per_objfile
23650 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23651
23652 /* For .gdb_index version 7 keep track of included TUs.
23653 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23654 if (dwarf2_per_objfile->index_table != NULL
23655 && dwarf2_per_objfile->index_table->version <= 7)
23656 {
23657 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23658 }
23659
23660 *ref_cu = sig_cu;
23661 if (sig_cu != cu)
23662 sig_cu->ancestor = cu;
23663
23664 return die;
23665 }
23666
23667 return NULL;
23668 }
23669
23670 /* Follow signatured type referenced by ATTR in SRC_DIE.
23671 On entry *REF_CU is the CU of SRC_DIE.
23672 On exit *REF_CU is the CU of the result.
23673 The result is the DIE of the type.
23674 If the referenced type cannot be found an error is thrown. */
23675
23676 static struct die_info *
23677 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23678 struct dwarf2_cu **ref_cu)
23679 {
23680 ULONGEST signature = DW_SIGNATURE (attr);
23681 struct signatured_type *sig_type;
23682 struct die_info *die;
23683
23684 gdb_assert (attr->form == DW_FORM_ref_sig8);
23685
23686 sig_type = lookup_signatured_type (*ref_cu, signature);
23687 /* sig_type will be NULL if the signatured type is missing from
23688 the debug info. */
23689 if (sig_type == NULL)
23690 {
23691 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23692 " from DIE at %s [in module %s]"),
23693 hex_string (signature), sect_offset_str (src_die->sect_off),
23694 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23695 }
23696
23697 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23698 if (die == NULL)
23699 {
23700 dump_die_for_error (src_die);
23701 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23702 " from DIE at %s [in module %s]"),
23703 hex_string (signature), sect_offset_str (src_die->sect_off),
23704 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23705 }
23706
23707 return die;
23708 }
23709
23710 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23711 reading in and processing the type unit if necessary. */
23712
23713 static struct type *
23714 get_signatured_type (struct die_info *die, ULONGEST signature,
23715 struct dwarf2_cu *cu)
23716 {
23717 struct dwarf2_per_objfile *dwarf2_per_objfile
23718 = cu->per_cu->dwarf2_per_objfile;
23719 struct signatured_type *sig_type;
23720 struct dwarf2_cu *type_cu;
23721 struct die_info *type_die;
23722 struct type *type;
23723
23724 sig_type = lookup_signatured_type (cu, signature);
23725 /* sig_type will be NULL if the signatured type is missing from
23726 the debug info. */
23727 if (sig_type == NULL)
23728 {
23729 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23730 " from DIE at %s [in module %s]"),
23731 hex_string (signature), sect_offset_str (die->sect_off),
23732 objfile_name (dwarf2_per_objfile->objfile));
23733 return build_error_marker_type (cu, die);
23734 }
23735
23736 /* If we already know the type we're done. */
23737 if (sig_type->type != NULL)
23738 return sig_type->type;
23739
23740 type_cu = cu;
23741 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23742 if (type_die != NULL)
23743 {
23744 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23745 is created. This is important, for example, because for c++ classes
23746 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23747 type = read_type_die (type_die, type_cu);
23748 if (type == NULL)
23749 {
23750 complaint (_("Dwarf Error: Cannot build signatured type %s"
23751 " referenced from DIE at %s [in module %s]"),
23752 hex_string (signature), sect_offset_str (die->sect_off),
23753 objfile_name (dwarf2_per_objfile->objfile));
23754 type = build_error_marker_type (cu, die);
23755 }
23756 }
23757 else
23758 {
23759 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23760 " from DIE at %s [in module %s]"),
23761 hex_string (signature), sect_offset_str (die->sect_off),
23762 objfile_name (dwarf2_per_objfile->objfile));
23763 type = build_error_marker_type (cu, die);
23764 }
23765 sig_type->type = type;
23766
23767 return type;
23768 }
23769
23770 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23771 reading in and processing the type unit if necessary. */
23772
23773 static struct type *
23774 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23775 struct dwarf2_cu *cu) /* ARI: editCase function */
23776 {
23777 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23778 if (attr_form_is_ref (attr))
23779 {
23780 struct dwarf2_cu *type_cu = cu;
23781 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23782
23783 return read_type_die (type_die, type_cu);
23784 }
23785 else if (attr->form == DW_FORM_ref_sig8)
23786 {
23787 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23788 }
23789 else
23790 {
23791 struct dwarf2_per_objfile *dwarf2_per_objfile
23792 = cu->per_cu->dwarf2_per_objfile;
23793
23794 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23795 " at %s [in module %s]"),
23796 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23797 objfile_name (dwarf2_per_objfile->objfile));
23798 return build_error_marker_type (cu, die);
23799 }
23800 }
23801
23802 /* Load the DIEs associated with type unit PER_CU into memory. */
23803
23804 static void
23805 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23806 {
23807 struct signatured_type *sig_type;
23808
23809 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23810 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23811
23812 /* We have the per_cu, but we need the signatured_type.
23813 Fortunately this is an easy translation. */
23814 gdb_assert (per_cu->is_debug_types);
23815 sig_type = (struct signatured_type *) per_cu;
23816
23817 gdb_assert (per_cu->cu == NULL);
23818
23819 read_signatured_type (sig_type);
23820
23821 gdb_assert (per_cu->cu != NULL);
23822 }
23823
23824 /* die_reader_func for read_signatured_type.
23825 This is identical to load_full_comp_unit_reader,
23826 but is kept separate for now. */
23827
23828 static void
23829 read_signatured_type_reader (const struct die_reader_specs *reader,
23830 const gdb_byte *info_ptr,
23831 struct die_info *comp_unit_die,
23832 int has_children,
23833 void *data)
23834 {
23835 struct dwarf2_cu *cu = reader->cu;
23836
23837 gdb_assert (cu->die_hash == NULL);
23838 cu->die_hash =
23839 htab_create_alloc_ex (cu->header.length / 12,
23840 die_hash,
23841 die_eq,
23842 NULL,
23843 &cu->comp_unit_obstack,
23844 hashtab_obstack_allocate,
23845 dummy_obstack_deallocate);
23846
23847 if (has_children)
23848 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23849 &info_ptr, comp_unit_die);
23850 cu->dies = comp_unit_die;
23851 /* comp_unit_die is not stored in die_hash, no need. */
23852
23853 /* We try not to read any attributes in this function, because not
23854 all CUs needed for references have been loaded yet, and symbol
23855 table processing isn't initialized. But we have to set the CU language,
23856 or we won't be able to build types correctly.
23857 Similarly, if we do not read the producer, we can not apply
23858 producer-specific interpretation. */
23859 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23860 }
23861
23862 /* Read in a signatured type and build its CU and DIEs.
23863 If the type is a stub for the real type in a DWO file,
23864 read in the real type from the DWO file as well. */
23865
23866 static void
23867 read_signatured_type (struct signatured_type *sig_type)
23868 {
23869 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23870
23871 gdb_assert (per_cu->is_debug_types);
23872 gdb_assert (per_cu->cu == NULL);
23873
23874 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
23875 read_signatured_type_reader, NULL);
23876 sig_type->per_cu.tu_read = 1;
23877 }
23878
23879 /* Decode simple location descriptions.
23880 Given a pointer to a dwarf block that defines a location, compute
23881 the location and return the value.
23882
23883 NOTE drow/2003-11-18: This function is called in two situations
23884 now: for the address of static or global variables (partial symbols
23885 only) and for offsets into structures which are expected to be
23886 (more or less) constant. The partial symbol case should go away,
23887 and only the constant case should remain. That will let this
23888 function complain more accurately. A few special modes are allowed
23889 without complaint for global variables (for instance, global
23890 register values and thread-local values).
23891
23892 A location description containing no operations indicates that the
23893 object is optimized out. The return value is 0 for that case.
23894 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23895 callers will only want a very basic result and this can become a
23896 complaint.
23897
23898 Note that stack[0] is unused except as a default error return. */
23899
23900 static CORE_ADDR
23901 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23902 {
23903 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23904 size_t i;
23905 size_t size = blk->size;
23906 const gdb_byte *data = blk->data;
23907 CORE_ADDR stack[64];
23908 int stacki;
23909 unsigned int bytes_read, unsnd;
23910 gdb_byte op;
23911
23912 i = 0;
23913 stacki = 0;
23914 stack[stacki] = 0;
23915 stack[++stacki] = 0;
23916
23917 while (i < size)
23918 {
23919 op = data[i++];
23920 switch (op)
23921 {
23922 case DW_OP_lit0:
23923 case DW_OP_lit1:
23924 case DW_OP_lit2:
23925 case DW_OP_lit3:
23926 case DW_OP_lit4:
23927 case DW_OP_lit5:
23928 case DW_OP_lit6:
23929 case DW_OP_lit7:
23930 case DW_OP_lit8:
23931 case DW_OP_lit9:
23932 case DW_OP_lit10:
23933 case DW_OP_lit11:
23934 case DW_OP_lit12:
23935 case DW_OP_lit13:
23936 case DW_OP_lit14:
23937 case DW_OP_lit15:
23938 case DW_OP_lit16:
23939 case DW_OP_lit17:
23940 case DW_OP_lit18:
23941 case DW_OP_lit19:
23942 case DW_OP_lit20:
23943 case DW_OP_lit21:
23944 case DW_OP_lit22:
23945 case DW_OP_lit23:
23946 case DW_OP_lit24:
23947 case DW_OP_lit25:
23948 case DW_OP_lit26:
23949 case DW_OP_lit27:
23950 case DW_OP_lit28:
23951 case DW_OP_lit29:
23952 case DW_OP_lit30:
23953 case DW_OP_lit31:
23954 stack[++stacki] = op - DW_OP_lit0;
23955 break;
23956
23957 case DW_OP_reg0:
23958 case DW_OP_reg1:
23959 case DW_OP_reg2:
23960 case DW_OP_reg3:
23961 case DW_OP_reg4:
23962 case DW_OP_reg5:
23963 case DW_OP_reg6:
23964 case DW_OP_reg7:
23965 case DW_OP_reg8:
23966 case DW_OP_reg9:
23967 case DW_OP_reg10:
23968 case DW_OP_reg11:
23969 case DW_OP_reg12:
23970 case DW_OP_reg13:
23971 case DW_OP_reg14:
23972 case DW_OP_reg15:
23973 case DW_OP_reg16:
23974 case DW_OP_reg17:
23975 case DW_OP_reg18:
23976 case DW_OP_reg19:
23977 case DW_OP_reg20:
23978 case DW_OP_reg21:
23979 case DW_OP_reg22:
23980 case DW_OP_reg23:
23981 case DW_OP_reg24:
23982 case DW_OP_reg25:
23983 case DW_OP_reg26:
23984 case DW_OP_reg27:
23985 case DW_OP_reg28:
23986 case DW_OP_reg29:
23987 case DW_OP_reg30:
23988 case DW_OP_reg31:
23989 stack[++stacki] = op - DW_OP_reg0;
23990 if (i < size)
23991 dwarf2_complex_location_expr_complaint ();
23992 break;
23993
23994 case DW_OP_regx:
23995 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23996 i += bytes_read;
23997 stack[++stacki] = unsnd;
23998 if (i < size)
23999 dwarf2_complex_location_expr_complaint ();
24000 break;
24001
24002 case DW_OP_addr:
24003 stack[++stacki] = read_address (objfile->obfd, &data[i],
24004 cu, &bytes_read);
24005 i += bytes_read;
24006 break;
24007
24008 case DW_OP_const1u:
24009 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24010 i += 1;
24011 break;
24012
24013 case DW_OP_const1s:
24014 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24015 i += 1;
24016 break;
24017
24018 case DW_OP_const2u:
24019 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24020 i += 2;
24021 break;
24022
24023 case DW_OP_const2s:
24024 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24025 i += 2;
24026 break;
24027
24028 case DW_OP_const4u:
24029 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24030 i += 4;
24031 break;
24032
24033 case DW_OP_const4s:
24034 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24035 i += 4;
24036 break;
24037
24038 case DW_OP_const8u:
24039 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24040 i += 8;
24041 break;
24042
24043 case DW_OP_constu:
24044 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24045 &bytes_read);
24046 i += bytes_read;
24047 break;
24048
24049 case DW_OP_consts:
24050 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24051 i += bytes_read;
24052 break;
24053
24054 case DW_OP_dup:
24055 stack[stacki + 1] = stack[stacki];
24056 stacki++;
24057 break;
24058
24059 case DW_OP_plus:
24060 stack[stacki - 1] += stack[stacki];
24061 stacki--;
24062 break;
24063
24064 case DW_OP_plus_uconst:
24065 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24066 &bytes_read);
24067 i += bytes_read;
24068 break;
24069
24070 case DW_OP_minus:
24071 stack[stacki - 1] -= stack[stacki];
24072 stacki--;
24073 break;
24074
24075 case DW_OP_deref:
24076 /* If we're not the last op, then we definitely can't encode
24077 this using GDB's address_class enum. This is valid for partial
24078 global symbols, although the variable's address will be bogus
24079 in the psymtab. */
24080 if (i < size)
24081 dwarf2_complex_location_expr_complaint ();
24082 break;
24083
24084 case DW_OP_GNU_push_tls_address:
24085 case DW_OP_form_tls_address:
24086 /* The top of the stack has the offset from the beginning
24087 of the thread control block at which the variable is located. */
24088 /* Nothing should follow this operator, so the top of stack would
24089 be returned. */
24090 /* This is valid for partial global symbols, but the variable's
24091 address will be bogus in the psymtab. Make it always at least
24092 non-zero to not look as a variable garbage collected by linker
24093 which have DW_OP_addr 0. */
24094 if (i < size)
24095 dwarf2_complex_location_expr_complaint ();
24096 stack[stacki]++;
24097 break;
24098
24099 case DW_OP_GNU_uninit:
24100 break;
24101
24102 case DW_OP_addrx:
24103 case DW_OP_GNU_addr_index:
24104 case DW_OP_GNU_const_index:
24105 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24106 &bytes_read);
24107 i += bytes_read;
24108 break;
24109
24110 default:
24111 {
24112 const char *name = get_DW_OP_name (op);
24113
24114 if (name)
24115 complaint (_("unsupported stack op: '%s'"),
24116 name);
24117 else
24118 complaint (_("unsupported stack op: '%02x'"),
24119 op);
24120 }
24121
24122 return (stack[stacki]);
24123 }
24124
24125 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24126 outside of the allocated space. Also enforce minimum>0. */
24127 if (stacki >= ARRAY_SIZE (stack) - 1)
24128 {
24129 complaint (_("location description stack overflow"));
24130 return 0;
24131 }
24132
24133 if (stacki <= 0)
24134 {
24135 complaint (_("location description stack underflow"));
24136 return 0;
24137 }
24138 }
24139 return (stack[stacki]);
24140 }
24141
24142 /* memory allocation interface */
24143
24144 static struct dwarf_block *
24145 dwarf_alloc_block (struct dwarf2_cu *cu)
24146 {
24147 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24148 }
24149
24150 static struct die_info *
24151 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24152 {
24153 struct die_info *die;
24154 size_t size = sizeof (struct die_info);
24155
24156 if (num_attrs > 1)
24157 size += (num_attrs - 1) * sizeof (struct attribute);
24158
24159 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24160 memset (die, 0, sizeof (struct die_info));
24161 return (die);
24162 }
24163
24164 \f
24165 /* Macro support. */
24166
24167 /* Return file name relative to the compilation directory of file number I in
24168 *LH's file name table. The result is allocated using xmalloc; the caller is
24169 responsible for freeing it. */
24170
24171 static char *
24172 file_file_name (int file, struct line_header *lh)
24173 {
24174 /* Is the file number a valid index into the line header's file name
24175 table? Remember that file numbers start with one, not zero. */
24176 if (1 <= file && file <= lh->file_names.size ())
24177 {
24178 const file_entry &fe = lh->file_names[file - 1];
24179
24180 if (!IS_ABSOLUTE_PATH (fe.name))
24181 {
24182 const char *dir = fe.include_dir (lh);
24183 if (dir != NULL)
24184 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
24185 }
24186 return xstrdup (fe.name);
24187 }
24188 else
24189 {
24190 /* The compiler produced a bogus file number. We can at least
24191 record the macro definitions made in the file, even if we
24192 won't be able to find the file by name. */
24193 char fake_name[80];
24194
24195 xsnprintf (fake_name, sizeof (fake_name),
24196 "<bad macro file number %d>", file);
24197
24198 complaint (_("bad file number in macro information (%d)"),
24199 file);
24200
24201 return xstrdup (fake_name);
24202 }
24203 }
24204
24205 /* Return the full name of file number I in *LH's file name table.
24206 Use COMP_DIR as the name of the current directory of the
24207 compilation. The result is allocated using xmalloc; the caller is
24208 responsible for freeing it. */
24209 static char *
24210 file_full_name (int file, struct line_header *lh, const char *comp_dir)
24211 {
24212 /* Is the file number a valid index into the line header's file name
24213 table? Remember that file numbers start with one, not zero. */
24214 if (1 <= file && file <= lh->file_names.size ())
24215 {
24216 char *relative = file_file_name (file, lh);
24217
24218 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24219 return relative;
24220 return reconcat (relative, comp_dir, SLASH_STRING,
24221 relative, (char *) NULL);
24222 }
24223 else
24224 return file_file_name (file, lh);
24225 }
24226
24227
24228 static struct macro_source_file *
24229 macro_start_file (struct dwarf2_cu *cu,
24230 int file, int line,
24231 struct macro_source_file *current_file,
24232 struct line_header *lh)
24233 {
24234 /* File name relative to the compilation directory of this source file. */
24235 char *file_name = file_file_name (file, lh);
24236
24237 if (! current_file)
24238 {
24239 /* Note: We don't create a macro table for this compilation unit
24240 at all until we actually get a filename. */
24241 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
24242
24243 /* If we have no current file, then this must be the start_file
24244 directive for the compilation unit's main source file. */
24245 current_file = macro_set_main (macro_table, file_name);
24246 macro_define_special (macro_table);
24247 }
24248 else
24249 current_file = macro_include (current_file, line, file_name);
24250
24251 xfree (file_name);
24252
24253 return current_file;
24254 }
24255
24256 static const char *
24257 consume_improper_spaces (const char *p, const char *body)
24258 {
24259 if (*p == ' ')
24260 {
24261 complaint (_("macro definition contains spaces "
24262 "in formal argument list:\n`%s'"),
24263 body);
24264
24265 while (*p == ' ')
24266 p++;
24267 }
24268
24269 return p;
24270 }
24271
24272
24273 static void
24274 parse_macro_definition (struct macro_source_file *file, int line,
24275 const char *body)
24276 {
24277 const char *p;
24278
24279 /* The body string takes one of two forms. For object-like macro
24280 definitions, it should be:
24281
24282 <macro name> " " <definition>
24283
24284 For function-like macro definitions, it should be:
24285
24286 <macro name> "() " <definition>
24287 or
24288 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24289
24290 Spaces may appear only where explicitly indicated, and in the
24291 <definition>.
24292
24293 The Dwarf 2 spec says that an object-like macro's name is always
24294 followed by a space, but versions of GCC around March 2002 omit
24295 the space when the macro's definition is the empty string.
24296
24297 The Dwarf 2 spec says that there should be no spaces between the
24298 formal arguments in a function-like macro's formal argument list,
24299 but versions of GCC around March 2002 include spaces after the
24300 commas. */
24301
24302
24303 /* Find the extent of the macro name. The macro name is terminated
24304 by either a space or null character (for an object-like macro) or
24305 an opening paren (for a function-like macro). */
24306 for (p = body; *p; p++)
24307 if (*p == ' ' || *p == '(')
24308 break;
24309
24310 if (*p == ' ' || *p == '\0')
24311 {
24312 /* It's an object-like macro. */
24313 int name_len = p - body;
24314 char *name = savestring (body, name_len);
24315 const char *replacement;
24316
24317 if (*p == ' ')
24318 replacement = body + name_len + 1;
24319 else
24320 {
24321 dwarf2_macro_malformed_definition_complaint (body);
24322 replacement = body + name_len;
24323 }
24324
24325 macro_define_object (file, line, name, replacement);
24326
24327 xfree (name);
24328 }
24329 else if (*p == '(')
24330 {
24331 /* It's a function-like macro. */
24332 char *name = savestring (body, p - body);
24333 int argc = 0;
24334 int argv_size = 1;
24335 char **argv = XNEWVEC (char *, argv_size);
24336
24337 p++;
24338
24339 p = consume_improper_spaces (p, body);
24340
24341 /* Parse the formal argument list. */
24342 while (*p && *p != ')')
24343 {
24344 /* Find the extent of the current argument name. */
24345 const char *arg_start = p;
24346
24347 while (*p && *p != ',' && *p != ')' && *p != ' ')
24348 p++;
24349
24350 if (! *p || p == arg_start)
24351 dwarf2_macro_malformed_definition_complaint (body);
24352 else
24353 {
24354 /* Make sure argv has room for the new argument. */
24355 if (argc >= argv_size)
24356 {
24357 argv_size *= 2;
24358 argv = XRESIZEVEC (char *, argv, argv_size);
24359 }
24360
24361 argv[argc++] = savestring (arg_start, p - arg_start);
24362 }
24363
24364 p = consume_improper_spaces (p, body);
24365
24366 /* Consume the comma, if present. */
24367 if (*p == ',')
24368 {
24369 p++;
24370
24371 p = consume_improper_spaces (p, body);
24372 }
24373 }
24374
24375 if (*p == ')')
24376 {
24377 p++;
24378
24379 if (*p == ' ')
24380 /* Perfectly formed definition, no complaints. */
24381 macro_define_function (file, line, name,
24382 argc, (const char **) argv,
24383 p + 1);
24384 else if (*p == '\0')
24385 {
24386 /* Complain, but do define it. */
24387 dwarf2_macro_malformed_definition_complaint (body);
24388 macro_define_function (file, line, name,
24389 argc, (const char **) argv,
24390 p);
24391 }
24392 else
24393 /* Just complain. */
24394 dwarf2_macro_malformed_definition_complaint (body);
24395 }
24396 else
24397 /* Just complain. */
24398 dwarf2_macro_malformed_definition_complaint (body);
24399
24400 xfree (name);
24401 {
24402 int i;
24403
24404 for (i = 0; i < argc; i++)
24405 xfree (argv[i]);
24406 }
24407 xfree (argv);
24408 }
24409 else
24410 dwarf2_macro_malformed_definition_complaint (body);
24411 }
24412
24413 /* Skip some bytes from BYTES according to the form given in FORM.
24414 Returns the new pointer. */
24415
24416 static const gdb_byte *
24417 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24418 enum dwarf_form form,
24419 unsigned int offset_size,
24420 struct dwarf2_section_info *section)
24421 {
24422 unsigned int bytes_read;
24423
24424 switch (form)
24425 {
24426 case DW_FORM_data1:
24427 case DW_FORM_flag:
24428 ++bytes;
24429 break;
24430
24431 case DW_FORM_data2:
24432 bytes += 2;
24433 break;
24434
24435 case DW_FORM_data4:
24436 bytes += 4;
24437 break;
24438
24439 case DW_FORM_data8:
24440 bytes += 8;
24441 break;
24442
24443 case DW_FORM_data16:
24444 bytes += 16;
24445 break;
24446
24447 case DW_FORM_string:
24448 read_direct_string (abfd, bytes, &bytes_read);
24449 bytes += bytes_read;
24450 break;
24451
24452 case DW_FORM_sec_offset:
24453 case DW_FORM_strp:
24454 case DW_FORM_GNU_strp_alt:
24455 bytes += offset_size;
24456 break;
24457
24458 case DW_FORM_block:
24459 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24460 bytes += bytes_read;
24461 break;
24462
24463 case DW_FORM_block1:
24464 bytes += 1 + read_1_byte (abfd, bytes);
24465 break;
24466 case DW_FORM_block2:
24467 bytes += 2 + read_2_bytes (abfd, bytes);
24468 break;
24469 case DW_FORM_block4:
24470 bytes += 4 + read_4_bytes (abfd, bytes);
24471 break;
24472
24473 case DW_FORM_addrx:
24474 case DW_FORM_sdata:
24475 case DW_FORM_strx:
24476 case DW_FORM_udata:
24477 case DW_FORM_GNU_addr_index:
24478 case DW_FORM_GNU_str_index:
24479 bytes = gdb_skip_leb128 (bytes, buffer_end);
24480 if (bytes == NULL)
24481 {
24482 dwarf2_section_buffer_overflow_complaint (section);
24483 return NULL;
24484 }
24485 break;
24486
24487 case DW_FORM_implicit_const:
24488 break;
24489
24490 default:
24491 {
24492 complaint (_("invalid form 0x%x in `%s'"),
24493 form, get_section_name (section));
24494 return NULL;
24495 }
24496 }
24497
24498 return bytes;
24499 }
24500
24501 /* A helper for dwarf_decode_macros that handles skipping an unknown
24502 opcode. Returns an updated pointer to the macro data buffer; or,
24503 on error, issues a complaint and returns NULL. */
24504
24505 static const gdb_byte *
24506 skip_unknown_opcode (unsigned int opcode,
24507 const gdb_byte **opcode_definitions,
24508 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24509 bfd *abfd,
24510 unsigned int offset_size,
24511 struct dwarf2_section_info *section)
24512 {
24513 unsigned int bytes_read, i;
24514 unsigned long arg;
24515 const gdb_byte *defn;
24516
24517 if (opcode_definitions[opcode] == NULL)
24518 {
24519 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
24520 opcode);
24521 return NULL;
24522 }
24523
24524 defn = opcode_definitions[opcode];
24525 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24526 defn += bytes_read;
24527
24528 for (i = 0; i < arg; ++i)
24529 {
24530 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24531 (enum dwarf_form) defn[i], offset_size,
24532 section);
24533 if (mac_ptr == NULL)
24534 {
24535 /* skip_form_bytes already issued the complaint. */
24536 return NULL;
24537 }
24538 }
24539
24540 return mac_ptr;
24541 }
24542
24543 /* A helper function which parses the header of a macro section.
24544 If the macro section is the extended (for now called "GNU") type,
24545 then this updates *OFFSET_SIZE. Returns a pointer to just after
24546 the header, or issues a complaint and returns NULL on error. */
24547
24548 static const gdb_byte *
24549 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24550 bfd *abfd,
24551 const gdb_byte *mac_ptr,
24552 unsigned int *offset_size,
24553 int section_is_gnu)
24554 {
24555 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24556
24557 if (section_is_gnu)
24558 {
24559 unsigned int version, flags;
24560
24561 version = read_2_bytes (abfd, mac_ptr);
24562 if (version != 4 && version != 5)
24563 {
24564 complaint (_("unrecognized version `%d' in .debug_macro section"),
24565 version);
24566 return NULL;
24567 }
24568 mac_ptr += 2;
24569
24570 flags = read_1_byte (abfd, mac_ptr);
24571 ++mac_ptr;
24572 *offset_size = (flags & 1) ? 8 : 4;
24573
24574 if ((flags & 2) != 0)
24575 /* We don't need the line table offset. */
24576 mac_ptr += *offset_size;
24577
24578 /* Vendor opcode descriptions. */
24579 if ((flags & 4) != 0)
24580 {
24581 unsigned int i, count;
24582
24583 count = read_1_byte (abfd, mac_ptr);
24584 ++mac_ptr;
24585 for (i = 0; i < count; ++i)
24586 {
24587 unsigned int opcode, bytes_read;
24588 unsigned long arg;
24589
24590 opcode = read_1_byte (abfd, mac_ptr);
24591 ++mac_ptr;
24592 opcode_definitions[opcode] = mac_ptr;
24593 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24594 mac_ptr += bytes_read;
24595 mac_ptr += arg;
24596 }
24597 }
24598 }
24599
24600 return mac_ptr;
24601 }
24602
24603 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24604 including DW_MACRO_import. */
24605
24606 static void
24607 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
24608 bfd *abfd,
24609 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24610 struct macro_source_file *current_file,
24611 struct line_header *lh,
24612 struct dwarf2_section_info *section,
24613 int section_is_gnu, int section_is_dwz,
24614 unsigned int offset_size,
24615 htab_t include_hash)
24616 {
24617 struct dwarf2_per_objfile *dwarf2_per_objfile
24618 = cu->per_cu->dwarf2_per_objfile;
24619 struct objfile *objfile = dwarf2_per_objfile->objfile;
24620 enum dwarf_macro_record_type macinfo_type;
24621 int at_commandline;
24622 const gdb_byte *opcode_definitions[256];
24623
24624 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24625 &offset_size, section_is_gnu);
24626 if (mac_ptr == NULL)
24627 {
24628 /* We already issued a complaint. */
24629 return;
24630 }
24631
24632 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24633 GDB is still reading the definitions from command line. First
24634 DW_MACINFO_start_file will need to be ignored as it was already executed
24635 to create CURRENT_FILE for the main source holding also the command line
24636 definitions. On first met DW_MACINFO_start_file this flag is reset to
24637 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24638
24639 at_commandline = 1;
24640
24641 do
24642 {
24643 /* Do we at least have room for a macinfo type byte? */
24644 if (mac_ptr >= mac_end)
24645 {
24646 dwarf2_section_buffer_overflow_complaint (section);
24647 break;
24648 }
24649
24650 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24651 mac_ptr++;
24652
24653 /* Note that we rely on the fact that the corresponding GNU and
24654 DWARF constants are the same. */
24655 DIAGNOSTIC_PUSH
24656 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24657 switch (macinfo_type)
24658 {
24659 /* A zero macinfo type indicates the end of the macro
24660 information. */
24661 case 0:
24662 break;
24663
24664 case DW_MACRO_define:
24665 case DW_MACRO_undef:
24666 case DW_MACRO_define_strp:
24667 case DW_MACRO_undef_strp:
24668 case DW_MACRO_define_sup:
24669 case DW_MACRO_undef_sup:
24670 {
24671 unsigned int bytes_read;
24672 int line;
24673 const char *body;
24674 int is_define;
24675
24676 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24677 mac_ptr += bytes_read;
24678
24679 if (macinfo_type == DW_MACRO_define
24680 || macinfo_type == DW_MACRO_undef)
24681 {
24682 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24683 mac_ptr += bytes_read;
24684 }
24685 else
24686 {
24687 LONGEST str_offset;
24688
24689 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24690 mac_ptr += offset_size;
24691
24692 if (macinfo_type == DW_MACRO_define_sup
24693 || macinfo_type == DW_MACRO_undef_sup
24694 || section_is_dwz)
24695 {
24696 struct dwz_file *dwz
24697 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24698
24699 body = read_indirect_string_from_dwz (objfile,
24700 dwz, str_offset);
24701 }
24702 else
24703 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24704 abfd, str_offset);
24705 }
24706
24707 is_define = (macinfo_type == DW_MACRO_define
24708 || macinfo_type == DW_MACRO_define_strp
24709 || macinfo_type == DW_MACRO_define_sup);
24710 if (! current_file)
24711 {
24712 /* DWARF violation as no main source is present. */
24713 complaint (_("debug info with no main source gives macro %s "
24714 "on line %d: %s"),
24715 is_define ? _("definition") : _("undefinition"),
24716 line, body);
24717 break;
24718 }
24719 if ((line == 0 && !at_commandline)
24720 || (line != 0 && at_commandline))
24721 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
24722 at_commandline ? _("command-line") : _("in-file"),
24723 is_define ? _("definition") : _("undefinition"),
24724 line == 0 ? _("zero") : _("non-zero"), line, body);
24725
24726 if (body == NULL)
24727 {
24728 /* Fedora's rpm-build's "debugedit" binary
24729 corrupted .debug_macro sections.
24730
24731 For more info, see
24732 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24733 complaint (_("debug info gives %s invalid macro %s "
24734 "without body (corrupted?) at line %d "
24735 "on file %s"),
24736 at_commandline ? _("command-line") : _("in-file"),
24737 is_define ? _("definition") : _("undefinition"),
24738 line, current_file->filename);
24739 }
24740 else if (is_define)
24741 parse_macro_definition (current_file, line, body);
24742 else
24743 {
24744 gdb_assert (macinfo_type == DW_MACRO_undef
24745 || macinfo_type == DW_MACRO_undef_strp
24746 || macinfo_type == DW_MACRO_undef_sup);
24747 macro_undef (current_file, line, body);
24748 }
24749 }
24750 break;
24751
24752 case DW_MACRO_start_file:
24753 {
24754 unsigned int bytes_read;
24755 int line, file;
24756
24757 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24758 mac_ptr += bytes_read;
24759 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24760 mac_ptr += bytes_read;
24761
24762 if ((line == 0 && !at_commandline)
24763 || (line != 0 && at_commandline))
24764 complaint (_("debug info gives source %d included "
24765 "from %s at %s line %d"),
24766 file, at_commandline ? _("command-line") : _("file"),
24767 line == 0 ? _("zero") : _("non-zero"), line);
24768
24769 if (at_commandline)
24770 {
24771 /* This DW_MACRO_start_file was executed in the
24772 pass one. */
24773 at_commandline = 0;
24774 }
24775 else
24776 current_file = macro_start_file (cu, file, line, current_file,
24777 lh);
24778 }
24779 break;
24780
24781 case DW_MACRO_end_file:
24782 if (! current_file)
24783 complaint (_("macro debug info has an unmatched "
24784 "`close_file' directive"));
24785 else
24786 {
24787 current_file = current_file->included_by;
24788 if (! current_file)
24789 {
24790 enum dwarf_macro_record_type next_type;
24791
24792 /* GCC circa March 2002 doesn't produce the zero
24793 type byte marking the end of the compilation
24794 unit. Complain if it's not there, but exit no
24795 matter what. */
24796
24797 /* Do we at least have room for a macinfo type byte? */
24798 if (mac_ptr >= mac_end)
24799 {
24800 dwarf2_section_buffer_overflow_complaint (section);
24801 return;
24802 }
24803
24804 /* We don't increment mac_ptr here, so this is just
24805 a look-ahead. */
24806 next_type
24807 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24808 mac_ptr);
24809 if (next_type != 0)
24810 complaint (_("no terminating 0-type entry for "
24811 "macros in `.debug_macinfo' section"));
24812
24813 return;
24814 }
24815 }
24816 break;
24817
24818 case DW_MACRO_import:
24819 case DW_MACRO_import_sup:
24820 {
24821 LONGEST offset;
24822 void **slot;
24823 bfd *include_bfd = abfd;
24824 struct dwarf2_section_info *include_section = section;
24825 const gdb_byte *include_mac_end = mac_end;
24826 int is_dwz = section_is_dwz;
24827 const gdb_byte *new_mac_ptr;
24828
24829 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24830 mac_ptr += offset_size;
24831
24832 if (macinfo_type == DW_MACRO_import_sup)
24833 {
24834 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24835
24836 dwarf2_read_section (objfile, &dwz->macro);
24837
24838 include_section = &dwz->macro;
24839 include_bfd = get_section_bfd_owner (include_section);
24840 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24841 is_dwz = 1;
24842 }
24843
24844 new_mac_ptr = include_section->buffer + offset;
24845 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24846
24847 if (*slot != NULL)
24848 {
24849 /* This has actually happened; see
24850 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24851 complaint (_("recursive DW_MACRO_import in "
24852 ".debug_macro section"));
24853 }
24854 else
24855 {
24856 *slot = (void *) new_mac_ptr;
24857
24858 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
24859 include_mac_end, current_file, lh,
24860 section, section_is_gnu, is_dwz,
24861 offset_size, include_hash);
24862
24863 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24864 }
24865 }
24866 break;
24867
24868 case DW_MACINFO_vendor_ext:
24869 if (!section_is_gnu)
24870 {
24871 unsigned int bytes_read;
24872
24873 /* This reads the constant, but since we don't recognize
24874 any vendor extensions, we ignore it. */
24875 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24876 mac_ptr += bytes_read;
24877 read_direct_string (abfd, mac_ptr, &bytes_read);
24878 mac_ptr += bytes_read;
24879
24880 /* We don't recognize any vendor extensions. */
24881 break;
24882 }
24883 /* FALLTHROUGH */
24884
24885 default:
24886 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24887 mac_ptr, mac_end, abfd, offset_size,
24888 section);
24889 if (mac_ptr == NULL)
24890 return;
24891 break;
24892 }
24893 DIAGNOSTIC_POP
24894 } while (macinfo_type != 0);
24895 }
24896
24897 static void
24898 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24899 int section_is_gnu)
24900 {
24901 struct dwarf2_per_objfile *dwarf2_per_objfile
24902 = cu->per_cu->dwarf2_per_objfile;
24903 struct objfile *objfile = dwarf2_per_objfile->objfile;
24904 struct line_header *lh = cu->line_header;
24905 bfd *abfd;
24906 const gdb_byte *mac_ptr, *mac_end;
24907 struct macro_source_file *current_file = 0;
24908 enum dwarf_macro_record_type macinfo_type;
24909 unsigned int offset_size = cu->header.offset_size;
24910 const gdb_byte *opcode_definitions[256];
24911 void **slot;
24912 struct dwarf2_section_info *section;
24913 const char *section_name;
24914
24915 if (cu->dwo_unit != NULL)
24916 {
24917 if (section_is_gnu)
24918 {
24919 section = &cu->dwo_unit->dwo_file->sections.macro;
24920 section_name = ".debug_macro.dwo";
24921 }
24922 else
24923 {
24924 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24925 section_name = ".debug_macinfo.dwo";
24926 }
24927 }
24928 else
24929 {
24930 if (section_is_gnu)
24931 {
24932 section = &dwarf2_per_objfile->macro;
24933 section_name = ".debug_macro";
24934 }
24935 else
24936 {
24937 section = &dwarf2_per_objfile->macinfo;
24938 section_name = ".debug_macinfo";
24939 }
24940 }
24941
24942 dwarf2_read_section (objfile, section);
24943 if (section->buffer == NULL)
24944 {
24945 complaint (_("missing %s section"), section_name);
24946 return;
24947 }
24948 abfd = get_section_bfd_owner (section);
24949
24950 /* First pass: Find the name of the base filename.
24951 This filename is needed in order to process all macros whose definition
24952 (or undefinition) comes from the command line. These macros are defined
24953 before the first DW_MACINFO_start_file entry, and yet still need to be
24954 associated to the base file.
24955
24956 To determine the base file name, we scan the macro definitions until we
24957 reach the first DW_MACINFO_start_file entry. We then initialize
24958 CURRENT_FILE accordingly so that any macro definition found before the
24959 first DW_MACINFO_start_file can still be associated to the base file. */
24960
24961 mac_ptr = section->buffer + offset;
24962 mac_end = section->buffer + section->size;
24963
24964 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24965 &offset_size, section_is_gnu);
24966 if (mac_ptr == NULL)
24967 {
24968 /* We already issued a complaint. */
24969 return;
24970 }
24971
24972 do
24973 {
24974 /* Do we at least have room for a macinfo type byte? */
24975 if (mac_ptr >= mac_end)
24976 {
24977 /* Complaint is printed during the second pass as GDB will probably
24978 stop the first pass earlier upon finding
24979 DW_MACINFO_start_file. */
24980 break;
24981 }
24982
24983 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24984 mac_ptr++;
24985
24986 /* Note that we rely on the fact that the corresponding GNU and
24987 DWARF constants are the same. */
24988 DIAGNOSTIC_PUSH
24989 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24990 switch (macinfo_type)
24991 {
24992 /* A zero macinfo type indicates the end of the macro
24993 information. */
24994 case 0:
24995 break;
24996
24997 case DW_MACRO_define:
24998 case DW_MACRO_undef:
24999 /* Only skip the data by MAC_PTR. */
25000 {
25001 unsigned int bytes_read;
25002
25003 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25004 mac_ptr += bytes_read;
25005 read_direct_string (abfd, mac_ptr, &bytes_read);
25006 mac_ptr += bytes_read;
25007 }
25008 break;
25009
25010 case DW_MACRO_start_file:
25011 {
25012 unsigned int bytes_read;
25013 int line, file;
25014
25015 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25016 mac_ptr += bytes_read;
25017 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25018 mac_ptr += bytes_read;
25019
25020 current_file = macro_start_file (cu, file, line, current_file, lh);
25021 }
25022 break;
25023
25024 case DW_MACRO_end_file:
25025 /* No data to skip by MAC_PTR. */
25026 break;
25027
25028 case DW_MACRO_define_strp:
25029 case DW_MACRO_undef_strp:
25030 case DW_MACRO_define_sup:
25031 case DW_MACRO_undef_sup:
25032 {
25033 unsigned int bytes_read;
25034
25035 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25036 mac_ptr += bytes_read;
25037 mac_ptr += offset_size;
25038 }
25039 break;
25040
25041 case DW_MACRO_import:
25042 case DW_MACRO_import_sup:
25043 /* Note that, according to the spec, a transparent include
25044 chain cannot call DW_MACRO_start_file. So, we can just
25045 skip this opcode. */
25046 mac_ptr += offset_size;
25047 break;
25048
25049 case DW_MACINFO_vendor_ext:
25050 /* Only skip the data by MAC_PTR. */
25051 if (!section_is_gnu)
25052 {
25053 unsigned int bytes_read;
25054
25055 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25056 mac_ptr += bytes_read;
25057 read_direct_string (abfd, mac_ptr, &bytes_read);
25058 mac_ptr += bytes_read;
25059 }
25060 /* FALLTHROUGH */
25061
25062 default:
25063 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25064 mac_ptr, mac_end, abfd, offset_size,
25065 section);
25066 if (mac_ptr == NULL)
25067 return;
25068 break;
25069 }
25070 DIAGNOSTIC_POP
25071 } while (macinfo_type != 0 && current_file == NULL);
25072
25073 /* Second pass: Process all entries.
25074
25075 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25076 command-line macro definitions/undefinitions. This flag is unset when we
25077 reach the first DW_MACINFO_start_file entry. */
25078
25079 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25080 htab_eq_pointer,
25081 NULL, xcalloc, xfree));
25082 mac_ptr = section->buffer + offset;
25083 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
25084 *slot = (void *) mac_ptr;
25085 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
25086 current_file, lh, section,
25087 section_is_gnu, 0, offset_size,
25088 include_hash.get ());
25089 }
25090
25091 /* Check if the attribute's form is a DW_FORM_block*
25092 if so return true else false. */
25093
25094 static int
25095 attr_form_is_block (const struct attribute *attr)
25096 {
25097 return (attr == NULL ? 0 :
25098 attr->form == DW_FORM_block1
25099 || attr->form == DW_FORM_block2
25100 || attr->form == DW_FORM_block4
25101 || attr->form == DW_FORM_block
25102 || attr->form == DW_FORM_exprloc);
25103 }
25104
25105 /* Return non-zero if ATTR's value is a section offset --- classes
25106 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25107 You may use DW_UNSND (attr) to retrieve such offsets.
25108
25109 Section 7.5.4, "Attribute Encodings", explains that no attribute
25110 may have a value that belongs to more than one of these classes; it
25111 would be ambiguous if we did, because we use the same forms for all
25112 of them. */
25113
25114 static int
25115 attr_form_is_section_offset (const struct attribute *attr)
25116 {
25117 return (attr->form == DW_FORM_data4
25118 || attr->form == DW_FORM_data8
25119 || attr->form == DW_FORM_sec_offset);
25120 }
25121
25122 /* Return non-zero if ATTR's value falls in the 'constant' class, or
25123 zero otherwise. When this function returns true, you can apply
25124 dwarf2_get_attr_constant_value to it.
25125
25126 However, note that for some attributes you must check
25127 attr_form_is_section_offset before using this test. DW_FORM_data4
25128 and DW_FORM_data8 are members of both the constant class, and of
25129 the classes that contain offsets into other debug sections
25130 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25131 that, if an attribute's can be either a constant or one of the
25132 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
25133 taken as section offsets, not constants.
25134
25135 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25136 cannot handle that. */
25137
25138 static int
25139 attr_form_is_constant (const struct attribute *attr)
25140 {
25141 switch (attr->form)
25142 {
25143 case DW_FORM_sdata:
25144 case DW_FORM_udata:
25145 case DW_FORM_data1:
25146 case DW_FORM_data2:
25147 case DW_FORM_data4:
25148 case DW_FORM_data8:
25149 case DW_FORM_implicit_const:
25150 return 1;
25151 default:
25152 return 0;
25153 }
25154 }
25155
25156
25157 /* DW_ADDR is always stored already as sect_offset; despite for the forms
25158 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25159
25160 static int
25161 attr_form_is_ref (const struct attribute *attr)
25162 {
25163 switch (attr->form)
25164 {
25165 case DW_FORM_ref_addr:
25166 case DW_FORM_ref1:
25167 case DW_FORM_ref2:
25168 case DW_FORM_ref4:
25169 case DW_FORM_ref8:
25170 case DW_FORM_ref_udata:
25171 case DW_FORM_GNU_ref_alt:
25172 return 1;
25173 default:
25174 return 0;
25175 }
25176 }
25177
25178 /* Return the .debug_loc section to use for CU.
25179 For DWO files use .debug_loc.dwo. */
25180
25181 static struct dwarf2_section_info *
25182 cu_debug_loc_section (struct dwarf2_cu *cu)
25183 {
25184 struct dwarf2_per_objfile *dwarf2_per_objfile
25185 = cu->per_cu->dwarf2_per_objfile;
25186
25187 if (cu->dwo_unit)
25188 {
25189 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25190
25191 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25192 }
25193 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25194 : &dwarf2_per_objfile->loc);
25195 }
25196
25197 /* A helper function that fills in a dwarf2_loclist_baton. */
25198
25199 static void
25200 fill_in_loclist_baton (struct dwarf2_cu *cu,
25201 struct dwarf2_loclist_baton *baton,
25202 const struct attribute *attr)
25203 {
25204 struct dwarf2_per_objfile *dwarf2_per_objfile
25205 = cu->per_cu->dwarf2_per_objfile;
25206 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25207
25208 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
25209
25210 baton->per_cu = cu->per_cu;
25211 gdb_assert (baton->per_cu);
25212 /* We don't know how long the location list is, but make sure we
25213 don't run off the edge of the section. */
25214 baton->size = section->size - DW_UNSND (attr);
25215 baton->data = section->buffer + DW_UNSND (attr);
25216 baton->base_address = cu->base_address;
25217 baton->from_dwo = cu->dwo_unit != NULL;
25218 }
25219
25220 static void
25221 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
25222 struct dwarf2_cu *cu, int is_block)
25223 {
25224 struct dwarf2_per_objfile *dwarf2_per_objfile
25225 = cu->per_cu->dwarf2_per_objfile;
25226 struct objfile *objfile = dwarf2_per_objfile->objfile;
25227 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25228
25229 if (attr_form_is_section_offset (attr)
25230 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
25231 the section. If so, fall through to the complaint in the
25232 other branch. */
25233 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
25234 {
25235 struct dwarf2_loclist_baton *baton;
25236
25237 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
25238
25239 fill_in_loclist_baton (cu, baton, attr);
25240
25241 if (cu->base_known == 0)
25242 complaint (_("Location list used without "
25243 "specifying the CU base address."));
25244
25245 SYMBOL_ACLASS_INDEX (sym) = (is_block
25246 ? dwarf2_loclist_block_index
25247 : dwarf2_loclist_index);
25248 SYMBOL_LOCATION_BATON (sym) = baton;
25249 }
25250 else
25251 {
25252 struct dwarf2_locexpr_baton *baton;
25253
25254 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
25255 baton->per_cu = cu->per_cu;
25256 gdb_assert (baton->per_cu);
25257
25258 if (attr_form_is_block (attr))
25259 {
25260 /* Note that we're just copying the block's data pointer
25261 here, not the actual data. We're still pointing into the
25262 info_buffer for SYM's objfile; right now we never release
25263 that buffer, but when we do clean up properly this may
25264 need to change. */
25265 baton->size = DW_BLOCK (attr)->size;
25266 baton->data = DW_BLOCK (attr)->data;
25267 }
25268 else
25269 {
25270 dwarf2_invalid_attrib_class_complaint ("location description",
25271 SYMBOL_NATURAL_NAME (sym));
25272 baton->size = 0;
25273 }
25274
25275 SYMBOL_ACLASS_INDEX (sym) = (is_block
25276 ? dwarf2_locexpr_block_index
25277 : dwarf2_locexpr_index);
25278 SYMBOL_LOCATION_BATON (sym) = baton;
25279 }
25280 }
25281
25282 /* Return the OBJFILE associated with the compilation unit CU. If CU
25283 came from a separate debuginfo file, then the master objfile is
25284 returned. */
25285
25286 struct objfile *
25287 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25288 {
25289 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25290
25291 /* Return the master objfile, so that we can report and look up the
25292 correct file containing this variable. */
25293 if (objfile->separate_debug_objfile_backlink)
25294 objfile = objfile->separate_debug_objfile_backlink;
25295
25296 return objfile;
25297 }
25298
25299 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25300 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25301 CU_HEADERP first. */
25302
25303 static const struct comp_unit_head *
25304 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25305 struct dwarf2_per_cu_data *per_cu)
25306 {
25307 const gdb_byte *info_ptr;
25308
25309 if (per_cu->cu)
25310 return &per_cu->cu->header;
25311
25312 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
25313
25314 memset (cu_headerp, 0, sizeof (*cu_headerp));
25315 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25316 rcuh_kind::COMPILE);
25317
25318 return cu_headerp;
25319 }
25320
25321 /* Return the address size given in the compilation unit header for CU. */
25322
25323 int
25324 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25325 {
25326 struct comp_unit_head cu_header_local;
25327 const struct comp_unit_head *cu_headerp;
25328
25329 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25330
25331 return cu_headerp->addr_size;
25332 }
25333
25334 /* Return the offset size given in the compilation unit header for CU. */
25335
25336 int
25337 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25338 {
25339 struct comp_unit_head cu_header_local;
25340 const struct comp_unit_head *cu_headerp;
25341
25342 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25343
25344 return cu_headerp->offset_size;
25345 }
25346
25347 /* See its dwarf2loc.h declaration. */
25348
25349 int
25350 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25351 {
25352 struct comp_unit_head cu_header_local;
25353 const struct comp_unit_head *cu_headerp;
25354
25355 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25356
25357 if (cu_headerp->version == 2)
25358 return cu_headerp->addr_size;
25359 else
25360 return cu_headerp->offset_size;
25361 }
25362
25363 /* Return the text offset of the CU. The returned offset comes from
25364 this CU's objfile. If this objfile came from a separate debuginfo
25365 file, then the offset may be different from the corresponding
25366 offset in the parent objfile. */
25367
25368 CORE_ADDR
25369 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25370 {
25371 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25372
25373 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25374 }
25375
25376 /* Return a type that is a generic pointer type, the size of which matches
25377 the address size given in the compilation unit header for PER_CU. */
25378 static struct type *
25379 dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25380 {
25381 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25382 struct type *void_type = objfile_type (objfile)->builtin_void;
25383 struct type *addr_type = lookup_pointer_type (void_type);
25384 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25385
25386 if (TYPE_LENGTH (addr_type) == addr_size)
25387 return addr_type;
25388
25389 addr_type
25390 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25391 return addr_type;
25392 }
25393
25394 /* Return DWARF version number of PER_CU. */
25395
25396 short
25397 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25398 {
25399 return per_cu->dwarf_version;
25400 }
25401
25402 /* Locate the .debug_info compilation unit from CU's objfile which contains
25403 the DIE at OFFSET. Raises an error on failure. */
25404
25405 static struct dwarf2_per_cu_data *
25406 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25407 unsigned int offset_in_dwz,
25408 struct dwarf2_per_objfile *dwarf2_per_objfile)
25409 {
25410 struct dwarf2_per_cu_data *this_cu;
25411 int low, high;
25412
25413 low = 0;
25414 high = dwarf2_per_objfile->all_comp_units.size () - 1;
25415 while (high > low)
25416 {
25417 struct dwarf2_per_cu_data *mid_cu;
25418 int mid = low + (high - low) / 2;
25419
25420 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25421 if (mid_cu->is_dwz > offset_in_dwz
25422 || (mid_cu->is_dwz == offset_in_dwz
25423 && mid_cu->sect_off + mid_cu->length >= sect_off))
25424 high = mid;
25425 else
25426 low = mid + 1;
25427 }
25428 gdb_assert (low == high);
25429 this_cu = dwarf2_per_objfile->all_comp_units[low];
25430 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
25431 {
25432 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25433 error (_("Dwarf Error: could not find partial DIE containing "
25434 "offset %s [in module %s]"),
25435 sect_offset_str (sect_off),
25436 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25437
25438 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25439 <= sect_off);
25440 return dwarf2_per_objfile->all_comp_units[low-1];
25441 }
25442 else
25443 {
25444 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
25445 && sect_off >= this_cu->sect_off + this_cu->length)
25446 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25447 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25448 return this_cu;
25449 }
25450 }
25451
25452 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25453
25454 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25455 : per_cu (per_cu_),
25456 mark (false),
25457 has_loclist (false),
25458 checked_producer (false),
25459 producer_is_gxx_lt_4_6 (false),
25460 producer_is_gcc_lt_4_3 (false),
25461 producer_is_icc (false),
25462 producer_is_icc_lt_14 (false),
25463 producer_is_codewarrior (false),
25464 processing_has_namespace_info (false)
25465 {
25466 per_cu->cu = this;
25467 }
25468
25469 /* Destroy a dwarf2_cu. */
25470
25471 dwarf2_cu::~dwarf2_cu ()
25472 {
25473 per_cu->cu = NULL;
25474 }
25475
25476 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25477
25478 static void
25479 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25480 enum language pretend_language)
25481 {
25482 struct attribute *attr;
25483
25484 /* Set the language we're debugging. */
25485 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25486 if (attr)
25487 set_cu_language (DW_UNSND (attr), cu);
25488 else
25489 {
25490 cu->language = pretend_language;
25491 cu->language_defn = language_def (cu->language);
25492 }
25493
25494 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25495 }
25496
25497 /* Increase the age counter on each cached compilation unit, and free
25498 any that are too old. */
25499
25500 static void
25501 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25502 {
25503 struct dwarf2_per_cu_data *per_cu, **last_chain;
25504
25505 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25506 per_cu = dwarf2_per_objfile->read_in_chain;
25507 while (per_cu != NULL)
25508 {
25509 per_cu->cu->last_used ++;
25510 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25511 dwarf2_mark (per_cu->cu);
25512 per_cu = per_cu->cu->read_in_chain;
25513 }
25514
25515 per_cu = dwarf2_per_objfile->read_in_chain;
25516 last_chain = &dwarf2_per_objfile->read_in_chain;
25517 while (per_cu != NULL)
25518 {
25519 struct dwarf2_per_cu_data *next_cu;
25520
25521 next_cu = per_cu->cu->read_in_chain;
25522
25523 if (!per_cu->cu->mark)
25524 {
25525 delete per_cu->cu;
25526 *last_chain = next_cu;
25527 }
25528 else
25529 last_chain = &per_cu->cu->read_in_chain;
25530
25531 per_cu = next_cu;
25532 }
25533 }
25534
25535 /* Remove a single compilation unit from the cache. */
25536
25537 static void
25538 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25539 {
25540 struct dwarf2_per_cu_data *per_cu, **last_chain;
25541 struct dwarf2_per_objfile *dwarf2_per_objfile
25542 = target_per_cu->dwarf2_per_objfile;
25543
25544 per_cu = dwarf2_per_objfile->read_in_chain;
25545 last_chain = &dwarf2_per_objfile->read_in_chain;
25546 while (per_cu != NULL)
25547 {
25548 struct dwarf2_per_cu_data *next_cu;
25549
25550 next_cu = per_cu->cu->read_in_chain;
25551
25552 if (per_cu == target_per_cu)
25553 {
25554 delete per_cu->cu;
25555 per_cu->cu = NULL;
25556 *last_chain = next_cu;
25557 break;
25558 }
25559 else
25560 last_chain = &per_cu->cu->read_in_chain;
25561
25562 per_cu = next_cu;
25563 }
25564 }
25565
25566 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25567 We store these in a hash table separate from the DIEs, and preserve them
25568 when the DIEs are flushed out of cache.
25569
25570 The CU "per_cu" pointer is needed because offset alone is not enough to
25571 uniquely identify the type. A file may have multiple .debug_types sections,
25572 or the type may come from a DWO file. Furthermore, while it's more logical
25573 to use per_cu->section+offset, with Fission the section with the data is in
25574 the DWO file but we don't know that section at the point we need it.
25575 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25576 because we can enter the lookup routine, get_die_type_at_offset, from
25577 outside this file, and thus won't necessarily have PER_CU->cu.
25578 Fortunately, PER_CU is stable for the life of the objfile. */
25579
25580 struct dwarf2_per_cu_offset_and_type
25581 {
25582 const struct dwarf2_per_cu_data *per_cu;
25583 sect_offset sect_off;
25584 struct type *type;
25585 };
25586
25587 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25588
25589 static hashval_t
25590 per_cu_offset_and_type_hash (const void *item)
25591 {
25592 const struct dwarf2_per_cu_offset_and_type *ofs
25593 = (const struct dwarf2_per_cu_offset_and_type *) item;
25594
25595 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25596 }
25597
25598 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25599
25600 static int
25601 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25602 {
25603 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25604 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25605 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25606 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25607
25608 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25609 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25610 }
25611
25612 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25613 table if necessary. For convenience, return TYPE.
25614
25615 The DIEs reading must have careful ordering to:
25616 * Not cause infite loops trying to read in DIEs as a prerequisite for
25617 reading current DIE.
25618 * Not trying to dereference contents of still incompletely read in types
25619 while reading in other DIEs.
25620 * Enable referencing still incompletely read in types just by a pointer to
25621 the type without accessing its fields.
25622
25623 Therefore caller should follow these rules:
25624 * Try to fetch any prerequisite types we may need to build this DIE type
25625 before building the type and calling set_die_type.
25626 * After building type call set_die_type for current DIE as soon as
25627 possible before fetching more types to complete the current type.
25628 * Make the type as complete as possible before fetching more types. */
25629
25630 static struct type *
25631 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25632 {
25633 struct dwarf2_per_objfile *dwarf2_per_objfile
25634 = cu->per_cu->dwarf2_per_objfile;
25635 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25636 struct objfile *objfile = dwarf2_per_objfile->objfile;
25637 struct attribute *attr;
25638 struct dynamic_prop prop;
25639
25640 /* For Ada types, make sure that the gnat-specific data is always
25641 initialized (if not already set). There are a few types where
25642 we should not be doing so, because the type-specific area is
25643 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25644 where the type-specific area is used to store the floatformat).
25645 But this is not a problem, because the gnat-specific information
25646 is actually not needed for these types. */
25647 if (need_gnat_info (cu)
25648 && TYPE_CODE (type) != TYPE_CODE_FUNC
25649 && TYPE_CODE (type) != TYPE_CODE_FLT
25650 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25651 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25652 && TYPE_CODE (type) != TYPE_CODE_METHOD
25653 && !HAVE_GNAT_AUX_INFO (type))
25654 INIT_GNAT_SPECIFIC (type);
25655
25656 /* Read DW_AT_allocated and set in type. */
25657 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25658 if (attr_form_is_block (attr))
25659 {
25660 struct type *prop_type
25661 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25662 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25663 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25664 }
25665 else if (attr != NULL)
25666 {
25667 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25668 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25669 sect_offset_str (die->sect_off));
25670 }
25671
25672 /* Read DW_AT_associated and set in type. */
25673 attr = dwarf2_attr (die, DW_AT_associated, cu);
25674 if (attr_form_is_block (attr))
25675 {
25676 struct type *prop_type
25677 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25678 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25679 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25680 }
25681 else if (attr != NULL)
25682 {
25683 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
25684 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25685 sect_offset_str (die->sect_off));
25686 }
25687
25688 /* Read DW_AT_data_location and set in type. */
25689 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25690 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25691 dwarf2_per_cu_addr_type (cu->per_cu)))
25692 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25693
25694 if (dwarf2_per_objfile->die_type_hash == NULL)
25695 {
25696 dwarf2_per_objfile->die_type_hash =
25697 htab_create_alloc_ex (127,
25698 per_cu_offset_and_type_hash,
25699 per_cu_offset_and_type_eq,
25700 NULL,
25701 &objfile->objfile_obstack,
25702 hashtab_obstack_allocate,
25703 dummy_obstack_deallocate);
25704 }
25705
25706 ofs.per_cu = cu->per_cu;
25707 ofs.sect_off = die->sect_off;
25708 ofs.type = type;
25709 slot = (struct dwarf2_per_cu_offset_and_type **)
25710 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25711 if (*slot)
25712 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25713 sect_offset_str (die->sect_off));
25714 *slot = XOBNEW (&objfile->objfile_obstack,
25715 struct dwarf2_per_cu_offset_and_type);
25716 **slot = ofs;
25717 return type;
25718 }
25719
25720 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25721 or return NULL if the die does not have a saved type. */
25722
25723 static struct type *
25724 get_die_type_at_offset (sect_offset sect_off,
25725 struct dwarf2_per_cu_data *per_cu)
25726 {
25727 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25728 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25729
25730 if (dwarf2_per_objfile->die_type_hash == NULL)
25731 return NULL;
25732
25733 ofs.per_cu = per_cu;
25734 ofs.sect_off = sect_off;
25735 slot = ((struct dwarf2_per_cu_offset_and_type *)
25736 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25737 if (slot)
25738 return slot->type;
25739 else
25740 return NULL;
25741 }
25742
25743 /* Look up the type for DIE in CU in die_type_hash,
25744 or return NULL if DIE does not have a saved type. */
25745
25746 static struct type *
25747 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25748 {
25749 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25750 }
25751
25752 /* Add a dependence relationship from CU to REF_PER_CU. */
25753
25754 static void
25755 dwarf2_add_dependence (struct dwarf2_cu *cu,
25756 struct dwarf2_per_cu_data *ref_per_cu)
25757 {
25758 void **slot;
25759
25760 if (cu->dependencies == NULL)
25761 cu->dependencies
25762 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25763 NULL, &cu->comp_unit_obstack,
25764 hashtab_obstack_allocate,
25765 dummy_obstack_deallocate);
25766
25767 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25768 if (*slot == NULL)
25769 *slot = ref_per_cu;
25770 }
25771
25772 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25773 Set the mark field in every compilation unit in the
25774 cache that we must keep because we are keeping CU. */
25775
25776 static int
25777 dwarf2_mark_helper (void **slot, void *data)
25778 {
25779 struct dwarf2_per_cu_data *per_cu;
25780
25781 per_cu = (struct dwarf2_per_cu_data *) *slot;
25782
25783 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25784 reading of the chain. As such dependencies remain valid it is not much
25785 useful to track and undo them during QUIT cleanups. */
25786 if (per_cu->cu == NULL)
25787 return 1;
25788
25789 if (per_cu->cu->mark)
25790 return 1;
25791 per_cu->cu->mark = true;
25792
25793 if (per_cu->cu->dependencies != NULL)
25794 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25795
25796 return 1;
25797 }
25798
25799 /* Set the mark field in CU and in every other compilation unit in the
25800 cache that we must keep because we are keeping CU. */
25801
25802 static void
25803 dwarf2_mark (struct dwarf2_cu *cu)
25804 {
25805 if (cu->mark)
25806 return;
25807 cu->mark = true;
25808 if (cu->dependencies != NULL)
25809 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25810 }
25811
25812 static void
25813 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25814 {
25815 while (per_cu)
25816 {
25817 per_cu->cu->mark = false;
25818 per_cu = per_cu->cu->read_in_chain;
25819 }
25820 }
25821
25822 /* Trivial hash function for partial_die_info: the hash value of a DIE
25823 is its offset in .debug_info for this objfile. */
25824
25825 static hashval_t
25826 partial_die_hash (const void *item)
25827 {
25828 const struct partial_die_info *part_die
25829 = (const struct partial_die_info *) item;
25830
25831 return to_underlying (part_die->sect_off);
25832 }
25833
25834 /* Trivial comparison function for partial_die_info structures: two DIEs
25835 are equal if they have the same offset. */
25836
25837 static int
25838 partial_die_eq (const void *item_lhs, const void *item_rhs)
25839 {
25840 const struct partial_die_info *part_die_lhs
25841 = (const struct partial_die_info *) item_lhs;
25842 const struct partial_die_info *part_die_rhs
25843 = (const struct partial_die_info *) item_rhs;
25844
25845 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25846 }
25847
25848 struct cmd_list_element *set_dwarf_cmdlist;
25849 struct cmd_list_element *show_dwarf_cmdlist;
25850
25851 static void
25852 set_dwarf_cmd (const char *args, int from_tty)
25853 {
25854 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25855 gdb_stdout);
25856 }
25857
25858 static void
25859 show_dwarf_cmd (const char *args, int from_tty)
25860 {
25861 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25862 }
25863
25864 bool dwarf_always_disassemble;
25865
25866 static void
25867 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25868 struct cmd_list_element *c, const char *value)
25869 {
25870 fprintf_filtered (file,
25871 _("Whether to always disassemble "
25872 "DWARF expressions is %s.\n"),
25873 value);
25874 }
25875
25876 static void
25877 show_check_physname (struct ui_file *file, int from_tty,
25878 struct cmd_list_element *c, const char *value)
25879 {
25880 fprintf_filtered (file,
25881 _("Whether to check \"physname\" is %s.\n"),
25882 value);
25883 }
25884
25885 void
25886 _initialize_dwarf2_read (void)
25887 {
25888 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25889 Set DWARF specific variables.\n\
25890 Configure DWARF variables such as the cache size."),
25891 &set_dwarf_cmdlist, "maintenance set dwarf ",
25892 0/*allow-unknown*/, &maintenance_set_cmdlist);
25893
25894 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25895 Show DWARF specific variables.\n\
25896 Show DWARF variables such as the cache size."),
25897 &show_dwarf_cmdlist, "maintenance show dwarf ",
25898 0/*allow-unknown*/, &maintenance_show_cmdlist);
25899
25900 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25901 &dwarf_max_cache_age, _("\
25902 Set the upper bound on the age of cached DWARF compilation units."), _("\
25903 Show the upper bound on the age of cached DWARF compilation units."), _("\
25904 A higher limit means that cached compilation units will be stored\n\
25905 in memory longer, and more total memory will be used. Zero disables\n\
25906 caching, which can slow down startup."),
25907 NULL,
25908 show_dwarf_max_cache_age,
25909 &set_dwarf_cmdlist,
25910 &show_dwarf_cmdlist);
25911
25912 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25913 &dwarf_always_disassemble, _("\
25914 Set whether `info address' always disassembles DWARF expressions."), _("\
25915 Show whether `info address' always disassembles DWARF expressions."), _("\
25916 When enabled, DWARF expressions are always printed in an assembly-like\n\
25917 syntax. When disabled, expressions will be printed in a more\n\
25918 conversational style, when possible."),
25919 NULL,
25920 show_dwarf_always_disassemble,
25921 &set_dwarf_cmdlist,
25922 &show_dwarf_cmdlist);
25923
25924 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25925 Set debugging of the DWARF reader."), _("\
25926 Show debugging of the DWARF reader."), _("\
25927 When enabled (non-zero), debugging messages are printed during DWARF\n\
25928 reading and symtab expansion. A value of 1 (one) provides basic\n\
25929 information. A value greater than 1 provides more verbose information."),
25930 NULL,
25931 NULL,
25932 &setdebuglist, &showdebuglist);
25933
25934 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25935 Set debugging of the DWARF DIE reader."), _("\
25936 Show debugging of the DWARF DIE reader."), _("\
25937 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25938 The value is the maximum depth to print."),
25939 NULL,
25940 NULL,
25941 &setdebuglist, &showdebuglist);
25942
25943 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25944 Set debugging of the dwarf line reader."), _("\
25945 Show debugging of the dwarf line reader."), _("\
25946 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25947 A value of 1 (one) provides basic information.\n\
25948 A value greater than 1 provides more verbose information."),
25949 NULL,
25950 NULL,
25951 &setdebuglist, &showdebuglist);
25952
25953 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25954 Set cross-checking of \"physname\" code against demangler."), _("\
25955 Show cross-checking of \"physname\" code against demangler."), _("\
25956 When enabled, GDB's internal \"physname\" code is checked against\n\
25957 the demangler."),
25958 NULL, show_check_physname,
25959 &setdebuglist, &showdebuglist);
25960
25961 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25962 no_class, &use_deprecated_index_sections, _("\
25963 Set whether to use deprecated gdb_index sections."), _("\
25964 Show whether to use deprecated gdb_index sections."), _("\
25965 When enabled, deprecated .gdb_index sections are used anyway.\n\
25966 Normally they are ignored either because of a missing feature or\n\
25967 performance issue.\n\
25968 Warning: This option must be enabled before gdb reads the file."),
25969 NULL,
25970 NULL,
25971 &setlist, &showlist);
25972
25973 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25974 &dwarf2_locexpr_funcs);
25975 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25976 &dwarf2_loclist_funcs);
25977
25978 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25979 &dwarf2_block_frame_base_locexpr_funcs);
25980 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25981 &dwarf2_block_frame_base_loclist_funcs);
25982
25983 #if GDB_SELF_TEST
25984 selftests::register_test ("dw2_expand_symtabs_matching",
25985 selftests::dw2_expand_symtabs_matching::run_test);
25986 #endif
25987 }