Fix misleading indentation error.
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
e2882c85 3 Copyright (C) 1994-2018 Free Software Foundation, Inc.
c906108c
SS
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
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
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
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
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.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
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
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
80626a55 33#include "elf-bfd.h"
c906108c
SS
34#include "symtab.h"
35#include "gdbtypes.h"
c906108c 36#include "objfiles.h"
fa8f86ff 37#include "dwarf2.h"
c906108c
SS
38#include "buildsym.h"
39#include "demangle.h"
50f182aa 40#include "gdb-demangle.h"
c906108c 41#include "expression.h"
d5166ae1 42#include "filenames.h" /* for DOSish file names */
2e276125 43#include "macrotab.h"
c906108c
SS
44#include "language.h"
45#include "complaints.h"
357e46e7 46#include "bcache.h"
4c2df51b
DJ
47#include "dwarf2expr.h"
48#include "dwarf2loc.h"
9219021c 49#include "cp-support.h"
72bf9492 50#include "hashtab.h"
ae038cb0
DJ
51#include "command.h"
52#include "gdbcmd.h"
edb3359d 53#include "block.h"
ff013f42 54#include "addrmap.h"
94af9270 55#include "typeprint.h"
ccefe4c4 56#include "psympriv.h"
53ce3c39 57#include <sys/stat.h>
96d19272 58#include "completer.h"
34eaf542 59#include "vec.h"
98bfdba5 60#include "c-lang.h"
a766d390 61#include "go-lang.h"
98bfdba5 62#include "valprint.h"
3019eac3 63#include "gdbcore.h" /* for gnutarget */
156942c7 64#include "gdb/gdb-index.h"
60d5a603 65#include <ctype.h>
cbb099e8 66#include "gdb_bfd.h"
4357ac6c 67#include "f-lang.h"
05cba821 68#include "source.h"
614c279d 69#include "filestuff.h"
dc294be5 70#include "build-id.h"
22cee43f 71#include "namespace.h"
bef155c3 72#include "common/gdb_unlinker.h"
14bc53a8 73#include "common/function-view.h"
ecfb656c
PA
74#include "common/gdb_optional.h"
75#include "common/underlying.h"
d5722aa2 76#include "common/byte-vector.h"
927aa2e7 77#include "common/hash_enum.h"
bbf2f4df 78#include "filename-seen-cache.h"
b32b108a 79#include "producer.h"
c906108c 80#include <fcntl.h>
c906108c 81#include <sys/types.h>
325fac50 82#include <algorithm>
bc8f2430
JK
83#include <unordered_set>
84#include <unordered_map>
c62446b1 85#include "selftest.h"
437afbb8
JK
86#include <cmath>
87#include <set>
88#include <forward_list>
c9317f21 89#include "rust-lang.h"
b4987c95 90#include "common/pathstuff.h"
437afbb8 91
73be47f5
DE
92/* When == 1, print basic high level tracing messages.
93 When > 1, be more verbose.
b4f54984
DE
94 This is in contrast to the low level DIE reading of dwarf_die_debug. */
95static unsigned int dwarf_read_debug = 0;
45cfd468 96
d97bc12b 97/* When non-zero, dump DIEs after they are read in. */
b4f54984 98static unsigned int dwarf_die_debug = 0;
d97bc12b 99
27e0867f
DE
100/* When non-zero, dump line number entries as they are read in. */
101static unsigned int dwarf_line_debug = 0;
102
900e11f9
JK
103/* When non-zero, cross-check physname against demangler. */
104static int check_physname = 0;
105
481860b3 106/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 107static int use_deprecated_index_sections = 0;
481860b3 108
6502dd73
DJ
109static const struct objfile_data *dwarf2_objfile_data_key;
110
f1e6e072
TT
111/* The "aclass" indices for various kinds of computed DWARF symbols. */
112
113static int dwarf2_locexpr_index;
114static int dwarf2_loclist_index;
115static int dwarf2_locexpr_block_index;
116static int dwarf2_loclist_block_index;
117
73869dc2
DE
118/* A descriptor for dwarf sections.
119
120 S.ASECTION, SIZE are typically initialized when the objfile is first
121 scanned. BUFFER, READIN are filled in later when the section is read.
122 If the section contained compressed data then SIZE is updated to record
123 the uncompressed size of the section.
124
125 DWP file format V2 introduces a wrinkle that is easiest to handle by
126 creating the concept of virtual sections contained within a real section.
127 In DWP V2 the sections of the input DWO files are concatenated together
128 into one section, but section offsets are kept relative to the original
129 input section.
130 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
131 the real section this "virtual" section is contained in, and BUFFER,SIZE
132 describe the virtual section. */
133
dce234bc
PP
134struct dwarf2_section_info
135{
73869dc2
DE
136 union
137 {
e5aa3347 138 /* If this is a real section, the bfd section. */
049412e3 139 asection *section;
73869dc2 140 /* If this is a virtual section, pointer to the containing ("real")
e5aa3347 141 section. */
73869dc2
DE
142 struct dwarf2_section_info *containing_section;
143 } s;
19ac8c2e 144 /* Pointer to section data, only valid if readin. */
d521ce57 145 const gdb_byte *buffer;
73869dc2 146 /* The size of the section, real or virtual. */
dce234bc 147 bfd_size_type size;
73869dc2
DE
148 /* If this is a virtual section, the offset in the real section.
149 Only valid if is_virtual. */
150 bfd_size_type virtual_offset;
be391dca 151 /* True if we have tried to read this section. */
73869dc2
DE
152 char readin;
153 /* True if this is a virtual section, False otherwise.
049412e3 154 This specifies which of s.section and s.containing_section to use. */
73869dc2 155 char is_virtual;
dce234bc
PP
156};
157
8b70b953
TT
158typedef struct dwarf2_section_info dwarf2_section_info_def;
159DEF_VEC_O (dwarf2_section_info_def);
160
9291a0cd
TT
161/* All offsets in the index are of this type. It must be
162 architecture-independent. */
163typedef uint32_t offset_type;
164
165DEF_VEC_I (offset_type);
166
156942c7
DE
167/* Ensure only legit values are used. */
168#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
169 do { \
170 gdb_assert ((unsigned int) (value) <= 1); \
171 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
172 } while (0)
173
174/* Ensure only legit values are used. */
175#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
176 do { \
177 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
178 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
179 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
180 } while (0)
181
182/* Ensure we don't use more than the alloted nuber of bits for the CU. */
183#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
184 do { \
185 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
186 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
187 } while (0)
188
3f563c84
PA
189#if WORDS_BIGENDIAN
190
191/* Convert VALUE between big- and little-endian. */
192
193static offset_type
194byte_swap (offset_type value)
195{
196 offset_type result;
197
198 result = (value & 0xff) << 24;
199 result |= (value & 0xff00) << 8;
200 result |= (value & 0xff0000) >> 8;
201 result |= (value & 0xff000000) >> 24;
202 return result;
203}
204
205#define MAYBE_SWAP(V) byte_swap (V)
206
207#else
208#define MAYBE_SWAP(V) static_cast<offset_type> (V)
209#endif /* WORDS_BIGENDIAN */
210
211/* An index into a (C++) symbol name component in a symbol name as
212 recorded in the mapped_index's symbol table. For each C++ symbol
213 in the symbol table, we record one entry for the start of each
214 component in the symbol in a table of name components, and then
215 sort the table, in order to be able to binary search symbol names,
216 ignoring leading namespaces, both completion and regular look up.
217 For example, for symbol "A::B::C", we'll have an entry that points
218 to "A::B::C", another that points to "B::C", and another for "C".
219 Note that function symbols in GDB index have no parameter
220 information, just the function/method names. You can convert a
221 name_component to a "const char *" using the
222 'mapped_index::symbol_name_at(offset_type)' method. */
223
224struct name_component
225{
226 /* Offset in the symbol name where the component starts. Stored as
227 a (32-bit) offset instead of a pointer to save memory and improve
228 locality on 64-bit architectures. */
229 offset_type name_offset;
230
231 /* The symbol's index in the symbol and constant pool tables of a
232 mapped_index. */
233 offset_type idx;
234};
235
44ed8f3e
PA
236/* Base class containing bits shared by both .gdb_index and
237 .debug_name indexes. */
238
239struct mapped_index_base
240{
241 /* The name_component table (a sorted vector). See name_component's
242 description above. */
243 std::vector<name_component> name_components;
244
245 /* How NAME_COMPONENTS is sorted. */
246 enum case_sensitivity name_components_casing;
247
248 /* Return the number of names in the symbol table. */
249 virtual size_t symbol_name_count () const = 0;
250
251 /* Get the name of the symbol at IDX in the symbol table. */
252 virtual const char *symbol_name_at (offset_type idx) const = 0;
253
254 /* Return whether the name at IDX in the symbol table should be
255 ignored. */
256 virtual bool symbol_name_slot_invalid (offset_type idx) const
257 {
258 return false;
259 }
260
261 /* Build the symbol name component sorted vector, if we haven't
262 yet. */
263 void build_name_components ();
264
265 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
266 possible matches for LN_NO_PARAMS in the name component
267 vector. */
268 std::pair<std::vector<name_component>::const_iterator,
269 std::vector<name_component>::const_iterator>
270 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
271
272 /* Prevent deleting/destroying via a base class pointer. */
273protected:
274 ~mapped_index_base() = default;
275};
276
9291a0cd
TT
277/* A description of the mapped index. The file format is described in
278 a comment by the code that writes the index. */
fc898b42 279struct mapped_index final : public mapped_index_base
9291a0cd 280{
f00a2de2
PA
281 /* A slot/bucket in the symbol table hash. */
282 struct symbol_table_slot
283 {
284 const offset_type name;
285 const offset_type vec;
286 };
287
559a7a62
JK
288 /* Index data format version. */
289 int version;
290
9291a0cd
TT
291 /* The total length of the buffer. */
292 off_t total_size;
b11b1f88 293
f00a2de2
PA
294 /* The address table data. */
295 gdb::array_view<const gdb_byte> address_table;
b11b1f88 296
3876f04e 297 /* The symbol table, implemented as a hash table. */
f00a2de2 298 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 299
9291a0cd
TT
300 /* A pointer to the constant pool. */
301 const char *constant_pool;
3f563c84 302
44ed8f3e
PA
303 bool symbol_name_slot_invalid (offset_type idx) const override
304 {
305 const auto &bucket = this->symbol_table[idx];
306 return bucket.name == 0 && bucket.vec;
307 }
5c58de74 308
3f563c84
PA
309 /* Convenience method to get at the name of the symbol at IDX in the
310 symbol table. */
44ed8f3e 311 const char *symbol_name_at (offset_type idx) const override
f00a2de2 312 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 313
44ed8f3e
PA
314 size_t symbol_name_count () const override
315 { return this->symbol_table.size (); }
9291a0cd
TT
316};
317
927aa2e7
JK
318/* A description of the mapped .debug_names.
319 Uninitialized map has CU_COUNT 0. */
fc898b42 320struct mapped_debug_names final : public mapped_index_base
927aa2e7 321{
ed2dc618
SM
322 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
323 : dwarf2_per_objfile (dwarf2_per_objfile_)
324 {}
325
326 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
327 bfd_endian dwarf5_byte_order;
328 bool dwarf5_is_dwarf64;
329 bool augmentation_is_gdb;
330 uint8_t offset_size;
331 uint32_t cu_count = 0;
332 uint32_t tu_count, bucket_count, name_count;
333 const gdb_byte *cu_table_reordered, *tu_table_reordered;
334 const uint32_t *bucket_table_reordered, *hash_table_reordered;
335 const gdb_byte *name_table_string_offs_reordered;
336 const gdb_byte *name_table_entry_offs_reordered;
337 const gdb_byte *entry_pool;
338
339 struct index_val
340 {
341 ULONGEST dwarf_tag;
342 struct attr
343 {
344 /* Attribute name DW_IDX_*. */
345 ULONGEST dw_idx;
346
347 /* Attribute form DW_FORM_*. */
348 ULONGEST form;
349
350 /* Value if FORM is DW_FORM_implicit_const. */
351 LONGEST implicit_const;
352 };
353 std::vector<attr> attr_vec;
354 };
355
356 std::unordered_map<ULONGEST, index_val> abbrev_map;
357
358 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
359
360 /* Implementation of the mapped_index_base virtual interface, for
361 the name_components cache. */
362
363 const char *symbol_name_at (offset_type idx) const override
364 { return namei_to_name (idx); }
365
366 size_t symbol_name_count () const override
367 { return this->name_count; }
927aa2e7
JK
368};
369
95554aad
TT
370typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
371DEF_VEC_P (dwarf2_per_cu_ptr);
372
52059ffd
TT
373struct tu_stats
374{
375 int nr_uniq_abbrev_tables;
376 int nr_symtabs;
377 int nr_symtab_sharers;
378 int nr_stmt_less_type_units;
379 int nr_all_type_units_reallocs;
380};
381
9cdd5dbd
DE
382/* Collection of data recorded per objfile.
383 This hangs off of dwarf2_objfile_data_key. */
384
fd90ace4 385struct dwarf2_per_objfile : public allocate_on_obstack
6502dd73 386{
330cdd98
PA
387 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
388 dwarf2 section names, or is NULL if the standard ELF names are
389 used. */
390 dwarf2_per_objfile (struct objfile *objfile,
391 const dwarf2_debug_sections *names);
ae038cb0 392
330cdd98
PA
393 ~dwarf2_per_objfile ();
394
d6541620 395 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
330cdd98
PA
396
397 /* Free all cached compilation units. */
398 void free_cached_comp_units ();
399private:
400 /* This function is mapped across the sections and remembers the
401 offset and size of each of the debugging sections we are
402 interested in. */
403 void locate_sections (bfd *abfd, asection *sectp,
404 const dwarf2_debug_sections &names);
405
406public:
407 dwarf2_section_info info {};
408 dwarf2_section_info abbrev {};
409 dwarf2_section_info line {};
410 dwarf2_section_info loc {};
411 dwarf2_section_info loclists {};
412 dwarf2_section_info macinfo {};
413 dwarf2_section_info macro {};
414 dwarf2_section_info str {};
415 dwarf2_section_info line_str {};
416 dwarf2_section_info ranges {};
417 dwarf2_section_info rnglists {};
418 dwarf2_section_info addr {};
419 dwarf2_section_info frame {};
420 dwarf2_section_info eh_frame {};
421 dwarf2_section_info gdb_index {};
927aa2e7
JK
422 dwarf2_section_info debug_names {};
423 dwarf2_section_info debug_aranges {};
330cdd98
PA
424
425 VEC (dwarf2_section_info_def) *types = NULL;
8b70b953 426
be391dca 427 /* Back link. */
330cdd98 428 struct objfile *objfile = NULL;
be391dca 429
d467dd73 430 /* Table of all the compilation units. This is used to locate
10b3939b 431 the target compilation unit of a particular reference. */
330cdd98 432 struct dwarf2_per_cu_data **all_comp_units = NULL;
ae038cb0
DJ
433
434 /* The number of compilation units in ALL_COMP_UNITS. */
330cdd98 435 int n_comp_units = 0;
ae038cb0 436
1fd400ff 437 /* The number of .debug_types-related CUs. */
330cdd98 438 int n_type_units = 0;
1fd400ff 439
6aa5f3a6
DE
440 /* The number of elements allocated in all_type_units.
441 If there are skeleton-less TUs, we add them to all_type_units lazily. */
330cdd98 442 int n_allocated_type_units = 0;
6aa5f3a6 443
a2ce51a0
DE
444 /* The .debug_types-related CUs (TUs).
445 This is stored in malloc space because we may realloc it. */
330cdd98 446 struct signatured_type **all_type_units = NULL;
1fd400ff 447
f4dc4d17
DE
448 /* Table of struct type_unit_group objects.
449 The hash key is the DW_AT_stmt_list value. */
330cdd98 450 htab_t type_unit_groups {};
72dca2f5 451
348e048f
DE
452 /* A table mapping .debug_types signatures to its signatured_type entry.
453 This is NULL if the .debug_types section hasn't been read in yet. */
330cdd98 454 htab_t signatured_types {};
348e048f 455
f4dc4d17
DE
456 /* Type unit statistics, to see how well the scaling improvements
457 are doing. */
330cdd98 458 struct tu_stats tu_stats {};
f4dc4d17
DE
459
460 /* A chain of compilation units that are currently read in, so that
461 they can be freed later. */
330cdd98 462 dwarf2_per_cu_data *read_in_chain = NULL;
f4dc4d17 463
3019eac3
DE
464 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
465 This is NULL if the table hasn't been allocated yet. */
330cdd98 466 htab_t dwo_files {};
3019eac3 467
330cdd98
PA
468 /* True if we've checked for whether there is a DWP file. */
469 bool dwp_checked = false;
80626a55
DE
470
471 /* The DWP file if there is one, or NULL. */
330cdd98 472 struct dwp_file *dwp_file = NULL;
80626a55 473
36586728
TT
474 /* The shared '.dwz' file, if one exists. This is used when the
475 original data was compressed using 'dwz -m'. */
330cdd98 476 struct dwz_file *dwz_file = NULL;
36586728 477
330cdd98 478 /* A flag indicating whether this objfile has a section loaded at a
72dca2f5 479 VMA of 0. */
330cdd98 480 bool has_section_at_zero = false;
9291a0cd 481
ae2de4f8
DE
482 /* True if we are using the mapped index,
483 or we are faking it for OBJF_READNOW's sake. */
330cdd98 484 bool using_index = false;
9291a0cd 485
ae2de4f8 486 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
330cdd98 487 mapped_index *index_table = NULL;
98bfdba5 488
927aa2e7
JK
489 /* The mapped index, or NULL if .debug_names is missing or not being used. */
490 std::unique_ptr<mapped_debug_names> debug_names_table;
491
7b9f3c50 492 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
493 TUs typically share line table entries with a CU, so we maintain a
494 separate table of all line table entries to support the sharing.
495 Note that while there can be way more TUs than CUs, we've already
496 sorted all the TUs into "type unit groups", grouped by their
497 DW_AT_stmt_list value. Therefore the only sharing done here is with a
498 CU and its associated TU group if there is one. */
330cdd98 499 htab_t quick_file_names_table {};
7b9f3c50 500
98bfdba5
PA
501 /* Set during partial symbol reading, to prevent queueing of full
502 symbols. */
330cdd98 503 bool reading_partial_symbols = false;
673bfd45 504
dee91e82 505 /* Table mapping type DIEs to their struct type *.
673bfd45 506 This is NULL if not allocated yet.
02142a6c 507 The mapping is done via (CU/TU + DIE offset) -> type. */
330cdd98 508 htab_t die_type_hash {};
95554aad
TT
509
510 /* The CUs we recently read. */
330cdd98 511 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
527f3840
JK
512
513 /* Table containing line_header indexed by offset and offset_in_dwz. */
330cdd98 514 htab_t line_header_hash {};
bbf2f4df
PA
515
516 /* Table containing all filenames. This is an optional because the
517 table is lazily constructed on first access. */
518 gdb::optional<filename_seen_cache> filenames_cache;
6502dd73
DJ
519};
520
ed2dc618
SM
521/* Get the dwarf2_per_objfile associated to OBJFILE. */
522
523struct dwarf2_per_objfile *
524get_dwarf2_per_objfile (struct objfile *objfile)
525{
526 return ((struct dwarf2_per_objfile *)
527 objfile_data (objfile, dwarf2_objfile_data_key));
528}
529
530/* Set the dwarf2_per_objfile associated to OBJFILE. */
531
532void
533set_dwarf2_per_objfile (struct objfile *objfile,
534 struct dwarf2_per_objfile *dwarf2_per_objfile)
535{
536 gdb_assert (get_dwarf2_per_objfile (objfile) == NULL);
537 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
538}
c906108c 539
251d32d9 540/* Default names of the debugging sections. */
c906108c 541
233a11ab
CS
542/* Note that if the debugging section has been compressed, it might
543 have a name like .zdebug_info. */
544
9cdd5dbd
DE
545static const struct dwarf2_debug_sections dwarf2_elf_names =
546{
251d32d9
TG
547 { ".debug_info", ".zdebug_info" },
548 { ".debug_abbrev", ".zdebug_abbrev" },
549 { ".debug_line", ".zdebug_line" },
550 { ".debug_loc", ".zdebug_loc" },
43988095 551 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 552 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 553 { ".debug_macro", ".zdebug_macro" },
251d32d9 554 { ".debug_str", ".zdebug_str" },
43988095 555 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 556 { ".debug_ranges", ".zdebug_ranges" },
43988095 557 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 558 { ".debug_types", ".zdebug_types" },
3019eac3 559 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
560 { ".debug_frame", ".zdebug_frame" },
561 { ".eh_frame", NULL },
24d3216f 562 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
563 { ".debug_names", ".zdebug_names" },
564 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 565 23
251d32d9 566};
c906108c 567
80626a55 568/* List of DWO/DWP sections. */
3019eac3 569
80626a55 570static const struct dwop_section_names
3019eac3
DE
571{
572 struct dwarf2_section_names abbrev_dwo;
573 struct dwarf2_section_names info_dwo;
574 struct dwarf2_section_names line_dwo;
575 struct dwarf2_section_names loc_dwo;
43988095 576 struct dwarf2_section_names loclists_dwo;
09262596
DE
577 struct dwarf2_section_names macinfo_dwo;
578 struct dwarf2_section_names macro_dwo;
3019eac3
DE
579 struct dwarf2_section_names str_dwo;
580 struct dwarf2_section_names str_offsets_dwo;
581 struct dwarf2_section_names types_dwo;
80626a55
DE
582 struct dwarf2_section_names cu_index;
583 struct dwarf2_section_names tu_index;
3019eac3 584}
80626a55 585dwop_section_names =
3019eac3
DE
586{
587 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
588 { ".debug_info.dwo", ".zdebug_info.dwo" },
589 { ".debug_line.dwo", ".zdebug_line.dwo" },
590 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 591 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
592 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
593 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
594 { ".debug_str.dwo", ".zdebug_str.dwo" },
595 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
596 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
597 { ".debug_cu_index", ".zdebug_cu_index" },
598 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
599};
600
c906108c
SS
601/* local data types */
602
107d2387
AC
603/* The data in a compilation unit header, after target2host
604 translation, looks like this. */
c906108c 605struct comp_unit_head
a738430d 606{
c764a876 607 unsigned int length;
a738430d 608 short version;
a738430d
MK
609 unsigned char addr_size;
610 unsigned char signed_addr_p;
9c541725 611 sect_offset abbrev_sect_off;
57349743 612
a738430d
MK
613 /* Size of file offsets; either 4 or 8. */
614 unsigned int offset_size;
57349743 615
a738430d
MK
616 /* Size of the length field; either 4 or 12. */
617 unsigned int initial_length_size;
57349743 618
43988095
JK
619 enum dwarf_unit_type unit_type;
620
a738430d
MK
621 /* Offset to the first byte of this compilation unit header in the
622 .debug_info section, for resolving relative reference dies. */
9c541725 623 sect_offset sect_off;
57349743 624
d00adf39
DE
625 /* Offset to first die in this cu from the start of the cu.
626 This will be the first byte following the compilation unit header. */
9c541725 627 cu_offset first_die_cu_offset;
43988095
JK
628
629 /* 64-bit signature of this type unit - it is valid only for
630 UNIT_TYPE DW_UT_type. */
631 ULONGEST signature;
632
633 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 634 cu_offset type_cu_offset_in_tu;
a738430d 635};
c906108c 636
3da10d80
KS
637/* Type used for delaying computation of method physnames.
638 See comments for compute_delayed_physnames. */
639struct delayed_method_info
640{
641 /* The type to which the method is attached, i.e., its parent class. */
642 struct type *type;
643
644 /* The index of the method in the type's function fieldlists. */
645 int fnfield_index;
646
647 /* The index of the method in the fieldlist. */
648 int index;
649
650 /* The name of the DIE. */
651 const char *name;
652
653 /* The DIE associated with this method. */
654 struct die_info *die;
655};
656
e7c27a73
DJ
657/* Internal state when decoding a particular compilation unit. */
658struct dwarf2_cu
659{
fcd3b13d
SM
660 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
661 ~dwarf2_cu ();
662
663 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
664
d00adf39 665 /* The header of the compilation unit. */
fcd3b13d 666 struct comp_unit_head header {};
e142c38c 667
d00adf39 668 /* Base address of this compilation unit. */
fcd3b13d 669 CORE_ADDR base_address = 0;
d00adf39
DE
670
671 /* Non-zero if base_address has been set. */
fcd3b13d 672 int base_known = 0;
d00adf39 673
e142c38c 674 /* The language we are debugging. */
fcd3b13d
SM
675 enum language language = language_unknown;
676 const struct language_defn *language_defn = nullptr;
e142c38c 677
fcd3b13d 678 const char *producer = nullptr;
b0f35d58 679
e142c38c
DJ
680 /* The generic symbol table building routines have separate lists for
681 file scope symbols and all all other scopes (local scopes). So
682 we need to select the right one to pass to add_symbol_to_list().
683 We do it by keeping a pointer to the correct list in list_in_scope.
684
685 FIXME: The original dwarf code just treated the file scope as the
686 first local scope, and all other local scopes as nested local
687 scopes, and worked fine. Check to see if we really need to
688 distinguish these in buildsym.c. */
fcd3b13d 689 struct pending **list_in_scope = nullptr;
e142c38c 690
b64f50a1
JK
691 /* Hash table holding all the loaded partial DIEs
692 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 693 htab_t partial_dies = nullptr;
72bf9492
DJ
694
695 /* Storage for things with the same lifetime as this read-in compilation
696 unit, including partial DIEs. */
fcd3b13d 697 auto_obstack comp_unit_obstack;
72bf9492 698
ae038cb0
DJ
699 /* When multiple dwarf2_cu structures are living in memory, this field
700 chains them all together, so that they can be released efficiently.
701 We will probably also want a generation counter so that most-recently-used
702 compilation units are cached... */
fcd3b13d 703 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 704
69d751e3 705 /* Backlink to our per_cu entry. */
ae038cb0
DJ
706 struct dwarf2_per_cu_data *per_cu;
707
708 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 709 int last_used = 0;
ae038cb0 710
b64f50a1
JK
711 /* A hash table of DIE cu_offset for following references with
712 die_info->offset.sect_off as hash. */
fcd3b13d 713 htab_t die_hash = nullptr;
10b3939b
DJ
714
715 /* Full DIEs if read in. */
fcd3b13d 716 struct die_info *dies = nullptr;
10b3939b
DJ
717
718 /* A set of pointers to dwarf2_per_cu_data objects for compilation
719 units referenced by this one. Only set during full symbol processing;
720 partial symbol tables do not have dependencies. */
fcd3b13d 721 htab_t dependencies = nullptr;
10b3939b 722
cb1df416 723 /* Header data from the line table, during full symbol processing. */
fcd3b13d 724 struct line_header *line_header = nullptr;
4c8aa72d
PA
725 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
726 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
727 this is the DW_TAG_compile_unit die for this CU. We'll hold on
728 to the line header as long as this DIE is being processed. See
729 process_die_scope. */
fcd3b13d 730 die_info *line_header_die_owner = nullptr;
cb1df416 731
3da10d80
KS
732 /* A list of methods which need to have physnames computed
733 after all type information has been read. */
c89b44cd 734 std::vector<delayed_method_info> method_list;
3da10d80 735
96408a79 736 /* To be copied to symtab->call_site_htab. */
fcd3b13d 737 htab_t call_site_htab = nullptr;
96408a79 738
034e5797
DE
739 /* Non-NULL if this CU came from a DWO file.
740 There is an invariant here that is important to remember:
741 Except for attributes copied from the top level DIE in the "main"
742 (or "stub") file in preparation for reading the DWO file
743 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
744 Either there isn't a DWO file (in which case this is NULL and the point
745 is moot), or there is and either we're not going to read it (in which
746 case this is NULL) or there is and we are reading it (in which case this
747 is non-NULL). */
fcd3b13d 748 struct dwo_unit *dwo_unit = nullptr;
3019eac3
DE
749
750 /* The DW_AT_addr_base attribute if present, zero otherwise
751 (zero is a valid value though).
1dbab08b 752 Note this value comes from the Fission stub CU/TU's DIE. */
fcd3b13d 753 ULONGEST addr_base = 0;
3019eac3 754
2e3cf129
DE
755 /* The DW_AT_ranges_base attribute if present, zero otherwise
756 (zero is a valid value though).
1dbab08b 757 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 758 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
759 be used without needing to know whether DWO files are in use or not.
760 N.B. This does not apply to DW_AT_ranges appearing in
761 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
762 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
763 DW_AT_ranges_base *would* have to be applied, and we'd have to care
764 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 765 ULONGEST ranges_base = 0;
2e3cf129 766
c9317f21
TT
767 /* When reading debug info generated by older versions of rustc, we
768 have to rewrite some union types to be struct types with a
769 variant part. This rewriting must be done after the CU is fully
770 read in, because otherwise at the point of rewriting some struct
771 type might not have been fully processed. So, we keep a list of
772 all such types here and process them after expansion. */
773 std::vector<struct type *> rust_unions;
774
ae038cb0
DJ
775 /* Mark used when releasing cached dies. */
776 unsigned int mark : 1;
777
8be455d7
JK
778 /* This CU references .debug_loc. See the symtab->locations_valid field.
779 This test is imperfect as there may exist optimized debug code not using
780 any location list and still facing inlining issues if handled as
781 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 782 unsigned int has_loclist : 1;
ba919b58 783
1b80a9fa
JK
784 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
785 if all the producer_is_* fields are valid. This information is cached
786 because profiling CU expansion showed excessive time spent in
787 producer_is_gxx_lt_4_6. */
ba919b58
TT
788 unsigned int checked_producer : 1;
789 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 790 unsigned int producer_is_gcc_lt_4_3 : 1;
5230b05a 791 unsigned int producer_is_icc_lt_14 : 1;
4d4ec4e5
TT
792
793 /* When set, the file that we're processing is known to have
794 debugging info for C++ namespaces. GCC 3.3.x did not produce
795 this information, but later versions do. */
796
797 unsigned int processing_has_namespace_info : 1;
d590ff25
YQ
798
799 struct partial_die_info *find_partial_die (sect_offset sect_off);
e7c27a73
DJ
800};
801
10b3939b
DJ
802/* Persistent data held for a compilation unit, even when not
803 processing it. We put a pointer to this structure in the
28dee7f5 804 read_symtab_private field of the psymtab. */
10b3939b 805
ae038cb0
DJ
806struct dwarf2_per_cu_data
807{
36586728 808 /* The start offset and length of this compilation unit.
45452591 809 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
810 initial_length_size.
811 If the DIE refers to a DWO file, this is always of the original die,
812 not the DWO file. */
9c541725 813 sect_offset sect_off;
36586728 814 unsigned int length;
ae038cb0 815
43988095
JK
816 /* DWARF standard version this data has been read from (such as 4 or 5). */
817 short dwarf_version;
818
ae038cb0
DJ
819 /* Flag indicating this compilation unit will be read in before
820 any of the current compilation units are processed. */
c764a876 821 unsigned int queued : 1;
ae038cb0 822
0d99eb77
DE
823 /* This flag will be set when reading partial DIEs if we need to load
824 absolutely all DIEs for this compilation unit, instead of just the ones
825 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
826 hash table and don't find it. */
827 unsigned int load_all_dies : 1;
828
0186c6a7
DE
829 /* Non-zero if this CU is from .debug_types.
830 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
831 this is non-zero. */
3019eac3
DE
832 unsigned int is_debug_types : 1;
833
36586728
TT
834 /* Non-zero if this CU is from the .dwz file. */
835 unsigned int is_dwz : 1;
836
a2ce51a0
DE
837 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
838 This flag is only valid if is_debug_types is true.
839 We can't read a CU directly from a DWO file: There are required
840 attributes in the stub. */
841 unsigned int reading_dwo_directly : 1;
842
7ee85ab1
DE
843 /* Non-zero if the TU has been read.
844 This is used to assist the "Stay in DWO Optimization" for Fission:
845 When reading a DWO, it's faster to read TUs from the DWO instead of
846 fetching them from random other DWOs (due to comdat folding).
847 If the TU has already been read, the optimization is unnecessary
848 (and unwise - we don't want to change where gdb thinks the TU lives
849 "midflight").
850 This flag is only valid if is_debug_types is true. */
851 unsigned int tu_read : 1;
852
3019eac3
DE
853 /* The section this CU/TU lives in.
854 If the DIE refers to a DWO file, this is always the original die,
855 not the DWO file. */
8a0459fd 856 struct dwarf2_section_info *section;
348e048f 857
17ea53c3 858 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
cc12ce38
DE
859 of the CU cache it gets reset to NULL again. This is left as NULL for
860 dummy CUs (a CU header, but nothing else). */
ae038cb0 861 struct dwarf2_cu *cu;
1c379e20 862
e3b94546
SM
863 /* The corresponding dwarf2_per_objfile. */
864 struct dwarf2_per_objfile *dwarf2_per_objfile;
9291a0cd 865
fffbe6a8
YQ
866 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
867 is active. Otherwise, the 'psymtab' field is active. */
9291a0cd
TT
868 union
869 {
870 /* The partial symbol table associated with this compilation unit,
95554aad 871 or NULL for unread partial units. */
9291a0cd
TT
872 struct partial_symtab *psymtab;
873
874 /* Data needed by the "quick" functions. */
875 struct dwarf2_per_cu_quick_data *quick;
876 } v;
95554aad 877
796a7ff8
DE
878 /* The CUs we import using DW_TAG_imported_unit. This is filled in
879 while reading psymtabs, used to compute the psymtab dependencies,
880 and then cleared. Then it is filled in again while reading full
881 symbols, and only deleted when the objfile is destroyed.
882
883 This is also used to work around a difference between the way gold
884 generates .gdb_index version <=7 and the way gdb does. Arguably this
885 is a gold bug. For symbols coming from TUs, gold records in the index
886 the CU that includes the TU instead of the TU itself. This breaks
887 dw2_lookup_symbol: It assumes that if the index says symbol X lives
888 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
889 will find X. Alas TUs live in their own symtab, so after expanding CU Y
890 we need to look in TU Z to find X. Fortunately, this is akin to
891 DW_TAG_imported_unit, so we just use the same mechanism: For
892 .gdb_index version <=7 this also records the TUs that the CU referred
893 to. Concurrently with this change gdb was modified to emit version 8
69d751e3
DE
894 indices so we only pay a price for gold generated indices.
895 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
796a7ff8 896 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
897};
898
348e048f
DE
899/* Entry in the signatured_types hash table. */
900
901struct signatured_type
902{
42e7ad6c 903 /* The "per_cu" object of this type.
ac9ec31b 904 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
905 N.B.: This is the first member so that it's easy to convert pointers
906 between them. */
907 struct dwarf2_per_cu_data per_cu;
908
3019eac3 909 /* The type's signature. */
348e048f
DE
910 ULONGEST signature;
911
3019eac3 912 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
913 If this TU is a DWO stub and the definition lives in a DWO file
914 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
915 cu_offset type_offset_in_tu;
916
917 /* Offset in the section of the type's DIE.
918 If the definition lives in a DWO file, this is the offset in the
919 .debug_types.dwo section.
920 The value is zero until the actual value is known.
921 Zero is otherwise not a valid section offset. */
922 sect_offset type_offset_in_section;
0186c6a7
DE
923
924 /* Type units are grouped by their DW_AT_stmt_list entry so that they
925 can share them. This points to the containing symtab. */
926 struct type_unit_group *type_unit_group;
ac9ec31b
DE
927
928 /* The type.
929 The first time we encounter this type we fully read it in and install it
930 in the symbol tables. Subsequent times we only need the type. */
931 struct type *type;
a2ce51a0
DE
932
933 /* Containing DWO unit.
934 This field is valid iff per_cu.reading_dwo_directly. */
935 struct dwo_unit *dwo_unit;
348e048f
DE
936};
937
0186c6a7
DE
938typedef struct signatured_type *sig_type_ptr;
939DEF_VEC_P (sig_type_ptr);
940
094b34ac
DE
941/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
942 This includes type_unit_group and quick_file_names. */
943
944struct stmt_list_hash
945{
946 /* The DWO unit this table is from or NULL if there is none. */
947 struct dwo_unit *dwo_unit;
948
949 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 950 sect_offset line_sect_off;
094b34ac
DE
951};
952
f4dc4d17
DE
953/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
954 an object of this type. */
955
956struct type_unit_group
957{
0186c6a7 958 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
959 To simplify things we create an artificial CU that "includes" all the
960 type units using this stmt_list so that the rest of the code still has
961 a "per_cu" handle on the symtab.
962 This PER_CU is recognized by having no section. */
8a0459fd 963#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
964 struct dwarf2_per_cu_data per_cu;
965
0186c6a7
DE
966 /* The TUs that share this DW_AT_stmt_list entry.
967 This is added to while parsing type units to build partial symtabs,
968 and is deleted afterwards and not used again. */
969 VEC (sig_type_ptr) *tus;
f4dc4d17 970
43f3e411 971 /* The compunit symtab.
094b34ac 972 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
973 so we create an essentially anonymous symtab as the compunit symtab. */
974 struct compunit_symtab *compunit_symtab;
f4dc4d17 975
094b34ac
DE
976 /* The data used to construct the hash key. */
977 struct stmt_list_hash hash;
f4dc4d17
DE
978
979 /* The number of symtabs from the line header.
980 The value here must match line_header.num_file_names. */
981 unsigned int num_symtabs;
982
983 /* The symbol tables for this TU (obtained from the files listed in
984 DW_AT_stmt_list).
985 WARNING: The order of entries here must match the order of entries
986 in the line header. After the first TU using this type_unit_group, the
987 line header for the subsequent TUs is recreated from this. This is done
988 because we need to use the same symtabs for each TU using the same
989 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
990 there's no guarantee the line header doesn't have duplicate entries. */
991 struct symtab **symtabs;
992};
993
73869dc2 994/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
995
996struct dwo_sections
997{
998 struct dwarf2_section_info abbrev;
3019eac3
DE
999 struct dwarf2_section_info line;
1000 struct dwarf2_section_info loc;
43988095 1001 struct dwarf2_section_info loclists;
09262596
DE
1002 struct dwarf2_section_info macinfo;
1003 struct dwarf2_section_info macro;
3019eac3
DE
1004 struct dwarf2_section_info str;
1005 struct dwarf2_section_info str_offsets;
80626a55
DE
1006 /* In the case of a virtual DWO file, these two are unused. */
1007 struct dwarf2_section_info info;
3019eac3
DE
1008 VEC (dwarf2_section_info_def) *types;
1009};
1010
c88ee1f0 1011/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
1012
1013struct dwo_unit
1014{
1015 /* Backlink to the containing struct dwo_file. */
1016 struct dwo_file *dwo_file;
1017
1018 /* The "id" that distinguishes this CU/TU.
1019 .debug_info calls this "dwo_id", .debug_types calls this "signature".
1020 Since signatures came first, we stick with it for consistency. */
1021 ULONGEST signature;
1022
1023 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 1024 struct dwarf2_section_info *section;
3019eac3 1025
9c541725
PA
1026 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
1027 sect_offset sect_off;
3019eac3
DE
1028 unsigned int length;
1029
1030 /* For types, offset in the type's DIE of the type defined by this TU. */
1031 cu_offset type_offset_in_tu;
1032};
1033
73869dc2
DE
1034/* include/dwarf2.h defines the DWP section codes.
1035 It defines a max value but it doesn't define a min value, which we
1036 use for error checking, so provide one. */
1037
1038enum dwp_v2_section_ids
1039{
1040 DW_SECT_MIN = 1
1041};
1042
80626a55 1043/* Data for one DWO file.
57d63ce2
DE
1044
1045 This includes virtual DWO files (a virtual DWO file is a DWO file as it
1046 appears in a DWP file). DWP files don't really have DWO files per se -
1047 comdat folding of types "loses" the DWO file they came from, and from
1048 a high level view DWP files appear to contain a mass of random types.
1049 However, to maintain consistency with the non-DWP case we pretend DWP
1050 files contain virtual DWO files, and we assign each TU with one virtual
1051 DWO file (generally based on the line and abbrev section offsets -
1052 a heuristic that seems to work in practice). */
3019eac3
DE
1053
1054struct dwo_file
1055{
0ac5b59e 1056 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
1057 For virtual DWO files the name is constructed from the section offsets
1058 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
1059 from related CU+TUs. */
0ac5b59e
DE
1060 const char *dwo_name;
1061
1062 /* The DW_AT_comp_dir attribute. */
1063 const char *comp_dir;
3019eac3 1064
80626a55
DE
1065 /* The bfd, when the file is open. Otherwise this is NULL.
1066 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
1067 bfd *dbfd;
3019eac3 1068
73869dc2
DE
1069 /* The sections that make up this DWO file.
1070 Remember that for virtual DWO files in DWP V2, these are virtual
1071 sections (for lack of a better name). */
3019eac3
DE
1072 struct dwo_sections sections;
1073
33c5cd75
DB
1074 /* The CUs in the file.
1075 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
1076 an extension to handle LLVM's Link Time Optimization output (where
1077 multiple source files may be compiled into a single object/dwo pair). */
1078 htab_t cus;
3019eac3
DE
1079
1080 /* Table of TUs in the file.
1081 Each element is a struct dwo_unit. */
1082 htab_t tus;
1083};
1084
80626a55
DE
1085/* These sections are what may appear in a DWP file. */
1086
1087struct dwp_sections
1088{
73869dc2 1089 /* These are used by both DWP version 1 and 2. */
80626a55
DE
1090 struct dwarf2_section_info str;
1091 struct dwarf2_section_info cu_index;
1092 struct dwarf2_section_info tu_index;
73869dc2
DE
1093
1094 /* These are only used by DWP version 2 files.
1095 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
1096 sections are referenced by section number, and are not recorded here.
1097 In DWP version 2 there is at most one copy of all these sections, each
1098 section being (effectively) comprised of the concatenation of all of the
1099 individual sections that exist in the version 1 format.
1100 To keep the code simple we treat each of these concatenated pieces as a
1101 section itself (a virtual section?). */
1102 struct dwarf2_section_info abbrev;
1103 struct dwarf2_section_info info;
1104 struct dwarf2_section_info line;
1105 struct dwarf2_section_info loc;
1106 struct dwarf2_section_info macinfo;
1107 struct dwarf2_section_info macro;
1108 struct dwarf2_section_info str_offsets;
1109 struct dwarf2_section_info types;
80626a55
DE
1110};
1111
73869dc2
DE
1112/* These sections are what may appear in a virtual DWO file in DWP version 1.
1113 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 1114
73869dc2 1115struct virtual_v1_dwo_sections
80626a55
DE
1116{
1117 struct dwarf2_section_info abbrev;
1118 struct dwarf2_section_info line;
1119 struct dwarf2_section_info loc;
1120 struct dwarf2_section_info macinfo;
1121 struct dwarf2_section_info macro;
1122 struct dwarf2_section_info str_offsets;
1123 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 1124 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
1125 struct dwarf2_section_info info_or_types;
1126};
1127
73869dc2
DE
1128/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
1129 In version 2, the sections of the DWO files are concatenated together
1130 and stored in one section of that name. Thus each ELF section contains
1131 several "virtual" sections. */
1132
1133struct virtual_v2_dwo_sections
1134{
1135 bfd_size_type abbrev_offset;
1136 bfd_size_type abbrev_size;
1137
1138 bfd_size_type line_offset;
1139 bfd_size_type line_size;
1140
1141 bfd_size_type loc_offset;
1142 bfd_size_type loc_size;
1143
1144 bfd_size_type macinfo_offset;
1145 bfd_size_type macinfo_size;
1146
1147 bfd_size_type macro_offset;
1148 bfd_size_type macro_size;
1149
1150 bfd_size_type str_offsets_offset;
1151 bfd_size_type str_offsets_size;
1152
1153 /* Each DWP hash table entry records one CU or one TU.
1154 That is recorded here, and copied to dwo_unit.section. */
1155 bfd_size_type info_or_types_offset;
1156 bfd_size_type info_or_types_size;
1157};
1158
80626a55
DE
1159/* Contents of DWP hash tables. */
1160
1161struct dwp_hash_table
1162{
73869dc2 1163 uint32_t version, nr_columns;
80626a55 1164 uint32_t nr_units, nr_slots;
73869dc2
DE
1165 const gdb_byte *hash_table, *unit_table;
1166 union
1167 {
1168 struct
1169 {
1170 const gdb_byte *indices;
1171 } v1;
1172 struct
1173 {
1174 /* This is indexed by column number and gives the id of the section
1175 in that column. */
1176#define MAX_NR_V2_DWO_SECTIONS \
1177 (1 /* .debug_info or .debug_types */ \
1178 + 1 /* .debug_abbrev */ \
1179 + 1 /* .debug_line */ \
1180 + 1 /* .debug_loc */ \
1181 + 1 /* .debug_str_offsets */ \
1182 + 1 /* .debug_macro or .debug_macinfo */)
1183 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1184 const gdb_byte *offsets;
1185 const gdb_byte *sizes;
1186 } v2;
1187 } section_pool;
80626a55
DE
1188};
1189
1190/* Data for one DWP file. */
1191
1192struct dwp_file
1193{
1194 /* Name of the file. */
1195 const char *name;
1196
73869dc2
DE
1197 /* File format version. */
1198 int version;
1199
93417882 1200 /* The bfd. */
80626a55
DE
1201 bfd *dbfd;
1202
1203 /* Section info for this file. */
1204 struct dwp_sections sections;
1205
57d63ce2 1206 /* Table of CUs in the file. */
80626a55
DE
1207 const struct dwp_hash_table *cus;
1208
1209 /* Table of TUs in the file. */
1210 const struct dwp_hash_table *tus;
1211
19ac8c2e
DE
1212 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1213 htab_t loaded_cus;
1214 htab_t loaded_tus;
80626a55 1215
73869dc2
DE
1216 /* Table to map ELF section numbers to their sections.
1217 This is only needed for the DWP V1 file format. */
80626a55
DE
1218 unsigned int num_sections;
1219 asection **elf_sections;
1220};
1221
36586728
TT
1222/* This represents a '.dwz' file. */
1223
1224struct dwz_file
1225{
1226 /* A dwz file can only contain a few sections. */
1227 struct dwarf2_section_info abbrev;
1228 struct dwarf2_section_info info;
1229 struct dwarf2_section_info str;
1230 struct dwarf2_section_info line;
1231 struct dwarf2_section_info macro;
2ec9a5e0 1232 struct dwarf2_section_info gdb_index;
927aa2e7 1233 struct dwarf2_section_info debug_names;
36586728
TT
1234
1235 /* The dwz's BFD. */
1236 bfd *dwz_bfd;
1237};
1238
0963b4bd
MS
1239/* Struct used to pass misc. parameters to read_die_and_children, et
1240 al. which are used for both .debug_info and .debug_types dies.
1241 All parameters here are unchanging for the life of the call. This
dee91e82 1242 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
1243
1244struct die_reader_specs
1245{
a32a8923 1246 /* The bfd of die_section. */
93311388
DE
1247 bfd* abfd;
1248
1249 /* The CU of the DIE we are parsing. */
1250 struct dwarf2_cu *cu;
1251
80626a55 1252 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
1253 struct dwo_file *dwo_file;
1254
dee91e82 1255 /* The section the die comes from.
3019eac3 1256 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
1257 struct dwarf2_section_info *die_section;
1258
1259 /* die_section->buffer. */
d521ce57 1260 const gdb_byte *buffer;
f664829e
DE
1261
1262 /* The end of the buffer. */
1263 const gdb_byte *buffer_end;
a2ce51a0
DE
1264
1265 /* The value of the DW_AT_comp_dir attribute. */
1266 const char *comp_dir;
685af9cd
TT
1267
1268 /* The abbreviation table to use when reading the DIEs. */
1269 struct abbrev_table *abbrev_table;
93311388
DE
1270};
1271
fd820528 1272/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 1273typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 1274 const gdb_byte *info_ptr,
dee91e82
DE
1275 struct die_info *comp_unit_die,
1276 int has_children,
1277 void *data);
1278
ecfb656c
PA
1279/* A 1-based directory index. This is a strong typedef to prevent
1280 accidentally using a directory index as a 0-based index into an
1281 array/vector. */
1282enum class dir_index : unsigned int {};
1283
1284/* Likewise, a 1-based file name index. */
1285enum class file_name_index : unsigned int {};
1286
52059ffd
TT
1287struct file_entry
1288{
fff8551c
PA
1289 file_entry () = default;
1290
ecfb656c 1291 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
1292 unsigned int mod_time_, unsigned int length_)
1293 : name (name_),
ecfb656c 1294 d_index (d_index_),
fff8551c
PA
1295 mod_time (mod_time_),
1296 length (length_)
1297 {}
1298
ecfb656c
PA
1299 /* Return the include directory at D_INDEX stored in LH. Returns
1300 NULL if D_INDEX is out of bounds. */
8c43009f
PA
1301 const char *include_dir (const line_header *lh) const;
1302
fff8551c
PA
1303 /* The file name. Note this is an observing pointer. The memory is
1304 owned by debug_line_buffer. */
1305 const char *name {};
1306
8c43009f 1307 /* The directory index (1-based). */
ecfb656c 1308 dir_index d_index {};
fff8551c
PA
1309
1310 unsigned int mod_time {};
1311
1312 unsigned int length {};
1313
1314 /* True if referenced by the Line Number Program. */
1315 bool included_p {};
1316
83769d0b 1317 /* The associated symbol table, if any. */
fff8551c 1318 struct symtab *symtab {};
52059ffd
TT
1319};
1320
debd256d
JB
1321/* The line number information for a compilation unit (found in the
1322 .debug_line section) begins with a "statement program header",
1323 which contains the following information. */
1324struct line_header
1325{
fff8551c
PA
1326 line_header ()
1327 : offset_in_dwz {}
1328 {}
1329
1330 /* Add an entry to the include directory table. */
1331 void add_include_dir (const char *include_dir);
1332
1333 /* Add an entry to the file name table. */
ecfb656c 1334 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
1335 unsigned int mod_time, unsigned int length);
1336
ecfb656c 1337 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
8c43009f 1338 is out of bounds. */
ecfb656c 1339 const char *include_dir_at (dir_index index) const
8c43009f 1340 {
ecfb656c
PA
1341 /* Convert directory index number (1-based) to vector index
1342 (0-based). */
1343 size_t vec_index = to_underlying (index) - 1;
1344
1345 if (vec_index >= include_dirs.size ())
8c43009f 1346 return NULL;
ecfb656c 1347 return include_dirs[vec_index];
8c43009f
PA
1348 }
1349
ecfb656c 1350 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
8c43009f 1351 is out of bounds. */
ecfb656c 1352 file_entry *file_name_at (file_name_index index)
8c43009f 1353 {
ecfb656c
PA
1354 /* Convert file name index number (1-based) to vector index
1355 (0-based). */
1356 size_t vec_index = to_underlying (index) - 1;
1357
1358 if (vec_index >= file_names.size ())
fff8551c 1359 return NULL;
ecfb656c 1360 return &file_names[vec_index];
fff8551c
PA
1361 }
1362
1363 /* Const version of the above. */
1364 const file_entry *file_name_at (unsigned int index) const
1365 {
1366 if (index >= file_names.size ())
8c43009f
PA
1367 return NULL;
1368 return &file_names[index];
1369 }
1370
527f3840 1371 /* Offset of line number information in .debug_line section. */
9c541725 1372 sect_offset sect_off {};
527f3840
JK
1373
1374 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1375 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1376
1377 unsigned int total_length {};
1378 unsigned short version {};
1379 unsigned int header_length {};
1380 unsigned char minimum_instruction_length {};
1381 unsigned char maximum_ops_per_instruction {};
1382 unsigned char default_is_stmt {};
1383 int line_base {};
1384 unsigned char line_range {};
1385 unsigned char opcode_base {};
debd256d
JB
1386
1387 /* standard_opcode_lengths[i] is the number of operands for the
1388 standard opcode whose value is i. This means that
1389 standard_opcode_lengths[0] is unused, and the last meaningful
1390 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1391 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1392
fff8551c
PA
1393 /* The include_directories table. Note these are observing
1394 pointers. The memory is owned by debug_line_buffer. */
1395 std::vector<const char *> include_dirs;
debd256d 1396
fff8551c
PA
1397 /* The file_names table. */
1398 std::vector<file_entry> file_names;
debd256d
JB
1399
1400 /* The start and end of the statement program following this
6502dd73 1401 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1402 const gdb_byte *statement_program_start {}, *statement_program_end {};
debd256d 1403};
c906108c 1404
fff8551c
PA
1405typedef std::unique_ptr<line_header> line_header_up;
1406
8c43009f
PA
1407const char *
1408file_entry::include_dir (const line_header *lh) const
1409{
ecfb656c 1410 return lh->include_dir_at (d_index);
8c43009f
PA
1411}
1412
c906108c 1413/* When we construct a partial symbol table entry we only
0963b4bd 1414 need this much information. */
6f06d47b 1415struct partial_die_info : public allocate_on_obstack
c906108c 1416 {
6f06d47b
YQ
1417 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1418
1419 /* Disable assign but still keep copy ctor, which is needed
1420 load_partial_dies. */
1421 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1422
52356b79
YQ
1423 /* Adjust the partial die before generating a symbol for it. This
1424 function may set the is_external flag or change the DIE's
1425 name. */
1426 void fixup (struct dwarf2_cu *cu);
1427
48fbe735
YQ
1428 /* Read a minimal amount of information into the minimal die
1429 structure. */
1430 const gdb_byte *read (const struct die_reader_specs *reader,
1431 const struct abbrev_info &abbrev,
1432 const gdb_byte *info_ptr);
1433
72bf9492 1434 /* Offset of this DIE. */
6f06d47b 1435 const sect_offset sect_off;
72bf9492
DJ
1436
1437 /* DWARF-2 tag for this DIE. */
6f06d47b 1438 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1439
72bf9492 1440 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1441 const unsigned int has_children : 1;
1442
72bf9492
DJ
1443 unsigned int is_external : 1;
1444 unsigned int is_declaration : 1;
1445 unsigned int has_type : 1;
1446 unsigned int has_specification : 1;
1447 unsigned int has_pc_info : 1;
481860b3 1448 unsigned int may_be_inlined : 1;
72bf9492 1449
0c1b455e
TT
1450 /* This DIE has been marked DW_AT_main_subprogram. */
1451 unsigned int main_subprogram : 1;
1452
72bf9492
DJ
1453 /* Flag set if the SCOPE field of this structure has been
1454 computed. */
1455 unsigned int scope_set : 1;
1456
fa4028e9
JB
1457 /* Flag set if the DIE has a byte_size attribute. */
1458 unsigned int has_byte_size : 1;
1459
ff908ebf
AW
1460 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1461 unsigned int has_const_value : 1;
1462
98bfdba5
PA
1463 /* Flag set if any of the DIE's children are template arguments. */
1464 unsigned int has_template_arguments : 1;
1465
52356b79 1466 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1467 unsigned int fixup_called : 1;
1468
36586728
TT
1469 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1470 unsigned int is_dwz : 1;
1471
1472 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1473 unsigned int spec_is_dwz : 1;
1474
72bf9492 1475 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1476 sometimes a default name for unnamed DIEs. */
6f06d47b 1477 const char *name = nullptr;
72bf9492 1478
abc72ce4 1479 /* The linkage name, if present. */
6f06d47b 1480 const char *linkage_name = nullptr;
abc72ce4 1481
72bf9492
DJ
1482 /* The scope to prepend to our children. This is generally
1483 allocated on the comp_unit_obstack, so will disappear
1484 when this compilation unit leaves the cache. */
6f06d47b 1485 const char *scope = nullptr;
72bf9492 1486
95554aad
TT
1487 /* Some data associated with the partial DIE. The tag determines
1488 which field is live. */
1489 union
1490 {
1491 /* The location description associated with this DIE, if any. */
1492 struct dwarf_block *locdesc;
1493 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1494 sect_offset sect_off;
6f06d47b 1495 } d {};
72bf9492
DJ
1496
1497 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1498 CORE_ADDR lowpc = 0;
1499 CORE_ADDR highpc = 0;
72bf9492 1500
93311388 1501 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1502 DW_AT_sibling, if any. */
48fbe735
YQ
1503 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1504 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1505 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1506
1507 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1508 DW_AT_specification (or DW_AT_abstract_origin or
1509 DW_AT_extension). */
6f06d47b 1510 sect_offset spec_offset {};
72bf9492
DJ
1511
1512 /* Pointers to this DIE's parent, first child, and next sibling,
1513 if any. */
6f06d47b
YQ
1514 struct partial_die_info *die_parent = nullptr;
1515 struct partial_die_info *die_child = nullptr;
1516 struct partial_die_info *die_sibling = nullptr;
1517
1518 friend struct partial_die_info *
1519 dwarf2_cu::find_partial_die (sect_offset sect_off);
1520
1521 private:
1522 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1523 partial_die_info (sect_offset sect_off)
1524 : partial_die_info (sect_off, DW_TAG_padding, 0)
1525 {
1526 }
1527
1528 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1529 int has_children_)
1530 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1531 {
1532 is_external = 0;
1533 is_declaration = 0;
1534 has_type = 0;
1535 has_specification = 0;
1536 has_pc_info = 0;
1537 may_be_inlined = 0;
1538 main_subprogram = 0;
1539 scope_set = 0;
1540 has_byte_size = 0;
1541 has_const_value = 0;
1542 has_template_arguments = 0;
1543 fixup_called = 0;
1544 is_dwz = 0;
1545 spec_is_dwz = 0;
1546 }
c906108c
SS
1547 };
1548
0963b4bd 1549/* This data structure holds the information of an abbrev. */
c906108c
SS
1550struct abbrev_info
1551 {
1552 unsigned int number; /* number identifying abbrev */
1553 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1554 unsigned short has_children; /* boolean */
1555 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1556 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1557 struct abbrev_info *next; /* next in chain */
1558 };
1559
1560struct attr_abbrev
1561 {
9d25dd43
DE
1562 ENUM_BITFIELD(dwarf_attribute) name : 16;
1563 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1564
1565 /* It is valid only if FORM is DW_FORM_implicit_const. */
1566 LONGEST implicit_const;
c906108c
SS
1567 };
1568
433df2d4
DE
1569/* Size of abbrev_table.abbrev_hash_table. */
1570#define ABBREV_HASH_SIZE 121
1571
1572/* Top level data structure to contain an abbreviation table. */
1573
1574struct abbrev_table
1575{
685af9cd
TT
1576 explicit abbrev_table (sect_offset off)
1577 : sect_off (off)
1578 {
4a17f768 1579 m_abbrevs =
685af9cd 1580 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
4a17f768 1581 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
685af9cd
TT
1582 }
1583
1584 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1585
1586 /* Allocate space for a struct abbrev_info object in
1587 ABBREV_TABLE. */
1588 struct abbrev_info *alloc_abbrev ();
1589
1590 /* Add an abbreviation to the table. */
1591 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1592
1593 /* Look up an abbrev in the table.
1594 Returns NULL if the abbrev is not found. */
1595
1596 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1597
1598
f4dc4d17
DE
1599 /* Where the abbrev table came from.
1600 This is used as a sanity check when the table is used. */
685af9cd 1601 const sect_offset sect_off;
433df2d4
DE
1602
1603 /* Storage for the abbrev table. */
685af9cd 1604 auto_obstack abbrev_obstack;
433df2d4 1605
4a17f768
YQ
1606private:
1607
433df2d4
DE
1608 /* Hash table of abbrevs.
1609 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1610 It could be statically allocated, but the previous code didn't so we
1611 don't either. */
4a17f768 1612 struct abbrev_info **m_abbrevs;
433df2d4
DE
1613};
1614
685af9cd
TT
1615typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1616
0963b4bd 1617/* Attributes have a name and a value. */
b60c80d6
DJ
1618struct attribute
1619 {
9d25dd43 1620 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1621 ENUM_BITFIELD(dwarf_form) form : 15;
1622
1623 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1624 field should be in u.str (existing only for DW_STRING) but it is kept
1625 here for better struct attribute alignment. */
1626 unsigned int string_is_canonical : 1;
1627
b60c80d6
DJ
1628 union
1629 {
15d034d0 1630 const char *str;
b60c80d6 1631 struct dwarf_block *blk;
43bbcdc2
PH
1632 ULONGEST unsnd;
1633 LONGEST snd;
b60c80d6 1634 CORE_ADDR addr;
ac9ec31b 1635 ULONGEST signature;
b60c80d6
DJ
1636 }
1637 u;
1638 };
1639
0963b4bd 1640/* This data structure holds a complete die structure. */
c906108c
SS
1641struct die_info
1642 {
76815b17
DE
1643 /* DWARF-2 tag for this DIE. */
1644 ENUM_BITFIELD(dwarf_tag) tag : 16;
1645
1646 /* Number of attributes */
98bfdba5
PA
1647 unsigned char num_attrs;
1648
1649 /* True if we're presently building the full type name for the
1650 type derived from this DIE. */
1651 unsigned char building_fullname : 1;
76815b17 1652
adde2bff
DE
1653 /* True if this die is in process. PR 16581. */
1654 unsigned char in_process : 1;
1655
76815b17
DE
1656 /* Abbrev number */
1657 unsigned int abbrev;
1658
93311388 1659 /* Offset in .debug_info or .debug_types section. */
9c541725 1660 sect_offset sect_off;
78ba4af6
JB
1661
1662 /* The dies in a compilation unit form an n-ary tree. PARENT
1663 points to this die's parent; CHILD points to the first child of
1664 this node; and all the children of a given node are chained
4950bc1c 1665 together via their SIBLING fields. */
639d11d3
DC
1666 struct die_info *child; /* Its first child, if any. */
1667 struct die_info *sibling; /* Its next sibling, if any. */
1668 struct die_info *parent; /* Its parent, if any. */
c906108c 1669
b60c80d6
DJ
1670 /* An array of attributes, with NUM_ATTRS elements. There may be
1671 zero, but it's not common and zero-sized arrays are not
1672 sufficiently portable C. */
1673 struct attribute attrs[1];
c906108c
SS
1674 };
1675
0963b4bd 1676/* Get at parts of an attribute structure. */
c906108c
SS
1677
1678#define DW_STRING(attr) ((attr)->u.str)
8285870a 1679#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1680#define DW_UNSND(attr) ((attr)->u.unsnd)
1681#define DW_BLOCK(attr) ((attr)->u.blk)
1682#define DW_SND(attr) ((attr)->u.snd)
1683#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1684#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1685
0963b4bd 1686/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1687struct dwarf_block
1688 {
56eb65bd 1689 size_t size;
1d6edc3c
JK
1690
1691 /* Valid only if SIZE is not zero. */
d521ce57 1692 const gdb_byte *data;
c906108c
SS
1693 };
1694
c906108c
SS
1695#ifndef ATTR_ALLOC_CHUNK
1696#define ATTR_ALLOC_CHUNK 4
1697#endif
1698
c906108c
SS
1699/* Allocate fields for structs, unions and enums in this size. */
1700#ifndef DW_FIELD_ALLOC_CHUNK
1701#define DW_FIELD_ALLOC_CHUNK 4
1702#endif
1703
c906108c
SS
1704/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1705 but this would require a corresponding change in unpack_field_as_long
1706 and friends. */
1707static int bits_per_byte = 8;
1708
2ddeaf8a
TT
1709/* When reading a variant or variant part, we track a bit more
1710 information about the field, and store it in an object of this
1711 type. */
1712
1713struct variant_field
1714{
1715 /* If we see a DW_TAG_variant, then this will be the discriminant
1716 value. */
1717 ULONGEST discriminant_value;
1718 /* If we see a DW_TAG_variant, then this will be set if this is the
1719 default branch. */
1720 bool default_branch;
1721 /* While reading a DW_TAG_variant_part, this will be set if this
1722 field is the discriminant. */
1723 bool is_discriminant;
1724};
1725
52059ffd
TT
1726struct nextfield
1727{
be2daae6
TT
1728 int accessibility = 0;
1729 int virtuality = 0;
2ddeaf8a 1730 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1731 struct variant_field variant {};
1732 struct field field {};
52059ffd
TT
1733};
1734
1735struct fnfieldlist
1736{
be2daae6
TT
1737 const char *name = nullptr;
1738 std::vector<struct fn_field> fnfields;
52059ffd
TT
1739};
1740
c906108c
SS
1741/* The routines that read and process dies for a C struct or C++ class
1742 pass lists of data member fields and lists of member function fields
1743 in an instance of a field_info structure, as defined below. */
1744struct field_info
c5aa993b 1745 {
0963b4bd 1746 /* List of data member and baseclasses fields. */
be2daae6
TT
1747 std::vector<struct nextfield> fields;
1748 std::vector<struct nextfield> baseclasses;
c906108c 1749
7d0ccb61 1750 /* Number of fields (including baseclasses). */
be2daae6 1751 int nfields = 0;
c906108c 1752
c5aa993b 1753 /* Set if the accesibility of one of the fields is not public. */
be2daae6 1754 int non_public_fields = 0;
c906108c 1755
c5aa993b
JM
1756 /* Member function fieldlist array, contains name of possibly overloaded
1757 member function, number of overloaded member functions and a pointer
1758 to the head of the member function field chain. */
be2daae6 1759 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1760
1761 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1762 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1763 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1764
1765 /* Nested types defined by this class and the number of elements in this
1766 list. */
be2daae6 1767 std::vector<struct decl_field> nested_types_list;
c5aa993b 1768 };
c906108c 1769
10b3939b
DJ
1770/* One item on the queue of compilation units to read in full symbols
1771 for. */
1772struct dwarf2_queue_item
1773{
1774 struct dwarf2_per_cu_data *per_cu;
95554aad 1775 enum language pretend_language;
10b3939b
DJ
1776 struct dwarf2_queue_item *next;
1777};
1778
1779/* The current queue. */
1780static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1781
ae038cb0
DJ
1782/* Loaded secondary compilation units are kept in memory until they
1783 have not been referenced for the processing of this many
1784 compilation units. Set this to zero to disable caching. Cache
1785 sizes of up to at least twenty will improve startup time for
1786 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1787static int dwarf_max_cache_age = 5;
920d2a44 1788static void
b4f54984
DE
1789show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1790 struct cmd_list_element *c, const char *value)
920d2a44 1791{
3e43a32a 1792 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1793 "DWARF compilation units is %s.\n"),
920d2a44
AC
1794 value);
1795}
4390d890 1796\f
c906108c
SS
1797/* local function prototypes */
1798
a32a8923
DE
1799static const char *get_section_name (const struct dwarf2_section_info *);
1800
1801static const char *get_section_file_name (const struct dwarf2_section_info *);
1802
918dd910
JK
1803static void dwarf2_find_base_address (struct die_info *die,
1804 struct dwarf2_cu *cu);
1805
0018ea6f
DE
1806static struct partial_symtab *create_partial_symtab
1807 (struct dwarf2_per_cu_data *per_cu, const char *name);
1808
f1902523
JK
1809static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1810 const gdb_byte *info_ptr,
1811 struct die_info *type_unit_die,
1812 int has_children, void *data);
1813
ed2dc618
SM
1814static void dwarf2_build_psymtabs_hard
1815 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1816
72bf9492
DJ
1817static void scan_partial_symbols (struct partial_die_info *,
1818 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1819 int, struct dwarf2_cu *);
c906108c 1820
72bf9492
DJ
1821static void add_partial_symbol (struct partial_die_info *,
1822 struct dwarf2_cu *);
63d06c5c 1823
72bf9492
DJ
1824static void add_partial_namespace (struct partial_die_info *pdi,
1825 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1826 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1827
5d7cb8df 1828static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1829 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1830 struct dwarf2_cu *cu);
1831
72bf9492
DJ
1832static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1833 struct dwarf2_cu *cu);
91c24f0a 1834
bc30ff58
JB
1835static void add_partial_subprogram (struct partial_die_info *pdi,
1836 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1837 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1838
257e7a09
YQ
1839static void dwarf2_read_symtab (struct partial_symtab *,
1840 struct objfile *);
c906108c 1841
a14ed312 1842static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1843
685af9cd 1844static abbrev_table_up abbrev_table_read_table
ed2dc618
SM
1845 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1846 sect_offset);
433df2d4 1847
d521ce57 1848static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1849
dee91e82 1850static struct partial_die_info *load_partial_dies
d521ce57 1851 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1852
36586728 1853static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1854 struct dwarf2_cu *);
72bf9492 1855
d521ce57
TT
1856static const gdb_byte *read_attribute (const struct die_reader_specs *,
1857 struct attribute *, struct attr_abbrev *,
1858 const gdb_byte *);
a8329558 1859
a1855c1d 1860static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1861
a1855c1d 1862static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1863
a1855c1d 1864static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1865
a1855c1d 1866static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1867
a1855c1d 1868static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1869
d521ce57 1870static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1871 unsigned int *);
c906108c 1872
d521ce57 1873static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1874
1875static LONGEST read_checked_initial_length_and_offset
d521ce57 1876 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1877 unsigned int *, unsigned int *);
613e1657 1878
d521ce57
TT
1879static LONGEST read_offset (bfd *, const gdb_byte *,
1880 const struct comp_unit_head *,
c764a876
DE
1881 unsigned int *);
1882
d521ce57 1883static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1884
ed2dc618
SM
1885static sect_offset read_abbrev_offset
1886 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1887 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1888
d521ce57 1889static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1890
d521ce57 1891static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1892
ed2dc618
SM
1893static const char *read_indirect_string
1894 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1895 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1896
ed2dc618
SM
1897static const char *read_indirect_line_string
1898 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1899 const struct comp_unit_head *, unsigned int *);
36586728 1900
ed2dc618
SM
1901static const char *read_indirect_string_at_offset
1902 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1903 LONGEST str_offset);
927aa2e7 1904
ed2dc618
SM
1905static const char *read_indirect_string_from_dwz
1906 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1907
d521ce57 1908static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1909
d521ce57
TT
1910static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1911 const gdb_byte *,
3019eac3
DE
1912 unsigned int *);
1913
d521ce57 1914static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1915 ULONGEST str_index);
3019eac3 1916
e142c38c 1917static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1918
e142c38c
DJ
1919static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1920 struct dwarf2_cu *);
c906108c 1921
348e048f 1922static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1923 unsigned int);
348e048f 1924
7d45c7c3
KB
1925static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1926 struct dwarf2_cu *cu);
1927
05cf31d1
JB
1928static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1929 struct dwarf2_cu *cu);
1930
e142c38c 1931static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1932
e142c38c 1933static struct die_info *die_specification (struct die_info *die,
f2f0e013 1934 struct dwarf2_cu **);
63d06c5c 1935
9c541725 1936static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1937 struct dwarf2_cu *cu);
debd256d 1938
f3f5162e 1939static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1940 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1941 CORE_ADDR, int decode_mapping);
c906108c 1942
4d663531 1943static void dwarf2_start_subfile (const char *, const char *);
c906108c 1944
43f3e411
DE
1945static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1946 const char *, const char *,
1947 CORE_ADDR);
f4dc4d17 1948
a14ed312 1949static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1950 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1951
ff39bb5e 1952static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1953 struct dwarf2_cu *);
c906108c 1954
ff39bb5e 1955static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1956 struct type *type,
1957 const char *name,
1958 struct obstack *obstack,
12df843f 1959 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1960 const gdb_byte **bytes,
98bfdba5 1961 struct dwarf2_locexpr_baton **baton);
2df3850c 1962
e7c27a73 1963static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1964
b4ba55a1
JB
1965static int need_gnat_info (struct dwarf2_cu *);
1966
3e43a32a
MS
1967static struct type *die_descriptive_type (struct die_info *,
1968 struct dwarf2_cu *);
b4ba55a1
JB
1969
1970static void set_descriptive_type (struct type *, struct die_info *,
1971 struct dwarf2_cu *);
1972
e7c27a73
DJ
1973static struct type *die_containing_type (struct die_info *,
1974 struct dwarf2_cu *);
c906108c 1975
ff39bb5e 1976static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1977 struct dwarf2_cu *);
c906108c 1978
f792889a 1979static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1980
673bfd45
DE
1981static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1982
0d5cff50 1983static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1984
6e70227d 1985static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1986 const char *suffix, int physname,
1987 struct dwarf2_cu *cu);
63d06c5c 1988
e7c27a73 1989static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1990
348e048f
DE
1991static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1992
e7c27a73 1993static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1994
e7c27a73 1995static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1996
96408a79
SA
1997static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1998
71a3c369
TT
1999static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
2000
ff013f42
JK
2001static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
2002 struct dwarf2_cu *, struct partial_symtab *);
2003
3a2b436a 2004/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 2005 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
2006enum pc_bounds_kind
2007{
e385593e 2008 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
2009 PC_BOUNDS_NOT_PRESENT,
2010
e385593e
JK
2011 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
2012 were present but they do not form a valid range of PC addresses. */
2013 PC_BOUNDS_INVALID,
2014
3a2b436a
JK
2015 /* Discontiguous range was found - that is DW_AT_ranges was found. */
2016 PC_BOUNDS_RANGES,
2017
2018 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
2019 PC_BOUNDS_HIGH_LOW,
2020};
2021
2022static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
2023 CORE_ADDR *, CORE_ADDR *,
2024 struct dwarf2_cu *,
2025 struct partial_symtab *);
c906108c 2026
fae299cd
DC
2027static void get_scope_pc_bounds (struct die_info *,
2028 CORE_ADDR *, CORE_ADDR *,
2029 struct dwarf2_cu *);
2030
801e3a5b
JB
2031static void dwarf2_record_block_ranges (struct die_info *, struct block *,
2032 CORE_ADDR, struct dwarf2_cu *);
2033
a14ed312 2034static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 2035 struct dwarf2_cu *);
c906108c 2036
a14ed312 2037static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 2038 struct type *, struct dwarf2_cu *);
c906108c 2039
a14ed312 2040static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 2041 struct die_info *, struct type *,
e7c27a73 2042 struct dwarf2_cu *);
c906108c 2043
a14ed312 2044static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
2045 struct type *,
2046 struct dwarf2_cu *);
c906108c 2047
134d01f1 2048static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 2049
e7c27a73 2050static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 2051
e7c27a73 2052static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 2053
5d7cb8df
JK
2054static void read_module (struct die_info *die, struct dwarf2_cu *cu);
2055
22cee43f
PMR
2056static struct using_direct **using_directives (enum language);
2057
27aa8d6a
SW
2058static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
2059
74921315
KS
2060static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
2061
f55ee35c
JK
2062static struct type *read_module_type (struct die_info *die,
2063 struct dwarf2_cu *cu);
2064
38d518c9 2065static const char *namespace_name (struct die_info *die,
e142c38c 2066 int *is_anonymous, struct dwarf2_cu *);
38d518c9 2067
134d01f1 2068static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 2069
e7c27a73 2070static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 2071
6e70227d 2072static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
2073 struct dwarf2_cu *);
2074
bf6af496 2075static struct die_info *read_die_and_siblings_1
d521ce57 2076 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 2077 struct die_info *);
639d11d3 2078
dee91e82 2079static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
2080 const gdb_byte *info_ptr,
2081 const gdb_byte **new_info_ptr,
639d11d3
DC
2082 struct die_info *parent);
2083
d521ce57
TT
2084static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
2085 struct die_info **, const gdb_byte *,
2086 int *, int);
3019eac3 2087
d521ce57
TT
2088static const gdb_byte *read_full_die (const struct die_reader_specs *,
2089 struct die_info **, const gdb_byte *,
2090 int *);
93311388 2091
e7c27a73 2092static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 2093
15d034d0
TT
2094static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
2095 struct obstack *);
71c25dea 2096
15d034d0 2097static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 2098
15d034d0 2099static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
2100 struct die_info *die,
2101 struct dwarf2_cu *cu);
2102
ca69b9e6
DE
2103static const char *dwarf2_physname (const char *name, struct die_info *die,
2104 struct dwarf2_cu *cu);
2105
e142c38c 2106static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 2107 struct dwarf2_cu **);
9219021c 2108
f39c6ffd 2109static const char *dwarf_tag_name (unsigned int);
c906108c 2110
f39c6ffd 2111static const char *dwarf_attr_name (unsigned int);
c906108c 2112
f39c6ffd 2113static const char *dwarf_form_name (unsigned int);
c906108c 2114
a121b7c1 2115static const char *dwarf_bool_name (unsigned int);
c906108c 2116
f39c6ffd 2117static const char *dwarf_type_encoding_name (unsigned int);
c906108c 2118
f9aca02d 2119static struct die_info *sibling_die (struct die_info *);
c906108c 2120
d97bc12b
DE
2121static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
2122
2123static void dump_die_for_error (struct die_info *);
2124
2125static void dump_die_1 (struct ui_file *, int level, int max_level,
2126 struct die_info *);
c906108c 2127
d97bc12b 2128/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 2129
51545339 2130static void store_in_ref_table (struct die_info *,
10b3939b 2131 struct dwarf2_cu *);
c906108c 2132
ff39bb5e 2133static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 2134
ff39bb5e 2135static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 2136
348e048f 2137static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 2138 const struct attribute *,
348e048f
DE
2139 struct dwarf2_cu **);
2140
10b3939b 2141static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 2142 const struct attribute *,
f2f0e013 2143 struct dwarf2_cu **);
c906108c 2144
348e048f 2145static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 2146 const struct attribute *,
348e048f
DE
2147 struct dwarf2_cu **);
2148
ac9ec31b
DE
2149static struct type *get_signatured_type (struct die_info *, ULONGEST,
2150 struct dwarf2_cu *);
2151
2152static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 2153 const struct attribute *,
ac9ec31b
DE
2154 struct dwarf2_cu *);
2155
e5fe5e75 2156static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 2157
52dc124a 2158static void read_signatured_type (struct signatured_type *);
348e048f 2159
63e43d3a
PMR
2160static int attr_to_dynamic_prop (const struct attribute *attr,
2161 struct die_info *die, struct dwarf2_cu *cu,
2162 struct dynamic_prop *prop);
2163
c906108c
SS
2164/* memory allocation interface */
2165
7b5a2f43 2166static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 2167
b60c80d6 2168static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 2169
43f3e411 2170static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 2171
6e5a29e1 2172static int attr_form_is_block (const struct attribute *);
8e19ed76 2173
6e5a29e1 2174static int attr_form_is_section_offset (const struct attribute *);
3690dd37 2175
6e5a29e1 2176static int attr_form_is_constant (const struct attribute *);
3690dd37 2177
6e5a29e1 2178static int attr_form_is_ref (const struct attribute *);
7771576e 2179
8cf6f0b1
TT
2180static void fill_in_loclist_baton (struct dwarf2_cu *cu,
2181 struct dwarf2_loclist_baton *baton,
ff39bb5e 2182 const struct attribute *attr);
8cf6f0b1 2183
ff39bb5e 2184static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 2185 struct symbol *sym,
f1e6e072
TT
2186 struct dwarf2_cu *cu,
2187 int is_block);
4c2df51b 2188
d521ce57
TT
2189static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
2190 const gdb_byte *info_ptr,
2191 struct abbrev_info *abbrev);
4bb7a0a7 2192
72bf9492
DJ
2193static hashval_t partial_die_hash (const void *item);
2194
2195static int partial_die_eq (const void *item_lhs, const void *item_rhs);
2196
ae038cb0 2197static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
2198 (sect_offset sect_off, unsigned int offset_in_dwz,
2199 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 2200
9816fde3 2201static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
2202 struct die_info *comp_unit_die,
2203 enum language pretend_language);
93311388 2204
ae038cb0
DJ
2205static void free_cached_comp_units (void *);
2206
ed2dc618 2207static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 2208
dee91e82 2209static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 2210
f792889a
DJ
2211static struct type *set_die_type (struct die_info *, struct type *,
2212 struct dwarf2_cu *);
1c379e20 2213
ed2dc618 2214static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 2215
ed2dc618 2216static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 2217
95554aad
TT
2218static void load_full_comp_unit (struct dwarf2_per_cu_data *,
2219 enum language);
10b3939b 2220
95554aad
TT
2221static void process_full_comp_unit (struct dwarf2_per_cu_data *,
2222 enum language);
10b3939b 2223
f4dc4d17
DE
2224static void process_full_type_unit (struct dwarf2_per_cu_data *,
2225 enum language);
2226
10b3939b
DJ
2227static void dwarf2_add_dependence (struct dwarf2_cu *,
2228 struct dwarf2_per_cu_data *);
2229
ae038cb0
DJ
2230static void dwarf2_mark (struct dwarf2_cu *);
2231
2232static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
2233
b64f50a1 2234static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 2235 struct dwarf2_per_cu_data *);
673bfd45 2236
f792889a 2237static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 2238
95554aad
TT
2239static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
2240 enum language pretend_language);
2241
ed2dc618 2242static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 2243
b303c6f6
AB
2244/* Class, the destructor of which frees all allocated queue entries. This
2245 will only have work to do if an error was thrown while processing the
2246 dwarf. If no error was thrown then the queue entries should have all
2247 been processed, and freed, as we went along. */
2248
2249class dwarf2_queue_guard
2250{
2251public:
2252 dwarf2_queue_guard () = default;
2253
2254 /* Free any entries remaining on the queue. There should only be
2255 entries left if we hit an error while processing the dwarf. */
2256 ~dwarf2_queue_guard ()
2257 {
2258 struct dwarf2_queue_item *item, *last;
2259
2260 item = dwarf2_queue;
2261 while (item)
2262 {
2263 /* Anything still marked queued is likely to be in an
2264 inconsistent state, so discard it. */
2265 if (item->per_cu->queued)
2266 {
2267 if (item->per_cu->cu != NULL)
2268 free_one_cached_comp_unit (item->per_cu);
2269 item->per_cu->queued = 0;
2270 }
2271
2272 last = item;
2273 item = item->next;
2274 xfree (last);
2275 }
2276
2277 dwarf2_queue = dwarf2_queue_tail = NULL;
2278 }
2279};
2280
d721ba37
PA
2281/* The return type of find_file_and_directory. Note, the enclosed
2282 string pointers are only valid while this object is valid. */
2283
2284struct file_and_directory
2285{
2286 /* The filename. This is never NULL. */
2287 const char *name;
2288
2289 /* The compilation directory. NULL if not known. If we needed to
2290 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2291 points directly to the DW_AT_comp_dir string attribute owned by
2292 the obstack that owns the DIE. */
2293 const char *comp_dir;
2294
2295 /* If we needed to build a new string for comp_dir, this is what
2296 owns the storage. */
2297 std::string comp_dir_storage;
2298};
2299
2300static file_and_directory find_file_and_directory (struct die_info *die,
2301 struct dwarf2_cu *cu);
9291a0cd
TT
2302
2303static char *file_full_name (int file, struct line_header *lh,
2304 const char *comp_dir);
2305
43988095
JK
2306/* Expected enum dwarf_unit_type for read_comp_unit_head. */
2307enum class rcuh_kind { COMPILE, TYPE };
2308
d521ce57 2309static const gdb_byte *read_and_check_comp_unit_head
ed2dc618
SM
2310 (struct dwarf2_per_objfile* dwarf2_per_objfile,
2311 struct comp_unit_head *header,
36586728 2312 struct dwarf2_section_info *section,
d521ce57 2313 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 2314 rcuh_kind section_kind);
36586728 2315
fd820528 2316static void init_cutu_and_read_dies
f4dc4d17
DE
2317 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2318 int use_existing_cu, int keep,
3019eac3
DE
2319 die_reader_func_ftype *die_reader_func, void *data);
2320
dee91e82
DE
2321static void init_cutu_and_read_dies_simple
2322 (struct dwarf2_per_cu_data *this_cu,
2323 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 2324
673bfd45 2325static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 2326
3019eac3
DE
2327static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2328
57d63ce2 2329static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
2330 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2331 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 2332 ULONGEST signature, int is_debug_types);
a2ce51a0 2333
ed2dc618
SM
2334static struct dwp_file *get_dwp_file
2335 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 2336
3019eac3 2337static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 2338 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
2339
2340static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 2341 (struct signatured_type *, const char *, const char *);
3019eac3 2342
89e63ee4
DE
2343static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2344
3019eac3
DE
2345static void free_dwo_file_cleanup (void *);
2346
ed2dc618
SM
2347struct free_dwo_file_cleanup_data
2348{
2349 struct dwo_file *dwo_file;
2350 struct dwarf2_per_objfile *dwarf2_per_objfile;
2351};
2352
2353static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 2354
1b80a9fa 2355static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2356
2357static void free_line_header_voidp (void *arg);
4390d890
DE
2358\f
2359/* Various complaints about symbol reading that don't abort the process. */
2360
2361static void
2362dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2363{
2364 complaint (&symfile_complaints,
2365 _("statement list doesn't fit in .debug_line section"));
2366}
2367
2368static void
2369dwarf2_debug_line_missing_file_complaint (void)
2370{
2371 complaint (&symfile_complaints,
2372 _(".debug_line section has line data without a file"));
2373}
2374
2375static void
2376dwarf2_debug_line_missing_end_sequence_complaint (void)
2377{
2378 complaint (&symfile_complaints,
2379 _(".debug_line section has line "
2380 "program sequence without an end"));
2381}
2382
2383static void
2384dwarf2_complex_location_expr_complaint (void)
2385{
2386 complaint (&symfile_complaints, _("location expression too complex"));
2387}
2388
2389static void
2390dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2391 int arg3)
2392{
2393 complaint (&symfile_complaints,
2394 _("const value length mismatch for '%s', got %d, expected %d"),
2395 arg1, arg2, arg3);
2396}
2397
2398static void
2399dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2400{
2401 complaint (&symfile_complaints,
2402 _("debug info runs off end of %s section"
2403 " [in module %s]"),
a32a8923
DE
2404 get_section_name (section),
2405 get_section_file_name (section));
4390d890 2406}
1b80a9fa 2407
4390d890
DE
2408static void
2409dwarf2_macro_malformed_definition_complaint (const char *arg1)
2410{
2411 complaint (&symfile_complaints,
2412 _("macro debug info contains a "
2413 "malformed macro definition:\n`%s'"),
2414 arg1);
2415}
2416
2417static void
2418dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2419{
2420 complaint (&symfile_complaints,
2421 _("invalid attribute class or form for '%s' in '%s'"),
2422 arg1, arg2);
2423}
527f3840
JK
2424
2425/* Hash function for line_header_hash. */
2426
2427static hashval_t
2428line_header_hash (const struct line_header *ofs)
2429{
9c541725 2430 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2431}
2432
2433/* Hash function for htab_create_alloc_ex for line_header_hash. */
2434
2435static hashval_t
2436line_header_hash_voidp (const void *item)
2437{
9a3c8263 2438 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2439
2440 return line_header_hash (ofs);
2441}
2442
2443/* Equality function for line_header_hash. */
2444
2445static int
2446line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2447{
9a3c8263
SM
2448 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2449 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2450
9c541725 2451 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2452 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2453}
2454
4390d890 2455\f
9291a0cd 2456
31aa7e4e
JB
2457/* Read the given attribute value as an address, taking the attribute's
2458 form into account. */
2459
2460static CORE_ADDR
2461attr_value_as_address (struct attribute *attr)
2462{
2463 CORE_ADDR addr;
2464
2465 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2466 {
2467 /* Aside from a few clearly defined exceptions, attributes that
2468 contain an address must always be in DW_FORM_addr form.
2469 Unfortunately, some compilers happen to be violating this
2470 requirement by encoding addresses using other forms, such
2471 as DW_FORM_data4 for example. For those broken compilers,
2472 we try to do our best, without any guarantee of success,
2473 to interpret the address correctly. It would also be nice
2474 to generate a complaint, but that would require us to maintain
2475 a list of legitimate cases where a non-address form is allowed,
2476 as well as update callers to pass in at least the CU's DWARF
2477 version. This is more overhead than what we're willing to
2478 expand for a pretty rare case. */
2479 addr = DW_UNSND (attr);
2480 }
2481 else
2482 addr = DW_ADDR (attr);
2483
2484 return addr;
2485}
2486
9291a0cd 2487/* The suffix for an index file. */
437afbb8
JK
2488#define INDEX4_SUFFIX ".gdb-index"
2489#define INDEX5_SUFFIX ".debug_names"
2490#define DEBUG_STR_SUFFIX ".debug_str"
9291a0cd 2491
330cdd98
PA
2492/* See declaration. */
2493
2494dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2495 const dwarf2_debug_sections *names)
2496 : objfile (objfile_)
2497{
2498 if (names == NULL)
2499 names = &dwarf2_elf_names;
2500
2501 bfd *obfd = objfile->obfd;
2502
2503 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2504 locate_sections (obfd, sec, *names);
2505}
2506
fc8e7e75
SM
2507static void free_dwo_files (htab_t dwo_files, struct objfile *objfile);
2508
330cdd98
PA
2509dwarf2_per_objfile::~dwarf2_per_objfile ()
2510{
2511 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2512 free_cached_comp_units ();
2513
2514 if (quick_file_names_table)
2515 htab_delete (quick_file_names_table);
2516
2517 if (line_header_hash)
2518 htab_delete (line_header_hash);
2519
fc8e7e75
SM
2520 for (int ix = 0; ix < n_comp_units; ++ix)
2521 VEC_free (dwarf2_per_cu_ptr, all_comp_units[ix]->imported_symtabs);
2522
2523 for (int ix = 0; ix < n_type_units; ++ix)
2524 VEC_free (dwarf2_per_cu_ptr,
2525 all_type_units[ix]->per_cu.imported_symtabs);
2526 xfree (all_type_units);
2527
2528 VEC_free (dwarf2_section_info_def, types);
2529
2530 if (dwo_files != NULL)
2531 free_dwo_files (dwo_files, objfile);
2532 if (dwp_file != NULL)
2533 gdb_bfd_unref (dwp_file->dbfd);
2534
2535 if (dwz_file != NULL && dwz_file->dwz_bfd)
2536 gdb_bfd_unref (dwz_file->dwz_bfd);
2537
2538 if (index_table != NULL)
2539 index_table->~mapped_index ();
2540
330cdd98
PA
2541 /* Everything else should be on the objfile obstack. */
2542}
2543
2544/* See declaration. */
2545
2546void
2547dwarf2_per_objfile::free_cached_comp_units ()
2548{
2549 dwarf2_per_cu_data *per_cu = read_in_chain;
2550 dwarf2_per_cu_data **last_chain = &read_in_chain;
2551 while (per_cu != NULL)
2552 {
2553 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2554
fcd3b13d 2555 delete per_cu->cu;
330cdd98
PA
2556 *last_chain = next_cu;
2557 per_cu = next_cu;
2558 }
2559}
2560
c906108c 2561/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2562 information and return true if we have enough to do something.
2563 NAMES points to the dwarf2 section names, or is NULL if the standard
2564 ELF names are used. */
c906108c
SS
2565
2566int
251d32d9
TG
2567dwarf2_has_info (struct objfile *objfile,
2568 const struct dwarf2_debug_sections *names)
c906108c 2569{
97cbe998
SDJ
2570 if (objfile->flags & OBJF_READNEVER)
2571 return 0;
2572
ed2dc618
SM
2573 struct dwarf2_per_objfile *dwarf2_per_objfile
2574 = get_dwarf2_per_objfile (objfile);
2575
2576 if (dwarf2_per_objfile == NULL)
be391dca
TT
2577 {
2578 /* Initialize per-objfile state. */
fd90ace4
YQ
2579 dwarf2_per_objfile
2580 = new (&objfile->objfile_obstack) struct dwarf2_per_objfile (objfile,
2581 names);
ed2dc618 2582 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
be391dca 2583 }
73869dc2 2584 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2585 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2586 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2587 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2588}
2589
2590/* Return the containing section of virtual section SECTION. */
2591
2592static struct dwarf2_section_info *
2593get_containing_section (const struct dwarf2_section_info *section)
2594{
2595 gdb_assert (section->is_virtual);
2596 return section->s.containing_section;
c906108c
SS
2597}
2598
a32a8923
DE
2599/* Return the bfd owner of SECTION. */
2600
2601static struct bfd *
2602get_section_bfd_owner (const struct dwarf2_section_info *section)
2603{
73869dc2
DE
2604 if (section->is_virtual)
2605 {
2606 section = get_containing_section (section);
2607 gdb_assert (!section->is_virtual);
2608 }
049412e3 2609 return section->s.section->owner;
a32a8923
DE
2610}
2611
2612/* Return the bfd section of SECTION.
2613 Returns NULL if the section is not present. */
2614
2615static asection *
2616get_section_bfd_section (const struct dwarf2_section_info *section)
2617{
73869dc2
DE
2618 if (section->is_virtual)
2619 {
2620 section = get_containing_section (section);
2621 gdb_assert (!section->is_virtual);
2622 }
049412e3 2623 return section->s.section;
a32a8923
DE
2624}
2625
2626/* Return the name of SECTION. */
2627
2628static const char *
2629get_section_name (const struct dwarf2_section_info *section)
2630{
2631 asection *sectp = get_section_bfd_section (section);
2632
2633 gdb_assert (sectp != NULL);
2634 return bfd_section_name (get_section_bfd_owner (section), sectp);
2635}
2636
2637/* Return the name of the file SECTION is in. */
2638
2639static const char *
2640get_section_file_name (const struct dwarf2_section_info *section)
2641{
2642 bfd *abfd = get_section_bfd_owner (section);
2643
2644 return bfd_get_filename (abfd);
2645}
2646
2647/* Return the id of SECTION.
2648 Returns 0 if SECTION doesn't exist. */
2649
2650static int
2651get_section_id (const struct dwarf2_section_info *section)
2652{
2653 asection *sectp = get_section_bfd_section (section);
2654
2655 if (sectp == NULL)
2656 return 0;
2657 return sectp->id;
2658}
2659
2660/* Return the flags of SECTION.
73869dc2 2661 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2662
2663static int
2664get_section_flags (const struct dwarf2_section_info *section)
2665{
2666 asection *sectp = get_section_bfd_section (section);
2667
2668 gdb_assert (sectp != NULL);
2669 return bfd_get_section_flags (sectp->owner, sectp);
2670}
2671
251d32d9
TG
2672/* When loading sections, we look either for uncompressed section or for
2673 compressed section names. */
233a11ab
CS
2674
2675static int
251d32d9
TG
2676section_is_p (const char *section_name,
2677 const struct dwarf2_section_names *names)
233a11ab 2678{
251d32d9
TG
2679 if (names->normal != NULL
2680 && strcmp (section_name, names->normal) == 0)
2681 return 1;
2682 if (names->compressed != NULL
2683 && strcmp (section_name, names->compressed) == 0)
2684 return 1;
2685 return 0;
233a11ab
CS
2686}
2687
330cdd98 2688/* See declaration. */
c906108c 2689
330cdd98
PA
2690void
2691dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2692 const dwarf2_debug_sections &names)
c906108c 2693{
dc7650b8 2694 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9 2695
dc7650b8
JK
2696 if ((aflag & SEC_HAS_CONTENTS) == 0)
2697 {
2698 }
330cdd98 2699 else if (section_is_p (sectp->name, &names.info))
c906108c 2700 {
330cdd98
PA
2701 this->info.s.section = sectp;
2702 this->info.size = bfd_get_section_size (sectp);
c906108c 2703 }
330cdd98 2704 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2705 {
330cdd98
PA
2706 this->abbrev.s.section = sectp;
2707 this->abbrev.size = bfd_get_section_size (sectp);
c906108c 2708 }
330cdd98 2709 else if (section_is_p (sectp->name, &names.line))
c906108c 2710 {
330cdd98
PA
2711 this->line.s.section = sectp;
2712 this->line.size = bfd_get_section_size (sectp);
c906108c 2713 }
330cdd98 2714 else if (section_is_p (sectp->name, &names.loc))
c906108c 2715 {
330cdd98
PA
2716 this->loc.s.section = sectp;
2717 this->loc.size = bfd_get_section_size (sectp);
c906108c 2718 }
330cdd98 2719 else if (section_is_p (sectp->name, &names.loclists))
43988095 2720 {
330cdd98
PA
2721 this->loclists.s.section = sectp;
2722 this->loclists.size = bfd_get_section_size (sectp);
43988095 2723 }
330cdd98 2724 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2725 {
330cdd98
PA
2726 this->macinfo.s.section = sectp;
2727 this->macinfo.size = bfd_get_section_size (sectp);
c906108c 2728 }
330cdd98 2729 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2730 {
330cdd98
PA
2731 this->macro.s.section = sectp;
2732 this->macro.size = bfd_get_section_size (sectp);
cf2c3c16 2733 }
330cdd98 2734 else if (section_is_p (sectp->name, &names.str))
c906108c 2735 {
330cdd98
PA
2736 this->str.s.section = sectp;
2737 this->str.size = bfd_get_section_size (sectp);
c906108c 2738 }
330cdd98 2739 else if (section_is_p (sectp->name, &names.line_str))
43988095 2740 {
330cdd98
PA
2741 this->line_str.s.section = sectp;
2742 this->line_str.size = bfd_get_section_size (sectp);
43988095 2743 }
330cdd98 2744 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2745 {
330cdd98
PA
2746 this->addr.s.section = sectp;
2747 this->addr.size = bfd_get_section_size (sectp);
3019eac3 2748 }
330cdd98 2749 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2750 {
330cdd98
PA
2751 this->frame.s.section = sectp;
2752 this->frame.size = bfd_get_section_size (sectp);
b6af0555 2753 }
330cdd98 2754 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2755 {
330cdd98
PA
2756 this->eh_frame.s.section = sectp;
2757 this->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2758 }
330cdd98 2759 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2760 {
330cdd98
PA
2761 this->ranges.s.section = sectp;
2762 this->ranges.size = bfd_get_section_size (sectp);
af34e669 2763 }
330cdd98 2764 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2765 {
330cdd98
PA
2766 this->rnglists.s.section = sectp;
2767 this->rnglists.size = bfd_get_section_size (sectp);
43988095 2768 }
330cdd98 2769 else if (section_is_p (sectp->name, &names.types))
348e048f 2770 {
8b70b953
TT
2771 struct dwarf2_section_info type_section;
2772
2773 memset (&type_section, 0, sizeof (type_section));
049412e3 2774 type_section.s.section = sectp;
8b70b953
TT
2775 type_section.size = bfd_get_section_size (sectp);
2776
330cdd98 2777 VEC_safe_push (dwarf2_section_info_def, this->types,
8b70b953 2778 &type_section);
348e048f 2779 }
330cdd98 2780 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2781 {
330cdd98
PA
2782 this->gdb_index.s.section = sectp;
2783 this->gdb_index.size = bfd_get_section_size (sectp);
9291a0cd 2784 }
927aa2e7
JK
2785 else if (section_is_p (sectp->name, &names.debug_names))
2786 {
2787 this->debug_names.s.section = sectp;
2788 this->debug_names.size = bfd_get_section_size (sectp);
2789 }
2790 else if (section_is_p (sectp->name, &names.debug_aranges))
2791 {
2792 this->debug_aranges.s.section = sectp;
2793 this->debug_aranges.size = bfd_get_section_size (sectp);
2794 }
dce234bc 2795
b4e1fd61 2796 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5 2797 && bfd_section_vma (abfd, sectp) == 0)
330cdd98 2798 this->has_section_at_zero = true;
c906108c
SS
2799}
2800
fceca515
DE
2801/* A helper function that decides whether a section is empty,
2802 or not present. */
9e0ac564
TT
2803
2804static int
19ac8c2e 2805dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2806{
73869dc2
DE
2807 if (section->is_virtual)
2808 return section->size == 0;
049412e3 2809 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2810}
2811
3019eac3
DE
2812/* Read the contents of the section INFO.
2813 OBJFILE is the main object file, but not necessarily the file where
a32a8923
DE
2814 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2815 of the DWO file.
dce234bc 2816 If the section is compressed, uncompress it before returning. */
c906108c 2817
dce234bc
PP
2818static void
2819dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 2820{
a32a8923 2821 asection *sectp;
3019eac3 2822 bfd *abfd;
dce234bc 2823 gdb_byte *buf, *retbuf;
c906108c 2824
be391dca
TT
2825 if (info->readin)
2826 return;
dce234bc 2827 info->buffer = NULL;
be391dca 2828 info->readin = 1;
188dd5d6 2829
9e0ac564 2830 if (dwarf2_section_empty_p (info))
dce234bc 2831 return;
c906108c 2832
a32a8923 2833 sectp = get_section_bfd_section (info);
3019eac3 2834
73869dc2
DE
2835 /* If this is a virtual section we need to read in the real one first. */
2836 if (info->is_virtual)
2837 {
2838 struct dwarf2_section_info *containing_section =
2839 get_containing_section (info);
2840
2841 gdb_assert (sectp != NULL);
2842 if ((sectp->flags & SEC_RELOC) != 0)
2843 {
2844 error (_("Dwarf Error: DWP format V2 with relocations is not"
2845 " supported in section %s [in module %s]"),
2846 get_section_name (info), get_section_file_name (info));
2847 }
2848 dwarf2_read_section (objfile, containing_section);
2849 /* Other code should have already caught virtual sections that don't
2850 fit. */
2851 gdb_assert (info->virtual_offset + info->size
2852 <= containing_section->size);
2853 /* If the real section is empty or there was a problem reading the
2854 section we shouldn't get here. */
2855 gdb_assert (containing_section->buffer != NULL);
2856 info->buffer = containing_section->buffer + info->virtual_offset;
2857 return;
2858 }
2859
4bf44c1c
TT
2860 /* If the section has relocations, we must read it ourselves.
2861 Otherwise we attach it to the BFD. */
2862 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2863 {
d521ce57 2864 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2865 return;
dce234bc 2866 }
dce234bc 2867
224c3ddb 2868 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2869 info->buffer = buf;
dce234bc
PP
2870
2871 /* When debugging .o files, we may need to apply relocations; see
2872 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2873 We never compress sections in .o files, so we only need to
2874 try this when the section is not compressed. */
ac8035ab 2875 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2876 if (retbuf != NULL)
2877 {
2878 info->buffer = retbuf;
2879 return;
2880 }
2881
a32a8923
DE
2882 abfd = get_section_bfd_owner (info);
2883 gdb_assert (abfd != NULL);
2884
dce234bc
PP
2885 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2886 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2887 {
2888 error (_("Dwarf Error: Can't read DWARF data"
2889 " in section %s [in module %s]"),
2890 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2891 }
dce234bc
PP
2892}
2893
9e0ac564
TT
2894/* A helper function that returns the size of a section in a safe way.
2895 If you are positive that the section has been read before using the
2896 size, then it is safe to refer to the dwarf2_section_info object's
2897 "size" field directly. In other cases, you must call this
2898 function, because for compressed sections the size field is not set
2899 correctly until the section has been read. */
2900
2901static bfd_size_type
2902dwarf2_section_size (struct objfile *objfile,
2903 struct dwarf2_section_info *info)
2904{
2905 if (!info->readin)
2906 dwarf2_read_section (objfile, info);
2907 return info->size;
2908}
2909
dce234bc 2910/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2911 SECTION_NAME. */
af34e669 2912
dce234bc 2913void
3017a003
TG
2914dwarf2_get_section_info (struct objfile *objfile,
2915 enum dwarf2_section_enum sect,
d521ce57 2916 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2917 bfd_size_type *sizep)
2918{
2919 struct dwarf2_per_objfile *data
9a3c8263
SM
2920 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2921 dwarf2_objfile_data_key);
dce234bc 2922 struct dwarf2_section_info *info;
a3b2a86b
TT
2923
2924 /* We may see an objfile without any DWARF, in which case we just
2925 return nothing. */
2926 if (data == NULL)
2927 {
2928 *sectp = NULL;
2929 *bufp = NULL;
2930 *sizep = 0;
2931 return;
2932 }
3017a003
TG
2933 switch (sect)
2934 {
2935 case DWARF2_DEBUG_FRAME:
2936 info = &data->frame;
2937 break;
2938 case DWARF2_EH_FRAME:
2939 info = &data->eh_frame;
2940 break;
2941 default:
2942 gdb_assert_not_reached ("unexpected section");
2943 }
dce234bc 2944
9e0ac564 2945 dwarf2_read_section (objfile, info);
dce234bc 2946
a32a8923 2947 *sectp = get_section_bfd_section (info);
dce234bc
PP
2948 *bufp = info->buffer;
2949 *sizep = info->size;
2950}
2951
36586728
TT
2952/* A helper function to find the sections for a .dwz file. */
2953
2954static void
2955locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2956{
9a3c8263 2957 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2958
2959 /* Note that we only support the standard ELF names, because .dwz
2960 is ELF-only (at the time of writing). */
2961 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2962 {
049412e3 2963 dwz_file->abbrev.s.section = sectp;
36586728
TT
2964 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2965 }
2966 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2967 {
049412e3 2968 dwz_file->info.s.section = sectp;
36586728
TT
2969 dwz_file->info.size = bfd_get_section_size (sectp);
2970 }
2971 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2972 {
049412e3 2973 dwz_file->str.s.section = sectp;
36586728
TT
2974 dwz_file->str.size = bfd_get_section_size (sectp);
2975 }
2976 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2977 {
049412e3 2978 dwz_file->line.s.section = sectp;
36586728
TT
2979 dwz_file->line.size = bfd_get_section_size (sectp);
2980 }
2981 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2982 {
049412e3 2983 dwz_file->macro.s.section = sectp;
36586728
TT
2984 dwz_file->macro.size = bfd_get_section_size (sectp);
2985 }
2ec9a5e0
TT
2986 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2987 {
049412e3 2988 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2989 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2990 }
927aa2e7
JK
2991 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2992 {
2993 dwz_file->debug_names.s.section = sectp;
2994 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2995 }
36586728
TT
2996}
2997
4db1a1dc
TT
2998/* Open the separate '.dwz' debug file, if needed. Return NULL if
2999 there is no .gnu_debugaltlink section in the file. Error if there
3000 is such a section but the file cannot be found. */
36586728
TT
3001
3002static struct dwz_file *
ed2dc618 3003dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 3004{
36586728
TT
3005 const char *filename;
3006 struct dwz_file *result;
acd13123 3007 bfd_size_type buildid_len_arg;
dc294be5
TT
3008 size_t buildid_len;
3009 bfd_byte *buildid;
36586728
TT
3010
3011 if (dwarf2_per_objfile->dwz_file != NULL)
3012 return dwarf2_per_objfile->dwz_file;
3013
4db1a1dc 3014 bfd_set_error (bfd_error_no_error);
791afaa2
TT
3015 gdb::unique_xmalloc_ptr<char> data
3016 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
3017 &buildid_len_arg, &buildid));
4db1a1dc
TT
3018 if (data == NULL)
3019 {
3020 if (bfd_get_error () == bfd_error_no_error)
3021 return NULL;
3022 error (_("could not read '.gnu_debugaltlink' section: %s"),
3023 bfd_errmsg (bfd_get_error ()));
3024 }
791afaa2
TT
3025
3026 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 3027
acd13123
TT
3028 buildid_len = (size_t) buildid_len_arg;
3029
791afaa2 3030 filename = data.get ();
d721ba37
PA
3031
3032 std::string abs_storage;
36586728
TT
3033 if (!IS_ABSOLUTE_PATH (filename))
3034 {
14278e1f
TT
3035 gdb::unique_xmalloc_ptr<char> abs
3036 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 3037
14278e1f 3038 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 3039 filename = abs_storage.c_str ();
36586728
TT
3040 }
3041
dc294be5
TT
3042 /* First try the file name given in the section. If that doesn't
3043 work, try to use the build-id instead. */
192b62ce 3044 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 3045 if (dwz_bfd != NULL)
36586728 3046 {
192b62ce
TT
3047 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
3048 dwz_bfd.release ();
36586728
TT
3049 }
3050
dc294be5
TT
3051 if (dwz_bfd == NULL)
3052 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
3053
3054 if (dwz_bfd == NULL)
3055 error (_("could not find '.gnu_debugaltlink' file for %s"),
3056 objfile_name (dwarf2_per_objfile->objfile));
3057
36586728
TT
3058 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
3059 struct dwz_file);
192b62ce 3060 result->dwz_bfd = dwz_bfd.release ();
36586728 3061
192b62ce 3062 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
36586728 3063
192b62ce 3064 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
8d2cc612 3065 dwarf2_per_objfile->dwz_file = result;
36586728
TT
3066 return result;
3067}
9291a0cd 3068\f
7b9f3c50
DE
3069/* DWARF quick_symbols_functions support. */
3070
3071/* TUs can share .debug_line entries, and there can be a lot more TUs than
3072 unique line tables, so we maintain a separate table of all .debug_line
3073 derived entries to support the sharing.
3074 All the quick functions need is the list of file names. We discard the
3075 line_header when we're done and don't need to record it here. */
3076struct quick_file_names
3077{
094b34ac
DE
3078 /* The data used to construct the hash key. */
3079 struct stmt_list_hash hash;
7b9f3c50
DE
3080
3081 /* The number of entries in file_names, real_names. */
3082 unsigned int num_file_names;
3083
3084 /* The file names from the line table, after being run through
3085 file_full_name. */
3086 const char **file_names;
3087
3088 /* The file names from the line table after being run through
3089 gdb_realpath. These are computed lazily. */
3090 const char **real_names;
3091};
3092
3093/* When using the index (and thus not using psymtabs), each CU has an
3094 object of this type. This is used to hold information needed by
3095 the various "quick" methods. */
3096struct dwarf2_per_cu_quick_data
3097{
3098 /* The file table. This can be NULL if there was no file table
3099 or it's currently not read in.
3100 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
3101 struct quick_file_names *file_names;
3102
3103 /* The corresponding symbol table. This is NULL if symbols for this
3104 CU have not yet been read. */
43f3e411 3105 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
3106
3107 /* A temporary mark bit used when iterating over all CUs in
3108 expand_symtabs_matching. */
3109 unsigned int mark : 1;
3110
3111 /* True if we've tried to read the file table and found there isn't one.
3112 There will be no point in trying to read it again next time. */
3113 unsigned int no_file_data : 1;
3114};
3115
094b34ac
DE
3116/* Utility hash function for a stmt_list_hash. */
3117
3118static hashval_t
3119hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
3120{
3121 hashval_t v = 0;
3122
3123 if (stmt_list_hash->dwo_unit != NULL)
3124 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 3125 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
3126 return v;
3127}
3128
3129/* Utility equality function for a stmt_list_hash. */
3130
3131static int
3132eq_stmt_list_entry (const struct stmt_list_hash *lhs,
3133 const struct stmt_list_hash *rhs)
3134{
3135 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
3136 return 0;
3137 if (lhs->dwo_unit != NULL
3138 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
3139 return 0;
3140
9c541725 3141 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
3142}
3143
7b9f3c50
DE
3144/* Hash function for a quick_file_names. */
3145
3146static hashval_t
3147hash_file_name_entry (const void *e)
3148{
9a3c8263
SM
3149 const struct quick_file_names *file_data
3150 = (const struct quick_file_names *) e;
7b9f3c50 3151
094b34ac 3152 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
3153}
3154
3155/* Equality function for a quick_file_names. */
3156
3157static int
3158eq_file_name_entry (const void *a, const void *b)
3159{
9a3c8263
SM
3160 const struct quick_file_names *ea = (const struct quick_file_names *) a;
3161 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 3162
094b34ac 3163 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
3164}
3165
3166/* Delete function for a quick_file_names. */
3167
3168static void
3169delete_file_name_entry (void *e)
3170{
9a3c8263 3171 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
3172 int i;
3173
3174 for (i = 0; i < file_data->num_file_names; ++i)
3175 {
3176 xfree ((void*) file_data->file_names[i]);
3177 if (file_data->real_names)
3178 xfree ((void*) file_data->real_names[i]);
3179 }
3180
3181 /* The space for the struct itself lives on objfile_obstack,
3182 so we don't free it here. */
3183}
3184
3185/* Create a quick_file_names hash table. */
3186
3187static htab_t
3188create_quick_file_names_table (unsigned int nr_initial_entries)
3189{
3190 return htab_create_alloc (nr_initial_entries,
3191 hash_file_name_entry, eq_file_name_entry,
3192 delete_file_name_entry, xcalloc, xfree);
3193}
9291a0cd 3194
918dd910
JK
3195/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
3196 have to be created afterwards. You should call age_cached_comp_units after
3197 processing PER_CU->CU. dw2_setup must have been already called. */
3198
3199static void
3200load_cu (struct dwarf2_per_cu_data *per_cu)
3201{
3019eac3 3202 if (per_cu->is_debug_types)
e5fe5e75 3203 load_full_type_unit (per_cu);
918dd910 3204 else
95554aad 3205 load_full_comp_unit (per_cu, language_minimal);
918dd910 3206
cc12ce38
DE
3207 if (per_cu->cu == NULL)
3208 return; /* Dummy CU. */
2dc860c0
DE
3209
3210 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
3211}
3212
a0f42c21 3213/* Read in the symbols for PER_CU. */
2fdf6df6 3214
9291a0cd 3215static void
a0f42c21 3216dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 3217{
ed2dc618 3218 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 3219
f4dc4d17
DE
3220 /* Skip type_unit_groups, reading the type units they contain
3221 is handled elsewhere. */
3222 if (IS_TYPE_UNIT_GROUP (per_cu))
3223 return;
3224
b303c6f6
AB
3225 /* The destructor of dwarf2_queue_guard frees any entries left on
3226 the queue. After this point we're guaranteed to leave this function
3227 with the dwarf queue empty. */
3228 dwarf2_queue_guard q_guard;
9291a0cd 3229
95554aad 3230 if (dwarf2_per_objfile->using_index
43f3e411 3231 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
3232 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
3233 {
3234 queue_comp_unit (per_cu, language_minimal);
3235 load_cu (per_cu);
89e63ee4
DE
3236
3237 /* If we just loaded a CU from a DWO, and we're working with an index
3238 that may badly handle TUs, load all the TUs in that DWO as well.
3239 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
3240 if (!per_cu->is_debug_types
cc12ce38 3241 && per_cu->cu != NULL
89e63ee4
DE
3242 && per_cu->cu->dwo_unit != NULL
3243 && dwarf2_per_objfile->index_table != NULL
3244 && dwarf2_per_objfile->index_table->version <= 7
3245 /* DWP files aren't supported yet. */
ed2dc618 3246 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 3247 queue_and_load_all_dwo_tus (per_cu);
95554aad 3248 }
9291a0cd 3249
ed2dc618 3250 process_queue (dwarf2_per_objfile);
9291a0cd
TT
3251
3252 /* Age the cache, releasing compilation units that have not
3253 been used recently. */
ed2dc618 3254 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
3255}
3256
3257/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
3258 the objfile from which this CU came. Returns the resulting symbol
3259 table. */
2fdf6df6 3260
43f3e411 3261static struct compunit_symtab *
a0f42c21 3262dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 3263{
ed2dc618
SM
3264 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3265
95554aad 3266 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 3267 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 3268 {
ed2dc618
SM
3269 struct cleanup *back_to = make_cleanup (free_cached_comp_units,
3270 dwarf2_per_objfile);
c83dd867 3271 scoped_restore decrementer = increment_reading_symtab ();
a0f42c21 3272 dw2_do_instantiate_symtab (per_cu);
ed2dc618 3273 process_cu_includes (dwarf2_per_objfile);
9291a0cd
TT
3274 do_cleanups (back_to);
3275 }
f194fefb 3276
43f3e411 3277 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
3278}
3279
8832e7e3 3280/* Return the CU/TU given its index.
f4dc4d17
DE
3281
3282 This is intended for loops like:
3283
3284 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3285 + dwarf2_per_objfile->n_type_units); ++i)
3286 {
8832e7e3 3287 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
f4dc4d17
DE
3288
3289 ...;
3290 }
3291*/
2fdf6df6 3292
1fd400ff 3293static struct dwarf2_per_cu_data *
ed2dc618
SM
3294dw2_get_cutu (struct dwarf2_per_objfile *dwarf2_per_objfile,
3295 int index)
1fd400ff
TT
3296{
3297 if (index >= dwarf2_per_objfile->n_comp_units)
3298 {
f4dc4d17 3299 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
3300 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3301 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
3302 }
3303
3304 return dwarf2_per_objfile->all_comp_units[index];
3305}
3306
8832e7e3
DE
3307/* Return the CU given its index.
3308 This differs from dw2_get_cutu in that it's for when you know INDEX
3309 refers to a CU. */
f4dc4d17
DE
3310
3311static struct dwarf2_per_cu_data *
ed2dc618 3312dw2_get_cu (struct dwarf2_per_objfile *dwarf2_per_objfile, int index)
f4dc4d17 3313{
8832e7e3 3314 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
f4dc4d17 3315
1fd400ff
TT
3316 return dwarf2_per_objfile->all_comp_units[index];
3317}
3318
4b514bc8
JK
3319/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
3320 objfile_obstack, and constructed with the specified field
3321 values. */
3322
3323static dwarf2_per_cu_data *
ed2dc618 3324create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
3325 struct dwarf2_section_info *section,
3326 int is_dwz,
3327 sect_offset sect_off, ULONGEST length)
3328{
ed2dc618 3329 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
3330 dwarf2_per_cu_data *the_cu
3331 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3332 struct dwarf2_per_cu_data);
3333 the_cu->sect_off = sect_off;
3334 the_cu->length = length;
e3b94546 3335 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
3336 the_cu->section = section;
3337 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3338 struct dwarf2_per_cu_quick_data);
3339 the_cu->is_dwz = is_dwz;
3340 return the_cu;
3341}
3342
2ec9a5e0
TT
3343/* A helper for create_cus_from_index that handles a given list of
3344 CUs. */
2fdf6df6 3345
74a0d9f6 3346static void
2ec9a5e0
TT
3347create_cus_from_index_list (struct objfile *objfile,
3348 const gdb_byte *cu_list, offset_type n_elements,
3349 struct dwarf2_section_info *section,
3350 int is_dwz,
3351 int base_offset)
9291a0cd
TT
3352{
3353 offset_type i;
ed2dc618
SM
3354 struct dwarf2_per_objfile *dwarf2_per_objfile
3355 = get_dwarf2_per_objfile (objfile);
9291a0cd 3356
2ec9a5e0 3357 for (i = 0; i < n_elements; i += 2)
9291a0cd 3358 {
74a0d9f6 3359 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3360
3361 sect_offset sect_off
3362 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3363 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
3364 cu_list += 2 * 8;
3365
4b514bc8 3366 dwarf2_per_objfile->all_comp_units[base_offset + i / 2]
ed2dc618
SM
3367 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3368 sect_off, length);
9291a0cd 3369 }
9291a0cd
TT
3370}
3371
2ec9a5e0 3372/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3373 the CU objects for this objfile. */
2ec9a5e0 3374
74a0d9f6 3375static void
2ec9a5e0
TT
3376create_cus_from_index (struct objfile *objfile,
3377 const gdb_byte *cu_list, offset_type cu_list_elements,
3378 const gdb_byte *dwz_list, offset_type dwz_elements)
3379{
3380 struct dwz_file *dwz;
ed2dc618
SM
3381 struct dwarf2_per_objfile *dwarf2_per_objfile
3382 = get_dwarf2_per_objfile (objfile);
2ec9a5e0
TT
3383
3384 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
8d749320
SM
3385 dwarf2_per_objfile->all_comp_units =
3386 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3387 dwarf2_per_objfile->n_comp_units);
2ec9a5e0 3388
74a0d9f6
JK
3389 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3390 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
3391
3392 if (dwz_elements == 0)
74a0d9f6 3393 return;
2ec9a5e0 3394
ed2dc618 3395 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
74a0d9f6
JK
3396 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3397 cu_list_elements / 2);
2ec9a5e0
TT
3398}
3399
1fd400ff 3400/* Create the signatured type hash table from the index. */
673bfd45 3401
74a0d9f6 3402static void
673bfd45 3403create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 3404 struct dwarf2_section_info *section,
673bfd45
DE
3405 const gdb_byte *bytes,
3406 offset_type elements)
1fd400ff
TT
3407{
3408 offset_type i;
673bfd45 3409 htab_t sig_types_hash;
ed2dc618
SM
3410 struct dwarf2_per_objfile *dwarf2_per_objfile
3411 = get_dwarf2_per_objfile (objfile);
1fd400ff 3412
6aa5f3a6
DE
3413 dwarf2_per_objfile->n_type_units
3414 = dwarf2_per_objfile->n_allocated_type_units
3415 = elements / 3;
8d749320
SM
3416 dwarf2_per_objfile->all_type_units =
3417 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
1fd400ff 3418
673bfd45 3419 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
3420
3421 for (i = 0; i < elements; i += 3)
3422 {
52dc124a 3423 struct signatured_type *sig_type;
9c541725 3424 ULONGEST signature;
1fd400ff 3425 void **slot;
9c541725 3426 cu_offset type_offset_in_tu;
1fd400ff 3427
74a0d9f6 3428 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3429 sect_offset sect_off
3430 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3431 type_offset_in_tu
3432 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3433 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3434 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3435 bytes += 3 * 8;
3436
52dc124a 3437 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3438 struct signatured_type);
52dc124a 3439 sig_type->signature = signature;
9c541725 3440 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3441 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3442 sig_type->per_cu.section = section;
9c541725 3443 sig_type->per_cu.sect_off = sect_off;
e3b94546 3444 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 3445 sig_type->per_cu.v.quick
1fd400ff
TT
3446 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3447 struct dwarf2_per_cu_quick_data);
3448
52dc124a
DE
3449 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3450 *slot = sig_type;
1fd400ff 3451
b4dd5633 3452 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
3453 }
3454
673bfd45 3455 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3456}
3457
927aa2e7
JK
3458/* Create the signatured type hash table from .debug_names. */
3459
3460static void
3461create_signatured_type_table_from_debug_names
ed2dc618 3462 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3463 const mapped_debug_names &map,
3464 struct dwarf2_section_info *section,
3465 struct dwarf2_section_info *abbrev_section)
3466{
ed2dc618
SM
3467 struct objfile *objfile = dwarf2_per_objfile->objfile;
3468
927aa2e7
JK
3469 dwarf2_read_section (objfile, section);
3470 dwarf2_read_section (objfile, abbrev_section);
3471
3472 dwarf2_per_objfile->n_type_units
3473 = dwarf2_per_objfile->n_allocated_type_units
3474 = map.tu_count;
3475 dwarf2_per_objfile->all_type_units
3476 = XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3477
3478 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3479
3480 for (uint32_t i = 0; i < map.tu_count; ++i)
3481 {
3482 struct signatured_type *sig_type;
3483 ULONGEST signature;
3484 void **slot;
3485 cu_offset type_offset_in_tu;
3486
3487 sect_offset sect_off
3488 = (sect_offset) (extract_unsigned_integer
3489 (map.tu_table_reordered + i * map.offset_size,
3490 map.offset_size,
3491 map.dwarf5_byte_order));
3492
3493 comp_unit_head cu_header;
ed2dc618
SM
3494 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3495 abbrev_section,
927aa2e7
JK
3496 section->buffer + to_underlying (sect_off),
3497 rcuh_kind::TYPE);
3498
3499 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3500 struct signatured_type);
3501 sig_type->signature = cu_header.signature;
3502 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3503 sig_type->per_cu.is_debug_types = 1;
3504 sig_type->per_cu.section = section;
3505 sig_type->per_cu.sect_off = sect_off;
e3b94546 3506 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
3507 sig_type->per_cu.v.quick
3508 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3509 struct dwarf2_per_cu_quick_data);
3510
3511 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3512 *slot = sig_type;
3513
3514 dwarf2_per_objfile->all_type_units[i] = sig_type;
3515 }
3516
3517 dwarf2_per_objfile->signatured_types = sig_types_hash;
3518}
3519
9291a0cd
TT
3520/* Read the address map data from the mapped index, and use it to
3521 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3522
9291a0cd 3523static void
ed2dc618
SM
3524create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3525 struct mapped_index *index)
9291a0cd 3526{
ed2dc618 3527 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 3528 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3529 const gdb_byte *iter, *end;
9291a0cd 3530 struct addrmap *mutable_map;
9291a0cd
TT
3531 CORE_ADDR baseaddr;
3532
8268c778
PA
3533 auto_obstack temp_obstack;
3534
9291a0cd
TT
3535 mutable_map = addrmap_create_mutable (&temp_obstack);
3536
f00a2de2
PA
3537 iter = index->address_table.data ();
3538 end = iter + index->address_table.size ();
9291a0cd
TT
3539
3540 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3541
3542 while (iter < end)
3543 {
3544 ULONGEST hi, lo, cu_index;
3545 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3546 iter += 8;
3547 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3548 iter += 8;
3549 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3550 iter += 4;
f652bce2 3551
24a55014 3552 if (lo > hi)
f652bce2 3553 {
24a55014
DE
3554 complaint (&symfile_complaints,
3555 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3556 hex_string (lo), hex_string (hi));
24a55014 3557 continue;
f652bce2 3558 }
24a55014
DE
3559
3560 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
3561 {
3562 complaint (&symfile_complaints,
3563 _(".gdb_index address table has invalid CU number %u"),
3564 (unsigned) cu_index);
24a55014 3565 continue;
f652bce2 3566 }
24a55014 3567
3e29f34a
MR
3568 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3569 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
ed2dc618
SM
3570 addrmap_set_empty (mutable_map, lo, hi - 1,
3571 dw2_get_cutu (dwarf2_per_objfile, cu_index));
9291a0cd
TT
3572 }
3573
3574 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3575 &objfile->objfile_obstack);
9291a0cd
TT
3576}
3577
927aa2e7
JK
3578/* Read the address map data from DWARF-5 .debug_aranges, and use it to
3579 populate the objfile's psymtabs_addrmap. */
3580
3581static void
ed2dc618 3582create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3583 struct dwarf2_section_info *section)
3584{
ed2dc618 3585 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
3586 bfd *abfd = objfile->obfd;
3587 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3588 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3589 SECT_OFF_TEXT (objfile));
3590
3591 auto_obstack temp_obstack;
3592 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3593
3594 std::unordered_map<sect_offset,
3595 dwarf2_per_cu_data *,
3596 gdb::hash_enum<sect_offset>>
3597 debug_info_offset_to_per_cu;
3598 for (int cui = 0; cui < dwarf2_per_objfile->n_comp_units; ++cui)
3599 {
ed2dc618 3600 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, cui);
927aa2e7
JK
3601 const auto insertpair
3602 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3603 if (!insertpair.second)
3604 {
3605 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
3606 "debug_info_offset %s, ignoring .debug_aranges."),
3607 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
3608 return;
3609 }
3610 }
3611
3612 dwarf2_read_section (objfile, section);
3613
3614 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3615
3616 const gdb_byte *addr = section->buffer;
3617
3618 while (addr < section->buffer + section->size)
3619 {
3620 const gdb_byte *const entry_addr = addr;
3621 unsigned int bytes_read;
3622
3623 const LONGEST entry_length = read_initial_length (abfd, addr,
3624 &bytes_read);
3625 addr += bytes_read;
3626
3627 const gdb_byte *const entry_end = addr + entry_length;
3628 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3629 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3630 if (addr + entry_length > section->buffer + section->size)
3631 {
3632 warning (_("Section .debug_aranges in %s entry at offset %zu "
3633 "length %s exceeds section length %s, "
3634 "ignoring .debug_aranges."),
3635 objfile_name (objfile), entry_addr - section->buffer,
3636 plongest (bytes_read + entry_length),
3637 pulongest (section->size));
3638 return;
3639 }
3640
3641 /* The version number. */
3642 const uint16_t version = read_2_bytes (abfd, addr);
3643 addr += 2;
3644 if (version != 2)
3645 {
3646 warning (_("Section .debug_aranges in %s entry at offset %zu "
3647 "has unsupported version %d, ignoring .debug_aranges."),
3648 objfile_name (objfile), entry_addr - section->buffer,
3649 version);
3650 return;
3651 }
3652
3653 const uint64_t debug_info_offset
3654 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3655 addr += offset_size;
3656 const auto per_cu_it
3657 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3658 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3659 {
3660 warning (_("Section .debug_aranges in %s entry at offset %zu "
3661 "debug_info_offset %s does not exists, "
3662 "ignoring .debug_aranges."),
3663 objfile_name (objfile), entry_addr - section->buffer,
3664 pulongest (debug_info_offset));
3665 return;
3666 }
3667 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3668
3669 const uint8_t address_size = *addr++;
3670 if (address_size < 1 || address_size > 8)
3671 {
3672 warning (_("Section .debug_aranges in %s entry at offset %zu "
3673 "address_size %u is invalid, ignoring .debug_aranges."),
3674 objfile_name (objfile), entry_addr - section->buffer,
3675 address_size);
3676 return;
3677 }
3678
3679 const uint8_t segment_selector_size = *addr++;
3680 if (segment_selector_size != 0)
3681 {
3682 warning (_("Section .debug_aranges in %s entry at offset %zu "
3683 "segment_selector_size %u is not supported, "
3684 "ignoring .debug_aranges."),
3685 objfile_name (objfile), entry_addr - section->buffer,
3686 segment_selector_size);
3687 return;
3688 }
3689
3690 /* Must pad to an alignment boundary that is twice the address
3691 size. It is undocumented by the DWARF standard but GCC does
3692 use it. */
3693 for (size_t padding = ((-(addr - section->buffer))
3694 & (2 * address_size - 1));
3695 padding > 0; padding--)
3696 if (*addr++ != 0)
3697 {
3698 warning (_("Section .debug_aranges in %s entry at offset %zu "
3699 "padding is not zero, ignoring .debug_aranges."),
3700 objfile_name (objfile), entry_addr - section->buffer);
3701 return;
3702 }
3703
3704 for (;;)
3705 {
3706 if (addr + 2 * address_size > entry_end)
3707 {
3708 warning (_("Section .debug_aranges in %s entry at offset %zu "
3709 "address list is not properly terminated, "
3710 "ignoring .debug_aranges."),
3711 objfile_name (objfile), entry_addr - section->buffer);
3712 return;
3713 }
3714 ULONGEST start = extract_unsigned_integer (addr, address_size,
3715 dwarf5_byte_order);
3716 addr += address_size;
3717 ULONGEST length = extract_unsigned_integer (addr, address_size,
3718 dwarf5_byte_order);
3719 addr += address_size;
3720 if (start == 0 && length == 0)
3721 break;
3722 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3723 {
3724 /* Symbol was eliminated due to a COMDAT group. */
3725 continue;
3726 }
3727 ULONGEST end = start + length;
3728 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3729 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3730 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3731 }
3732 }
3733
3734 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3735 &objfile->objfile_obstack);
3736}
3737
59d7bcaf
JK
3738/* The hash function for strings in the mapped index. This is the same as
3739 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3740 implementation. This is necessary because the hash function is tied to the
3741 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
3742 SYMBOL_HASH_NEXT.
3743
3744 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 3745
9291a0cd 3746static hashval_t
559a7a62 3747mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
3748{
3749 const unsigned char *str = (const unsigned char *) p;
3750 hashval_t r = 0;
3751 unsigned char c;
3752
3753 while ((c = *str++) != 0)
559a7a62
JK
3754 {
3755 if (index_version >= 5)
3756 c = tolower (c);
3757 r = r * 67 + c - 113;
3758 }
9291a0cd
TT
3759
3760 return r;
3761}
3762
3763/* Find a slot in the mapped index INDEX for the object named NAME.
3764 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3765 constant pool and return true. If NAME cannot be found, return
3766 false. */
2fdf6df6 3767
109483d9 3768static bool
9291a0cd
TT
3769find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3770 offset_type **vec_out)
3771{
0cf03b49 3772 offset_type hash;
9291a0cd 3773 offset_type slot, step;
559a7a62 3774 int (*cmp) (const char *, const char *);
9291a0cd 3775
791afaa2 3776 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3777 if (current_language->la_language == language_cplus
45280282
IB
3778 || current_language->la_language == language_fortran
3779 || current_language->la_language == language_d)
0cf03b49
JK
3780 {
3781 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3782 not contain any. */
a8719064 3783
72998fb3 3784 if (strchr (name, '(') != NULL)
0cf03b49 3785 {
109483d9 3786 without_params = cp_remove_params (name);
0cf03b49 3787
72998fb3 3788 if (without_params != NULL)
791afaa2 3789 name = without_params.get ();
0cf03b49
JK
3790 }
3791 }
3792
559a7a62 3793 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3794 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3795 simulate our NAME being searched is also lowercased. */
3796 hash = mapped_index_string_hash ((index->version == 4
3797 && case_sensitivity == case_sensitive_off
3798 ? 5 : index->version),
3799 name);
3800
f00a2de2
PA
3801 slot = hash & (index->symbol_table.size () - 1);
3802 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3803 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3804
3805 for (;;)
3806 {
9291a0cd 3807 const char *str;
f00a2de2
PA
3808
3809 const auto &bucket = index->symbol_table[slot];
3810 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3811 return false;
9291a0cd 3812
f00a2de2 3813 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3814 if (!cmp (name, str))
9291a0cd
TT
3815 {
3816 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3817 + MAYBE_SWAP (bucket.vec));
109483d9 3818 return true;
9291a0cd
TT
3819 }
3820
f00a2de2 3821 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3822 }
3823}
3824
2ec9a5e0
TT
3825/* A helper function that reads the .gdb_index from SECTION and fills
3826 in MAP. FILENAME is the name of the file containing the section;
3827 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3828 ok to use deprecated sections.
3829
3830 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3831 out parameters that are filled in with information about the CU and
3832 TU lists in the section.
3833
3834 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3835
9291a0cd 3836static int
2ec9a5e0
TT
3837read_index_from_section (struct objfile *objfile,
3838 const char *filename,
3839 int deprecated_ok,
3840 struct dwarf2_section_info *section,
3841 struct mapped_index *map,
3842 const gdb_byte **cu_list,
3843 offset_type *cu_list_elements,
3844 const gdb_byte **types_list,
3845 offset_type *types_list_elements)
9291a0cd 3846{
948f8e3d 3847 const gdb_byte *addr;
2ec9a5e0 3848 offset_type version;
b3b272e1 3849 offset_type *metadata;
1fd400ff 3850 int i;
9291a0cd 3851
2ec9a5e0 3852 if (dwarf2_section_empty_p (section))
9291a0cd 3853 return 0;
82430852
JK
3854
3855 /* Older elfutils strip versions could keep the section in the main
3856 executable while splitting it for the separate debug info file. */
a32a8923 3857 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3858 return 0;
3859
2ec9a5e0 3860 dwarf2_read_section (objfile, section);
9291a0cd 3861
2ec9a5e0 3862 addr = section->buffer;
9291a0cd 3863 /* Version check. */
1fd400ff 3864 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3865 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3866 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3867 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3868 indices. */
831adc1f 3869 if (version < 4)
481860b3
GB
3870 {
3871 static int warning_printed = 0;
3872 if (!warning_printed)
3873 {
3874 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3875 filename);
481860b3
GB
3876 warning_printed = 1;
3877 }
3878 return 0;
3879 }
3880 /* Index version 4 uses a different hash function than index version
3881 5 and later.
3882
3883 Versions earlier than 6 did not emit psymbols for inlined
3884 functions. Using these files will cause GDB not to be able to
3885 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3886 indices unless the user has done
3887 "set use-deprecated-index-sections on". */
2ec9a5e0 3888 if (version < 6 && !deprecated_ok)
481860b3
GB
3889 {
3890 static int warning_printed = 0;
3891 if (!warning_printed)
3892 {
e615022a
DE
3893 warning (_("\
3894Skipping deprecated .gdb_index section in %s.\n\
3895Do \"set use-deprecated-index-sections on\" before the file is read\n\
3896to use the section anyway."),
2ec9a5e0 3897 filename);
481860b3
GB
3898 warning_printed = 1;
3899 }
3900 return 0;
3901 }
796a7ff8 3902 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3903 of the TU (for symbols coming from TUs),
3904 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3905 Plus gold-generated indices can have duplicate entries for global symbols,
3906 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3907 These are just performance bugs, and we can't distinguish gdb-generated
3908 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3909
481860b3 3910 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3911 longer backward compatible. */
796a7ff8 3912 if (version > 8)
594e8718 3913 return 0;
9291a0cd 3914
559a7a62 3915 map->version = version;
2ec9a5e0 3916 map->total_size = section->size;
9291a0cd
TT
3917
3918 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3919
3920 i = 0;
2ec9a5e0
TT
3921 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3922 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3923 / 8);
1fd400ff
TT
3924 ++i;
3925
2ec9a5e0
TT
3926 *types_list = addr + MAYBE_SWAP (metadata[i]);
3927 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3928 - MAYBE_SWAP (metadata[i]))
3929 / 8);
987d643c 3930 ++i;
1fd400ff 3931
f00a2de2
PA
3932 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3933 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3934 map->address_table
3935 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3936 ++i;
3937
f00a2de2
PA
3938 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3939 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3940 map->symbol_table
3941 = gdb::array_view<mapped_index::symbol_table_slot>
3942 ((mapped_index::symbol_table_slot *) symbol_table,
3943 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3944
f00a2de2 3945 ++i;
f9d83a0b 3946 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3947
2ec9a5e0
TT
3948 return 1;
3949}
3950
927aa2e7 3951/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3952 elements of all the CUs and return 1. Otherwise, return 0. */
3953
3954static int
3955dwarf2_read_index (struct objfile *objfile)
3956{
3957 struct mapped_index local_map, *map;
3958 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3959 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3960 struct dwz_file *dwz;
ed2dc618
SM
3961 struct dwarf2_per_objfile *dwarf2_per_objfile
3962 = get_dwarf2_per_objfile (objfile);
2ec9a5e0 3963
4262abfb 3964 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0
TT
3965 use_deprecated_index_sections,
3966 &dwarf2_per_objfile->gdb_index, &local_map,
3967 &cu_list, &cu_list_elements,
3968 &types_list, &types_list_elements))
3969 return 0;
3970
0fefef59 3971 /* Don't use the index if it's empty. */
f00a2de2 3972 if (local_map.symbol_table.empty ())
0fefef59
DE
3973 return 0;
3974
2ec9a5e0
TT
3975 /* If there is a .dwz file, read it so we can get its CU list as
3976 well. */
ed2dc618 3977 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3978 if (dwz != NULL)
2ec9a5e0 3979 {
2ec9a5e0
TT
3980 struct mapped_index dwz_map;
3981 const gdb_byte *dwz_types_ignore;
3982 offset_type dwz_types_elements_ignore;
3983
3984 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3985 1,
3986 &dwz->gdb_index, &dwz_map,
3987 &dwz_list, &dwz_list_elements,
3988 &dwz_types_ignore,
3989 &dwz_types_elements_ignore))
3990 {
3991 warning (_("could not read '.gdb_index' section from %s; skipping"),
3992 bfd_get_filename (dwz->dwz_bfd));
3993 return 0;
3994 }
3995 }
3996
74a0d9f6
JK
3997 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3998 dwz_list_elements);
1fd400ff 3999
8b70b953
TT
4000 if (types_list_elements)
4001 {
4002 struct dwarf2_section_info *section;
4003
4004 /* We can only handle a single .debug_types when we have an
4005 index. */
4006 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
4007 return 0;
4008
4009 section = VEC_index (dwarf2_section_info_def,
4010 dwarf2_per_objfile->types, 0);
4011
74a0d9f6
JK
4012 create_signatured_type_table_from_index (objfile, section, types_list,
4013 types_list_elements);
8b70b953 4014 }
9291a0cd 4015
ed2dc618 4016 create_addrmap_from_index (dwarf2_per_objfile, &local_map);
2ec9a5e0 4017
8d749320 4018 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3f563c84 4019 map = new (map) mapped_index ();
2ec9a5e0 4020 *map = local_map;
9291a0cd
TT
4021
4022 dwarf2_per_objfile->index_table = map;
4023 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
4024 dwarf2_per_objfile->quick_file_names_table =
4025 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
4026
4027 return 1;
4028}
4029
dee91e82 4030/* die_reader_func for dw2_get_file_names. */
2fdf6df6 4031
dee91e82
DE
4032static void
4033dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 4034 const gdb_byte *info_ptr,
dee91e82
DE
4035 struct die_info *comp_unit_die,
4036 int has_children,
4037 void *data)
9291a0cd 4038{
dee91e82 4039 struct dwarf2_cu *cu = reader->cu;
ed2dc618 4040 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
4041 struct dwarf2_per_objfile *dwarf2_per_objfile
4042 = cu->per_cu->dwarf2_per_objfile;
dee91e82 4043 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 4044 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 4045 struct attribute *attr;
dee91e82 4046 int i;
7b9f3c50
DE
4047 void **slot;
4048 struct quick_file_names *qfn;
9291a0cd 4049
0186c6a7
DE
4050 gdb_assert (! this_cu->is_debug_types);
4051
07261596
TT
4052 /* Our callers never want to match partial units -- instead they
4053 will match the enclosing full CU. */
4054 if (comp_unit_die->tag == DW_TAG_partial_unit)
4055 {
4056 this_cu->v.quick->no_file_data = 1;
4057 return;
4058 }
4059
0186c6a7 4060 lh_cu = this_cu;
7b9f3c50 4061 slot = NULL;
dee91e82 4062
fff8551c 4063 line_header_up lh;
9c541725 4064 sect_offset line_offset {};
fff8551c 4065
dee91e82 4066 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
4067 if (attr)
4068 {
7b9f3c50
DE
4069 struct quick_file_names find_entry;
4070
9c541725 4071 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
4072
4073 /* We may have already read in this line header (TU line header sharing).
4074 If we have we're done. */
094b34ac 4075 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 4076 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
4077 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
4078 &find_entry, INSERT);
4079 if (*slot != NULL)
4080 {
9a3c8263 4081 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 4082 return;
7b9f3c50
DE
4083 }
4084
3019eac3 4085 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
4086 }
4087 if (lh == NULL)
4088 {
094b34ac 4089 lh_cu->v.quick->no_file_data = 1;
dee91e82 4090 return;
9291a0cd
TT
4091 }
4092
8d749320 4093 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 4094 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 4095 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
4096 gdb_assert (slot != NULL);
4097 *slot = qfn;
9291a0cd 4098
d721ba37 4099 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 4100
fff8551c 4101 qfn->num_file_names = lh->file_names.size ();
8d749320 4102 qfn->file_names =
fff8551c
PA
4103 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
4104 for (i = 0; i < lh->file_names.size (); ++i)
4105 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 4106 qfn->real_names = NULL;
9291a0cd 4107
094b34ac 4108 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
4109}
4110
4111/* A helper for the "quick" functions which attempts to read the line
4112 table for THIS_CU. */
4113
4114static struct quick_file_names *
e4a48d9d 4115dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 4116{
0186c6a7
DE
4117 /* This should never be called for TUs. */
4118 gdb_assert (! this_cu->is_debug_types);
4119 /* Nor type unit groups. */
4120 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 4121
dee91e82
DE
4122 if (this_cu->v.quick->file_names != NULL)
4123 return this_cu->v.quick->file_names;
4124 /* If we know there is no line data, no point in looking again. */
4125 if (this_cu->v.quick->no_file_data)
4126 return NULL;
4127
0186c6a7 4128 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
4129
4130 if (this_cu->v.quick->no_file_data)
4131 return NULL;
4132 return this_cu->v.quick->file_names;
9291a0cd
TT
4133}
4134
4135/* A helper for the "quick" functions which computes and caches the
7b9f3c50 4136 real path for a given file name from the line table. */
2fdf6df6 4137
9291a0cd 4138static const char *
7b9f3c50
DE
4139dw2_get_real_path (struct objfile *objfile,
4140 struct quick_file_names *qfn, int index)
9291a0cd 4141{
7b9f3c50
DE
4142 if (qfn->real_names == NULL)
4143 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 4144 qfn->num_file_names, const char *);
9291a0cd 4145
7b9f3c50 4146 if (qfn->real_names[index] == NULL)
14278e1f 4147 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 4148
7b9f3c50 4149 return qfn->real_names[index];
9291a0cd
TT
4150}
4151
4152static struct symtab *
4153dw2_find_last_source_symtab (struct objfile *objfile)
4154{
ed2dc618
SM
4155 struct dwarf2_per_objfile *dwarf2_per_objfile
4156 = get_dwarf2_per_objfile (objfile);
4157 int index = dwarf2_per_objfile->n_comp_units - 1;
4158 dwarf2_per_cu_data *dwarf_cu = dw2_get_cutu (dwarf2_per_objfile, index);
4159 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu);
ae2de4f8 4160
43f3e411
DE
4161 if (cust == NULL)
4162 return NULL;
ed2dc618 4163
43f3e411 4164 return compunit_primary_filetab (cust);
9291a0cd
TT
4165}
4166
7b9f3c50
DE
4167/* Traversal function for dw2_forget_cached_source_info. */
4168
4169static int
4170dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 4171{
7b9f3c50 4172 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 4173
7b9f3c50 4174 if (file_data->real_names)
9291a0cd 4175 {
7b9f3c50 4176 int i;
9291a0cd 4177
7b9f3c50 4178 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 4179 {
7b9f3c50
DE
4180 xfree ((void*) file_data->real_names[i]);
4181 file_data->real_names[i] = NULL;
9291a0cd
TT
4182 }
4183 }
7b9f3c50
DE
4184
4185 return 1;
4186}
4187
4188static void
4189dw2_forget_cached_source_info (struct objfile *objfile)
4190{
ed2dc618
SM
4191 struct dwarf2_per_objfile *dwarf2_per_objfile
4192 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
4193
4194 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
4195 dw2_free_cached_file_names, NULL);
9291a0cd
TT
4196}
4197
f8eba3c6
TT
4198/* Helper function for dw2_map_symtabs_matching_filename that expands
4199 the symtabs and calls the iterator. */
4200
4201static int
4202dw2_map_expand_apply (struct objfile *objfile,
4203 struct dwarf2_per_cu_data *per_cu,
f5b95b50 4204 const char *name, const char *real_path,
14bc53a8 4205 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 4206{
43f3e411 4207 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
4208
4209 /* Don't visit already-expanded CUs. */
43f3e411 4210 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
4211 return 0;
4212
4213 /* This may expand more than one symtab, and we want to iterate over
4214 all of them. */
a0f42c21 4215 dw2_instantiate_symtab (per_cu);
f8eba3c6 4216
14bc53a8
PA
4217 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
4218 last_made, callback);
f8eba3c6
TT
4219}
4220
4221/* Implementation of the map_symtabs_matching_filename method. */
4222
14bc53a8
PA
4223static bool
4224dw2_map_symtabs_matching_filename
4225 (struct objfile *objfile, const char *name, const char *real_path,
4226 gdb::function_view<bool (symtab *)> callback)
9291a0cd
TT
4227{
4228 int i;
c011a4f4 4229 const char *name_basename = lbasename (name);
ed2dc618
SM
4230 struct dwarf2_per_objfile *dwarf2_per_objfile
4231 = get_dwarf2_per_objfile (objfile);
ae2de4f8 4232
848e3e78
DE
4233 /* The rule is CUs specify all the files, including those used by
4234 any TU, so there's no need to scan TUs here. */
f4dc4d17 4235
ed2dc618 4236 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4237 {
4238 int j;
ed2dc618 4239 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
7b9f3c50 4240 struct quick_file_names *file_data;
9291a0cd 4241
3d7bb9d9 4242 /* We only need to look at symtabs not already expanded. */
43f3e411 4243 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4244 continue;
4245
e4a48d9d 4246 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4247 if (file_data == NULL)
9291a0cd
TT
4248 continue;
4249
7b9f3c50 4250 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4251 {
7b9f3c50 4252 const char *this_name = file_data->file_names[j];
da235a7c 4253 const char *this_real_name;
9291a0cd 4254
af529f8f 4255 if (compare_filenames_for_search (this_name, name))
9291a0cd 4256 {
f5b95b50 4257 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
4258 callback))
4259 return true;
288e77a7 4260 continue;
4aac40c8 4261 }
9291a0cd 4262
c011a4f4
DE
4263 /* Before we invoke realpath, which can get expensive when many
4264 files are involved, do a quick comparison of the basenames. */
4265 if (! basenames_may_differ
4266 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
4267 continue;
4268
da235a7c
JK
4269 this_real_name = dw2_get_real_path (objfile, file_data, j);
4270 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 4271 {
da235a7c 4272 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
4273 callback))
4274 return true;
288e77a7 4275 continue;
da235a7c 4276 }
9291a0cd 4277
da235a7c
JK
4278 if (real_path != NULL)
4279 {
af529f8f
JK
4280 gdb_assert (IS_ABSOLUTE_PATH (real_path));
4281 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 4282 if (this_real_name != NULL
af529f8f 4283 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 4284 {
f5b95b50 4285 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
4286 callback))
4287 return true;
288e77a7 4288 continue;
9291a0cd
TT
4289 }
4290 }
4291 }
4292 }
4293
14bc53a8 4294 return false;
9291a0cd
TT
4295}
4296
da51c347
DE
4297/* Struct used to manage iterating over all CUs looking for a symbol. */
4298
4299struct dw2_symtab_iterator
9291a0cd 4300{
ed2dc618
SM
4301 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
4302 struct dwarf2_per_objfile *dwarf2_per_objfile;
da51c347
DE
4303 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
4304 int want_specific_block;
4305 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
4306 Unused if !WANT_SPECIFIC_BLOCK. */
4307 int block_index;
4308 /* The kind of symbol we're looking for. */
4309 domain_enum domain;
4310 /* The list of CUs from the index entry of the symbol,
4311 or NULL if not found. */
4312 offset_type *vec;
4313 /* The next element in VEC to look at. */
4314 int next;
4315 /* The number of elements in VEC, or zero if there is no match. */
4316 int length;
8943b874
DE
4317 /* Have we seen a global version of the symbol?
4318 If so we can ignore all further global instances.
4319 This is to work around gold/15646, inefficient gold-generated
4320 indices. */
4321 int global_seen;
da51c347 4322};
9291a0cd 4323
da51c347
DE
4324/* Initialize the index symtab iterator ITER.
4325 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
4326 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 4327
9291a0cd 4328static void
da51c347 4329dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 4330 struct dwarf2_per_objfile *dwarf2_per_objfile,
da51c347
DE
4331 int want_specific_block,
4332 int block_index,
4333 domain_enum domain,
4334 const char *name)
4335{
ed2dc618 4336 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
4337 iter->want_specific_block = want_specific_block;
4338 iter->block_index = block_index;
4339 iter->domain = domain;
4340 iter->next = 0;
8943b874 4341 iter->global_seen = 0;
da51c347 4342
ed2dc618
SM
4343 mapped_index *index = dwarf2_per_objfile->index_table;
4344
4345 /* index is NULL if OBJF_READNOW. */
4346 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
4347 iter->length = MAYBE_SWAP (*iter->vec);
4348 else
4349 {
4350 iter->vec = NULL;
4351 iter->length = 0;
4352 }
4353}
4354
4355/* Return the next matching CU or NULL if there are no more. */
4356
4357static struct dwarf2_per_cu_data *
4358dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
4359{
ed2dc618
SM
4360 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
4361
da51c347
DE
4362 for ( ; iter->next < iter->length; ++iter->next)
4363 {
4364 offset_type cu_index_and_attrs =
4365 MAYBE_SWAP (iter->vec[iter->next + 1]);
4366 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 4367 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
4368 int want_static = iter->block_index != GLOBAL_BLOCK;
4369 /* This value is only valid for index versions >= 7. */
4370 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4371 gdb_index_symbol_kind symbol_kind =
4372 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4373 /* Only check the symbol attributes if they're present.
4374 Indices prior to version 7 don't record them,
4375 and indices >= 7 may elide them for certain symbols
4376 (gold does this). */
4377 int attrs_valid =
ed2dc618 4378 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
4379 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4380
3190f0c6
DE
4381 /* Don't crash on bad data. */
4382 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4383 + dwarf2_per_objfile->n_type_units))
4384 {
4385 complaint (&symfile_complaints,
4386 _(".gdb_index entry has bad CU index"
4262abfb
JK
4387 " [in module %s]"),
4388 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
4389 continue;
4390 }
4391
ed2dc618 4392 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
3190f0c6 4393
da51c347 4394 /* Skip if already read in. */
43f3e411 4395 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
4396 continue;
4397
8943b874
DE
4398 /* Check static vs global. */
4399 if (attrs_valid)
4400 {
4401 if (iter->want_specific_block
4402 && want_static != is_static)
4403 continue;
4404 /* Work around gold/15646. */
4405 if (!is_static && iter->global_seen)
4406 continue;
4407 if (!is_static)
4408 iter->global_seen = 1;
4409 }
da51c347
DE
4410
4411 /* Only check the symbol's kind if it has one. */
4412 if (attrs_valid)
4413 {
4414 switch (iter->domain)
4415 {
4416 case VAR_DOMAIN:
4417 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4418 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4419 /* Some types are also in VAR_DOMAIN. */
4420 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4421 continue;
4422 break;
4423 case STRUCT_DOMAIN:
4424 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4425 continue;
4426 break;
4427 case LABEL_DOMAIN:
4428 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4429 continue;
4430 break;
4431 default:
4432 break;
4433 }
4434 }
4435
4436 ++iter->next;
4437 return per_cu;
4438 }
4439
4440 return NULL;
4441}
4442
43f3e411 4443static struct compunit_symtab *
da51c347
DE
4444dw2_lookup_symbol (struct objfile *objfile, int block_index,
4445 const char *name, domain_enum domain)
9291a0cd 4446{
43f3e411 4447 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
4448 struct dwarf2_per_objfile *dwarf2_per_objfile
4449 = get_dwarf2_per_objfile (objfile);
9291a0cd 4450
b5ec771e
PA
4451 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4452
ed2dc618
SM
4453 struct dw2_symtab_iterator iter;
4454 struct dwarf2_per_cu_data *per_cu;
da51c347 4455
ed2dc618 4456 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
9291a0cd 4457
ed2dc618
SM
4458 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4459 {
4460 struct symbol *sym, *with_opaque = NULL;
4461 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
4462 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4463 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4464
ed2dc618
SM
4465 sym = block_find_symbol (block, name, domain,
4466 block_find_non_opaque_type_preferred,
4467 &with_opaque);
b2e2f908 4468
ed2dc618
SM
4469 /* Some caution must be observed with overloaded functions
4470 and methods, since the index will not contain any overload
4471 information (but NAME might contain it). */
da51c347 4472
ed2dc618
SM
4473 if (sym != NULL
4474 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4475 return stab;
4476 if (with_opaque != NULL
4477 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4478 stab_best = stab;
da51c347 4479
ed2dc618 4480 /* Keep looking through other CUs. */
9291a0cd 4481 }
9291a0cd 4482
da51c347 4483 return stab_best;
9291a0cd
TT
4484}
4485
4486static void
4487dw2_print_stats (struct objfile *objfile)
4488{
ed2dc618
SM
4489 struct dwarf2_per_objfile *dwarf2_per_objfile
4490 = get_dwarf2_per_objfile (objfile);
4491 int total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
4492 int count = 0;
9291a0cd 4493
ed2dc618 4494 for (int i = 0; i < total; ++i)
9291a0cd 4495 {
ed2dc618 4496 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
9291a0cd 4497
43f3e411 4498 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4499 ++count;
4500 }
e4a48d9d 4501 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4502 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4503}
4504
779bd270
DE
4505/* This dumps minimal information about the index.
4506 It is called via "mt print objfiles".
4507 One use is to verify .gdb_index has been loaded by the
4508 gdb.dwarf2/gdb-index.exp testcase. */
4509
9291a0cd
TT
4510static void
4511dw2_dump (struct objfile *objfile)
4512{
ed2dc618
SM
4513 struct dwarf2_per_objfile *dwarf2_per_objfile
4514 = get_dwarf2_per_objfile (objfile);
4515
779bd270
DE
4516 gdb_assert (dwarf2_per_objfile->using_index);
4517 printf_filtered (".gdb_index:");
4518 if (dwarf2_per_objfile->index_table != NULL)
4519 {
4520 printf_filtered (" version %d\n",
4521 dwarf2_per_objfile->index_table->version);
4522 }
4523 else
4524 printf_filtered (" faked for \"readnow\"\n");
4525 printf_filtered ("\n");
9291a0cd
TT
4526}
4527
4528static void
3189cb12
DE
4529dw2_relocate (struct objfile *objfile,
4530 const struct section_offsets *new_offsets,
4531 const struct section_offsets *delta)
9291a0cd
TT
4532{
4533 /* There's nothing to relocate here. */
4534}
4535
4536static void
4537dw2_expand_symtabs_for_function (struct objfile *objfile,
4538 const char *func_name)
4539{
ed2dc618
SM
4540 struct dwarf2_per_objfile *dwarf2_per_objfile
4541 = get_dwarf2_per_objfile (objfile);
da51c347 4542
ed2dc618
SM
4543 struct dw2_symtab_iterator iter;
4544 struct dwarf2_per_cu_data *per_cu;
da51c347 4545
ed2dc618
SM
4546 /* Note: It doesn't matter what we pass for block_index here. */
4547 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4548 func_name);
da51c347 4549
ed2dc618
SM
4550 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4551 dw2_instantiate_symtab (per_cu);
da51c347 4552
9291a0cd
TT
4553}
4554
4555static void
4556dw2_expand_all_symtabs (struct objfile *objfile)
4557{
ed2dc618
SM
4558 struct dwarf2_per_objfile *dwarf2_per_objfile
4559 = get_dwarf2_per_objfile (objfile);
4560 int total_units = (dwarf2_per_objfile->n_comp_units
4561 + dwarf2_per_objfile->n_type_units);
9291a0cd 4562
ed2dc618 4563 for (int i = 0; i < total_units; ++i)
9291a0cd 4564 {
ed2dc618
SM
4565 struct dwarf2_per_cu_data *per_cu
4566 = dw2_get_cutu (dwarf2_per_objfile, i);
9291a0cd 4567
a0f42c21 4568 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4569 }
4570}
4571
4572static void
652a8996
JK
4573dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4574 const char *fullname)
9291a0cd 4575{
ed2dc618
SM
4576 struct dwarf2_per_objfile *dwarf2_per_objfile
4577 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
4578
4579 /* We don't need to consider type units here.
4580 This is only called for examining code, e.g. expand_line_sal.
4581 There can be an order of magnitude (or more) more type units
4582 than comp units, and we avoid them if we can. */
4583
ed2dc618 4584 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4585 {
4586 int j;
ed2dc618 4587 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
7b9f3c50 4588 struct quick_file_names *file_data;
9291a0cd 4589
3d7bb9d9 4590 /* We only need to look at symtabs not already expanded. */
43f3e411 4591 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4592 continue;
4593
e4a48d9d 4594 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4595 if (file_data == NULL)
9291a0cd
TT
4596 continue;
4597
7b9f3c50 4598 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4599 {
652a8996
JK
4600 const char *this_fullname = file_data->file_names[j];
4601
4602 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4603 {
a0f42c21 4604 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4605 break;
4606 }
4607 }
4608 }
4609}
4610
9291a0cd 4611static void
ade7ed9e 4612dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 4613 const char * name, domain_enum domain,
ade7ed9e 4614 int global,
40658b94
PH
4615 int (*callback) (struct block *,
4616 struct symbol *, void *),
b5ec771e 4617 void *data, symbol_name_match_type match,
2edb89d3 4618 symbol_compare_ftype *ordered_compare)
9291a0cd 4619{
40658b94 4620 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4621 current language is Ada for a non-Ada objfile using GNU index. As Ada
4622 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4623}
4624
b5ec771e
PA
4625/* Symbol name matcher for .gdb_index names.
4626
4627 Symbol names in .gdb_index have a few particularities:
4628
4629 - There's no indication of which is the language of each symbol.
4630
4631 Since each language has its own symbol name matching algorithm,
4632 and we don't know which language is the right one, we must match
3f563c84
PA
4633 each symbol against all languages. This would be a potential
4634 performance problem if it were not mitigated by the
4635 mapped_index::name_components lookup table, which significantly
4636 reduces the number of times we need to call into this matcher,
4637 making it a non-issue.
b5ec771e
PA
4638
4639 - Symbol names in the index have no overload (parameter)
4640 information. I.e., in C++, "foo(int)" and "foo(long)" both
4641 appear as "foo" in the index, for example.
4642
4643 This means that the lookup names passed to the symbol name
4644 matcher functions must have no parameter information either
4645 because (e.g.) symbol search name "foo" does not match
4646 lookup-name "foo(int)" [while swapping search name for lookup
4647 name would match].
4648*/
4649class gdb_index_symbol_name_matcher
4650{
4651public:
4652 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4653 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4654
4655 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4656 Returns true if any matcher matches. */
4657 bool matches (const char *symbol_name);
4658
4659private:
4660 /* A reference to the lookup name we're matching against. */
4661 const lookup_name_info &m_lookup_name;
4662
4663 /* A vector holding all the different symbol name matchers, for all
4664 languages. */
4665 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4666};
4667
4668gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4669 (const lookup_name_info &lookup_name)
4670 : m_lookup_name (lookup_name)
4671{
4672 /* Prepare the vector of comparison functions upfront, to avoid
4673 doing the same work for each symbol. Care is taken to avoid
4674 matching with the same matcher more than once if/when multiple
4675 languages use the same matcher function. */
4676 auto &matchers = m_symbol_name_matcher_funcs;
4677 matchers.reserve (nr_languages);
4678
4679 matchers.push_back (default_symbol_name_matcher);
4680
4681 for (int i = 0; i < nr_languages; i++)
4682 {
4683 const language_defn *lang = language_def ((enum language) i);
c63d3e8d 4684 symbol_name_matcher_ftype *name_matcher
618daa93 4685 = get_symbol_name_matcher (lang, m_lookup_name);
c63d3e8d
PA
4686
4687 /* Don't insert the same comparison routine more than once.
4688 Note that we do this linear walk instead of a seemingly
4689 cheaper sorted insert, or use a std::set or something like
4690 that, because relative order of function addresses is not
4691 stable. This is not a problem in practice because the number
4692 of supported languages is low, and the cost here is tiny
4693 compared to the number of searches we'll do afterwards using
4694 this object. */
4695 if (name_matcher != default_symbol_name_matcher
4696 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4697 == matchers.end ()))
4698 matchers.push_back (name_matcher);
b5ec771e
PA
4699 }
4700}
4701
4702bool
4703gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4704{
4705 for (auto matches_name : m_symbol_name_matcher_funcs)
4706 if (matches_name (symbol_name, m_lookup_name, NULL))
4707 return true;
4708
4709 return false;
4710}
4711
e1ef7d7a
PA
4712/* Starting from a search name, return the string that finds the upper
4713 bound of all strings that start with SEARCH_NAME in a sorted name
4714 list. Returns the empty string to indicate that the upper bound is
4715 the end of the list. */
4716
4717static std::string
4718make_sort_after_prefix_name (const char *search_name)
4719{
4720 /* When looking to complete "func", we find the upper bound of all
4721 symbols that start with "func" by looking for where we'd insert
4722 the closest string that would follow "func" in lexicographical
4723 order. Usually, that's "func"-with-last-character-incremented,
4724 i.e. "fund". Mind non-ASCII characters, though. Usually those
4725 will be UTF-8 multi-byte sequences, but we can't be certain.
4726 Especially mind the 0xff character, which is a valid character in
4727 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4728 rule out compilers allowing it in identifiers. Note that
4729 conveniently, strcmp/strcasecmp are specified to compare
4730 characters interpreted as unsigned char. So what we do is treat
4731 the whole string as a base 256 number composed of a sequence of
4732 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4733 to 0, and carries 1 to the following more-significant position.
4734 If the very first character in SEARCH_NAME ends up incremented
4735 and carries/overflows, then the upper bound is the end of the
4736 list. The string after the empty string is also the empty
4737 string.
4738
4739 Some examples of this operation:
4740
4741 SEARCH_NAME => "+1" RESULT
4742
4743 "abc" => "abd"
4744 "ab\xff" => "ac"
4745 "\xff" "a" "\xff" => "\xff" "b"
4746 "\xff" => ""
4747 "\xff\xff" => ""
4748 "" => ""
4749
4750 Then, with these symbols for example:
4751
4752 func
4753 func1
4754 fund
4755
4756 completing "func" looks for symbols between "func" and
4757 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4758 which finds "func" and "func1", but not "fund".
4759
4760 And with:
4761
4762 funcÿ (Latin1 'ÿ' [0xff])
4763 funcÿ1
4764 fund
4765
4766 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4767 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4768
4769 And with:
4770
4771 ÿÿ (Latin1 'ÿ' [0xff])
4772 ÿÿ1
4773
4774 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4775 the end of the list.
4776 */
4777 std::string after = search_name;
4778 while (!after.empty () && (unsigned char) after.back () == 0xff)
4779 after.pop_back ();
4780 if (!after.empty ())
4781 after.back () = (unsigned char) after.back () + 1;
4782 return after;
4783}
4784
5c58de74 4785/* See declaration. */
61d96d7e 4786
5c58de74
PA
4787std::pair<std::vector<name_component>::const_iterator,
4788 std::vector<name_component>::const_iterator>
44ed8f3e 4789mapped_index_base::find_name_components_bounds
5c58de74 4790 (const lookup_name_info &lookup_name_without_params) const
3f563c84 4791{
5c58de74
PA
4792 auto *name_cmp
4793 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84
PA
4794
4795 const char *cplus
c62446b1 4796 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
9291a0cd 4797
3f563c84
PA
4798 /* Comparison function object for lower_bound that matches against a
4799 given symbol name. */
4800 auto lookup_compare_lower = [&] (const name_component &elem,
4801 const char *name)
4802 {
5c58de74 4803 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4804 const char *elem_name = elem_qualified + elem.name_offset;
4805 return name_cmp (elem_name, name) < 0;
4806 };
4807
4808 /* Comparison function object for upper_bound that matches against a
4809 given symbol name. */
4810 auto lookup_compare_upper = [&] (const char *name,
4811 const name_component &elem)
4812 {
5c58de74 4813 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4814 const char *elem_name = elem_qualified + elem.name_offset;
4815 return name_cmp (name, elem_name) < 0;
4816 };
4817
5c58de74
PA
4818 auto begin = this->name_components.begin ();
4819 auto end = this->name_components.end ();
3f563c84
PA
4820
4821 /* Find the lower bound. */
4822 auto lower = [&] ()
4823 {
5c58de74 4824 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
3f563c84
PA
4825 return begin;
4826 else
4827 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4828 } ();
4829
4830 /* Find the upper bound. */
4831 auto upper = [&] ()
4832 {
5c58de74 4833 if (lookup_name_without_params.completion_mode ())
3f563c84 4834 {
e1ef7d7a
PA
4835 /* In completion mode, we want UPPER to point past all
4836 symbols names that have the same prefix. I.e., with
4837 these symbols, and completing "func":
4838
4839 function << lower bound
4840 function1
4841 other_function << upper bound
4842
4843 We find the upper bound by looking for the insertion
4844 point of "func"-with-last-character-incremented,
4845 i.e. "fund". */
4846 std::string after = make_sort_after_prefix_name (cplus);
4847 if (after.empty ())
3f563c84 4848 return end;
e6b2f5ef
PA
4849 return std::lower_bound (lower, end, after.c_str (),
4850 lookup_compare_lower);
3f563c84
PA
4851 }
4852 else
4853 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4854 } ();
4855
5c58de74
PA
4856 return {lower, upper};
4857}
4858
4859/* See declaration. */
4860
4861void
44ed8f3e 4862mapped_index_base::build_name_components ()
5c58de74
PA
4863{
4864 if (!this->name_components.empty ())
4865 return;
4866
4867 this->name_components_casing = case_sensitivity;
4868 auto *name_cmp
4869 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4870
4871 /* The code below only knows how to break apart components of C++
4872 symbol names (and other languages that use '::' as
4873 namespace/module separator). If we add support for wild matching
4874 to some language that uses some other operator (E.g., Ada, Go and
4875 D use '.'), then we'll need to try splitting the symbol name
4876 according to that language too. Note that Ada does support wild
4877 matching, but doesn't currently support .gdb_index. */
44ed8f3e
PA
4878 auto count = this->symbol_name_count ();
4879 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4880 {
44ed8f3e 4881 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4882 continue;
4883
4884 const char *name = this->symbol_name_at (idx);
4885
4886 /* Add each name component to the name component table. */
4887 unsigned int previous_len = 0;
4888 for (unsigned int current_len = cp_find_first_component (name);
4889 name[current_len] != '\0';
4890 current_len += cp_find_first_component (name + current_len))
4891 {
4892 gdb_assert (name[current_len] == ':');
4893 this->name_components.push_back ({previous_len, idx});
4894 /* Skip the '::'. */
4895 current_len += 2;
4896 previous_len = current_len;
4897 }
4898 this->name_components.push_back ({previous_len, idx});
4899 }
4900
4901 /* Sort name_components elements by name. */
4902 auto name_comp_compare = [&] (const name_component &left,
4903 const name_component &right)
4904 {
4905 const char *left_qualified = this->symbol_name_at (left.idx);
4906 const char *right_qualified = this->symbol_name_at (right.idx);
4907
4908 const char *left_name = left_qualified + left.name_offset;
4909 const char *right_name = right_qualified + right.name_offset;
4910
4911 return name_cmp (left_name, right_name) < 0;
4912 };
4913
4914 std::sort (this->name_components.begin (),
4915 this->name_components.end (),
4916 name_comp_compare);
4917}
4918
4919/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4920 mapped_index_base instead of the containing objfile. This is split
4921 to a separate function in order to be able to unit test the
4922 name_components matching using a mock mapped_index_base. For each
5c58de74 4923 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4924 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4925
4926static void
4927dw2_expand_symtabs_matching_symbol
44ed8f3e 4928 (mapped_index_base &index,
5c58de74
PA
4929 const lookup_name_info &lookup_name_in,
4930 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4931 enum search_domain kind,
4932 gdb::function_view<void (offset_type)> match_callback)
4933{
4934 lookup_name_info lookup_name_without_params
4935 = lookup_name_in.make_ignore_params ();
4936 gdb_index_symbol_name_matcher lookup_name_matcher
4937 (lookup_name_without_params);
4938
4939 /* Build the symbol name component sorted vector, if we haven't
4940 yet. */
4941 index.build_name_components ();
4942
4943 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4944
3f563c84
PA
4945 /* Now for each symbol name in range, check to see if we have a name
4946 match, and if so, call the MATCH_CALLBACK callback. */
4947
4948 /* The same symbol may appear more than once in the range though.
4949 E.g., if we're looking for symbols that complete "w", and we have
4950 a symbol named "w1::w2", we'll find the two name components for
4951 that same symbol in the range. To be sure we only call the
4952 callback once per symbol, we first collect the symbol name
4953 indexes that matched in a temporary vector and ignore
4954 duplicates. */
4955 std::vector<offset_type> matches;
5c58de74 4956 matches.reserve (std::distance (bounds.first, bounds.second));
3f563c84 4957
5c58de74 4958 for (; bounds.first != bounds.second; ++bounds.first)
3f563c84 4959 {
5c58de74 4960 const char *qualified = index.symbol_name_at (bounds.first->idx);
3f563c84
PA
4961
4962 if (!lookup_name_matcher.matches (qualified)
4963 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
9291a0cd
TT
4964 continue;
4965
5c58de74 4966 matches.push_back (bounds.first->idx);
3f563c84
PA
4967 }
4968
4969 std::sort (matches.begin (), matches.end ());
4970
4971 /* Finally call the callback, once per match. */
4972 ULONGEST prev = -1;
4973 for (offset_type idx : matches)
4974 {
4975 if (prev != idx)
4976 {
4977 match_callback (idx);
4978 prev = idx;
4979 }
4980 }
4981
4982 /* Above we use a type wider than idx's for 'prev', since 0 and
4983 (offset_type)-1 are both possible values. */
4984 static_assert (sizeof (prev) > sizeof (offset_type), "");
4985}
4986
c62446b1
PA
4987#if GDB_SELF_TEST
4988
4989namespace selftests { namespace dw2_expand_symtabs_matching {
4990
a3c5fafd
PA
4991/* A mock .gdb_index/.debug_names-like name index table, enough to
4992 exercise dw2_expand_symtabs_matching_symbol, which works with the
4993 mapped_index_base interface. Builds an index from the symbol list
4994 passed as parameter to the constructor. */
4995class mock_mapped_index : public mapped_index_base
c62446b1
PA
4996{
4997public:
a3c5fafd
PA
4998 mock_mapped_index (gdb::array_view<const char *> symbols)
4999 : m_symbol_table (symbols)
c62446b1
PA
5000 {}
5001
a3c5fafd 5002 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 5003
a3c5fafd
PA
5004 /* Return the number of names in the symbol table. */
5005 virtual size_t symbol_name_count () const
c62446b1 5006 {
a3c5fafd 5007 return m_symbol_table.size ();
c62446b1
PA
5008 }
5009
a3c5fafd
PA
5010 /* Get the name of the symbol at IDX in the symbol table. */
5011 virtual const char *symbol_name_at (offset_type idx) const
5012 {
5013 return m_symbol_table[idx];
5014 }
c62446b1 5015
a3c5fafd
PA
5016private:
5017 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
5018};
5019
5020/* Convenience function that converts a NULL pointer to a "<null>"
5021 string, to pass to print routines. */
5022
5023static const char *
5024string_or_null (const char *str)
5025{
5026 return str != NULL ? str : "<null>";
5027}
5028
5029/* Check if a lookup_name_info built from
5030 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
5031 index. EXPECTED_LIST is the list of expected matches, in expected
5032 matching order. If no match expected, then an empty list is
5033 specified. Returns true on success. On failure prints a warning
5034 indicating the file:line that failed, and returns false. */
5035
5036static bool
5037check_match (const char *file, int line,
5038 mock_mapped_index &mock_index,
5039 const char *name, symbol_name_match_type match_type,
5040 bool completion_mode,
5041 std::initializer_list<const char *> expected_list)
5042{
5043 lookup_name_info lookup_name (name, match_type, completion_mode);
5044
5045 bool matched = true;
5046
5047 auto mismatch = [&] (const char *expected_str,
5048 const char *got)
5049 {
5050 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
5051 "expected=\"%s\", got=\"%s\"\n"),
5052 file, line,
5053 (match_type == symbol_name_match_type::FULL
5054 ? "FULL" : "WILD"),
5055 name, string_or_null (expected_str), string_or_null (got));
5056 matched = false;
5057 };
5058
5059 auto expected_it = expected_list.begin ();
5060 auto expected_end = expected_list.end ();
5061
a3c5fafd 5062 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
5063 NULL, ALL_DOMAIN,
5064 [&] (offset_type idx)
5065 {
a3c5fafd 5066 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
5067 const char *expected_str
5068 = expected_it == expected_end ? NULL : *expected_it++;
5069
5070 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
5071 mismatch (expected_str, matched_name);
5072 });
5073
5074 const char *expected_str
5075 = expected_it == expected_end ? NULL : *expected_it++;
5076 if (expected_str != NULL)
5077 mismatch (expected_str, NULL);
5078
5079 return matched;
5080}
5081
5082/* The symbols added to the mock mapped_index for testing (in
5083 canonical form). */
5084static const char *test_symbols[] = {
5085 "function",
5086 "std::bar",
5087 "std::zfunction",
5088 "std::zfunction2",
5089 "w1::w2",
5090 "ns::foo<char*>",
5091 "ns::foo<int>",
5092 "ns::foo<long>",
a20714ff
PA
5093 "ns2::tmpl<int>::foo2",
5094 "(anonymous namespace)::A::B::C",
c62446b1 5095
e1ef7d7a
PA
5096 /* These are used to check that the increment-last-char in the
5097 matching algorithm for completion doesn't match "t1_fund" when
5098 completing "t1_func". */
5099 "t1_func",
5100 "t1_func1",
5101 "t1_fund",
5102 "t1_fund1",
5103
5104 /* A UTF-8 name with multi-byte sequences to make sure that
5105 cp-name-parser understands this as a single identifier ("função"
5106 is "function" in PT). */
5107 u8"u8função",
5108
5109 /* \377 (0xff) is Latin1 'ÿ'. */
5110 "yfunc\377",
5111
5112 /* \377 (0xff) is Latin1 'ÿ'. */
5113 "\377",
5114 "\377\377123",
5115
c62446b1
PA
5116 /* A name with all sorts of complications. Starts with "z" to make
5117 it easier for the completion tests below. */
5118#define Z_SYM_NAME \
5119 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
5120 "::tuple<(anonymous namespace)::ui*, " \
5121 "std::default_delete<(anonymous namespace)::ui>, void>"
5122
5123 Z_SYM_NAME
5124};
5125
a3c5fafd
PA
5126/* Returns true if the mapped_index_base::find_name_component_bounds
5127 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
5128 in completion mode. */
5c58de74
PA
5129
5130static bool
a3c5fafd 5131check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
5132 const char *search_name,
5133 gdb::array_view<const char *> expected_syms)
5134{
5135 lookup_name_info lookup_name (search_name,
5136 symbol_name_match_type::FULL, true);
5137
5138 auto bounds = index.find_name_components_bounds (lookup_name);
5139
5140 size_t distance = std::distance (bounds.first, bounds.second);
5141 if (distance != expected_syms.size ())
5142 return false;
5143
5144 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
5145 {
5146 auto nc_elem = bounds.first + exp_elem;
5147 const char *qualified = index.symbol_name_at (nc_elem->idx);
5148 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
5149 return false;
5150 }
5151
5152 return true;
5153}
5154
5155/* Test the lower-level mapped_index::find_name_component_bounds
5156 method. */
5157
c62446b1 5158static void
5c58de74
PA
5159test_mapped_index_find_name_component_bounds ()
5160{
5161 mock_mapped_index mock_index (test_symbols);
5162
a3c5fafd 5163 mock_index.build_name_components ();
5c58de74
PA
5164
5165 /* Test the lower-level mapped_index::find_name_component_bounds
5166 method in completion mode. */
5167 {
5168 static const char *expected_syms[] = {
5169 "t1_func",
5170 "t1_func1",
5c58de74
PA
5171 };
5172
a3c5fafd 5173 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
5174 "t1_func", expected_syms));
5175 }
5176
5177 /* Check that the increment-last-char in the name matching algorithm
5178 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
5179 {
5180 static const char *expected_syms1[] = {
5181 "\377",
5182 "\377\377123",
5183 };
a3c5fafd 5184 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
5185 "\377", expected_syms1));
5186
5187 static const char *expected_syms2[] = {
5188 "\377\377123",
5189 };
a3c5fafd 5190 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
5191 "\377\377", expected_syms2));
5192 }
5193}
5194
5195/* Test dw2_expand_symtabs_matching_symbol. */
5196
5197static void
5198test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
5199{
5200 mock_mapped_index mock_index (test_symbols);
5201
5202 /* We let all tests run until the end even if some fails, for debug
5203 convenience. */
5204 bool any_mismatch = false;
5205
5206 /* Create the expected symbols list (an initializer_list). Needed
5207 because lists have commas, and we need to pass them to CHECK,
5208 which is a macro. */
5209#define EXPECT(...) { __VA_ARGS__ }
5210
5211 /* Wrapper for check_match that passes down the current
5212 __FILE__/__LINE__. */
5213#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
5214 any_mismatch |= !check_match (__FILE__, __LINE__, \
5215 mock_index, \
5216 NAME, MATCH_TYPE, COMPLETION_MODE, \
5217 EXPECTED_LIST)
5218
5219 /* Identity checks. */
5220 for (const char *sym : test_symbols)
5221 {
5222 /* Should be able to match all existing symbols. */
5223 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
5224 EXPECT (sym));
5225
5226 /* Should be able to match all existing symbols with
5227 parameters. */
5228 std::string with_params = std::string (sym) + "(int)";
5229 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5230 EXPECT (sym));
5231
5232 /* Should be able to match all existing symbols with
5233 parameters and qualifiers. */
5234 with_params = std::string (sym) + " ( int ) const";
5235 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5236 EXPECT (sym));
5237
5238 /* This should really find sym, but cp-name-parser.y doesn't
5239 know about lvalue/rvalue qualifiers yet. */
5240 with_params = std::string (sym) + " ( int ) &&";
5241 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5242 {});
5243 }
5244
e1ef7d7a
PA
5245 /* Check that the name matching algorithm for completion doesn't get
5246 confused with Latin1 'ÿ' / 0xff. */
5247 {
5248 static const char str[] = "\377";
5249 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5250 EXPECT ("\377", "\377\377123"));
5251 }
5252
5253 /* Check that the increment-last-char in the matching algorithm for
5254 completion doesn't match "t1_fund" when completing "t1_func". */
5255 {
5256 static const char str[] = "t1_func";
5257 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5258 EXPECT ("t1_func", "t1_func1"));
5259 }
5260
c62446b1
PA
5261 /* Check that completion mode works at each prefix of the expected
5262 symbol name. */
5263 {
5264 static const char str[] = "function(int)";
5265 size_t len = strlen (str);
5266 std::string lookup;
5267
5268 for (size_t i = 1; i < len; i++)
5269 {
5270 lookup.assign (str, i);
5271 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5272 EXPECT ("function"));
5273 }
5274 }
5275
5276 /* While "w" is a prefix of both components, the match function
5277 should still only be called once. */
5278 {
5279 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
5280 EXPECT ("w1::w2"));
a20714ff
PA
5281 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
5282 EXPECT ("w1::w2"));
c62446b1
PA
5283 }
5284
5285 /* Same, with a "complicated" symbol. */
5286 {
5287 static const char str[] = Z_SYM_NAME;
5288 size_t len = strlen (str);
5289 std::string lookup;
5290
5291 for (size_t i = 1; i < len; i++)
5292 {
5293 lookup.assign (str, i);
5294 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5295 EXPECT (Z_SYM_NAME));
5296 }
5297 }
5298
5299 /* In FULL mode, an incomplete symbol doesn't match. */
5300 {
5301 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
5302 {});
5303 }
5304
5305 /* A complete symbol with parameters matches any overload, since the
5306 index has no overload info. */
5307 {
5308 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
5309 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
5310 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
5311 EXPECT ("std::zfunction", "std::zfunction2"));
5312 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
5313 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
5314 }
5315
5316 /* Check that whitespace is ignored appropriately. A symbol with a
5317 template argument list. */
5318 {
5319 static const char expected[] = "ns::foo<int>";
5320 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
5321 EXPECT (expected));
a20714ff
PA
5322 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
5323 EXPECT (expected));
c62446b1
PA
5324 }
5325
5326 /* Check that whitespace is ignored appropriately. A symbol with a
5327 template argument list that includes a pointer. */
5328 {
5329 static const char expected[] = "ns::foo<char*>";
5330 /* Try both completion and non-completion modes. */
5331 static const bool completion_mode[2] = {false, true};
5332 for (size_t i = 0; i < 2; i++)
5333 {
5334 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
5335 completion_mode[i], EXPECT (expected));
a20714ff
PA
5336 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
5337 completion_mode[i], EXPECT (expected));
c62446b1
PA
5338
5339 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
5340 completion_mode[i], EXPECT (expected));
a20714ff
PA
5341 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
5342 completion_mode[i], EXPECT (expected));
c62446b1
PA
5343 }
5344 }
5345
5346 {
5347 /* Check method qualifiers are ignored. */
5348 static const char expected[] = "ns::foo<char*>";
5349 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
5350 symbol_name_match_type::FULL, true, EXPECT (expected));
5351 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
5352 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
5353 CHECK_MATCH ("foo < char * > ( int ) const",
5354 symbol_name_match_type::WILD, true, EXPECT (expected));
5355 CHECK_MATCH ("foo < char * > ( int ) &&",
5356 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
5357 }
5358
5359 /* Test lookup names that don't match anything. */
5360 {
a20714ff
PA
5361 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
5362 {});
5363
c62446b1
PA
5364 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
5365 {});
5366 }
5367
a20714ff
PA
5368 /* Some wild matching tests, exercising "(anonymous namespace)",
5369 which should not be confused with a parameter list. */
5370 {
5371 static const char *syms[] = {
5372 "A::B::C",
5373 "B::C",
5374 "C",
5375 "A :: B :: C ( int )",
5376 "B :: C ( int )",
5377 "C ( int )",
5378 };
5379
5380 for (const char *s : syms)
5381 {
5382 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
5383 EXPECT ("(anonymous namespace)::A::B::C"));
5384 }
5385 }
5386
5387 {
5388 static const char expected[] = "ns2::tmpl<int>::foo2";
5389 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
5390 EXPECT (expected));
5391 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5392 EXPECT (expected));
5393 }
5394
c62446b1
PA
5395 SELF_CHECK (!any_mismatch);
5396
5397#undef EXPECT
5398#undef CHECK_MATCH
5399}
5400
5c58de74
PA
5401static void
5402run_test ()
5403{
5404 test_mapped_index_find_name_component_bounds ();
5405 test_dw2_expand_symtabs_matching_symbol ();
5406}
5407
c62446b1
PA
5408}} // namespace selftests::dw2_expand_symtabs_matching
5409
5410#endif /* GDB_SELF_TEST */
5411
4b514bc8
JK
5412/* If FILE_MATCHER is NULL or if PER_CU has
5413 dwarf2_per_cu_quick_data::MARK set (see
5414 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5415 EXPANSION_NOTIFY on it. */
5416
5417static void
5418dw2_expand_symtabs_matching_one
5419 (struct dwarf2_per_cu_data *per_cu,
5420 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5421 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5422{
5423 if (file_matcher == NULL || per_cu->v.quick->mark)
5424 {
5425 bool symtab_was_null
5426 = (per_cu->v.quick->compunit_symtab == NULL);
5427
5428 dw2_instantiate_symtab (per_cu);
5429
5430 if (expansion_notify != NULL
5431 && symtab_was_null
5432 && per_cu->v.quick->compunit_symtab != NULL)
5433 expansion_notify (per_cu->v.quick->compunit_symtab);
5434 }
5435}
5436
3f563c84
PA
5437/* Helper for dw2_expand_matching symtabs. Called on each symbol
5438 matched, to expand corresponding CUs that were marked. IDX is the
5439 index of the symbol name that matched. */
5440
5441static void
5442dw2_expand_marked_cus
ed2dc618 5443 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
5444 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5445 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5446 search_domain kind)
5447{
3f563c84
PA
5448 offset_type *vec, vec_len, vec_idx;
5449 bool global_seen = false;
ed2dc618 5450 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 5451
61920122 5452 vec = (offset_type *) (index.constant_pool
f00a2de2 5453 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5454 vec_len = MAYBE_SWAP (vec[0]);
5455 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5456 {
5457 struct dwarf2_per_cu_data *per_cu;
5458 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5459 /* This value is only valid for index versions >= 7. */
5460 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5461 gdb_index_symbol_kind symbol_kind =
5462 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5463 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5464 /* Only check the symbol attributes if they're present.
5465 Indices prior to version 7 don't record them,
5466 and indices >= 7 may elide them for certain symbols
5467 (gold does this). */
5468 int attrs_valid =
5469 (index.version >= 7
5470 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5471
5472 /* Work around gold/15646. */
5473 if (attrs_valid)
9291a0cd 5474 {
61920122
PA
5475 if (!is_static && global_seen)
5476 continue;
5477 if (!is_static)
5478 global_seen = true;
5479 }
3190f0c6 5480
61920122
PA
5481 /* Only check the symbol's kind if it has one. */
5482 if (attrs_valid)
5483 {
5484 switch (kind)
8943b874 5485 {
61920122
PA
5486 case VARIABLES_DOMAIN:
5487 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5488 continue;
5489 break;
5490 case FUNCTIONS_DOMAIN:
5491 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5492 continue;
61920122
PA
5493 break;
5494 case TYPES_DOMAIN:
5495 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5496 continue;
5497 break;
5498 default:
5499 break;
8943b874 5500 }
61920122 5501 }
8943b874 5502
61920122
PA
5503 /* Don't crash on bad data. */
5504 if (cu_index >= (dwarf2_per_objfile->n_comp_units
5505 + dwarf2_per_objfile->n_type_units))
5506 {
5507 complaint (&symfile_complaints,
5508 _(".gdb_index entry has bad CU index"
ed2dc618
SM
5509 " [in module %s]"),
5510 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
5511 continue;
5512 }
5513
ed2dc618 5514 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
4b514bc8
JK
5515 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5516 expansion_notify);
61920122
PA
5517 }
5518}
5519
4b514bc8
JK
5520/* If FILE_MATCHER is non-NULL, set all the
5521 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5522 that match FILE_MATCHER. */
5523
61920122 5524static void
4b514bc8 5525dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
5526 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5527 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5528{
4b514bc8 5529 if (file_matcher == NULL)
61920122
PA
5530 return;
5531
4b514bc8
JK
5532 objfile *const objfile = dwarf2_per_objfile->objfile;
5533
5534 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5535 htab_eq_pointer,
5536 NULL, xcalloc, xfree));
5537 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5538 htab_eq_pointer,
5539 NULL, xcalloc, xfree));
61920122 5540
4b514bc8
JK
5541 /* The rule is CUs specify all the files, including those used by
5542 any TU, so there's no need to scan TUs here. */
61920122 5543
927aa2e7
JK
5544 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5545 {
5546 int j;
ed2dc618 5547 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
927aa2e7
JK
5548 struct quick_file_names *file_data;
5549 void **slot;
5550
5551 QUIT;
5552
5553 per_cu->v.quick->mark = 0;
5554
5555 /* We only need to look at symtabs not already expanded. */
5556 if (per_cu->v.quick->compunit_symtab)
5557 continue;
5558
5559 file_data = dw2_get_file_names (per_cu);
5560 if (file_data == NULL)
5561 continue;
5562
5563 if (htab_find (visited_not_found.get (), file_data) != NULL)
5564 continue;
5565 else if (htab_find (visited_found.get (), file_data) != NULL)
5566 {
5567 per_cu->v.quick->mark = 1;
5568 continue;
5569 }
5570
5571 for (j = 0; j < file_data->num_file_names; ++j)
5572 {
5573 const char *this_real_name;
5574
5575 if (file_matcher (file_data->file_names[j], false))
5576 {
5577 per_cu->v.quick->mark = 1;
5578 break;
5579 }
5580
5581 /* Before we invoke realpath, which can get expensive when many
5582 files are involved, do a quick comparison of the basenames. */
5583 if (!basenames_may_differ
5584 && !file_matcher (lbasename (file_data->file_names[j]),
5585 true))
5586 continue;
5587
5588 this_real_name = dw2_get_real_path (objfile, file_data, j);
5589 if (file_matcher (this_real_name, false))
5590 {
5591 per_cu->v.quick->mark = 1;
5592 break;
5593 }
5594 }
5595
5596 slot = htab_find_slot (per_cu->v.quick->mark
5597 ? visited_found.get ()
5598 : visited_not_found.get (),
5599 file_data, INSERT);
5600 *slot = file_data;
5601 }
5602}
5603
5604static void
5605dw2_expand_symtabs_matching
5606 (struct objfile *objfile,
5607 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5608 const lookup_name_info &lookup_name,
5609 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5610 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5611 enum search_domain kind)
5612{
ed2dc618
SM
5613 struct dwarf2_per_objfile *dwarf2_per_objfile
5614 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5615
5616 /* index_table is NULL if OBJF_READNOW. */
5617 if (!dwarf2_per_objfile->index_table)
5618 return;
5619
ed2dc618 5620 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
5621
5622 mapped_index &index = *dwarf2_per_objfile->index_table;
5623
5624 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5625 symbol_matcher,
5626 kind, [&] (offset_type idx)
5627 {
ed2dc618 5628 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7
JK
5629 expansion_notify, kind);
5630 });
5631}
5632
5633/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5634 symtab. */
5635
5636static struct compunit_symtab *
5637recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5638 CORE_ADDR pc)
5639{
5640 int i;
5641
5642 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5643 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5644 return cust;
5645
5646 if (cust->includes == NULL)
5647 return NULL;
5648
5649 for (i = 0; cust->includes[i]; ++i)
5650 {
5651 struct compunit_symtab *s = cust->includes[i];
5652
5653 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5654 if (s != NULL)
5655 return s;
5656 }
5657
5658 return NULL;
5659}
5660
5661static struct compunit_symtab *
5662dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5663 struct bound_minimal_symbol msymbol,
5664 CORE_ADDR pc,
5665 struct obj_section *section,
5666 int warn_if_readin)
5667{
5668 struct dwarf2_per_cu_data *data;
5669 struct compunit_symtab *result;
5670
927aa2e7
JK
5671 if (!objfile->psymtabs_addrmap)
5672 return NULL;
5673
5674 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5675 pc);
5676 if (!data)
5677 return NULL;
5678
5679 if (warn_if_readin && data->v.quick->compunit_symtab)
5680 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5681 paddress (get_objfile_arch (objfile), pc));
5682
5683 result
5684 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5685 pc);
5686 gdb_assert (result != NULL);
5687 return result;
5688}
5689
5690static void
5691dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5692 void *data, int need_fullname)
5693{
ed2dc618
SM
5694 struct dwarf2_per_objfile *dwarf2_per_objfile
5695 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5696
5697 if (!dwarf2_per_objfile->filenames_cache)
5698 {
5699 dwarf2_per_objfile->filenames_cache.emplace ();
5700
5701 htab_up visited (htab_create_alloc (10,
5702 htab_hash_pointer, htab_eq_pointer,
5703 NULL, xcalloc, xfree));
5704
5705 /* The rule is CUs specify all the files, including those used
5706 by any TU, so there's no need to scan TUs here. We can
5707 ignore file names coming from already-expanded CUs. */
5708
5709 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5710 {
ed2dc618 5711 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
927aa2e7
JK
5712
5713 if (per_cu->v.quick->compunit_symtab)
5714 {
5715 void **slot = htab_find_slot (visited.get (),
5716 per_cu->v.quick->file_names,
5717 INSERT);
5718
5719 *slot = per_cu->v.quick->file_names;
5720 }
5721 }
5722
5723 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5724 {
ed2dc618 5725 dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
927aa2e7
JK
5726 struct quick_file_names *file_data;
5727 void **slot;
5728
5729 /* We only need to look at symtabs not already expanded. */
5730 if (per_cu->v.quick->compunit_symtab)
5731 continue;
5732
5733 file_data = dw2_get_file_names (per_cu);
5734 if (file_data == NULL)
5735 continue;
5736
5737 slot = htab_find_slot (visited.get (), file_data, INSERT);
5738 if (*slot)
5739 {
5740 /* Already visited. */
5741 continue;
5742 }
5743 *slot = file_data;
5744
5745 for (int j = 0; j < file_data->num_file_names; ++j)
5746 {
5747 const char *filename = file_data->file_names[j];
5748 dwarf2_per_objfile->filenames_cache->seen (filename);
5749 }
5750 }
5751 }
5752
5753 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5754 {
5755 gdb::unique_xmalloc_ptr<char> this_real_name;
5756
5757 if (need_fullname)
5758 this_real_name = gdb_realpath (filename);
5759 (*fun) (filename, this_real_name.get (), data);
5760 });
5761}
5762
5763static int
5764dw2_has_symbols (struct objfile *objfile)
5765{
5766 return 1;
5767}
5768
5769const struct quick_symbol_functions dwarf2_gdb_index_functions =
5770{
5771 dw2_has_symbols,
5772 dw2_find_last_source_symtab,
5773 dw2_forget_cached_source_info,
5774 dw2_map_symtabs_matching_filename,
5775 dw2_lookup_symbol,
5776 dw2_print_stats,
5777 dw2_dump,
5778 dw2_relocate,
5779 dw2_expand_symtabs_for_function,
5780 dw2_expand_all_symtabs,
5781 dw2_expand_symtabs_with_fullname,
5782 dw2_map_matching_symbols,
5783 dw2_expand_symtabs_matching,
5784 dw2_find_pc_sect_compunit_symtab,
5785 NULL,
5786 dw2_map_symbol_filenames
5787};
5788
5789/* DWARF-5 debug_names reader. */
5790
5791/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5792static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5793
5794/* A helper function that reads the .debug_names section in SECTION
5795 and fills in MAP. FILENAME is the name of the file containing the
5796 section; it is used for error reporting.
5797
5798 Returns true if all went well, false otherwise. */
5799
5800static bool
5801read_debug_names_from_section (struct objfile *objfile,
5802 const char *filename,
5803 struct dwarf2_section_info *section,
5804 mapped_debug_names &map)
5805{
5806 if (dwarf2_section_empty_p (section))
5807 return false;
5808
5809 /* Older elfutils strip versions could keep the section in the main
5810 executable while splitting it for the separate debug info file. */
5811 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5812 return false;
5813
5814 dwarf2_read_section (objfile, section);
5815
5816 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5817
5818 const gdb_byte *addr = section->buffer;
5819
5820 bfd *const abfd = get_section_bfd_owner (section);
5821
5822 unsigned int bytes_read;
5823 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5824 addr += bytes_read;
5825
5826 map.dwarf5_is_dwarf64 = bytes_read != 4;
5827 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5828 if (bytes_read + length != section->size)
5829 {
5830 /* There may be multiple per-CU indices. */
5831 warning (_("Section .debug_names in %s length %s does not match "
5832 "section length %s, ignoring .debug_names."),
5833 filename, plongest (bytes_read + length),
5834 pulongest (section->size));
5835 return false;
5836 }
5837
5838 /* The version number. */
5839 uint16_t version = read_2_bytes (abfd, addr);
5840 addr += 2;
5841 if (version != 5)
5842 {
5843 warning (_("Section .debug_names in %s has unsupported version %d, "
5844 "ignoring .debug_names."),
5845 filename, version);
5846 return false;
5847 }
5848
5849 /* Padding. */
5850 uint16_t padding = read_2_bytes (abfd, addr);
5851 addr += 2;
5852 if (padding != 0)
5853 {
5854 warning (_("Section .debug_names in %s has unsupported padding %d, "
5855 "ignoring .debug_names."),
5856 filename, padding);
5857 return false;
5858 }
5859
5860 /* comp_unit_count - The number of CUs in the CU list. */
5861 map.cu_count = read_4_bytes (abfd, addr);
5862 addr += 4;
5863
5864 /* local_type_unit_count - The number of TUs in the local TU
5865 list. */
5866 map.tu_count = read_4_bytes (abfd, addr);
5867 addr += 4;
5868
5869 /* foreign_type_unit_count - The number of TUs in the foreign TU
5870 list. */
5871 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5872 addr += 4;
5873 if (foreign_tu_count != 0)
5874 {
5875 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5876 "ignoring .debug_names."),
5877 filename, static_cast<unsigned long> (foreign_tu_count));
5878 return false;
5879 }
5880
5881 /* bucket_count - The number of hash buckets in the hash lookup
5882 table. */
5883 map.bucket_count = read_4_bytes (abfd, addr);
5884 addr += 4;
5885
5886 /* name_count - The number of unique names in the index. */
5887 map.name_count = read_4_bytes (abfd, addr);
5888 addr += 4;
5889
5890 /* abbrev_table_size - The size in bytes of the abbreviations
5891 table. */
5892 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5893 addr += 4;
5894
5895 /* augmentation_string_size - The size in bytes of the augmentation
5896 string. This value is rounded up to a multiple of 4. */
5897 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5898 addr += 4;
5899 map.augmentation_is_gdb = ((augmentation_string_size
5900 == sizeof (dwarf5_augmentation))
5901 && memcmp (addr, dwarf5_augmentation,
5902 sizeof (dwarf5_augmentation)) == 0);
5903 augmentation_string_size += (-augmentation_string_size) & 3;
5904 addr += augmentation_string_size;
5905
5906 /* List of CUs */
5907 map.cu_table_reordered = addr;
5908 addr += map.cu_count * map.offset_size;
5909
5910 /* List of Local TUs */
5911 map.tu_table_reordered = addr;
5912 addr += map.tu_count * map.offset_size;
5913
5914 /* Hash Lookup Table */
5915 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5916 addr += map.bucket_count * 4;
5917 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5918 addr += map.name_count * 4;
5919
5920 /* Name Table */
5921 map.name_table_string_offs_reordered = addr;
5922 addr += map.name_count * map.offset_size;
5923 map.name_table_entry_offs_reordered = addr;
5924 addr += map.name_count * map.offset_size;
5925
5926 const gdb_byte *abbrev_table_start = addr;
5927 for (;;)
5928 {
5929 unsigned int bytes_read;
5930 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5931 addr += bytes_read;
5932 if (index_num == 0)
5933 break;
5934
5935 const auto insertpair
5936 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5937 if (!insertpair.second)
5938 {
5939 warning (_("Section .debug_names in %s has duplicate index %s, "
5940 "ignoring .debug_names."),
5941 filename, pulongest (index_num));
5942 return false;
5943 }
5944 mapped_debug_names::index_val &indexval = insertpair.first->second;
5945 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5946 addr += bytes_read;
5947
5948 for (;;)
5949 {
5950 mapped_debug_names::index_val::attr attr;
5951 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5952 addr += bytes_read;
5953 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5954 addr += bytes_read;
5955 if (attr.form == DW_FORM_implicit_const)
5956 {
5957 attr.implicit_const = read_signed_leb128 (abfd, addr,
5958 &bytes_read);
5959 addr += bytes_read;
5960 }
5961 if (attr.dw_idx == 0 && attr.form == 0)
5962 break;
5963 indexval.attr_vec.push_back (std::move (attr));
5964 }
5965 }
5966 if (addr != abbrev_table_start + abbrev_table_size)
5967 {
5968 warning (_("Section .debug_names in %s has abbreviation_table "
5969 "of size %zu vs. written as %u, ignoring .debug_names."),
5970 filename, addr - abbrev_table_start, abbrev_table_size);
5971 return false;
5972 }
5973 map.entry_pool = addr;
5974
5975 return true;
5976}
5977
5978/* A helper for create_cus_from_debug_names that handles the MAP's CU
5979 list. */
5980
5981static void
ed2dc618 5982create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5983 const mapped_debug_names &map,
5984 dwarf2_section_info &section,
5985 bool is_dwz, int base_offset)
5986{
5987 sect_offset sect_off_prev;
5988 for (uint32_t i = 0; i <= map.cu_count; ++i)
5989 {
5990 sect_offset sect_off_next;
5991 if (i < map.cu_count)
5992 {
5993 sect_off_next
5994 = (sect_offset) (extract_unsigned_integer
5995 (map.cu_table_reordered + i * map.offset_size,
5996 map.offset_size,
5997 map.dwarf5_byte_order));
5998 }
5999 else
6000 sect_off_next = (sect_offset) section.size;
6001 if (i >= 1)
6002 {
6003 const ULONGEST length = sect_off_next - sect_off_prev;
6004 dwarf2_per_objfile->all_comp_units[base_offset + (i - 1)]
ed2dc618 6005 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7
JK
6006 sect_off_prev, length);
6007 }
6008 sect_off_prev = sect_off_next;
6009 }
6010}
6011
6012/* Read the CU list from the mapped index, and use it to create all
ed2dc618 6013 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
6014
6015static void
ed2dc618 6016create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
6017 const mapped_debug_names &map,
6018 const mapped_debug_names &dwz_map)
6019{
ed2dc618 6020 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
6021
6022 dwarf2_per_objfile->n_comp_units = map.cu_count + dwz_map.cu_count;
6023 dwarf2_per_objfile->all_comp_units
6024 = XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
6025 dwarf2_per_objfile->n_comp_units);
6026
ed2dc618
SM
6027 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
6028 dwarf2_per_objfile->info,
927aa2e7
JK
6029 false /* is_dwz */,
6030 0 /* base_offset */);
6031
6032 if (dwz_map.cu_count == 0)
6033 return;
6034
ed2dc618
SM
6035 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
6036 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
927aa2e7
JK
6037 true /* is_dwz */,
6038 map.cu_count /* base_offset */);
6039}
6040
6041/* Read .debug_names. If everything went ok, initialize the "quick"
6042 elements of all the CUs and return true. Otherwise, return false. */
6043
6044static bool
ed2dc618 6045dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 6046{
ed2dc618
SM
6047 mapped_debug_names local_map (dwarf2_per_objfile);
6048 mapped_debug_names dwz_map (dwarf2_per_objfile);
6049 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
6050
6051 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
6052 &dwarf2_per_objfile->debug_names,
6053 local_map))
6054 return false;
6055
6056 /* Don't use the index if it's empty. */
6057 if (local_map.name_count == 0)
6058 return false;
6059
6060 /* If there is a .dwz file, read it so we can get its CU list as
6061 well. */
ed2dc618 6062 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
6063 if (dwz != NULL)
6064 {
6065 if (!read_debug_names_from_section (objfile,
6066 bfd_get_filename (dwz->dwz_bfd),
6067 &dwz->debug_names, dwz_map))
6068 {
6069 warning (_("could not read '.debug_names' section from %s; skipping"),
6070 bfd_get_filename (dwz->dwz_bfd));
6071 return false;
6072 }
6073 }
6074
ed2dc618 6075 create_cus_from_debug_names (dwarf2_per_objfile, local_map, dwz_map);
927aa2e7
JK
6076
6077 if (local_map.tu_count != 0)
6078 {
6079 /* We can only handle a single .debug_types when we have an
6080 index. */
6081 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
6082 return false;
6083
6084 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
6085 dwarf2_per_objfile->types, 0);
6086
6087 create_signatured_type_table_from_debug_names
ed2dc618 6088 (dwarf2_per_objfile, local_map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
6089 }
6090
ed2dc618
SM
6091 create_addrmap_from_aranges (dwarf2_per_objfile,
6092 &dwarf2_per_objfile->debug_aranges);
927aa2e7 6093
ed2dc618
SM
6094 dwarf2_per_objfile->debug_names_table.reset
6095 (new mapped_debug_names (dwarf2_per_objfile));
927aa2e7
JK
6096 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
6097 dwarf2_per_objfile->using_index = 1;
6098 dwarf2_per_objfile->quick_file_names_table =
6099 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6100
6101 return true;
6102}
6103
6104/* Symbol name hashing function as specified by DWARF-5. */
6105
6106static uint32_t
6107dwarf5_djb_hash (const char *str_)
6108{
6109 const unsigned char *str = (const unsigned char *) str_;
6110
6111 /* Note: tolower here ignores UTF-8, which isn't fully compliant.
6112 See http://dwarfstd.org/ShowIssue.php?issue=161027.1. */
6113
6114 uint32_t hash = 5381;
6115 while (int c = *str++)
6116 hash = hash * 33 + tolower (c);
6117 return hash;
6118}
6119
6120/* Type used to manage iterating over all CUs looking for a symbol for
6121 .debug_names. */
6122
6123class dw2_debug_names_iterator
6124{
6125public:
6126 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
6127 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
6128 dw2_debug_names_iterator (const mapped_debug_names &map,
6129 bool want_specific_block,
6130 block_enum block_index, domain_enum domain,
6131 const char *name)
6132 : m_map (map), m_want_specific_block (want_specific_block),
6133 m_block_index (block_index), m_domain (domain),
6134 m_addr (find_vec_in_debug_names (map, name))
6135 {}
6136
6137 dw2_debug_names_iterator (const mapped_debug_names &map,
6138 search_domain search, uint32_t namei)
6139 : m_map (map),
6140 m_search (search),
6141 m_addr (find_vec_in_debug_names (map, namei))
6142 {}
6143
6144 /* Return the next matching CU or NULL if there are no more. */
6145 dwarf2_per_cu_data *next ();
6146
6147private:
6148 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6149 const char *name);
6150 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6151 uint32_t namei);
6152
6153 /* The internalized form of .debug_names. */
6154 const mapped_debug_names &m_map;
6155
6156 /* If true, only look for symbols that match BLOCK_INDEX. */
6157 const bool m_want_specific_block = false;
6158
6159 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
6160 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
6161 value. */
6162 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
6163
6164 /* The kind of symbol we're looking for. */
6165 const domain_enum m_domain = UNDEF_DOMAIN;
6166 const search_domain m_search = ALL_DOMAIN;
6167
6168 /* The list of CUs from the index entry of the symbol, or NULL if
6169 not found. */
6170 const gdb_byte *m_addr;
6171};
6172
6173const char *
6174mapped_debug_names::namei_to_name (uint32_t namei) const
6175{
6176 const ULONGEST namei_string_offs
6177 = extract_unsigned_integer ((name_table_string_offs_reordered
6178 + namei * offset_size),
6179 offset_size,
6180 dwarf5_byte_order);
6181 return read_indirect_string_at_offset
ed2dc618 6182 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
6183}
6184
6185/* Find a slot in .debug_names for the object named NAME. If NAME is
6186 found, return pointer to its pool data. If NAME cannot be found,
6187 return NULL. */
6188
6189const gdb_byte *
6190dw2_debug_names_iterator::find_vec_in_debug_names
6191 (const mapped_debug_names &map, const char *name)
6192{
6193 int (*cmp) (const char *, const char *);
6194
6195 if (current_language->la_language == language_cplus
6196 || current_language->la_language == language_fortran
6197 || current_language->la_language == language_d)
6198 {
6199 /* NAME is already canonical. Drop any qualifiers as
6200 .debug_names does not contain any. */
6201
6202 if (strchr (name, '(') != NULL)
6203 {
6204 gdb::unique_xmalloc_ptr<char> without_params
6205 = cp_remove_params (name);
6206
6207 if (without_params != NULL)
6208 {
6209 name = without_params.get();
6210 }
6211 }
6212 }
6213
6214 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
6215
6216 const uint32_t full_hash = dwarf5_djb_hash (name);
6217 uint32_t namei
6218 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6219 (map.bucket_table_reordered
6220 + (full_hash % map.bucket_count)), 4,
6221 map.dwarf5_byte_order);
6222 if (namei == 0)
6223 return NULL;
6224 --namei;
6225 if (namei >= map.name_count)
6226 {
6227 complaint (&symfile_complaints,
6228 _("Wrong .debug_names with name index %u but name_count=%u "
6229 "[in module %s]"),
6230 namei, map.name_count,
ed2dc618 6231 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
6232 return NULL;
6233 }
6234
6235 for (;;)
6236 {
6237 const uint32_t namei_full_hash
6238 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6239 (map.hash_table_reordered + namei), 4,
6240 map.dwarf5_byte_order);
6241 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
6242 return NULL;
6243
6244 if (full_hash == namei_full_hash)
6245 {
6246 const char *const namei_string = map.namei_to_name (namei);
6247
6248#if 0 /* An expensive sanity check. */
6249 if (namei_full_hash != dwarf5_djb_hash (namei_string))
6250 {
6251 complaint (&symfile_complaints,
6252 _("Wrong .debug_names hash for string at index %u "
6253 "[in module %s]"),
6254 namei, objfile_name (dwarf2_per_objfile->objfile));
6255 return NULL;
6256 }
6257#endif
6258
6259 if (cmp (namei_string, name) == 0)
6260 {
6261 const ULONGEST namei_entry_offs
6262 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6263 + namei * map.offset_size),
6264 map.offset_size, map.dwarf5_byte_order);
6265 return map.entry_pool + namei_entry_offs;
6266 }
6267 }
6268
6269 ++namei;
6270 if (namei >= map.name_count)
6271 return NULL;
6272 }
6273}
6274
6275const gdb_byte *
6276dw2_debug_names_iterator::find_vec_in_debug_names
6277 (const mapped_debug_names &map, uint32_t namei)
6278{
6279 if (namei >= map.name_count)
6280 {
6281 complaint (&symfile_complaints,
6282 _("Wrong .debug_names with name index %u but name_count=%u "
6283 "[in module %s]"),
6284 namei, map.name_count,
ed2dc618 6285 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
6286 return NULL;
6287 }
6288
6289 const ULONGEST namei_entry_offs
6290 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6291 + namei * map.offset_size),
6292 map.offset_size, map.dwarf5_byte_order);
6293 return map.entry_pool + namei_entry_offs;
6294}
6295
6296/* See dw2_debug_names_iterator. */
6297
6298dwarf2_per_cu_data *
6299dw2_debug_names_iterator::next ()
6300{
6301 if (m_addr == NULL)
6302 return NULL;
6303
ed2dc618
SM
6304 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
6305 struct objfile *objfile = dwarf2_per_objfile->objfile;
6306 bfd *const abfd = objfile->obfd;
927aa2e7
JK
6307
6308 again:
6309
6310 unsigned int bytes_read;
6311 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6312 m_addr += bytes_read;
6313 if (abbrev == 0)
6314 return NULL;
6315
6316 const auto indexval_it = m_map.abbrev_map.find (abbrev);
6317 if (indexval_it == m_map.abbrev_map.cend ())
6318 {
6319 complaint (&symfile_complaints,
6320 _("Wrong .debug_names undefined abbrev code %s "
6321 "[in module %s]"),
ed2dc618 6322 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
6323 return NULL;
6324 }
6325 const mapped_debug_names::index_val &indexval = indexval_it->second;
6326 bool have_is_static = false;
6327 bool is_static;
6328 dwarf2_per_cu_data *per_cu = NULL;
6329 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
6330 {
6331 ULONGEST ull;
6332 switch (attr.form)
6333 {
6334 case DW_FORM_implicit_const:
6335 ull = attr.implicit_const;
6336 break;
6337 case DW_FORM_flag_present:
6338 ull = 1;
6339 break;
6340 case DW_FORM_udata:
6341 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6342 m_addr += bytes_read;
6343 break;
6344 default:
6345 complaint (&symfile_complaints,
6346 _("Unsupported .debug_names form %s [in module %s]"),
6347 dwarf_form_name (attr.form),
ed2dc618 6348 objfile_name (objfile));
927aa2e7
JK
6349 return NULL;
6350 }
6351 switch (attr.dw_idx)
6352 {
6353 case DW_IDX_compile_unit:
6354 /* Don't crash on bad data. */
8af5c486 6355 if (ull >= dwarf2_per_objfile->n_comp_units)
927aa2e7
JK
6356 {
6357 complaint (&symfile_complaints,
6358 _(".debug_names entry has bad CU index %s"
6359 " [in module %s]"),
6360 pulongest (ull),
6361 objfile_name (dwarf2_per_objfile->objfile));
6362 continue;
6363 }
ed2dc618 6364 per_cu = dw2_get_cutu (dwarf2_per_objfile, ull);
927aa2e7 6365 break;
8af5c486
JK
6366 case DW_IDX_type_unit:
6367 /* Don't crash on bad data. */
6368 if (ull >= dwarf2_per_objfile->n_type_units)
6369 {
6370 complaint (&symfile_complaints,
6371 _(".debug_names entry has bad TU index %s"
6372 " [in module %s]"),
6373 pulongest (ull),
6374 objfile_name (dwarf2_per_objfile->objfile));
6375 continue;
6376 }
ed2dc618
SM
6377 per_cu = dw2_get_cutu (dwarf2_per_objfile,
6378 dwarf2_per_objfile->n_comp_units + ull);
8af5c486 6379 break;
927aa2e7
JK
6380 case DW_IDX_GNU_internal:
6381 if (!m_map.augmentation_is_gdb)
6382 break;
6383 have_is_static = true;
6384 is_static = true;
6385 break;
6386 case DW_IDX_GNU_external:
6387 if (!m_map.augmentation_is_gdb)
6388 break;
6389 have_is_static = true;
6390 is_static = false;
6391 break;
6392 }
6393 }
6394
6395 /* Skip if already read in. */
6396 if (per_cu->v.quick->compunit_symtab)
6397 goto again;
6398
6399 /* Check static vs global. */
6400 if (have_is_static)
6401 {
6402 const bool want_static = m_block_index != GLOBAL_BLOCK;
6403 if (m_want_specific_block && want_static != is_static)
6404 goto again;
6405 }
6406
6407 /* Match dw2_symtab_iter_next, symbol_kind
6408 and debug_names::psymbol_tag. */
6409 switch (m_domain)
6410 {
6411 case VAR_DOMAIN:
6412 switch (indexval.dwarf_tag)
6413 {
6414 case DW_TAG_variable:
6415 case DW_TAG_subprogram:
6416 /* Some types are also in VAR_DOMAIN. */
6417 case DW_TAG_typedef:
6418 case DW_TAG_structure_type:
6419 break;
6420 default:
6421 goto again;
6422 }
6423 break;
6424 case STRUCT_DOMAIN:
6425 switch (indexval.dwarf_tag)
6426 {
6427 case DW_TAG_typedef:
6428 case DW_TAG_structure_type:
6429 break;
6430 default:
6431 goto again;
6432 }
6433 break;
6434 case LABEL_DOMAIN:
6435 switch (indexval.dwarf_tag)
6436 {
6437 case 0:
6438 case DW_TAG_variable:
6439 break;
6440 default:
6441 goto again;
6442 }
6443 break;
6444 default:
6445 break;
6446 }
6447
6448 /* Match dw2_expand_symtabs_matching, symbol_kind and
6449 debug_names::psymbol_tag. */
6450 switch (m_search)
4b514bc8 6451 {
927aa2e7
JK
6452 case VARIABLES_DOMAIN:
6453 switch (indexval.dwarf_tag)
4b514bc8 6454 {
927aa2e7
JK
6455 case DW_TAG_variable:
6456 break;
6457 default:
6458 goto again;
4b514bc8 6459 }
927aa2e7
JK
6460 break;
6461 case FUNCTIONS_DOMAIN:
6462 switch (indexval.dwarf_tag)
4b514bc8 6463 {
927aa2e7
JK
6464 case DW_TAG_subprogram:
6465 break;
6466 default:
6467 goto again;
4b514bc8 6468 }
927aa2e7
JK
6469 break;
6470 case TYPES_DOMAIN:
6471 switch (indexval.dwarf_tag)
6472 {
6473 case DW_TAG_typedef:
6474 case DW_TAG_structure_type:
6475 break;
6476 default:
6477 goto again;
6478 }
6479 break;
6480 default:
6481 break;
4b514bc8 6482 }
927aa2e7
JK
6483
6484 return per_cu;
4b514bc8 6485}
61920122 6486
927aa2e7
JK
6487static struct compunit_symtab *
6488dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6489 const char *name, domain_enum domain)
4b514bc8 6490{
927aa2e7 6491 const block_enum block_index = static_cast<block_enum> (block_index_int);
ed2dc618
SM
6492 struct dwarf2_per_objfile *dwarf2_per_objfile
6493 = get_dwarf2_per_objfile (objfile);
61920122 6494
927aa2e7
JK
6495 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6496 if (!mapp)
61920122 6497 {
927aa2e7
JK
6498 /* index is NULL if OBJF_READNOW. */
6499 return NULL;
6500 }
6501 const auto &map = *mapp;
9291a0cd 6502
927aa2e7
JK
6503 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6504 block_index, domain, name);
9703b513 6505
927aa2e7
JK
6506 struct compunit_symtab *stab_best = NULL;
6507 struct dwarf2_per_cu_data *per_cu;
6508 while ((per_cu = iter.next ()) != NULL)
6509 {
6510 struct symbol *sym, *with_opaque = NULL;
6511 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6512 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6513 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6514
927aa2e7
JK
6515 sym = block_find_symbol (block, name, domain,
6516 block_find_non_opaque_type_preferred,
6517 &with_opaque);
9703b513 6518
927aa2e7
JK
6519 /* Some caution must be observed with overloaded functions and
6520 methods, since the index will not contain any overload
6521 information (but NAME might contain it). */
a3ec0bb1 6522
927aa2e7
JK
6523 if (sym != NULL
6524 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6525 return stab;
6526 if (with_opaque != NULL
6527 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6528 stab_best = stab;
9703b513 6529
927aa2e7 6530 /* Keep looking through other CUs. */
9703b513
TT
6531 }
6532
927aa2e7 6533 return stab_best;
9703b513
TT
6534}
6535
927aa2e7
JK
6536/* This dumps minimal information about .debug_names. It is called
6537 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6538 uses this to verify that .debug_names has been loaded. */
9291a0cd 6539
927aa2e7
JK
6540static void
6541dw2_debug_names_dump (struct objfile *objfile)
6542{
ed2dc618
SM
6543 struct dwarf2_per_objfile *dwarf2_per_objfile
6544 = get_dwarf2_per_objfile (objfile);
6545
927aa2e7
JK
6546 gdb_assert (dwarf2_per_objfile->using_index);
6547 printf_filtered (".debug_names:");
6548 if (dwarf2_per_objfile->debug_names_table)
6549 printf_filtered (" exists\n");
6550 else
6551 printf_filtered (" faked for \"readnow\"\n");
6552 printf_filtered ("\n");
9291a0cd
TT
6553}
6554
9291a0cd 6555static void
927aa2e7
JK
6556dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6557 const char *func_name)
9291a0cd 6558{
ed2dc618
SM
6559 struct dwarf2_per_objfile *dwarf2_per_objfile
6560 = get_dwarf2_per_objfile (objfile);
ae2de4f8 6561
927aa2e7
JK
6562 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6563 if (dwarf2_per_objfile->debug_names_table)
24c79950 6564 {
927aa2e7 6565 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6566
927aa2e7
JK
6567 /* Note: It doesn't matter what we pass for block_index here. */
6568 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6569 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
24c79950 6570
927aa2e7
JK
6571 struct dwarf2_per_cu_data *per_cu;
6572 while ((per_cu = iter.next ()) != NULL)
6573 dw2_instantiate_symtab (per_cu);
6574 }
6575}
24c79950 6576
927aa2e7
JK
6577static void
6578dw2_debug_names_expand_symtabs_matching
6579 (struct objfile *objfile,
6580 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6581 const lookup_name_info &lookup_name,
6582 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6583 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6584 enum search_domain kind)
6585{
ed2dc618
SM
6586 struct dwarf2_per_objfile *dwarf2_per_objfile
6587 = get_dwarf2_per_objfile (objfile);
9291a0cd 6588
927aa2e7
JK
6589 /* debug_names_table is NULL if OBJF_READNOW. */
6590 if (!dwarf2_per_objfile->debug_names_table)
6591 return;
9291a0cd 6592
ed2dc618 6593 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 6594
44ed8f3e 6595 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6596
44ed8f3e
PA
6597 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6598 symbol_matcher,
6599 kind, [&] (offset_type namei)
927aa2e7 6600 {
927aa2e7
JK
6601 /* The name was matched, now expand corresponding CUs that were
6602 marked. */
6603 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6604
927aa2e7
JK
6605 struct dwarf2_per_cu_data *per_cu;
6606 while ((per_cu = iter.next ()) != NULL)
6607 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6608 expansion_notify);
44ed8f3e 6609 });
9291a0cd
TT
6610}
6611
927aa2e7 6612const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6613{
6614 dw2_has_symbols,
6615 dw2_find_last_source_symtab,
6616 dw2_forget_cached_source_info,
f8eba3c6 6617 dw2_map_symtabs_matching_filename,
927aa2e7 6618 dw2_debug_names_lookup_symbol,
9291a0cd 6619 dw2_print_stats,
927aa2e7 6620 dw2_debug_names_dump,
9291a0cd 6621 dw2_relocate,
927aa2e7 6622 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6623 dw2_expand_all_symtabs,
652a8996 6624 dw2_expand_symtabs_with_fullname,
40658b94 6625 dw2_map_matching_symbols,
927aa2e7 6626 dw2_debug_names_expand_symtabs_matching,
43f3e411 6627 dw2_find_pc_sect_compunit_symtab,
71a3c369 6628 NULL,
9291a0cd
TT
6629 dw2_map_symbol_filenames
6630};
6631
3c0aa29a 6632/* See symfile.h. */
9291a0cd 6633
3c0aa29a
PA
6634bool
6635dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6636{
ed2dc618
SM
6637 struct dwarf2_per_objfile *dwarf2_per_objfile
6638 = get_dwarf2_per_objfile (objfile);
6639
9291a0cd
TT
6640 /* If we're about to read full symbols, don't bother with the
6641 indices. In this case we also don't care if some other debug
6642 format is making psymtabs, because they are all about to be
6643 expanded anyway. */
6644 if ((objfile->flags & OBJF_READNOW))
6645 {
6646 int i;
6647
6648 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
6649 create_all_comp_units (dwarf2_per_objfile);
6650 create_all_type_units (dwarf2_per_objfile);
7b9f3c50
DE
6651 dwarf2_per_objfile->quick_file_names_table =
6652 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 6653
1fd400ff 6654 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 6655 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 6656 {
ed2dc618 6657 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
9291a0cd 6658
e254ef6a
DE
6659 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6660 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6661 }
6662
6663 /* Return 1 so that gdb sees the "quick" functions. However,
6664 these functions will be no-ops because we will have expanded
6665 all symtabs. */
3c0aa29a
PA
6666 *index_kind = dw_index_kind::GDB_INDEX;
6667 return true;
9291a0cd
TT
6668 }
6669
ed2dc618 6670 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6671 {
6672 *index_kind = dw_index_kind::DEBUG_NAMES;
6673 return true;
6674 }
927aa2e7 6675
9291a0cd 6676 if (dwarf2_read_index (objfile))
3c0aa29a
PA
6677 {
6678 *index_kind = dw_index_kind::GDB_INDEX;
6679 return true;
6680 }
9291a0cd 6681
3c0aa29a 6682 return false;
9291a0cd
TT
6683}
6684
6685\f
6686
dce234bc
PP
6687/* Build a partial symbol table. */
6688
6689void
f29dff0a 6690dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6691{
ed2dc618
SM
6692 struct dwarf2_per_objfile *dwarf2_per_objfile
6693 = get_dwarf2_per_objfile (objfile);
c9bf0622 6694
af5bf4ad
SM
6695 if (objfile->global_psymbols.capacity () == 0
6696 && objfile->static_psymbols.capacity () == 0)
6697 init_psymbol_list (objfile, 1024);
c906108c 6698
492d29ea 6699 TRY
c9bf0622
TT
6700 {
6701 /* This isn't really ideal: all the data we allocate on the
6702 objfile's obstack is still uselessly kept around. However,
6703 freeing it seems unsafe. */
906768f9 6704 psymtab_discarder psymtabs (objfile);
ed2dc618 6705 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6706 psymtabs.keep ();
c9bf0622 6707 }
492d29ea
PA
6708 CATCH (except, RETURN_MASK_ERROR)
6709 {
6710 exception_print (gdb_stderr, except);
6711 }
6712 END_CATCH
c906108c 6713}
c906108c 6714
1ce1cefd
DE
6715/* Return the total length of the CU described by HEADER. */
6716
6717static unsigned int
6718get_cu_length (const struct comp_unit_head *header)
6719{
6720 return header->initial_length_size + header->length;
6721}
6722
9c541725 6723/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6724
9c541725
PA
6725static inline bool
6726offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6727{
9c541725
PA
6728 sect_offset bottom = cu_header->sect_off;
6729 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6730
9c541725 6731 return sect_off >= bottom && sect_off < top;
45452591
DE
6732}
6733
3b80fe9b
DE
6734/* Find the base address of the compilation unit for range lists and
6735 location lists. It will normally be specified by DW_AT_low_pc.
6736 In DWARF-3 draft 4, the base address could be overridden by
6737 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6738 compilation units with discontinuous ranges. */
6739
6740static void
6741dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6742{
6743 struct attribute *attr;
6744
6745 cu->base_known = 0;
6746 cu->base_address = 0;
6747
6748 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6749 if (attr)
6750 {
31aa7e4e 6751 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6752 cu->base_known = 1;
6753 }
6754 else
6755 {
6756 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6757 if (attr)
6758 {
31aa7e4e 6759 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6760 cu->base_known = 1;
6761 }
6762 }
6763}
6764
93311388 6765/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6766 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6767 NOTE: This leaves members offset, first_die_offset to be filled in
6768 by the caller. */
107d2387 6769
d521ce57 6770static const gdb_byte *
107d2387 6771read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6772 const gdb_byte *info_ptr,
6773 struct dwarf2_section_info *section,
6774 rcuh_kind section_kind)
107d2387
AC
6775{
6776 int signed_addr;
891d2f0b 6777 unsigned int bytes_read;
43988095
JK
6778 const char *filename = get_section_file_name (section);
6779 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6780
6781 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6782 cu_header->initial_length_size = bytes_read;
6783 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6784 info_ptr += bytes_read;
107d2387
AC
6785 cu_header->version = read_2_bytes (abfd, info_ptr);
6786 info_ptr += 2;
43988095
JK
6787 if (cu_header->version < 5)
6788 switch (section_kind)
6789 {
6790 case rcuh_kind::COMPILE:
6791 cu_header->unit_type = DW_UT_compile;
6792 break;
6793 case rcuh_kind::TYPE:
6794 cu_header->unit_type = DW_UT_type;
6795 break;
6796 default:
6797 internal_error (__FILE__, __LINE__,
6798 _("read_comp_unit_head: invalid section_kind"));
6799 }
6800 else
6801 {
6802 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6803 (read_1_byte (abfd, info_ptr));
6804 info_ptr += 1;
6805 switch (cu_header->unit_type)
6806 {
6807 case DW_UT_compile:
6808 if (section_kind != rcuh_kind::COMPILE)
6809 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6810 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6811 filename);
6812 break;
6813 case DW_UT_type:
6814 section_kind = rcuh_kind::TYPE;
6815 break;
6816 default:
6817 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6818 "(is %d, should be %d or %d) [in module %s]"),
6819 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6820 }
6821
6822 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6823 info_ptr += 1;
6824 }
9c541725
PA
6825 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6826 cu_header,
6827 &bytes_read);
613e1657 6828 info_ptr += bytes_read;
43988095
JK
6829 if (cu_header->version < 5)
6830 {
6831 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6832 info_ptr += 1;
6833 }
107d2387
AC
6834 signed_addr = bfd_get_sign_extend_vma (abfd);
6835 if (signed_addr < 0)
8e65ff28 6836 internal_error (__FILE__, __LINE__,
e2e0b3e5 6837 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6838 cu_header->signed_addr_p = signed_addr;
c764a876 6839
43988095
JK
6840 if (section_kind == rcuh_kind::TYPE)
6841 {
6842 LONGEST type_offset;
6843
6844 cu_header->signature = read_8_bytes (abfd, info_ptr);
6845 info_ptr += 8;
6846
6847 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6848 info_ptr += bytes_read;
9c541725
PA
6849 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6850 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6851 error (_("Dwarf Error: Too big type_offset in compilation unit "
6852 "header (is %s) [in module %s]"), plongest (type_offset),
6853 filename);
6854 }
6855
107d2387
AC
6856 return info_ptr;
6857}
6858
36586728
TT
6859/* Helper function that returns the proper abbrev section for
6860 THIS_CU. */
6861
6862static struct dwarf2_section_info *
6863get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6864{
6865 struct dwarf2_section_info *abbrev;
ed2dc618 6866 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6867
6868 if (this_cu->is_dwz)
ed2dc618 6869 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6870 else
6871 abbrev = &dwarf2_per_objfile->abbrev;
6872
6873 return abbrev;
6874}
6875
9ff913ba
DE
6876/* Subroutine of read_and_check_comp_unit_head and
6877 read_and_check_type_unit_head to simplify them.
6878 Perform various error checking on the header. */
6879
6880static void
ed2dc618
SM
6881error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6882 struct comp_unit_head *header,
4bdcc0c1
DE
6883 struct dwarf2_section_info *section,
6884 struct dwarf2_section_info *abbrev_section)
9ff913ba 6885{
a32a8923 6886 const char *filename = get_section_file_name (section);
9ff913ba 6887
43988095 6888 if (header->version < 2 || header->version > 5)
9ff913ba 6889 error (_("Dwarf Error: wrong version in compilation unit header "
43988095 6890 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
9ff913ba
DE
6891 filename);
6892
9c541725 6893 if (to_underlying (header->abbrev_sect_off)
36586728 6894 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9d8780f0
SM
6895 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6896 "(offset %s + 6) [in module %s]"),
6897 sect_offset_str (header->abbrev_sect_off),
6898 sect_offset_str (header->sect_off),
9ff913ba
DE
6899 filename);
6900
9c541725 6901 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6902 avoid potential 32-bit overflow. */
9c541725 6903 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6904 > section->size)
9c541725 6905 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
9d8780f0
SM
6906 "(offset %s + 0) [in module %s]"),
6907 header->length, sect_offset_str (header->sect_off),
9ff913ba
DE
6908 filename);
6909}
6910
6911/* Read in a CU/TU header and perform some basic error checking.
6912 The contents of the header are stored in HEADER.
6913 The result is a pointer to the start of the first DIE. */
adabb602 6914
d521ce57 6915static const gdb_byte *
ed2dc618
SM
6916read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6917 struct comp_unit_head *header,
9ff913ba 6918 struct dwarf2_section_info *section,
4bdcc0c1 6919 struct dwarf2_section_info *abbrev_section,
d521ce57 6920 const gdb_byte *info_ptr,
43988095 6921 rcuh_kind section_kind)
72bf9492 6922{
d521ce57 6923 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6924
9c541725 6925 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6926
43988095 6927 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6928
9c541725 6929 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6930
ed2dc618
SM
6931 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6932 abbrev_section);
9ff913ba
DE
6933
6934 return info_ptr;
348e048f
DE
6935}
6936
f4dc4d17
DE
6937/* Fetch the abbreviation table offset from a comp or type unit header. */
6938
6939static sect_offset
ed2dc618
SM
6940read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6941 struct dwarf2_section_info *section,
9c541725 6942 sect_offset sect_off)
f4dc4d17 6943{
a32a8923 6944 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6945 const gdb_byte *info_ptr;
ac298888 6946 unsigned int initial_length_size, offset_size;
43988095 6947 uint16_t version;
f4dc4d17
DE
6948
6949 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6950 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6951 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6952 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6953 info_ptr += initial_length_size;
6954
6955 version = read_2_bytes (abfd, info_ptr);
6956 info_ptr += 2;
6957 if (version >= 5)
6958 {
6959 /* Skip unit type and address size. */
6960 info_ptr += 2;
6961 }
6962
9c541725 6963 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6964}
6965
aaa75496
JB
6966/* Allocate a new partial symtab for file named NAME and mark this new
6967 partial symtab as being an include of PST. */
6968
6969static void
d521ce57 6970dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
6971 struct objfile *objfile)
6972{
6973 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6974
fbd9ab74
JK
6975 if (!IS_ABSOLUTE_PATH (subpst->filename))
6976 {
6977 /* It shares objfile->objfile_obstack. */
6978 subpst->dirname = pst->dirname;
6979 }
6980
aaa75496
JB
6981 subpst->textlow = 0;
6982 subpst->texthigh = 0;
6983
8d749320
SM
6984 subpst->dependencies
6985 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
6986 subpst->dependencies[0] = pst;
6987 subpst->number_of_dependencies = 1;
6988
6989 subpst->globals_offset = 0;
6990 subpst->n_global_syms = 0;
6991 subpst->statics_offset = 0;
6992 subpst->n_static_syms = 0;
43f3e411 6993 subpst->compunit_symtab = NULL;
aaa75496
JB
6994 subpst->read_symtab = pst->read_symtab;
6995 subpst->readin = 0;
6996
6997 /* No private part is necessary for include psymtabs. This property
6998 can be used to differentiate between such include psymtabs and
10b3939b 6999 the regular ones. */
58a9656e 7000 subpst->read_symtab_private = NULL;
aaa75496
JB
7001}
7002
7003/* Read the Line Number Program data and extract the list of files
7004 included by the source file represented by PST. Build an include
d85a05f0 7005 partial symtab for each of these included files. */
aaa75496
JB
7006
7007static void
7008dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
7009 struct die_info *die,
7010 struct partial_symtab *pst)
aaa75496 7011{
fff8551c 7012 line_header_up lh;
d85a05f0 7013 struct attribute *attr;
aaa75496 7014
d85a05f0
DJ
7015 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7016 if (attr)
9c541725 7017 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
7018 if (lh == NULL)
7019 return; /* No linetable, so no includes. */
7020
c6da4cef 7021 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
fff8551c 7022 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
7023}
7024
348e048f 7025static hashval_t
52dc124a 7026hash_signatured_type (const void *item)
348e048f 7027{
9a3c8263
SM
7028 const struct signatured_type *sig_type
7029 = (const struct signatured_type *) item;
9a619af0 7030
348e048f 7031 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 7032 return sig_type->signature;
348e048f
DE
7033}
7034
7035static int
52dc124a 7036eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 7037{
9a3c8263
SM
7038 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
7039 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 7040
348e048f
DE
7041 return lhs->signature == rhs->signature;
7042}
7043
1fd400ff
TT
7044/* Allocate a hash table for signatured types. */
7045
7046static htab_t
673bfd45 7047allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
7048{
7049 return htab_create_alloc_ex (41,
52dc124a
DE
7050 hash_signatured_type,
7051 eq_signatured_type,
1fd400ff
TT
7052 NULL,
7053 &objfile->objfile_obstack,
7054 hashtab_obstack_allocate,
7055 dummy_obstack_deallocate);
7056}
7057
d467dd73 7058/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
7059
7060static int
d467dd73 7061add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 7062{
9a3c8263
SM
7063 struct signatured_type *sigt = (struct signatured_type *) *slot;
7064 struct signatured_type ***datap = (struct signatured_type ***) datum;
1fd400ff 7065
b4dd5633 7066 **datap = sigt;
1fd400ff
TT
7067 ++*datap;
7068
7069 return 1;
7070}
7071
78d4d2c5 7072/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
7073 and fill them into TYPES_HTAB. It will process only type units,
7074 therefore DW_UT_type. */
c88ee1f0 7075
78d4d2c5 7076static void
ed2dc618
SM
7077create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
7078 struct dwo_file *dwo_file,
43988095
JK
7079 dwarf2_section_info *section, htab_t &types_htab,
7080 rcuh_kind section_kind)
348e048f 7081{
3019eac3 7082 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 7083 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
7084 bfd *abfd;
7085 const gdb_byte *info_ptr, *end_ptr;
348e048f 7086
4bdcc0c1
DE
7087 abbrev_section = (dwo_file != NULL
7088 ? &dwo_file->sections.abbrev
7089 : &dwarf2_per_objfile->abbrev);
7090
b4f54984 7091 if (dwarf_read_debug)
43988095
JK
7092 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
7093 get_section_name (section),
a32a8923 7094 get_section_file_name (abbrev_section));
09406207 7095
78d4d2c5
JK
7096 dwarf2_read_section (objfile, section);
7097 info_ptr = section->buffer;
348e048f 7098
78d4d2c5
JK
7099 if (info_ptr == NULL)
7100 return;
348e048f 7101
78d4d2c5
JK
7102 /* We can't set abfd until now because the section may be empty or
7103 not present, in which case the bfd is unknown. */
7104 abfd = get_section_bfd_owner (section);
348e048f 7105
78d4d2c5
JK
7106 /* We don't use init_cutu_and_read_dies_simple, or some such, here
7107 because we don't need to read any dies: the signature is in the
7108 header. */
3019eac3 7109
78d4d2c5
JK
7110 end_ptr = info_ptr + section->size;
7111 while (info_ptr < end_ptr)
7112 {
78d4d2c5
JK
7113 struct signatured_type *sig_type;
7114 struct dwo_unit *dwo_tu;
7115 void **slot;
7116 const gdb_byte *ptr = info_ptr;
7117 struct comp_unit_head header;
7118 unsigned int length;
8b70b953 7119
9c541725 7120 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 7121
a49dd8dd
JK
7122 /* Initialize it due to a false compiler warning. */
7123 header.signature = -1;
9c541725 7124 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 7125
78d4d2c5
JK
7126 /* We need to read the type's signature in order to build the hash
7127 table, but we don't need anything else just yet. */
348e048f 7128
ed2dc618 7129 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 7130 abbrev_section, ptr, section_kind);
348e048f 7131
78d4d2c5 7132 length = get_cu_length (&header);
6caca83c 7133
78d4d2c5
JK
7134 /* Skip dummy type units. */
7135 if (ptr >= info_ptr + length
43988095
JK
7136 || peek_abbrev_code (abfd, ptr) == 0
7137 || header.unit_type != DW_UT_type)
78d4d2c5
JK
7138 {
7139 info_ptr += length;
7140 continue;
7141 }
dee91e82 7142
78d4d2c5
JK
7143 if (types_htab == NULL)
7144 {
7145 if (dwo_file)
7146 types_htab = allocate_dwo_unit_table (objfile);
7147 else
7148 types_htab = allocate_signatured_type_table (objfile);
7149 }
8b70b953 7150
78d4d2c5
JK
7151 if (dwo_file)
7152 {
7153 sig_type = NULL;
7154 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7155 struct dwo_unit);
7156 dwo_tu->dwo_file = dwo_file;
43988095 7157 dwo_tu->signature = header.signature;
9c541725 7158 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 7159 dwo_tu->section = section;
9c541725 7160 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
7161 dwo_tu->length = length;
7162 }
7163 else
7164 {
7165 /* N.B.: type_offset is not usable if this type uses a DWO file.
7166 The real type_offset is in the DWO file. */
7167 dwo_tu = NULL;
7168 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7169 struct signatured_type);
43988095 7170 sig_type->signature = header.signature;
9c541725 7171 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 7172 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
7173 sig_type->per_cu.is_debug_types = 1;
7174 sig_type->per_cu.section = section;
9c541725 7175 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
7176 sig_type->per_cu.length = length;
7177 }
7178
7179 slot = htab_find_slot (types_htab,
7180 dwo_file ? (void*) dwo_tu : (void *) sig_type,
7181 INSERT);
7182 gdb_assert (slot != NULL);
7183 if (*slot != NULL)
7184 {
9c541725 7185 sect_offset dup_sect_off;
0349ea22 7186
3019eac3
DE
7187 if (dwo_file)
7188 {
78d4d2c5
JK
7189 const struct dwo_unit *dup_tu
7190 = (const struct dwo_unit *) *slot;
7191
9c541725 7192 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
7193 }
7194 else
7195 {
78d4d2c5
JK
7196 const struct signatured_type *dup_tu
7197 = (const struct signatured_type *) *slot;
7198
9c541725 7199 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 7200 }
8b70b953 7201
78d4d2c5 7202 complaint (&symfile_complaints,
9d8780f0
SM
7203 _("debug type entry at offset %s is duplicate to"
7204 " the entry at offset %s, signature %s"),
7205 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 7206 hex_string (header.signature));
78d4d2c5
JK
7207 }
7208 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 7209
78d4d2c5 7210 if (dwarf_read_debug > 1)
9d8780f0
SM
7211 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
7212 sect_offset_str (sect_off),
43988095 7213 hex_string (header.signature));
3019eac3 7214
78d4d2c5
JK
7215 info_ptr += length;
7216 }
7217}
3019eac3 7218
78d4d2c5
JK
7219/* Create the hash table of all entries in the .debug_types
7220 (or .debug_types.dwo) section(s).
7221 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
7222 otherwise it is NULL.
b3c8eb43 7223
78d4d2c5 7224 The result is a pointer to the hash table or NULL if there are no types.
348e048f 7225
78d4d2c5 7226 Note: This function processes DWO files only, not DWP files. */
348e048f 7227
78d4d2c5 7228static void
ed2dc618
SM
7229create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
7230 struct dwo_file *dwo_file,
78d4d2c5
JK
7231 VEC (dwarf2_section_info_def) *types,
7232 htab_t &types_htab)
7233{
7234 int ix;
7235 struct dwarf2_section_info *section;
7236
7237 if (VEC_empty (dwarf2_section_info_def, types))
7238 return;
348e048f 7239
78d4d2c5
JK
7240 for (ix = 0;
7241 VEC_iterate (dwarf2_section_info_def, types, ix, section);
7242 ++ix)
ed2dc618
SM
7243 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
7244 types_htab, rcuh_kind::TYPE);
3019eac3
DE
7245}
7246
7247/* Create the hash table of all entries in the .debug_types section,
7248 and initialize all_type_units.
7249 The result is zero if there is an error (e.g. missing .debug_types section),
7250 otherwise non-zero. */
7251
7252static int
ed2dc618 7253create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 7254{
78d4d2c5 7255 htab_t types_htab = NULL;
b4dd5633 7256 struct signatured_type **iter;
3019eac3 7257
ed2dc618
SM
7258 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
7259 &dwarf2_per_objfile->info, types_htab,
43988095 7260 rcuh_kind::COMPILE);
ed2dc618
SM
7261 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
7262 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
7263 if (types_htab == NULL)
7264 {
7265 dwarf2_per_objfile->signatured_types = NULL;
7266 return 0;
7267 }
7268
348e048f
DE
7269 dwarf2_per_objfile->signatured_types = types_htab;
7270
6aa5f3a6
DE
7271 dwarf2_per_objfile->n_type_units
7272 = dwarf2_per_objfile->n_allocated_type_units
7273 = htab_elements (types_htab);
8d749320
SM
7274 dwarf2_per_objfile->all_type_units =
7275 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
d467dd73
DE
7276 iter = &dwarf2_per_objfile->all_type_units[0];
7277 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
7278 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
7279 == dwarf2_per_objfile->n_type_units);
1fd400ff 7280
348e048f
DE
7281 return 1;
7282}
7283
6aa5f3a6
DE
7284/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
7285 If SLOT is non-NULL, it is the entry to use in the hash table.
7286 Otherwise we find one. */
7287
7288static struct signatured_type *
ed2dc618
SM
7289add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
7290 void **slot)
6aa5f3a6
DE
7291{
7292 struct objfile *objfile = dwarf2_per_objfile->objfile;
7293 int n_type_units = dwarf2_per_objfile->n_type_units;
7294 struct signatured_type *sig_type;
7295
7296 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
7297 ++n_type_units;
7298 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
7299 {
7300 if (dwarf2_per_objfile->n_allocated_type_units == 0)
7301 dwarf2_per_objfile->n_allocated_type_units = 1;
7302 dwarf2_per_objfile->n_allocated_type_units *= 2;
7303 dwarf2_per_objfile->all_type_units
224c3ddb
SM
7304 = XRESIZEVEC (struct signatured_type *,
7305 dwarf2_per_objfile->all_type_units,
7306 dwarf2_per_objfile->n_allocated_type_units);
6aa5f3a6
DE
7307 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
7308 }
7309 dwarf2_per_objfile->n_type_units = n_type_units;
7310
7311 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7312 struct signatured_type);
7313 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
7314 sig_type->signature = sig;
7315 sig_type->per_cu.is_debug_types = 1;
7316 if (dwarf2_per_objfile->using_index)
7317 {
7318 sig_type->per_cu.v.quick =
7319 OBSTACK_ZALLOC (&objfile->objfile_obstack,
7320 struct dwarf2_per_cu_quick_data);
7321 }
7322
7323 if (slot == NULL)
7324 {
7325 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7326 sig_type, INSERT);
7327 }
7328 gdb_assert (*slot == NULL);
7329 *slot = sig_type;
7330 /* The rest of sig_type must be filled in by the caller. */
7331 return sig_type;
7332}
7333
a2ce51a0
DE
7334/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7335 Fill in SIG_ENTRY with DWO_ENTRY. */
7336
7337static void
ed2dc618 7338fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
7339 struct signatured_type *sig_entry,
7340 struct dwo_unit *dwo_entry)
7341{
7ee85ab1 7342 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
7343 gdb_assert (! sig_entry->per_cu.queued);
7344 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
7345 if (dwarf2_per_objfile->using_index)
7346 {
7347 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 7348 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
7349 }
7350 else
7351 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 7352 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 7353 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 7354 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
7355 gdb_assert (sig_entry->dwo_unit == NULL);
7356
7357 sig_entry->per_cu.section = dwo_entry->section;
9c541725 7358 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
7359 sig_entry->per_cu.length = dwo_entry->length;
7360 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 7361 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
7362 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7363 sig_entry->dwo_unit = dwo_entry;
7364}
7365
7366/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
7367 If we haven't read the TU yet, create the signatured_type data structure
7368 for a TU to be read in directly from a DWO file, bypassing the stub.
7369 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7370 using .gdb_index, then when reading a CU we want to stay in the DWO file
7371 containing that CU. Otherwise we could end up reading several other DWO
7372 files (due to comdat folding) to process the transitive closure of all the
7373 mentioned TUs, and that can be slow. The current DWO file will have every
7374 type signature that it needs.
a2ce51a0
DE
7375 We only do this for .gdb_index because in the psymtab case we already have
7376 to read all the DWOs to build the type unit groups. */
7377
7378static struct signatured_type *
7379lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7380{
518817b3
SM
7381 struct dwarf2_per_objfile *dwarf2_per_objfile
7382 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
7383 struct objfile *objfile = dwarf2_per_objfile->objfile;
7384 struct dwo_file *dwo_file;
7385 struct dwo_unit find_dwo_entry, *dwo_entry;
7386 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7387 void **slot;
a2ce51a0
DE
7388
7389 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7390
6aa5f3a6
DE
7391 /* If TU skeletons have been removed then we may not have read in any
7392 TUs yet. */
7393 if (dwarf2_per_objfile->signatured_types == NULL)
7394 {
7395 dwarf2_per_objfile->signatured_types
7396 = allocate_signatured_type_table (objfile);
7397 }
a2ce51a0
DE
7398
7399 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
7400 Use the global signatured_types array to do our own comdat-folding
7401 of types. If this is the first time we're reading this TU, and
7402 the TU has an entry in .gdb_index, replace the recorded data from
7403 .gdb_index with this TU. */
a2ce51a0 7404
a2ce51a0 7405 find_sig_entry.signature = sig;
6aa5f3a6
DE
7406 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7407 &find_sig_entry, INSERT);
9a3c8263 7408 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
7409
7410 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
7411 read. Don't reassign the global entry to point to this DWO if that's
7412 the case. Also note that if the TU is already being read, it may not
7413 have come from a DWO, the program may be a mix of Fission-compiled
7414 code and non-Fission-compiled code. */
7415
7416 /* Have we already tried to read this TU?
7417 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7418 needn't exist in the global table yet). */
7419 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
7420 return sig_entry;
7421
6aa5f3a6
DE
7422 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7423 dwo_unit of the TU itself. */
7424 dwo_file = cu->dwo_unit->dwo_file;
7425
a2ce51a0
DE
7426 /* Ok, this is the first time we're reading this TU. */
7427 if (dwo_file->tus == NULL)
7428 return NULL;
7429 find_dwo_entry.signature = sig;
9a3c8263 7430 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
7431 if (dwo_entry == NULL)
7432 return NULL;
7433
6aa5f3a6
DE
7434 /* If the global table doesn't have an entry for this TU, add one. */
7435 if (sig_entry == NULL)
ed2dc618 7436 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 7437
ed2dc618 7438 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 7439 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
7440 return sig_entry;
7441}
7442
a2ce51a0
DE
7443/* Subroutine of lookup_signatured_type.
7444 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
7445 then try the DWP file. If the TU stub (skeleton) has been removed then
7446 it won't be in .gdb_index. */
a2ce51a0
DE
7447
7448static struct signatured_type *
7449lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7450{
518817b3
SM
7451 struct dwarf2_per_objfile *dwarf2_per_objfile
7452 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 7453 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 7454 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
7455 struct dwo_unit *dwo_entry;
7456 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7457 void **slot;
a2ce51a0
DE
7458
7459 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7460 gdb_assert (dwp_file != NULL);
7461
6aa5f3a6
DE
7462 /* If TU skeletons have been removed then we may not have read in any
7463 TUs yet. */
7464 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 7465 {
6aa5f3a6
DE
7466 dwarf2_per_objfile->signatured_types
7467 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
7468 }
7469
6aa5f3a6
DE
7470 find_sig_entry.signature = sig;
7471 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7472 &find_sig_entry, INSERT);
9a3c8263 7473 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
7474
7475 /* Have we already tried to read this TU?
7476 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7477 needn't exist in the global table yet). */
7478 if (sig_entry != NULL)
7479 return sig_entry;
7480
a2ce51a0
DE
7481 if (dwp_file->tus == NULL)
7482 return NULL;
ed2dc618 7483 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 7484 sig, 1 /* is_debug_types */);
a2ce51a0
DE
7485 if (dwo_entry == NULL)
7486 return NULL;
7487
ed2dc618
SM
7488 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7489 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 7490
a2ce51a0
DE
7491 return sig_entry;
7492}
7493
380bca97 7494/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7495 Returns NULL if signature SIG is not present in the table.
7496 It is up to the caller to complain about this. */
348e048f
DE
7497
7498static struct signatured_type *
a2ce51a0 7499lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7500{
518817b3
SM
7501 struct dwarf2_per_objfile *dwarf2_per_objfile
7502 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 7503
a2ce51a0
DE
7504 if (cu->dwo_unit
7505 && dwarf2_per_objfile->using_index)
7506 {
7507 /* We're in a DWO/DWP file, and we're using .gdb_index.
7508 These cases require special processing. */
ed2dc618 7509 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
7510 return lookup_dwo_signatured_type (cu, sig);
7511 else
7512 return lookup_dwp_signatured_type (cu, sig);
7513 }
7514 else
7515 {
7516 struct signatured_type find_entry, *entry;
348e048f 7517
a2ce51a0
DE
7518 if (dwarf2_per_objfile->signatured_types == NULL)
7519 return NULL;
7520 find_entry.signature = sig;
9a3c8263
SM
7521 entry = ((struct signatured_type *)
7522 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7523 return entry;
7524 }
348e048f 7525}
42e7ad6c
DE
7526\f
7527/* Low level DIE reading support. */
348e048f 7528
d85a05f0
DJ
7529/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7530
7531static void
7532init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7533 struct dwarf2_cu *cu,
3019eac3 7534 struct dwarf2_section_info *section,
685af9cd
TT
7535 struct dwo_file *dwo_file,
7536 struct abbrev_table *abbrev_table)
d85a05f0 7537{
fceca515 7538 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7539 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7540 reader->cu = cu;
3019eac3 7541 reader->dwo_file = dwo_file;
dee91e82
DE
7542 reader->die_section = section;
7543 reader->buffer = section->buffer;
f664829e 7544 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7545 reader->comp_dir = NULL;
685af9cd 7546 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
7547}
7548
b0c7bfa9
DE
7549/* Subroutine of init_cutu_and_read_dies to simplify it.
7550 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7551 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7552 already.
7553
7554 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7555 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7556 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7557 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7558 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7559 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7560 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7561 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
7562 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7563 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7564 kept around for at least as long as *RESULT_READER.
7565
b0c7bfa9
DE
7566 The result is non-zero if a valid (non-dummy) DIE was found. */
7567
7568static int
7569read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7570 struct dwo_unit *dwo_unit,
b0c7bfa9 7571 struct die_info *stub_comp_unit_die,
a2ce51a0 7572 const char *stub_comp_dir,
b0c7bfa9 7573 struct die_reader_specs *result_reader,
d521ce57 7574 const gdb_byte **result_info_ptr,
b0c7bfa9 7575 struct die_info **result_comp_unit_die,
685af9cd
TT
7576 int *result_has_children,
7577 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 7578{
ed2dc618 7579 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
7580 struct objfile *objfile = dwarf2_per_objfile->objfile;
7581 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7582 bfd *abfd;
d521ce57 7583 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7584 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7585 int i,num_extra_attrs;
7586 struct dwarf2_section_info *dwo_abbrev_section;
7587 struct attribute *attr;
7588 struct die_info *comp_unit_die;
7589
b0aeadb3
DE
7590 /* At most one of these may be provided. */
7591 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7592
b0c7bfa9
DE
7593 /* These attributes aren't processed until later:
7594 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7595 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7596 referenced later. However, these attributes are found in the stub
7597 which we won't have later. In order to not impose this complication
7598 on the rest of the code, we read them here and copy them to the
7599 DWO CU/TU die. */
b0c7bfa9
DE
7600
7601 stmt_list = NULL;
7602 low_pc = NULL;
7603 high_pc = NULL;
7604 ranges = NULL;
7605 comp_dir = NULL;
7606
7607 if (stub_comp_unit_die != NULL)
7608 {
7609 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7610 DWO file. */
7611 if (! this_cu->is_debug_types)
7612 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7613 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7614 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7615 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7616 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7617
7618 /* There should be a DW_AT_addr_base attribute here (if needed).
7619 We need the value before we can process DW_FORM_GNU_addr_index. */
7620 cu->addr_base = 0;
7621 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7622 if (attr)
7623 cu->addr_base = DW_UNSND (attr);
7624
7625 /* There should be a DW_AT_ranges_base attribute here (if needed).
7626 We need the value before we can process DW_AT_ranges. */
7627 cu->ranges_base = 0;
7628 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7629 if (attr)
7630 cu->ranges_base = DW_UNSND (attr);
7631 }
a2ce51a0
DE
7632 else if (stub_comp_dir != NULL)
7633 {
7634 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7635 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7636 comp_dir->name = DW_AT_comp_dir;
7637 comp_dir->form = DW_FORM_string;
7638 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7639 DW_STRING (comp_dir) = stub_comp_dir;
7640 }
b0c7bfa9
DE
7641
7642 /* Set up for reading the DWO CU/TU. */
7643 cu->dwo_unit = dwo_unit;
685af9cd 7644 dwarf2_section_info *section = dwo_unit->section;
b0c7bfa9 7645 dwarf2_read_section (objfile, section);
a32a8923 7646 abfd = get_section_bfd_owner (section);
9c541725
PA
7647 begin_info_ptr = info_ptr = (section->buffer
7648 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7649 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
7650
7651 if (this_cu->is_debug_types)
7652 {
b0c7bfa9
DE
7653 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7654
ed2dc618
SM
7655 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7656 &cu->header, section,
b0c7bfa9 7657 dwo_abbrev_section,
43988095 7658 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7659 /* This is not an assert because it can be caused by bad debug info. */
43988095 7660 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7661 {
7662 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7663 " TU at offset %s [in module %s]"),
a2ce51a0 7664 hex_string (sig_type->signature),
43988095 7665 hex_string (cu->header.signature),
9d8780f0 7666 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7667 bfd_get_filename (abfd));
7668 }
9c541725 7669 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7670 /* For DWOs coming from DWP files, we don't know the CU length
7671 nor the type's offset in the TU until now. */
7672 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7673 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7674
7675 /* Establish the type offset that can be used to lookup the type.
7676 For DWO files, we don't know it until now. */
9c541725
PA
7677 sig_type->type_offset_in_section
7678 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7679 }
7680 else
7681 {
ed2dc618
SM
7682 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7683 &cu->header, section,
b0c7bfa9 7684 dwo_abbrev_section,
43988095 7685 info_ptr, rcuh_kind::COMPILE);
9c541725 7686 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7687 /* For DWOs coming from DWP files, we don't know the CU length
7688 until now. */
7689 dwo_unit->length = get_cu_length (&cu->header);
7690 }
7691
685af9cd
TT
7692 *result_dwo_abbrev_table
7693 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7694 cu->header.abbrev_sect_off);
7695 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7696 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7697
7698 /* Read in the die, but leave space to copy over the attributes
7699 from the stub. This has the benefit of simplifying the rest of
7700 the code - all the work to maintain the illusion of a single
7701 DW_TAG_{compile,type}_unit DIE is done here. */
7702 num_extra_attrs = ((stmt_list != NULL)
7703 + (low_pc != NULL)
7704 + (high_pc != NULL)
7705 + (ranges != NULL)
7706 + (comp_dir != NULL));
7707 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7708 result_has_children, num_extra_attrs);
7709
7710 /* Copy over the attributes from the stub to the DIE we just read in. */
7711 comp_unit_die = *result_comp_unit_die;
7712 i = comp_unit_die->num_attrs;
7713 if (stmt_list != NULL)
7714 comp_unit_die->attrs[i++] = *stmt_list;
7715 if (low_pc != NULL)
7716 comp_unit_die->attrs[i++] = *low_pc;
7717 if (high_pc != NULL)
7718 comp_unit_die->attrs[i++] = *high_pc;
7719 if (ranges != NULL)
7720 comp_unit_die->attrs[i++] = *ranges;
7721 if (comp_dir != NULL)
7722 comp_unit_die->attrs[i++] = *comp_dir;
7723 comp_unit_die->num_attrs += num_extra_attrs;
7724
b4f54984 7725 if (dwarf_die_debug)
bf6af496
DE
7726 {
7727 fprintf_unfiltered (gdb_stdlog,
7728 "Read die from %s@0x%x of %s:\n",
a32a8923 7729 get_section_name (section),
bf6af496
DE
7730 (unsigned) (begin_info_ptr - section->buffer),
7731 bfd_get_filename (abfd));
b4f54984 7732 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7733 }
7734
a2ce51a0
DE
7735 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7736 TUs by skipping the stub and going directly to the entry in the DWO file.
7737 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7738 to get it via circuitous means. Blech. */
7739 if (comp_dir != NULL)
7740 result_reader->comp_dir = DW_STRING (comp_dir);
7741
b0c7bfa9
DE
7742 /* Skip dummy compilation units. */
7743 if (info_ptr >= begin_info_ptr + dwo_unit->length
7744 || peek_abbrev_code (abfd, info_ptr) == 0)
7745 return 0;
7746
7747 *result_info_ptr = info_ptr;
7748 return 1;
7749}
7750
7751/* Subroutine of init_cutu_and_read_dies to simplify it.
7752 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7753 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7754
7755static struct dwo_unit *
7756lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7757 struct die_info *comp_unit_die)
7758{
7759 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9
DE
7760 ULONGEST signature;
7761 struct dwo_unit *dwo_unit;
7762 const char *comp_dir, *dwo_name;
7763
a2ce51a0
DE
7764 gdb_assert (cu != NULL);
7765
b0c7bfa9 7766 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
7767 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7768 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7769
7770 if (this_cu->is_debug_types)
7771 {
7772 struct signatured_type *sig_type;
7773
7774 /* Since this_cu is the first member of struct signatured_type,
7775 we can go from a pointer to one to a pointer to the other. */
7776 sig_type = (struct signatured_type *) this_cu;
7777 signature = sig_type->signature;
7778 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7779 }
7780 else
7781 {
7782 struct attribute *attr;
7783
7784 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7785 if (! attr)
7786 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7787 " [in module %s]"),
e3b94546 7788 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9
DE
7789 signature = DW_UNSND (attr);
7790 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7791 signature);
7792 }
7793
b0c7bfa9
DE
7794 return dwo_unit;
7795}
7796
a2ce51a0 7797/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6 7798 See it for a description of the parameters.
fcd3b13d 7799 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0
DE
7800
7801static void
6aa5f3a6
DE
7802init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7803 int use_existing_cu, int keep,
a2ce51a0
DE
7804 die_reader_func_ftype *die_reader_func,
7805 void *data)
7806{
fcd3b13d 7807 std::unique_ptr<dwarf2_cu> new_cu;
a2ce51a0 7808 struct signatured_type *sig_type;
a2ce51a0
DE
7809 struct die_reader_specs reader;
7810 const gdb_byte *info_ptr;
7811 struct die_info *comp_unit_die;
7812 int has_children;
ed2dc618 7813 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
a2ce51a0
DE
7814
7815 /* Verify we can do the following downcast, and that we have the
7816 data we need. */
7817 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7818 sig_type = (struct signatured_type *) this_cu;
7819 gdb_assert (sig_type->dwo_unit != NULL);
7820
6aa5f3a6
DE
7821 if (use_existing_cu && this_cu->cu != NULL)
7822 {
7823 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6
DE
7824 /* There's no need to do the rereading_dwo_cu handling that
7825 init_cutu_and_read_dies does since we don't read the stub. */
7826 }
7827 else
7828 {
7829 /* If !use_existing_cu, this_cu->cu must be NULL. */
7830 gdb_assert (this_cu->cu == NULL);
fcd3b13d 7831 new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7832 }
7833
7834 /* A future optimization, if needed, would be to use an existing
7835 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7836 could share abbrev tables. */
a2ce51a0 7837
685af9cd
TT
7838 /* The abbreviation table used by READER, this must live at least as long as
7839 READER. */
7840 abbrev_table_up dwo_abbrev_table;
7841
a2ce51a0 7842 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7843 NULL /* stub_comp_unit_die */,
7844 sig_type->dwo_unit->dwo_file->comp_dir,
7845 &reader, &info_ptr,
685af9cd
TT
7846 &comp_unit_die, &has_children,
7847 &dwo_abbrev_table) == 0)
a2ce51a0
DE
7848 {
7849 /* Dummy die. */
a2ce51a0
DE
7850 return;
7851 }
7852
7853 /* All the "real" work is done here. */
7854 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7855
6aa5f3a6 7856 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
7857 but the alternative is making the latter more complex.
7858 This function is only for the special case of using DWO files directly:
7859 no point in overly complicating the general case just to handle this. */
fcd3b13d 7860 if (new_cu != NULL && keep)
a2ce51a0 7861 {
fcd3b13d
SM
7862 /* Link this CU into read_in_chain. */
7863 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7864 dwarf2_per_objfile->read_in_chain = this_cu;
7865 /* The chain owns it now. */
7866 new_cu.release ();
a2ce51a0 7867 }
a2ce51a0
DE
7868}
7869
fd820528 7870/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7871 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7872
f4dc4d17
DE
7873 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7874 Otherwise the table specified in the comp unit header is read in and used.
7875 This is an optimization for when we already have the abbrev table.
7876
dee91e82
DE
7877 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7878 Otherwise, a new CU is allocated with xmalloc.
7879
7880 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7881 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7882
7883 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 7884 linker) then DIE_READER_FUNC will not get called. */
aaa75496 7885
70221824 7886static void
fd820528 7887init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 7888 struct abbrev_table *abbrev_table,
fd820528
DE
7889 int use_existing_cu, int keep,
7890 die_reader_func_ftype *die_reader_func,
7891 void *data)
c906108c 7892{
ed2dc618 7893 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7894 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7895 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7896 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7897 struct dwarf2_cu *cu;
d521ce57 7898 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7899 struct die_reader_specs reader;
d85a05f0 7900 struct die_info *comp_unit_die;
dee91e82 7901 int has_children;
d85a05f0 7902 struct attribute *attr;
dee91e82 7903 struct signatured_type *sig_type = NULL;
4bdcc0c1 7904 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7905 /* Non-zero if CU currently points to a DWO file and we need to
7906 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7907 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7908 int rereading_dwo_cu = 0;
c906108c 7909
b4f54984 7910 if (dwarf_die_debug)
9d8780f0 7911 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7912 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7913 sect_offset_str (this_cu->sect_off));
09406207 7914
dee91e82
DE
7915 if (use_existing_cu)
7916 gdb_assert (keep);
23745b47 7917
a2ce51a0
DE
7918 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7919 file (instead of going through the stub), short-circuit all of this. */
7920 if (this_cu->reading_dwo_directly)
7921 {
7922 /* Narrow down the scope of possibilities to have to understand. */
7923 gdb_assert (this_cu->is_debug_types);
7924 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
7925 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7926 die_reader_func, data);
a2ce51a0
DE
7927 return;
7928 }
7929
dee91e82
DE
7930 /* This is cheap if the section is already read in. */
7931 dwarf2_read_section (objfile, section);
7932
9c541725 7933 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7934
7935 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7936
fcd3b13d 7937 std::unique_ptr<dwarf2_cu> new_cu;
dee91e82
DE
7938 if (use_existing_cu && this_cu->cu != NULL)
7939 {
7940 cu = this_cu->cu;
42e7ad6c
DE
7941 /* If this CU is from a DWO file we need to start over, we need to
7942 refetch the attributes from the skeleton CU.
7943 This could be optimized by retrieving those attributes from when we
7944 were here the first time: the previous comp_unit_die was stored in
7945 comp_unit_obstack. But there's no data yet that we need this
7946 optimization. */
7947 if (cu->dwo_unit != NULL)
7948 rereading_dwo_cu = 1;
dee91e82
DE
7949 }
7950 else
7951 {
7952 /* If !use_existing_cu, this_cu->cu must be NULL. */
7953 gdb_assert (this_cu->cu == NULL);
fcd3b13d
SM
7954 new_cu.reset (new dwarf2_cu (this_cu));
7955 cu = new_cu.get ();
42e7ad6c 7956 }
dee91e82 7957
b0c7bfa9 7958 /* Get the header. */
9c541725 7959 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7960 {
7961 /* We already have the header, there's no need to read it in again. */
9c541725 7962 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7963 }
7964 else
7965 {
3019eac3 7966 if (this_cu->is_debug_types)
dee91e82 7967 {
ed2dc618
SM
7968 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7969 &cu->header, section,
4bdcc0c1 7970 abbrev_section, info_ptr,
43988095 7971 rcuh_kind::TYPE);
dee91e82 7972
42e7ad6c
DE
7973 /* Since per_cu is the first member of struct signatured_type,
7974 we can go from a pointer to one to a pointer to the other. */
7975 sig_type = (struct signatured_type *) this_cu;
43988095 7976 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7977 gdb_assert (sig_type->type_offset_in_tu
7978 == cu->header.type_cu_offset_in_tu);
7979 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7980
42e7ad6c
DE
7981 /* LENGTH has not been set yet for type units if we're
7982 using .gdb_index. */
1ce1cefd 7983 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7984
7985 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7986 sig_type->type_offset_in_section =
7987 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7988
7989 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7990 }
7991 else
7992 {
ed2dc618
SM
7993 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7994 &cu->header, section,
4bdcc0c1 7995 abbrev_section,
43988095
JK
7996 info_ptr,
7997 rcuh_kind::COMPILE);
dee91e82 7998
9c541725 7999 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 8000 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 8001 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
8002 }
8003 }
10b3939b 8004
6caca83c 8005 /* Skip dummy compilation units. */
dee91e82 8006 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 8007 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 8008 return;
6caca83c 8009
433df2d4
DE
8010 /* If we don't have them yet, read the abbrevs for this compilation unit.
8011 And if we need to read them now, make sure they're freed when we're
685af9cd
TT
8012 done (own the table through ABBREV_TABLE_HOLDER). */
8013 abbrev_table_up abbrev_table_holder;
f4dc4d17 8014 if (abbrev_table != NULL)
685af9cd
TT
8015 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
8016 else
f4dc4d17 8017 {
685af9cd
TT
8018 abbrev_table_holder
8019 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
8020 cu->header.abbrev_sect_off);
8021 abbrev_table = abbrev_table_holder.get ();
42e7ad6c 8022 }
af703f96 8023
dee91e82 8024 /* Read the top level CU/TU die. */
685af9cd 8025 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
dee91e82 8026 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 8027
b0c7bfa9 8028 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
8029 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
8030 table from the DWO file and pass the ownership over to us. It will be
8031 referenced from READER, so we must make sure to free it after we're done
8032 with READER.
8033
b0c7bfa9
DE
8034 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
8035 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3 8036 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
685af9cd 8037 abbrev_table_up dwo_abbrev_table;
3019eac3
DE
8038 if (attr)
8039 {
3019eac3 8040 struct dwo_unit *dwo_unit;
b0c7bfa9 8041 struct die_info *dwo_comp_unit_die;
3019eac3
DE
8042
8043 if (has_children)
6a506a2d
DE
8044 {
8045 complaint (&symfile_complaints,
8046 _("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
8047 " has children (offset %s) [in module %s]"),
8048 sect_offset_str (this_cu->sect_off),
8049 bfd_get_filename (abfd));
6a506a2d 8050 }
b0c7bfa9 8051 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 8052 if (dwo_unit != NULL)
3019eac3 8053 {
6a506a2d 8054 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 8055 comp_unit_die, NULL,
6a506a2d 8056 &reader, &info_ptr,
685af9cd
TT
8057 &dwo_comp_unit_die, &has_children,
8058 &dwo_abbrev_table) == 0)
6a506a2d
DE
8059 {
8060 /* Dummy die. */
6a506a2d
DE
8061 return;
8062 }
8063 comp_unit_die = dwo_comp_unit_die;
8064 }
8065 else
8066 {
8067 /* Yikes, we couldn't find the rest of the DIE, we only have
8068 the stub. A complaint has already been logged. There's
8069 not much more we can do except pass on the stub DIE to
8070 die_reader_func. We don't want to throw an error on bad
8071 debug info. */
3019eac3
DE
8072 }
8073 }
8074
b0c7bfa9 8075 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
8076 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8077
b0c7bfa9 8078 /* Done, clean up. */
fcd3b13d 8079 if (new_cu != NULL && keep)
348e048f 8080 {
fcd3b13d
SM
8081 /* Link this CU into read_in_chain. */
8082 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
8083 dwarf2_per_objfile->read_in_chain = this_cu;
8084 /* The chain owns it now. */
8085 new_cu.release ();
348e048f 8086 }
dee91e82
DE
8087}
8088
33e80786
DE
8089/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
8090 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
8091 to have already done the lookup to find the DWO file).
dee91e82
DE
8092
8093 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 8094 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
8095
8096 We fill in THIS_CU->length.
8097
8098 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
8099 linker) then DIE_READER_FUNC will not get called.
8100
8101 THIS_CU->cu is always freed when done.
3019eac3
DE
8102 This is done in order to not leave THIS_CU->cu in a state where we have
8103 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
8104
8105static void
8106init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 8107 struct dwo_file *dwo_file,
dee91e82
DE
8108 die_reader_func_ftype *die_reader_func,
8109 void *data)
8110{
ed2dc618 8111 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 8112 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 8113 struct dwarf2_section_info *section = this_cu->section;
a32a8923 8114 bfd *abfd = get_section_bfd_owner (section);
33e80786 8115 struct dwarf2_section_info *abbrev_section;
d521ce57 8116 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 8117 struct die_reader_specs reader;
dee91e82
DE
8118 struct die_info *comp_unit_die;
8119 int has_children;
8120
b4f54984 8121 if (dwarf_die_debug)
9d8780f0 8122 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 8123 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 8124 sect_offset_str (this_cu->sect_off));
09406207 8125
dee91e82
DE
8126 gdb_assert (this_cu->cu == NULL);
8127
33e80786
DE
8128 abbrev_section = (dwo_file != NULL
8129 ? &dwo_file->sections.abbrev
8130 : get_abbrev_section_for_cu (this_cu));
8131
dee91e82
DE
8132 /* This is cheap if the section is already read in. */
8133 dwarf2_read_section (objfile, section);
8134
fcd3b13d 8135 struct dwarf2_cu cu (this_cu);
dee91e82 8136
9c541725 8137 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618
SM
8138 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
8139 &cu.header, section,
4bdcc0c1 8140 abbrev_section, info_ptr,
43988095
JK
8141 (this_cu->is_debug_types
8142 ? rcuh_kind::TYPE
8143 : rcuh_kind::COMPILE));
dee91e82 8144
1ce1cefd 8145 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
8146
8147 /* Skip dummy compilation units. */
8148 if (info_ptr >= begin_info_ptr + this_cu->length
8149 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 8150 return;
72bf9492 8151
685af9cd
TT
8152 abbrev_table_up abbrev_table
8153 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
8154 cu.header.abbrev_sect_off);
dee91e82 8155
685af9cd 8156 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
dee91e82
DE
8157 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
8158
8159 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
dee91e82
DE
8160}
8161
3019eac3
DE
8162/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
8163 does not lookup the specified DWO file.
8164 This cannot be used to read DWO files.
dee91e82
DE
8165
8166 THIS_CU->cu is always freed when done.
3019eac3
DE
8167 This is done in order to not leave THIS_CU->cu in a state where we have
8168 to care whether it refers to the "main" CU or the DWO CU.
8169 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
8170
8171static void
8172init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
8173 die_reader_func_ftype *die_reader_func,
8174 void *data)
8175{
33e80786 8176 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 8177}
0018ea6f
DE
8178\f
8179/* Type Unit Groups.
dee91e82 8180
0018ea6f
DE
8181 Type Unit Groups are a way to collapse the set of all TUs (type units) into
8182 a more manageable set. The grouping is done by DW_AT_stmt_list entry
8183 so that all types coming from the same compilation (.o file) are grouped
8184 together. A future step could be to put the types in the same symtab as
8185 the CU the types ultimately came from. */
ff013f42 8186
f4dc4d17
DE
8187static hashval_t
8188hash_type_unit_group (const void *item)
8189{
9a3c8263
SM
8190 const struct type_unit_group *tu_group
8191 = (const struct type_unit_group *) item;
f4dc4d17 8192
094b34ac 8193 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 8194}
348e048f
DE
8195
8196static int
f4dc4d17 8197eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 8198{
9a3c8263
SM
8199 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
8200 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 8201
094b34ac 8202 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 8203}
348e048f 8204
f4dc4d17
DE
8205/* Allocate a hash table for type unit groups. */
8206
8207static htab_t
ed2dc618 8208allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
8209{
8210 return htab_create_alloc_ex (3,
8211 hash_type_unit_group,
8212 eq_type_unit_group,
8213 NULL,
ed2dc618 8214 &objfile->objfile_obstack,
f4dc4d17
DE
8215 hashtab_obstack_allocate,
8216 dummy_obstack_deallocate);
8217}
dee91e82 8218
f4dc4d17
DE
8219/* Type units that don't have DW_AT_stmt_list are grouped into their own
8220 partial symtabs. We combine several TUs per psymtab to not let the size
8221 of any one psymtab grow too big. */
8222#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
8223#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 8224
094b34ac 8225/* Helper routine for get_type_unit_group.
f4dc4d17
DE
8226 Create the type_unit_group object used to hold one or more TUs. */
8227
8228static struct type_unit_group *
094b34ac 8229create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 8230{
518817b3
SM
8231 struct dwarf2_per_objfile *dwarf2_per_objfile
8232 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 8233 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 8234 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 8235 struct type_unit_group *tu_group;
f4dc4d17
DE
8236
8237 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8238 struct type_unit_group);
094b34ac 8239 per_cu = &tu_group->per_cu;
518817b3 8240 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 8241
094b34ac
DE
8242 if (dwarf2_per_objfile->using_index)
8243 {
8244 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8245 struct dwarf2_per_cu_quick_data);
094b34ac
DE
8246 }
8247 else
8248 {
9c541725 8249 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac
DE
8250 struct partial_symtab *pst;
8251 char *name;
8252
8253 /* Give the symtab a useful name for debug purposes. */
8254 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
8255 name = xstrprintf ("<type_units_%d>",
8256 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
8257 else
8258 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
8259
8260 pst = create_partial_symtab (per_cu, name);
8261 pst->anonymous = 1;
f4dc4d17 8262
094b34ac
DE
8263 xfree (name);
8264 }
f4dc4d17 8265
094b34ac 8266 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 8267 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
8268
8269 return tu_group;
8270}
8271
094b34ac
DE
8272/* Look up the type_unit_group for type unit CU, and create it if necessary.
8273 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
8274
8275static struct type_unit_group *
ff39bb5e 8276get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 8277{
518817b3
SM
8278 struct dwarf2_per_objfile *dwarf2_per_objfile
8279 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8280 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8281 struct type_unit_group *tu_group;
8282 void **slot;
8283 unsigned int line_offset;
8284 struct type_unit_group type_unit_group_for_lookup;
8285
8286 if (dwarf2_per_objfile->type_unit_groups == NULL)
8287 {
8288 dwarf2_per_objfile->type_unit_groups =
ed2dc618 8289 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
8290 }
8291
8292 /* Do we need to create a new group, or can we use an existing one? */
8293
8294 if (stmt_list)
8295 {
8296 line_offset = DW_UNSND (stmt_list);
8297 ++tu_stats->nr_symtab_sharers;
8298 }
8299 else
8300 {
8301 /* Ugh, no stmt_list. Rare, but we have to handle it.
8302 We can do various things here like create one group per TU or
8303 spread them over multiple groups to split up the expansion work.
8304 To avoid worst case scenarios (too many groups or too large groups)
8305 we, umm, group them in bunches. */
8306 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
8307 | (tu_stats->nr_stmt_less_type_units
8308 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
8309 ++tu_stats->nr_stmt_less_type_units;
8310 }
8311
094b34ac 8312 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 8313 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
8314 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
8315 &type_unit_group_for_lookup, INSERT);
8316 if (*slot != NULL)
8317 {
9a3c8263 8318 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
8319 gdb_assert (tu_group != NULL);
8320 }
8321 else
8322 {
9c541725 8323 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 8324 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
8325 *slot = tu_group;
8326 ++tu_stats->nr_symtabs;
8327 }
8328
8329 return tu_group;
8330}
0018ea6f
DE
8331\f
8332/* Partial symbol tables. */
8333
8334/* Create a psymtab named NAME and assign it to PER_CU.
8335
8336 The caller must fill in the following details:
8337 dirname, textlow, texthigh. */
8338
8339static struct partial_symtab *
8340create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8341{
e3b94546 8342 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
0018ea6f
DE
8343 struct partial_symtab *pst;
8344
18a94d75 8345 pst = start_psymtab_common (objfile, name, 0,
af5bf4ad
SM
8346 objfile->global_psymbols,
8347 objfile->static_psymbols);
0018ea6f
DE
8348
8349 pst->psymtabs_addrmap_supported = 1;
8350
8351 /* This is the glue that links PST into GDB's symbol API. */
8352 pst->read_symtab_private = per_cu;
8353 pst->read_symtab = dwarf2_read_symtab;
8354 per_cu->v.psymtab = pst;
8355
8356 return pst;
8357}
8358
b93601f3
TT
8359/* The DATA object passed to process_psymtab_comp_unit_reader has this
8360 type. */
8361
8362struct process_psymtab_comp_unit_data
8363{
8364 /* True if we are reading a DW_TAG_partial_unit. */
8365
8366 int want_partial_unit;
8367
8368 /* The "pretend" language that is used if the CU doesn't declare a
8369 language. */
8370
8371 enum language pretend_language;
8372};
8373
0018ea6f
DE
8374/* die_reader_func for process_psymtab_comp_unit. */
8375
8376static void
8377process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8378 const gdb_byte *info_ptr,
0018ea6f
DE
8379 struct die_info *comp_unit_die,
8380 int has_children,
8381 void *data)
8382{
8383 struct dwarf2_cu *cu = reader->cu;
518817b3 8384 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 8385 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 8386 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
8387 CORE_ADDR baseaddr;
8388 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8389 struct partial_symtab *pst;
3a2b436a 8390 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 8391 const char *filename;
9a3c8263
SM
8392 struct process_psymtab_comp_unit_data *info
8393 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 8394
b93601f3 8395 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
8396 return;
8397
8398 gdb_assert (! per_cu->is_debug_types);
8399
b93601f3 8400 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
8401
8402 cu->list_in_scope = &file_symbols;
8403
8404 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
8405 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8406 if (filename == NULL)
0018ea6f 8407 filename = "";
0018ea6f
DE
8408
8409 pst = create_partial_symtab (per_cu, filename);
8410
8411 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 8412 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
8413
8414 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8415
8416 dwarf2_find_base_address (comp_unit_die, cu);
8417
8418 /* Possibly set the default values of LOWPC and HIGHPC from
8419 `DW_AT_ranges'. */
3a2b436a
JK
8420 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8421 &best_highpc, cu, pst);
8422 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
0018ea6f
DE
8423 /* Store the contiguous range if it is not empty; it can be empty for
8424 CUs with no code. */
8425 addrmap_set_empty (objfile->psymtabs_addrmap,
3e29f34a
MR
8426 gdbarch_adjust_dwarf2_addr (gdbarch,
8427 best_lowpc + baseaddr),
8428 gdbarch_adjust_dwarf2_addr (gdbarch,
8429 best_highpc + baseaddr) - 1,
8430 pst);
0018ea6f
DE
8431
8432 /* Check if comp unit has_children.
8433 If so, read the rest of the partial symbols from this comp unit.
8434 If not, there's no more debug_info for this comp unit. */
8435 if (has_children)
8436 {
8437 struct partial_die_info *first_die;
8438 CORE_ADDR lowpc, highpc;
8439
8440 lowpc = ((CORE_ADDR) -1);
8441 highpc = ((CORE_ADDR) 0);
8442
8443 first_die = load_partial_dies (reader, info_ptr, 1);
8444
8445 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 8446 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
8447
8448 /* If we didn't find a lowpc, set it to highpc to avoid
8449 complaints from `maint check'. */
8450 if (lowpc == ((CORE_ADDR) -1))
8451 lowpc = highpc;
8452
8453 /* If the compilation unit didn't have an explicit address range,
8454 then use the information extracted from its child dies. */
e385593e 8455 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
8456 {
8457 best_lowpc = lowpc;
8458 best_highpc = highpc;
8459 }
8460 }
3e29f34a
MR
8461 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
8462 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 8463
8763cede 8464 end_psymtab_common (objfile, pst);
0018ea6f
DE
8465
8466 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8467 {
8468 int i;
8469 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8470 struct dwarf2_per_cu_data *iter;
8471
8472 /* Fill in 'dependencies' here; we fill in 'users' in a
8473 post-pass. */
8474 pst->number_of_dependencies = len;
8d749320
SM
8475 pst->dependencies =
8476 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
8477 for (i = 0;
8478 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8479 i, iter);
8480 ++i)
8481 pst->dependencies[i] = iter->v.psymtab;
8482
8483 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8484 }
8485
8486 /* Get the list of files included in the current compilation unit,
8487 and build a psymtab for each of them. */
8488 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8489
b4f54984 8490 if (dwarf_read_debug)
0018ea6f
DE
8491 {
8492 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8493
8494 fprintf_unfiltered (gdb_stdlog,
9d8780f0 8495 "Psymtab for %s unit @%s: %s - %s"
0018ea6f
DE
8496 ", %d global, %d static syms\n",
8497 per_cu->is_debug_types ? "type" : "comp",
9d8780f0 8498 sect_offset_str (per_cu->sect_off),
0018ea6f
DE
8499 paddress (gdbarch, pst->textlow),
8500 paddress (gdbarch, pst->texthigh),
8501 pst->n_global_syms, pst->n_static_syms);
8502 }
8503}
8504
8505/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8506 Process compilation unit THIS_CU for a psymtab. */
8507
8508static void
8509process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8510 int want_partial_unit,
8511 enum language pretend_language)
0018ea6f
DE
8512{
8513 /* If this compilation unit was already read in, free the
8514 cached copy in order to read it in again. This is
8515 necessary because we skipped some symbols when we first
8516 read in the compilation unit (see load_partial_dies).
8517 This problem could be avoided, but the benefit is unclear. */
8518 if (this_cu->cu != NULL)
8519 free_one_cached_comp_unit (this_cu);
8520
f1902523
JK
8521 if (this_cu->is_debug_types)
8522 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8523 NULL);
8524 else
8525 {
8526 process_psymtab_comp_unit_data info;
8527 info.want_partial_unit = want_partial_unit;
8528 info.pretend_language = pretend_language;
8529 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8530 process_psymtab_comp_unit_reader, &info);
8531 }
0018ea6f
DE
8532
8533 /* Age out any secondary CUs. */
ed2dc618 8534 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 8535}
f4dc4d17
DE
8536
8537/* Reader function for build_type_psymtabs. */
8538
8539static void
8540build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8541 const gdb_byte *info_ptr,
f4dc4d17
DE
8542 struct die_info *type_unit_die,
8543 int has_children,
8544 void *data)
8545{
ed2dc618 8546 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 8547 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8548 struct objfile *objfile = dwarf2_per_objfile->objfile;
8549 struct dwarf2_cu *cu = reader->cu;
8550 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8551 struct signatured_type *sig_type;
f4dc4d17
DE
8552 struct type_unit_group *tu_group;
8553 struct attribute *attr;
8554 struct partial_die_info *first_die;
8555 CORE_ADDR lowpc, highpc;
8556 struct partial_symtab *pst;
8557
8558 gdb_assert (data == NULL);
0186c6a7
DE
8559 gdb_assert (per_cu->is_debug_types);
8560 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8561
8562 if (! has_children)
8563 return;
8564
8565 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8566 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8567
0186c6a7 8568 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
8569
8570 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8571 cu->list_in_scope = &file_symbols;
8572 pst = create_partial_symtab (per_cu, "");
8573 pst->anonymous = 1;
8574
8575 first_die = load_partial_dies (reader, info_ptr, 1);
8576
8577 lowpc = (CORE_ADDR) -1;
8578 highpc = (CORE_ADDR) 0;
8579 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8580
8763cede 8581 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8582}
8583
73051182
DE
8584/* Struct used to sort TUs by their abbreviation table offset. */
8585
8586struct tu_abbrev_offset
8587{
8588 struct signatured_type *sig_type;
8589 sect_offset abbrev_offset;
8590};
8591
484cf504 8592/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 8593
484cf504
TT
8594static bool
8595sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8596 const struct tu_abbrev_offset &b)
73051182 8597{
484cf504 8598 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
8599}
8600
8601/* Efficiently read all the type units.
8602 This does the bulk of the work for build_type_psymtabs.
8603
8604 The efficiency is because we sort TUs by the abbrev table they use and
8605 only read each abbrev table once. In one program there are 200K TUs
8606 sharing 8K abbrev tables.
8607
8608 The main purpose of this function is to support building the
8609 dwarf2_per_objfile->type_unit_groups table.
8610 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8611 can collapse the search space by grouping them by stmt_list.
8612 The savings can be significant, in the same program from above the 200K TUs
8613 share 8K stmt_list tables.
8614
8615 FUNC is expected to call get_type_unit_group, which will create the
8616 struct type_unit_group if necessary and add it to
8617 dwarf2_per_objfile->type_unit_groups. */
8618
8619static void
ed2dc618 8620build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 8621{
73051182 8622 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 8623 abbrev_table_up abbrev_table;
73051182 8624 sect_offset abbrev_offset;
73051182
DE
8625 int i;
8626
8627 /* It's up to the caller to not call us multiple times. */
8628 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8629
8630 if (dwarf2_per_objfile->n_type_units == 0)
8631 return;
8632
8633 /* TUs typically share abbrev tables, and there can be way more TUs than
8634 abbrev tables. Sort by abbrev table to reduce the number of times we
8635 read each abbrev table in.
8636 Alternatives are to punt or to maintain a cache of abbrev tables.
8637 This is simpler and efficient enough for now.
8638
8639 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8640 symtab to use). Typically TUs with the same abbrev offset have the same
8641 stmt_list value too so in practice this should work well.
8642
8643 The basic algorithm here is:
8644
8645 sort TUs by abbrev table
8646 for each TU with same abbrev table:
8647 read abbrev table if first user
8648 read TU top level DIE
8649 [IWBN if DWO skeletons had DW_AT_stmt_list]
8650 call FUNC */
8651
b4f54984 8652 if (dwarf_read_debug)
73051182
DE
8653 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8654
8655 /* Sort in a separate table to maintain the order of all_type_units
8656 for .gdb_index: TU indices directly index all_type_units. */
484cf504
TT
8657 std::vector<struct tu_abbrev_offset> sorted_by_abbrev
8658 (dwarf2_per_objfile->n_type_units);
73051182
DE
8659 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8660 {
8661 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
8662
8663 sorted_by_abbrev[i].sig_type = sig_type;
8664 sorted_by_abbrev[i].abbrev_offset =
ed2dc618
SM
8665 read_abbrev_offset (dwarf2_per_objfile,
8666 sig_type->per_cu.section,
9c541725 8667 sig_type->per_cu.sect_off);
73051182 8668 }
484cf504
TT
8669 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8670 sort_tu_by_abbrev_offset);
73051182 8671
9c541725 8672 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182
DE
8673
8674 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8675 {
8676 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
8677
8678 /* Switch to the next abbrev table if necessary. */
8679 if (abbrev_table == NULL
9c541725 8680 || tu->abbrev_offset != abbrev_offset)
73051182 8681 {
73051182
DE
8682 abbrev_offset = tu->abbrev_offset;
8683 abbrev_table =
ed2dc618
SM
8684 abbrev_table_read_table (dwarf2_per_objfile,
8685 &dwarf2_per_objfile->abbrev,
73051182
DE
8686 abbrev_offset);
8687 ++tu_stats->nr_uniq_abbrev_tables;
8688 }
8689
685af9cd
TT
8690 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table.get (),
8691 0, 0, build_type_psymtabs_reader, NULL);
73051182 8692 }
6aa5f3a6 8693}
73051182 8694
6aa5f3a6
DE
8695/* Print collected type unit statistics. */
8696
8697static void
ed2dc618 8698print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8699{
8700 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8701
8702 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8703 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
8704 dwarf2_per_objfile->n_type_units);
8705 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8706 tu_stats->nr_uniq_abbrev_tables);
8707 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8708 tu_stats->nr_symtabs);
8709 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8710 tu_stats->nr_symtab_sharers);
8711 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8712 tu_stats->nr_stmt_less_type_units);
8713 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8714 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8715}
8716
f4dc4d17
DE
8717/* Traversal function for build_type_psymtabs. */
8718
8719static int
8720build_type_psymtab_dependencies (void **slot, void *info)
8721{
ed2dc618
SM
8722 struct dwarf2_per_objfile *dwarf2_per_objfile
8723 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8724 struct objfile *objfile = dwarf2_per_objfile->objfile;
8725 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8726 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 8727 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
8728 int len = VEC_length (sig_type_ptr, tu_group->tus);
8729 struct signatured_type *iter;
f4dc4d17
DE
8730 int i;
8731
8732 gdb_assert (len > 0);
0186c6a7 8733 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8734
8735 pst->number_of_dependencies = len;
8d749320
SM
8736 pst->dependencies =
8737 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 8738 for (i = 0;
0186c6a7 8739 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
8740 ++i)
8741 {
0186c6a7
DE
8742 gdb_assert (iter->per_cu.is_debug_types);
8743 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8744 iter->type_unit_group = tu_group;
f4dc4d17
DE
8745 }
8746
0186c6a7 8747 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
8748
8749 return 1;
8750}
8751
8752/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8753 Build partial symbol tables for the .debug_types comp-units. */
8754
8755static void
ed2dc618 8756build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8757{
ed2dc618 8758 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8759 return;
8760
ed2dc618 8761 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8762}
f4dc4d17 8763
6aa5f3a6
DE
8764/* Traversal function for process_skeletonless_type_unit.
8765 Read a TU in a DWO file and build partial symbols for it. */
8766
8767static int
8768process_skeletonless_type_unit (void **slot, void *info)
8769{
8770 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8771 struct dwarf2_per_objfile *dwarf2_per_objfile
8772 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8773 struct signatured_type find_entry, *entry;
8774
8775 /* If this TU doesn't exist in the global table, add it and read it in. */
8776
8777 if (dwarf2_per_objfile->signatured_types == NULL)
8778 {
8779 dwarf2_per_objfile->signatured_types
ed2dc618 8780 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8781 }
8782
8783 find_entry.signature = dwo_unit->signature;
8784 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8785 INSERT);
8786 /* If we've already seen this type there's nothing to do. What's happening
8787 is we're doing our own version of comdat-folding here. */
8788 if (*slot != NULL)
8789 return 1;
8790
8791 /* This does the job that create_all_type_units would have done for
8792 this TU. */
ed2dc618
SM
8793 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8794 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8795 *slot = entry;
8796
8797 /* This does the job that build_type_psymtabs_1 would have done. */
8798 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8799 build_type_psymtabs_reader, NULL);
8800
8801 return 1;
8802}
8803
8804/* Traversal function for process_skeletonless_type_units. */
8805
8806static int
8807process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8808{
8809 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8810
8811 if (dwo_file->tus != NULL)
8812 {
8813 htab_traverse_noresize (dwo_file->tus,
8814 process_skeletonless_type_unit, info);
8815 }
8816
8817 return 1;
8818}
8819
8820/* Scan all TUs of DWO files, verifying we've processed them.
8821 This is needed in case a TU was emitted without its skeleton.
8822 Note: This can't be done until we know what all the DWO files are. */
8823
8824static void
ed2dc618 8825process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8826{
8827 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8828 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8829 && dwarf2_per_objfile->dwo_files != NULL)
8830 {
8831 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8832 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8833 dwarf2_per_objfile);
6aa5f3a6 8834 }
348e048f
DE
8835}
8836
ed2dc618 8837/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8838
8839static void
ed2dc618 8840set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad
TT
8841{
8842 int i;
8843
8844 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8845 {
ed2dc618 8846 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
95554aad
TT
8847 struct partial_symtab *pst = per_cu->v.psymtab;
8848 int j;
8849
36586728
TT
8850 if (pst == NULL)
8851 continue;
8852
95554aad
TT
8853 for (j = 0; j < pst->number_of_dependencies; ++j)
8854 {
8855 /* Set the 'user' field only if it is not already set. */
8856 if (pst->dependencies[j]->user == NULL)
8857 pst->dependencies[j]->user = pst;
8858 }
8859 }
8860}
8861
93311388
DE
8862/* Build the partial symbol table by doing a quick pass through the
8863 .debug_info and .debug_abbrev sections. */
72bf9492 8864
93311388 8865static void
ed2dc618 8866dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8867{
791afaa2 8868 struct cleanup *back_to;
21b2bd31 8869 int i;
ed2dc618 8870 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8871
b4f54984 8872 if (dwarf_read_debug)
45cfd468
DE
8873 {
8874 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8875 objfile_name (objfile));
45cfd468
DE
8876 }
8877
98bfdba5
PA
8878 dwarf2_per_objfile->reading_partial_symbols = 1;
8879
be391dca 8880 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8881
93311388
DE
8882 /* Any cached compilation units will be linked by the per-objfile
8883 read_in_chain. Make sure to free them when we're done. */
ed2dc618 8884 back_to = make_cleanup (free_cached_comp_units, dwarf2_per_objfile);
72bf9492 8885
ed2dc618 8886 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8887
ed2dc618 8888 create_all_comp_units (dwarf2_per_objfile);
c906108c 8889
60606b2c
TT
8890 /* Create a temporary address map on a temporary obstack. We later
8891 copy this to the final obstack. */
8268c778 8892 auto_obstack temp_obstack;
791afaa2
TT
8893
8894 scoped_restore save_psymtabs_addrmap
8895 = make_scoped_restore (&objfile->psymtabs_addrmap,
8896 addrmap_create_mutable (&temp_obstack));
72bf9492 8897
21b2bd31 8898 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 8899 {
ed2dc618 8900 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
aaa75496 8901
b93601f3 8902 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 8903 }
ff013f42 8904
6aa5f3a6 8905 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8906 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8907
8908 /* Now that all TUs have been processed we can fill in the dependencies. */
8909 if (dwarf2_per_objfile->type_unit_groups != NULL)
8910 {
8911 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8912 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8913 }
8914
b4f54984 8915 if (dwarf_read_debug)
ed2dc618 8916 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8917
ed2dc618 8918 set_partial_user (dwarf2_per_objfile);
95554aad 8919
ff013f42
JK
8920 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8921 &objfile->objfile_obstack);
791afaa2
TT
8922 /* At this point we want to keep the address map. */
8923 save_psymtabs_addrmap.release ();
ff013f42 8924
ae038cb0 8925 do_cleanups (back_to);
45cfd468 8926
b4f54984 8927 if (dwarf_read_debug)
45cfd468 8928 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8929 objfile_name (objfile));
ae038cb0
DJ
8930}
8931
3019eac3 8932/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
8933
8934static void
dee91e82 8935load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8936 const gdb_byte *info_ptr,
dee91e82
DE
8937 struct die_info *comp_unit_die,
8938 int has_children,
8939 void *data)
ae038cb0 8940{
dee91e82 8941 struct dwarf2_cu *cu = reader->cu;
ae038cb0 8942
95554aad 8943 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 8944
ae038cb0
DJ
8945 /* Check if comp unit has_children.
8946 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 8947 If not, there's no more debug_info for this comp unit. */
d85a05f0 8948 if (has_children)
dee91e82
DE
8949 load_partial_dies (reader, info_ptr, 0);
8950}
98bfdba5 8951
dee91e82
DE
8952/* Load the partial DIEs for a secondary CU into memory.
8953 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8954
dee91e82
DE
8955static void
8956load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8957{
f4dc4d17
DE
8958 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8959 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
8960}
8961
ae038cb0 8962static void
ed2dc618 8963read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8964 struct dwarf2_section_info *section,
f1902523 8965 struct dwarf2_section_info *abbrev_section,
36586728
TT
8966 unsigned int is_dwz,
8967 int *n_allocated,
8968 int *n_comp_units,
8969 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 8970{
d521ce57 8971 const gdb_byte *info_ptr;
ed2dc618 8972 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8973
b4f54984 8974 if (dwarf_read_debug)
bf6af496 8975 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8976 get_section_name (section),
8977 get_section_file_name (section));
bf6af496 8978
36586728 8979 dwarf2_read_section (objfile, section);
ae038cb0 8980
36586728 8981 info_ptr = section->buffer;
6e70227d 8982
36586728 8983 while (info_ptr < section->buffer + section->size)
ae038cb0 8984 {
ae038cb0 8985 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8986
9c541725 8987 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8988
f1902523 8989 comp_unit_head cu_header;
ed2dc618
SM
8990 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8991 abbrev_section, info_ptr,
8992 rcuh_kind::COMPILE);
ae038cb0
DJ
8993
8994 /* Save the compilation unit for later lookup. */
f1902523
JK
8995 if (cu_header.unit_type != DW_UT_type)
8996 {
8997 this_cu = XOBNEW (&objfile->objfile_obstack,
8998 struct dwarf2_per_cu_data);
8999 memset (this_cu, 0, sizeof (*this_cu));
9000 }
9001 else
9002 {
9003 auto sig_type = XOBNEW (&objfile->objfile_obstack,
9004 struct signatured_type);
9005 memset (sig_type, 0, sizeof (*sig_type));
9006 sig_type->signature = cu_header.signature;
9007 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
9008 this_cu = &sig_type->per_cu;
9009 }
9010 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 9011 this_cu->sect_off = sect_off;
f1902523 9012 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 9013 this_cu->is_dwz = is_dwz;
e3b94546 9014 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 9015 this_cu->section = section;
ae038cb0 9016
36586728 9017 if (*n_comp_units == *n_allocated)
ae038cb0 9018 {
36586728 9019 *n_allocated *= 2;
224c3ddb
SM
9020 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
9021 *all_comp_units, *n_allocated);
ae038cb0 9022 }
36586728
TT
9023 (*all_comp_units)[*n_comp_units] = this_cu;
9024 ++*n_comp_units;
ae038cb0
DJ
9025
9026 info_ptr = info_ptr + this_cu->length;
9027 }
36586728
TT
9028}
9029
9030/* Create a list of all compilation units in OBJFILE.
9031 This is only done for -readnow and building partial symtabs. */
9032
9033static void
ed2dc618 9034create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728
TT
9035{
9036 int n_allocated;
9037 int n_comp_units;
9038 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 9039 struct dwz_file *dwz;
ed2dc618 9040 struct objfile *objfile = dwarf2_per_objfile->objfile;
36586728
TT
9041
9042 n_comp_units = 0;
9043 n_allocated = 10;
8d749320 9044 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
36586728 9045
ed2dc618 9046 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
f1902523 9047 &dwarf2_per_objfile->abbrev, 0,
36586728
TT
9048 &n_allocated, &n_comp_units, &all_comp_units);
9049
ed2dc618 9050 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 9051 if (dwz != NULL)
ed2dc618
SM
9052 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
9053 1, &n_allocated, &n_comp_units,
4db1a1dc 9054 &all_comp_units);
ae038cb0 9055
8d749320
SM
9056 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
9057 struct dwarf2_per_cu_data *,
9058 n_comp_units);
ae038cb0
DJ
9059 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
9060 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
9061 xfree (all_comp_units);
9062 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
9063}
9064
5734ee8b 9065/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 9066 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 9067 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
9068 DW_AT_ranges). See the comments of add_partial_subprogram on how
9069 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 9070
72bf9492
DJ
9071static void
9072scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
9073 CORE_ADDR *highpc, int set_addrmap,
9074 struct dwarf2_cu *cu)
c906108c 9075{
72bf9492 9076 struct partial_die_info *pdi;
c906108c 9077
91c24f0a
DC
9078 /* Now, march along the PDI's, descending into ones which have
9079 interesting children but skipping the children of the other ones,
9080 until we reach the end of the compilation unit. */
c906108c 9081
72bf9492 9082 pdi = first_die;
91c24f0a 9083
72bf9492
DJ
9084 while (pdi != NULL)
9085 {
52356b79 9086 pdi->fixup (cu);
c906108c 9087
f55ee35c 9088 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
9089 children, so we need to look at them. Ditto for anonymous
9090 enums. */
933c6fe4 9091
72bf9492 9092 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 9093 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
9094 || pdi->tag == DW_TAG_imported_unit
9095 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 9096 {
72bf9492 9097 switch (pdi->tag)
c906108c
SS
9098 {
9099 case DW_TAG_subprogram:
b1dc1806 9100 case DW_TAG_inlined_subroutine:
cdc07690 9101 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 9102 break;
72929c62 9103 case DW_TAG_constant:
c906108c
SS
9104 case DW_TAG_variable:
9105 case DW_TAG_typedef:
91c24f0a 9106 case DW_TAG_union_type:
72bf9492 9107 if (!pdi->is_declaration)
63d06c5c 9108 {
72bf9492 9109 add_partial_symbol (pdi, cu);
63d06c5c
DC
9110 }
9111 break;
c906108c 9112 case DW_TAG_class_type:
680b30c7 9113 case DW_TAG_interface_type:
c906108c 9114 case DW_TAG_structure_type:
72bf9492 9115 if (!pdi->is_declaration)
c906108c 9116 {
72bf9492 9117 add_partial_symbol (pdi, cu);
c906108c 9118 }
e98c9e7c
TT
9119 if (cu->language == language_rust && pdi->has_children)
9120 scan_partial_symbols (pdi->die_child, lowpc, highpc,
9121 set_addrmap, cu);
c906108c 9122 break;
91c24f0a 9123 case DW_TAG_enumeration_type:
72bf9492
DJ
9124 if (!pdi->is_declaration)
9125 add_partial_enumeration (pdi, cu);
c906108c
SS
9126 break;
9127 case DW_TAG_base_type:
a02abb62 9128 case DW_TAG_subrange_type:
c906108c 9129 /* File scope base type definitions are added to the partial
c5aa993b 9130 symbol table. */
72bf9492 9131 add_partial_symbol (pdi, cu);
c906108c 9132 break;
d9fa45fe 9133 case DW_TAG_namespace:
cdc07690 9134 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 9135 break;
5d7cb8df 9136 case DW_TAG_module:
cdc07690 9137 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 9138 break;
95554aad
TT
9139 case DW_TAG_imported_unit:
9140 {
9141 struct dwarf2_per_cu_data *per_cu;
9142
f4dc4d17
DE
9143 /* For now we don't handle imported units in type units. */
9144 if (cu->per_cu->is_debug_types)
9145 {
9146 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9147 " supported in type units [in module %s]"),
518817b3 9148 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
9149 }
9150
e3b94546
SM
9151 per_cu = dwarf2_find_containing_comp_unit
9152 (pdi->d.sect_off, pdi->is_dwz,
518817b3 9153 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
9154
9155 /* Go read the partial unit, if needed. */
9156 if (per_cu->v.psymtab == NULL)
b93601f3 9157 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 9158
f4dc4d17 9159 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 9160 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
9161 }
9162 break;
74921315
KS
9163 case DW_TAG_imported_declaration:
9164 add_partial_symbol (pdi, cu);
9165 break;
c906108c
SS
9166 default:
9167 break;
9168 }
9169 }
9170
72bf9492
DJ
9171 /* If the die has a sibling, skip to the sibling. */
9172
9173 pdi = pdi->die_sibling;
9174 }
9175}
9176
9177/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 9178
72bf9492 9179 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 9180 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
9181 Enumerators are an exception; they use the scope of their parent
9182 enumeration type, i.e. the name of the enumeration type is not
9183 prepended to the enumerator.
91c24f0a 9184
72bf9492
DJ
9185 There are two complexities. One is DW_AT_specification; in this
9186 case "parent" means the parent of the target of the specification,
9187 instead of the direct parent of the DIE. The other is compilers
9188 which do not emit DW_TAG_namespace; in this case we try to guess
9189 the fully qualified name of structure types from their members'
9190 linkage names. This must be done using the DIE's children rather
9191 than the children of any DW_AT_specification target. We only need
9192 to do this for structures at the top level, i.e. if the target of
9193 any DW_AT_specification (if any; otherwise the DIE itself) does not
9194 have a parent. */
9195
9196/* Compute the scope prefix associated with PDI's parent, in
9197 compilation unit CU. The result will be allocated on CU's
9198 comp_unit_obstack, or a copy of the already allocated PDI->NAME
9199 field. NULL is returned if no prefix is necessary. */
15d034d0 9200static const char *
72bf9492
DJ
9201partial_die_parent_scope (struct partial_die_info *pdi,
9202 struct dwarf2_cu *cu)
9203{
15d034d0 9204 const char *grandparent_scope;
72bf9492 9205 struct partial_die_info *parent, *real_pdi;
91c24f0a 9206
72bf9492
DJ
9207 /* We need to look at our parent DIE; if we have a DW_AT_specification,
9208 then this means the parent of the specification DIE. */
9209
9210 real_pdi = pdi;
72bf9492 9211 while (real_pdi->has_specification)
36586728
TT
9212 real_pdi = find_partial_die (real_pdi->spec_offset,
9213 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
9214
9215 parent = real_pdi->die_parent;
9216 if (parent == NULL)
9217 return NULL;
9218
9219 if (parent->scope_set)
9220 return parent->scope;
9221
52356b79 9222 parent->fixup (cu);
72bf9492 9223
10b3939b 9224 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 9225
acebe513
UW
9226 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
9227 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
9228 Work around this problem here. */
9229 if (cu->language == language_cplus
6e70227d 9230 && parent->tag == DW_TAG_namespace
acebe513
UW
9231 && strcmp (parent->name, "::") == 0
9232 && grandparent_scope == NULL)
9233 {
9234 parent->scope = NULL;
9235 parent->scope_set = 1;
9236 return NULL;
9237 }
9238
9c6c53f7
SA
9239 if (pdi->tag == DW_TAG_enumerator)
9240 /* Enumerators should not get the name of the enumeration as a prefix. */
9241 parent->scope = grandparent_scope;
9242 else if (parent->tag == DW_TAG_namespace
f55ee35c 9243 || parent->tag == DW_TAG_module
72bf9492
DJ
9244 || parent->tag == DW_TAG_structure_type
9245 || parent->tag == DW_TAG_class_type
680b30c7 9246 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
9247 || parent->tag == DW_TAG_union_type
9248 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
9249 {
9250 if (grandparent_scope == NULL)
9251 parent->scope = parent->name;
9252 else
3e43a32a
MS
9253 parent->scope = typename_concat (&cu->comp_unit_obstack,
9254 grandparent_scope,
f55ee35c 9255 parent->name, 0, cu);
72bf9492 9256 }
72bf9492
DJ
9257 else
9258 {
9259 /* FIXME drow/2004-04-01: What should we be doing with
9260 function-local names? For partial symbols, we should probably be
9261 ignoring them. */
9262 complaint (&symfile_complaints,
9d8780f0
SM
9263 _("unhandled containing DIE tag %d for DIE at %s"),
9264 parent->tag, sect_offset_str (pdi->sect_off));
72bf9492 9265 parent->scope = grandparent_scope;
c906108c
SS
9266 }
9267
72bf9492
DJ
9268 parent->scope_set = 1;
9269 return parent->scope;
9270}
9271
9272/* Return the fully scoped name associated with PDI, from compilation unit
9273 CU. The result will be allocated with malloc. */
4568ecf9 9274
72bf9492
DJ
9275static char *
9276partial_die_full_name (struct partial_die_info *pdi,
9277 struct dwarf2_cu *cu)
9278{
15d034d0 9279 const char *parent_scope;
72bf9492 9280
98bfdba5
PA
9281 /* If this is a template instantiation, we can not work out the
9282 template arguments from partial DIEs. So, unfortunately, we have
9283 to go through the full DIEs. At least any work we do building
9284 types here will be reused if full symbols are loaded later. */
9285 if (pdi->has_template_arguments)
9286 {
52356b79 9287 pdi->fixup (cu);
98bfdba5
PA
9288
9289 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
9290 {
9291 struct die_info *die;
9292 struct attribute attr;
9293 struct dwarf2_cu *ref_cu = cu;
9294
b64f50a1 9295 /* DW_FORM_ref_addr is using section offset. */
b4069958 9296 attr.name = (enum dwarf_attribute) 0;
98bfdba5 9297 attr.form = DW_FORM_ref_addr;
9c541725 9298 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
9299 die = follow_die_ref (NULL, &attr, &ref_cu);
9300
9301 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
9302 }
9303 }
9304
72bf9492
DJ
9305 parent_scope = partial_die_parent_scope (pdi, cu);
9306 if (parent_scope == NULL)
9307 return NULL;
9308 else
f55ee35c 9309 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
9310}
9311
9312static void
72bf9492 9313add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 9314{
518817b3
SM
9315 struct dwarf2_per_objfile *dwarf2_per_objfile
9316 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 9317 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9318 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 9319 CORE_ADDR addr = 0;
15d034d0 9320 const char *actual_name = NULL;
e142c38c 9321 CORE_ADDR baseaddr;
15d034d0 9322 char *built_actual_name;
e142c38c
DJ
9323
9324 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9325
15d034d0
TT
9326 built_actual_name = partial_die_full_name (pdi, cu);
9327 if (built_actual_name != NULL)
9328 actual_name = built_actual_name;
63d06c5c 9329
72bf9492
DJ
9330 if (actual_name == NULL)
9331 actual_name = pdi->name;
9332
c906108c
SS
9333 switch (pdi->tag)
9334 {
b1dc1806 9335 case DW_TAG_inlined_subroutine:
c906108c 9336 case DW_TAG_subprogram:
3e29f34a 9337 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 9338 if (pdi->is_external || cu->language == language_ada)
c906108c 9339 {
2cfa0c8d
JB
9340 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
9341 of the global scope. But in Ada, we want to be able to access
9342 nested procedures globally. So all Ada subprograms are stored
9343 in the global scope. */
f47fb265 9344 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9345 built_actual_name != NULL,
f47fb265
MS
9346 VAR_DOMAIN, LOC_BLOCK,
9347 &objfile->global_psymbols,
1762568f 9348 addr, cu->language, objfile);
c906108c
SS
9349 }
9350 else
9351 {
f47fb265 9352 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9353 built_actual_name != NULL,
f47fb265
MS
9354 VAR_DOMAIN, LOC_BLOCK,
9355 &objfile->static_psymbols,
1762568f 9356 addr, cu->language, objfile);
c906108c 9357 }
0c1b455e
TT
9358
9359 if (pdi->main_subprogram && actual_name != NULL)
9360 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 9361 break;
72929c62
JB
9362 case DW_TAG_constant:
9363 {
af5bf4ad 9364 std::vector<partial_symbol *> *list;
72929c62
JB
9365
9366 if (pdi->is_external)
9367 list = &objfile->global_psymbols;
9368 else
9369 list = &objfile->static_psymbols;
f47fb265 9370 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9371 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 9372 list, 0, cu->language, objfile);
72929c62
JB
9373 }
9374 break;
c906108c 9375 case DW_TAG_variable:
95554aad
TT
9376 if (pdi->d.locdesc)
9377 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 9378
95554aad 9379 if (pdi->d.locdesc
caac4577
JG
9380 && addr == 0
9381 && !dwarf2_per_objfile->has_section_at_zero)
9382 {
9383 /* A global or static variable may also have been stripped
9384 out by the linker if unused, in which case its address
9385 will be nullified; do not add such variables into partial
9386 symbol table then. */
9387 }
9388 else if (pdi->is_external)
c906108c
SS
9389 {
9390 /* Global Variable.
9391 Don't enter into the minimal symbol tables as there is
9392 a minimal symbol table entry from the ELF symbols already.
9393 Enter into partial symbol table if it has a location
9394 descriptor or a type.
9395 If the location descriptor is missing, new_symbol will create
9396 a LOC_UNRESOLVED symbol, the address of the variable will then
9397 be determined from the minimal symbol table whenever the variable
9398 is referenced.
9399 The address for the partial symbol table entry is not
9400 used by GDB, but it comes in handy for debugging partial symbol
9401 table building. */
9402
95554aad 9403 if (pdi->d.locdesc || pdi->has_type)
f47fb265 9404 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9405 built_actual_name != NULL,
f47fb265
MS
9406 VAR_DOMAIN, LOC_STATIC,
9407 &objfile->global_psymbols,
1762568f 9408 addr + baseaddr,
f47fb265 9409 cu->language, objfile);
c906108c
SS
9410 }
9411 else
9412 {
ff908ebf
AW
9413 int has_loc = pdi->d.locdesc != NULL;
9414
9415 /* Static Variable. Skip symbols whose value we cannot know (those
9416 without location descriptors or constant values). */
9417 if (!has_loc && !pdi->has_const_value)
decbce07 9418 {
15d034d0 9419 xfree (built_actual_name);
decbce07
MS
9420 return;
9421 }
ff908ebf 9422
f47fb265 9423 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9424 built_actual_name != NULL,
f47fb265
MS
9425 VAR_DOMAIN, LOC_STATIC,
9426 &objfile->static_psymbols,
ff908ebf 9427 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 9428 cu->language, objfile);
c906108c
SS
9429 }
9430 break;
9431 case DW_TAG_typedef:
9432 case DW_TAG_base_type:
a02abb62 9433 case DW_TAG_subrange_type:
38d518c9 9434 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9435 built_actual_name != NULL,
176620f1 9436 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 9437 &objfile->static_psymbols,
1762568f 9438 0, cu->language, objfile);
c906108c 9439 break;
74921315 9440 case DW_TAG_imported_declaration:
72bf9492
DJ
9441 case DW_TAG_namespace:
9442 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9443 built_actual_name != NULL,
72bf9492
DJ
9444 VAR_DOMAIN, LOC_TYPEDEF,
9445 &objfile->global_psymbols,
1762568f 9446 0, cu->language, objfile);
72bf9492 9447 break;
530e8392
KB
9448 case DW_TAG_module:
9449 add_psymbol_to_list (actual_name, strlen (actual_name),
9450 built_actual_name != NULL,
9451 MODULE_DOMAIN, LOC_TYPEDEF,
9452 &objfile->global_psymbols,
1762568f 9453 0, cu->language, objfile);
530e8392 9454 break;
c906108c 9455 case DW_TAG_class_type:
680b30c7 9456 case DW_TAG_interface_type:
c906108c
SS
9457 case DW_TAG_structure_type:
9458 case DW_TAG_union_type:
9459 case DW_TAG_enumeration_type:
fa4028e9
JB
9460 /* Skip external references. The DWARF standard says in the section
9461 about "Structure, Union, and Class Type Entries": "An incomplete
9462 structure, union or class type is represented by a structure,
9463 union or class entry that does not have a byte size attribute
9464 and that has a DW_AT_declaration attribute." */
9465 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 9466 {
15d034d0 9467 xfree (built_actual_name);
decbce07
MS
9468 return;
9469 }
fa4028e9 9470
63d06c5c
DC
9471 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9472 static vs. global. */
38d518c9 9473 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9474 built_actual_name != NULL,
176620f1 9475 STRUCT_DOMAIN, LOC_TYPEDEF,
9c37b5ae 9476 cu->language == language_cplus
63d06c5c
DC
9477 ? &objfile->global_psymbols
9478 : &objfile->static_psymbols,
1762568f 9479 0, cu->language, objfile);
c906108c 9480
c906108c
SS
9481 break;
9482 case DW_TAG_enumerator:
38d518c9 9483 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9484 built_actual_name != NULL,
176620f1 9485 VAR_DOMAIN, LOC_CONST,
9c37b5ae 9486 cu->language == language_cplus
f6fe98ef
DJ
9487 ? &objfile->global_psymbols
9488 : &objfile->static_psymbols,
1762568f 9489 0, cu->language, objfile);
c906108c
SS
9490 break;
9491 default:
9492 break;
9493 }
5c4e30ca 9494
15d034d0 9495 xfree (built_actual_name);
c906108c
SS
9496}
9497
5c4e30ca
DC
9498/* Read a partial die corresponding to a namespace; also, add a symbol
9499 corresponding to that namespace to the symbol table. NAMESPACE is
9500 the name of the enclosing namespace. */
91c24f0a 9501
72bf9492
DJ
9502static void
9503add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 9504 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9505 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 9506{
72bf9492 9507 /* Add a symbol for the namespace. */
e7c27a73 9508
72bf9492 9509 add_partial_symbol (pdi, cu);
5c4e30ca
DC
9510
9511 /* Now scan partial symbols in that namespace. */
9512
91c24f0a 9513 if (pdi->has_children)
cdc07690 9514 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
9515}
9516
5d7cb8df
JK
9517/* Read a partial die corresponding to a Fortran module. */
9518
9519static void
9520add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 9521 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 9522{
530e8392
KB
9523 /* Add a symbol for the namespace. */
9524
9525 add_partial_symbol (pdi, cu);
9526
f55ee35c 9527 /* Now scan partial symbols in that module. */
5d7cb8df
JK
9528
9529 if (pdi->has_children)
cdc07690 9530 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
9531}
9532
b1dc1806
XR
9533/* Read a partial die corresponding to a subprogram or an inlined
9534 subprogram and create a partial symbol for that subprogram.
9535 When the CU language allows it, this routine also defines a partial
9536 symbol for each nested subprogram that this subprogram contains.
9537 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9538 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 9539
cdc07690
YQ
9540 PDI may also be a lexical block, in which case we simply search
9541 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9542 Again, this is only performed when the CU language allows this
9543 type of definitions. */
9544
9545static void
9546add_partial_subprogram (struct partial_die_info *pdi,
9547 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9548 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 9549{
b1dc1806 9550 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
9551 {
9552 if (pdi->has_pc_info)
9553 {
9554 if (pdi->lowpc < *lowpc)
9555 *lowpc = pdi->lowpc;
9556 if (pdi->highpc > *highpc)
9557 *highpc = pdi->highpc;
cdc07690 9558 if (set_addrmap)
5734ee8b 9559 {
518817b3 9560 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
9561 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9562 CORE_ADDR baseaddr;
9563 CORE_ADDR highpc;
9564 CORE_ADDR lowpc;
5734ee8b
DJ
9565
9566 baseaddr = ANOFFSET (objfile->section_offsets,
9567 SECT_OFF_TEXT (objfile));
3e29f34a
MR
9568 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9569 pdi->lowpc + baseaddr);
9570 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9571 pdi->highpc + baseaddr);
9572 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 9573 cu->per_cu->v.psymtab);
5734ee8b 9574 }
481860b3
GB
9575 }
9576
9577 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9578 {
bc30ff58 9579 if (!pdi->is_declaration)
e8d05480
JB
9580 /* Ignore subprogram DIEs that do not have a name, they are
9581 illegal. Do not emit a complaint at this point, we will
9582 do so when we convert this psymtab into a symtab. */
9583 if (pdi->name)
9584 add_partial_symbol (pdi, cu);
bc30ff58
JB
9585 }
9586 }
6e70227d 9587
bc30ff58
JB
9588 if (! pdi->has_children)
9589 return;
9590
9591 if (cu->language == language_ada)
9592 {
9593 pdi = pdi->die_child;
9594 while (pdi != NULL)
9595 {
52356b79 9596 pdi->fixup (cu);
bc30ff58 9597 if (pdi->tag == DW_TAG_subprogram
b1dc1806 9598 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 9599 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9600 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9601 pdi = pdi->die_sibling;
9602 }
9603 }
9604}
9605
91c24f0a
DC
9606/* Read a partial die corresponding to an enumeration type. */
9607
72bf9492
DJ
9608static void
9609add_partial_enumeration (struct partial_die_info *enum_pdi,
9610 struct dwarf2_cu *cu)
91c24f0a 9611{
72bf9492 9612 struct partial_die_info *pdi;
91c24f0a
DC
9613
9614 if (enum_pdi->name != NULL)
72bf9492
DJ
9615 add_partial_symbol (enum_pdi, cu);
9616
9617 pdi = enum_pdi->die_child;
9618 while (pdi)
91c24f0a 9619 {
72bf9492 9620 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 9621 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 9622 else
72bf9492
DJ
9623 add_partial_symbol (pdi, cu);
9624 pdi = pdi->die_sibling;
91c24f0a 9625 }
91c24f0a
DC
9626}
9627
6caca83c
CC
9628/* Return the initial uleb128 in the die at INFO_PTR. */
9629
9630static unsigned int
d521ce57 9631peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9632{
9633 unsigned int bytes_read;
9634
9635 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9636}
9637
685af9cd
TT
9638/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9639 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9640
4bb7a0a7
DJ
9641 Return the corresponding abbrev, or NULL if the number is zero (indicating
9642 an empty DIE). In either case *BYTES_READ will be set to the length of
9643 the initial number. */
9644
9645static struct abbrev_info *
685af9cd
TT
9646peek_die_abbrev (const die_reader_specs &reader,
9647 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 9648{
685af9cd 9649 dwarf2_cu *cu = reader.cu;
518817b3 9650 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
9651 unsigned int abbrev_number
9652 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
9653
9654 if (abbrev_number == 0)
9655 return NULL;
9656
685af9cd 9657 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
9658 if (!abbrev)
9659 {
422b9917 9660 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 9661 " at offset %s [in module %s]"),
422b9917 9662 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 9663 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9664 }
9665
9666 return abbrev;
9667}
9668
93311388
DE
9669/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9670 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9671 DIE. Any children of the skipped DIEs will also be skipped. */
9672
d521ce57
TT
9673static const gdb_byte *
9674skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9675{
4bb7a0a7
DJ
9676 while (1)
9677 {
685af9cd
TT
9678 unsigned int bytes_read;
9679 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9680
4bb7a0a7
DJ
9681 if (abbrev == NULL)
9682 return info_ptr + bytes_read;
9683 else
dee91e82 9684 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9685 }
9686}
9687
93311388
DE
9688/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9689 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9690 abbrev corresponding to that skipped uleb128 should be passed in
9691 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9692 children. */
9693
d521ce57
TT
9694static const gdb_byte *
9695skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9696 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9697{
9698 unsigned int bytes_read;
9699 struct attribute attr;
dee91e82
DE
9700 bfd *abfd = reader->abfd;
9701 struct dwarf2_cu *cu = reader->cu;
d521ce57 9702 const gdb_byte *buffer = reader->buffer;
f664829e 9703 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9704 unsigned int form, i;
9705
9706 for (i = 0; i < abbrev->num_attrs; i++)
9707 {
9708 /* The only abbrev we care about is DW_AT_sibling. */
9709 if (abbrev->attrs[i].name == DW_AT_sibling)
9710 {
dee91e82 9711 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9712 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9713 complaint (&symfile_complaints,
9714 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9715 else
b9502d3f 9716 {
9c541725
PA
9717 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9718 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9719
9720 if (sibling_ptr < info_ptr)
9721 complaint (&symfile_complaints,
9722 _("DW_AT_sibling points backwards"));
22869d73
KS
9723 else if (sibling_ptr > reader->buffer_end)
9724 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9725 else
9726 return sibling_ptr;
9727 }
4bb7a0a7
DJ
9728 }
9729
9730 /* If it isn't DW_AT_sibling, skip this attribute. */
9731 form = abbrev->attrs[i].form;
9732 skip_attribute:
9733 switch (form)
9734 {
4bb7a0a7 9735 case DW_FORM_ref_addr:
ae411497
TT
9736 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9737 and later it is offset sized. */
9738 if (cu->header.version == 2)
9739 info_ptr += cu->header.addr_size;
9740 else
9741 info_ptr += cu->header.offset_size;
9742 break;
36586728
TT
9743 case DW_FORM_GNU_ref_alt:
9744 info_ptr += cu->header.offset_size;
9745 break;
ae411497 9746 case DW_FORM_addr:
4bb7a0a7
DJ
9747 info_ptr += cu->header.addr_size;
9748 break;
9749 case DW_FORM_data1:
9750 case DW_FORM_ref1:
9751 case DW_FORM_flag:
9752 info_ptr += 1;
9753 break;
2dc7f7b3 9754 case DW_FORM_flag_present:
43988095 9755 case DW_FORM_implicit_const:
2dc7f7b3 9756 break;
4bb7a0a7
DJ
9757 case DW_FORM_data2:
9758 case DW_FORM_ref2:
9759 info_ptr += 2;
9760 break;
9761 case DW_FORM_data4:
9762 case DW_FORM_ref4:
9763 info_ptr += 4;
9764 break;
9765 case DW_FORM_data8:
9766 case DW_FORM_ref8:
55f1336d 9767 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9768 info_ptr += 8;
9769 break;
0224619f
JK
9770 case DW_FORM_data16:
9771 info_ptr += 16;
9772 break;
4bb7a0a7 9773 case DW_FORM_string:
9b1c24c8 9774 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9775 info_ptr += bytes_read;
9776 break;
2dc7f7b3 9777 case DW_FORM_sec_offset:
4bb7a0a7 9778 case DW_FORM_strp:
36586728 9779 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9780 info_ptr += cu->header.offset_size;
9781 break;
2dc7f7b3 9782 case DW_FORM_exprloc:
4bb7a0a7
DJ
9783 case DW_FORM_block:
9784 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9785 info_ptr += bytes_read;
9786 break;
9787 case DW_FORM_block1:
9788 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9789 break;
9790 case DW_FORM_block2:
9791 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9792 break;
9793 case DW_FORM_block4:
9794 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9795 break;
9796 case DW_FORM_sdata:
9797 case DW_FORM_udata:
9798 case DW_FORM_ref_udata:
3019eac3
DE
9799 case DW_FORM_GNU_addr_index:
9800 case DW_FORM_GNU_str_index:
d521ce57 9801 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9802 break;
9803 case DW_FORM_indirect:
9804 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9805 info_ptr += bytes_read;
9806 /* We need to continue parsing from here, so just go back to
9807 the top. */
9808 goto skip_attribute;
9809
9810 default:
3e43a32a
MS
9811 error (_("Dwarf Error: Cannot handle %s "
9812 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9813 dwarf_form_name (form),
9814 bfd_get_filename (abfd));
9815 }
9816 }
9817
9818 if (abbrev->has_children)
dee91e82 9819 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9820 else
9821 return info_ptr;
9822}
9823
93311388 9824/* Locate ORIG_PDI's sibling.
dee91e82 9825 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9826
d521ce57 9827static const gdb_byte *
dee91e82
DE
9828locate_pdi_sibling (const struct die_reader_specs *reader,
9829 struct partial_die_info *orig_pdi,
d521ce57 9830 const gdb_byte *info_ptr)
91c24f0a
DC
9831{
9832 /* Do we know the sibling already? */
72bf9492 9833
91c24f0a
DC
9834 if (orig_pdi->sibling)
9835 return orig_pdi->sibling;
9836
9837 /* Are there any children to deal with? */
9838
9839 if (!orig_pdi->has_children)
9840 return info_ptr;
9841
4bb7a0a7 9842 /* Skip the children the long way. */
91c24f0a 9843
dee91e82 9844 return skip_children (reader, info_ptr);
91c24f0a
DC
9845}
9846
257e7a09 9847/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9848 not NULL. */
c906108c
SS
9849
9850static void
257e7a09
YQ
9851dwarf2_read_symtab (struct partial_symtab *self,
9852 struct objfile *objfile)
c906108c 9853{
ed2dc618
SM
9854 struct dwarf2_per_objfile *dwarf2_per_objfile
9855 = get_dwarf2_per_objfile (objfile);
9856
257e7a09 9857 if (self->readin)
c906108c 9858 {
442e4d9c 9859 warning (_("bug: psymtab for %s is already read in."),
257e7a09 9860 self->filename);
442e4d9c
YQ
9861 }
9862 else
9863 {
9864 if (info_verbose)
c906108c 9865 {
442e4d9c 9866 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 9867 self->filename);
442e4d9c 9868 gdb_flush (gdb_stdout);
c906108c 9869 }
c906108c 9870
442e4d9c
YQ
9871 /* If this psymtab is constructed from a debug-only objfile, the
9872 has_section_at_zero flag will not necessarily be correct. We
9873 can get the correct value for this flag by looking at the data
9874 associated with the (presumably stripped) associated objfile. */
9875 if (objfile->separate_debug_objfile_backlink)
9876 {
9877 struct dwarf2_per_objfile *dpo_backlink
ed2dc618 9878 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9a619af0 9879
442e4d9c
YQ
9880 dwarf2_per_objfile->has_section_at_zero
9881 = dpo_backlink->has_section_at_zero;
9882 }
b2ab525c 9883
442e4d9c 9884 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 9885
257e7a09 9886 psymtab_to_symtab_1 (self);
c906108c 9887
442e4d9c
YQ
9888 /* Finish up the debug error message. */
9889 if (info_verbose)
9890 printf_filtered (_("done.\n"));
c906108c 9891 }
95554aad 9892
ed2dc618 9893 process_cu_includes (dwarf2_per_objfile);
c906108c 9894}
9cdd5dbd
DE
9895\f
9896/* Reading in full CUs. */
c906108c 9897
10b3939b
DJ
9898/* Add PER_CU to the queue. */
9899
9900static void
95554aad
TT
9901queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9902 enum language pretend_language)
10b3939b
DJ
9903{
9904 struct dwarf2_queue_item *item;
9905
9906 per_cu->queued = 1;
8d749320 9907 item = XNEW (struct dwarf2_queue_item);
10b3939b 9908 item->per_cu = per_cu;
95554aad 9909 item->pretend_language = pretend_language;
10b3939b
DJ
9910 item->next = NULL;
9911
9912 if (dwarf2_queue == NULL)
9913 dwarf2_queue = item;
9914 else
9915 dwarf2_queue_tail->next = item;
9916
9917 dwarf2_queue_tail = item;
9918}
9919
89e63ee4
DE
9920/* If PER_CU is not yet queued, add it to the queue.
9921 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9922 dependency.
0907af0c 9923 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9924 meaning either PER_CU is already queued or it is already loaded.
9925
9926 N.B. There is an invariant here that if a CU is queued then it is loaded.
9927 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9928
9929static int
89e63ee4 9930maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9931 struct dwarf2_per_cu_data *per_cu,
9932 enum language pretend_language)
9933{
9934 /* We may arrive here during partial symbol reading, if we need full
9935 DIEs to process an unusual case (e.g. template arguments). Do
9936 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9937 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9938 {
9939 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9940 return 1;
9941 return 0;
9942 }
9943
9944 /* Mark the dependence relation so that we don't flush PER_CU
9945 too early. */
89e63ee4
DE
9946 if (dependent_cu != NULL)
9947 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9948
9949 /* If it's already on the queue, we have nothing to do. */
9950 if (per_cu->queued)
9951 return 0;
9952
9953 /* If the compilation unit is already loaded, just mark it as
9954 used. */
9955 if (per_cu->cu != NULL)
9956 {
9957 per_cu->cu->last_used = 0;
9958 return 0;
9959 }
9960
9961 /* Add it to the queue. */
9962 queue_comp_unit (per_cu, pretend_language);
9963
9964 return 1;
9965}
9966
10b3939b
DJ
9967/* Process the queue. */
9968
9969static void
ed2dc618 9970process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
9971{
9972 struct dwarf2_queue_item *item, *next_item;
9973
b4f54984 9974 if (dwarf_read_debug)
45cfd468
DE
9975 {
9976 fprintf_unfiltered (gdb_stdlog,
9977 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9978 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9979 }
9980
03dd20cc
DJ
9981 /* The queue starts out with one item, but following a DIE reference
9982 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9983 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9984 {
cc12ce38
DE
9985 if ((dwarf2_per_objfile->using_index
9986 ? !item->per_cu->v.quick->compunit_symtab
9987 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9988 /* Skip dummy CUs. */
9989 && item->per_cu->cu != NULL)
f4dc4d17
DE
9990 {
9991 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9992 unsigned int debug_print_threshold;
247f5c4f 9993 char buf[100];
f4dc4d17 9994
247f5c4f 9995 if (per_cu->is_debug_types)
f4dc4d17 9996 {
247f5c4f
DE
9997 struct signatured_type *sig_type =
9998 (struct signatured_type *) per_cu;
9999
9d8780f0 10000 sprintf (buf, "TU %s at offset %s",
73be47f5 10001 hex_string (sig_type->signature),
9d8780f0 10002 sect_offset_str (per_cu->sect_off));
73be47f5
DE
10003 /* There can be 100s of TUs.
10004 Only print them in verbose mode. */
10005 debug_print_threshold = 2;
f4dc4d17 10006 }
247f5c4f 10007 else
73be47f5 10008 {
9d8780f0
SM
10009 sprintf (buf, "CU at offset %s",
10010 sect_offset_str (per_cu->sect_off));
73be47f5
DE
10011 debug_print_threshold = 1;
10012 }
247f5c4f 10013
b4f54984 10014 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 10015 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
10016
10017 if (per_cu->is_debug_types)
10018 process_full_type_unit (per_cu, item->pretend_language);
10019 else
10020 process_full_comp_unit (per_cu, item->pretend_language);
10021
b4f54984 10022 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 10023 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 10024 }
10b3939b
DJ
10025
10026 item->per_cu->queued = 0;
10027 next_item = item->next;
10028 xfree (item);
10029 }
10030
10031 dwarf2_queue_tail = NULL;
45cfd468 10032
b4f54984 10033 if (dwarf_read_debug)
45cfd468
DE
10034 {
10035 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 10036 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 10037 }
10b3939b
DJ
10038}
10039
10b3939b
DJ
10040/* Read in full symbols for PST, and anything it depends on. */
10041
c906108c 10042static void
fba45db2 10043psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 10044{
10b3939b 10045 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
10046 int i;
10047
95554aad
TT
10048 if (pst->readin)
10049 return;
10050
aaa75496 10051 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
10052 if (!pst->dependencies[i]->readin
10053 && pst->dependencies[i]->user == NULL)
aaa75496
JB
10054 {
10055 /* Inform about additional files that need to be read in. */
10056 if (info_verbose)
10057 {
a3f17187 10058 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
10059 fputs_filtered (" ", gdb_stdout);
10060 wrap_here ("");
10061 fputs_filtered ("and ", gdb_stdout);
10062 wrap_here ("");
10063 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 10064 wrap_here (""); /* Flush output. */
aaa75496
JB
10065 gdb_flush (gdb_stdout);
10066 }
10067 psymtab_to_symtab_1 (pst->dependencies[i]);
10068 }
10069
9a3c8263 10070 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
10071
10072 if (per_cu == NULL)
aaa75496
JB
10073 {
10074 /* It's an include file, no symbols to read for it.
10075 Everything is in the parent symtab. */
10076 pst->readin = 1;
10077 return;
10078 }
c906108c 10079
a0f42c21 10080 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
10081}
10082
dee91e82
DE
10083/* Trivial hash function for die_info: the hash value of a DIE
10084 is its offset in .debug_info for this objfile. */
10b3939b 10085
dee91e82
DE
10086static hashval_t
10087die_hash (const void *item)
10b3939b 10088{
9a3c8263 10089 const struct die_info *die = (const struct die_info *) item;
6502dd73 10090
9c541725 10091 return to_underlying (die->sect_off);
dee91e82 10092}
63d06c5c 10093
dee91e82
DE
10094/* Trivial comparison function for die_info structures: two DIEs
10095 are equal if they have the same offset. */
98bfdba5 10096
dee91e82
DE
10097static int
10098die_eq (const void *item_lhs, const void *item_rhs)
10099{
9a3c8263
SM
10100 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
10101 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 10102
9c541725 10103 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 10104}
c906108c 10105
dee91e82
DE
10106/* die_reader_func for load_full_comp_unit.
10107 This is identical to read_signatured_type_reader,
10108 but is kept separate for now. */
c906108c 10109
dee91e82
DE
10110static void
10111load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 10112 const gdb_byte *info_ptr,
dee91e82
DE
10113 struct die_info *comp_unit_die,
10114 int has_children,
10115 void *data)
10116{
10117 struct dwarf2_cu *cu = reader->cu;
9a3c8263 10118 enum language *language_ptr = (enum language *) data;
6caca83c 10119
dee91e82
DE
10120 gdb_assert (cu->die_hash == NULL);
10121 cu->die_hash =
10122 htab_create_alloc_ex (cu->header.length / 12,
10123 die_hash,
10124 die_eq,
10125 NULL,
10126 &cu->comp_unit_obstack,
10127 hashtab_obstack_allocate,
10128 dummy_obstack_deallocate);
e142c38c 10129
dee91e82
DE
10130 if (has_children)
10131 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
10132 &info_ptr, comp_unit_die);
10133 cu->dies = comp_unit_die;
10134 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
10135
10136 /* We try not to read any attributes in this function, because not
9cdd5dbd 10137 all CUs needed for references have been loaded yet, and symbol
10b3939b 10138 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
10139 or we won't be able to build types correctly.
10140 Similarly, if we do not read the producer, we can not apply
10141 producer-specific interpretation. */
95554aad 10142 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 10143}
10b3939b 10144
dee91e82 10145/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 10146
dee91e82 10147static void
95554aad
TT
10148load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
10149 enum language pretend_language)
dee91e82 10150{
3019eac3 10151 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 10152
f4dc4d17
DE
10153 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
10154 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
10155}
10156
3da10d80
KS
10157/* Add a DIE to the delayed physname list. */
10158
10159static void
10160add_to_method_list (struct type *type, int fnfield_index, int index,
10161 const char *name, struct die_info *die,
10162 struct dwarf2_cu *cu)
10163{
10164 struct delayed_method_info mi;
10165 mi.type = type;
10166 mi.fnfield_index = fnfield_index;
10167 mi.index = index;
10168 mi.name = name;
10169 mi.die = die;
c89b44cd 10170 cu->method_list.push_back (mi);
3da10d80
KS
10171}
10172
3693fdb3
PA
10173/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
10174 "const" / "volatile". If so, decrements LEN by the length of the
10175 modifier and return true. Otherwise return false. */
10176
10177template<size_t N>
10178static bool
10179check_modifier (const char *physname, size_t &len, const char (&mod)[N])
10180{
10181 size_t mod_len = sizeof (mod) - 1;
10182 if (len > mod_len && startswith (physname + (len - mod_len), mod))
10183 {
10184 len -= mod_len;
10185 return true;
10186 }
10187 return false;
10188}
10189
3da10d80
KS
10190/* Compute the physnames of any methods on the CU's method list.
10191
10192 The computation of method physnames is delayed in order to avoid the
10193 (bad) condition that one of the method's formal parameters is of an as yet
10194 incomplete type. */
10195
10196static void
10197compute_delayed_physnames (struct dwarf2_cu *cu)
10198{
3693fdb3 10199 /* Only C++ delays computing physnames. */
c89b44cd 10200 if (cu->method_list.empty ())
3693fdb3
PA
10201 return;
10202 gdb_assert (cu->language == language_cplus);
10203
c89b44cd 10204 for (struct delayed_method_info &mi : cu->method_list)
3da10d80 10205 {
1d06ead6 10206 const char *physname;
3da10d80 10207 struct fn_fieldlist *fn_flp
c89b44cd
TT
10208 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
10209 physname = dwarf2_physname (mi.name, mi.die, cu);
10210 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 10211 = physname ? physname : "";
3693fdb3
PA
10212
10213 /* Since there's no tag to indicate whether a method is a
10214 const/volatile overload, extract that information out of the
10215 demangled name. */
10216 if (physname != NULL)
10217 {
10218 size_t len = strlen (physname);
10219
10220 while (1)
10221 {
10222 if (physname[len] == ')') /* shortcut */
10223 break;
10224 else if (check_modifier (physname, len, " const"))
c89b44cd 10225 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 10226 else if (check_modifier (physname, len, " volatile"))
c89b44cd 10227 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
10228 else
10229 break;
10230 }
10231 }
3da10d80 10232 }
c89b44cd
TT
10233
10234 /* The list is no longer needed. */
10235 cu->method_list.clear ();
3da10d80
KS
10236}
10237
a766d390
DE
10238/* Go objects should be embedded in a DW_TAG_module DIE,
10239 and it's not clear if/how imported objects will appear.
10240 To keep Go support simple until that's worked out,
10241 go back through what we've read and create something usable.
10242 We could do this while processing each DIE, and feels kinda cleaner,
10243 but that way is more invasive.
10244 This is to, for example, allow the user to type "p var" or "b main"
10245 without having to specify the package name, and allow lookups
10246 of module.object to work in contexts that use the expression
10247 parser. */
10248
10249static void
10250fixup_go_packaging (struct dwarf2_cu *cu)
10251{
10252 char *package_name = NULL;
10253 struct pending *list;
10254 int i;
10255
10256 for (list = global_symbols; list != NULL; list = list->next)
10257 {
10258 for (i = 0; i < list->nsyms; ++i)
10259 {
10260 struct symbol *sym = list->symbol[i];
10261
10262 if (SYMBOL_LANGUAGE (sym) == language_go
10263 && SYMBOL_CLASS (sym) == LOC_BLOCK)
10264 {
10265 char *this_package_name = go_symbol_package_name (sym);
10266
10267 if (this_package_name == NULL)
10268 continue;
10269 if (package_name == NULL)
10270 package_name = this_package_name;
10271 else
10272 {
518817b3
SM
10273 struct objfile *objfile
10274 = cu->per_cu->dwarf2_per_objfile->objfile;
a766d390
DE
10275 if (strcmp (package_name, this_package_name) != 0)
10276 complaint (&symfile_complaints,
10277 _("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
10278 (symbol_symtab (sym) != NULL
10279 ? symtab_to_filename_for_display
10280 (symbol_symtab (sym))
e3b94546 10281 : objfile_name (objfile)),
a766d390
DE
10282 this_package_name, package_name);
10283 xfree (this_package_name);
10284 }
10285 }
10286 }
10287 }
10288
10289 if (package_name != NULL)
10290 {
518817b3 10291 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 10292 const char *saved_package_name
224c3ddb
SM
10293 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
10294 package_name,
10295 strlen (package_name));
19f392bc
UW
10296 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
10297 saved_package_name);
a766d390
DE
10298 struct symbol *sym;
10299
10300 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10301
e623cf5d 10302 sym = allocate_symbol (objfile);
f85f34ed 10303 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
10304 SYMBOL_SET_NAMES (sym, saved_package_name,
10305 strlen (saved_package_name), 0, objfile);
a766d390
DE
10306 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
10307 e.g., "main" finds the "main" module and not C's main(). */
10308 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 10309 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
10310 SYMBOL_TYPE (sym) = type;
10311
10312 add_symbol_to_list (sym, &global_symbols);
10313
10314 xfree (package_name);
10315 }
10316}
10317
c9317f21
TT
10318/* Allocate a fully-qualified name consisting of the two parts on the
10319 obstack. */
10320
10321static const char *
10322rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
10323{
10324 return obconcat (obstack, p1, "::", p2, (char *) NULL);
10325}
10326
10327/* A helper that allocates a struct discriminant_info to attach to a
10328 union type. */
10329
10330static struct discriminant_info *
10331alloc_discriminant_info (struct type *type, int discriminant_index,
10332 int default_index)
10333{
10334 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
10335 gdb_assert (discriminant_index == -1
10336 || (discriminant_index >= 0
10337 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 10338 gdb_assert (default_index == -1
c7b15a66 10339 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
10340
10341 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
10342
10343 struct discriminant_info *disc
10344 = ((struct discriminant_info *)
10345 TYPE_ZALLOC (type,
10346 offsetof (struct discriminant_info, discriminants)
10347 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
10348 disc->default_index = default_index;
10349 disc->discriminant_index = discriminant_index;
10350
10351 struct dynamic_prop prop;
10352 prop.kind = PROP_UNDEFINED;
10353 prop.data.baton = disc;
10354
10355 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
10356
10357 return disc;
10358}
10359
10360/* Some versions of rustc emitted enums in an unusual way.
10361
10362 Ordinary enums were emitted as unions. The first element of each
10363 structure in the union was named "RUST$ENUM$DISR". This element
10364 held the discriminant.
10365
10366 These versions of Rust also implemented the "non-zero"
10367 optimization. When the enum had two values, and one is empty and
10368 the other holds a pointer that cannot be zero, the pointer is used
10369 as the discriminant, with a zero value meaning the empty variant.
10370 Here, the union's first member is of the form
10371 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
10372 where the fieldnos are the indices of the fields that should be
10373 traversed in order to find the field (which may be several fields deep)
10374 and the variantname is the name of the variant of the case when the
10375 field is zero.
10376
10377 This function recognizes whether TYPE is of one of these forms,
10378 and, if so, smashes it to be a variant type. */
10379
10380static void
10381quirk_rust_enum (struct type *type, struct objfile *objfile)
10382{
10383 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10384
10385 /* We don't need to deal with empty enums. */
10386 if (TYPE_NFIELDS (type) == 0)
10387 return;
10388
10389#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
10390 if (TYPE_NFIELDS (type) == 1
10391 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
10392 {
10393 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
10394
10395 /* Decode the field name to find the offset of the
10396 discriminant. */
10397 ULONGEST bit_offset = 0;
10398 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10399 while (name[0] >= '0' && name[0] <= '9')
10400 {
10401 char *tail;
10402 unsigned long index = strtoul (name, &tail, 10);
10403 name = tail;
10404 if (*name != '$'
10405 || index >= TYPE_NFIELDS (field_type)
10406 || (TYPE_FIELD_LOC_KIND (field_type, index)
10407 != FIELD_LOC_KIND_BITPOS))
10408 {
10409 complaint (&symfile_complaints,
10410 _("Could not parse Rust enum encoding string \"%s\""
10411 "[in module %s]"),
10412 TYPE_FIELD_NAME (type, 0),
10413 objfile_name (objfile));
10414 return;
10415 }
10416 ++name;
10417
10418 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10419 field_type = TYPE_FIELD_TYPE (field_type, index);
10420 }
10421
10422 /* Make a union to hold the variants. */
10423 struct type *union_type = alloc_type (objfile);
10424 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10425 TYPE_NFIELDS (union_type) = 3;
10426 TYPE_FIELDS (union_type)
10427 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10428 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10429
10430 /* Put the discriminant must at index 0. */
10431 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10432 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10433 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10434 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10435
10436 /* The order of fields doesn't really matter, so put the real
10437 field at index 1 and the data-less field at index 2. */
10438 struct discriminant_info *disc
10439 = alloc_discriminant_info (union_type, 0, 1);
10440 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10441 TYPE_FIELD_NAME (union_type, 1)
10442 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10443 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10444 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10445 TYPE_FIELD_NAME (union_type, 1));
10446
10447 const char *dataless_name
10448 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10449 name);
10450 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10451 dataless_name);
10452 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10453 /* NAME points into the original discriminant name, which
10454 already has the correct lifetime. */
10455 TYPE_FIELD_NAME (union_type, 2) = name;
10456 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10457 disc->discriminants[2] = 0;
10458
10459 /* Smash this type to be a structure type. We have to do this
10460 because the type has already been recorded. */
10461 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10462 TYPE_NFIELDS (type) = 1;
10463 TYPE_FIELDS (type)
10464 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10465
10466 /* Install the variant part. */
10467 TYPE_FIELD_TYPE (type, 0) = union_type;
10468 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10469 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10470 }
10471 else if (TYPE_NFIELDS (type) == 1)
10472 {
10473 /* We assume that a union with a single field is a univariant
10474 enum. */
10475 /* Smash this type to be a structure type. We have to do this
10476 because the type has already been recorded. */
10477 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10478
10479 /* Make a union to hold the variants. */
10480 struct type *union_type = alloc_type (objfile);
10481 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10482 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10483 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10484 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10485
10486 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10487 const char *variant_name
10488 = rust_last_path_segment (TYPE_NAME (field_type));
10489 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10490 TYPE_NAME (field_type)
10491 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 10492 TYPE_NAME (type), variant_name);
c9317f21
TT
10493
10494 /* Install the union in the outer struct type. */
10495 TYPE_NFIELDS (type) = 1;
10496 TYPE_FIELDS (type)
10497 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10498 TYPE_FIELD_TYPE (type, 0) = union_type;
10499 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10500 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10501
10502 alloc_discriminant_info (union_type, -1, 0);
10503 }
10504 else
10505 {
10506 struct type *disr_type = nullptr;
10507 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10508 {
10509 disr_type = TYPE_FIELD_TYPE (type, i);
10510
10511 if (TYPE_NFIELDS (disr_type) == 0)
10512 {
10513 /* Could be data-less variant, so keep going. */
10514 }
10515 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10516 "RUST$ENUM$DISR") != 0)
10517 {
10518 /* Not a Rust enum. */
10519 return;
10520 }
10521 else
10522 {
10523 /* Found one. */
10524 break;
10525 }
10526 }
10527
10528 /* If we got here without a discriminant, then it's probably
10529 just a union. */
10530 if (disr_type == nullptr)
10531 return;
10532
10533 /* Smash this type to be a structure type. We have to do this
10534 because the type has already been recorded. */
10535 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10536
10537 /* Make a union to hold the variants. */
10538 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10539 struct type *union_type = alloc_type (objfile);
10540 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10541 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10542 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10543 TYPE_FIELDS (union_type)
10544 = (struct field *) TYPE_ZALLOC (union_type,
10545 (TYPE_NFIELDS (union_type)
10546 * sizeof (struct field)));
10547
10548 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10549 TYPE_NFIELDS (type) * sizeof (struct field));
10550
10551 /* Install the discriminant at index 0 in the union. */
10552 TYPE_FIELD (union_type, 0) = *disr_field;
10553 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10554 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10555
10556 /* Install the union in the outer struct type. */
10557 TYPE_FIELD_TYPE (type, 0) = union_type;
10558 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10559 TYPE_NFIELDS (type) = 1;
10560
10561 /* Set the size and offset of the union type. */
10562 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10563
10564 /* We need a way to find the correct discriminant given a
10565 variant name. For convenience we build a map here. */
10566 struct type *enum_type = FIELD_TYPE (*disr_field);
10567 std::unordered_map<std::string, ULONGEST> discriminant_map;
10568 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10569 {
10570 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10571 {
10572 const char *name
10573 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10574 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10575 }
10576 }
10577
10578 int n_fields = TYPE_NFIELDS (union_type);
10579 struct discriminant_info *disc
10580 = alloc_discriminant_info (union_type, 0, -1);
10581 /* Skip the discriminant here. */
10582 for (int i = 1; i < n_fields; ++i)
10583 {
10584 /* Find the final word in the name of this variant's type.
10585 That name can be used to look up the correct
10586 discriminant. */
10587 const char *variant_name
10588 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10589 i)));
10590
10591 auto iter = discriminant_map.find (variant_name);
10592 if (iter != discriminant_map.end ())
10593 disc->discriminants[i] = iter->second;
10594
10595 /* Remove the discriminant field. */
10596 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10597 --TYPE_NFIELDS (sub_type);
10598 ++TYPE_FIELDS (sub_type);
10599 TYPE_FIELD_NAME (union_type, i) = variant_name;
10600 TYPE_NAME (sub_type)
10601 = rust_fully_qualify (&objfile->objfile_obstack,
10602 TYPE_NAME (type), variant_name);
10603 }
10604 }
10605}
10606
10607/* Rewrite some Rust unions to be structures with variants parts. */
10608
10609static void
10610rust_union_quirks (struct dwarf2_cu *cu)
10611{
10612 gdb_assert (cu->language == language_rust);
10613 for (struct type *type : cu->rust_unions)
10614 quirk_rust_enum (type, cu->per_cu->dwarf2_per_objfile->objfile);
10615}
10616
95554aad
TT
10617/* Return the symtab for PER_CU. This works properly regardless of
10618 whether we're using the index or psymtabs. */
10619
43f3e411
DE
10620static struct compunit_symtab *
10621get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 10622{
ed2dc618 10623 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
10624 ? per_cu->v.quick->compunit_symtab
10625 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10626}
10627
10628/* A helper function for computing the list of all symbol tables
10629 included by PER_CU. */
10630
10631static void
43f3e411 10632recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 10633 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10634 struct dwarf2_per_cu_data *per_cu,
43f3e411 10635 struct compunit_symtab *immediate_parent)
95554aad
TT
10636{
10637 void **slot;
10638 int ix;
43f3e411 10639 struct compunit_symtab *cust;
95554aad
TT
10640 struct dwarf2_per_cu_data *iter;
10641
10642 slot = htab_find_slot (all_children, per_cu, INSERT);
10643 if (*slot != NULL)
10644 {
10645 /* This inclusion and its children have been processed. */
10646 return;
10647 }
10648
10649 *slot = per_cu;
10650 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10651 cust = get_compunit_symtab (per_cu);
10652 if (cust != NULL)
ec94af83
DE
10653 {
10654 /* If this is a type unit only add its symbol table if we haven't
10655 seen it yet (type unit per_cu's can share symtabs). */
10656 if (per_cu->is_debug_types)
10657 {
43f3e411 10658 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10659 if (*slot == NULL)
10660 {
43f3e411
DE
10661 *slot = cust;
10662 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10663 if (cust->user == NULL)
10664 cust->user = immediate_parent;
ec94af83
DE
10665 }
10666 }
10667 else
f9125b6c 10668 {
43f3e411
DE
10669 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10670 if (cust->user == NULL)
10671 cust->user = immediate_parent;
f9125b6c 10672 }
ec94af83 10673 }
95554aad
TT
10674
10675 for (ix = 0;
796a7ff8 10676 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 10677 ++ix)
ec94af83
DE
10678 {
10679 recursively_compute_inclusions (result, all_children,
43f3e411 10680 all_type_symtabs, iter, cust);
ec94af83 10681 }
95554aad
TT
10682}
10683
43f3e411 10684/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10685 PER_CU. */
10686
10687static void
43f3e411 10688compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10689{
f4dc4d17
DE
10690 gdb_assert (! per_cu->is_debug_types);
10691
796a7ff8 10692 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
10693 {
10694 int ix, len;
ec94af83 10695 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
10696 struct compunit_symtab *compunit_symtab_iter;
10697 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 10698 htab_t all_children, all_type_symtabs;
43f3e411 10699 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10700
10701 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10702 if (cust == NULL)
95554aad
TT
10703 return;
10704
10705 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10706 NULL, xcalloc, xfree);
ec94af83
DE
10707 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10708 NULL, xcalloc, xfree);
95554aad
TT
10709
10710 for (ix = 0;
796a7ff8 10711 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 10712 ix, per_cu_iter);
95554aad 10713 ++ix)
ec94af83
DE
10714 {
10715 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 10716 all_type_symtabs, per_cu_iter,
43f3e411 10717 cust);
ec94af83 10718 }
95554aad 10719
ec94af83 10720 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
10721 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10722 cust->includes
ed2dc618 10723 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 10724 struct compunit_symtab *, len + 1);
95554aad 10725 for (ix = 0;
43f3e411
DE
10726 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10727 compunit_symtab_iter);
95554aad 10728 ++ix)
43f3e411
DE
10729 cust->includes[ix] = compunit_symtab_iter;
10730 cust->includes[len] = NULL;
95554aad 10731
43f3e411 10732 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 10733 htab_delete (all_children);
ec94af83 10734 htab_delete (all_type_symtabs);
95554aad
TT
10735 }
10736}
10737
10738/* Compute the 'includes' field for the symtabs of all the CUs we just
10739 read. */
10740
10741static void
ed2dc618 10742process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad
TT
10743{
10744 int ix;
10745 struct dwarf2_per_cu_data *iter;
10746
10747 for (ix = 0;
10748 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10749 ix, iter);
10750 ++ix)
f4dc4d17
DE
10751 {
10752 if (! iter->is_debug_types)
43f3e411 10753 compute_compunit_symtab_includes (iter);
f4dc4d17 10754 }
95554aad
TT
10755
10756 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10757}
10758
9cdd5dbd 10759/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10760 already been loaded into memory. */
10761
10762static void
95554aad
TT
10763process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10764 enum language pretend_language)
10b3939b 10765{
10b3939b 10766 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10767 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10768 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10769 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10770 CORE_ADDR lowpc, highpc;
43f3e411 10771 struct compunit_symtab *cust;
10b3939b 10772 CORE_ADDR baseaddr;
4359dff1 10773 struct block *static_block;
3e29f34a 10774 CORE_ADDR addr;
10b3939b
DJ
10775
10776 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10777
10b3939b 10778 buildsym_init ();
33c7c59d 10779 scoped_free_pendings free_pending;
c89b44cd
TT
10780
10781 /* Clear the list here in case something was left over. */
10782 cu->method_list.clear ();
10b3939b
DJ
10783
10784 cu->list_in_scope = &file_symbols;
c906108c 10785
95554aad
TT
10786 cu->language = pretend_language;
10787 cu->language_defn = language_def (cu->language);
10788
c906108c 10789 /* Do line number decoding in read_file_scope () */
10b3939b 10790 process_die (cu->dies, cu);
c906108c 10791
a766d390
DE
10792 /* For now fudge the Go package. */
10793 if (cu->language == language_go)
10794 fixup_go_packaging (cu);
10795
3da10d80
KS
10796 /* Now that we have processed all the DIEs in the CU, all the types
10797 should be complete, and it should now be safe to compute all of the
10798 physnames. */
10799 compute_delayed_physnames (cu);
3da10d80 10800
c9317f21
TT
10801 if (cu->language == language_rust)
10802 rust_union_quirks (cu);
10803
fae299cd
DC
10804 /* Some compilers don't define a DW_AT_high_pc attribute for the
10805 compilation unit. If the DW_AT_high_pc is missing, synthesize
10806 it, by scanning the DIE's below the compilation unit. */
10b3939b 10807 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10808
3e29f34a
MR
10809 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10810 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10811
10812 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10813 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10814 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10815 addrmap to help ensure it has an accurate map of pc values belonging to
10816 this comp unit. */
10817 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10818
43f3e411
DE
10819 cust = end_symtab_from_static_block (static_block,
10820 SECT_OFF_TEXT (objfile), 0);
c906108c 10821
43f3e411 10822 if (cust != NULL)
c906108c 10823 {
df15bd07 10824 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10825
8be455d7
JK
10826 /* Set symtab language to language from DW_AT_language. If the
10827 compilation is from a C file generated by language preprocessors, do
10828 not set the language if it was already deduced by start_subfile. */
43f3e411 10829 if (!(cu->language == language_c
40e3ad0e 10830 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10831 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10832
10833 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10834 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10835 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10836 there were bugs in prologue debug info, fixed later in GCC-4.5
10837 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10838
10839 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10840 needed, it would be wrong due to missing DW_AT_producer there.
10841
10842 Still one can confuse GDB by using non-standard GCC compilation
10843 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10844 */
ab260dad 10845 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10846 cust->locations_valid = 1;
e0d00bc7
JK
10847
10848 if (gcc_4_minor >= 5)
43f3e411 10849 cust->epilogue_unwind_valid = 1;
96408a79 10850
43f3e411 10851 cust->call_site_htab = cu->call_site_htab;
c906108c 10852 }
9291a0cd
TT
10853
10854 if (dwarf2_per_objfile->using_index)
43f3e411 10855 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10856 else
10857 {
10858 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10859 pst->compunit_symtab = cust;
9291a0cd
TT
10860 pst->readin = 1;
10861 }
c906108c 10862
95554aad
TT
10863 /* Push it for inclusion processing later. */
10864 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
f4dc4d17 10865}
45cfd468 10866
f4dc4d17
DE
10867/* Generate full symbol information for type unit PER_CU, whose DIEs have
10868 already been loaded into memory. */
10869
10870static void
10871process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10872 enum language pretend_language)
10873{
10874 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10875 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10876 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10877 struct compunit_symtab *cust;
0186c6a7
DE
10878 struct signatured_type *sig_type;
10879
10880 gdb_assert (per_cu->is_debug_types);
10881 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
10882
10883 buildsym_init ();
33c7c59d 10884 scoped_free_pendings free_pending;
c89b44cd
TT
10885
10886 /* Clear the list here in case something was left over. */
10887 cu->method_list.clear ();
f4dc4d17
DE
10888
10889 cu->list_in_scope = &file_symbols;
10890
10891 cu->language = pretend_language;
10892 cu->language_defn = language_def (cu->language);
10893
10894 /* The symbol tables are set up in read_type_unit_scope. */
10895 process_die (cu->dies, cu);
10896
10897 /* For now fudge the Go package. */
10898 if (cu->language == language_go)
10899 fixup_go_packaging (cu);
10900
10901 /* Now that we have processed all the DIEs in the CU, all the types
10902 should be complete, and it should now be safe to compute all of the
10903 physnames. */
10904 compute_delayed_physnames (cu);
f4dc4d17 10905
c9317f21
TT
10906 if (cu->language == language_rust)
10907 rust_union_quirks (cu);
10908
f4dc4d17
DE
10909 /* TUs share symbol tables.
10910 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10911 of it with end_expandable_symtab. Otherwise, complete the addition of
10912 this TU's symbols to the existing symtab. */
43f3e411 10913 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10914 {
43f3e411
DE
10915 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10916 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10917
43f3e411 10918 if (cust != NULL)
f4dc4d17
DE
10919 {
10920 /* Set symtab language to language from DW_AT_language. If the
10921 compilation is from a C file generated by language preprocessors,
10922 do not set the language if it was already deduced by
10923 start_subfile. */
43f3e411
DE
10924 if (!(cu->language == language_c
10925 && COMPUNIT_FILETABS (cust)->language != language_c))
10926 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10927 }
10928 }
10929 else
10930 {
0ab9ce85 10931 augment_type_symtab ();
43f3e411 10932 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10933 }
10934
10935 if (dwarf2_per_objfile->using_index)
43f3e411 10936 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10937 else
10938 {
10939 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10940 pst->compunit_symtab = cust;
f4dc4d17 10941 pst->readin = 1;
45cfd468 10942 }
c906108c
SS
10943}
10944
95554aad
TT
10945/* Process an imported unit DIE. */
10946
10947static void
10948process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10949{
10950 struct attribute *attr;
10951
f4dc4d17
DE
10952 /* For now we don't handle imported units in type units. */
10953 if (cu->per_cu->is_debug_types)
10954 {
10955 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10956 " supported in type units [in module %s]"),
518817b3 10957 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10958 }
10959
95554aad
TT
10960 attr = dwarf2_attr (die, DW_AT_import, cu);
10961 if (attr != NULL)
10962 {
9c541725
PA
10963 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10964 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10965 dwarf2_per_cu_data *per_cu
e3b94546 10966 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10967 cu->per_cu->dwarf2_per_objfile);
95554aad 10968
69d751e3 10969 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
10970 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10971 load_full_comp_unit (per_cu, cu->language);
10972
796a7ff8 10973 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
10974 per_cu);
10975 }
10976}
10977
4c8aa72d
PA
10978/* RAII object that represents a process_die scope: i.e.,
10979 starts/finishes processing a DIE. */
10980class process_die_scope
adde2bff 10981{
4c8aa72d
PA
10982public:
10983 process_die_scope (die_info *die, dwarf2_cu *cu)
10984 : m_die (die), m_cu (cu)
10985 {
10986 /* We should only be processing DIEs not already in process. */
10987 gdb_assert (!m_die->in_process);
10988 m_die->in_process = true;
10989 }
8c3cb9fa 10990
4c8aa72d
PA
10991 ~process_die_scope ()
10992 {
10993 m_die->in_process = false;
10994
10995 /* If we're done processing the DIE for the CU that owns the line
10996 header, we don't need the line header anymore. */
10997 if (m_cu->line_header_die_owner == m_die)
10998 {
10999 delete m_cu->line_header;
11000 m_cu->line_header = NULL;
11001 m_cu->line_header_die_owner = NULL;
11002 }
11003 }
11004
11005private:
11006 die_info *m_die;
11007 dwarf2_cu *m_cu;
11008};
adde2bff 11009
c906108c
SS
11010/* Process a die and its children. */
11011
11012static void
e7c27a73 11013process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11014{
4c8aa72d 11015 process_die_scope scope (die, cu);
adde2bff 11016
c906108c
SS
11017 switch (die->tag)
11018 {
11019 case DW_TAG_padding:
11020 break;
11021 case DW_TAG_compile_unit:
95554aad 11022 case DW_TAG_partial_unit:
e7c27a73 11023 read_file_scope (die, cu);
c906108c 11024 break;
348e048f
DE
11025 case DW_TAG_type_unit:
11026 read_type_unit_scope (die, cu);
11027 break;
c906108c 11028 case DW_TAG_subprogram:
c906108c 11029 case DW_TAG_inlined_subroutine:
edb3359d 11030 read_func_scope (die, cu);
c906108c
SS
11031 break;
11032 case DW_TAG_lexical_block:
14898363
L
11033 case DW_TAG_try_block:
11034 case DW_TAG_catch_block:
e7c27a73 11035 read_lexical_block_scope (die, cu);
c906108c 11036 break;
216f72a1 11037 case DW_TAG_call_site:
96408a79
SA
11038 case DW_TAG_GNU_call_site:
11039 read_call_site_scope (die, cu);
11040 break;
c906108c 11041 case DW_TAG_class_type:
680b30c7 11042 case DW_TAG_interface_type:
c906108c
SS
11043 case DW_TAG_structure_type:
11044 case DW_TAG_union_type:
134d01f1 11045 process_structure_scope (die, cu);
c906108c
SS
11046 break;
11047 case DW_TAG_enumeration_type:
134d01f1 11048 process_enumeration_scope (die, cu);
c906108c 11049 break;
134d01f1 11050
f792889a
DJ
11051 /* These dies have a type, but processing them does not create
11052 a symbol or recurse to process the children. Therefore we can
11053 read them on-demand through read_type_die. */
c906108c 11054 case DW_TAG_subroutine_type:
72019c9c 11055 case DW_TAG_set_type:
c906108c 11056 case DW_TAG_array_type:
c906108c 11057 case DW_TAG_pointer_type:
c906108c 11058 case DW_TAG_ptr_to_member_type:
c906108c 11059 case DW_TAG_reference_type:
4297a3f0 11060 case DW_TAG_rvalue_reference_type:
c906108c 11061 case DW_TAG_string_type:
c906108c 11062 break;
134d01f1 11063
c906108c 11064 case DW_TAG_base_type:
a02abb62 11065 case DW_TAG_subrange_type:
cb249c71 11066 case DW_TAG_typedef:
134d01f1
DJ
11067 /* Add a typedef symbol for the type definition, if it has a
11068 DW_AT_name. */
f792889a 11069 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 11070 break;
c906108c 11071 case DW_TAG_common_block:
e7c27a73 11072 read_common_block (die, cu);
c906108c
SS
11073 break;
11074 case DW_TAG_common_inclusion:
11075 break;
d9fa45fe 11076 case DW_TAG_namespace:
4d4ec4e5 11077 cu->processing_has_namespace_info = 1;
e7c27a73 11078 read_namespace (die, cu);
d9fa45fe 11079 break;
5d7cb8df 11080 case DW_TAG_module:
4d4ec4e5 11081 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
11082 read_module (die, cu);
11083 break;
d9fa45fe 11084 case DW_TAG_imported_declaration:
74921315
KS
11085 cu->processing_has_namespace_info = 1;
11086 if (read_namespace_alias (die, cu))
11087 break;
11088 /* The declaration is not a global namespace alias: fall through. */
d9fa45fe 11089 case DW_TAG_imported_module:
4d4ec4e5 11090 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
11091 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
11092 || cu->language != language_fortran))
11093 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
11094 dwarf_tag_name (die->tag));
11095 read_import_statement (die, cu);
d9fa45fe 11096 break;
95554aad
TT
11097
11098 case DW_TAG_imported_unit:
11099 process_imported_unit_die (die, cu);
11100 break;
11101
71a3c369
TT
11102 case DW_TAG_variable:
11103 read_variable (die, cu);
11104 break;
11105
c906108c 11106 default:
e7c27a73 11107 new_symbol (die, NULL, cu);
c906108c
SS
11108 break;
11109 }
11110}
ca69b9e6
DE
11111\f
11112/* DWARF name computation. */
c906108c 11113
94af9270
KS
11114/* A helper function for dwarf2_compute_name which determines whether DIE
11115 needs to have the name of the scope prepended to the name listed in the
11116 die. */
11117
11118static int
11119die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
11120{
1c809c68
TT
11121 struct attribute *attr;
11122
94af9270
KS
11123 switch (die->tag)
11124 {
11125 case DW_TAG_namespace:
11126 case DW_TAG_typedef:
11127 case DW_TAG_class_type:
11128 case DW_TAG_interface_type:
11129 case DW_TAG_structure_type:
11130 case DW_TAG_union_type:
11131 case DW_TAG_enumeration_type:
11132 case DW_TAG_enumerator:
11133 case DW_TAG_subprogram:
08a76f8a 11134 case DW_TAG_inlined_subroutine:
94af9270 11135 case DW_TAG_member:
74921315 11136 case DW_TAG_imported_declaration:
94af9270
KS
11137 return 1;
11138
11139 case DW_TAG_variable:
c2b0a229 11140 case DW_TAG_constant:
94af9270
KS
11141 /* We only need to prefix "globally" visible variables. These include
11142 any variable marked with DW_AT_external or any variable that
11143 lives in a namespace. [Variables in anonymous namespaces
11144 require prefixing, but they are not DW_AT_external.] */
11145
11146 if (dwarf2_attr (die, DW_AT_specification, cu))
11147 {
11148 struct dwarf2_cu *spec_cu = cu;
9a619af0 11149
94af9270
KS
11150 return die_needs_namespace (die_specification (die, &spec_cu),
11151 spec_cu);
11152 }
11153
1c809c68 11154 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
11155 if (attr == NULL && die->parent->tag != DW_TAG_namespace
11156 && die->parent->tag != DW_TAG_module)
1c809c68
TT
11157 return 0;
11158 /* A variable in a lexical block of some kind does not need a
11159 namespace, even though in C++ such variables may be external
11160 and have a mangled name. */
11161 if (die->parent->tag == DW_TAG_lexical_block
11162 || die->parent->tag == DW_TAG_try_block
1054b214
TT
11163 || die->parent->tag == DW_TAG_catch_block
11164 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
11165 return 0;
11166 return 1;
94af9270
KS
11167
11168 default:
11169 return 0;
11170 }
11171}
11172
73b9be8b
KS
11173/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
11174 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
11175 defined for the given DIE. */
11176
11177static struct attribute *
11178dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
11179{
11180 struct attribute *attr;
11181
11182 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
11183 if (attr == NULL)
11184 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
11185
11186 return attr;
11187}
11188
11189/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
11190 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
11191 defined for the given DIE. */
11192
11193static const char *
11194dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
11195{
11196 const char *linkage_name;
11197
11198 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
11199 if (linkage_name == NULL)
11200 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
11201
11202 return linkage_name;
11203}
11204
94af9270 11205/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 11206 compute the physname for the object, which include a method's:
9c37b5ae 11207 - formal parameters (C++),
a766d390 11208 - receiver type (Go),
a766d390
DE
11209
11210 The term "physname" is a bit confusing.
11211 For C++, for example, it is the demangled name.
11212 For Go, for example, it's the mangled name.
94af9270 11213
af6b7be1
JB
11214 For Ada, return the DIE's linkage name rather than the fully qualified
11215 name. PHYSNAME is ignored..
11216
94af9270
KS
11217 The result is allocated on the objfile_obstack and canonicalized. */
11218
11219static const char *
15d034d0
TT
11220dwarf2_compute_name (const char *name,
11221 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
11222 int physname)
11223{
518817b3 11224 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 11225
94af9270
KS
11226 if (name == NULL)
11227 name = dwarf2_name (die, cu);
11228
2ee7123e
DE
11229 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
11230 but otherwise compute it by typename_concat inside GDB.
11231 FIXME: Actually this is not really true, or at least not always true.
11232 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
5e2db402 11233 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
11234 will set the demangled name to the result of dwarf2_full_name, and it is
11235 the demangled name that GDB uses if it exists. */
f55ee35c
JK
11236 if (cu->language == language_ada
11237 || (cu->language == language_fortran && physname))
11238 {
11239 /* For Ada unit, we prefer the linkage name over the name, as
11240 the former contains the exported name, which the user expects
11241 to be able to reference. Ideally, we want the user to be able
11242 to reference this entity using either natural or linkage name,
11243 but we haven't started looking at this enhancement yet. */
73b9be8b 11244 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 11245
2ee7123e
DE
11246 if (linkage_name != NULL)
11247 return linkage_name;
f55ee35c
JK
11248 }
11249
94af9270
KS
11250 /* These are the only languages we know how to qualify names in. */
11251 if (name != NULL
9c37b5ae 11252 && (cu->language == language_cplus
c44af4eb
TT
11253 || cu->language == language_fortran || cu->language == language_d
11254 || cu->language == language_rust))
94af9270
KS
11255 {
11256 if (die_needs_namespace (die, cu))
11257 {
0d5cff50 11258 const char *prefix;
34a68019 11259 const char *canonical_name = NULL;
94af9270 11260
d7e74731
PA
11261 string_file buf;
11262
94af9270 11263 prefix = determine_prefix (die, cu);
94af9270
KS
11264 if (*prefix != '\0')
11265 {
f55ee35c
JK
11266 char *prefixed_name = typename_concat (NULL, prefix, name,
11267 physname, cu);
9a619af0 11268
d7e74731 11269 buf.puts (prefixed_name);
94af9270
KS
11270 xfree (prefixed_name);
11271 }
11272 else
d7e74731 11273 buf.puts (name);
94af9270 11274
98bfdba5
PA
11275 /* Template parameters may be specified in the DIE's DW_AT_name, or
11276 as children with DW_TAG_template_type_param or
11277 DW_TAG_value_type_param. If the latter, add them to the name
11278 here. If the name already has template parameters, then
11279 skip this step; some versions of GCC emit both, and
11280 it is more efficient to use the pre-computed name.
11281
11282 Something to keep in mind about this process: it is very
11283 unlikely, or in some cases downright impossible, to produce
11284 something that will match the mangled name of a function.
11285 If the definition of the function has the same debug info,
11286 we should be able to match up with it anyway. But fallbacks
11287 using the minimal symbol, for instance to find a method
11288 implemented in a stripped copy of libstdc++, will not work.
11289 If we do not have debug info for the definition, we will have to
11290 match them up some other way.
11291
11292 When we do name matching there is a related problem with function
11293 templates; two instantiated function templates are allowed to
11294 differ only by their return types, which we do not add here. */
11295
11296 if (cu->language == language_cplus && strchr (name, '<') == NULL)
11297 {
11298 struct attribute *attr;
11299 struct die_info *child;
11300 int first = 1;
11301
11302 die->building_fullname = 1;
11303
11304 for (child = die->child; child != NULL; child = child->sibling)
11305 {
11306 struct type *type;
12df843f 11307 LONGEST value;
d521ce57 11308 const gdb_byte *bytes;
98bfdba5
PA
11309 struct dwarf2_locexpr_baton *baton;
11310 struct value *v;
11311
11312 if (child->tag != DW_TAG_template_type_param
11313 && child->tag != DW_TAG_template_value_param)
11314 continue;
11315
11316 if (first)
11317 {
d7e74731 11318 buf.puts ("<");
98bfdba5
PA
11319 first = 0;
11320 }
11321 else
d7e74731 11322 buf.puts (", ");
98bfdba5
PA
11323
11324 attr = dwarf2_attr (child, DW_AT_type, cu);
11325 if (attr == NULL)
11326 {
11327 complaint (&symfile_complaints,
11328 _("template parameter missing DW_AT_type"));
d7e74731 11329 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
11330 continue;
11331 }
11332 type = die_type (child, cu);
11333
11334 if (child->tag == DW_TAG_template_type_param)
11335 {
d7e74731 11336 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
11337 continue;
11338 }
11339
11340 attr = dwarf2_attr (child, DW_AT_const_value, cu);
11341 if (attr == NULL)
11342 {
11343 complaint (&symfile_complaints,
3e43a32a
MS
11344 _("template parameter missing "
11345 "DW_AT_const_value"));
d7e74731 11346 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
11347 continue;
11348 }
11349
11350 dwarf2_const_value_attr (attr, type, name,
11351 &cu->comp_unit_obstack, cu,
11352 &value, &bytes, &baton);
11353
11354 if (TYPE_NOSIGN (type))
11355 /* GDB prints characters as NUMBER 'CHAR'. If that's
11356 changed, this can use value_print instead. */
d7e74731 11357 c_printchar (value, type, &buf);
98bfdba5
PA
11358 else
11359 {
11360 struct value_print_options opts;
11361
11362 if (baton != NULL)
11363 v = dwarf2_evaluate_loc_desc (type, NULL,
11364 baton->data,
11365 baton->size,
11366 baton->per_cu);
11367 else if (bytes != NULL)
11368 {
11369 v = allocate_value (type);
11370 memcpy (value_contents_writeable (v), bytes,
11371 TYPE_LENGTH (type));
11372 }
11373 else
11374 v = value_from_longest (type, value);
11375
3e43a32a
MS
11376 /* Specify decimal so that we do not depend on
11377 the radix. */
98bfdba5
PA
11378 get_formatted_print_options (&opts, 'd');
11379 opts.raw = 1;
d7e74731 11380 value_print (v, &buf, &opts);
98bfdba5
PA
11381 release_value (v);
11382 value_free (v);
11383 }
11384 }
11385
11386 die->building_fullname = 0;
11387
11388 if (!first)
11389 {
11390 /* Close the argument list, with a space if necessary
11391 (nested templates). */
d7e74731
PA
11392 if (!buf.empty () && buf.string ().back () == '>')
11393 buf.puts (" >");
98bfdba5 11394 else
d7e74731 11395 buf.puts (">");
98bfdba5
PA
11396 }
11397 }
11398
9c37b5ae 11399 /* For C++ methods, append formal parameter type
94af9270 11400 information, if PHYSNAME. */
6e70227d 11401
94af9270 11402 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 11403 && cu->language == language_cplus)
94af9270
KS
11404 {
11405 struct type *type = read_type_die (die, cu);
11406
d7e74731 11407 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 11408 &type_print_raw_options);
94af9270 11409
9c37b5ae 11410 if (cu->language == language_cplus)
94af9270 11411 {
60430eff
DJ
11412 /* Assume that an artificial first parameter is
11413 "this", but do not crash if it is not. RealView
11414 marks unnamed (and thus unused) parameters as
11415 artificial; there is no way to differentiate
11416 the two cases. */
94af9270
KS
11417 if (TYPE_NFIELDS (type) > 0
11418 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 11419 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
11420 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11421 0))))
d7e74731 11422 buf.puts (" const");
94af9270
KS
11423 }
11424 }
11425
d7e74731 11426 const std::string &intermediate_name = buf.string ();
94af9270
KS
11427
11428 if (cu->language == language_cplus)
34a68019 11429 canonical_name
322a8516 11430 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
11431 &objfile->per_bfd->storage_obstack);
11432
11433 /* If we only computed INTERMEDIATE_NAME, or if
11434 INTERMEDIATE_NAME is already canonical, then we need to
11435 copy it to the appropriate obstack. */
322a8516 11436 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
11437 name = ((const char *)
11438 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
11439 intermediate_name.c_str (),
11440 intermediate_name.length ()));
34a68019
TT
11441 else
11442 name = canonical_name;
94af9270
KS
11443 }
11444 }
11445
11446 return name;
11447}
11448
0114d602
DJ
11449/* Return the fully qualified name of DIE, based on its DW_AT_name.
11450 If scope qualifiers are appropriate they will be added. The result
34a68019 11451 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
11452 not have a name. NAME may either be from a previous call to
11453 dwarf2_name or NULL.
11454
9c37b5ae 11455 The output string will be canonicalized (if C++). */
0114d602
DJ
11456
11457static const char *
15d034d0 11458dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 11459{
94af9270
KS
11460 return dwarf2_compute_name (name, die, cu, 0);
11461}
0114d602 11462
94af9270
KS
11463/* Construct a physname for the given DIE in CU. NAME may either be
11464 from a previous call to dwarf2_name or NULL. The result will be
11465 allocated on the objfile_objstack or NULL if the DIE does not have a
11466 name.
0114d602 11467
9c37b5ae 11468 The output string will be canonicalized (if C++). */
0114d602 11469
94af9270 11470static const char *
15d034d0 11471dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 11472{
518817b3 11473 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 11474 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
11475 int need_copy = 1;
11476
11477 /* In this case dwarf2_compute_name is just a shortcut not building anything
11478 on its own. */
11479 if (!die_needs_namespace (die, cu))
11480 return dwarf2_compute_name (name, die, cu, 1);
11481
73b9be8b 11482 mangled = dw2_linkage_name (die, cu);
900e11f9 11483
e98c9e7c
TT
11484 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11485 See https://github.com/rust-lang/rust/issues/32925. */
11486 if (cu->language == language_rust && mangled != NULL
11487 && strchr (mangled, '{') != NULL)
11488 mangled = NULL;
11489
900e11f9
JK
11490 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11491 has computed. */
791afaa2 11492 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 11493 if (mangled != NULL)
900e11f9 11494 {
900e11f9 11495
a766d390
DE
11496 if (cu->language == language_go)
11497 {
5e2db402
TT
11498 /* This is a lie, but we already lie to the caller new_symbol.
11499 new_symbol assumes we return the mangled name.
a766d390 11500 This just undoes that lie until things are cleaned up. */
a766d390
DE
11501 }
11502 else
11503 {
0eb876f5
JB
11504 /* Use DMGL_RET_DROP for C++ template functions to suppress
11505 their return type. It is easier for GDB users to search
11506 for such functions as `name(params)' than `long name(params)'.
11507 In such case the minimal symbol names do not match the full
11508 symbol names but for template functions there is never a need
11509 to look up their definition from their declaration so
11510 the only disadvantage remains the minimal symbol variant
11511 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
11512 demangled.reset (gdb_demangle (mangled,
11513 (DMGL_PARAMS | DMGL_ANSI
11514 | DMGL_RET_DROP)));
a766d390 11515 }
900e11f9 11516 if (demangled)
791afaa2 11517 canon = demangled.get ();
900e11f9
JK
11518 else
11519 {
11520 canon = mangled;
11521 need_copy = 0;
11522 }
11523 }
11524
11525 if (canon == NULL || check_physname)
11526 {
11527 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11528
11529 if (canon != NULL && strcmp (physname, canon) != 0)
11530 {
11531 /* It may not mean a bug in GDB. The compiler could also
11532 compute DW_AT_linkage_name incorrectly. But in such case
11533 GDB would need to be bug-to-bug compatible. */
11534
11535 complaint (&symfile_complaints,
11536 _("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
11537 "(from linkage <%s>) - DIE at %s [in module %s]"),
11538 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 11539 objfile_name (objfile));
900e11f9
JK
11540
11541 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11542 is available here - over computed PHYSNAME. It is safer
11543 against both buggy GDB and buggy compilers. */
11544
11545 retval = canon;
11546 }
11547 else
11548 {
11549 retval = physname;
11550 need_copy = 0;
11551 }
11552 }
11553 else
11554 retval = canon;
11555
11556 if (need_copy)
224c3ddb
SM
11557 retval = ((const char *)
11558 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11559 retval, strlen (retval)));
900e11f9 11560
900e11f9 11561 return retval;
0114d602
DJ
11562}
11563
74921315
KS
11564/* Inspect DIE in CU for a namespace alias. If one exists, record
11565 a new symbol for it.
11566
11567 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11568
11569static int
11570read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11571{
11572 struct attribute *attr;
11573
11574 /* If the die does not have a name, this is not a namespace
11575 alias. */
11576 attr = dwarf2_attr (die, DW_AT_name, cu);
11577 if (attr != NULL)
11578 {
11579 int num;
11580 struct die_info *d = die;
11581 struct dwarf2_cu *imported_cu = cu;
11582
11583 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11584 keep inspecting DIEs until we hit the underlying import. */
11585#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11586 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11587 {
11588 attr = dwarf2_attr (d, DW_AT_import, cu);
11589 if (attr == NULL)
11590 break;
11591
11592 d = follow_die_ref (d, attr, &imported_cu);
11593 if (d->tag != DW_TAG_imported_declaration)
11594 break;
11595 }
11596
11597 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11598 {
11599 complaint (&symfile_complaints,
9d8780f0
SM
11600 _("DIE at %s has too many recursively imported "
11601 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
11602 return 0;
11603 }
11604
11605 if (attr != NULL)
11606 {
11607 struct type *type;
9c541725 11608 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11609
9c541725 11610 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11611 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11612 {
11613 /* This declaration is a global namespace alias. Add
11614 a symbol for it whose type is the aliased namespace. */
11615 new_symbol (die, type, cu);
11616 return 1;
11617 }
11618 }
11619 }
11620
11621 return 0;
11622}
11623
22cee43f
PMR
11624/* Return the using directives repository (global or local?) to use in the
11625 current context for LANGUAGE.
11626
11627 For Ada, imported declarations can materialize renamings, which *may* be
11628 global. However it is impossible (for now?) in DWARF to distinguish
11629 "external" imported declarations and "static" ones. As all imported
11630 declarations seem to be static in all other languages, make them all CU-wide
11631 global only in Ada. */
11632
11633static struct using_direct **
11634using_directives (enum language language)
11635{
11636 if (language == language_ada && context_stack_depth == 0)
11637 return &global_using_directives;
11638 else
11639 return &local_using_directives;
11640}
11641
27aa8d6a
SW
11642/* Read the import statement specified by the given die and record it. */
11643
11644static void
11645read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11646{
518817b3 11647 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 11648 struct attribute *import_attr;
32019081 11649 struct die_info *imported_die, *child_die;
de4affc9 11650 struct dwarf2_cu *imported_cu;
27aa8d6a 11651 const char *imported_name;
794684b6 11652 const char *imported_name_prefix;
13387711
SW
11653 const char *canonical_name;
11654 const char *import_alias;
11655 const char *imported_declaration = NULL;
794684b6 11656 const char *import_prefix;
eb1e02fd 11657 std::vector<const char *> excludes;
13387711 11658
27aa8d6a
SW
11659 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11660 if (import_attr == NULL)
11661 {
11662 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11663 dwarf_tag_name (die->tag));
11664 return;
11665 }
11666
de4affc9
CC
11667 imported_cu = cu;
11668 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11669 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11670 if (imported_name == NULL)
11671 {
11672 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11673
11674 The import in the following code:
11675 namespace A
11676 {
11677 typedef int B;
11678 }
11679
11680 int main ()
11681 {
11682 using A::B;
11683 B b;
11684 return b;
11685 }
11686
11687 ...
11688 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11689 <52> DW_AT_decl_file : 1
11690 <53> DW_AT_decl_line : 6
11691 <54> DW_AT_import : <0x75>
11692 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11693 <59> DW_AT_name : B
11694 <5b> DW_AT_decl_file : 1
11695 <5c> DW_AT_decl_line : 2
11696 <5d> DW_AT_type : <0x6e>
11697 ...
11698 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11699 <76> DW_AT_byte_size : 4
11700 <77> DW_AT_encoding : 5 (signed)
11701
11702 imports the wrong die ( 0x75 instead of 0x58 ).
11703 This case will be ignored until the gcc bug is fixed. */
11704 return;
11705 }
11706
82856980
SW
11707 /* Figure out the local name after import. */
11708 import_alias = dwarf2_name (die, cu);
27aa8d6a 11709
794684b6
SW
11710 /* Figure out where the statement is being imported to. */
11711 import_prefix = determine_prefix (die, cu);
11712
11713 /* Figure out what the scope of the imported die is and prepend it
11714 to the name of the imported die. */
de4affc9 11715 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11716
f55ee35c
JK
11717 if (imported_die->tag != DW_TAG_namespace
11718 && imported_die->tag != DW_TAG_module)
794684b6 11719 {
13387711
SW
11720 imported_declaration = imported_name;
11721 canonical_name = imported_name_prefix;
794684b6 11722 }
13387711 11723 else if (strlen (imported_name_prefix) > 0)
12aaed36 11724 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11725 imported_name_prefix,
11726 (cu->language == language_d ? "." : "::"),
11727 imported_name, (char *) NULL);
13387711
SW
11728 else
11729 canonical_name = imported_name;
794684b6 11730
32019081
JK
11731 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11732 for (child_die = die->child; child_die && child_die->tag;
11733 child_die = sibling_die (child_die))
11734 {
11735 /* DWARF-4: A Fortran use statement with a “rename list” may be
11736 represented by an imported module entry with an import attribute
11737 referring to the module and owned entries corresponding to those
11738 entities that are renamed as part of being imported. */
11739
11740 if (child_die->tag != DW_TAG_imported_declaration)
11741 {
11742 complaint (&symfile_complaints,
11743 _("child DW_TAG_imported_declaration expected "
9d8780f0
SM
11744 "- DIE at %s [in module %s]"),
11745 sect_offset_str (child_die->sect_off),
11746 objfile_name (objfile));
32019081
JK
11747 continue;
11748 }
11749
11750 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11751 if (import_attr == NULL)
11752 {
11753 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11754 dwarf_tag_name (child_die->tag));
11755 continue;
11756 }
11757
11758 imported_cu = cu;
11759 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11760 &imported_cu);
11761 imported_name = dwarf2_name (imported_die, imported_cu);
11762 if (imported_name == NULL)
11763 {
11764 complaint (&symfile_complaints,
11765 _("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11766 "imported name - DIE at %s [in module %s]"),
11767 sect_offset_str (child_die->sect_off),
11768 objfile_name (objfile));
32019081
JK
11769 continue;
11770 }
11771
eb1e02fd 11772 excludes.push_back (imported_name);
32019081
JK
11773
11774 process_die (child_die, cu);
11775 }
11776
22cee43f
PMR
11777 add_using_directive (using_directives (cu->language),
11778 import_prefix,
11779 canonical_name,
11780 import_alias,
11781 imported_declaration,
11782 excludes,
11783 0,
11784 &objfile->objfile_obstack);
27aa8d6a
SW
11785}
11786
5230b05a
WT
11787/* ICC<14 does not output the required DW_AT_declaration on incomplete
11788 types, but gives them a size of zero. Starting with version 14,
11789 ICC is compatible with GCC. */
11790
11791static int
11792producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11793{
11794 if (!cu->checked_producer)
11795 check_producer (cu);
11796
11797 return cu->producer_is_icc_lt_14;
11798}
11799
1b80a9fa
JK
11800/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11801 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11802 this, it was first present in GCC release 4.3.0. */
11803
11804static int
11805producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11806{
11807 if (!cu->checked_producer)
11808 check_producer (cu);
11809
11810 return cu->producer_is_gcc_lt_4_3;
11811}
11812
d721ba37
PA
11813static file_and_directory
11814find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11815{
d721ba37
PA
11816 file_and_directory res;
11817
9291a0cd
TT
11818 /* Find the filename. Do not use dwarf2_name here, since the filename
11819 is not a source language identifier. */
d721ba37
PA
11820 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11821 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11822
d721ba37
PA
11823 if (res.comp_dir == NULL
11824 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11825 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11826 {
d721ba37
PA
11827 res.comp_dir_storage = ldirname (res.name);
11828 if (!res.comp_dir_storage.empty ())
11829 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11830 }
d721ba37 11831 if (res.comp_dir != NULL)
9291a0cd
TT
11832 {
11833 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11834 directory, get rid of it. */
d721ba37 11835 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11836
d721ba37
PA
11837 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11838 res.comp_dir = cp + 1;
9291a0cd
TT
11839 }
11840
d721ba37
PA
11841 if (res.name == NULL)
11842 res.name = "<unknown>";
11843
11844 return res;
9291a0cd
TT
11845}
11846
f4dc4d17
DE
11847/* Handle DW_AT_stmt_list for a compilation unit.
11848 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11849 COMP_DIR is the compilation directory. LOWPC is passed to
11850 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11851
11852static void
11853handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11854 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11855{
518817b3
SM
11856 struct dwarf2_per_objfile *dwarf2_per_objfile
11857 = cu->per_cu->dwarf2_per_objfile;
527f3840 11858 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11859 struct attribute *attr;
527f3840
JK
11860 struct line_header line_header_local;
11861 hashval_t line_header_local_hash;
527f3840
JK
11862 void **slot;
11863 int decode_mapping;
2ab95328 11864
f4dc4d17
DE
11865 gdb_assert (! cu->per_cu->is_debug_types);
11866
2ab95328 11867 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11868 if (attr == NULL)
11869 return;
11870
9c541725 11871 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11872
11873 /* The line header hash table is only created if needed (it exists to
11874 prevent redundant reading of the line table for partial_units).
11875 If we're given a partial_unit, we'll need it. If we're given a
11876 compile_unit, then use the line header hash table if it's already
11877 created, but don't create one just yet. */
11878
11879 if (dwarf2_per_objfile->line_header_hash == NULL
11880 && die->tag == DW_TAG_partial_unit)
2ab95328 11881 {
527f3840
JK
11882 dwarf2_per_objfile->line_header_hash
11883 = htab_create_alloc_ex (127, line_header_hash_voidp,
11884 line_header_eq_voidp,
11885 free_line_header_voidp,
11886 &objfile->objfile_obstack,
11887 hashtab_obstack_allocate,
11888 dummy_obstack_deallocate);
11889 }
2ab95328 11890
9c541725 11891 line_header_local.sect_off = line_offset;
527f3840
JK
11892 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11893 line_header_local_hash = line_header_hash (&line_header_local);
11894 if (dwarf2_per_objfile->line_header_hash != NULL)
11895 {
11896 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11897 &line_header_local,
11898 line_header_local_hash, NO_INSERT);
11899
11900 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11901 is not present in *SLOT (since if there is something in *SLOT then
11902 it will be for a partial_unit). */
11903 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11904 {
527f3840 11905 gdb_assert (*slot != NULL);
9a3c8263 11906 cu->line_header = (struct line_header *) *slot;
527f3840 11907 return;
dee91e82 11908 }
2ab95328 11909 }
527f3840
JK
11910
11911 /* dwarf_decode_line_header does not yet provide sufficient information.
11912 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11913 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11914 if (lh == NULL)
527f3840 11915 return;
4c8aa72d
PA
11916
11917 cu->line_header = lh.release ();
11918 cu->line_header_die_owner = die;
527f3840
JK
11919
11920 if (dwarf2_per_objfile->line_header_hash == NULL)
11921 slot = NULL;
11922 else
11923 {
11924 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11925 &line_header_local,
11926 line_header_local_hash, INSERT);
11927 gdb_assert (slot != NULL);
11928 }
11929 if (slot != NULL && *slot == NULL)
11930 {
11931 /* This newly decoded line number information unit will be owned
11932 by line_header_hash hash table. */
11933 *slot = cu->line_header;
4c8aa72d 11934 cu->line_header_die_owner = NULL;
527f3840
JK
11935 }
11936 else
11937 {
11938 /* We cannot free any current entry in (*slot) as that struct line_header
11939 may be already used by multiple CUs. Create only temporary decoded
11940 line_header for this CU - it may happen at most once for each line
11941 number information unit. And if we're not using line_header_hash
11942 then this is what we want as well. */
11943 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11944 }
11945 decode_mapping = (die->tag != DW_TAG_partial_unit);
11946 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11947 decode_mapping);
fff8551c 11948
2ab95328
TT
11949}
11950
95554aad 11951/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11952
c906108c 11953static void
e7c27a73 11954read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11955{
518817b3
SM
11956 struct dwarf2_per_objfile *dwarf2_per_objfile
11957 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11958 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11959 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11960 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11961 CORE_ADDR highpc = ((CORE_ADDR) 0);
11962 struct attribute *attr;
c906108c 11963 struct die_info *child_die;
e142c38c 11964 CORE_ADDR baseaddr;
6e70227d 11965
e142c38c 11966 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11967
fae299cd 11968 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11969
11970 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11971 from finish_block. */
2acceee2 11972 if (lowpc == ((CORE_ADDR) -1))
c906108c 11973 lowpc = highpc;
3e29f34a 11974 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11975
d721ba37 11976 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11977
95554aad 11978 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 11979
f4b8a18d
KW
11980 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11981 standardised yet. As a workaround for the language detection we fall
11982 back to the DW_AT_producer string. */
11983 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11984 cu->language = language_opencl;
11985
3019eac3
DE
11986 /* Similar hack for Go. */
11987 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11988 set_cu_language (DW_LANG_Go, cu);
11989
d721ba37 11990 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11991
11992 /* Decode line number information if present. We do this before
11993 processing child DIEs, so that the line header table is available
11994 for DW_AT_decl_file. */
d721ba37 11995 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11996
11997 /* Process all dies in compilation unit. */
11998 if (die->child != NULL)
11999 {
12000 child_die = die->child;
12001 while (child_die && child_die->tag)
12002 {
12003 process_die (child_die, cu);
12004 child_die = sibling_die (child_die);
12005 }
12006 }
12007
12008 /* Decode macro information, if present. Dwarf 2 macro information
12009 refers to information in the line number info statement program
12010 header, so we can only read it if we've read the header
12011 successfully. */
0af92d60
JK
12012 attr = dwarf2_attr (die, DW_AT_macros, cu);
12013 if (attr == NULL)
12014 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
12015 if (attr && cu->line_header)
12016 {
12017 if (dwarf2_attr (die, DW_AT_macro_info, cu))
12018 complaint (&symfile_complaints,
0af92d60 12019 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 12020
43f3e411 12021 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
12022 }
12023 else
12024 {
12025 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
12026 if (attr && cu->line_header)
12027 {
12028 unsigned int macro_offset = DW_UNSND (attr);
12029
43f3e411 12030 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
12031 }
12032 }
3019eac3
DE
12033}
12034
f4dc4d17
DE
12035/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
12036 Create the set of symtabs used by this TU, or if this TU is sharing
12037 symtabs with another TU and the symtabs have already been created
12038 then restore those symtabs in the line header.
12039 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
12040
12041static void
f4dc4d17 12042setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 12043{
f4dc4d17
DE
12044 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
12045 struct type_unit_group *tu_group;
12046 int first_time;
3019eac3 12047 struct attribute *attr;
9c541725 12048 unsigned int i;
0186c6a7 12049 struct signatured_type *sig_type;
3019eac3 12050
f4dc4d17 12051 gdb_assert (per_cu->is_debug_types);
0186c6a7 12052 sig_type = (struct signatured_type *) per_cu;
3019eac3 12053
f4dc4d17 12054 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 12055
f4dc4d17 12056 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 12057 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
12058 if (sig_type->type_unit_group == NULL)
12059 sig_type->type_unit_group = get_type_unit_group (cu, attr);
12060 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
12061
12062 /* If we've already processed this stmt_list there's no real need to
12063 do it again, we could fake it and just recreate the part we need
12064 (file name,index -> symtab mapping). If data shows this optimization
12065 is useful we can do it then. */
43f3e411 12066 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
12067
12068 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
12069 debug info. */
fff8551c 12070 line_header_up lh;
f4dc4d17 12071 if (attr != NULL)
3019eac3 12072 {
9c541725 12073 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
f4dc4d17
DE
12074 lh = dwarf_decode_line_header (line_offset, cu);
12075 }
12076 if (lh == NULL)
12077 {
12078 if (first_time)
12079 dwarf2_start_symtab (cu, "", NULL, 0);
12080 else
12081 {
12082 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 12083 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 12084 }
f4dc4d17 12085 return;
3019eac3
DE
12086 }
12087
4c8aa72d
PA
12088 cu->line_header = lh.release ();
12089 cu->line_header_die_owner = die;
3019eac3 12090
f4dc4d17
DE
12091 if (first_time)
12092 {
43f3e411 12093 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 12094
1fd60fc0
DE
12095 /* Note: We don't assign tu_group->compunit_symtab yet because we're
12096 still initializing it, and our caller (a few levels up)
12097 process_full_type_unit still needs to know if this is the first
12098 time. */
12099
4c8aa72d
PA
12100 tu_group->num_symtabs = cu->line_header->file_names.size ();
12101 tu_group->symtabs = XNEWVEC (struct symtab *,
12102 cu->line_header->file_names.size ());
3019eac3 12103
4c8aa72d 12104 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 12105 {
4c8aa72d 12106 file_entry &fe = cu->line_header->file_names[i];
3019eac3 12107
4c8aa72d 12108 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
3019eac3 12109
f4dc4d17
DE
12110 if (current_subfile->symtab == NULL)
12111 {
4c8aa72d
PA
12112 /* NOTE: start_subfile will recognize when it's been
12113 passed a file it has already seen. So we can't
12114 assume there's a simple mapping from
12115 cu->line_header->file_names to subfiles, plus
12116 cu->line_header->file_names may contain dups. */
43f3e411
DE
12117 current_subfile->symtab
12118 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
12119 }
12120
8c43009f
PA
12121 fe.symtab = current_subfile->symtab;
12122 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
12123 }
12124 }
12125 else
3019eac3 12126 {
0ab9ce85 12127 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 12128
4c8aa72d 12129 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 12130 {
4c8aa72d 12131 file_entry &fe = cu->line_header->file_names[i];
f4dc4d17 12132
4c8aa72d 12133 fe.symtab = tu_group->symtabs[i];
f4dc4d17 12134 }
3019eac3
DE
12135 }
12136
f4dc4d17
DE
12137 /* The main symtab is allocated last. Type units don't have DW_AT_name
12138 so they don't have a "real" (so to speak) symtab anyway.
12139 There is later code that will assign the main symtab to all symbols
12140 that don't have one. We need to handle the case of a symbol with a
12141 missing symtab (DW_AT_decl_file) anyway. */
12142}
3019eac3 12143
f4dc4d17
DE
12144/* Process DW_TAG_type_unit.
12145 For TUs we want to skip the first top level sibling if it's not the
12146 actual type being defined by this TU. In this case the first top
12147 level sibling is there to provide context only. */
3019eac3 12148
f4dc4d17
DE
12149static void
12150read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
12151{
12152 struct die_info *child_die;
3019eac3 12153
f4dc4d17
DE
12154 prepare_one_comp_unit (cu, die, language_minimal);
12155
12156 /* Initialize (or reinitialize) the machinery for building symtabs.
12157 We do this before processing child DIEs, so that the line header table
12158 is available for DW_AT_decl_file. */
12159 setup_type_unit_groups (die, cu);
12160
12161 if (die->child != NULL)
12162 {
12163 child_die = die->child;
12164 while (child_die && child_die->tag)
12165 {
12166 process_die (child_die, cu);
12167 child_die = sibling_die (child_die);
12168 }
12169 }
3019eac3
DE
12170}
12171\f
80626a55
DE
12172/* DWO/DWP files.
12173
12174 http://gcc.gnu.org/wiki/DebugFission
12175 http://gcc.gnu.org/wiki/DebugFissionDWP
12176
12177 To simplify handling of both DWO files ("object" files with the DWARF info)
12178 and DWP files (a file with the DWOs packaged up into one file), we treat
12179 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
12180
12181static hashval_t
12182hash_dwo_file (const void *item)
12183{
9a3c8263 12184 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 12185 hashval_t hash;
3019eac3 12186
a2ce51a0
DE
12187 hash = htab_hash_string (dwo_file->dwo_name);
12188 if (dwo_file->comp_dir != NULL)
12189 hash += htab_hash_string (dwo_file->comp_dir);
12190 return hash;
3019eac3
DE
12191}
12192
12193static int
12194eq_dwo_file (const void *item_lhs, const void *item_rhs)
12195{
9a3c8263
SM
12196 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
12197 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 12198
a2ce51a0
DE
12199 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
12200 return 0;
12201 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
12202 return lhs->comp_dir == rhs->comp_dir;
12203 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
12204}
12205
12206/* Allocate a hash table for DWO files. */
12207
12208static htab_t
ed2dc618 12209allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 12210{
3019eac3
DE
12211 return htab_create_alloc_ex (41,
12212 hash_dwo_file,
12213 eq_dwo_file,
12214 NULL,
12215 &objfile->objfile_obstack,
12216 hashtab_obstack_allocate,
12217 dummy_obstack_deallocate);
12218}
12219
80626a55
DE
12220/* Lookup DWO file DWO_NAME. */
12221
12222static void **
ed2dc618
SM
12223lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
12224 const char *dwo_name,
12225 const char *comp_dir)
80626a55
DE
12226{
12227 struct dwo_file find_entry;
12228 void **slot;
12229
12230 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
12231 dwarf2_per_objfile->dwo_files
12232 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55
DE
12233
12234 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
12235 find_entry.dwo_name = dwo_name;
12236 find_entry.comp_dir = comp_dir;
80626a55
DE
12237 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
12238
12239 return slot;
12240}
12241
3019eac3
DE
12242static hashval_t
12243hash_dwo_unit (const void *item)
12244{
9a3c8263 12245 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
12246
12247 /* This drops the top 32 bits of the id, but is ok for a hash. */
12248 return dwo_unit->signature;
12249}
12250
12251static int
12252eq_dwo_unit (const void *item_lhs, const void *item_rhs)
12253{
9a3c8263
SM
12254 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
12255 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
12256
12257 /* The signature is assumed to be unique within the DWO file.
12258 So while object file CU dwo_id's always have the value zero,
12259 that's OK, assuming each object file DWO file has only one CU,
12260 and that's the rule for now. */
12261 return lhs->signature == rhs->signature;
12262}
12263
12264/* Allocate a hash table for DWO CUs,TUs.
12265 There is one of these tables for each of CUs,TUs for each DWO file. */
12266
12267static htab_t
12268allocate_dwo_unit_table (struct objfile *objfile)
12269{
12270 /* Start out with a pretty small number.
12271 Generally DWO files contain only one CU and maybe some TUs. */
12272 return htab_create_alloc_ex (3,
12273 hash_dwo_unit,
12274 eq_dwo_unit,
12275 NULL,
12276 &objfile->objfile_obstack,
12277 hashtab_obstack_allocate,
12278 dummy_obstack_deallocate);
12279}
12280
80626a55 12281/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 12282
19c3d4c9 12283struct create_dwo_cu_data
3019eac3
DE
12284{
12285 struct dwo_file *dwo_file;
19c3d4c9 12286 struct dwo_unit dwo_unit;
3019eac3
DE
12287};
12288
19c3d4c9 12289/* die_reader_func for create_dwo_cu. */
3019eac3
DE
12290
12291static void
19c3d4c9
DE
12292create_dwo_cu_reader (const struct die_reader_specs *reader,
12293 const gdb_byte *info_ptr,
12294 struct die_info *comp_unit_die,
12295 int has_children,
12296 void *datap)
3019eac3
DE
12297{
12298 struct dwarf2_cu *cu = reader->cu;
9c541725 12299 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 12300 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 12301 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 12302 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 12303 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 12304 struct attribute *attr;
3019eac3
DE
12305
12306 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
12307 if (attr == NULL)
12308 {
19c3d4c9 12309 complaint (&symfile_complaints,
9d8780f0 12310 _("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 12311 " its dwo_id [in module %s]"),
9d8780f0 12312 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
12313 return;
12314 }
12315
3019eac3
DE
12316 dwo_unit->dwo_file = dwo_file;
12317 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 12318 dwo_unit->section = section;
9c541725 12319 dwo_unit->sect_off = sect_off;
3019eac3
DE
12320 dwo_unit->length = cu->per_cu->length;
12321
b4f54984 12322 if (dwarf_read_debug)
9d8780f0
SM
12323 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
12324 sect_offset_str (sect_off),
9c541725 12325 hex_string (dwo_unit->signature));
3019eac3
DE
12326}
12327
33c5cd75 12328/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 12329 Note: This function processes DWO files only, not DWP files. */
3019eac3 12330
33c5cd75 12331static void
ed2dc618
SM
12332create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12333 struct dwo_file &dwo_file, dwarf2_section_info &section,
33c5cd75 12334 htab_t &cus_htab)
3019eac3
DE
12335{
12336 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 12337 const gdb_byte *info_ptr, *end_ptr;
3019eac3 12338
33c5cd75
DB
12339 dwarf2_read_section (objfile, &section);
12340 info_ptr = section.buffer;
3019eac3
DE
12341
12342 if (info_ptr == NULL)
33c5cd75 12343 return;
3019eac3 12344
b4f54984 12345 if (dwarf_read_debug)
19c3d4c9
DE
12346 {
12347 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
12348 get_section_name (&section),
12349 get_section_file_name (&section));
19c3d4c9 12350 }
3019eac3 12351
33c5cd75 12352 end_ptr = info_ptr + section.size;
3019eac3
DE
12353 while (info_ptr < end_ptr)
12354 {
12355 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
12356 struct create_dwo_cu_data create_dwo_cu_data;
12357 struct dwo_unit *dwo_unit;
12358 void **slot;
12359 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 12360
19c3d4c9
DE
12361 memset (&create_dwo_cu_data.dwo_unit, 0,
12362 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3 12363 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 12364 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 12365 per_cu.is_debug_types = 0;
33c5cd75
DB
12366 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12367 per_cu.section = &section;
c5ed0576 12368 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
12369
12370 init_cutu_and_read_dies_no_follow (
12371 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12372 info_ptr += per_cu.length;
12373
12374 // If the unit could not be parsed, skip it.
12375 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12376 continue;
3019eac3 12377
33c5cd75
DB
12378 if (cus_htab == NULL)
12379 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 12380
33c5cd75
DB
12381 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12382 *dwo_unit = create_dwo_cu_data.dwo_unit;
12383 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12384 gdb_assert (slot != NULL);
12385 if (*slot != NULL)
19c3d4c9 12386 {
33c5cd75
DB
12387 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12388 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 12389
33c5cd75 12390 complaint (&symfile_complaints,
9d8780f0
SM
12391 _("debug cu entry at offset %s is duplicate to"
12392 " the entry at offset %s, signature %s"),
12393 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 12394 hex_string (dwo_unit->signature));
19c3d4c9 12395 }
33c5cd75 12396 *slot = (void *)dwo_unit;
3019eac3 12397 }
3019eac3
DE
12398}
12399
80626a55
DE
12400/* DWP file .debug_{cu,tu}_index section format:
12401 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12402
d2415c6c
DE
12403 DWP Version 1:
12404
80626a55
DE
12405 Both index sections have the same format, and serve to map a 64-bit
12406 signature to a set of section numbers. Each section begins with a header,
12407 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12408 indexes, and a pool of 32-bit section numbers. The index sections will be
12409 aligned at 8-byte boundaries in the file.
12410
d2415c6c
DE
12411 The index section header consists of:
12412
12413 V, 32 bit version number
12414 -, 32 bits unused
12415 N, 32 bit number of compilation units or type units in the index
12416 M, 32 bit number of slots in the hash table
80626a55 12417
d2415c6c 12418 Numbers are recorded using the byte order of the application binary.
80626a55 12419
d2415c6c
DE
12420 The hash table begins at offset 16 in the section, and consists of an array
12421 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12422 order of the application binary). Unused slots in the hash table are 0.
12423 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 12424
d2415c6c
DE
12425 The parallel table begins immediately after the hash table
12426 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12427 array of 32-bit indexes (using the byte order of the application binary),
12428 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12429 table contains a 32-bit index into the pool of section numbers. For unused
12430 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 12431
73869dc2
DE
12432 The pool of section numbers begins immediately following the hash table
12433 (at offset 16 + 12 * M from the beginning of the section). The pool of
12434 section numbers consists of an array of 32-bit words (using the byte order
12435 of the application binary). Each item in the array is indexed starting
12436 from 0. The hash table entry provides the index of the first section
12437 number in the set. Additional section numbers in the set follow, and the
12438 set is terminated by a 0 entry (section number 0 is not used in ELF).
12439
12440 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12441 section must be the first entry in the set, and the .debug_abbrev.dwo must
12442 be the second entry. Other members of the set may follow in any order.
12443
12444 ---
12445
12446 DWP Version 2:
12447
12448 DWP Version 2 combines all the .debug_info, etc. sections into one,
12449 and the entries in the index tables are now offsets into these sections.
12450 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12451 section.
12452
12453 Index Section Contents:
12454 Header
12455 Hash Table of Signatures dwp_hash_table.hash_table
12456 Parallel Table of Indices dwp_hash_table.unit_table
12457 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12458 Table of Section Sizes dwp_hash_table.v2.sizes
12459
12460 The index section header consists of:
12461
12462 V, 32 bit version number
12463 L, 32 bit number of columns in the table of section offsets
12464 N, 32 bit number of compilation units or type units in the index
12465 M, 32 bit number of slots in the hash table
12466
12467 Numbers are recorded using the byte order of the application binary.
12468
12469 The hash table has the same format as version 1.
12470 The parallel table of indices has the same format as version 1,
12471 except that the entries are origin-1 indices into the table of sections
12472 offsets and the table of section sizes.
12473
12474 The table of offsets begins immediately following the parallel table
12475 (at offset 16 + 12 * M from the beginning of the section). The table is
12476 a two-dimensional array of 32-bit words (using the byte order of the
12477 application binary), with L columns and N+1 rows, in row-major order.
12478 Each row in the array is indexed starting from 0. The first row provides
12479 a key to the remaining rows: each column in this row provides an identifier
12480 for a debug section, and the offsets in the same column of subsequent rows
12481 refer to that section. The section identifiers are:
12482
12483 DW_SECT_INFO 1 .debug_info.dwo
12484 DW_SECT_TYPES 2 .debug_types.dwo
12485 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12486 DW_SECT_LINE 4 .debug_line.dwo
12487 DW_SECT_LOC 5 .debug_loc.dwo
12488 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12489 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12490 DW_SECT_MACRO 8 .debug_macro.dwo
12491
12492 The offsets provided by the CU and TU index sections are the base offsets
12493 for the contributions made by each CU or TU to the corresponding section
12494 in the package file. Each CU and TU header contains an abbrev_offset
12495 field, used to find the abbreviations table for that CU or TU within the
12496 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12497 be interpreted as relative to the base offset given in the index section.
12498 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12499 should be interpreted as relative to the base offset for .debug_line.dwo,
12500 and offsets into other debug sections obtained from DWARF attributes should
12501 also be interpreted as relative to the corresponding base offset.
12502
12503 The table of sizes begins immediately following the table of offsets.
12504 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12505 with L columns and N rows, in row-major order. Each row in the array is
12506 indexed starting from 1 (row 0 is shared by the two tables).
12507
12508 ---
12509
12510 Hash table lookup is handled the same in version 1 and 2:
12511
12512 We assume that N and M will not exceed 2^32 - 1.
12513 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12514
d2415c6c
DE
12515 Given a 64-bit compilation unit signature or a type signature S, an entry
12516 in the hash table is located as follows:
80626a55 12517
d2415c6c
DE
12518 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12519 the low-order k bits all set to 1.
80626a55 12520
d2415c6c 12521 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12522
d2415c6c
DE
12523 3) If the hash table entry at index H matches the signature, use that
12524 entry. If the hash table entry at index H is unused (all zeroes),
12525 terminate the search: the signature is not present in the table.
80626a55 12526
d2415c6c 12527 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12528
d2415c6c 12529 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12530 to stop at an unused slot or find the match. */
80626a55
DE
12531
12532/* Create a hash table to map DWO IDs to their CU/TU entry in
12533 .debug_{info,types}.dwo in DWP_FILE.
12534 Returns NULL if there isn't one.
12535 Note: This function processes DWP files only, not DWO files. */
12536
12537static struct dwp_hash_table *
ed2dc618
SM
12538create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12539 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
12540{
12541 struct objfile *objfile = dwarf2_per_objfile->objfile;
12542 bfd *dbfd = dwp_file->dbfd;
948f8e3d 12543 const gdb_byte *index_ptr, *index_end;
80626a55 12544 struct dwarf2_section_info *index;
73869dc2 12545 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12546 struct dwp_hash_table *htab;
12547
12548 if (is_debug_types)
12549 index = &dwp_file->sections.tu_index;
12550 else
12551 index = &dwp_file->sections.cu_index;
12552
12553 if (dwarf2_section_empty_p (index))
12554 return NULL;
12555 dwarf2_read_section (objfile, index);
12556
12557 index_ptr = index->buffer;
12558 index_end = index_ptr + index->size;
12559
12560 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12561 index_ptr += 4;
12562 if (version == 2)
12563 nr_columns = read_4_bytes (dbfd, index_ptr);
12564 else
12565 nr_columns = 0;
12566 index_ptr += 4;
80626a55
DE
12567 nr_units = read_4_bytes (dbfd, index_ptr);
12568 index_ptr += 4;
12569 nr_slots = read_4_bytes (dbfd, index_ptr);
12570 index_ptr += 4;
12571
73869dc2 12572 if (version != 1 && version != 2)
80626a55 12573 {
21aa081e 12574 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12575 " [in module %s]"),
21aa081e 12576 pulongest (version), dwp_file->name);
80626a55
DE
12577 }
12578 if (nr_slots != (nr_slots & -nr_slots))
12579 {
21aa081e 12580 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12581 " is not power of 2 [in module %s]"),
21aa081e 12582 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12583 }
12584
12585 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12586 htab->version = version;
12587 htab->nr_columns = nr_columns;
80626a55
DE
12588 htab->nr_units = nr_units;
12589 htab->nr_slots = nr_slots;
12590 htab->hash_table = index_ptr;
12591 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12592
12593 /* Exit early if the table is empty. */
12594 if (nr_slots == 0 || nr_units == 0
12595 || (version == 2 && nr_columns == 0))
12596 {
12597 /* All must be zero. */
12598 if (nr_slots != 0 || nr_units != 0
12599 || (version == 2 && nr_columns != 0))
12600 {
12601 complaint (&symfile_complaints,
12602 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12603 " all zero [in modules %s]"),
12604 dwp_file->name);
12605 }
12606 return htab;
12607 }
12608
12609 if (version == 1)
12610 {
12611 htab->section_pool.v1.indices =
12612 htab->unit_table + sizeof (uint32_t) * nr_slots;
12613 /* It's harder to decide whether the section is too small in v1.
12614 V1 is deprecated anyway so we punt. */
12615 }
12616 else
12617 {
12618 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12619 int *ids = htab->section_pool.v2.section_ids;
12620 /* Reverse map for error checking. */
12621 int ids_seen[DW_SECT_MAX + 1];
12622 int i;
12623
12624 if (nr_columns < 2)
12625 {
12626 error (_("Dwarf Error: bad DWP hash table, too few columns"
12627 " in section table [in module %s]"),
12628 dwp_file->name);
12629 }
12630 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12631 {
12632 error (_("Dwarf Error: bad DWP hash table, too many columns"
12633 " in section table [in module %s]"),
12634 dwp_file->name);
12635 }
12636 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12637 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12638 for (i = 0; i < nr_columns; ++i)
12639 {
12640 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12641
12642 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12643 {
12644 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12645 " in section table [in module %s]"),
12646 id, dwp_file->name);
12647 }
12648 if (ids_seen[id] != -1)
12649 {
12650 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12651 " id %d in section table [in module %s]"),
12652 id, dwp_file->name);
12653 }
12654 ids_seen[id] = i;
12655 ids[i] = id;
12656 }
12657 /* Must have exactly one info or types section. */
12658 if (((ids_seen[DW_SECT_INFO] != -1)
12659 + (ids_seen[DW_SECT_TYPES] != -1))
12660 != 1)
12661 {
12662 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12663 " DWO info/types section [in module %s]"),
12664 dwp_file->name);
12665 }
12666 /* Must have an abbrev section. */
12667 if (ids_seen[DW_SECT_ABBREV] == -1)
12668 {
12669 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12670 " section [in module %s]"),
12671 dwp_file->name);
12672 }
12673 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12674 htab->section_pool.v2.sizes =
12675 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12676 * nr_units * nr_columns);
12677 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12678 * nr_units * nr_columns))
12679 > index_end)
12680 {
12681 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12682 " [in module %s]"),
12683 dwp_file->name);
12684 }
12685 }
80626a55
DE
12686
12687 return htab;
12688}
12689
12690/* Update SECTIONS with the data from SECTP.
12691
12692 This function is like the other "locate" section routines that are
12693 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12694 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12695
12696 The result is non-zero for success, or zero if an error was found. */
12697
12698static int
73869dc2
DE
12699locate_v1_virtual_dwo_sections (asection *sectp,
12700 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12701{
12702 const struct dwop_section_names *names = &dwop_section_names;
12703
12704 if (section_is_p (sectp->name, &names->abbrev_dwo))
12705 {
12706 /* There can be only one. */
049412e3 12707 if (sections->abbrev.s.section != NULL)
80626a55 12708 return 0;
049412e3 12709 sections->abbrev.s.section = sectp;
80626a55
DE
12710 sections->abbrev.size = bfd_get_section_size (sectp);
12711 }
12712 else if (section_is_p (sectp->name, &names->info_dwo)
12713 || section_is_p (sectp->name, &names->types_dwo))
12714 {
12715 /* There can be only one. */
049412e3 12716 if (sections->info_or_types.s.section != NULL)
80626a55 12717 return 0;
049412e3 12718 sections->info_or_types.s.section = sectp;
80626a55
DE
12719 sections->info_or_types.size = bfd_get_section_size (sectp);
12720 }
12721 else if (section_is_p (sectp->name, &names->line_dwo))
12722 {
12723 /* There can be only one. */
049412e3 12724 if (sections->line.s.section != NULL)
80626a55 12725 return 0;
049412e3 12726 sections->line.s.section = sectp;
80626a55
DE
12727 sections->line.size = bfd_get_section_size (sectp);
12728 }
12729 else if (section_is_p (sectp->name, &names->loc_dwo))
12730 {
12731 /* There can be only one. */
049412e3 12732 if (sections->loc.s.section != NULL)
80626a55 12733 return 0;
049412e3 12734 sections->loc.s.section = sectp;
80626a55
DE
12735 sections->loc.size = bfd_get_section_size (sectp);
12736 }
12737 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12738 {
12739 /* There can be only one. */
049412e3 12740 if (sections->macinfo.s.section != NULL)
80626a55 12741 return 0;
049412e3 12742 sections->macinfo.s.section = sectp;
80626a55
DE
12743 sections->macinfo.size = bfd_get_section_size (sectp);
12744 }
12745 else if (section_is_p (sectp->name, &names->macro_dwo))
12746 {
12747 /* There can be only one. */
049412e3 12748 if (sections->macro.s.section != NULL)
80626a55 12749 return 0;
049412e3 12750 sections->macro.s.section = sectp;
80626a55
DE
12751 sections->macro.size = bfd_get_section_size (sectp);
12752 }
12753 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12754 {
12755 /* There can be only one. */
049412e3 12756 if (sections->str_offsets.s.section != NULL)
80626a55 12757 return 0;
049412e3 12758 sections->str_offsets.s.section = sectp;
80626a55
DE
12759 sections->str_offsets.size = bfd_get_section_size (sectp);
12760 }
12761 else
12762 {
12763 /* No other kind of section is valid. */
12764 return 0;
12765 }
12766
12767 return 1;
12768}
12769
73869dc2
DE
12770/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12771 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12772 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12773 This is for DWP version 1 files. */
80626a55
DE
12774
12775static struct dwo_unit *
ed2dc618
SM
12776create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12777 struct dwp_file *dwp_file,
73869dc2
DE
12778 uint32_t unit_index,
12779 const char *comp_dir,
12780 ULONGEST signature, int is_debug_types)
80626a55
DE
12781{
12782 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12783 const struct dwp_hash_table *dwp_htab =
12784 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
12785 bfd *dbfd = dwp_file->dbfd;
12786 const char *kind = is_debug_types ? "TU" : "CU";
12787 struct dwo_file *dwo_file;
12788 struct dwo_unit *dwo_unit;
73869dc2 12789 struct virtual_v1_dwo_sections sections;
80626a55 12790 void **dwo_file_slot;
80626a55
DE
12791 int i;
12792
73869dc2
DE
12793 gdb_assert (dwp_file->version == 1);
12794
b4f54984 12795 if (dwarf_read_debug)
80626a55 12796 {
73869dc2 12797 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12798 kind,
73869dc2 12799 pulongest (unit_index), hex_string (signature),
80626a55
DE
12800 dwp_file->name);
12801 }
12802
19ac8c2e 12803 /* Fetch the sections of this DWO unit.
80626a55
DE
12804 Put a limit on the number of sections we look for so that bad data
12805 doesn't cause us to loop forever. */
12806
73869dc2 12807#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12808 (1 /* .debug_info or .debug_types */ \
12809 + 1 /* .debug_abbrev */ \
12810 + 1 /* .debug_line */ \
12811 + 1 /* .debug_loc */ \
12812 + 1 /* .debug_str_offsets */ \
19ac8c2e 12813 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12814 + 1 /* trailing zero */)
12815
12816 memset (&sections, 0, sizeof (sections));
80626a55 12817
73869dc2 12818 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12819 {
12820 asection *sectp;
12821 uint32_t section_nr =
12822 read_4_bytes (dbfd,
73869dc2
DE
12823 dwp_htab->section_pool.v1.indices
12824 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12825
12826 if (section_nr == 0)
12827 break;
12828 if (section_nr >= dwp_file->num_sections)
12829 {
12830 error (_("Dwarf Error: bad DWP hash table, section number too large"
12831 " [in module %s]"),
12832 dwp_file->name);
12833 }
12834
12835 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12836 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12837 {
12838 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12839 " [in module %s]"),
12840 dwp_file->name);
12841 }
12842 }
12843
12844 if (i < 2
a32a8923
DE
12845 || dwarf2_section_empty_p (&sections.info_or_types)
12846 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12847 {
12848 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12849 " [in module %s]"),
12850 dwp_file->name);
12851 }
73869dc2 12852 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12853 {
12854 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12855 " [in module %s]"),
12856 dwp_file->name);
12857 }
12858
12859 /* It's easier for the rest of the code if we fake a struct dwo_file and
12860 have dwo_unit "live" in that. At least for now.
12861
12862 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12863 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12864 file, we can combine them back into a virtual DWO file to save space
12865 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12866 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12867
791afaa2
TT
12868 std::string virtual_dwo_name =
12869 string_printf ("virtual-dwo/%d-%d-%d-%d",
12870 get_section_id (&sections.abbrev),
12871 get_section_id (&sections.line),
12872 get_section_id (&sections.loc),
12873 get_section_id (&sections.str_offsets));
80626a55 12874 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12875 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12876 virtual_dwo_name.c_str (),
12877 comp_dir);
80626a55
DE
12878 /* Create one if necessary. */
12879 if (*dwo_file_slot == NULL)
12880 {
b4f54984 12881 if (dwarf_read_debug)
80626a55
DE
12882 {
12883 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12884 virtual_dwo_name.c_str ());
80626a55
DE
12885 }
12886 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12887 dwo_file->dwo_name
12888 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12889 virtual_dwo_name.c_str (),
12890 virtual_dwo_name.size ());
0ac5b59e 12891 dwo_file->comp_dir = comp_dir;
80626a55
DE
12892 dwo_file->sections.abbrev = sections.abbrev;
12893 dwo_file->sections.line = sections.line;
12894 dwo_file->sections.loc = sections.loc;
12895 dwo_file->sections.macinfo = sections.macinfo;
12896 dwo_file->sections.macro = sections.macro;
12897 dwo_file->sections.str_offsets = sections.str_offsets;
12898 /* The "str" section is global to the entire DWP file. */
12899 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12900 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12901 there's no need to record it in dwo_file.
12902 Also, we can't simply record type sections in dwo_file because
12903 we record a pointer into the vector in dwo_unit. As we collect more
12904 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12905 for it, invalidating all copies of pointers into the previous
12906 contents. */
80626a55
DE
12907 *dwo_file_slot = dwo_file;
12908 }
12909 else
12910 {
b4f54984 12911 if (dwarf_read_debug)
80626a55
DE
12912 {
12913 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12914 virtual_dwo_name.c_str ());
80626a55 12915 }
9a3c8263 12916 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12917 }
80626a55
DE
12918
12919 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12920 dwo_unit->dwo_file = dwo_file;
12921 dwo_unit->signature = signature;
8d749320
SM
12922 dwo_unit->section =
12923 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12924 *dwo_unit->section = sections.info_or_types;
57d63ce2 12925 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12926
12927 return dwo_unit;
12928}
12929
73869dc2
DE
12930/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12931 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12932 piece within that section used by a TU/CU, return a virtual section
12933 of just that piece. */
12934
12935static struct dwarf2_section_info
ed2dc618
SM
12936create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12937 struct dwarf2_section_info *section,
73869dc2
DE
12938 bfd_size_type offset, bfd_size_type size)
12939{
12940 struct dwarf2_section_info result;
12941 asection *sectp;
12942
12943 gdb_assert (section != NULL);
12944 gdb_assert (!section->is_virtual);
12945
12946 memset (&result, 0, sizeof (result));
12947 result.s.containing_section = section;
12948 result.is_virtual = 1;
12949
12950 if (size == 0)
12951 return result;
12952
12953 sectp = get_section_bfd_section (section);
12954
12955 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12956 bounds of the real section. This is a pretty-rare event, so just
12957 flag an error (easier) instead of a warning and trying to cope. */
12958 if (sectp == NULL
12959 || offset + size > bfd_get_section_size (sectp))
12960 {
73869dc2
DE
12961 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12962 " in section %s [in module %s]"),
12963 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12964 objfile_name (dwarf2_per_objfile->objfile));
12965 }
12966
12967 result.virtual_offset = offset;
12968 result.size = size;
12969 return result;
12970}
12971
12972/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12973 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12974 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12975 This is for DWP version 2 files. */
12976
12977static struct dwo_unit *
ed2dc618
SM
12978create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12979 struct dwp_file *dwp_file,
73869dc2
DE
12980 uint32_t unit_index,
12981 const char *comp_dir,
12982 ULONGEST signature, int is_debug_types)
12983{
12984 struct objfile *objfile = dwarf2_per_objfile->objfile;
12985 const struct dwp_hash_table *dwp_htab =
12986 is_debug_types ? dwp_file->tus : dwp_file->cus;
12987 bfd *dbfd = dwp_file->dbfd;
12988 const char *kind = is_debug_types ? "TU" : "CU";
12989 struct dwo_file *dwo_file;
12990 struct dwo_unit *dwo_unit;
12991 struct virtual_v2_dwo_sections sections;
12992 void **dwo_file_slot;
73869dc2
DE
12993 int i;
12994
12995 gdb_assert (dwp_file->version == 2);
12996
b4f54984 12997 if (dwarf_read_debug)
73869dc2
DE
12998 {
12999 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
13000 kind,
13001 pulongest (unit_index), hex_string (signature),
13002 dwp_file->name);
13003 }
13004
13005 /* Fetch the section offsets of this DWO unit. */
13006
13007 memset (&sections, 0, sizeof (sections));
73869dc2
DE
13008
13009 for (i = 0; i < dwp_htab->nr_columns; ++i)
13010 {
13011 uint32_t offset = read_4_bytes (dbfd,
13012 dwp_htab->section_pool.v2.offsets
13013 + (((unit_index - 1) * dwp_htab->nr_columns
13014 + i)
13015 * sizeof (uint32_t)));
13016 uint32_t size = read_4_bytes (dbfd,
13017 dwp_htab->section_pool.v2.sizes
13018 + (((unit_index - 1) * dwp_htab->nr_columns
13019 + i)
13020 * sizeof (uint32_t)));
13021
13022 switch (dwp_htab->section_pool.v2.section_ids[i])
13023 {
13024 case DW_SECT_INFO:
13025 case DW_SECT_TYPES:
13026 sections.info_or_types_offset = offset;
13027 sections.info_or_types_size = size;
13028 break;
13029 case DW_SECT_ABBREV:
13030 sections.abbrev_offset = offset;
13031 sections.abbrev_size = size;
13032 break;
13033 case DW_SECT_LINE:
13034 sections.line_offset = offset;
13035 sections.line_size = size;
13036 break;
13037 case DW_SECT_LOC:
13038 sections.loc_offset = offset;
13039 sections.loc_size = size;
13040 break;
13041 case DW_SECT_STR_OFFSETS:
13042 sections.str_offsets_offset = offset;
13043 sections.str_offsets_size = size;
13044 break;
13045 case DW_SECT_MACINFO:
13046 sections.macinfo_offset = offset;
13047 sections.macinfo_size = size;
13048 break;
13049 case DW_SECT_MACRO:
13050 sections.macro_offset = offset;
13051 sections.macro_size = size;
13052 break;
13053 }
13054 }
13055
13056 /* It's easier for the rest of the code if we fake a struct dwo_file and
13057 have dwo_unit "live" in that. At least for now.
13058
13059 The DWP file can be made up of a random collection of CUs and TUs.
13060 However, for each CU + set of TUs that came from the same original DWO
13061 file, we can combine them back into a virtual DWO file to save space
13062 (fewer struct dwo_file objects to allocate). Remember that for really
13063 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
13064
791afaa2
TT
13065 std::string virtual_dwo_name =
13066 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
13067 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
13068 (long) (sections.line_size ? sections.line_offset : 0),
13069 (long) (sections.loc_size ? sections.loc_offset : 0),
13070 (long) (sections.str_offsets_size
13071 ? sections.str_offsets_offset : 0));
73869dc2 13072 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
13073 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13074 virtual_dwo_name.c_str (),
13075 comp_dir);
73869dc2
DE
13076 /* Create one if necessary. */
13077 if (*dwo_file_slot == NULL)
13078 {
b4f54984 13079 if (dwarf_read_debug)
73869dc2
DE
13080 {
13081 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 13082 virtual_dwo_name.c_str ());
73869dc2
DE
13083 }
13084 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
13085 dwo_file->dwo_name
13086 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
13087 virtual_dwo_name.c_str (),
13088 virtual_dwo_name.size ());
73869dc2
DE
13089 dwo_file->comp_dir = comp_dir;
13090 dwo_file->sections.abbrev =
ed2dc618 13091 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
13092 sections.abbrev_offset, sections.abbrev_size);
13093 dwo_file->sections.line =
ed2dc618 13094 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
13095 sections.line_offset, sections.line_size);
13096 dwo_file->sections.loc =
ed2dc618 13097 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
13098 sections.loc_offset, sections.loc_size);
13099 dwo_file->sections.macinfo =
ed2dc618 13100 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
13101 sections.macinfo_offset, sections.macinfo_size);
13102 dwo_file->sections.macro =
ed2dc618 13103 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
13104 sections.macro_offset, sections.macro_size);
13105 dwo_file->sections.str_offsets =
ed2dc618
SM
13106 create_dwp_v2_section (dwarf2_per_objfile,
13107 &dwp_file->sections.str_offsets,
73869dc2
DE
13108 sections.str_offsets_offset,
13109 sections.str_offsets_size);
13110 /* The "str" section is global to the entire DWP file. */
13111 dwo_file->sections.str = dwp_file->sections.str;
13112 /* The info or types section is assigned below to dwo_unit,
13113 there's no need to record it in dwo_file.
13114 Also, we can't simply record type sections in dwo_file because
13115 we record a pointer into the vector in dwo_unit. As we collect more
13116 types we'll grow the vector and eventually have to reallocate space
13117 for it, invalidating all copies of pointers into the previous
13118 contents. */
13119 *dwo_file_slot = dwo_file;
13120 }
13121 else
13122 {
b4f54984 13123 if (dwarf_read_debug)
73869dc2
DE
13124 {
13125 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 13126 virtual_dwo_name.c_str ());
73869dc2 13127 }
9a3c8263 13128 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 13129 }
73869dc2
DE
13130
13131 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
13132 dwo_unit->dwo_file = dwo_file;
13133 dwo_unit->signature = signature;
8d749320
SM
13134 dwo_unit->section =
13135 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
13136 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
13137 is_debug_types
73869dc2
DE
13138 ? &dwp_file->sections.types
13139 : &dwp_file->sections.info,
13140 sections.info_or_types_offset,
13141 sections.info_or_types_size);
13142 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
13143
13144 return dwo_unit;
13145}
13146
57d63ce2
DE
13147/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
13148 Returns NULL if the signature isn't found. */
80626a55
DE
13149
13150static struct dwo_unit *
ed2dc618
SM
13151lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
13152 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 13153 ULONGEST signature, int is_debug_types)
80626a55 13154{
57d63ce2
DE
13155 const struct dwp_hash_table *dwp_htab =
13156 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 13157 bfd *dbfd = dwp_file->dbfd;
57d63ce2 13158 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
13159 uint32_t hash = signature & mask;
13160 uint32_t hash2 = ((signature >> 32) & mask) | 1;
13161 unsigned int i;
13162 void **slot;
870f88f7 13163 struct dwo_unit find_dwo_cu;
80626a55
DE
13164
13165 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
13166 find_dwo_cu.signature = signature;
19ac8c2e
DE
13167 slot = htab_find_slot (is_debug_types
13168 ? dwp_file->loaded_tus
13169 : dwp_file->loaded_cus,
13170 &find_dwo_cu, INSERT);
80626a55
DE
13171
13172 if (*slot != NULL)
9a3c8263 13173 return (struct dwo_unit *) *slot;
80626a55
DE
13174
13175 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 13176 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
13177 {
13178 ULONGEST signature_in_table;
13179
13180 signature_in_table =
57d63ce2 13181 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
13182 if (signature_in_table == signature)
13183 {
57d63ce2
DE
13184 uint32_t unit_index =
13185 read_4_bytes (dbfd,
13186 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 13187
73869dc2
DE
13188 if (dwp_file->version == 1)
13189 {
ed2dc618
SM
13190 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
13191 dwp_file, unit_index,
73869dc2
DE
13192 comp_dir, signature,
13193 is_debug_types);
13194 }
13195 else
13196 {
ed2dc618
SM
13197 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
13198 dwp_file, unit_index,
73869dc2
DE
13199 comp_dir, signature,
13200 is_debug_types);
13201 }
9a3c8263 13202 return (struct dwo_unit *) *slot;
80626a55
DE
13203 }
13204 if (signature_in_table == 0)
13205 return NULL;
13206 hash = (hash + hash2) & mask;
13207 }
13208
13209 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
13210 " [in module %s]"),
13211 dwp_file->name);
13212}
13213
ab5088bf 13214/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
13215 Open the file specified by FILE_NAME and hand it off to BFD for
13216 preliminary analysis. Return a newly initialized bfd *, which
13217 includes a canonicalized copy of FILE_NAME.
80626a55 13218 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
13219 SEARCH_CWD is true if the current directory is to be searched.
13220 It will be searched before debug-file-directory.
13aaf454
DE
13221 If successful, the file is added to the bfd include table of the
13222 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 13223 If unable to find/open the file, return NULL.
3019eac3
DE
13224 NOTE: This function is derived from symfile_bfd_open. */
13225
192b62ce 13226static gdb_bfd_ref_ptr
ed2dc618
SM
13227try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13228 const char *file_name, int is_dwp, int search_cwd)
3019eac3 13229{
24b9144d 13230 int desc;
9c02c129
DE
13231 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
13232 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
13233 to debug_file_directory. */
e0cc99a6 13234 const char *search_path;
9c02c129
DE
13235 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
13236
e0cc99a6 13237 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
13238 if (search_cwd)
13239 {
13240 if (*debug_file_directory != '\0')
e0cc99a6
TT
13241 {
13242 search_path_holder.reset (concat (".", dirname_separator_string,
13243 debug_file_directory,
13244 (char *) NULL));
13245 search_path = search_path_holder.get ();
13246 }
6ac97d4c 13247 else
e0cc99a6 13248 search_path = ".";
6ac97d4c 13249 }
9c02c129 13250 else
e0cc99a6 13251 search_path = debug_file_directory;
3019eac3 13252
24b9144d 13253 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
13254 if (is_dwp)
13255 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
13256
13257 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 13258 desc = openp (search_path, flags, file_name,
3019eac3
DE
13259 O_RDONLY | O_BINARY, &absolute_name);
13260 if (desc < 0)
13261 return NULL;
13262
e0cc99a6
TT
13263 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
13264 gnutarget, desc));
9c02c129
DE
13265 if (sym_bfd == NULL)
13266 return NULL;
192b62ce 13267 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 13268
192b62ce
TT
13269 if (!bfd_check_format (sym_bfd.get (), bfd_object))
13270 return NULL;
3019eac3 13271
13aaf454
DE
13272 /* Success. Record the bfd as having been included by the objfile's bfd.
13273 This is important because things like demangled_names_hash lives in the
13274 objfile's per_bfd space and may have references to things like symbol
13275 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 13276 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 13277
3019eac3
DE
13278 return sym_bfd;
13279}
13280
ab5088bf 13281/* Try to open DWO file FILE_NAME.
3019eac3
DE
13282 COMP_DIR is the DW_AT_comp_dir attribute.
13283 The result is the bfd handle of the file.
13284 If there is a problem finding or opening the file, return NULL.
13285 Upon success, the canonicalized path of the file is stored in the bfd,
13286 same as symfile_bfd_open. */
13287
192b62ce 13288static gdb_bfd_ref_ptr
ed2dc618
SM
13289open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13290 const char *file_name, const char *comp_dir)
3019eac3 13291{
80626a55 13292 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
13293 return try_open_dwop_file (dwarf2_per_objfile, file_name,
13294 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
13295
13296 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
13297
13298 if (comp_dir != NULL)
13299 {
b36cec19
PA
13300 char *path_to_try = concat (comp_dir, SLASH_STRING,
13301 file_name, (char *) NULL);
3019eac3
DE
13302
13303 /* NOTE: If comp_dir is a relative path, this will also try the
13304 search path, which seems useful. */
ed2dc618
SM
13305 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
13306 path_to_try,
13307 0 /*is_dwp*/,
192b62ce 13308 1 /*search_cwd*/));
3019eac3
DE
13309 xfree (path_to_try);
13310 if (abfd != NULL)
13311 return abfd;
13312 }
13313
13314 /* That didn't work, try debug-file-directory, which, despite its name,
13315 is a list of paths. */
13316
13317 if (*debug_file_directory == '\0')
13318 return NULL;
13319
ed2dc618
SM
13320 return try_open_dwop_file (dwarf2_per_objfile, file_name,
13321 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
13322}
13323
80626a55
DE
13324/* This function is mapped across the sections and remembers the offset and
13325 size of each of the DWO debugging sections we are interested in. */
13326
13327static void
13328dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
13329{
9a3c8263 13330 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
13331 const struct dwop_section_names *names = &dwop_section_names;
13332
13333 if (section_is_p (sectp->name, &names->abbrev_dwo))
13334 {
049412e3 13335 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
13336 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
13337 }
13338 else if (section_is_p (sectp->name, &names->info_dwo))
13339 {
049412e3 13340 dwo_sections->info.s.section = sectp;
80626a55
DE
13341 dwo_sections->info.size = bfd_get_section_size (sectp);
13342 }
13343 else if (section_is_p (sectp->name, &names->line_dwo))
13344 {
049412e3 13345 dwo_sections->line.s.section = sectp;
80626a55
DE
13346 dwo_sections->line.size = bfd_get_section_size (sectp);
13347 }
13348 else if (section_is_p (sectp->name, &names->loc_dwo))
13349 {
049412e3 13350 dwo_sections->loc.s.section = sectp;
80626a55
DE
13351 dwo_sections->loc.size = bfd_get_section_size (sectp);
13352 }
13353 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13354 {
049412e3 13355 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
13356 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
13357 }
13358 else if (section_is_p (sectp->name, &names->macro_dwo))
13359 {
049412e3 13360 dwo_sections->macro.s.section = sectp;
80626a55
DE
13361 dwo_sections->macro.size = bfd_get_section_size (sectp);
13362 }
13363 else if (section_is_p (sectp->name, &names->str_dwo))
13364 {
049412e3 13365 dwo_sections->str.s.section = sectp;
80626a55
DE
13366 dwo_sections->str.size = bfd_get_section_size (sectp);
13367 }
13368 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13369 {
049412e3 13370 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
13371 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
13372 }
13373 else if (section_is_p (sectp->name, &names->types_dwo))
13374 {
13375 struct dwarf2_section_info type_section;
13376
13377 memset (&type_section, 0, sizeof (type_section));
049412e3 13378 type_section.s.section = sectp;
80626a55
DE
13379 type_section.size = bfd_get_section_size (sectp);
13380 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
13381 &type_section);
13382 }
13383}
13384
ab5088bf 13385/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 13386 by PER_CU. This is for the non-DWP case.
80626a55 13387 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
13388
13389static struct dwo_file *
0ac5b59e
DE
13390open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13391 const char *dwo_name, const char *comp_dir)
3019eac3 13392{
ed2dc618 13393 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 13394 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 13395 struct dwo_file *dwo_file;
3019eac3
DE
13396 struct cleanup *cleanups;
13397
ed2dc618 13398 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
80626a55
DE
13399 if (dbfd == NULL)
13400 {
b4f54984 13401 if (dwarf_read_debug)
80626a55
DE
13402 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13403 return NULL;
13404 }
13405 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
13406 dwo_file->dwo_name = dwo_name;
13407 dwo_file->comp_dir = comp_dir;
192b62ce 13408 dwo_file->dbfd = dbfd.release ();
3019eac3 13409
ed2dc618
SM
13410 free_dwo_file_cleanup_data *cleanup_data = XNEW (free_dwo_file_cleanup_data);
13411 cleanup_data->dwo_file = dwo_file;
13412 cleanup_data->dwarf2_per_objfile = dwarf2_per_objfile;
13413
13414 cleanups = make_cleanup (free_dwo_file_cleanup, cleanup_data);
3019eac3 13415
192b62ce
TT
13416 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
13417 &dwo_file->sections);
3019eac3 13418
ed2dc618
SM
13419 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13420 dwo_file->cus);
3019eac3 13421
ed2dc618
SM
13422 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file,
13423 dwo_file->sections.types, dwo_file->tus);
3019eac3
DE
13424
13425 discard_cleanups (cleanups);
13426
b4f54984 13427 if (dwarf_read_debug)
80626a55
DE
13428 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13429
3019eac3
DE
13430 return dwo_file;
13431}
13432
80626a55 13433/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
13434 size of each of the DWP debugging sections common to version 1 and 2 that
13435 we are interested in. */
3019eac3 13436
80626a55 13437static void
73869dc2
DE
13438dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13439 void *dwp_file_ptr)
3019eac3 13440{
9a3c8263 13441 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
13442 const struct dwop_section_names *names = &dwop_section_names;
13443 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 13444
80626a55 13445 /* Record the ELF section number for later lookup: this is what the
73869dc2 13446 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
13447 gdb_assert (elf_section_nr < dwp_file->num_sections);
13448 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 13449
80626a55
DE
13450 /* Look for specific sections that we need. */
13451 if (section_is_p (sectp->name, &names->str_dwo))
13452 {
049412e3 13453 dwp_file->sections.str.s.section = sectp;
80626a55
DE
13454 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13455 }
13456 else if (section_is_p (sectp->name, &names->cu_index))
13457 {
049412e3 13458 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
13459 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13460 }
13461 else if (section_is_p (sectp->name, &names->tu_index))
13462 {
049412e3 13463 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
13464 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13465 }
13466}
3019eac3 13467
73869dc2
DE
13468/* This function is mapped across the sections and remembers the offset and
13469 size of each of the DWP version 2 debugging sections that we are interested
13470 in. This is split into a separate function because we don't know if we
13471 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13472
13473static void
13474dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13475{
9a3c8263 13476 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
13477 const struct dwop_section_names *names = &dwop_section_names;
13478 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13479
13480 /* Record the ELF section number for later lookup: this is what the
13481 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13482 gdb_assert (elf_section_nr < dwp_file->num_sections);
13483 dwp_file->elf_sections[elf_section_nr] = sectp;
13484
13485 /* Look for specific sections that we need. */
13486 if (section_is_p (sectp->name, &names->abbrev_dwo))
13487 {
049412e3 13488 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
13489 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13490 }
13491 else if (section_is_p (sectp->name, &names->info_dwo))
13492 {
049412e3 13493 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
13494 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13495 }
13496 else if (section_is_p (sectp->name, &names->line_dwo))
13497 {
049412e3 13498 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
13499 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13500 }
13501 else if (section_is_p (sectp->name, &names->loc_dwo))
13502 {
049412e3 13503 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
13504 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13505 }
13506 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13507 {
049412e3 13508 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
13509 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13510 }
13511 else if (section_is_p (sectp->name, &names->macro_dwo))
13512 {
049412e3 13513 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
13514 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13515 }
13516 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13517 {
049412e3 13518 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
13519 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13520 }
13521 else if (section_is_p (sectp->name, &names->types_dwo))
13522 {
049412e3 13523 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
13524 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13525 }
13526}
13527
80626a55 13528/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13529
80626a55
DE
13530static hashval_t
13531hash_dwp_loaded_cutus (const void *item)
13532{
9a3c8263 13533 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13534
80626a55
DE
13535 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13536 return dwo_unit->signature;
3019eac3
DE
13537}
13538
80626a55 13539/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13540
80626a55
DE
13541static int
13542eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13543{
9a3c8263
SM
13544 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13545 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13546
80626a55
DE
13547 return dua->signature == dub->signature;
13548}
3019eac3 13549
80626a55 13550/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13551
80626a55
DE
13552static htab_t
13553allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13554{
13555 return htab_create_alloc_ex (3,
13556 hash_dwp_loaded_cutus,
13557 eq_dwp_loaded_cutus,
13558 NULL,
13559 &objfile->objfile_obstack,
13560 hashtab_obstack_allocate,
13561 dummy_obstack_deallocate);
13562}
3019eac3 13563
ab5088bf
DE
13564/* Try to open DWP file FILE_NAME.
13565 The result is the bfd handle of the file.
13566 If there is a problem finding or opening the file, return NULL.
13567 Upon success, the canonicalized path of the file is stored in the bfd,
13568 same as symfile_bfd_open. */
13569
192b62ce 13570static gdb_bfd_ref_ptr
ed2dc618
SM
13571open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13572 const char *file_name)
ab5088bf 13573{
ed2dc618
SM
13574 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13575 1 /*is_dwp*/,
192b62ce 13576 1 /*search_cwd*/));
6ac97d4c
DE
13577 if (abfd != NULL)
13578 return abfd;
13579
13580 /* Work around upstream bug 15652.
13581 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13582 [Whether that's a "bug" is debatable, but it is getting in our way.]
13583 We have no real idea where the dwp file is, because gdb's realpath-ing
13584 of the executable's path may have discarded the needed info.
13585 [IWBN if the dwp file name was recorded in the executable, akin to
13586 .gnu_debuglink, but that doesn't exist yet.]
13587 Strip the directory from FILE_NAME and search again. */
13588 if (*debug_file_directory != '\0')
13589 {
13590 /* Don't implicitly search the current directory here.
13591 If the user wants to search "." to handle this case,
13592 it must be added to debug-file-directory. */
ed2dc618
SM
13593 return try_open_dwop_file (dwarf2_per_objfile,
13594 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
13595 0 /*search_cwd*/);
13596 }
13597
13598 return NULL;
ab5088bf
DE
13599}
13600
80626a55
DE
13601/* Initialize the use of the DWP file for the current objfile.
13602 By convention the name of the DWP file is ${objfile}.dwp.
13603 The result is NULL if it can't be found. */
a766d390 13604
80626a55 13605static struct dwp_file *
ed2dc618 13606open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
13607{
13608 struct objfile *objfile = dwarf2_per_objfile->objfile;
13609 struct dwp_file *dwp_file;
80626a55 13610
82bf32bc
JK
13611 /* Try to find first .dwp for the binary file before any symbolic links
13612 resolving. */
6c447423
DE
13613
13614 /* If the objfile is a debug file, find the name of the real binary
13615 file and get the name of dwp file from there. */
d721ba37 13616 std::string dwp_name;
6c447423
DE
13617 if (objfile->separate_debug_objfile_backlink != NULL)
13618 {
13619 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13620 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13621
d721ba37 13622 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13623 }
13624 else
d721ba37
PA
13625 dwp_name = objfile->original_name;
13626
13627 dwp_name += ".dwp";
80626a55 13628
ed2dc618 13629 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13630 if (dbfd == NULL
13631 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13632 {
13633 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13634 dwp_name = objfile_name (objfile);
13635 dwp_name += ".dwp";
ed2dc618 13636 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
13637 }
13638
80626a55
DE
13639 if (dbfd == NULL)
13640 {
b4f54984 13641 if (dwarf_read_debug)
d721ba37 13642 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
80626a55 13643 return NULL;
3019eac3 13644 }
80626a55 13645 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
192b62ce
TT
13646 dwp_file->name = bfd_get_filename (dbfd.get ());
13647 dwp_file->dbfd = dbfd.release ();
c906108c 13648
80626a55 13649 /* +1: section 0 is unused */
192b62ce 13650 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
13651 dwp_file->elf_sections =
13652 OBSTACK_CALLOC (&objfile->objfile_obstack,
13653 dwp_file->num_sections, asection *);
13654
192b62ce
TT
13655 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13656 dwp_file);
80626a55 13657
ed2dc618 13658 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 0);
80626a55 13659
ed2dc618 13660 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 1);
80626a55 13661
73869dc2 13662 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13663 if (dwp_file->cus && dwp_file->tus
13664 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13665 {
13666 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13667 pretty bizarre. We use pulongest here because that's the established
4d65956b 13668 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13669 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13670 " TU version %s [in DWP file %s]"),
13671 pulongest (dwp_file->cus->version),
d721ba37 13672 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13673 }
08302ed2
DE
13674
13675 if (dwp_file->cus)
13676 dwp_file->version = dwp_file->cus->version;
13677 else if (dwp_file->tus)
13678 dwp_file->version = dwp_file->tus->version;
13679 else
13680 dwp_file->version = 2;
73869dc2
DE
13681
13682 if (dwp_file->version == 2)
192b62ce
TT
13683 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13684 dwp_file);
73869dc2 13685
19ac8c2e
DE
13686 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13687 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13688
b4f54984 13689 if (dwarf_read_debug)
80626a55
DE
13690 {
13691 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13692 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13693 " %s CUs, %s TUs\n",
13694 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13695 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13696 }
13697
13698 return dwp_file;
3019eac3 13699}
c906108c 13700
ab5088bf
DE
13701/* Wrapper around open_and_init_dwp_file, only open it once. */
13702
13703static struct dwp_file *
ed2dc618 13704get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
13705{
13706 if (! dwarf2_per_objfile->dwp_checked)
13707 {
ed2dc618
SM
13708 dwarf2_per_objfile->dwp_file
13709 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
13710 dwarf2_per_objfile->dwp_checked = 1;
13711 }
13712 return dwarf2_per_objfile->dwp_file;
13713}
13714
80626a55
DE
13715/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13716 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13717 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13718 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13719 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13720
13721 This is called, for example, when wanting to read a variable with a
13722 complex location. Therefore we don't want to do file i/o for every call.
13723 Therefore we don't want to look for a DWO file on every call.
13724 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13725 then we check if we've already seen DWO_NAME, and only THEN do we check
13726 for a DWO file.
13727
1c658ad5 13728 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13729 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13730
3019eac3 13731static struct dwo_unit *
80626a55
DE
13732lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13733 const char *dwo_name, const char *comp_dir,
13734 ULONGEST signature, int is_debug_types)
3019eac3 13735{
ed2dc618 13736 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 13737 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13738 const char *kind = is_debug_types ? "TU" : "CU";
13739 void **dwo_file_slot;
3019eac3 13740 struct dwo_file *dwo_file;
80626a55 13741 struct dwp_file *dwp_file;
cb1df416 13742
6a506a2d
DE
13743 /* First see if there's a DWP file.
13744 If we have a DWP file but didn't find the DWO inside it, don't
13745 look for the original DWO file. It makes gdb behave differently
13746 depending on whether one is debugging in the build tree. */
cf2c3c16 13747
ed2dc618 13748 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 13749 if (dwp_file != NULL)
cf2c3c16 13750 {
80626a55
DE
13751 const struct dwp_hash_table *dwp_htab =
13752 is_debug_types ? dwp_file->tus : dwp_file->cus;
13753
13754 if (dwp_htab != NULL)
13755 {
13756 struct dwo_unit *dwo_cutu =
ed2dc618 13757 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 13758 signature, is_debug_types);
80626a55
DE
13759
13760 if (dwo_cutu != NULL)
13761 {
b4f54984 13762 if (dwarf_read_debug)
80626a55
DE
13763 {
13764 fprintf_unfiltered (gdb_stdlog,
13765 "Virtual DWO %s %s found: @%s\n",
13766 kind, hex_string (signature),
13767 host_address_to_string (dwo_cutu));
13768 }
13769 return dwo_cutu;
13770 }
13771 }
13772 }
6a506a2d 13773 else
80626a55 13774 {
6a506a2d 13775 /* No DWP file, look for the DWO file. */
80626a55 13776
ed2dc618
SM
13777 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13778 dwo_name, comp_dir);
6a506a2d 13779 if (*dwo_file_slot == NULL)
80626a55 13780 {
6a506a2d
DE
13781 /* Read in the file and build a table of the CUs/TUs it contains. */
13782 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13783 }
6a506a2d 13784 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13785 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13786
6a506a2d 13787 if (dwo_file != NULL)
19c3d4c9 13788 {
6a506a2d
DE
13789 struct dwo_unit *dwo_cutu = NULL;
13790
13791 if (is_debug_types && dwo_file->tus)
13792 {
13793 struct dwo_unit find_dwo_cutu;
13794
13795 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13796 find_dwo_cutu.signature = signature;
9a3c8263
SM
13797 dwo_cutu
13798 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13799 }
33c5cd75 13800 else if (!is_debug_types && dwo_file->cus)
80626a55 13801 {
33c5cd75
DB
13802 struct dwo_unit find_dwo_cutu;
13803
13804 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13805 find_dwo_cutu.signature = signature;
13806 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13807 &find_dwo_cutu);
6a506a2d
DE
13808 }
13809
13810 if (dwo_cutu != NULL)
13811 {
b4f54984 13812 if (dwarf_read_debug)
6a506a2d
DE
13813 {
13814 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13815 kind, dwo_name, hex_string (signature),
13816 host_address_to_string (dwo_cutu));
13817 }
13818 return dwo_cutu;
80626a55
DE
13819 }
13820 }
2e276125 13821 }
9cdd5dbd 13822
80626a55
DE
13823 /* We didn't find it. This could mean a dwo_id mismatch, or
13824 someone deleted the DWO/DWP file, or the search path isn't set up
13825 correctly to find the file. */
13826
b4f54984 13827 if (dwarf_read_debug)
80626a55
DE
13828 {
13829 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13830 kind, dwo_name, hex_string (signature));
13831 }
3019eac3 13832
6656a72d
DE
13833 /* This is a warning and not a complaint because it can be caused by
13834 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13835 {
13836 /* Print the name of the DWP file if we looked there, helps the user
13837 better diagnose the problem. */
791afaa2 13838 std::string dwp_text;
43942612
DE
13839
13840 if (dwp_file != NULL)
791afaa2
TT
13841 dwp_text = string_printf (" [in DWP file %s]",
13842 lbasename (dwp_file->name));
43942612 13843
9d8780f0 13844 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
13845 " [in module %s]"),
13846 kind, dwo_name, hex_string (signature),
791afaa2 13847 dwp_text.c_str (),
43942612 13848 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 13849 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 13850 }
3019eac3 13851 return NULL;
5fb290d7
DJ
13852}
13853
80626a55
DE
13854/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13855 See lookup_dwo_cutu_unit for details. */
13856
13857static struct dwo_unit *
13858lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13859 const char *dwo_name, const char *comp_dir,
13860 ULONGEST signature)
13861{
13862 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13863}
13864
13865/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13866 See lookup_dwo_cutu_unit for details. */
13867
13868static struct dwo_unit *
13869lookup_dwo_type_unit (struct signatured_type *this_tu,
13870 const char *dwo_name, const char *comp_dir)
13871{
13872 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13873}
13874
89e63ee4
DE
13875/* Traversal function for queue_and_load_all_dwo_tus. */
13876
13877static int
13878queue_and_load_dwo_tu (void **slot, void *info)
13879{
13880 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13881 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13882 ULONGEST signature = dwo_unit->signature;
13883 struct signatured_type *sig_type =
13884 lookup_dwo_signatured_type (per_cu->cu, signature);
13885
13886 if (sig_type != NULL)
13887 {
13888 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13889
13890 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13891 a real dependency of PER_CU on SIG_TYPE. That is detected later
13892 while processing PER_CU. */
13893 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13894 load_full_type_unit (sig_cu);
13895 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13896 }
13897
13898 return 1;
13899}
13900
13901/* Queue all TUs contained in the DWO of PER_CU to be read in.
13902 The DWO may have the only definition of the type, though it may not be
13903 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13904 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13905
13906static void
13907queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13908{
13909 struct dwo_unit *dwo_unit;
13910 struct dwo_file *dwo_file;
13911
13912 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13913 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13914 gdb_assert (per_cu->cu != NULL);
13915
13916 dwo_unit = per_cu->cu->dwo_unit;
13917 gdb_assert (dwo_unit != NULL);
13918
13919 dwo_file = dwo_unit->dwo_file;
13920 if (dwo_file->tus != NULL)
13921 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13922}
13923
3019eac3
DE
13924/* Free all resources associated with DWO_FILE.
13925 Close the DWO file and munmap the sections.
13926 All memory should be on the objfile obstack. */
348e048f
DE
13927
13928static void
3019eac3 13929free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 13930{
348e048f 13931
5c6fa7ab 13932 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 13933 gdb_bfd_unref (dwo_file->dbfd);
348e048f 13934
3019eac3
DE
13935 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13936}
348e048f 13937
3019eac3 13938/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 13939
3019eac3
DE
13940static void
13941free_dwo_file_cleanup (void *arg)
13942{
ed2dc618
SM
13943 struct free_dwo_file_cleanup_data *data
13944 = (struct free_dwo_file_cleanup_data *) arg;
13945 struct objfile *objfile = data->dwarf2_per_objfile->objfile;
348e048f 13946
ed2dc618
SM
13947 free_dwo_file (data->dwo_file, objfile);
13948
13949 xfree (data);
3019eac3 13950}
348e048f 13951
3019eac3 13952/* Traversal function for free_dwo_files. */
2ab95328 13953
3019eac3
DE
13954static int
13955free_dwo_file_from_slot (void **slot, void *info)
13956{
13957 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13958 struct objfile *objfile = (struct objfile *) info;
348e048f 13959
3019eac3 13960 free_dwo_file (dwo_file, objfile);
348e048f 13961
3019eac3
DE
13962 return 1;
13963}
348e048f 13964
3019eac3 13965/* Free all resources associated with DWO_FILES. */
348e048f 13966
3019eac3
DE
13967static void
13968free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13969{
13970 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 13971}
3019eac3
DE
13972\f
13973/* Read in various DIEs. */
348e048f 13974
d389af10 13975/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13976 Inherit only the children of the DW_AT_abstract_origin DIE not being
13977 already referenced by DW_AT_abstract_origin from the children of the
13978 current DIE. */
d389af10
JK
13979
13980static void
13981inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13982{
13983 struct die_info *child_die;
791afaa2 13984 sect_offset *offsetp;
d389af10
JK
13985 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13986 struct die_info *origin_die;
13987 /* Iterator of the ORIGIN_DIE children. */
13988 struct die_info *origin_child_die;
d389af10 13989 struct attribute *attr;
cd02d79d
PA
13990 struct dwarf2_cu *origin_cu;
13991 struct pending **origin_previous_list_in_scope;
d389af10
JK
13992
13993 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13994 if (!attr)
13995 return;
13996
cd02d79d
PA
13997 /* Note that following die references may follow to a die in a
13998 different cu. */
13999
14000 origin_cu = cu;
14001 origin_die = follow_die_ref (die, attr, &origin_cu);
14002
14003 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
14004 symbols in. */
14005 origin_previous_list_in_scope = origin_cu->list_in_scope;
14006 origin_cu->list_in_scope = cu->list_in_scope;
14007
edb3359d
DJ
14008 if (die->tag != origin_die->tag
14009 && !(die->tag == DW_TAG_inlined_subroutine
14010 && origin_die->tag == DW_TAG_subprogram))
d389af10 14011 complaint (&symfile_complaints,
9d8780f0
SM
14012 _("DIE %s and its abstract origin %s have different tags"),
14013 sect_offset_str (die->sect_off),
14014 sect_offset_str (origin_die->sect_off));
d389af10 14015
791afaa2 14016 std::vector<sect_offset> offsets;
d389af10 14017
3ea89b92
PMR
14018 for (child_die = die->child;
14019 child_die && child_die->tag;
14020 child_die = sibling_die (child_die))
14021 {
14022 struct die_info *child_origin_die;
14023 struct dwarf2_cu *child_origin_cu;
14024
14025 /* We are trying to process concrete instance entries:
216f72a1 14026 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
14027 it's not relevant to our analysis here. i.e. detecting DIEs that are
14028 present in the abstract instance but not referenced in the concrete
14029 one. */
216f72a1
JK
14030 if (child_die->tag == DW_TAG_call_site
14031 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
14032 continue;
14033
c38f313d
DJ
14034 /* For each CHILD_DIE, find the corresponding child of
14035 ORIGIN_DIE. If there is more than one layer of
14036 DW_AT_abstract_origin, follow them all; there shouldn't be,
14037 but GCC versions at least through 4.4 generate this (GCC PR
14038 40573). */
3ea89b92
PMR
14039 child_origin_die = child_die;
14040 child_origin_cu = cu;
c38f313d
DJ
14041 while (1)
14042 {
cd02d79d
PA
14043 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
14044 child_origin_cu);
c38f313d
DJ
14045 if (attr == NULL)
14046 break;
cd02d79d
PA
14047 child_origin_die = follow_die_ref (child_origin_die, attr,
14048 &child_origin_cu);
c38f313d
DJ
14049 }
14050
d389af10
JK
14051 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
14052 counterpart may exist. */
c38f313d 14053 if (child_origin_die != child_die)
d389af10 14054 {
edb3359d
DJ
14055 if (child_die->tag != child_origin_die->tag
14056 && !(child_die->tag == DW_TAG_inlined_subroutine
14057 && child_origin_die->tag == DW_TAG_subprogram))
d389af10 14058 complaint (&symfile_complaints,
9d8780f0 14059 _("Child DIE %s and its abstract origin %s have "
9c541725 14060 "different tags"),
9d8780f0
SM
14061 sect_offset_str (child_die->sect_off),
14062 sect_offset_str (child_origin_die->sect_off));
c38f313d
DJ
14063 if (child_origin_die->parent != origin_die)
14064 complaint (&symfile_complaints,
9d8780f0 14065 _("Child DIE %s and its abstract origin %s have "
9c541725 14066 "different parents"),
9d8780f0
SM
14067 sect_offset_str (child_die->sect_off),
14068 sect_offset_str (child_origin_die->sect_off));
c38f313d 14069 else
791afaa2 14070 offsets.push_back (child_origin_die->sect_off);
d389af10 14071 }
d389af10 14072 }
791afaa2
TT
14073 std::sort (offsets.begin (), offsets.end ());
14074 sect_offset *offsets_end = offsets.data () + offsets.size ();
14075 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 14076 if (offsetp[-1] == *offsetp)
3e43a32a 14077 complaint (&symfile_complaints,
9d8780f0
SM
14078 _("Multiple children of DIE %s refer "
14079 "to DIE %s as their abstract origin"),
14080 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 14081
791afaa2 14082 offsetp = offsets.data ();
d389af10
JK
14083 origin_child_die = origin_die->child;
14084 while (origin_child_die && origin_child_die->tag)
14085 {
14086 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 14087 while (offsetp < offsets_end
9c541725 14088 && *offsetp < origin_child_die->sect_off)
d389af10 14089 offsetp++;
b64f50a1 14090 if (offsetp >= offsets_end
9c541725 14091 || *offsetp > origin_child_die->sect_off)
d389af10 14092 {
adde2bff
DE
14093 /* Found that ORIGIN_CHILD_DIE is really not referenced.
14094 Check whether we're already processing ORIGIN_CHILD_DIE.
14095 This can happen with mutually referenced abstract_origins.
14096 PR 16581. */
14097 if (!origin_child_die->in_process)
14098 process_die (origin_child_die, origin_cu);
d389af10
JK
14099 }
14100 origin_child_die = sibling_die (origin_child_die);
14101 }
cd02d79d 14102 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
14103}
14104
c906108c 14105static void
e7c27a73 14106read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14107{
518817b3 14108 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14109 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 14110 struct context_stack *newobj;
c906108c
SS
14111 CORE_ADDR lowpc;
14112 CORE_ADDR highpc;
14113 struct die_info *child_die;
edb3359d 14114 struct attribute *attr, *call_line, *call_file;
15d034d0 14115 const char *name;
e142c38c 14116 CORE_ADDR baseaddr;
801e3a5b 14117 struct block *block;
edb3359d 14118 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 14119 std::vector<struct symbol *> template_args;
34eaf542 14120 struct template_symbol *templ_func = NULL;
edb3359d
DJ
14121
14122 if (inlined_func)
14123 {
14124 /* If we do not have call site information, we can't show the
14125 caller of this inlined function. That's too confusing, so
14126 only use the scope for local variables. */
14127 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
14128 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
14129 if (call_line == NULL || call_file == NULL)
14130 {
14131 read_lexical_block_scope (die, cu);
14132 return;
14133 }
14134 }
c906108c 14135
e142c38c
DJ
14136 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14137
94af9270 14138 name = dwarf2_name (die, cu);
c906108c 14139
e8d05480
JB
14140 /* Ignore functions with missing or empty names. These are actually
14141 illegal according to the DWARF standard. */
14142 if (name == NULL)
14143 {
14144 complaint (&symfile_complaints,
9d8780f0
SM
14145 _("missing name for subprogram DIE at %s"),
14146 sect_offset_str (die->sect_off));
e8d05480
JB
14147 return;
14148 }
14149
14150 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 14151 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 14152 <= PC_BOUNDS_INVALID)
e8d05480 14153 {
ae4d0c03
PM
14154 attr = dwarf2_attr (die, DW_AT_external, cu);
14155 if (!attr || !DW_UNSND (attr))
14156 complaint (&symfile_complaints,
3e43a32a 14157 _("cannot get low and high bounds "
9d8780f0
SM
14158 "for subprogram DIE at %s"),
14159 sect_offset_str (die->sect_off));
e8d05480
JB
14160 return;
14161 }
c906108c 14162
3e29f34a
MR
14163 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14164 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 14165
34eaf542
TT
14166 /* If we have any template arguments, then we must allocate a
14167 different sort of symbol. */
14168 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
14169 {
14170 if (child_die->tag == DW_TAG_template_type_param
14171 || child_die->tag == DW_TAG_template_value_param)
14172 {
e623cf5d 14173 templ_func = allocate_template_symbol (objfile);
cf724bc9 14174 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
14175 break;
14176 }
14177 }
14178
fe978cb0 14179 newobj = push_context (0, lowpc);
5e2db402
TT
14180 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
14181 (struct symbol *) templ_func);
4c2df51b 14182
4cecd739
DJ
14183 /* If there is a location expression for DW_AT_frame_base, record
14184 it. */
e142c38c 14185 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 14186 if (attr)
fe978cb0 14187 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 14188
63e43d3a
PMR
14189 /* If there is a location for the static link, record it. */
14190 newobj->static_link = NULL;
14191 attr = dwarf2_attr (die, DW_AT_static_link, cu);
14192 if (attr)
14193 {
224c3ddb
SM
14194 newobj->static_link
14195 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
14196 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
14197 }
14198
e142c38c 14199 cu->list_in_scope = &local_symbols;
c906108c 14200
639d11d3 14201 if (die->child != NULL)
c906108c 14202 {
639d11d3 14203 child_die = die->child;
c906108c
SS
14204 while (child_die && child_die->tag)
14205 {
34eaf542
TT
14206 if (child_die->tag == DW_TAG_template_type_param
14207 || child_die->tag == DW_TAG_template_value_param)
14208 {
14209 struct symbol *arg = new_symbol (child_die, NULL, cu);
14210
f1078f66 14211 if (arg != NULL)
2f4732b0 14212 template_args.push_back (arg);
34eaf542
TT
14213 }
14214 else
14215 process_die (child_die, cu);
c906108c
SS
14216 child_die = sibling_die (child_die);
14217 }
14218 }
14219
d389af10
JK
14220 inherit_abstract_dies (die, cu);
14221
4a811a97
UW
14222 /* If we have a DW_AT_specification, we might need to import using
14223 directives from the context of the specification DIE. See the
14224 comment in determine_prefix. */
14225 if (cu->language == language_cplus
14226 && dwarf2_attr (die, DW_AT_specification, cu))
14227 {
14228 struct dwarf2_cu *spec_cu = cu;
14229 struct die_info *spec_die = die_specification (die, &spec_cu);
14230
14231 while (spec_die)
14232 {
14233 child_die = spec_die->child;
14234 while (child_die && child_die->tag)
14235 {
14236 if (child_die->tag == DW_TAG_imported_module)
14237 process_die (child_die, spec_cu);
14238 child_die = sibling_die (child_die);
14239 }
14240
14241 /* In some cases, GCC generates specification DIEs that
14242 themselves contain DW_AT_specification attributes. */
14243 spec_die = die_specification (spec_die, &spec_cu);
14244 }
14245 }
14246
fe978cb0 14247 newobj = pop_context ();
c906108c 14248 /* Make a block for the local symbols within. */
fe978cb0 14249 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
63e43d3a 14250 newobj->static_link, lowpc, highpc);
801e3a5b 14251
df8a16a1 14252 /* For C++, set the block's scope. */
45280282
IB
14253 if ((cu->language == language_cplus
14254 || cu->language == language_fortran
c44af4eb
TT
14255 || cu->language == language_d
14256 || cu->language == language_rust)
4d4ec4e5 14257 && cu->processing_has_namespace_info)
195a3f6c
TT
14258 block_set_scope (block, determine_prefix (die, cu),
14259 &objfile->objfile_obstack);
df8a16a1 14260
801e3a5b
JB
14261 /* If we have address ranges, record them. */
14262 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 14263
fe978cb0 14264 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 14265
34eaf542 14266 /* Attach template arguments to function. */
2f4732b0 14267 if (!template_args.empty ())
34eaf542
TT
14268 {
14269 gdb_assert (templ_func != NULL);
14270
2f4732b0 14271 templ_func->n_template_arguments = template_args.size ();
34eaf542 14272 templ_func->template_arguments
8d749320
SM
14273 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
14274 templ_func->n_template_arguments);
34eaf542 14275 memcpy (templ_func->template_arguments,
2f4732b0 14276 template_args.data (),
34eaf542 14277 (templ_func->n_template_arguments * sizeof (struct symbol *)));
34eaf542
TT
14278 }
14279
208d8187
JB
14280 /* In C++, we can have functions nested inside functions (e.g., when
14281 a function declares a class that has methods). This means that
14282 when we finish processing a function scope, we may need to go
14283 back to building a containing block's symbol lists. */
fe978cb0 14284 local_symbols = newobj->locals;
22cee43f 14285 local_using_directives = newobj->local_using_directives;
208d8187 14286
921e78cf
JB
14287 /* If we've finished processing a top-level function, subsequent
14288 symbols go in the file symbol list. */
14289 if (outermost_context_p ())
e142c38c 14290 cu->list_in_scope = &file_symbols;
c906108c
SS
14291}
14292
14293/* Process all the DIES contained within a lexical block scope. Start
14294 a new scope, process the dies, and then close the scope. */
14295
14296static void
e7c27a73 14297read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14298{
518817b3 14299 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14300 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 14301 struct context_stack *newobj;
c906108c
SS
14302 CORE_ADDR lowpc, highpc;
14303 struct die_info *child_die;
e142c38c
DJ
14304 CORE_ADDR baseaddr;
14305
14306 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
14307
14308 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
14309 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
14310 as multiple lexical blocks? Handling children in a sane way would
6e70227d 14311 be nasty. Might be easier to properly extend generic blocks to
af34e669 14312 describe ranges. */
e385593e
JK
14313 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
14314 {
14315 case PC_BOUNDS_NOT_PRESENT:
14316 /* DW_TAG_lexical_block has no attributes, process its children as if
14317 there was no wrapping by that DW_TAG_lexical_block.
14318 GCC does no longer produces such DWARF since GCC r224161. */
14319 for (child_die = die->child;
14320 child_die != NULL && child_die->tag;
14321 child_die = sibling_die (child_die))
14322 process_die (child_die, cu);
14323 return;
14324 case PC_BOUNDS_INVALID:
14325 return;
14326 }
3e29f34a
MR
14327 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14328 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
14329
14330 push_context (0, lowpc);
639d11d3 14331 if (die->child != NULL)
c906108c 14332 {
639d11d3 14333 child_die = die->child;
c906108c
SS
14334 while (child_die && child_die->tag)
14335 {
e7c27a73 14336 process_die (child_die, cu);
c906108c
SS
14337 child_die = sibling_die (child_die);
14338 }
14339 }
3ea89b92 14340 inherit_abstract_dies (die, cu);
fe978cb0 14341 newobj = pop_context ();
c906108c 14342
22cee43f 14343 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 14344 {
801e3a5b 14345 struct block *block
63e43d3a 14346 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
fe978cb0 14347 newobj->start_addr, highpc);
801e3a5b
JB
14348
14349 /* Note that recording ranges after traversing children, as we
14350 do here, means that recording a parent's ranges entails
14351 walking across all its children's ranges as they appear in
14352 the address map, which is quadratic behavior.
14353
14354 It would be nicer to record the parent's ranges before
14355 traversing its children, simply overriding whatever you find
14356 there. But since we don't even decide whether to create a
14357 block until after we've traversed its children, that's hard
14358 to do. */
14359 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 14360 }
fe978cb0 14361 local_symbols = newobj->locals;
22cee43f 14362 local_using_directives = newobj->local_using_directives;
c906108c
SS
14363}
14364
216f72a1 14365/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
14366
14367static void
14368read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
14369{
518817b3 14370 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
14371 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14372 CORE_ADDR pc, baseaddr;
14373 struct attribute *attr;
14374 struct call_site *call_site, call_site_local;
14375 void **slot;
14376 int nparams;
14377 struct die_info *child_die;
14378
14379 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14380
216f72a1
JK
14381 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
14382 if (attr == NULL)
14383 {
14384 /* This was a pre-DWARF-5 GNU extension alias
14385 for DW_AT_call_return_pc. */
14386 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14387 }
96408a79
SA
14388 if (!attr)
14389 {
14390 complaint (&symfile_complaints,
216f72a1 14391 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
14392 "DIE %s [in module %s]"),
14393 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14394 return;
14395 }
31aa7e4e 14396 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 14397 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
14398
14399 if (cu->call_site_htab == NULL)
14400 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
14401 NULL, &objfile->objfile_obstack,
14402 hashtab_obstack_allocate, NULL);
14403 call_site_local.pc = pc;
14404 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
14405 if (*slot != NULL)
14406 {
14407 complaint (&symfile_complaints,
216f72a1 14408 _("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
14409 "DIE %s [in module %s]"),
14410 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 14411 objfile_name (objfile));
96408a79
SA
14412 return;
14413 }
14414
14415 /* Count parameters at the caller. */
14416
14417 nparams = 0;
14418 for (child_die = die->child; child_die && child_die->tag;
14419 child_die = sibling_die (child_die))
14420 {
216f72a1
JK
14421 if (child_die->tag != DW_TAG_call_site_parameter
14422 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14423 {
14424 complaint (&symfile_complaints,
216f72a1 14425 _("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
14426 "DW_TAG_call_site child DIE %s [in module %s]"),
14427 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 14428 objfile_name (objfile));
96408a79
SA
14429 continue;
14430 }
14431
14432 nparams++;
14433 }
14434
224c3ddb
SM
14435 call_site
14436 = ((struct call_site *)
14437 obstack_alloc (&objfile->objfile_obstack,
14438 sizeof (*call_site)
14439 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
14440 *slot = call_site;
14441 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14442 call_site->pc = pc;
14443
216f72a1
JK
14444 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14445 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
14446 {
14447 struct die_info *func_die;
14448
14449 /* Skip also over DW_TAG_inlined_subroutine. */
14450 for (func_die = die->parent;
14451 func_die && func_die->tag != DW_TAG_subprogram
14452 && func_die->tag != DW_TAG_subroutine_type;
14453 func_die = func_die->parent);
14454
216f72a1
JK
14455 /* DW_AT_call_all_calls is a superset
14456 of DW_AT_call_all_tail_calls. */
96408a79 14457 if (func_die
216f72a1 14458 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 14459 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 14460 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
14461 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14462 {
14463 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14464 not complete. But keep CALL_SITE for look ups via call_site_htab,
14465 both the initial caller containing the real return address PC and
14466 the final callee containing the current PC of a chain of tail
14467 calls do not need to have the tail call list complete. But any
14468 function candidate for a virtual tail call frame searched via
14469 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14470 determined unambiguously. */
14471 }
14472 else
14473 {
14474 struct type *func_type = NULL;
14475
14476 if (func_die)
14477 func_type = get_die_type (func_die, cu);
14478 if (func_type != NULL)
14479 {
14480 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14481
14482 /* Enlist this call site to the function. */
14483 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14484 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14485 }
14486 else
14487 complaint (&symfile_complaints,
216f72a1 14488 _("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
14489 "DIE %s [in module %s]"),
14490 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14491 }
14492 }
14493
216f72a1
JK
14494 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14495 if (attr == NULL)
14496 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14497 if (attr == NULL)
14498 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14499 if (attr == NULL)
216f72a1
JK
14500 {
14501 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14502 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14503 }
96408a79
SA
14504 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14505 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14506 /* Keep NULL DWARF_BLOCK. */;
14507 else if (attr_form_is_block (attr))
14508 {
14509 struct dwarf2_locexpr_baton *dlbaton;
14510
8d749320 14511 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
14512 dlbaton->data = DW_BLOCK (attr)->data;
14513 dlbaton->size = DW_BLOCK (attr)->size;
14514 dlbaton->per_cu = cu->per_cu;
14515
14516 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14517 }
7771576e 14518 else if (attr_form_is_ref (attr))
96408a79 14519 {
96408a79
SA
14520 struct dwarf2_cu *target_cu = cu;
14521 struct die_info *target_die;
14522
ac9ec31b 14523 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 14524 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
14525 if (die_is_declaration (target_die, target_cu))
14526 {
7d45c7c3 14527 const char *target_physname;
9112db09
JK
14528
14529 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14530 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14531 if (target_physname == NULL)
9112db09 14532 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
14533 if (target_physname == NULL)
14534 complaint (&symfile_complaints,
216f72a1 14535 _("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14536 "physname, for referencing DIE %s [in module %s]"),
14537 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14538 else
7d455152 14539 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14540 }
14541 else
14542 {
14543 CORE_ADDR lowpc;
14544
14545 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14546 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14547 <= PC_BOUNDS_INVALID)
96408a79 14548 complaint (&symfile_complaints,
216f72a1 14549 _("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14550 "low pc, for referencing DIE %s [in module %s]"),
14551 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14552 else
3e29f34a
MR
14553 {
14554 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14555 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14556 }
96408a79
SA
14557 }
14558 }
14559 else
14560 complaint (&symfile_complaints,
216f72a1 14561 _("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14562 "block nor reference, for DIE %s [in module %s]"),
14563 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14564
14565 call_site->per_cu = cu->per_cu;
14566
14567 for (child_die = die->child;
14568 child_die && child_die->tag;
14569 child_die = sibling_die (child_die))
14570 {
96408a79 14571 struct call_site_parameter *parameter;
1788b2d3 14572 struct attribute *loc, *origin;
96408a79 14573
216f72a1
JK
14574 if (child_die->tag != DW_TAG_call_site_parameter
14575 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14576 {
14577 /* Already printed the complaint above. */
14578 continue;
14579 }
14580
14581 gdb_assert (call_site->parameter_count < nparams);
14582 parameter = &call_site->parameter[call_site->parameter_count];
14583
1788b2d3
JK
14584 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14585 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14586 register is contained in DW_AT_call_value. */
96408a79 14587
24c5c679 14588 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14589 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14590 if (origin == NULL)
14591 {
14592 /* This was a pre-DWARF-5 GNU extension alias
14593 for DW_AT_call_parameter. */
14594 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14595 }
7771576e 14596 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14597 {
1788b2d3 14598 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14599
14600 sect_offset sect_off
14601 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14602 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14603 {
14604 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14605 binding can be done only inside one CU. Such referenced DIE
14606 therefore cannot be even moved to DW_TAG_partial_unit. */
14607 complaint (&symfile_complaints,
216f72a1 14608 _("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14609 "DW_TAG_call_site child DIE %s [in module %s]"),
14610 sect_offset_str (child_die->sect_off),
9c541725 14611 objfile_name (objfile));
d76b7dbc
JK
14612 continue;
14613 }
9c541725
PA
14614 parameter->u.param_cu_off
14615 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14616 }
14617 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
14618 {
14619 complaint (&symfile_complaints,
14620 _("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14621 "DW_TAG_call_site child DIE %s [in module %s]"),
14622 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14623 continue;
14624 }
24c5c679 14625 else
96408a79 14626 {
24c5c679
JK
14627 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14628 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14629 if (parameter->u.dwarf_reg != -1)
14630 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14631 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14632 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14633 &parameter->u.fb_offset))
14634 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14635 else
14636 {
14637 complaint (&symfile_complaints,
14638 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14639 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14640 "DW_TAG_call_site child DIE %s "
24c5c679 14641 "[in module %s]"),
9d8780f0 14642 sect_offset_str (child_die->sect_off),
9c541725 14643 objfile_name (objfile));
24c5c679
JK
14644 continue;
14645 }
96408a79
SA
14646 }
14647
216f72a1
JK
14648 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14649 if (attr == NULL)
14650 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14651 if (!attr_form_is_block (attr))
14652 {
14653 complaint (&symfile_complaints,
216f72a1 14654 _("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14655 "DW_TAG_call_site child DIE %s [in module %s]"),
14656 sect_offset_str (child_die->sect_off),
9c541725 14657 objfile_name (objfile));
96408a79
SA
14658 continue;
14659 }
14660 parameter->value = DW_BLOCK (attr)->data;
14661 parameter->value_size = DW_BLOCK (attr)->size;
14662
14663 /* Parameters are not pre-cleared by memset above. */
14664 parameter->data_value = NULL;
14665 parameter->data_value_size = 0;
14666 call_site->parameter_count++;
14667
216f72a1
JK
14668 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14669 if (attr == NULL)
14670 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
14671 if (attr)
14672 {
14673 if (!attr_form_is_block (attr))
14674 complaint (&symfile_complaints,
216f72a1 14675 _("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14676 "DW_TAG_call_site child DIE %s [in module %s]"),
14677 sect_offset_str (child_die->sect_off),
9c541725 14678 objfile_name (objfile));
96408a79
SA
14679 else
14680 {
14681 parameter->data_value = DW_BLOCK (attr)->data;
14682 parameter->data_value_size = DW_BLOCK (attr)->size;
14683 }
14684 }
14685 }
14686}
14687
71a3c369
TT
14688/* Helper function for read_variable. If DIE represents a virtual
14689 table, then return the type of the concrete object that is
14690 associated with the virtual table. Otherwise, return NULL. */
14691
14692static struct type *
14693rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14694{
14695 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14696 if (attr == NULL)
14697 return NULL;
14698
14699 /* Find the type DIE. */
14700 struct die_info *type_die = NULL;
14701 struct dwarf2_cu *type_cu = cu;
14702
14703 if (attr_form_is_ref (attr))
14704 type_die = follow_die_ref (die, attr, &type_cu);
14705 if (type_die == NULL)
14706 return NULL;
14707
14708 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14709 return NULL;
14710 return die_containing_type (type_die, type_cu);
14711}
14712
14713/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14714
14715static void
14716read_variable (struct die_info *die, struct dwarf2_cu *cu)
14717{
14718 struct rust_vtable_symbol *storage = NULL;
14719
14720 if (cu->language == language_rust)
14721 {
14722 struct type *containing_type = rust_containing_type (die, cu);
14723
14724 if (containing_type != NULL)
14725 {
518817b3 14726 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369
TT
14727
14728 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14729 struct rust_vtable_symbol);
14730 initialize_objfile_symbol (storage);
14731 storage->concrete_type = containing_type;
cf724bc9 14732 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14733 }
14734 }
14735
5e2db402 14736 new_symbol (die, NULL, cu, storage);
71a3c369
TT
14737}
14738
43988095
JK
14739/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14740 reading .debug_rnglists.
14741 Callback's type should be:
14742 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14743 Return true if the attributes are present and valid, otherwise,
14744 return false. */
14745
14746template <typename Callback>
14747static bool
14748dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14749 Callback &&callback)
14750{
ed2dc618 14751 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14752 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14753 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 14754 bfd *obfd = objfile->obfd;
43988095
JK
14755 /* Base address selection entry. */
14756 CORE_ADDR base;
14757 int found_base;
43988095 14758 const gdb_byte *buffer;
43988095
JK
14759 CORE_ADDR baseaddr;
14760 bool overflow = false;
14761
14762 found_base = cu->base_known;
14763 base = cu->base_address;
14764
14765 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14766 if (offset >= dwarf2_per_objfile->rnglists.size)
14767 {
14768 complaint (&symfile_complaints,
14769 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14770 offset);
14771 return false;
14772 }
14773 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14774
14775 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14776
14777 while (1)
14778 {
7814882a
JK
14779 /* Initialize it due to a false compiler warning. */
14780 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14781 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14782 + dwarf2_per_objfile->rnglists.size);
14783 unsigned int bytes_read;
14784
14785 if (buffer == buf_end)
14786 {
14787 overflow = true;
14788 break;
14789 }
14790 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14791 switch (rlet)
14792 {
14793 case DW_RLE_end_of_list:
14794 break;
14795 case DW_RLE_base_address:
14796 if (buffer + cu->header.addr_size > buf_end)
14797 {
14798 overflow = true;
14799 break;
14800 }
14801 base = read_address (obfd, buffer, cu, &bytes_read);
14802 found_base = 1;
14803 buffer += bytes_read;
14804 break;
14805 case DW_RLE_start_length:
14806 if (buffer + cu->header.addr_size > buf_end)
14807 {
14808 overflow = true;
14809 break;
14810 }
14811 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14812 buffer += bytes_read;
14813 range_end = (range_beginning
14814 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14815 buffer += bytes_read;
14816 if (buffer > buf_end)
14817 {
14818 overflow = true;
14819 break;
14820 }
14821 break;
14822 case DW_RLE_offset_pair:
14823 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14824 buffer += bytes_read;
14825 if (buffer > buf_end)
14826 {
14827 overflow = true;
14828 break;
14829 }
14830 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14831 buffer += bytes_read;
14832 if (buffer > buf_end)
14833 {
14834 overflow = true;
14835 break;
14836 }
14837 break;
14838 case DW_RLE_start_end:
14839 if (buffer + 2 * cu->header.addr_size > buf_end)
14840 {
14841 overflow = true;
14842 break;
14843 }
14844 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14845 buffer += bytes_read;
14846 range_end = read_address (obfd, buffer, cu, &bytes_read);
14847 buffer += bytes_read;
14848 break;
14849 default:
14850 complaint (&symfile_complaints,
14851 _("Invalid .debug_rnglists data (no base address)"));
14852 return false;
14853 }
14854 if (rlet == DW_RLE_end_of_list || overflow)
14855 break;
14856 if (rlet == DW_RLE_base_address)
14857 continue;
14858
14859 if (!found_base)
14860 {
14861 /* We have no valid base address for the ranges
14862 data. */
14863 complaint (&symfile_complaints,
14864 _("Invalid .debug_rnglists data (no base address)"));
14865 return false;
14866 }
14867
14868 if (range_beginning > range_end)
14869 {
14870 /* Inverted range entries are invalid. */
14871 complaint (&symfile_complaints,
14872 _("Invalid .debug_rnglists data (inverted range)"));
14873 return false;
14874 }
14875
14876 /* Empty range entries have no effect. */
14877 if (range_beginning == range_end)
14878 continue;
14879
14880 range_beginning += base;
14881 range_end += base;
14882
14883 /* A not-uncommon case of bad debug info.
14884 Don't pollute the addrmap with bad data. */
14885 if (range_beginning + baseaddr == 0
14886 && !dwarf2_per_objfile->has_section_at_zero)
14887 {
14888 complaint (&symfile_complaints,
14889 _(".debug_rnglists entry has start address of zero"
14890 " [in module %s]"), objfile_name (objfile));
14891 continue;
14892 }
14893
14894 callback (range_beginning, range_end);
14895 }
14896
14897 if (overflow)
14898 {
14899 complaint (&symfile_complaints,
14900 _("Offset %d is not terminated "
14901 "for DW_AT_ranges attribute"),
14902 offset);
14903 return false;
14904 }
14905
14906 return true;
14907}
14908
14909/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14910 Callback's type should be:
14911 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14912 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14913
43988095 14914template <typename Callback>
43039443 14915static int
5f46c5a5 14916dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14917 Callback &&callback)
43039443 14918{
ed2dc618 14919 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14920 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14921 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14922 struct comp_unit_head *cu_header = &cu->header;
14923 bfd *obfd = objfile->obfd;
14924 unsigned int addr_size = cu_header->addr_size;
14925 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14926 /* Base address selection entry. */
14927 CORE_ADDR base;
14928 int found_base;
14929 unsigned int dummy;
d521ce57 14930 const gdb_byte *buffer;
ff013f42 14931 CORE_ADDR baseaddr;
43039443 14932
43988095
JK
14933 if (cu_header->version >= 5)
14934 return dwarf2_rnglists_process (offset, cu, callback);
14935
d00adf39
DE
14936 found_base = cu->base_known;
14937 base = cu->base_address;
43039443 14938
be391dca 14939 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14940 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
14941 {
14942 complaint (&symfile_complaints,
14943 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14944 offset);
14945 return 0;
14946 }
dce234bc 14947 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14948
e7030f15 14949 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 14950
43039443
JK
14951 while (1)
14952 {
14953 CORE_ADDR range_beginning, range_end;
14954
14955 range_beginning = read_address (obfd, buffer, cu, &dummy);
14956 buffer += addr_size;
14957 range_end = read_address (obfd, buffer, cu, &dummy);
14958 buffer += addr_size;
14959 offset += 2 * addr_size;
14960
14961 /* An end of list marker is a pair of zero addresses. */
14962 if (range_beginning == 0 && range_end == 0)
14963 /* Found the end of list entry. */
14964 break;
14965
14966 /* Each base address selection entry is a pair of 2 values.
14967 The first is the largest possible address, the second is
14968 the base address. Check for a base address here. */
14969 if ((range_beginning & mask) == mask)
14970 {
28d2bfb9
AB
14971 /* If we found the largest possible address, then we already
14972 have the base address in range_end. */
14973 base = range_end;
43039443
JK
14974 found_base = 1;
14975 continue;
14976 }
14977
14978 if (!found_base)
14979 {
14980 /* We have no valid base address for the ranges
14981 data. */
14982 complaint (&symfile_complaints,
14983 _("Invalid .debug_ranges data (no base address)"));
14984 return 0;
14985 }
14986
9277c30c
UW
14987 if (range_beginning > range_end)
14988 {
14989 /* Inverted range entries are invalid. */
14990 complaint (&symfile_complaints,
14991 _("Invalid .debug_ranges data (inverted range)"));
14992 return 0;
14993 }
14994
14995 /* Empty range entries have no effect. */
14996 if (range_beginning == range_end)
14997 continue;
14998
43039443
JK
14999 range_beginning += base;
15000 range_end += base;
15001
01093045
DE
15002 /* A not-uncommon case of bad debug info.
15003 Don't pollute the addrmap with bad data. */
15004 if (range_beginning + baseaddr == 0
15005 && !dwarf2_per_objfile->has_section_at_zero)
15006 {
15007 complaint (&symfile_complaints,
15008 _(".debug_ranges entry has start address of zero"
4262abfb 15009 " [in module %s]"), objfile_name (objfile));
01093045
DE
15010 continue;
15011 }
15012
5f46c5a5
JK
15013 callback (range_beginning, range_end);
15014 }
15015
15016 return 1;
15017}
15018
15019/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
15020 Return 1 if the attributes are present and valid, otherwise, return 0.
15021 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
15022
15023static int
15024dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
15025 CORE_ADDR *high_return, struct dwarf2_cu *cu,
15026 struct partial_symtab *ranges_pst)
15027{
518817b3 15028 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5
JK
15029 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15030 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
15031 SECT_OFF_TEXT (objfile));
15032 int low_set = 0;
15033 CORE_ADDR low = 0;
15034 CORE_ADDR high = 0;
15035 int retval;
15036
15037 retval = dwarf2_ranges_process (offset, cu,
15038 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
15039 {
9277c30c 15040 if (ranges_pst != NULL)
3e29f34a
MR
15041 {
15042 CORE_ADDR lowpc;
15043 CORE_ADDR highpc;
15044
15045 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
15046 range_beginning + baseaddr);
15047 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
15048 range_end + baseaddr);
15049 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
15050 ranges_pst);
15051 }
ff013f42 15052
43039443
JK
15053 /* FIXME: This is recording everything as a low-high
15054 segment of consecutive addresses. We should have a
15055 data structure for discontiguous block ranges
15056 instead. */
15057 if (! low_set)
15058 {
15059 low = range_beginning;
15060 high = range_end;
15061 low_set = 1;
15062 }
15063 else
15064 {
15065 if (range_beginning < low)
15066 low = range_beginning;
15067 if (range_end > high)
15068 high = range_end;
15069 }
5f46c5a5
JK
15070 });
15071 if (!retval)
15072 return 0;
43039443
JK
15073
15074 if (! low_set)
15075 /* If the first entry is an end-of-list marker, the range
15076 describes an empty scope, i.e. no instructions. */
15077 return 0;
15078
15079 if (low_return)
15080 *low_return = low;
15081 if (high_return)
15082 *high_return = high;
15083 return 1;
15084}
15085
3a2b436a
JK
15086/* Get low and high pc attributes from a die. See enum pc_bounds_kind
15087 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 15088 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 15089
3a2b436a 15090static enum pc_bounds_kind
af34e669 15091dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
15092 CORE_ADDR *highpc, struct dwarf2_cu *cu,
15093 struct partial_symtab *pst)
c906108c 15094{
518817b3
SM
15095 struct dwarf2_per_objfile *dwarf2_per_objfile
15096 = cu->per_cu->dwarf2_per_objfile;
c906108c 15097 struct attribute *attr;
91da1414 15098 struct attribute *attr_high;
af34e669
DJ
15099 CORE_ADDR low = 0;
15100 CORE_ADDR high = 0;
e385593e 15101 enum pc_bounds_kind ret;
c906108c 15102
91da1414
MW
15103 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
15104 if (attr_high)
af34e669 15105 {
e142c38c 15106 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 15107 if (attr)
91da1414 15108 {
31aa7e4e
JB
15109 low = attr_value_as_address (attr);
15110 high = attr_value_as_address (attr_high);
15111 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
15112 high += low;
91da1414 15113 }
af34e669
DJ
15114 else
15115 /* Found high w/o low attribute. */
e385593e 15116 return PC_BOUNDS_INVALID;
af34e669
DJ
15117
15118 /* Found consecutive range of addresses. */
3a2b436a 15119 ret = PC_BOUNDS_HIGH_LOW;
af34e669 15120 }
c906108c 15121 else
af34e669 15122 {
e142c38c 15123 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
15124 if (attr != NULL)
15125 {
ab435259
DE
15126 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
15127 We take advantage of the fact that DW_AT_ranges does not appear
15128 in DW_TAG_compile_unit of DWO files. */
15129 int need_ranges_base = die->tag != DW_TAG_compile_unit;
15130 unsigned int ranges_offset = (DW_UNSND (attr)
15131 + (need_ranges_base
15132 ? cu->ranges_base
15133 : 0));
2e3cf129 15134
af34e669 15135 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 15136 .debug_ranges section. */
2e3cf129 15137 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 15138 return PC_BOUNDS_INVALID;
43039443 15139 /* Found discontinuous range of addresses. */
3a2b436a 15140 ret = PC_BOUNDS_RANGES;
af34e669 15141 }
e385593e
JK
15142 else
15143 return PC_BOUNDS_NOT_PRESENT;
af34e669 15144 }
c906108c 15145
48fbe735 15146 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 15147 if (high <= low)
e385593e 15148 return PC_BOUNDS_INVALID;
c906108c
SS
15149
15150 /* When using the GNU linker, .gnu.linkonce. sections are used to
15151 eliminate duplicate copies of functions and vtables and such.
15152 The linker will arbitrarily choose one and discard the others.
15153 The AT_*_pc values for such functions refer to local labels in
15154 these sections. If the section from that file was discarded, the
15155 labels are not in the output, so the relocs get a value of 0.
15156 If this is a discarded function, mark the pc bounds as invalid,
15157 so that GDB will ignore it. */
72dca2f5 15158 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 15159 return PC_BOUNDS_INVALID;
c906108c
SS
15160
15161 *lowpc = low;
96408a79
SA
15162 if (highpc)
15163 *highpc = high;
af34e669 15164 return ret;
c906108c
SS
15165}
15166
b084d499
JB
15167/* Assuming that DIE represents a subprogram DIE or a lexical block, get
15168 its low and high PC addresses. Do nothing if these addresses could not
15169 be determined. Otherwise, set LOWPC to the low address if it is smaller,
15170 and HIGHPC to the high address if greater than HIGHPC. */
15171
15172static void
15173dwarf2_get_subprogram_pc_bounds (struct die_info *die,
15174 CORE_ADDR *lowpc, CORE_ADDR *highpc,
15175 struct dwarf2_cu *cu)
15176{
15177 CORE_ADDR low, high;
15178 struct die_info *child = die->child;
15179
e385593e 15180 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 15181 {
325fac50
PA
15182 *lowpc = std::min (*lowpc, low);
15183 *highpc = std::max (*highpc, high);
b084d499
JB
15184 }
15185
15186 /* If the language does not allow nested subprograms (either inside
15187 subprograms or lexical blocks), we're done. */
15188 if (cu->language != language_ada)
15189 return;
6e70227d 15190
b084d499
JB
15191 /* Check all the children of the given DIE. If it contains nested
15192 subprograms, then check their pc bounds. Likewise, we need to
15193 check lexical blocks as well, as they may also contain subprogram
15194 definitions. */
15195 while (child && child->tag)
15196 {
15197 if (child->tag == DW_TAG_subprogram
15198 || child->tag == DW_TAG_lexical_block)
15199 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
15200 child = sibling_die (child);
15201 }
15202}
15203
fae299cd
DC
15204/* Get the low and high pc's represented by the scope DIE, and store
15205 them in *LOWPC and *HIGHPC. If the correct values can't be
15206 determined, set *LOWPC to -1 and *HIGHPC to 0. */
15207
15208static void
15209get_scope_pc_bounds (struct die_info *die,
15210 CORE_ADDR *lowpc, CORE_ADDR *highpc,
15211 struct dwarf2_cu *cu)
15212{
15213 CORE_ADDR best_low = (CORE_ADDR) -1;
15214 CORE_ADDR best_high = (CORE_ADDR) 0;
15215 CORE_ADDR current_low, current_high;
15216
3a2b436a 15217 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 15218 >= PC_BOUNDS_RANGES)
fae299cd
DC
15219 {
15220 best_low = current_low;
15221 best_high = current_high;
15222 }
15223 else
15224 {
15225 struct die_info *child = die->child;
15226
15227 while (child && child->tag)
15228 {
15229 switch (child->tag) {
15230 case DW_TAG_subprogram:
b084d499 15231 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
15232 break;
15233 case DW_TAG_namespace:
f55ee35c 15234 case DW_TAG_module:
fae299cd
DC
15235 /* FIXME: carlton/2004-01-16: Should we do this for
15236 DW_TAG_class_type/DW_TAG_structure_type, too? I think
15237 that current GCC's always emit the DIEs corresponding
15238 to definitions of methods of classes as children of a
15239 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
15240 the DIEs giving the declarations, which could be
15241 anywhere). But I don't see any reason why the
15242 standards says that they have to be there. */
15243 get_scope_pc_bounds (child, &current_low, &current_high, cu);
15244
15245 if (current_low != ((CORE_ADDR) -1))
15246 {
325fac50
PA
15247 best_low = std::min (best_low, current_low);
15248 best_high = std::max (best_high, current_high);
fae299cd
DC
15249 }
15250 break;
15251 default:
0963b4bd 15252 /* Ignore. */
fae299cd
DC
15253 break;
15254 }
15255
15256 child = sibling_die (child);
15257 }
15258 }
15259
15260 *lowpc = best_low;
15261 *highpc = best_high;
15262}
15263
801e3a5b
JB
15264/* Record the address ranges for BLOCK, offset by BASEADDR, as given
15265 in DIE. */
380bca97 15266
801e3a5b
JB
15267static void
15268dwarf2_record_block_ranges (struct die_info *die, struct block *block,
15269 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
15270{
518817b3 15271 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 15272 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 15273 struct attribute *attr;
91da1414 15274 struct attribute *attr_high;
801e3a5b 15275
91da1414
MW
15276 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
15277 if (attr_high)
801e3a5b 15278 {
801e3a5b
JB
15279 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
15280 if (attr)
15281 {
31aa7e4e
JB
15282 CORE_ADDR low = attr_value_as_address (attr);
15283 CORE_ADDR high = attr_value_as_address (attr_high);
15284
15285 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
15286 high += low;
9a619af0 15287
3e29f34a
MR
15288 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
15289 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
15290 record_block_range (block, low, high - 1);
801e3a5b
JB
15291 }
15292 }
15293
15294 attr = dwarf2_attr (die, DW_AT_ranges, cu);
15295 if (attr)
15296 {
ab435259
DE
15297 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
15298 We take advantage of the fact that DW_AT_ranges does not appear
15299 in DW_TAG_compile_unit of DWO files. */
15300 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
15301
15302 /* The value of the DW_AT_ranges attribute is the offset of the
15303 address range list in the .debug_ranges section. */
ab435259
DE
15304 unsigned long offset = (DW_UNSND (attr)
15305 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 15306 const gdb_byte *buffer;
801e3a5b
JB
15307
15308 /* For some target architectures, but not others, the
15309 read_address function sign-extends the addresses it returns.
15310 To recognize base address selection entries, we need a
15311 mask. */
15312 unsigned int addr_size = cu->header.addr_size;
15313 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
15314
15315 /* The base address, to which the next pair is relative. Note
15316 that this 'base' is a DWARF concept: most entries in a range
15317 list are relative, to reduce the number of relocs against the
15318 debugging information. This is separate from this function's
15319 'baseaddr' argument, which GDB uses to relocate debugging
15320 information from a shared library based on the address at
15321 which the library was loaded. */
d00adf39
DE
15322 CORE_ADDR base = cu->base_address;
15323 int base_known = cu->base_known;
801e3a5b 15324
5f46c5a5
JK
15325 dwarf2_ranges_process (offset, cu,
15326 [&] (CORE_ADDR start, CORE_ADDR end)
15327 {
58fdfd2c
JK
15328 start += baseaddr;
15329 end += baseaddr;
5f46c5a5
JK
15330 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
15331 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
15332 record_block_range (block, start, end - 1);
15333 });
801e3a5b
JB
15334 }
15335}
15336
685b1105
JK
15337/* Check whether the producer field indicates either of GCC < 4.6, or the
15338 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 15339
685b1105
JK
15340static void
15341check_producer (struct dwarf2_cu *cu)
60d5a603 15342{
38360086 15343 int major, minor;
60d5a603
JK
15344
15345 if (cu->producer == NULL)
15346 {
15347 /* For unknown compilers expect their behavior is DWARF version
15348 compliant.
15349
15350 GCC started to support .debug_types sections by -gdwarf-4 since
15351 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
15352 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
15353 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
15354 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 15355 }
b1ffba5a 15356 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 15357 {
38360086
MW
15358 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
15359 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 15360 }
5230b05a
WT
15361 else if (producer_is_icc (cu->producer, &major, &minor))
15362 cu->producer_is_icc_lt_14 = major < 14;
685b1105
JK
15363 else
15364 {
15365 /* For other non-GCC compilers, expect their behavior is DWARF version
15366 compliant. */
60d5a603
JK
15367 }
15368
ba919b58 15369 cu->checked_producer = 1;
685b1105 15370}
ba919b58 15371
685b1105
JK
15372/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
15373 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
15374 during 4.6.0 experimental. */
15375
15376static int
15377producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
15378{
15379 if (!cu->checked_producer)
15380 check_producer (cu);
15381
15382 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
15383}
15384
15385/* Return the default accessibility type if it is not overriden by
15386 DW_AT_accessibility. */
15387
15388static enum dwarf_access_attribute
15389dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
15390{
15391 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15392 {
15393 /* The default DWARF 2 accessibility for members is public, the default
15394 accessibility for inheritance is private. */
15395
15396 if (die->tag != DW_TAG_inheritance)
15397 return DW_ACCESS_public;
15398 else
15399 return DW_ACCESS_private;
15400 }
15401 else
15402 {
15403 /* DWARF 3+ defines the default accessibility a different way. The same
15404 rules apply now for DW_TAG_inheritance as for the members and it only
15405 depends on the container kind. */
15406
15407 if (die->parent->tag == DW_TAG_class_type)
15408 return DW_ACCESS_private;
15409 else
15410 return DW_ACCESS_public;
15411 }
15412}
15413
74ac6d43
TT
15414/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15415 offset. If the attribute was not found return 0, otherwise return
15416 1. If it was found but could not properly be handled, set *OFFSET
15417 to 0. */
15418
15419static int
15420handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15421 LONGEST *offset)
15422{
15423 struct attribute *attr;
15424
15425 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15426 if (attr != NULL)
15427 {
15428 *offset = 0;
15429
15430 /* Note that we do not check for a section offset first here.
15431 This is because DW_AT_data_member_location is new in DWARF 4,
15432 so if we see it, we can assume that a constant form is really
15433 a constant and not a section offset. */
15434 if (attr_form_is_constant (attr))
15435 *offset = dwarf2_get_attr_constant_value (attr, 0);
15436 else if (attr_form_is_section_offset (attr))
15437 dwarf2_complex_location_expr_complaint ();
15438 else if (attr_form_is_block (attr))
15439 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15440 else
15441 dwarf2_complex_location_expr_complaint ();
15442
15443 return 1;
15444 }
15445
15446 return 0;
15447}
15448
c906108c
SS
15449/* Add an aggregate field to the field list. */
15450
15451static void
107d2387 15452dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 15453 struct dwarf2_cu *cu)
6e70227d 15454{
518817b3 15455 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 15456 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
15457 struct nextfield *new_field;
15458 struct attribute *attr;
15459 struct field *fp;
15d034d0 15460 const char *fieldname = "";
c906108c 15461
7d0ccb61
DJ
15462 if (die->tag == DW_TAG_inheritance)
15463 {
be2daae6
TT
15464 fip->baseclasses.emplace_back ();
15465 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
15466 }
15467 else
15468 {
be2daae6
TT
15469 fip->fields.emplace_back ();
15470 new_field = &fip->fields.back ();
7d0ccb61 15471 }
be2daae6 15472
c906108c
SS
15473 fip->nfields++;
15474
e142c38c 15475 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
15476 if (attr)
15477 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
15478 else
15479 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
15480 if (new_field->accessibility != DW_ACCESS_public)
15481 fip->non_public_fields = 1;
60d5a603 15482
e142c38c 15483 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
15484 if (attr)
15485 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
15486 else
15487 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15488
15489 fp = &new_field->field;
a9a9bd0f 15490
e142c38c 15491 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15492 {
74ac6d43
TT
15493 LONGEST offset;
15494
a9a9bd0f 15495 /* Data member other than a C++ static data member. */
6e70227d 15496
c906108c 15497 /* Get type of field. */
e7c27a73 15498 fp->type = die_type (die, cu);
c906108c 15499
d6a843b5 15500 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15501
c906108c 15502 /* Get bit size of field (zero if none). */
e142c38c 15503 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
15504 if (attr)
15505 {
15506 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15507 }
15508 else
15509 {
15510 FIELD_BITSIZE (*fp) = 0;
15511 }
15512
15513 /* Get bit offset of field. */
74ac6d43
TT
15514 if (handle_data_member_location (die, cu, &offset))
15515 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 15516 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
15517 if (attr)
15518 {
5e2b427d 15519 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
15520 {
15521 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
15522 additional bit offset from the MSB of the containing
15523 anonymous object to the MSB of the field. We don't
15524 have to do anything special since we don't need to
15525 know the size of the anonymous object. */
f41f5e61 15526 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
15527 }
15528 else
15529 {
15530 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
15531 MSB of the anonymous object, subtract off the number of
15532 bits from the MSB of the field to the MSB of the
15533 object, and then subtract off the number of bits of
15534 the field itself. The result is the bit offset of
15535 the LSB of the field. */
c906108c
SS
15536 int anonymous_size;
15537 int bit_offset = DW_UNSND (attr);
15538
e142c38c 15539 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15540 if (attr)
15541 {
15542 /* The size of the anonymous object containing
15543 the bit field is explicit, so use the
15544 indicated size (in bytes). */
15545 anonymous_size = DW_UNSND (attr);
15546 }
15547 else
15548 {
15549 /* The size of the anonymous object containing
15550 the bit field must be inferred from the type
15551 attribute of the data member containing the
15552 bit field. */
15553 anonymous_size = TYPE_LENGTH (fp->type);
15554 }
f41f5e61
PA
15555 SET_FIELD_BITPOS (*fp,
15556 (FIELD_BITPOS (*fp)
15557 + anonymous_size * bits_per_byte
15558 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15559 }
15560 }
da5b30da
AA
15561 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15562 if (attr != NULL)
15563 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15564 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
15565
15566 /* Get name of field. */
39cbfefa
DJ
15567 fieldname = dwarf2_name (die, cu);
15568 if (fieldname == NULL)
15569 fieldname = "";
d8151005
DJ
15570
15571 /* The name is already allocated along with this objfile, so we don't
15572 need to duplicate it for the type. */
15573 fp->name = fieldname;
c906108c
SS
15574
15575 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15576 pointer or virtual base class pointer) to private. */
e142c38c 15577 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15578 {
d48cc9dd 15579 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
15580 new_field->accessibility = DW_ACCESS_private;
15581 fip->non_public_fields = 1;
15582 }
15583 }
a9a9bd0f 15584 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15585 {
a9a9bd0f
DC
15586 /* C++ static member. */
15587
15588 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15589 is a declaration, but all versions of G++ as of this writing
15590 (so through at least 3.2.1) incorrectly generate
15591 DW_TAG_variable tags. */
6e70227d 15592
ff355380 15593 const char *physname;
c906108c 15594
a9a9bd0f 15595 /* Get name of field. */
39cbfefa
DJ
15596 fieldname = dwarf2_name (die, cu);
15597 if (fieldname == NULL)
c906108c
SS
15598 return;
15599
254e6b9e 15600 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15601 if (attr
15602 /* Only create a symbol if this is an external value.
15603 new_symbol checks this and puts the value in the global symbol
15604 table, which we want. If it is not external, new_symbol
15605 will try to put the value in cu->list_in_scope which is wrong. */
15606 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15607 {
15608 /* A static const member, not much different than an enum as far as
15609 we're concerned, except that we can support more types. */
15610 new_symbol (die, NULL, cu);
15611 }
15612
2df3850c 15613 /* Get physical name. */
ff355380 15614 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15615
d8151005
DJ
15616 /* The name is already allocated along with this objfile, so we don't
15617 need to duplicate it for the type. */
15618 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15619 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15620 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15621 }
15622 else if (die->tag == DW_TAG_inheritance)
15623 {
74ac6d43 15624 LONGEST offset;
d4b96c9a 15625
74ac6d43
TT
15626 /* C++ base class field. */
15627 if (handle_data_member_location (die, cu, &offset))
15628 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15629 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15630 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c 15631 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
c906108c 15632 }
2ddeaf8a
TT
15633 else if (die->tag == DW_TAG_variant_part)
15634 {
15635 /* process_structure_scope will treat this DIE as a union. */
15636 process_structure_scope (die, cu);
15637
15638 /* The variant part is relative to the start of the enclosing
15639 structure. */
15640 SET_FIELD_BITPOS (*fp, 0);
15641 fp->type = get_die_type (die, cu);
15642 fp->artificial = 1;
15643 fp->name = "<<variant>>";
15644 }
15645 else
15646 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15647}
15648
883fd55a
KS
15649/* Can the type given by DIE define another type? */
15650
15651static bool
15652type_can_define_types (const struct die_info *die)
15653{
15654 switch (die->tag)
15655 {
15656 case DW_TAG_typedef:
15657 case DW_TAG_class_type:
15658 case DW_TAG_structure_type:
15659 case DW_TAG_union_type:
15660 case DW_TAG_enumeration_type:
15661 return true;
15662
15663 default:
15664 return false;
15665 }
15666}
15667
15668/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15669
15670static void
883fd55a
KS
15671dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15672 struct dwarf2_cu *cu)
6e70227d 15673{
be2daae6
TT
15674 struct decl_field fp;
15675 memset (&fp, 0, sizeof (fp));
98751a41 15676
883fd55a 15677 gdb_assert (type_can_define_types (die));
98751a41 15678
883fd55a 15679 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15680 fp.name = dwarf2_name (die, cu);
15681 fp.type = read_type_die (die, cu);
98751a41 15682
c191a687
KS
15683 /* Save accessibility. */
15684 enum dwarf_access_attribute accessibility;
15685 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15686 if (attr != NULL)
15687 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15688 else
15689 accessibility = dwarf2_default_access_attribute (die, cu);
15690 switch (accessibility)
15691 {
15692 case DW_ACCESS_public:
15693 /* The assumed value if neither private nor protected. */
15694 break;
15695 case DW_ACCESS_private:
be2daae6 15696 fp.is_private = 1;
c191a687
KS
15697 break;
15698 case DW_ACCESS_protected:
be2daae6 15699 fp.is_protected = 1;
c191a687
KS
15700 break;
15701 default:
37534686
KS
15702 complaint (&symfile_complaints,
15703 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15704 }
15705
883fd55a 15706 if (die->tag == DW_TAG_typedef)
be2daae6 15707 fip->typedef_field_list.push_back (fp);
883fd55a 15708 else
be2daae6 15709 fip->nested_types_list.push_back (fp);
98751a41
JK
15710}
15711
c906108c
SS
15712/* Create the vector of fields, and attach it to the type. */
15713
15714static void
fba45db2 15715dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15716 struct dwarf2_cu *cu)
c906108c
SS
15717{
15718 int nfields = fip->nfields;
15719
15720 /* Record the field count, allocate space for the array of fields,
15721 and create blank accessibility bitfields if necessary. */
15722 TYPE_NFIELDS (type) = nfields;
15723 TYPE_FIELDS (type) = (struct field *)
be2daae6 15724 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 15725
b4ba55a1 15726 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15727 {
15728 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15729
15730 TYPE_FIELD_PRIVATE_BITS (type) =
15731 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15732 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15733
15734 TYPE_FIELD_PROTECTED_BITS (type) =
15735 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15736 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15737
774b6a14
TT
15738 TYPE_FIELD_IGNORE_BITS (type) =
15739 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15740 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15741 }
15742
15743 /* If the type has baseclasses, allocate and clear a bit vector for
15744 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15745 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15746 {
be2daae6 15747 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15748 unsigned char *pointer;
c906108c
SS
15749
15750 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15751 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15752 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15753 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15754 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15755 }
15756
2ddeaf8a
TT
15757 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15758 {
15759 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15760
be2daae6 15761 for (int index = 0; index < nfields; ++index)
2ddeaf8a 15762 {
be2daae6
TT
15763 struct nextfield &field = fip->fields[index];
15764
15765 if (field.variant.is_discriminant)
2ddeaf8a 15766 di->discriminant_index = index;
be2daae6 15767 else if (field.variant.default_branch)
2ddeaf8a
TT
15768 di->default_index = index;
15769 else
be2daae6 15770 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
15771 }
15772 }
15773
be2daae6
TT
15774 /* Copy the saved-up fields into the field vector. */
15775 for (int i = 0; i < nfields; ++i)
c906108c 15776 {
be2daae6
TT
15777 struct nextfield &field
15778 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15779 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15780
be2daae6
TT
15781 TYPE_FIELD (type, i) = field.field;
15782 switch (field.accessibility)
c906108c 15783 {
c5aa993b 15784 case DW_ACCESS_private:
b4ba55a1 15785 if (cu->language != language_ada)
be2daae6 15786 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15787 break;
c906108c 15788
c5aa993b 15789 case DW_ACCESS_protected:
b4ba55a1 15790 if (cu->language != language_ada)
be2daae6 15791 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15792 break;
c906108c 15793
c5aa993b
JM
15794 case DW_ACCESS_public:
15795 break;
c906108c 15796
c5aa993b
JM
15797 default:
15798 /* Unknown accessibility. Complain and treat it as public. */
15799 {
e2e0b3e5 15800 complaint (&symfile_complaints, _("unsupported accessibility %d"),
be2daae6 15801 field.accessibility);
c5aa993b
JM
15802 }
15803 break;
c906108c 15804 }
be2daae6 15805 if (i < fip->baseclasses.size ())
c906108c 15806 {
be2daae6 15807 switch (field.virtuality)
c906108c 15808 {
c5aa993b
JM
15809 case DW_VIRTUALITY_virtual:
15810 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15811 if (cu->language == language_ada)
a73c6dcd 15812 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15813 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15814 break;
c906108c
SS
15815 }
15816 }
c906108c
SS
15817 }
15818}
15819
7d27a96d
TT
15820/* Return true if this member function is a constructor, false
15821 otherwise. */
15822
15823static int
15824dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15825{
15826 const char *fieldname;
fe978cb0 15827 const char *type_name;
7d27a96d
TT
15828 int len;
15829
15830 if (die->parent == NULL)
15831 return 0;
15832
15833 if (die->parent->tag != DW_TAG_structure_type
15834 && die->parent->tag != DW_TAG_union_type
15835 && die->parent->tag != DW_TAG_class_type)
15836 return 0;
15837
15838 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15839 type_name = dwarf2_name (die->parent, cu);
15840 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15841 return 0;
15842
15843 len = strlen (fieldname);
fe978cb0
PA
15844 return (strncmp (fieldname, type_name, len) == 0
15845 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15846}
15847
c906108c
SS
15848/* Add a member function to the proper fieldlist. */
15849
15850static void
107d2387 15851dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15852 struct type *type, struct dwarf2_cu *cu)
c906108c 15853{
518817b3 15854 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15855 struct attribute *attr;
c906108c 15856 int i;
be2daae6 15857 struct fnfieldlist *flp = nullptr;
c906108c 15858 struct fn_field *fnp;
15d034d0 15859 const char *fieldname;
f792889a 15860 struct type *this_type;
60d5a603 15861 enum dwarf_access_attribute accessibility;
c906108c 15862
b4ba55a1 15863 if (cu->language == language_ada)
a73c6dcd 15864 error (_("unexpected member function in Ada type"));
b4ba55a1 15865
2df3850c 15866 /* Get name of member function. */
39cbfefa
DJ
15867 fieldname = dwarf2_name (die, cu);
15868 if (fieldname == NULL)
2df3850c 15869 return;
c906108c 15870
c906108c 15871 /* Look up member function name in fieldlist. */
be2daae6 15872 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15873 {
27bfe10e 15874 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15875 {
15876 flp = &fip->fnfieldlists[i];
15877 break;
15878 }
c906108c
SS
15879 }
15880
be2daae6
TT
15881 /* Create a new fnfieldlist if necessary. */
15882 if (flp == nullptr)
c906108c 15883 {
be2daae6
TT
15884 fip->fnfieldlists.emplace_back ();
15885 flp = &fip->fnfieldlists.back ();
c906108c 15886 flp->name = fieldname;
be2daae6 15887 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15888 }
15889
be2daae6
TT
15890 /* Create a new member function field and add it to the vector of
15891 fnfieldlists. */
15892 flp->fnfields.emplace_back ();
15893 fnp = &flp->fnfields.back ();
3da10d80
KS
15894
15895 /* Delay processing of the physname until later. */
9c37b5ae 15896 if (cu->language == language_cplus)
be2daae6
TT
15897 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15898 die, cu);
3da10d80
KS
15899 else
15900 {
1d06ead6 15901 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15902 fnp->physname = physname ? physname : "";
15903 }
15904
c906108c 15905 fnp->type = alloc_type (objfile);
f792889a
DJ
15906 this_type = read_type_die (die, cu);
15907 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15908 {
f792889a 15909 int nparams = TYPE_NFIELDS (this_type);
c906108c 15910
f792889a 15911 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15912 of the method itself (TYPE_CODE_METHOD). */
15913 smash_to_method_type (fnp->type, type,
f792889a
DJ
15914 TYPE_TARGET_TYPE (this_type),
15915 TYPE_FIELDS (this_type),
15916 TYPE_NFIELDS (this_type),
15917 TYPE_VARARGS (this_type));
c906108c
SS
15918
15919 /* Handle static member functions.
c5aa993b 15920 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15921 member functions. G++ helps GDB by marking the first
15922 parameter for non-static member functions (which is the this
15923 pointer) as artificial. We obtain this information from
15924 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15925 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15926 fnp->voffset = VOFFSET_STATIC;
15927 }
15928 else
e2e0b3e5 15929 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 15930 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15931
15932 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15933 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15934 fnp->fcontext = die_containing_type (die, cu);
c906108c 15935
3e43a32a
MS
15936 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15937 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15938
15939 /* Get accessibility. */
e142c38c 15940 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 15941 if (attr)
aead7601 15942 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15943 else
15944 accessibility = dwarf2_default_access_attribute (die, cu);
15945 switch (accessibility)
c906108c 15946 {
60d5a603
JK
15947 case DW_ACCESS_private:
15948 fnp->is_private = 1;
15949 break;
15950 case DW_ACCESS_protected:
15951 fnp->is_protected = 1;
15952 break;
c906108c
SS
15953 }
15954
b02dede2 15955 /* Check for artificial methods. */
e142c38c 15956 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15957 if (attr && DW_UNSND (attr) != 0)
15958 fnp->is_artificial = 1;
15959
7d27a96d
TT
15960 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15961
0d564a31 15962 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15963 function. For older versions of GCC, this is an offset in the
15964 appropriate virtual table, as specified by DW_AT_containing_type.
15965 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15966 to the object address. */
15967
e142c38c 15968 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 15969 if (attr)
8e19ed76 15970 {
aec5aa8b 15971 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15972 {
aec5aa8b
TT
15973 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15974 {
15975 /* Old-style GCC. */
15976 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15977 }
15978 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15979 || (DW_BLOCK (attr)->size > 1
15980 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15981 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15982 {
aec5aa8b
TT
15983 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15984 if ((fnp->voffset % cu->header.addr_size) != 0)
15985 dwarf2_complex_location_expr_complaint ();
15986 else
15987 fnp->voffset /= cu->header.addr_size;
15988 fnp->voffset += 2;
15989 }
15990 else
15991 dwarf2_complex_location_expr_complaint ();
15992
15993 if (!fnp->fcontext)
7e993ebf
KS
15994 {
15995 /* If there is no `this' field and no DW_AT_containing_type,
15996 we cannot actually find a base class context for the
15997 vtable! */
15998 if (TYPE_NFIELDS (this_type) == 0
15999 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
16000 {
16001 complaint (&symfile_complaints,
16002 _("cannot determine context for virtual member "
9d8780f0
SM
16003 "function \"%s\" (offset %s)"),
16004 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
16005 }
16006 else
16007 {
16008 fnp->fcontext
16009 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
16010 }
16011 }
aec5aa8b 16012 }
3690dd37 16013 else if (attr_form_is_section_offset (attr))
8e19ed76 16014 {
4d3c2250 16015 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
16016 }
16017 else
16018 {
4d3c2250
KB
16019 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
16020 fieldname);
8e19ed76 16021 }
0d564a31 16022 }
d48cc9dd
DJ
16023 else
16024 {
16025 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
16026 if (attr && DW_UNSND (attr))
16027 {
16028 /* GCC does this, as of 2008-08-25; PR debug/37237. */
16029 complaint (&symfile_complaints,
9d8780f0 16030 _("Member function \"%s\" (offset %s) is virtual "
3e43a32a 16031 "but the vtable offset is not specified"),
9d8780f0 16032 fieldname, sect_offset_str (die->sect_off));
9655fd1a 16033 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
16034 TYPE_CPLUS_DYNAMIC (type) = 1;
16035 }
16036 }
c906108c
SS
16037}
16038
16039/* Create the vector of member function fields, and attach it to the type. */
16040
16041static void
fba45db2 16042dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 16043 struct dwarf2_cu *cu)
c906108c 16044{
b4ba55a1 16045 if (cu->language == language_ada)
a73c6dcd 16046 error (_("unexpected member functions in Ada type"));
b4ba55a1 16047
c906108c
SS
16048 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16049 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
16050 TYPE_ALLOC (type,
16051 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 16052
be2daae6 16053 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 16054 {
be2daae6 16055 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 16056 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 16057
be2daae6
TT
16058 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
16059 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 16060 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
16061 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
16062
16063 for (int k = 0; k < nf.fnfields.size (); ++k)
16064 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
16065 }
16066
be2daae6 16067 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
16068}
16069
1168df01
JB
16070/* Returns non-zero if NAME is the name of a vtable member in CU's
16071 language, zero otherwise. */
16072static int
16073is_vtable_name (const char *name, struct dwarf2_cu *cu)
16074{
16075 static const char vptr[] = "_vptr";
16076
9c37b5ae
TT
16077 /* Look for the C++ form of the vtable. */
16078 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
16079 return 1;
16080
16081 return 0;
16082}
16083
c0dd20ea 16084/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
16085 functions, with the ABI-specified layout. If TYPE describes
16086 such a structure, smash it into a member function type.
61049d3b
DJ
16087
16088 GCC shouldn't do this; it should just output pointer to member DIEs.
16089 This is GCC PR debug/28767. */
c0dd20ea 16090
0b92b5bb
TT
16091static void
16092quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 16093{
09e2d7c7 16094 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
16095
16096 /* Check for a structure with no name and two children. */
0b92b5bb
TT
16097 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
16098 return;
c0dd20ea
DJ
16099
16100 /* Check for __pfn and __delta members. */
0b92b5bb
TT
16101 if (TYPE_FIELD_NAME (type, 0) == NULL
16102 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
16103 || TYPE_FIELD_NAME (type, 1) == NULL
16104 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
16105 return;
c0dd20ea
DJ
16106
16107 /* Find the type of the method. */
0b92b5bb 16108 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
16109 if (pfn_type == NULL
16110 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
16111 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 16112 return;
c0dd20ea
DJ
16113
16114 /* Look for the "this" argument. */
16115 pfn_type = TYPE_TARGET_TYPE (pfn_type);
16116 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 16117 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 16118 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 16119 return;
c0dd20ea 16120
09e2d7c7 16121 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 16122 new_type = alloc_type (objfile);
09e2d7c7 16123 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
16124 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
16125 TYPE_VARARGS (pfn_type));
0b92b5bb 16126 smash_to_methodptr_type (type, new_type);
c0dd20ea 16127}
1168df01 16128
685b1105 16129
c906108c 16130/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
16131 (definition) to create a type for the structure or union. Fill in
16132 the type's name and general properties; the members will not be
83655187
DE
16133 processed until process_structure_scope. A symbol table entry for
16134 the type will also not be done until process_structure_scope (assuming
16135 the type has a name).
c906108c 16136
c767944b
DJ
16137 NOTE: we need to call these functions regardless of whether or not the
16138 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 16139 structure or union. This gets the type entered into our set of
83655187 16140 user defined types. */
c906108c 16141
f792889a 16142static struct type *
134d01f1 16143read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16144{
518817b3 16145 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
16146 struct type *type;
16147 struct attribute *attr;
15d034d0 16148 const char *name;
c906108c 16149
348e048f
DE
16150 /* If the definition of this type lives in .debug_types, read that type.
16151 Don't follow DW_AT_specification though, that will take us back up
16152 the chain and we want to go down. */
45e58e77 16153 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
16154 if (attr)
16155 {
ac9ec31b 16156 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16157
ac9ec31b 16158 /* The type's CU may not be the same as CU.
02142a6c 16159 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16160 return set_die_type (die, type, cu);
16161 }
16162
c0dd20ea 16163 type = alloc_type (objfile);
c906108c 16164 INIT_CPLUS_SPECIFIC (type);
93311388 16165
39cbfefa
DJ
16166 name = dwarf2_name (die, cu);
16167 if (name != NULL)
c906108c 16168 {
987504bb 16169 if (cu->language == language_cplus
c44af4eb
TT
16170 || cu->language == language_d
16171 || cu->language == language_rust)
63d06c5c 16172 {
15d034d0 16173 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
16174
16175 /* dwarf2_full_name might have already finished building the DIE's
16176 type. If so, there is no need to continue. */
16177 if (get_die_type (die, cu) != NULL)
16178 return get_die_type (die, cu);
16179
16180 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
16181 if (die->tag == DW_TAG_structure_type
16182 || die->tag == DW_TAG_class_type)
16183 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
16184 }
16185 else
16186 {
d8151005
DJ
16187 /* The name is already allocated along with this objfile, so
16188 we don't need to duplicate it for the type. */
7d455152 16189 TYPE_TAG_NAME (type) = name;
94af9270
KS
16190 if (die->tag == DW_TAG_class_type)
16191 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 16192 }
c906108c
SS
16193 }
16194
16195 if (die->tag == DW_TAG_structure_type)
16196 {
16197 TYPE_CODE (type) = TYPE_CODE_STRUCT;
16198 }
16199 else if (die->tag == DW_TAG_union_type)
16200 {
16201 TYPE_CODE (type) = TYPE_CODE_UNION;
16202 }
2ddeaf8a
TT
16203 else if (die->tag == DW_TAG_variant_part)
16204 {
16205 TYPE_CODE (type) = TYPE_CODE_UNION;
16206 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
16207 }
c906108c
SS
16208 else
16209 {
4753d33b 16210 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
16211 }
16212
0cc2414c
TT
16213 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16214 TYPE_DECLARED_CLASS (type) = 1;
16215
e142c38c 16216 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16217 if (attr)
16218 {
155bfbd3
JB
16219 if (attr_form_is_constant (attr))
16220 TYPE_LENGTH (type) = DW_UNSND (attr);
16221 else
16222 {
16223 /* For the moment, dynamic type sizes are not supported
16224 by GDB's struct type. The actual size is determined
16225 on-demand when resolving the type of a given object,
16226 so set the type's length to zero for now. Otherwise,
16227 we record an expression as the length, and that expression
16228 could lead to a very large value, which could eventually
16229 lead to us trying to allocate that much memory when creating
16230 a value of that type. */
16231 TYPE_LENGTH (type) = 0;
16232 }
c906108c
SS
16233 }
16234 else
16235 {
16236 TYPE_LENGTH (type) = 0;
16237 }
16238
5230b05a 16239 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 16240 {
5230b05a
WT
16241 /* ICC<14 does not output the required DW_AT_declaration on
16242 incomplete types, but gives them a size of zero. */
422b1cb0 16243 TYPE_STUB (type) = 1;
685b1105
JK
16244 }
16245 else
16246 TYPE_STUB_SUPPORTED (type) = 1;
16247
dc718098 16248 if (die_is_declaration (die, cu))
876cecd0 16249 TYPE_STUB (type) = 1;
a6c727b2
DJ
16250 else if (attr == NULL && die->child == NULL
16251 && producer_is_realview (cu->producer))
16252 /* RealView does not output the required DW_AT_declaration
16253 on incomplete types. */
16254 TYPE_STUB (type) = 1;
dc718098 16255
c906108c
SS
16256 /* We need to add the type field to the die immediately so we don't
16257 infinitely recurse when dealing with pointers to the structure
0963b4bd 16258 type within the structure itself. */
1c379e20 16259 set_die_type (die, type, cu);
c906108c 16260
7e314c57
JK
16261 /* set_die_type should be already done. */
16262 set_descriptive_type (type, die, cu);
16263
c767944b
DJ
16264 return type;
16265}
16266
2ddeaf8a
TT
16267/* A helper for process_structure_scope that handles a single member
16268 DIE. */
16269
16270static void
16271handle_struct_member_die (struct die_info *child_die, struct type *type,
16272 struct field_info *fi,
16273 std::vector<struct symbol *> *template_args,
16274 struct dwarf2_cu *cu)
16275{
16276 if (child_die->tag == DW_TAG_member
16277 || child_die->tag == DW_TAG_variable
16278 || child_die->tag == DW_TAG_variant_part)
16279 {
16280 /* NOTE: carlton/2002-11-05: A C++ static data member
16281 should be a DW_TAG_member that is a declaration, but
16282 all versions of G++ as of this writing (so through at
16283 least 3.2.1) incorrectly generate DW_TAG_variable
16284 tags for them instead. */
16285 dwarf2_add_field (fi, child_die, cu);
16286 }
16287 else if (child_die->tag == DW_TAG_subprogram)
16288 {
16289 /* Rust doesn't have member functions in the C++ sense.
16290 However, it does emit ordinary functions as children
16291 of a struct DIE. */
16292 if (cu->language == language_rust)
16293 read_func_scope (child_die, cu);
16294 else
16295 {
16296 /* C++ member function. */
16297 dwarf2_add_member_fn (fi, child_die, type, cu);
16298 }
16299 }
16300 else if (child_die->tag == DW_TAG_inheritance)
16301 {
16302 /* C++ base class field. */
16303 dwarf2_add_field (fi, child_die, cu);
16304 }
16305 else if (type_can_define_types (child_die))
16306 dwarf2_add_type_defn (fi, child_die, cu);
16307 else if (child_die->tag == DW_TAG_template_type_param
16308 || child_die->tag == DW_TAG_template_value_param)
16309 {
16310 struct symbol *arg = new_symbol (child_die, NULL, cu);
16311
16312 if (arg != NULL)
16313 template_args->push_back (arg);
16314 }
16315 else if (child_die->tag == DW_TAG_variant)
16316 {
16317 /* In a variant we want to get the discriminant and also add a
16318 field for our sole member child. */
16319 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
16320
16321 for (struct die_info *variant_child = child_die->child;
16322 variant_child != NULL;
16323 variant_child = sibling_die (variant_child))
16324 {
16325 if (variant_child->tag == DW_TAG_member)
16326 {
16327 handle_struct_member_die (variant_child, type, fi,
16328 template_args, cu);
16329 /* Only handle the one. */
16330 break;
16331 }
16332 }
16333
16334 /* We don't handle this but we might as well report it if we see
16335 it. */
16336 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
16337 complaint (&symfile_complaints,
16338 _("DW_AT_discr_list is not supported yet"
16339 " - DIE at %s [in module %s]"),
16340 sect_offset_str (child_die->sect_off),
16341 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16342
16343 /* The first field was just added, so we can stash the
16344 discriminant there. */
be2daae6 16345 gdb_assert (!fi->fields.empty ());
2ddeaf8a 16346 if (discr == NULL)
be2daae6 16347 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 16348 else
be2daae6 16349 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
16350 }
16351}
16352
c767944b
DJ
16353/* Finish creating a structure or union type, including filling in
16354 its members and creating a symbol for it. */
16355
16356static void
16357process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16358{
518817b3 16359 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 16360 struct die_info *child_die;
c767944b
DJ
16361 struct type *type;
16362
16363 type = get_die_type (die, cu);
16364 if (type == NULL)
16365 type = read_structure_type (die, cu);
16366
2ddeaf8a
TT
16367 /* When reading a DW_TAG_variant_part, we need to notice when we
16368 read the discriminant member, so we can record it later in the
16369 discriminant_info. */
16370 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
16371 sect_offset discr_offset;
16372
16373 if (is_variant_part)
16374 {
16375 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16376 if (discr == NULL)
16377 {
16378 /* Maybe it's a univariant form, an extension we support.
16379 In this case arrange not to check the offset. */
16380 is_variant_part = false;
16381 }
16382 else if (attr_form_is_ref (discr))
16383 {
16384 struct dwarf2_cu *target_cu = cu;
16385 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16386
16387 discr_offset = target_die->sect_off;
16388 }
16389 else
16390 {
16391 complaint (&symfile_complaints,
16392 _("DW_AT_discr does not have DIE reference form"
16393 " - DIE at %s [in module %s]"),
16394 sect_offset_str (die->sect_off),
16395 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16396 is_variant_part = false;
16397 }
16398 }
16399
e142c38c 16400 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16401 {
16402 struct field_info fi;
2f4732b0 16403 std::vector<struct symbol *> template_args;
c906108c 16404
639d11d3 16405 child_die = die->child;
c906108c
SS
16406
16407 while (child_die && child_die->tag)
16408 {
2ddeaf8a 16409 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 16410
2ddeaf8a 16411 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 16412 fi.fields.back ().variant.is_discriminant = true;
34eaf542 16413
c906108c
SS
16414 child_die = sibling_die (child_die);
16415 }
16416
34eaf542 16417 /* Attach template arguments to type. */
2f4732b0 16418 if (!template_args.empty ())
34eaf542
TT
16419 {
16420 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16421 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16422 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16423 = XOBNEWVEC (&objfile->objfile_obstack,
16424 struct symbol *,
16425 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16426 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16427 template_args.data (),
34eaf542
TT
16428 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16429 * sizeof (struct symbol *)));
34eaf542
TT
16430 }
16431
c906108c
SS
16432 /* Attach fields and member functions to the type. */
16433 if (fi.nfields)
e7c27a73 16434 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16435 if (!fi.fnfieldlists.empty ())
c906108c 16436 {
e7c27a73 16437 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16438
c5aa993b 16439 /* Get the type which refers to the base class (possibly this
c906108c 16440 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16441 class from the DW_AT_containing_type attribute. This use of
16442 DW_AT_containing_type is a GNU extension. */
c906108c 16443
e142c38c 16444 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16445 {
e7c27a73 16446 struct type *t = die_containing_type (die, cu);
c906108c 16447
ae6ae975 16448 set_type_vptr_basetype (type, t);
c906108c
SS
16449 if (type == t)
16450 {
c906108c
SS
16451 int i;
16452
16453 /* Our own class provides vtbl ptr. */
16454 for (i = TYPE_NFIELDS (t) - 1;
16455 i >= TYPE_N_BASECLASSES (t);
16456 --i)
16457 {
0d5cff50 16458 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16459
1168df01 16460 if (is_vtable_name (fieldname, cu))
c906108c 16461 {
ae6ae975 16462 set_type_vptr_fieldno (type, i);
c906108c
SS
16463 break;
16464 }
16465 }
16466
16467 /* Complain if virtual function table field not found. */
16468 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 16469 complaint (&symfile_complaints,
3e43a32a
MS
16470 _("virtual function table pointer "
16471 "not found when defining class '%s'"),
4d3c2250
KB
16472 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
16473 "");
c906108c
SS
16474 }
16475 else
16476 {
ae6ae975 16477 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16478 }
16479 }
f6235d4c 16480 else if (cu->producer
61012eef 16481 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16482 {
16483 /* The IBM XLC compiler does not provide direct indication
16484 of the containing type, but the vtable pointer is
16485 always named __vfp. */
16486
16487 int i;
16488
16489 for (i = TYPE_NFIELDS (type) - 1;
16490 i >= TYPE_N_BASECLASSES (type);
16491 --i)
16492 {
16493 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16494 {
ae6ae975
DE
16495 set_type_vptr_fieldno (type, i);
16496 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16497 break;
16498 }
16499 }
16500 }
c906108c 16501 }
98751a41
JK
16502
16503 /* Copy fi.typedef_field_list linked list elements content into the
16504 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16505 if (!fi.typedef_field_list.empty ())
98751a41 16506 {
be2daae6 16507 int count = fi.typedef_field_list.size ();
98751a41 16508
a0d7a4ff 16509 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16510 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16511 = ((struct decl_field *)
be2daae6
TT
16512 TYPE_ALLOC (type,
16513 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16514 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16515
be2daae6
TT
16516 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16517 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16518 }
c767944b 16519
883fd55a
KS
16520 /* Copy fi.nested_types_list linked list elements content into the
16521 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16522 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16523 {
be2daae6 16524 int count = fi.nested_types_list.size ();
883fd55a
KS
16525
16526 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16527 TYPE_NESTED_TYPES_ARRAY (type)
16528 = ((struct decl_field *)
be2daae6
TT
16529 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16530 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16531
be2daae6
TT
16532 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16533 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16534 }
c906108c 16535 }
63d06c5c 16536
bb5ed363 16537 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16538 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16539 cu->rust_unions.push_back (type);
0b92b5bb 16540
90aeadfc
DC
16541 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16542 snapshots) has been known to create a die giving a declaration
16543 for a class that has, as a child, a die giving a definition for a
16544 nested class. So we have to process our children even if the
16545 current die is a declaration. Normally, of course, a declaration
16546 won't have any children at all. */
134d01f1 16547
ca040673
DE
16548 child_die = die->child;
16549
90aeadfc
DC
16550 while (child_die != NULL && child_die->tag)
16551 {
16552 if (child_die->tag == DW_TAG_member
16553 || child_die->tag == DW_TAG_variable
34eaf542
TT
16554 || child_die->tag == DW_TAG_inheritance
16555 || child_die->tag == DW_TAG_template_value_param
16556 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16557 {
90aeadfc 16558 /* Do nothing. */
134d01f1 16559 }
90aeadfc
DC
16560 else
16561 process_die (child_die, cu);
134d01f1 16562
90aeadfc 16563 child_die = sibling_die (child_die);
134d01f1
DJ
16564 }
16565
fa4028e9
JB
16566 /* Do not consider external references. According to the DWARF standard,
16567 these DIEs are identified by the fact that they have no byte_size
16568 attribute, and a declaration attribute. */
16569 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16570 || !die_is_declaration (die, cu))
c767944b 16571 new_symbol (die, type, cu);
134d01f1
DJ
16572}
16573
55426c9d
JB
16574/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16575 update TYPE using some information only available in DIE's children. */
16576
16577static void
16578update_enumeration_type_from_children (struct die_info *die,
16579 struct type *type,
16580 struct dwarf2_cu *cu)
16581{
60f7655a 16582 struct die_info *child_die;
55426c9d
JB
16583 int unsigned_enum = 1;
16584 int flag_enum = 1;
16585 ULONGEST mask = 0;
55426c9d 16586
8268c778 16587 auto_obstack obstack;
55426c9d 16588
60f7655a
DE
16589 for (child_die = die->child;
16590 child_die != NULL && child_die->tag;
16591 child_die = sibling_die (child_die))
55426c9d
JB
16592 {
16593 struct attribute *attr;
16594 LONGEST value;
16595 const gdb_byte *bytes;
16596 struct dwarf2_locexpr_baton *baton;
16597 const char *name;
60f7655a 16598
55426c9d
JB
16599 if (child_die->tag != DW_TAG_enumerator)
16600 continue;
16601
16602 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16603 if (attr == NULL)
16604 continue;
16605
16606 name = dwarf2_name (child_die, cu);
16607 if (name == NULL)
16608 name = "<anonymous enumerator>";
16609
16610 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16611 &value, &bytes, &baton);
16612 if (value < 0)
16613 {
16614 unsigned_enum = 0;
16615 flag_enum = 0;
16616 }
16617 else if ((mask & value) != 0)
16618 flag_enum = 0;
16619 else
16620 mask |= value;
16621
16622 /* If we already know that the enum type is neither unsigned, nor
16623 a flag type, no need to look at the rest of the enumerates. */
16624 if (!unsigned_enum && !flag_enum)
16625 break;
55426c9d
JB
16626 }
16627
16628 if (unsigned_enum)
16629 TYPE_UNSIGNED (type) = 1;
16630 if (flag_enum)
16631 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16632}
16633
134d01f1
DJ
16634/* Given a DW_AT_enumeration_type die, set its type. We do not
16635 complete the type's fields yet, or create any symbols. */
c906108c 16636
f792889a 16637static struct type *
134d01f1 16638read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16639{
518817b3 16640 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16641 struct type *type;
c906108c 16642 struct attribute *attr;
0114d602 16643 const char *name;
134d01f1 16644
348e048f
DE
16645 /* If the definition of this type lives in .debug_types, read that type.
16646 Don't follow DW_AT_specification though, that will take us back up
16647 the chain and we want to go down. */
45e58e77 16648 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
16649 if (attr)
16650 {
ac9ec31b 16651 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16652
ac9ec31b 16653 /* The type's CU may not be the same as CU.
02142a6c 16654 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16655 return set_die_type (die, type, cu);
16656 }
16657
c906108c
SS
16658 type = alloc_type (objfile);
16659
16660 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16661 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16662 if (name != NULL)
7d455152 16663 TYPE_TAG_NAME (type) = name;
c906108c 16664
0626fc76
TT
16665 attr = dwarf2_attr (die, DW_AT_type, cu);
16666 if (attr != NULL)
16667 {
16668 struct type *underlying_type = die_type (die, cu);
16669
16670 TYPE_TARGET_TYPE (type) = underlying_type;
16671 }
16672
e142c38c 16673 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16674 if (attr)
16675 {
16676 TYPE_LENGTH (type) = DW_UNSND (attr);
16677 }
16678 else
16679 {
16680 TYPE_LENGTH (type) = 0;
16681 }
16682
137033e9
JB
16683 /* The enumeration DIE can be incomplete. In Ada, any type can be
16684 declared as private in the package spec, and then defined only
16685 inside the package body. Such types are known as Taft Amendment
16686 Types. When another package uses such a type, an incomplete DIE
16687 may be generated by the compiler. */
02eb380e 16688 if (die_is_declaration (die, cu))
876cecd0 16689 TYPE_STUB (type) = 1;
02eb380e 16690
0626fc76
TT
16691 /* Finish the creation of this type by using the enum's children.
16692 We must call this even when the underlying type has been provided
16693 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16694 update_enumeration_type_from_children (die, type, cu);
16695
0626fc76
TT
16696 /* If this type has an underlying type that is not a stub, then we
16697 may use its attributes. We always use the "unsigned" attribute
16698 in this situation, because ordinarily we guess whether the type
16699 is unsigned -- but the guess can be wrong and the underlying type
16700 can tell us the reality. However, we defer to a local size
16701 attribute if one exists, because this lets the compiler override
16702 the underlying type if needed. */
16703 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16704 {
16705 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16706 if (TYPE_LENGTH (type) == 0)
16707 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16708 }
16709
3d567982
TT
16710 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16711
f792889a 16712 return set_die_type (die, type, cu);
134d01f1
DJ
16713}
16714
16715/* Given a pointer to a die which begins an enumeration, process all
16716 the dies that define the members of the enumeration, and create the
16717 symbol for the enumeration type.
16718
16719 NOTE: We reverse the order of the element list. */
16720
16721static void
16722process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16723{
f792889a 16724 struct type *this_type;
134d01f1 16725
f792889a
DJ
16726 this_type = get_die_type (die, cu);
16727 if (this_type == NULL)
16728 this_type = read_enumeration_type (die, cu);
9dc481d3 16729
639d11d3 16730 if (die->child != NULL)
c906108c 16731 {
9dc481d3
DE
16732 struct die_info *child_die;
16733 struct symbol *sym;
16734 struct field *fields = NULL;
16735 int num_fields = 0;
15d034d0 16736 const char *name;
9dc481d3 16737
639d11d3 16738 child_die = die->child;
c906108c
SS
16739 while (child_die && child_die->tag)
16740 {
16741 if (child_die->tag != DW_TAG_enumerator)
16742 {
e7c27a73 16743 process_die (child_die, cu);
c906108c
SS
16744 }
16745 else
16746 {
39cbfefa
DJ
16747 name = dwarf2_name (child_die, cu);
16748 if (name)
c906108c 16749 {
f792889a 16750 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
16751
16752 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16753 {
16754 fields = (struct field *)
16755 xrealloc (fields,
16756 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 16757 * sizeof (struct field));
c906108c
SS
16758 }
16759
3567439c 16760 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 16761 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 16762 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
16763 FIELD_BITSIZE (fields[num_fields]) = 0;
16764
16765 num_fields++;
16766 }
16767 }
16768
16769 child_die = sibling_die (child_die);
16770 }
16771
16772 if (num_fields)
16773 {
f792889a
DJ
16774 TYPE_NFIELDS (this_type) = num_fields;
16775 TYPE_FIELDS (this_type) = (struct field *)
16776 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16777 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 16778 sizeof (struct field) * num_fields);
b8c9b27d 16779 xfree (fields);
c906108c 16780 }
c906108c 16781 }
134d01f1 16782
6c83ed52
TT
16783 /* If we are reading an enum from a .debug_types unit, and the enum
16784 is a declaration, and the enum is not the signatured type in the
16785 unit, then we do not want to add a symbol for it. Adding a
16786 symbol would in some cases obscure the true definition of the
16787 enum, giving users an incomplete type when the definition is
16788 actually available. Note that we do not want to do this for all
16789 enums which are just declarations, because C++0x allows forward
16790 enum declarations. */
3019eac3 16791 if (cu->per_cu->is_debug_types
6c83ed52
TT
16792 && die_is_declaration (die, cu))
16793 {
52dc124a 16794 struct signatured_type *sig_type;
6c83ed52 16795
c0f78cd4 16796 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16797 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16798 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16799 return;
16800 }
16801
f792889a 16802 new_symbol (die, this_type, cu);
c906108c
SS
16803}
16804
16805/* Extract all information from a DW_TAG_array_type DIE and put it in
16806 the DIE's type field. For now, this only handles one dimensional
16807 arrays. */
16808
f792889a 16809static struct type *
e7c27a73 16810read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16811{
518817b3 16812 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16813 struct die_info *child_die;
7e314c57 16814 struct type *type;
c906108c 16815 struct type *element_type, *range_type, *index_type;
c906108c 16816 struct attribute *attr;
15d034d0 16817 const char *name;
a405673c 16818 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16819 unsigned int bit_stride = 0;
c906108c 16820
e7c27a73 16821 element_type = die_type (die, cu);
c906108c 16822
7e314c57
JK
16823 /* The die_type call above may have already set the type for this DIE. */
16824 type = get_die_type (die, cu);
16825 if (type)
16826 return type;
16827
dc53a7ad
JB
16828 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16829 if (attr != NULL)
a405673c
JB
16830 {
16831 int stride_ok;
16832
16833 byte_stride_prop
16834 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16835 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16836 if (!stride_ok)
16837 {
16838 complaint (&symfile_complaints,
16839 _("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16840 " - DIE at %s [in module %s]"),
16841 sect_offset_str (die->sect_off),
518817b3 16842 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16843 /* Ignore this attribute. We will likely not be able to print
16844 arrays of this type correctly, but there is little we can do
16845 to help if we cannot read the attribute's value. */
16846 byte_stride_prop = NULL;
16847 }
16848 }
dc53a7ad
JB
16849
16850 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16851 if (attr != NULL)
16852 bit_stride = DW_UNSND (attr);
16853
c906108c
SS
16854 /* Irix 6.2 native cc creates array types without children for
16855 arrays with unspecified length. */
639d11d3 16856 if (die->child == NULL)
c906108c 16857 {
46bf5051 16858 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16859 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16860 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16861 byte_stride_prop, bit_stride);
f792889a 16862 return set_die_type (die, type, cu);
c906108c
SS
16863 }
16864
791afaa2 16865 std::vector<struct type *> range_types;
639d11d3 16866 child_die = die->child;
c906108c
SS
16867 while (child_die && child_die->tag)
16868 {
16869 if (child_die->tag == DW_TAG_subrange_type)
16870 {
f792889a 16871 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16872
f792889a 16873 if (child_type != NULL)
a02abb62 16874 {
0963b4bd
MS
16875 /* The range type was succesfully read. Save it for the
16876 array type creation. */
791afaa2 16877 range_types.push_back (child_type);
a02abb62 16878 }
c906108c
SS
16879 }
16880 child_die = sibling_die (child_die);
16881 }
16882
16883 /* Dwarf2 dimensions are output from left to right, create the
16884 necessary array types in backwards order. */
7ca2d3a3 16885
c906108c 16886 type = element_type;
7ca2d3a3
DL
16887
16888 if (read_array_order (die, cu) == DW_ORD_col_major)
16889 {
16890 int i = 0;
9a619af0 16891
791afaa2 16892 while (i < range_types.size ())
dc53a7ad 16893 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16894 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16895 }
16896 else
16897 {
791afaa2 16898 size_t ndim = range_types.size ();
7ca2d3a3 16899 while (ndim-- > 0)
dc53a7ad 16900 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16901 byte_stride_prop, bit_stride);
7ca2d3a3 16902 }
c906108c 16903
f5f8a009
EZ
16904 /* Understand Dwarf2 support for vector types (like they occur on
16905 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16906 array type. This is not part of the Dwarf2/3 standard yet, but a
16907 custom vendor extension. The main difference between a regular
16908 array and the vector variant is that vectors are passed by value
16909 to functions. */
e142c38c 16910 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 16911 if (attr)
ea37ba09 16912 make_vector_type (type);
f5f8a009 16913
dbc98a8b
KW
16914 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16915 implementation may choose to implement triple vectors using this
16916 attribute. */
16917 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16918 if (attr)
16919 {
16920 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16921 TYPE_LENGTH (type) = DW_UNSND (attr);
16922 else
3e43a32a
MS
16923 complaint (&symfile_complaints,
16924 _("DW_AT_byte_size for array type smaller "
16925 "than the total size of elements"));
dbc98a8b
KW
16926 }
16927
39cbfefa
DJ
16928 name = dwarf2_name (die, cu);
16929 if (name)
16930 TYPE_NAME (type) = name;
6e70227d 16931
0963b4bd 16932 /* Install the type in the die. */
7e314c57
JK
16933 set_die_type (die, type, cu);
16934
16935 /* set_die_type should be already done. */
b4ba55a1
JB
16936 set_descriptive_type (type, die, cu);
16937
7e314c57 16938 return type;
c906108c
SS
16939}
16940
7ca2d3a3 16941static enum dwarf_array_dim_ordering
6e70227d 16942read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16943{
16944 struct attribute *attr;
16945
16946 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16947
aead7601
SM
16948 if (attr)
16949 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16950
0963b4bd
MS
16951 /* GNU F77 is a special case, as at 08/2004 array type info is the
16952 opposite order to the dwarf2 specification, but data is still
16953 laid out as per normal fortran.
7ca2d3a3 16954
0963b4bd
MS
16955 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16956 version checking. */
7ca2d3a3 16957
905e0470
PM
16958 if (cu->language == language_fortran
16959 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16960 {
16961 return DW_ORD_row_major;
16962 }
16963
6e70227d 16964 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16965 {
16966 case array_column_major:
16967 return DW_ORD_col_major;
16968 case array_row_major:
16969 default:
16970 return DW_ORD_row_major;
16971 };
16972}
16973
72019c9c 16974/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16975 the DIE's type field. */
72019c9c 16976
f792889a 16977static struct type *
72019c9c
GM
16978read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16979{
7e314c57
JK
16980 struct type *domain_type, *set_type;
16981 struct attribute *attr;
f792889a 16982
7e314c57
JK
16983 domain_type = die_type (die, cu);
16984
16985 /* The die_type call above may have already set the type for this DIE. */
16986 set_type = get_die_type (die, cu);
16987 if (set_type)
16988 return set_type;
16989
16990 set_type = create_set_type (NULL, domain_type);
16991
16992 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
16993 if (attr)
16994 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16995
f792889a 16996 return set_die_type (die, set_type, cu);
72019c9c 16997}
7ca2d3a3 16998
0971de02
TT
16999/* A helper for read_common_block that creates a locexpr baton.
17000 SYM is the symbol which we are marking as computed.
17001 COMMON_DIE is the DIE for the common block.
17002 COMMON_LOC is the location expression attribute for the common
17003 block itself.
17004 MEMBER_LOC is the location expression attribute for the particular
17005 member of the common block that we are processing.
17006 CU is the CU from which the above come. */
17007
17008static void
17009mark_common_block_symbol_computed (struct symbol *sym,
17010 struct die_info *common_die,
17011 struct attribute *common_loc,
17012 struct attribute *member_loc,
17013 struct dwarf2_cu *cu)
17014{
518817b3
SM
17015 struct dwarf2_per_objfile *dwarf2_per_objfile
17016 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
17017 struct objfile *objfile = dwarf2_per_objfile->objfile;
17018 struct dwarf2_locexpr_baton *baton;
17019 gdb_byte *ptr;
17020 unsigned int cu_off;
17021 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
17022 LONGEST offset = 0;
17023
17024 gdb_assert (common_loc && member_loc);
17025 gdb_assert (attr_form_is_block (common_loc));
17026 gdb_assert (attr_form_is_block (member_loc)
17027 || attr_form_is_constant (member_loc));
17028
8d749320 17029 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
17030 baton->per_cu = cu->per_cu;
17031 gdb_assert (baton->per_cu);
17032
17033 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17034
17035 if (attr_form_is_constant (member_loc))
17036 {
17037 offset = dwarf2_get_attr_constant_value (member_loc, 0);
17038 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17039 }
17040 else
17041 baton->size += DW_BLOCK (member_loc)->size;
17042
224c3ddb 17043 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
17044 baton->data = ptr;
17045
17046 *ptr++ = DW_OP_call4;
9c541725 17047 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
17048 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17049 ptr += 4;
17050
17051 if (attr_form_is_constant (member_loc))
17052 {
17053 *ptr++ = DW_OP_addr;
17054 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17055 ptr += cu->header.addr_size;
17056 }
17057 else
17058 {
17059 /* We have to copy the data here, because DW_OP_call4 will only
17060 use a DW_AT_location attribute. */
17061 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
17062 ptr += DW_BLOCK (member_loc)->size;
17063 }
17064
17065 *ptr++ = DW_OP_plus;
17066 gdb_assert (ptr - baton->data == baton->size);
17067
0971de02 17068 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 17069 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
17070}
17071
4357ac6c
TT
17072/* Create appropriate locally-scoped variables for all the
17073 DW_TAG_common_block entries. Also create a struct common_block
17074 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
17075 is used to sepate the common blocks name namespace from regular
17076 variable names. */
c906108c
SS
17077
17078static void
e7c27a73 17079read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17080{
0971de02
TT
17081 struct attribute *attr;
17082
17083 attr = dwarf2_attr (die, DW_AT_location, cu);
17084 if (attr)
17085 {
17086 /* Support the .debug_loc offsets. */
17087 if (attr_form_is_block (attr))
17088 {
17089 /* Ok. */
17090 }
17091 else if (attr_form_is_section_offset (attr))
17092 {
17093 dwarf2_complex_location_expr_complaint ();
17094 attr = NULL;
17095 }
17096 else
17097 {
17098 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17099 "common block member");
17100 attr = NULL;
17101 }
17102 }
17103
639d11d3 17104 if (die->child != NULL)
c906108c 17105 {
518817b3 17106 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
17107 struct die_info *child_die;
17108 size_t n_entries = 0, size;
17109 struct common_block *common_block;
17110 struct symbol *sym;
74ac6d43 17111
4357ac6c
TT
17112 for (child_die = die->child;
17113 child_die && child_die->tag;
17114 child_die = sibling_die (child_die))
17115 ++n_entries;
17116
17117 size = (sizeof (struct common_block)
17118 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
17119 common_block
17120 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17121 size);
4357ac6c
TT
17122 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17123 common_block->n_entries = 0;
17124
17125 for (child_die = die->child;
17126 child_die && child_die->tag;
17127 child_die = sibling_die (child_die))
17128 {
17129 /* Create the symbol in the DW_TAG_common_block block in the current
17130 symbol scope. */
e7c27a73 17131 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
17132 if (sym != NULL)
17133 {
17134 struct attribute *member_loc;
17135
17136 common_block->contents[common_block->n_entries++] = sym;
17137
17138 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17139 cu);
17140 if (member_loc)
17141 {
17142 /* GDB has handled this for a long time, but it is
17143 not specified by DWARF. It seems to have been
17144 emitted by gfortran at least as recently as:
17145 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17146 complaint (&symfile_complaints,
17147 _("Variable in common block has "
17148 "DW_AT_data_member_location "
9d8780f0
SM
17149 "- DIE at %s [in module %s]"),
17150 sect_offset_str (child_die->sect_off),
518817b3 17151 objfile_name (objfile));
0971de02
TT
17152
17153 if (attr_form_is_section_offset (member_loc))
17154 dwarf2_complex_location_expr_complaint ();
17155 else if (attr_form_is_constant (member_loc)
17156 || attr_form_is_block (member_loc))
17157 {
17158 if (attr)
17159 mark_common_block_symbol_computed (sym, die, attr,
17160 member_loc, cu);
17161 }
17162 else
17163 dwarf2_complex_location_expr_complaint ();
17164 }
17165 }
c906108c 17166 }
4357ac6c
TT
17167
17168 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17169 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
17170 }
17171}
17172
0114d602 17173/* Create a type for a C++ namespace. */
d9fa45fe 17174
0114d602
DJ
17175static struct type *
17176read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 17177{
518817b3 17178 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17179 const char *previous_prefix, *name;
9219021c 17180 int is_anonymous;
0114d602
DJ
17181 struct type *type;
17182
17183 /* For extensions, reuse the type of the original namespace. */
17184 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17185 {
17186 struct die_info *ext_die;
17187 struct dwarf2_cu *ext_cu = cu;
9a619af0 17188
0114d602
DJ
17189 ext_die = dwarf2_extension (die, &ext_cu);
17190 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
17191
17192 /* EXT_CU may not be the same as CU.
02142a6c 17193 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
17194 return set_die_type (die, type, cu);
17195 }
9219021c 17196
e142c38c 17197 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
17198
17199 /* Now build the name of the current namespace. */
17200
0114d602
DJ
17201 previous_prefix = determine_prefix (die, cu);
17202 if (previous_prefix[0] != '\0')
17203 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 17204 previous_prefix, name, 0, cu);
0114d602
DJ
17205
17206 /* Create the type. */
19f392bc 17207 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602
DJ
17208 TYPE_TAG_NAME (type) = TYPE_NAME (type);
17209
60531b24 17210 return set_die_type (die, type, cu);
0114d602
DJ
17211}
17212
22cee43f 17213/* Read a namespace scope. */
0114d602
DJ
17214
17215static void
17216read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17217{
518817b3 17218 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17219 int is_anonymous;
9219021c 17220
5c4e30ca
DC
17221 /* Add a symbol associated to this if we haven't seen the namespace
17222 before. Also, add a using directive if it's an anonymous
17223 namespace. */
9219021c 17224
f2f0e013 17225 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
17226 {
17227 struct type *type;
17228
0114d602 17229 type = read_type_die (die, cu);
e7c27a73 17230 new_symbol (die, type, cu);
5c4e30ca 17231
e8e80198 17232 namespace_name (die, &is_anonymous, cu);
5c4e30ca 17233 if (is_anonymous)
0114d602
DJ
17234 {
17235 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 17236
eb1e02fd 17237 std::vector<const char *> excludes;
22cee43f
PMR
17238 add_using_directive (using_directives (cu->language),
17239 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 17240 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 17241 }
5c4e30ca 17242 }
9219021c 17243
639d11d3 17244 if (die->child != NULL)
d9fa45fe 17245 {
639d11d3 17246 struct die_info *child_die = die->child;
6e70227d 17247
d9fa45fe
DC
17248 while (child_die && child_die->tag)
17249 {
e7c27a73 17250 process_die (child_die, cu);
d9fa45fe
DC
17251 child_die = sibling_die (child_die);
17252 }
17253 }
38d518c9
EZ
17254}
17255
f55ee35c
JK
17256/* Read a Fortran module as type. This DIE can be only a declaration used for
17257 imported module. Still we need that type as local Fortran "use ... only"
17258 declaration imports depend on the created type in determine_prefix. */
17259
17260static struct type *
17261read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17262{
518817b3 17263 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 17264 const char *module_name;
f55ee35c
JK
17265 struct type *type;
17266
17267 module_name = dwarf2_name (die, cu);
17268 if (!module_name)
3e43a32a 17269 complaint (&symfile_complaints,
9d8780f0
SM
17270 _("DW_TAG_module has no name, offset %s"),
17271 sect_offset_str (die->sect_off));
19f392bc 17272 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c
JK
17273
17274 /* determine_prefix uses TYPE_TAG_NAME. */
17275 TYPE_TAG_NAME (type) = TYPE_NAME (type);
17276
17277 return set_die_type (die, type, cu);
17278}
17279
5d7cb8df
JK
17280/* Read a Fortran module. */
17281
17282static void
17283read_module (struct die_info *die, struct dwarf2_cu *cu)
17284{
17285 struct die_info *child_die = die->child;
530e8392
KB
17286 struct type *type;
17287
17288 type = read_type_die (die, cu);
17289 new_symbol (die, type, cu);
5d7cb8df 17290
5d7cb8df
JK
17291 while (child_die && child_die->tag)
17292 {
17293 process_die (child_die, cu);
17294 child_die = sibling_die (child_die);
17295 }
17296}
17297
38d518c9
EZ
17298/* Return the name of the namespace represented by DIE. Set
17299 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17300 namespace. */
17301
17302static const char *
e142c38c 17303namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
17304{
17305 struct die_info *current_die;
17306 const char *name = NULL;
17307
17308 /* Loop through the extensions until we find a name. */
17309
17310 for (current_die = die;
17311 current_die != NULL;
f2f0e013 17312 current_die = dwarf2_extension (die, &cu))
38d518c9 17313 {
96553a0c
DE
17314 /* We don't use dwarf2_name here so that we can detect the absence
17315 of a name -> anonymous namespace. */
7d45c7c3 17316 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17317
38d518c9
EZ
17318 if (name != NULL)
17319 break;
17320 }
17321
17322 /* Is it an anonymous namespace? */
17323
17324 *is_anonymous = (name == NULL);
17325 if (*is_anonymous)
2b1dbab0 17326 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17327
17328 return name;
d9fa45fe
DC
17329}
17330
c906108c
SS
17331/* Extract all information from a DW_TAG_pointer_type DIE and add to
17332 the user defined type vector. */
17333
f792889a 17334static struct type *
e7c27a73 17335read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17336{
518817b3
SM
17337 struct gdbarch *gdbarch
17338 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 17339 struct comp_unit_head *cu_header = &cu->header;
c906108c 17340 struct type *type;
8b2dbe47
KB
17341 struct attribute *attr_byte_size;
17342 struct attribute *attr_address_class;
17343 int byte_size, addr_class;
7e314c57
JK
17344 struct type *target_type;
17345
17346 target_type = die_type (die, cu);
c906108c 17347
7e314c57
JK
17348 /* The die_type call above may have already set the type for this DIE. */
17349 type = get_die_type (die, cu);
17350 if (type)
17351 return type;
17352
17353 type = lookup_pointer_type (target_type);
8b2dbe47 17354
e142c38c 17355 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
17356 if (attr_byte_size)
17357 byte_size = DW_UNSND (attr_byte_size);
c906108c 17358 else
8b2dbe47
KB
17359 byte_size = cu_header->addr_size;
17360
e142c38c 17361 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
17362 if (attr_address_class)
17363 addr_class = DW_UNSND (attr_address_class);
17364 else
17365 addr_class = DW_ADDR_none;
17366
17367 /* If the pointer size or address class is different than the
17368 default, create a type variant marked as such and set the
17369 length accordingly. */
17370 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 17371 {
5e2b427d 17372 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
17373 {
17374 int type_flags;
17375
849957d9 17376 type_flags = gdbarch_address_class_type_flags
5e2b427d 17377 (gdbarch, byte_size, addr_class);
876cecd0
TT
17378 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17379 == 0);
8b2dbe47
KB
17380 type = make_type_with_address_space (type, type_flags);
17381 }
17382 else if (TYPE_LENGTH (type) != byte_size)
17383 {
3e43a32a
MS
17384 complaint (&symfile_complaints,
17385 _("invalid pointer size %d"), byte_size);
8b2dbe47 17386 }
6e70227d 17387 else
9a619af0
MS
17388 {
17389 /* Should we also complain about unhandled address classes? */
17390 }
c906108c 17391 }
8b2dbe47
KB
17392
17393 TYPE_LENGTH (type) = byte_size;
f792889a 17394 return set_die_type (die, type, cu);
c906108c
SS
17395}
17396
17397/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17398 the user defined type vector. */
17399
f792889a 17400static struct type *
e7c27a73 17401read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17402{
17403 struct type *type;
17404 struct type *to_type;
17405 struct type *domain;
17406
e7c27a73
DJ
17407 to_type = die_type (die, cu);
17408 domain = die_containing_type (die, cu);
0d5de010 17409
7e314c57
JK
17410 /* The calls above may have already set the type for this DIE. */
17411 type = get_die_type (die, cu);
17412 if (type)
17413 return type;
17414
0d5de010
DJ
17415 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17416 type = lookup_methodptr_type (to_type);
7078baeb
TT
17417 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17418 {
518817b3
SM
17419 struct type *new_type
17420 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
17421
17422 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17423 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17424 TYPE_VARARGS (to_type));
17425 type = lookup_methodptr_type (new_type);
17426 }
0d5de010
DJ
17427 else
17428 type = lookup_memberptr_type (to_type, domain);
c906108c 17429
f792889a 17430 return set_die_type (die, type, cu);
c906108c
SS
17431}
17432
4297a3f0 17433/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17434 the user defined type vector. */
17435
f792889a 17436static struct type *
4297a3f0
AV
17437read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17438 enum type_code refcode)
c906108c 17439{
e7c27a73 17440 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17441 struct type *type, *target_type;
c906108c
SS
17442 struct attribute *attr;
17443
4297a3f0
AV
17444 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17445
7e314c57
JK
17446 target_type = die_type (die, cu);
17447
17448 /* The die_type call above may have already set the type for this DIE. */
17449 type = get_die_type (die, cu);
17450 if (type)
17451 return type;
17452
4297a3f0 17453 type = lookup_reference_type (target_type, refcode);
e142c38c 17454 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17455 if (attr)
17456 {
17457 TYPE_LENGTH (type) = DW_UNSND (attr);
17458 }
17459 else
17460 {
107d2387 17461 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17462 }
f792889a 17463 return set_die_type (die, type, cu);
c906108c
SS
17464}
17465
cf363f18
MW
17466/* Add the given cv-qualifiers to the element type of the array. GCC
17467 outputs DWARF type qualifiers that apply to an array, not the
17468 element type. But GDB relies on the array element type to carry
17469 the cv-qualifiers. This mimics section 6.7.3 of the C99
17470 specification. */
17471
17472static struct type *
17473add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17474 struct type *base_type, int cnst, int voltl)
17475{
17476 struct type *el_type, *inner_array;
17477
17478 base_type = copy_type (base_type);
17479 inner_array = base_type;
17480
17481 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17482 {
17483 TYPE_TARGET_TYPE (inner_array) =
17484 copy_type (TYPE_TARGET_TYPE (inner_array));
17485 inner_array = TYPE_TARGET_TYPE (inner_array);
17486 }
17487
17488 el_type = TYPE_TARGET_TYPE (inner_array);
17489 cnst |= TYPE_CONST (el_type);
17490 voltl |= TYPE_VOLATILE (el_type);
17491 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17492
17493 return set_die_type (die, base_type, cu);
17494}
17495
f792889a 17496static struct type *
e7c27a73 17497read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17498{
f792889a 17499 struct type *base_type, *cv_type;
c906108c 17500
e7c27a73 17501 base_type = die_type (die, cu);
7e314c57
JK
17502
17503 /* The die_type call above may have already set the type for this DIE. */
17504 cv_type = get_die_type (die, cu);
17505 if (cv_type)
17506 return cv_type;
17507
2f608a3a
KW
17508 /* In case the const qualifier is applied to an array type, the element type
17509 is so qualified, not the array type (section 6.7.3 of C99). */
17510 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 17511 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17512
f792889a
DJ
17513 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17514 return set_die_type (die, cv_type, cu);
c906108c
SS
17515}
17516
f792889a 17517static struct type *
e7c27a73 17518read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17519{
f792889a 17520 struct type *base_type, *cv_type;
c906108c 17521
e7c27a73 17522 base_type = die_type (die, cu);
7e314c57
JK
17523
17524 /* The die_type call above may have already set the type for this DIE. */
17525 cv_type = get_die_type (die, cu);
17526 if (cv_type)
17527 return cv_type;
17528
cf363f18
MW
17529 /* In case the volatile qualifier is applied to an array type, the
17530 element type is so qualified, not the array type (section 6.7.3
17531 of C99). */
17532 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17533 return add_array_cv_type (die, cu, base_type, 0, 1);
17534
f792889a
DJ
17535 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17536 return set_die_type (die, cv_type, cu);
c906108c
SS
17537}
17538
06d66ee9
TT
17539/* Handle DW_TAG_restrict_type. */
17540
17541static struct type *
17542read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17543{
17544 struct type *base_type, *cv_type;
17545
17546 base_type = die_type (die, cu);
17547
17548 /* The die_type call above may have already set the type for this DIE. */
17549 cv_type = get_die_type (die, cu);
17550 if (cv_type)
17551 return cv_type;
17552
17553 cv_type = make_restrict_type (base_type);
17554 return set_die_type (die, cv_type, cu);
17555}
17556
a2c2acaf
MW
17557/* Handle DW_TAG_atomic_type. */
17558
17559static struct type *
17560read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17561{
17562 struct type *base_type, *cv_type;
17563
17564 base_type = die_type (die, cu);
17565
17566 /* The die_type call above may have already set the type for this DIE. */
17567 cv_type = get_die_type (die, cu);
17568 if (cv_type)
17569 return cv_type;
17570
17571 cv_type = make_atomic_type (base_type);
17572 return set_die_type (die, cv_type, cu);
17573}
17574
c906108c
SS
17575/* Extract all information from a DW_TAG_string_type DIE and add to
17576 the user defined type vector. It isn't really a user defined type,
17577 but it behaves like one, with other DIE's using an AT_user_def_type
17578 attribute to reference it. */
17579
f792889a 17580static struct type *
e7c27a73 17581read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17582{
518817b3 17583 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 17584 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17585 struct type *type, *range_type, *index_type, *char_type;
17586 struct attribute *attr;
17587 unsigned int length;
17588
e142c38c 17589 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
17590 if (attr)
17591 {
17592 length = DW_UNSND (attr);
17593 }
17594 else
17595 {
0963b4bd 17596 /* Check for the DW_AT_byte_size attribute. */
e142c38c 17597 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
17598 if (attr)
17599 {
17600 length = DW_UNSND (attr);
17601 }
17602 else
17603 {
17604 length = 1;
17605 }
c906108c 17606 }
6ccb9162 17607
46bf5051 17608 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17609 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
17610 char_type = language_string_char_type (cu->language_defn, gdbarch);
17611 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17612
f792889a 17613 return set_die_type (die, type, cu);
c906108c
SS
17614}
17615
4d804846
JB
17616/* Assuming that DIE corresponds to a function, returns nonzero
17617 if the function is prototyped. */
17618
17619static int
17620prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17621{
17622 struct attribute *attr;
17623
17624 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17625 if (attr && (DW_UNSND (attr) != 0))
17626 return 1;
17627
17628 /* The DWARF standard implies that the DW_AT_prototyped attribute
17629 is only meaninful for C, but the concept also extends to other
17630 languages that allow unprototyped functions (Eg: Objective C).
17631 For all other languages, assume that functions are always
17632 prototyped. */
17633 if (cu->language != language_c
17634 && cu->language != language_objc
17635 && cu->language != language_opencl)
17636 return 1;
17637
17638 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17639 prototyped and unprototyped functions; default to prototyped,
17640 since that is more common in modern code (and RealView warns
17641 about unprototyped functions). */
17642 if (producer_is_realview (cu->producer))
17643 return 1;
17644
17645 return 0;
17646}
17647
c906108c
SS
17648/* Handle DIES due to C code like:
17649
17650 struct foo
c5aa993b
JM
17651 {
17652 int (*funcp)(int a, long l);
17653 int b;
17654 };
c906108c 17655
0963b4bd 17656 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17657
f792889a 17658static struct type *
e7c27a73 17659read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17660{
518817b3 17661 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17662 struct type *type; /* Type that this function returns. */
17663 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17664 struct attribute *attr;
17665
e7c27a73 17666 type = die_type (die, cu);
7e314c57
JK
17667
17668 /* The die_type call above may have already set the type for this DIE. */
17669 ftype = get_die_type (die, cu);
17670 if (ftype)
17671 return ftype;
17672
0c8b41f1 17673 ftype = lookup_function_type (type);
c906108c 17674
4d804846 17675 if (prototyped_function_p (die, cu))
a6c727b2 17676 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17677
c055b101
CV
17678 /* Store the calling convention in the type if it's available in
17679 the subroutine die. Otherwise set the calling convention to
17680 the default value DW_CC_normal. */
17681 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
17682 if (attr)
17683 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17684 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17685 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17686 else
17687 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17688
743649fd
MW
17689 /* Record whether the function returns normally to its caller or not
17690 if the DWARF producer set that information. */
17691 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17692 if (attr && (DW_UNSND (attr) != 0))
17693 TYPE_NO_RETURN (ftype) = 1;
17694
76c10ea2
GM
17695 /* We need to add the subroutine type to the die immediately so
17696 we don't infinitely recurse when dealing with parameters
0963b4bd 17697 declared as the same subroutine type. */
76c10ea2 17698 set_die_type (die, ftype, cu);
6e70227d 17699
639d11d3 17700 if (die->child != NULL)
c906108c 17701 {
bb5ed363 17702 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17703 struct die_info *child_die;
8072405b 17704 int nparams, iparams;
c906108c
SS
17705
17706 /* Count the number of parameters.
17707 FIXME: GDB currently ignores vararg functions, but knows about
17708 vararg member functions. */
8072405b 17709 nparams = 0;
639d11d3 17710 child_die = die->child;
c906108c
SS
17711 while (child_die && child_die->tag)
17712 {
17713 if (child_die->tag == DW_TAG_formal_parameter)
17714 nparams++;
17715 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17716 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17717 child_die = sibling_die (child_die);
17718 }
17719
17720 /* Allocate storage for parameters and fill them in. */
17721 TYPE_NFIELDS (ftype) = nparams;
17722 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17723 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17724
8072405b
JK
17725 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17726 even if we error out during the parameters reading below. */
17727 for (iparams = 0; iparams < nparams; iparams++)
17728 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17729
17730 iparams = 0;
639d11d3 17731 child_die = die->child;
c906108c
SS
17732 while (child_die && child_die->tag)
17733 {
17734 if (child_die->tag == DW_TAG_formal_parameter)
17735 {
3ce3b1ba
PA
17736 struct type *arg_type;
17737
17738 /* DWARF version 2 has no clean way to discern C++
17739 static and non-static member functions. G++ helps
17740 GDB by marking the first parameter for non-static
17741 member functions (which is the this pointer) as
17742 artificial. We pass this information to
17743 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17744
17745 DWARF version 3 added DW_AT_object_pointer, which GCC
17746 4.5 does not yet generate. */
e142c38c 17747 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
17748 if (attr)
17749 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17750 else
9c37b5ae 17751 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17752 arg_type = die_type (child_die, cu);
17753
17754 /* RealView does not mark THIS as const, which the testsuite
17755 expects. GCC marks THIS as const in method definitions,
17756 but not in the class specifications (GCC PR 43053). */
17757 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17758 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17759 {
17760 int is_this = 0;
17761 struct dwarf2_cu *arg_cu = cu;
17762 const char *name = dwarf2_name (child_die, cu);
17763
17764 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17765 if (attr)
17766 {
17767 /* If the compiler emits this, use it. */
17768 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17769 is_this = 1;
17770 }
17771 else if (name && strcmp (name, "this") == 0)
17772 /* Function definitions will have the argument names. */
17773 is_this = 1;
17774 else if (name == NULL && iparams == 0)
17775 /* Declarations may not have the names, so like
17776 elsewhere in GDB, assume an artificial first
17777 argument is "this". */
17778 is_this = 1;
17779
17780 if (is_this)
17781 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17782 arg_type, 0);
17783 }
17784
17785 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17786 iparams++;
17787 }
17788 child_die = sibling_die (child_die);
17789 }
17790 }
17791
76c10ea2 17792 return ftype;
c906108c
SS
17793}
17794
f792889a 17795static struct type *
e7c27a73 17796read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17797{
518817b3 17798 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17799 const char *name = NULL;
3c8e0968 17800 struct type *this_type, *target_type;
c906108c 17801
94af9270 17802 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17803 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17804 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17805 set_die_type (die, this_type, cu);
3c8e0968
DE
17806 target_type = die_type (die, cu);
17807 if (target_type != this_type)
17808 TYPE_TARGET_TYPE (this_type) = target_type;
17809 else
17810 {
17811 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17812 spec and cause infinite loops in GDB. */
17813 complaint (&symfile_complaints,
17814 _("Self-referential DW_TAG_typedef "
9d8780f0
SM
17815 "- DIE at %s [in module %s]"),
17816 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17817 TYPE_TARGET_TYPE (this_type) = NULL;
17818 }
f792889a 17819 return this_type;
c906108c
SS
17820}
17821
9b790ce7
UW
17822/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17823 (which may be different from NAME) to the architecture back-end to allow
17824 it to guess the correct format if necessary. */
17825
17826static struct type *
17827dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17828 const char *name_hint)
17829{
17830 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17831 const struct floatformat **format;
17832 struct type *type;
17833
17834 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17835 if (format)
17836 type = init_float_type (objfile, bits, name, format);
17837 else
77b7c781 17838 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17839
17840 return type;
17841}
17842
c906108c
SS
17843/* Find a representation of a given base type and install
17844 it in the TYPE field of the die. */
17845
f792889a 17846static struct type *
e7c27a73 17847read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17848{
518817b3 17849 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17850 struct type *type;
17851 struct attribute *attr;
19f392bc 17852 int encoding = 0, bits = 0;
15d034d0 17853 const char *name;
c906108c 17854
e142c38c 17855 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
17856 if (attr)
17857 {
17858 encoding = DW_UNSND (attr);
17859 }
e142c38c 17860 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17861 if (attr)
17862 {
19f392bc 17863 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 17864 }
39cbfefa 17865 name = dwarf2_name (die, cu);
6ccb9162 17866 if (!name)
c906108c 17867 {
6ccb9162
UW
17868 complaint (&symfile_complaints,
17869 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 17870 }
6ccb9162
UW
17871
17872 switch (encoding)
c906108c 17873 {
6ccb9162
UW
17874 case DW_ATE_address:
17875 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17876 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17877 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17878 break;
17879 case DW_ATE_boolean:
19f392bc 17880 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17881 break;
17882 case DW_ATE_complex_float:
9b790ce7 17883 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 17884 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17885 break;
17886 case DW_ATE_decimal_float:
19f392bc 17887 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17888 break;
17889 case DW_ATE_float:
9b790ce7 17890 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
17891 break;
17892 case DW_ATE_signed:
19f392bc 17893 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
17894 break;
17895 case DW_ATE_unsigned:
3b2b8fea
TT
17896 if (cu->language == language_fortran
17897 && name
61012eef 17898 && startswith (name, "character("))
19f392bc
UW
17899 type = init_character_type (objfile, bits, 1, name);
17900 else
17901 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
17902 break;
17903 case DW_ATE_signed_char:
6e70227d 17904 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17905 || cu->language == language_pascal
17906 || cu->language == language_fortran)
19f392bc
UW
17907 type = init_character_type (objfile, bits, 0, name);
17908 else
17909 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
17910 break;
17911 case DW_ATE_unsigned_char:
868a0084 17912 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17913 || cu->language == language_pascal
c44af4eb
TT
17914 || cu->language == language_fortran
17915 || cu->language == language_rust)
19f392bc
UW
17916 type = init_character_type (objfile, bits, 1, name);
17917 else
17918 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 17919 break;
75079b2b 17920 case DW_ATE_UTF:
53e710ac
PA
17921 {
17922 gdbarch *arch = get_objfile_arch (objfile);
17923
17924 if (bits == 16)
17925 type = builtin_type (arch)->builtin_char16;
17926 else if (bits == 32)
17927 type = builtin_type (arch)->builtin_char32;
17928 else
17929 {
17930 complaint (&symfile_complaints,
17931 _("unsupported DW_ATE_UTF bit size: '%d'"),
17932 bits);
17933 type = init_integer_type (objfile, bits, 1, name);
17934 }
17935 return set_die_type (die, type, cu);
17936 }
75079b2b
TT
17937 break;
17938
6ccb9162
UW
17939 default:
17940 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17941 dwarf_type_encoding_name (encoding));
77b7c781 17942 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17943 break;
c906108c 17944 }
6ccb9162 17945
0114d602 17946 if (name && strcmp (name, "char") == 0)
876cecd0 17947 TYPE_NOSIGN (type) = 1;
0114d602 17948
f792889a 17949 return set_die_type (die, type, cu);
c906108c
SS
17950}
17951
80180f79
SA
17952/* Parse dwarf attribute if it's a block, reference or constant and put the
17953 resulting value of the attribute into struct bound_prop.
17954 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17955
17956static int
17957attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17958 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17959{
17960 struct dwarf2_property_baton *baton;
518817b3
SM
17961 struct obstack *obstack
17962 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79
SA
17963
17964 if (attr == NULL || prop == NULL)
17965 return 0;
17966
17967 if (attr_form_is_block (attr))
17968 {
8d749320 17969 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
17970 baton->referenced_type = NULL;
17971 baton->locexpr.per_cu = cu->per_cu;
17972 baton->locexpr.size = DW_BLOCK (attr)->size;
17973 baton->locexpr.data = DW_BLOCK (attr)->data;
17974 prop->data.baton = baton;
17975 prop->kind = PROP_LOCEXPR;
17976 gdb_assert (prop->data.baton != NULL);
17977 }
17978 else if (attr_form_is_ref (attr))
17979 {
17980 struct dwarf2_cu *target_cu = cu;
17981 struct die_info *target_die;
17982 struct attribute *target_attr;
17983
17984 target_die = follow_die_ref (die, attr, &target_cu);
17985 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17986 if (target_attr == NULL)
17987 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17988 target_cu);
80180f79
SA
17989 if (target_attr == NULL)
17990 return 0;
17991
df25ebbd 17992 switch (target_attr->name)
80180f79 17993 {
df25ebbd
JB
17994 case DW_AT_location:
17995 if (attr_form_is_section_offset (target_attr))
17996 {
8d749320 17997 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17998 baton->referenced_type = die_type (target_die, target_cu);
17999 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18000 prop->data.baton = baton;
18001 prop->kind = PROP_LOCLIST;
18002 gdb_assert (prop->data.baton != NULL);
18003 }
18004 else if (attr_form_is_block (target_attr))
18005 {
8d749320 18006 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
18007 baton->referenced_type = die_type (target_die, target_cu);
18008 baton->locexpr.per_cu = cu->per_cu;
18009 baton->locexpr.size = DW_BLOCK (target_attr)->size;
18010 baton->locexpr.data = DW_BLOCK (target_attr)->data;
18011 prop->data.baton = baton;
18012 prop->kind = PROP_LOCEXPR;
18013 gdb_assert (prop->data.baton != NULL);
18014 }
18015 else
18016 {
18017 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18018 "dynamic property");
18019 return 0;
18020 }
18021 break;
18022 case DW_AT_data_member_location:
18023 {
18024 LONGEST offset;
18025
18026 if (!handle_data_member_location (target_die, target_cu,
18027 &offset))
18028 return 0;
18029
8d749320 18030 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
18031 baton->referenced_type = read_type_die (target_die->parent,
18032 target_cu);
df25ebbd
JB
18033 baton->offset_info.offset = offset;
18034 baton->offset_info.type = die_type (target_die, target_cu);
18035 prop->data.baton = baton;
18036 prop->kind = PROP_ADDR_OFFSET;
18037 break;
18038 }
80180f79
SA
18039 }
18040 }
18041 else if (attr_form_is_constant (attr))
18042 {
18043 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
18044 prop->kind = PROP_CONST;
18045 }
18046 else
18047 {
18048 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18049 dwarf2_name (die, cu));
18050 return 0;
18051 }
18052
18053 return 1;
18054}
18055
a02abb62
JB
18056/* Read the given DW_AT_subrange DIE. */
18057
f792889a 18058static struct type *
a02abb62
JB
18059read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18060{
4c9ad8c2 18061 struct type *base_type, *orig_base_type;
a02abb62
JB
18062 struct type *range_type;
18063 struct attribute *attr;
729efb13 18064 struct dynamic_prop low, high;
4fae6e18 18065 int low_default_is_valid;
c451ebe5 18066 int high_bound_is_count = 0;
15d034d0 18067 const char *name;
43bbcdc2 18068 LONGEST negative_mask;
e77813c8 18069
4c9ad8c2
TT
18070 orig_base_type = die_type (die, cu);
18071 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18072 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18073 creating the range type, but we use the result of check_typedef
18074 when examining properties of the type. */
18075 base_type = check_typedef (orig_base_type);
a02abb62 18076
7e314c57
JK
18077 /* The die_type call above may have already set the type for this DIE. */
18078 range_type = get_die_type (die, cu);
18079 if (range_type)
18080 return range_type;
18081
729efb13
SA
18082 low.kind = PROP_CONST;
18083 high.kind = PROP_CONST;
18084 high.data.const_val = 0;
18085
4fae6e18
JK
18086 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18087 omitting DW_AT_lower_bound. */
18088 switch (cu->language)
6e70227d 18089 {
4fae6e18
JK
18090 case language_c:
18091 case language_cplus:
729efb13 18092 low.data.const_val = 0;
4fae6e18
JK
18093 low_default_is_valid = 1;
18094 break;
18095 case language_fortran:
729efb13 18096 low.data.const_val = 1;
4fae6e18
JK
18097 low_default_is_valid = 1;
18098 break;
18099 case language_d:
4fae6e18 18100 case language_objc:
c44af4eb 18101 case language_rust:
729efb13 18102 low.data.const_val = 0;
4fae6e18
JK
18103 low_default_is_valid = (cu->header.version >= 4);
18104 break;
18105 case language_ada:
18106 case language_m2:
18107 case language_pascal:
729efb13 18108 low.data.const_val = 1;
4fae6e18
JK
18109 low_default_is_valid = (cu->header.version >= 4);
18110 break;
18111 default:
729efb13 18112 low.data.const_val = 0;
4fae6e18
JK
18113 low_default_is_valid = 0;
18114 break;
a02abb62
JB
18115 }
18116
e142c38c 18117 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 18118 if (attr)
11c1ba78 18119 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
18120 else if (!low_default_is_valid)
18121 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
9d8780f0
SM
18122 "- DIE at %s [in module %s]"),
18123 sect_offset_str (die->sect_off),
518817b3 18124 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 18125
e142c38c 18126 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 18127 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
18128 {
18129 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 18130 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 18131 {
c451ebe5
SA
18132 /* If bounds are constant do the final calculation here. */
18133 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
18134 high.data.const_val = low.data.const_val + high.data.const_val - 1;
18135 else
18136 high_bound_is_count = 1;
c2ff108b 18137 }
e77813c8
PM
18138 }
18139
18140 /* Dwarf-2 specifications explicitly allows to create subrange types
18141 without specifying a base type.
18142 In that case, the base type must be set to the type of
18143 the lower bound, upper bound or count, in that order, if any of these
18144 three attributes references an object that has a type.
18145 If no base type is found, the Dwarf-2 specifications say that
18146 a signed integer type of size equal to the size of an address should
18147 be used.
18148 For the following C code: `extern char gdb_int [];'
18149 GCC produces an empty range DIE.
18150 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 18151 high bound or count are not yet handled by this code. */
e77813c8
PM
18152 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
18153 {
518817b3 18154 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e77813c8
PM
18155 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18156 int addr_size = gdbarch_addr_bit (gdbarch) /8;
18157 struct type *int_type = objfile_type (objfile)->builtin_int;
18158
18159 /* Test "int", "long int", and "long long int" objfile types,
18160 and select the first one having a size above or equal to the
18161 architecture address size. */
18162 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
18163 base_type = int_type;
18164 else
18165 {
18166 int_type = objfile_type (objfile)->builtin_long;
18167 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
18168 base_type = int_type;
18169 else
18170 {
18171 int_type = objfile_type (objfile)->builtin_long_long;
18172 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
18173 base_type = int_type;
18174 }
18175 }
18176 }
a02abb62 18177
dbb9c2b1
JB
18178 /* Normally, the DWARF producers are expected to use a signed
18179 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18180 But this is unfortunately not always the case, as witnessed
18181 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18182 is used instead. To work around that ambiguity, we treat
18183 the bounds as signed, and thus sign-extend their values, when
18184 the base type is signed. */
6e70227d 18185 negative_mask =
66c6502d 18186 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
18187 if (low.kind == PROP_CONST
18188 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18189 low.data.const_val |= negative_mask;
18190 if (high.kind == PROP_CONST
18191 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18192 high.data.const_val |= negative_mask;
43bbcdc2 18193
729efb13 18194 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 18195
c451ebe5
SA
18196 if (high_bound_is_count)
18197 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18198
c2ff108b
JK
18199 /* Ada expects an empty array on no boundary attributes. */
18200 if (attr == NULL && cu->language != language_ada)
729efb13 18201 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 18202
39cbfefa
DJ
18203 name = dwarf2_name (die, cu);
18204 if (name)
18205 TYPE_NAME (range_type) = name;
6e70227d 18206
e142c38c 18207 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
18208 if (attr)
18209 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18210
7e314c57
JK
18211 set_die_type (die, range_type, cu);
18212
18213 /* set_die_type should be already done. */
b4ba55a1
JB
18214 set_descriptive_type (range_type, die, cu);
18215
7e314c57 18216 return range_type;
a02abb62 18217}
6e70227d 18218
f792889a 18219static struct type *
81a17f79
JB
18220read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18221{
18222 struct type *type;
81a17f79 18223
518817b3
SM
18224 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18225 NULL);
0114d602 18226 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 18227
74a2f8ff
JB
18228 /* In Ada, an unspecified type is typically used when the description
18229 of the type is defered to a different unit. When encountering
18230 such a type, we treat it as a stub, and try to resolve it later on,
18231 when needed. */
18232 if (cu->language == language_ada)
18233 TYPE_STUB (type) = 1;
18234
f792889a 18235 return set_die_type (die, type, cu);
81a17f79 18236}
a02abb62 18237
639d11d3
DC
18238/* Read a single die and all its descendents. Set the die's sibling
18239 field to NULL; set other fields in the die correctly, and set all
18240 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18241 location of the info_ptr after reading all of those dies. PARENT
18242 is the parent of the die in question. */
18243
18244static struct die_info *
dee91e82 18245read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18246 const gdb_byte *info_ptr,
18247 const gdb_byte **new_info_ptr,
dee91e82 18248 struct die_info *parent)
639d11d3
DC
18249{
18250 struct die_info *die;
d521ce57 18251 const gdb_byte *cur_ptr;
639d11d3
DC
18252 int has_children;
18253
bf6af496 18254 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
18255 if (die == NULL)
18256 {
18257 *new_info_ptr = cur_ptr;
18258 return NULL;
18259 }
93311388 18260 store_in_ref_table (die, reader->cu);
639d11d3
DC
18261
18262 if (has_children)
bf6af496 18263 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18264 else
18265 {
18266 die->child = NULL;
18267 *new_info_ptr = cur_ptr;
18268 }
18269
18270 die->sibling = NULL;
18271 die->parent = parent;
18272 return die;
18273}
18274
18275/* Read a die, all of its descendents, and all of its siblings; set
18276 all of the fields of all of the dies correctly. Arguments are as
18277 in read_die_and_children. */
18278
18279static struct die_info *
bf6af496 18280read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18281 const gdb_byte *info_ptr,
18282 const gdb_byte **new_info_ptr,
bf6af496 18283 struct die_info *parent)
639d11d3
DC
18284{
18285 struct die_info *first_die, *last_sibling;
d521ce57 18286 const gdb_byte *cur_ptr;
639d11d3 18287
c906108c 18288 cur_ptr = info_ptr;
639d11d3
DC
18289 first_die = last_sibling = NULL;
18290
18291 while (1)
c906108c 18292 {
639d11d3 18293 struct die_info *die
dee91e82 18294 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18295
1d325ec1 18296 if (die == NULL)
c906108c 18297 {
639d11d3
DC
18298 *new_info_ptr = cur_ptr;
18299 return first_die;
c906108c 18300 }
1d325ec1
DJ
18301
18302 if (!first_die)
18303 first_die = die;
c906108c 18304 else
1d325ec1
DJ
18305 last_sibling->sibling = die;
18306
18307 last_sibling = die;
c906108c 18308 }
c906108c
SS
18309}
18310
bf6af496
DE
18311/* Read a die, all of its descendents, and all of its siblings; set
18312 all of the fields of all of the dies correctly. Arguments are as
18313 in read_die_and_children.
18314 This the main entry point for reading a DIE and all its children. */
18315
18316static struct die_info *
18317read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18318 const gdb_byte *info_ptr,
18319 const gdb_byte **new_info_ptr,
bf6af496
DE
18320 struct die_info *parent)
18321{
18322 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18323 new_info_ptr, parent);
18324
b4f54984 18325 if (dwarf_die_debug)
bf6af496
DE
18326 {
18327 fprintf_unfiltered (gdb_stdlog,
18328 "Read die from %s@0x%x of %s:\n",
a32a8923 18329 get_section_name (reader->die_section),
bf6af496
DE
18330 (unsigned) (info_ptr - reader->die_section->buffer),
18331 bfd_get_filename (reader->abfd));
b4f54984 18332 dump_die (die, dwarf_die_debug);
bf6af496
DE
18333 }
18334
18335 return die;
18336}
18337
3019eac3
DE
18338/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18339 attributes.
18340 The caller is responsible for filling in the extra attributes
18341 and updating (*DIEP)->num_attrs.
18342 Set DIEP to point to a newly allocated die with its information,
18343 except for its child, sibling, and parent fields.
18344 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 18345
d521ce57 18346static const gdb_byte *
3019eac3 18347read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18348 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 18349 int *has_children, int num_extra_attrs)
93311388 18350{
b64f50a1 18351 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18352 struct abbrev_info *abbrev;
18353 struct die_info *die;
18354 struct dwarf2_cu *cu = reader->cu;
18355 bfd *abfd = reader->abfd;
18356
9c541725 18357 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18358 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18359 info_ptr += bytes_read;
18360 if (!abbrev_number)
18361 {
18362 *diep = NULL;
18363 *has_children = 0;
18364 return info_ptr;
18365 }
18366
685af9cd 18367 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18368 if (!abbrev)
348e048f
DE
18369 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18370 abbrev_number,
18371 bfd_get_filename (abfd));
18372
3019eac3 18373 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18374 die->sect_off = sect_off;
93311388
DE
18375 die->tag = abbrev->tag;
18376 die->abbrev = abbrev_number;
18377
3019eac3
DE
18378 /* Make the result usable.
18379 The caller needs to update num_attrs after adding the extra
18380 attributes. */
93311388
DE
18381 die->num_attrs = abbrev->num_attrs;
18382
18383 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
18384 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18385 info_ptr);
93311388
DE
18386
18387 *diep = die;
18388 *has_children = abbrev->has_children;
18389 return info_ptr;
18390}
18391
3019eac3
DE
18392/* Read a die and all its attributes.
18393 Set DIEP to point to a newly allocated die with its information,
18394 except for its child, sibling, and parent fields.
18395 Set HAS_CHILDREN to tell whether the die has children or not. */
18396
d521ce57 18397static const gdb_byte *
3019eac3 18398read_full_die (const struct die_reader_specs *reader,
d521ce57 18399 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
18400 int *has_children)
18401{
d521ce57 18402 const gdb_byte *result;
bf6af496
DE
18403
18404 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18405
b4f54984 18406 if (dwarf_die_debug)
bf6af496
DE
18407 {
18408 fprintf_unfiltered (gdb_stdlog,
18409 "Read die from %s@0x%x of %s:\n",
a32a8923 18410 get_section_name (reader->die_section),
bf6af496
DE
18411 (unsigned) (info_ptr - reader->die_section->buffer),
18412 bfd_get_filename (reader->abfd));
b4f54984 18413 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18414 }
18415
18416 return result;
3019eac3 18417}
433df2d4
DE
18418\f
18419/* Abbreviation tables.
3019eac3 18420
433df2d4 18421 In DWARF version 2, the description of the debugging information is
c906108c
SS
18422 stored in a separate .debug_abbrev section. Before we read any
18423 dies from a section we read in all abbreviations and install them
433df2d4
DE
18424 in a hash table. */
18425
18426/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18427
685af9cd
TT
18428struct abbrev_info *
18429abbrev_table::alloc_abbrev ()
433df2d4
DE
18430{
18431 struct abbrev_info *abbrev;
18432
685af9cd 18433 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
433df2d4 18434 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 18435
433df2d4
DE
18436 return abbrev;
18437}
18438
18439/* Add an abbreviation to the table. */
c906108c 18440
685af9cd
TT
18441void
18442abbrev_table::add_abbrev (unsigned int abbrev_number,
18443 struct abbrev_info *abbrev)
433df2d4
DE
18444{
18445 unsigned int hash_number;
18446
18447 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768
YQ
18448 abbrev->next = m_abbrevs[hash_number];
18449 m_abbrevs[hash_number] = abbrev;
433df2d4 18450}
dee91e82 18451
433df2d4
DE
18452/* Look up an abbrev in the table.
18453 Returns NULL if the abbrev is not found. */
18454
685af9cd
TT
18455struct abbrev_info *
18456abbrev_table::lookup_abbrev (unsigned int abbrev_number)
c906108c 18457{
433df2d4
DE
18458 unsigned int hash_number;
18459 struct abbrev_info *abbrev;
18460
18461 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768 18462 abbrev = m_abbrevs[hash_number];
433df2d4
DE
18463
18464 while (abbrev)
18465 {
18466 if (abbrev->number == abbrev_number)
18467 return abbrev;
18468 abbrev = abbrev->next;
18469 }
18470 return NULL;
18471}
18472
18473/* Read in an abbrev table. */
18474
685af9cd 18475static abbrev_table_up
ed2dc618
SM
18476abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18477 struct dwarf2_section_info *section,
9c541725 18478 sect_offset sect_off)
433df2d4
DE
18479{
18480 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 18481 bfd *abfd = get_section_bfd_owner (section);
d521ce57 18482 const gdb_byte *abbrev_ptr;
c906108c
SS
18483 struct abbrev_info *cur_abbrev;
18484 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 18485 unsigned int abbrev_form;
f3dd6933
DJ
18486 struct attr_abbrev *cur_attrs;
18487 unsigned int allocated_attrs;
c906108c 18488
685af9cd 18489 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
c906108c 18490
433df2d4 18491 dwarf2_read_section (objfile, section);
9c541725 18492 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
18493 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18494 abbrev_ptr += bytes_read;
18495
f3dd6933 18496 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 18497 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 18498
0963b4bd 18499 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
18500 while (abbrev_number)
18501 {
685af9cd 18502 cur_abbrev = abbrev_table->alloc_abbrev ();
c906108c
SS
18503
18504 /* read in abbrev header */
18505 cur_abbrev->number = abbrev_number;
aead7601
SM
18506 cur_abbrev->tag
18507 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
18508 abbrev_ptr += bytes_read;
18509 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18510 abbrev_ptr += 1;
18511
18512 /* now read in declarations */
22d2f3ab 18513 for (;;)
c906108c 18514 {
43988095
JK
18515 LONGEST implicit_const;
18516
22d2f3ab
JK
18517 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18518 abbrev_ptr += bytes_read;
18519 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18520 abbrev_ptr += bytes_read;
43988095
JK
18521 if (abbrev_form == DW_FORM_implicit_const)
18522 {
18523 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18524 &bytes_read);
18525 abbrev_ptr += bytes_read;
18526 }
18527 else
18528 {
18529 /* Initialize it due to a false compiler warning. */
18530 implicit_const = -1;
18531 }
22d2f3ab
JK
18532
18533 if (abbrev_name == 0)
18534 break;
18535
f3dd6933 18536 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 18537 {
f3dd6933
DJ
18538 allocated_attrs += ATTR_ALLOC_CHUNK;
18539 cur_attrs
224c3ddb 18540 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 18541 }
ae038cb0 18542
aead7601
SM
18543 cur_attrs[cur_abbrev->num_attrs].name
18544 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 18545 cur_attrs[cur_abbrev->num_attrs].form
aead7601 18546 = (enum dwarf_form) abbrev_form;
43988095 18547 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 18548 ++cur_abbrev->num_attrs;
c906108c
SS
18549 }
18550
8d749320
SM
18551 cur_abbrev->attrs =
18552 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18553 cur_abbrev->num_attrs);
f3dd6933
DJ
18554 memcpy (cur_abbrev->attrs, cur_attrs,
18555 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18556
685af9cd 18557 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
c906108c
SS
18558
18559 /* Get next abbreviation.
18560 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18561 always properly terminated with an abbrev number of 0.
18562 Exit loop if we encounter an abbreviation which we have
18563 already read (which means we are about to read the abbreviations
18564 for the next compile unit) or if the end of the abbreviation
18565 table is reached. */
433df2d4 18566 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18567 break;
18568 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18569 abbrev_ptr += bytes_read;
685af9cd 18570 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
c906108c
SS
18571 break;
18572 }
f3dd6933
DJ
18573
18574 xfree (cur_attrs);
433df2d4 18575 return abbrev_table;
c906108c
SS
18576}
18577
72bf9492
DJ
18578/* Returns nonzero if TAG represents a type that we might generate a partial
18579 symbol for. */
18580
18581static int
18582is_type_tag_for_partial (int tag)
18583{
18584 switch (tag)
18585 {
18586#if 0
18587 /* Some types that would be reasonable to generate partial symbols for,
18588 that we don't at present. */
18589 case DW_TAG_array_type:
18590 case DW_TAG_file_type:
18591 case DW_TAG_ptr_to_member_type:
18592 case DW_TAG_set_type:
18593 case DW_TAG_string_type:
18594 case DW_TAG_subroutine_type:
18595#endif
18596 case DW_TAG_base_type:
18597 case DW_TAG_class_type:
680b30c7 18598 case DW_TAG_interface_type:
72bf9492
DJ
18599 case DW_TAG_enumeration_type:
18600 case DW_TAG_structure_type:
18601 case DW_TAG_subrange_type:
18602 case DW_TAG_typedef:
18603 case DW_TAG_union_type:
18604 return 1;
18605 default:
18606 return 0;
18607 }
18608}
18609
18610/* Load all DIEs that are interesting for partial symbols into memory. */
18611
18612static struct partial_die_info *
dee91e82 18613load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18614 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18615{
dee91e82 18616 struct dwarf2_cu *cu = reader->cu;
518817b3 18617 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18618 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18619 unsigned int bytes_read;
5afb4e99 18620 unsigned int load_all = 0;
72bf9492
DJ
18621 int nesting_level = 1;
18622
18623 parent_die = NULL;
18624 last_die = NULL;
18625
7adf1e79
DE
18626 gdb_assert (cu->per_cu != NULL);
18627 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18628 load_all = 1;
18629
72bf9492
DJ
18630 cu->partial_dies
18631 = htab_create_alloc_ex (cu->header.length / 12,
18632 partial_die_hash,
18633 partial_die_eq,
18634 NULL,
18635 &cu->comp_unit_obstack,
18636 hashtab_obstack_allocate,
18637 dummy_obstack_deallocate);
18638
72bf9492
DJ
18639 while (1)
18640 {
685af9cd 18641 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18642
18643 /* A NULL abbrev means the end of a series of children. */
18644 if (abbrev == NULL)
18645 {
18646 if (--nesting_level == 0)
cd9983dd
YQ
18647 return first_die;
18648
72bf9492
DJ
18649 info_ptr += bytes_read;
18650 last_die = parent_die;
18651 parent_die = parent_die->die_parent;
18652 continue;
18653 }
18654
98bfdba5
PA
18655 /* Check for template arguments. We never save these; if
18656 they're seen, we just mark the parent, and go on our way. */
18657 if (parent_die != NULL
18658 && cu->language == language_cplus
18659 && (abbrev->tag == DW_TAG_template_type_param
18660 || abbrev->tag == DW_TAG_template_value_param))
18661 {
18662 parent_die->has_template_arguments = 1;
18663
18664 if (!load_all)
18665 {
18666 /* We don't need a partial DIE for the template argument. */
dee91e82 18667 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18668 continue;
18669 }
18670 }
18671
0d99eb77 18672 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18673 Skip their other children. */
18674 if (!load_all
18675 && cu->language == language_cplus
18676 && parent_die != NULL
18677 && parent_die->tag == DW_TAG_subprogram)
18678 {
dee91e82 18679 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18680 continue;
18681 }
18682
5afb4e99
DJ
18683 /* Check whether this DIE is interesting enough to save. Normally
18684 we would not be interested in members here, but there may be
18685 later variables referencing them via DW_AT_specification (for
18686 static members). */
18687 if (!load_all
18688 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18689 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18690 && abbrev->tag != DW_TAG_enumerator
18691 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18692 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18693 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18694 && abbrev->tag != DW_TAG_variable
5afb4e99 18695 && abbrev->tag != DW_TAG_namespace
f55ee35c 18696 && abbrev->tag != DW_TAG_module
95554aad 18697 && abbrev->tag != DW_TAG_member
74921315
KS
18698 && abbrev->tag != DW_TAG_imported_unit
18699 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18700 {
18701 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18702 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18703 continue;
18704 }
18705
6f06d47b
YQ
18706 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18707 abbrev);
cd9983dd 18708
48fbe735 18709 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18710
18711 /* This two-pass algorithm for processing partial symbols has a
18712 high cost in cache pressure. Thus, handle some simple cases
18713 here which cover the majority of C partial symbols. DIEs
18714 which neither have specification tags in them, nor could have
18715 specification tags elsewhere pointing at them, can simply be
18716 processed and discarded.
18717
18718 This segment is also optional; scan_partial_symbols and
18719 add_partial_symbol will handle these DIEs if we chain
18720 them in normally. When compilers which do not emit large
18721 quantities of duplicate debug information are more common,
18722 this code can probably be removed. */
18723
18724 /* Any complete simple types at the top level (pretty much all
18725 of them, for a language without namespaces), can be processed
18726 directly. */
18727 if (parent_die == NULL
cd9983dd
YQ
18728 && pdi.has_specification == 0
18729 && pdi.is_declaration == 0
18730 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18731 || pdi.tag == DW_TAG_base_type
18732 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18733 {
cd9983dd
YQ
18734 if (building_psymtab && pdi.name != NULL)
18735 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
72bf9492 18736 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 18737 &objfile->static_psymbols,
1762568f 18738 0, cu->language, objfile);
cd9983dd 18739 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18740 continue;
18741 }
18742
d8228535
JK
18743 /* The exception for DW_TAG_typedef with has_children above is
18744 a workaround of GCC PR debug/47510. In the case of this complaint
18745 type_name_no_tag_or_error will error on such types later.
18746
18747 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18748 it could not find the child DIEs referenced later, this is checked
18749 above. In correct DWARF DW_TAG_typedef should have no children. */
18750
cd9983dd 18751 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
d8228535
JK
18752 complaint (&symfile_complaints,
18753 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18754 "- DIE at %s [in module %s]"),
cd9983dd 18755 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18756
72bf9492
DJ
18757 /* If we're at the second level, and we're an enumerator, and
18758 our parent has no specification (meaning possibly lives in a
18759 namespace elsewhere), then we can add the partial symbol now
18760 instead of queueing it. */
cd9983dd 18761 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18762 && parent_die != NULL
18763 && parent_die->die_parent == NULL
18764 && parent_die->tag == DW_TAG_enumeration_type
18765 && parent_die->has_specification == 0)
18766 {
cd9983dd 18767 if (pdi.name == NULL)
3e43a32a
MS
18768 complaint (&symfile_complaints,
18769 _("malformed enumerator DIE ignored"));
72bf9492 18770 else if (building_psymtab)
cd9983dd 18771 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
72bf9492 18772 VAR_DOMAIN, LOC_CONST,
9c37b5ae 18773 cu->language == language_cplus
bb5ed363
DE
18774 ? &objfile->global_psymbols
18775 : &objfile->static_psymbols,
1762568f 18776 0, cu->language, objfile);
72bf9492 18777
cd9983dd 18778 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18779 continue;
18780 }
18781
cd9983dd 18782 struct partial_die_info *part_die
6f06d47b 18783 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18784
72bf9492
DJ
18785 /* We'll save this DIE so link it in. */
18786 part_die->die_parent = parent_die;
18787 part_die->die_sibling = NULL;
18788 part_die->die_child = NULL;
18789
18790 if (last_die && last_die == parent_die)
18791 last_die->die_child = part_die;
18792 else if (last_die)
18793 last_die->die_sibling = part_die;
18794
18795 last_die = part_die;
18796
18797 if (first_die == NULL)
18798 first_die = part_die;
18799
18800 /* Maybe add the DIE to the hash table. Not all DIEs that we
18801 find interesting need to be in the hash table, because we
18802 also have the parent/sibling/child chains; only those that we
18803 might refer to by offset later during partial symbol reading.
18804
18805 For now this means things that might have be the target of a
18806 DW_AT_specification, DW_AT_abstract_origin, or
18807 DW_AT_extension. DW_AT_extension will refer only to
18808 namespaces; DW_AT_abstract_origin refers to functions (and
18809 many things under the function DIE, but we do not recurse
18810 into function DIEs during partial symbol reading) and
18811 possibly variables as well; DW_AT_specification refers to
18812 declarations. Declarations ought to have the DW_AT_declaration
18813 flag. It happens that GCC forgets to put it in sometimes, but
18814 only for functions, not for types.
18815
18816 Adding more things than necessary to the hash table is harmless
18817 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18818 wasted time in find_partial_die, when we reread the compilation
18819 unit with load_all_dies set. */
72bf9492 18820
5afb4e99 18821 if (load_all
72929c62 18822 || abbrev->tag == DW_TAG_constant
5afb4e99 18823 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18824 || abbrev->tag == DW_TAG_variable
18825 || abbrev->tag == DW_TAG_namespace
18826 || part_die->is_declaration)
18827 {
18828 void **slot;
18829
18830 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18831 to_underlying (part_die->sect_off),
18832 INSERT);
72bf9492
DJ
18833 *slot = part_die;
18834 }
18835
72bf9492 18836 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18837 we have no reason to follow the children of structures; for other
98bfdba5
PA
18838 languages we have to, so that we can get at method physnames
18839 to infer fully qualified class names, for DW_AT_specification,
18840 and for C++ template arguments. For C++, we also look one level
18841 inside functions to find template arguments (if the name of the
18842 function does not already contain the template arguments).
bc30ff58
JB
18843
18844 For Ada, we need to scan the children of subprograms and lexical
18845 blocks as well because Ada allows the definition of nested
18846 entities that could be interesting for the debugger, such as
18847 nested subprograms for instance. */
72bf9492 18848 if (last_die->has_children
5afb4e99
DJ
18849 && (load_all
18850 || last_die->tag == DW_TAG_namespace
f55ee35c 18851 || last_die->tag == DW_TAG_module
72bf9492 18852 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18853 || (cu->language == language_cplus
18854 && last_die->tag == DW_TAG_subprogram
18855 && (last_die->name == NULL
18856 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18857 || (cu->language != language_c
18858 && (last_die->tag == DW_TAG_class_type
680b30c7 18859 || last_die->tag == DW_TAG_interface_type
72bf9492 18860 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
18861 || last_die->tag == DW_TAG_union_type))
18862 || (cu->language == language_ada
18863 && (last_die->tag == DW_TAG_subprogram
18864 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18865 {
18866 nesting_level++;
18867 parent_die = last_die;
18868 continue;
18869 }
18870
18871 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18872 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18873
18874 /* Back to the top, do it again. */
18875 }
18876}
18877
6f06d47b
YQ
18878partial_die_info::partial_die_info (sect_offset sect_off_,
18879 struct abbrev_info *abbrev)
18880 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18881{
18882}
18883
35cc7ed7
YQ
18884/* Read a minimal amount of information into the minimal die structure.
18885 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18886
48fbe735
YQ
18887const gdb_byte *
18888partial_die_info::read (const struct die_reader_specs *reader,
18889 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18890{
dee91e82 18891 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18892 struct dwarf2_per_objfile *dwarf2_per_objfile
18893 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18894 unsigned int i;
c5aa993b 18895 int has_low_pc_attr = 0;
c906108c 18896 int has_high_pc_attr = 0;
91da1414 18897 int high_pc_relative = 0;
c906108c 18898
fd0a254f 18899 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18900 {
48fbe735
YQ
18901 struct attribute attr;
18902
fd0a254f 18903 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
c906108c
SS
18904
18905 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18906 partial symbol table. */
c906108c
SS
18907 switch (attr.name)
18908 {
18909 case DW_AT_name:
48fbe735 18910 switch (tag)
71c25dea
TT
18911 {
18912 case DW_TAG_compile_unit:
95554aad 18913 case DW_TAG_partial_unit:
348e048f 18914 case DW_TAG_type_unit:
71c25dea
TT
18915 /* Compilation units have a DW_AT_name that is a filename, not
18916 a source language identifier. */
18917 case DW_TAG_enumeration_type:
18918 case DW_TAG_enumerator:
18919 /* These tags always have simple identifiers already; no need
18920 to canonicalize them. */
48fbe735 18921 name = DW_STRING (&attr);
71c25dea
TT
18922 break;
18923 default:
48fbe735
YQ
18924 {
18925 struct objfile *objfile = dwarf2_per_objfile->objfile;
18926
18927 name
18928 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18929 &objfile->per_bfd->storage_obstack);
18930 }
71c25dea
TT
18931 break;
18932 }
c906108c 18933 break;
31ef98ae 18934 case DW_AT_linkage_name:
c906108c 18935 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18936 /* Note that both forms of linkage name might appear. We
18937 assume they will be the same, and we only store the last
18938 one we see. */
94af9270 18939 if (cu->language == language_ada)
48fbe735
YQ
18940 name = DW_STRING (&attr);
18941 linkage_name = DW_STRING (&attr);
c906108c
SS
18942 break;
18943 case DW_AT_low_pc:
18944 has_low_pc_attr = 1;
48fbe735 18945 lowpc = attr_value_as_address (&attr);
c906108c
SS
18946 break;
18947 case DW_AT_high_pc:
18948 has_high_pc_attr = 1;
48fbe735 18949 highpc = attr_value_as_address (&attr);
31aa7e4e
JB
18950 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18951 high_pc_relative = 1;
c906108c
SS
18952 break;
18953 case DW_AT_location:
0963b4bd 18954 /* Support the .debug_loc offsets. */
8e19ed76
PS
18955 if (attr_form_is_block (&attr))
18956 {
48fbe735 18957 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18958 }
3690dd37 18959 else if (attr_form_is_section_offset (&attr))
8e19ed76 18960 {
4d3c2250 18961 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18962 }
18963 else
18964 {
4d3c2250
KB
18965 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18966 "partial symbol information");
8e19ed76 18967 }
c906108c 18968 break;
c906108c 18969 case DW_AT_external:
48fbe735 18970 is_external = DW_UNSND (&attr);
c906108c
SS
18971 break;
18972 case DW_AT_declaration:
48fbe735 18973 is_declaration = DW_UNSND (&attr);
c906108c
SS
18974 break;
18975 case DW_AT_type:
48fbe735 18976 has_type = 1;
c906108c
SS
18977 break;
18978 case DW_AT_abstract_origin:
18979 case DW_AT_specification:
72bf9492 18980 case DW_AT_extension:
48fbe735
YQ
18981 has_specification = 1;
18982 spec_offset = dwarf2_get_ref_die_offset (&attr);
18983 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18984 || cu->per_cu->is_dwz);
c906108c
SS
18985 break;
18986 case DW_AT_sibling:
18987 /* Ignore absolute siblings, they might point outside of
18988 the current compile unit. */
18989 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
18990 complaint (&symfile_complaints,
18991 _("ignoring absolute DW_AT_sibling"));
c906108c 18992 else
b9502d3f 18993 {
48fbe735 18994 const gdb_byte *buffer = reader->buffer;
9c541725
PA
18995 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18996 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18997
18998 if (sibling_ptr < info_ptr)
18999 complaint (&symfile_complaints,
19000 _("DW_AT_sibling points backwards"));
22869d73
KS
19001 else if (sibling_ptr > reader->buffer_end)
19002 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 19003 else
48fbe735 19004 sibling = sibling_ptr;
b9502d3f 19005 }
c906108c 19006 break;
fa4028e9 19007 case DW_AT_byte_size:
48fbe735 19008 has_byte_size = 1;
fa4028e9 19009 break;
ff908ebf 19010 case DW_AT_const_value:
48fbe735 19011 has_const_value = 1;
ff908ebf 19012 break;
68511cec
CES
19013 case DW_AT_calling_convention:
19014 /* DWARF doesn't provide a way to identify a program's source-level
19015 entry point. DW_AT_calling_convention attributes are only meant
19016 to describe functions' calling conventions.
19017
19018 However, because it's a necessary piece of information in
0c1b455e
TT
19019 Fortran, and before DWARF 4 DW_CC_program was the only
19020 piece of debugging information whose definition refers to
19021 a 'main program' at all, several compilers marked Fortran
19022 main programs with DW_CC_program --- even when those
19023 functions use the standard calling conventions.
19024
19025 Although DWARF now specifies a way to provide this
19026 information, we support this practice for backward
19027 compatibility. */
68511cec 19028 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 19029 && cu->language == language_fortran)
48fbe735 19030 main_subprogram = 1;
68511cec 19031 break;
481860b3
GB
19032 case DW_AT_inline:
19033 if (DW_UNSND (&attr) == DW_INL_inlined
19034 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 19035 may_be_inlined = 1;
481860b3 19036 break;
95554aad
TT
19037
19038 case DW_AT_import:
48fbe735 19039 if (tag == DW_TAG_imported_unit)
36586728 19040 {
48fbe735
YQ
19041 d.sect_off = dwarf2_get_ref_die_offset (&attr);
19042 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19043 || cu->per_cu->is_dwz);
19044 }
95554aad
TT
19045 break;
19046
0c1b455e 19047 case DW_AT_main_subprogram:
48fbe735 19048 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
19049 break;
19050
c906108c
SS
19051 default:
19052 break;
19053 }
19054 }
19055
91da1414 19056 if (high_pc_relative)
48fbe735 19057 highpc += lowpc;
91da1414 19058
9373cf26
JK
19059 if (has_low_pc_attr && has_high_pc_attr)
19060 {
19061 /* When using the GNU linker, .gnu.linkonce. sections are used to
19062 eliminate duplicate copies of functions and vtables and such.
19063 The linker will arbitrarily choose one and discard the others.
19064 The AT_*_pc values for such functions refer to local labels in
19065 these sections. If the section from that file was discarded, the
19066 labels are not in the output, so the relocs get a value of 0.
19067 If this is a discarded function, mark the pc bounds as invalid,
19068 so that GDB will ignore it. */
48fbe735 19069 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 19070 {
48fbe735 19071 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 19072 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
19073
19074 complaint (&symfile_complaints,
19075 _("DW_AT_low_pc %s is zero "
9d8780f0 19076 "for DIE at %s [in module %s]"),
48fbe735
YQ
19077 paddress (gdbarch, lowpc),
19078 sect_offset_str (sect_off),
9d8780f0 19079 objfile_name (objfile));
9373cf26
JK
19080 }
19081 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19082 else if (lowpc >= highpc)
9373cf26 19083 {
48fbe735 19084 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 19085 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
19086
19087 complaint (&symfile_complaints,
19088 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19089 "for DIE at %s [in module %s]"),
48fbe735
YQ
19090 paddress (gdbarch, lowpc),
19091 paddress (gdbarch, highpc),
19092 sect_offset_str (sect_off),
9c541725 19093 objfile_name (objfile));
9373cf26
JK
19094 }
19095 else
48fbe735 19096 has_pc_info = 1;
9373cf26 19097 }
85cbf3d3 19098
c906108c
SS
19099 return info_ptr;
19100}
19101
72bf9492
DJ
19102/* Find a cached partial DIE at OFFSET in CU. */
19103
d590ff25
YQ
19104struct partial_die_info *
19105dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19106{
19107 struct partial_die_info *lookup_die = NULL;
6f06d47b 19108 struct partial_die_info part_die (sect_off);
72bf9492 19109
9a3c8263 19110 lookup_die = ((struct partial_die_info *)
d590ff25 19111 htab_find_with_hash (partial_dies, &part_die,
9c541725 19112 to_underlying (sect_off)));
72bf9492 19113
72bf9492
DJ
19114 return lookup_die;
19115}
19116
348e048f
DE
19117/* Find a partial DIE at OFFSET, which may or may not be in CU,
19118 except in the case of .debug_types DIEs which do not reference
19119 outside their CU (they do however referencing other types via
55f1336d 19120 DW_FORM_ref_sig8). */
72bf9492
DJ
19121
19122static struct partial_die_info *
9c541725 19123find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19124{
518817b3
SM
19125 struct dwarf2_per_objfile *dwarf2_per_objfile
19126 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19127 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
19128 struct dwarf2_per_cu_data *per_cu = NULL;
19129 struct partial_die_info *pd = NULL;
72bf9492 19130
36586728 19131 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 19132 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 19133 {
d590ff25 19134 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
19135 if (pd != NULL)
19136 return pd;
0d99eb77
DE
19137 /* We missed recording what we needed.
19138 Load all dies and try again. */
19139 per_cu = cu->per_cu;
5afb4e99 19140 }
0d99eb77
DE
19141 else
19142 {
19143 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 19144 if (cu->per_cu->is_debug_types)
0d99eb77 19145 {
9d8780f0
SM
19146 error (_("Dwarf Error: Type Unit at offset %s contains"
19147 " external reference to offset %s [in module %s].\n"),
19148 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19149 bfd_get_filename (objfile->obfd));
19150 }
9c541725 19151 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 19152 dwarf2_per_objfile);
72bf9492 19153
0d99eb77
DE
19154 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19155 load_partial_comp_unit (per_cu);
ae038cb0 19156
0d99eb77 19157 per_cu->cu->last_used = 0;
d590ff25 19158 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 19159 }
5afb4e99 19160
dee91e82
DE
19161 /* If we didn't find it, and not all dies have been loaded,
19162 load them all and try again. */
19163
5afb4e99
DJ
19164 if (pd == NULL && per_cu->load_all_dies == 0)
19165 {
5afb4e99 19166 per_cu->load_all_dies = 1;
fd820528
DE
19167
19168 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19169 THIS_CU->cu may already be in use. So we can't just free it and
19170 replace its DIEs with the ones we read in. Instead, we leave those
19171 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19172 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19173 set. */
dee91e82 19174 load_partial_comp_unit (per_cu);
5afb4e99 19175
d590ff25 19176 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
19177 }
19178
19179 if (pd == NULL)
19180 internal_error (__FILE__, __LINE__,
9d8780f0 19181 _("could not find partial DIE %s "
3e43a32a 19182 "in cache [from module %s]\n"),
9d8780f0 19183 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
5afb4e99 19184 return pd;
72bf9492
DJ
19185}
19186
abc72ce4
DE
19187/* See if we can figure out if the class lives in a namespace. We do
19188 this by looking for a member function; its demangled name will
19189 contain namespace info, if there is any. */
19190
19191static void
19192guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19193 struct dwarf2_cu *cu)
19194{
19195 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19196 what template types look like, because the demangler
19197 frequently doesn't give the same name as the debug info. We
19198 could fix this by only using the demangled name to get the
19199 prefix (but see comment in read_structure_type). */
19200
19201 struct partial_die_info *real_pdi;
19202 struct partial_die_info *child_pdi;
19203
19204 /* If this DIE (this DIE's specification, if any) has a parent, then
19205 we should not do this. We'll prepend the parent's fully qualified
19206 name when we create the partial symbol. */
19207
19208 real_pdi = struct_pdi;
19209 while (real_pdi->has_specification)
36586728
TT
19210 real_pdi = find_partial_die (real_pdi->spec_offset,
19211 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
19212
19213 if (real_pdi->die_parent != NULL)
19214 return;
19215
19216 for (child_pdi = struct_pdi->die_child;
19217 child_pdi != NULL;
19218 child_pdi = child_pdi->die_sibling)
19219 {
19220 if (child_pdi->tag == DW_TAG_subprogram
19221 && child_pdi->linkage_name != NULL)
19222 {
19223 char *actual_class_name
19224 = language_class_name_from_physname (cu->language_defn,
19225 child_pdi->linkage_name);
19226 if (actual_class_name != NULL)
19227 {
518817b3 19228 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 19229 struct_pdi->name
224c3ddb 19230 = ((const char *)
e3b94546 19231 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb
SM
19232 actual_class_name,
19233 strlen (actual_class_name)));
abc72ce4
DE
19234 xfree (actual_class_name);
19235 }
19236 break;
19237 }
19238 }
19239}
19240
52356b79
YQ
19241void
19242partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19243{
abc72ce4
DE
19244 /* Once we've fixed up a die, there's no point in doing so again.
19245 This also avoids a memory leak if we were to call
19246 guess_partial_die_structure_name multiple times. */
52356b79 19247 if (fixup_called)
abc72ce4
DE
19248 return;
19249
72bf9492
DJ
19250 /* If we found a reference attribute and the DIE has no name, try
19251 to find a name in the referred to DIE. */
19252
52356b79 19253 if (name == NULL && has_specification)
72bf9492
DJ
19254 {
19255 struct partial_die_info *spec_die;
72bf9492 19256
52356b79 19257 spec_die = find_partial_die (spec_offset, spec_is_dwz, cu);
72bf9492 19258
52356b79 19259 spec_die->fixup (cu);
72bf9492
DJ
19260
19261 if (spec_die->name)
19262 {
52356b79 19263 name = spec_die->name;
72bf9492
DJ
19264
19265 /* Copy DW_AT_external attribute if it is set. */
19266 if (spec_die->is_external)
52356b79 19267 is_external = spec_die->is_external;
72bf9492
DJ
19268 }
19269 }
19270
19271 /* Set default names for some unnamed DIEs. */
72bf9492 19272
52356b79
YQ
19273 if (name == NULL && tag == DW_TAG_namespace)
19274 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 19275
abc72ce4
DE
19276 /* If there is no parent die to provide a namespace, and there are
19277 children, see if we can determine the namespace from their linkage
122d1940 19278 name. */
abc72ce4 19279 if (cu->language == language_cplus
518817b3
SM
19280 && !VEC_empty (dwarf2_section_info_def,
19281 cu->per_cu->dwarf2_per_objfile->types)
52356b79
YQ
19282 && die_parent == NULL
19283 && has_children
19284 && (tag == DW_TAG_class_type
19285 || tag == DW_TAG_structure_type
19286 || tag == DW_TAG_union_type))
19287 guess_partial_die_structure_name (this, cu);
abc72ce4 19288
53832f31
TT
19289 /* GCC might emit a nameless struct or union that has a linkage
19290 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
19291 if (name == NULL
19292 && (tag == DW_TAG_class_type
19293 || tag == DW_TAG_interface_type
19294 || tag == DW_TAG_structure_type
19295 || tag == DW_TAG_union_type)
19296 && linkage_name != NULL)
53832f31
TT
19297 {
19298 char *demangled;
19299
52356b79 19300 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
53832f31
TT
19301 if (demangled)
19302 {
96408a79
SA
19303 const char *base;
19304
19305 /* Strip any leading namespaces/classes, keep only the base name.
19306 DW_AT_name for named DIEs does not contain the prefixes. */
19307 base = strrchr (demangled, ':');
19308 if (base && base > demangled && base[-1] == ':')
19309 base++;
19310 else
19311 base = demangled;
19312
518817b3 19313 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
52356b79 19314 name
224c3ddb 19315 = ((const char *)
e3b94546 19316 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb 19317 base, strlen (base)));
53832f31
TT
19318 xfree (demangled);
19319 }
19320 }
19321
52356b79 19322 fixup_called = 1;
72bf9492
DJ
19323}
19324
a8329558 19325/* Read an attribute value described by an attribute form. */
c906108c 19326
d521ce57 19327static const gdb_byte *
dee91e82
DE
19328read_attribute_value (const struct die_reader_specs *reader,
19329 struct attribute *attr, unsigned form,
43988095 19330 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 19331{
dee91e82 19332 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19333 struct dwarf2_per_objfile *dwarf2_per_objfile
19334 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19335 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 19336 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 19337 bfd *abfd = reader->abfd;
e7c27a73 19338 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19339 unsigned int bytes_read;
19340 struct dwarf_block *blk;
19341
aead7601 19342 attr->form = (enum dwarf_form) form;
a8329558 19343 switch (form)
c906108c 19344 {
c906108c 19345 case DW_FORM_ref_addr:
ae411497 19346 if (cu->header.version == 2)
4568ecf9 19347 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 19348 else
4568ecf9
DE
19349 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19350 &cu->header, &bytes_read);
ae411497
TT
19351 info_ptr += bytes_read;
19352 break;
36586728
TT
19353 case DW_FORM_GNU_ref_alt:
19354 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19355 info_ptr += bytes_read;
19356 break;
ae411497 19357 case DW_FORM_addr:
e7c27a73 19358 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 19359 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 19360 info_ptr += bytes_read;
c906108c
SS
19361 break;
19362 case DW_FORM_block2:
7b5a2f43 19363 blk = dwarf_alloc_block (cu);
c906108c
SS
19364 blk->size = read_2_bytes (abfd, info_ptr);
19365 info_ptr += 2;
19366 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19367 info_ptr += blk->size;
19368 DW_BLOCK (attr) = blk;
19369 break;
19370 case DW_FORM_block4:
7b5a2f43 19371 blk = dwarf_alloc_block (cu);
c906108c
SS
19372 blk->size = read_4_bytes (abfd, info_ptr);
19373 info_ptr += 4;
19374 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19375 info_ptr += blk->size;
19376 DW_BLOCK (attr) = blk;
19377 break;
19378 case DW_FORM_data2:
19379 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19380 info_ptr += 2;
19381 break;
19382 case DW_FORM_data4:
19383 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19384 info_ptr += 4;
19385 break;
19386 case DW_FORM_data8:
19387 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19388 info_ptr += 8;
19389 break;
0224619f
JK
19390 case DW_FORM_data16:
19391 blk = dwarf_alloc_block (cu);
19392 blk->size = 16;
19393 blk->data = read_n_bytes (abfd, info_ptr, 16);
19394 info_ptr += 16;
19395 DW_BLOCK (attr) = blk;
19396 break;
2dc7f7b3
TT
19397 case DW_FORM_sec_offset:
19398 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19399 info_ptr += bytes_read;
19400 break;
c906108c 19401 case DW_FORM_string:
9b1c24c8 19402 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19403 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19404 info_ptr += bytes_read;
19405 break;
4bdf3d34 19406 case DW_FORM_strp:
36586728
TT
19407 if (!cu->per_cu->is_dwz)
19408 {
ed2dc618
SM
19409 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19410 abfd, info_ptr, cu_header,
36586728
TT
19411 &bytes_read);
19412 DW_STRING_IS_CANONICAL (attr) = 0;
19413 info_ptr += bytes_read;
19414 break;
19415 }
19416 /* FALLTHROUGH */
43988095
JK
19417 case DW_FORM_line_strp:
19418 if (!cu->per_cu->is_dwz)
19419 {
ed2dc618
SM
19420 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19421 abfd, info_ptr,
43988095
JK
19422 cu_header, &bytes_read);
19423 DW_STRING_IS_CANONICAL (attr) = 0;
19424 info_ptr += bytes_read;
19425 break;
19426 }
19427 /* FALLTHROUGH */
36586728
TT
19428 case DW_FORM_GNU_strp_alt:
19429 {
ed2dc618 19430 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19431 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19432 &bytes_read);
19433
ed2dc618
SM
19434 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19435 dwz, str_offset);
36586728
TT
19436 DW_STRING_IS_CANONICAL (attr) = 0;
19437 info_ptr += bytes_read;
19438 }
4bdf3d34 19439 break;
2dc7f7b3 19440 case DW_FORM_exprloc:
c906108c 19441 case DW_FORM_block:
7b5a2f43 19442 blk = dwarf_alloc_block (cu);
c906108c
SS
19443 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19444 info_ptr += bytes_read;
19445 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19446 info_ptr += blk->size;
19447 DW_BLOCK (attr) = blk;
19448 break;
19449 case DW_FORM_block1:
7b5a2f43 19450 blk = dwarf_alloc_block (cu);
c906108c
SS
19451 blk->size = read_1_byte (abfd, info_ptr);
19452 info_ptr += 1;
19453 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19454 info_ptr += blk->size;
19455 DW_BLOCK (attr) = blk;
19456 break;
19457 case DW_FORM_data1:
19458 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19459 info_ptr += 1;
19460 break;
19461 case DW_FORM_flag:
19462 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19463 info_ptr += 1;
19464 break;
2dc7f7b3
TT
19465 case DW_FORM_flag_present:
19466 DW_UNSND (attr) = 1;
19467 break;
c906108c
SS
19468 case DW_FORM_sdata:
19469 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19470 info_ptr += bytes_read;
19471 break;
19472 case DW_FORM_udata:
19473 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19474 info_ptr += bytes_read;
19475 break;
19476 case DW_FORM_ref1:
9c541725 19477 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19478 + read_1_byte (abfd, info_ptr));
c906108c
SS
19479 info_ptr += 1;
19480 break;
19481 case DW_FORM_ref2:
9c541725 19482 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19483 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19484 info_ptr += 2;
19485 break;
19486 case DW_FORM_ref4:
9c541725 19487 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19488 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19489 info_ptr += 4;
19490 break;
613e1657 19491 case DW_FORM_ref8:
9c541725 19492 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19493 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19494 info_ptr += 8;
19495 break;
55f1336d 19496 case DW_FORM_ref_sig8:
ac9ec31b 19497 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19498 info_ptr += 8;
19499 break;
c906108c 19500 case DW_FORM_ref_udata:
9c541725 19501 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19502 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19503 info_ptr += bytes_read;
19504 break;
c906108c 19505 case DW_FORM_indirect:
a8329558
KW
19506 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19507 info_ptr += bytes_read;
43988095
JK
19508 if (form == DW_FORM_implicit_const)
19509 {
19510 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19511 info_ptr += bytes_read;
19512 }
19513 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19514 info_ptr);
19515 break;
19516 case DW_FORM_implicit_const:
19517 DW_SND (attr) = implicit_const;
a8329558 19518 break;
3019eac3
DE
19519 case DW_FORM_GNU_addr_index:
19520 if (reader->dwo_file == NULL)
19521 {
19522 /* For now flag a hard error.
19523 Later we can turn this into a complaint. */
19524 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19525 dwarf_form_name (form),
19526 bfd_get_filename (abfd));
19527 }
19528 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19529 info_ptr += bytes_read;
19530 break;
19531 case DW_FORM_GNU_str_index:
19532 if (reader->dwo_file == NULL)
19533 {
19534 /* For now flag a hard error.
19535 Later we can turn this into a complaint if warranted. */
19536 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19537 dwarf_form_name (form),
19538 bfd_get_filename (abfd));
19539 }
19540 {
19541 ULONGEST str_index =
19542 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19543
342587c4 19544 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
19545 DW_STRING_IS_CANONICAL (attr) = 0;
19546 info_ptr += bytes_read;
19547 }
19548 break;
c906108c 19549 default:
8a3fe4f8 19550 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19551 dwarf_form_name (form),
19552 bfd_get_filename (abfd));
c906108c 19553 }
28e94949 19554
36586728 19555 /* Super hack. */
7771576e 19556 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19557 attr->form = DW_FORM_GNU_ref_alt;
19558
28e94949
JB
19559 /* We have seen instances where the compiler tried to emit a byte
19560 size attribute of -1 which ended up being encoded as an unsigned
19561 0xffffffff. Although 0xffffffff is technically a valid size value,
19562 an object of this size seems pretty unlikely so we can relatively
19563 safely treat these cases as if the size attribute was invalid and
19564 treat them as zero by default. */
19565 if (attr->name == DW_AT_byte_size
19566 && form == DW_FORM_data4
19567 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19568 {
19569 complaint
19570 (&symfile_complaints,
43bbcdc2
PH
19571 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19572 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19573 DW_UNSND (attr) = 0;
19574 }
28e94949 19575
c906108c
SS
19576 return info_ptr;
19577}
19578
a8329558
KW
19579/* Read an attribute described by an abbreviated attribute. */
19580
d521ce57 19581static const gdb_byte *
dee91e82
DE
19582read_attribute (const struct die_reader_specs *reader,
19583 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 19584 const gdb_byte *info_ptr)
a8329558
KW
19585{
19586 attr->name = abbrev->name;
43988095
JK
19587 return read_attribute_value (reader, attr, abbrev->form,
19588 abbrev->implicit_const, info_ptr);
a8329558
KW
19589}
19590
0963b4bd 19591/* Read dwarf information from a buffer. */
c906108c
SS
19592
19593static unsigned int
a1855c1d 19594read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19595{
fe1b8b76 19596 return bfd_get_8 (abfd, buf);
c906108c
SS
19597}
19598
19599static int
a1855c1d 19600read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19601{
fe1b8b76 19602 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19603}
19604
19605static unsigned int
a1855c1d 19606read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19607{
fe1b8b76 19608 return bfd_get_16 (abfd, buf);
c906108c
SS
19609}
19610
21ae7a4d 19611static int
a1855c1d 19612read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19613{
19614 return bfd_get_signed_16 (abfd, buf);
19615}
19616
c906108c 19617static unsigned int
a1855c1d 19618read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19619{
fe1b8b76 19620 return bfd_get_32 (abfd, buf);
c906108c
SS
19621}
19622
21ae7a4d 19623static int
a1855c1d 19624read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19625{
19626 return bfd_get_signed_32 (abfd, buf);
19627}
19628
93311388 19629static ULONGEST
a1855c1d 19630read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19631{
fe1b8b76 19632 return bfd_get_64 (abfd, buf);
c906108c
SS
19633}
19634
19635static CORE_ADDR
d521ce57 19636read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19637 unsigned int *bytes_read)
c906108c 19638{
e7c27a73 19639 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19640 CORE_ADDR retval = 0;
19641
107d2387 19642 if (cu_header->signed_addr_p)
c906108c 19643 {
107d2387
AC
19644 switch (cu_header->addr_size)
19645 {
19646 case 2:
fe1b8b76 19647 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19648 break;
19649 case 4:
fe1b8b76 19650 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19651 break;
19652 case 8:
fe1b8b76 19653 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19654 break;
19655 default:
8e65ff28 19656 internal_error (__FILE__, __LINE__,
e2e0b3e5 19657 _("read_address: bad switch, signed [in module %s]"),
659b0389 19658 bfd_get_filename (abfd));
107d2387
AC
19659 }
19660 }
19661 else
19662 {
19663 switch (cu_header->addr_size)
19664 {
19665 case 2:
fe1b8b76 19666 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19667 break;
19668 case 4:
fe1b8b76 19669 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19670 break;
19671 case 8:
fe1b8b76 19672 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19673 break;
19674 default:
8e65ff28 19675 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19676 _("read_address: bad switch, "
19677 "unsigned [in module %s]"),
659b0389 19678 bfd_get_filename (abfd));
107d2387 19679 }
c906108c 19680 }
64367e0a 19681
107d2387
AC
19682 *bytes_read = cu_header->addr_size;
19683 return retval;
c906108c
SS
19684}
19685
f7ef9339 19686/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19687 specification allows the initial length to take up either 4 bytes
19688 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19689 bytes describe the length and all offsets will be 8 bytes in length
19690 instead of 4.
19691
f7ef9339
KB
19692 An older, non-standard 64-bit format is also handled by this
19693 function. The older format in question stores the initial length
19694 as an 8-byte quantity without an escape value. Lengths greater
19695 than 2^32 aren't very common which means that the initial 4 bytes
19696 is almost always zero. Since a length value of zero doesn't make
19697 sense for the 32-bit format, this initial zero can be considered to
19698 be an escape value which indicates the presence of the older 64-bit
19699 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19700 greater than 4GB. If it becomes necessary to handle lengths
19701 somewhat larger than 4GB, we could allow other small values (such
19702 as the non-sensical values of 1, 2, and 3) to also be used as
19703 escape values indicating the presence of the old format.
f7ef9339 19704
917c78fc
MK
19705 The value returned via bytes_read should be used to increment the
19706 relevant pointer after calling read_initial_length().
c764a876 19707
613e1657
KB
19708 [ Note: read_initial_length() and read_offset() are based on the
19709 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19710 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19711 from:
19712
f7ef9339 19713 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19714
613e1657
KB
19715 This document is only a draft and is subject to change. (So beware.)
19716
f7ef9339 19717 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19718 determined empirically by examining 64-bit ELF files produced by
19719 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19720
19721 - Kevin, July 16, 2002
613e1657
KB
19722 ] */
19723
19724static LONGEST
d521ce57 19725read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19726{
fe1b8b76 19727 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19728
dd373385 19729 if (length == 0xffffffff)
613e1657 19730 {
fe1b8b76 19731 length = bfd_get_64 (abfd, buf + 4);
613e1657 19732 *bytes_read = 12;
613e1657 19733 }
dd373385 19734 else if (length == 0)
f7ef9339 19735 {
dd373385 19736 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19737 length = bfd_get_64 (abfd, buf);
f7ef9339 19738 *bytes_read = 8;
f7ef9339 19739 }
613e1657
KB
19740 else
19741 {
19742 *bytes_read = 4;
613e1657
KB
19743 }
19744
c764a876
DE
19745 return length;
19746}
dd373385 19747
c764a876
DE
19748/* Cover function for read_initial_length.
19749 Returns the length of the object at BUF, and stores the size of the
19750 initial length in *BYTES_READ and stores the size that offsets will be in
19751 *OFFSET_SIZE.
19752 If the initial length size is not equivalent to that specified in
19753 CU_HEADER then issue a complaint.
19754 This is useful when reading non-comp-unit headers. */
dd373385 19755
c764a876 19756static LONGEST
d521ce57 19757read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19758 const struct comp_unit_head *cu_header,
19759 unsigned int *bytes_read,
19760 unsigned int *offset_size)
19761{
19762 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19763
19764 gdb_assert (cu_header->initial_length_size == 4
19765 || cu_header->initial_length_size == 8
19766 || cu_header->initial_length_size == 12);
19767
19768 if (cu_header->initial_length_size != *bytes_read)
19769 complaint (&symfile_complaints,
19770 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19771
c764a876 19772 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19773 return length;
613e1657
KB
19774}
19775
19776/* Read an offset from the data stream. The size of the offset is
917c78fc 19777 given by cu_header->offset_size. */
613e1657
KB
19778
19779static LONGEST
d521ce57
TT
19780read_offset (bfd *abfd, const gdb_byte *buf,
19781 const struct comp_unit_head *cu_header,
891d2f0b 19782 unsigned int *bytes_read)
c764a876
DE
19783{
19784 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19785
c764a876
DE
19786 *bytes_read = cu_header->offset_size;
19787 return offset;
19788}
19789
19790/* Read an offset from the data stream. */
19791
19792static LONGEST
d521ce57 19793read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19794{
19795 LONGEST retval = 0;
19796
c764a876 19797 switch (offset_size)
613e1657
KB
19798 {
19799 case 4:
fe1b8b76 19800 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19801 break;
19802 case 8:
fe1b8b76 19803 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19804 break;
19805 default:
8e65ff28 19806 internal_error (__FILE__, __LINE__,
c764a876 19807 _("read_offset_1: bad switch [in module %s]"),
659b0389 19808 bfd_get_filename (abfd));
613e1657
KB
19809 }
19810
917c78fc 19811 return retval;
613e1657
KB
19812}
19813
d521ce57
TT
19814static const gdb_byte *
19815read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19816{
19817 /* If the size of a host char is 8 bits, we can return a pointer
19818 to the buffer, otherwise we have to copy the data to a buffer
19819 allocated on the temporary obstack. */
4bdf3d34 19820 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19821 return buf;
c906108c
SS
19822}
19823
d521ce57
TT
19824static const char *
19825read_direct_string (bfd *abfd, const gdb_byte *buf,
19826 unsigned int *bytes_read_ptr)
c906108c
SS
19827{
19828 /* If the size of a host char is 8 bits, we can return a pointer
19829 to the string, otherwise we have to copy the string to a buffer
19830 allocated on the temporary obstack. */
4bdf3d34 19831 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19832 if (*buf == '\0')
19833 {
19834 *bytes_read_ptr = 1;
19835 return NULL;
19836 }
d521ce57
TT
19837 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19838 return (const char *) buf;
4bdf3d34
JJ
19839}
19840
43988095
JK
19841/* Return pointer to string at section SECT offset STR_OFFSET with error
19842 reporting strings FORM_NAME and SECT_NAME. */
19843
d521ce57 19844static const char *
ed2dc618
SM
19845read_indirect_string_at_offset_from (struct objfile *objfile,
19846 bfd *abfd, LONGEST str_offset,
43988095
JK
19847 struct dwarf2_section_info *sect,
19848 const char *form_name,
19849 const char *sect_name)
19850{
ed2dc618 19851 dwarf2_read_section (objfile, sect);
43988095
JK
19852 if (sect->buffer == NULL)
19853 error (_("%s used without %s section [in module %s]"),
19854 form_name, sect_name, bfd_get_filename (abfd));
19855 if (str_offset >= sect->size)
19856 error (_("%s pointing outside of %s section [in module %s]"),
19857 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19858 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19859 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19860 return NULL;
43988095
JK
19861 return (const char *) (sect->buffer + str_offset);
19862}
19863
19864/* Return pointer to string at .debug_str offset STR_OFFSET. */
19865
19866static const char *
ed2dc618
SM
19867read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19868 bfd *abfd, LONGEST str_offset)
43988095 19869{
ed2dc618
SM
19870 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19871 abfd, str_offset,
43988095
JK
19872 &dwarf2_per_objfile->str,
19873 "DW_FORM_strp", ".debug_str");
19874}
19875
19876/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19877
19878static const char *
ed2dc618
SM
19879read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19880 bfd *abfd, LONGEST str_offset)
43988095 19881{
ed2dc618
SM
19882 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19883 abfd, str_offset,
43988095
JK
19884 &dwarf2_per_objfile->line_str,
19885 "DW_FORM_line_strp",
19886 ".debug_line_str");
c906108c
SS
19887}
19888
36586728
TT
19889/* Read a string at offset STR_OFFSET in the .debug_str section from
19890 the .dwz file DWZ. Throw an error if the offset is too large. If
19891 the string consists of a single NUL byte, return NULL; otherwise
19892 return a pointer to the string. */
19893
d521ce57 19894static const char *
ed2dc618
SM
19895read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19896 LONGEST str_offset)
36586728 19897{
ed2dc618 19898 dwarf2_read_section (objfile, &dwz->str);
36586728
TT
19899
19900 if (dwz->str.buffer == NULL)
19901 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19902 "section [in module %s]"),
19903 bfd_get_filename (dwz->dwz_bfd));
19904 if (str_offset >= dwz->str.size)
19905 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19906 ".debug_str section [in module %s]"),
19907 bfd_get_filename (dwz->dwz_bfd));
19908 gdb_assert (HOST_CHAR_BIT == 8);
19909 if (dwz->str.buffer[str_offset] == '\0')
19910 return NULL;
d521ce57 19911 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
19912}
19913
43988095
JK
19914/* Return pointer to string at .debug_str offset as read from BUF.
19915 BUF is assumed to be in a compilation unit described by CU_HEADER.
19916 Return *BYTES_READ_PTR count of bytes read from BUF. */
19917
d521ce57 19918static const char *
ed2dc618
SM
19919read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19920 const gdb_byte *buf,
cf2c3c16
TT
19921 const struct comp_unit_head *cu_header,
19922 unsigned int *bytes_read_ptr)
19923{
19924 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19925
ed2dc618 19926 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
19927}
19928
43988095
JK
19929/* Return pointer to string at .debug_line_str offset as read from BUF.
19930 BUF is assumed to be in a compilation unit described by CU_HEADER.
19931 Return *BYTES_READ_PTR count of bytes read from BUF. */
19932
19933static const char *
ed2dc618
SM
19934read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19935 bfd *abfd, const gdb_byte *buf,
43988095
JK
19936 const struct comp_unit_head *cu_header,
19937 unsigned int *bytes_read_ptr)
19938{
19939 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19940
ed2dc618
SM
19941 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19942 str_offset);
43988095
JK
19943}
19944
19945ULONGEST
d521ce57 19946read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 19947 unsigned int *bytes_read_ptr)
c906108c 19948{
12df843f 19949 ULONGEST result;
ce5d95e1 19950 unsigned int num_read;
870f88f7 19951 int shift;
c906108c
SS
19952 unsigned char byte;
19953
19954 result = 0;
19955 shift = 0;
19956 num_read = 0;
c906108c
SS
19957 while (1)
19958 {
fe1b8b76 19959 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19960 buf++;
19961 num_read++;
12df843f 19962 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19963 if ((byte & 128) == 0)
19964 {
19965 break;
19966 }
19967 shift += 7;
19968 }
19969 *bytes_read_ptr = num_read;
19970 return result;
19971}
19972
12df843f 19973static LONGEST
d521ce57
TT
19974read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19975 unsigned int *bytes_read_ptr)
c906108c 19976{
12df843f 19977 LONGEST result;
870f88f7 19978 int shift, num_read;
c906108c
SS
19979 unsigned char byte;
19980
19981 result = 0;
19982 shift = 0;
c906108c 19983 num_read = 0;
c906108c
SS
19984 while (1)
19985 {
fe1b8b76 19986 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19987 buf++;
19988 num_read++;
12df843f 19989 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
19990 shift += 7;
19991 if ((byte & 128) == 0)
19992 {
19993 break;
19994 }
19995 }
77e0b926 19996 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 19997 result |= -(((LONGEST) 1) << shift);
c906108c
SS
19998 *bytes_read_ptr = num_read;
19999 return result;
20000}
20001
3019eac3
DE
20002/* Given index ADDR_INDEX in .debug_addr, fetch the value.
20003 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
20004 ADDR_SIZE is the size of addresses from the CU header. */
20005
20006static CORE_ADDR
ed2dc618
SM
20007read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
20008 unsigned int addr_index, ULONGEST addr_base, int addr_size)
3019eac3
DE
20009{
20010 struct objfile *objfile = dwarf2_per_objfile->objfile;
20011 bfd *abfd = objfile->obfd;
20012 const gdb_byte *info_ptr;
20013
20014 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
20015 if (dwarf2_per_objfile->addr.buffer == NULL)
20016 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20017 objfile_name (objfile));
3019eac3
DE
20018 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
20019 error (_("DW_FORM_addr_index pointing outside of "
20020 ".debug_addr section [in module %s]"),
4262abfb 20021 objfile_name (objfile));
3019eac3
DE
20022 info_ptr = (dwarf2_per_objfile->addr.buffer
20023 + addr_base + addr_index * addr_size);
20024 if (addr_size == 4)
20025 return bfd_get_32 (abfd, info_ptr);
20026 else
20027 return bfd_get_64 (abfd, info_ptr);
20028}
20029
20030/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20031
20032static CORE_ADDR
20033read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20034{
518817b3
SM
20035 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
20036 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20037}
20038
20039/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20040
20041static CORE_ADDR
d521ce57 20042read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20043 unsigned int *bytes_read)
20044{
518817b3 20045 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
20046 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20047
20048 return read_addr_index (cu, addr_index);
20049}
20050
20051/* Data structure to pass results from dwarf2_read_addr_index_reader
20052 back to dwarf2_read_addr_index. */
20053
20054struct dwarf2_read_addr_index_data
20055{
20056 ULONGEST addr_base;
20057 int addr_size;
20058};
20059
20060/* die_reader_func for dwarf2_read_addr_index. */
20061
20062static void
20063dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 20064 const gdb_byte *info_ptr,
3019eac3
DE
20065 struct die_info *comp_unit_die,
20066 int has_children,
20067 void *data)
20068{
20069 struct dwarf2_cu *cu = reader->cu;
20070 struct dwarf2_read_addr_index_data *aidata =
20071 (struct dwarf2_read_addr_index_data *) data;
20072
20073 aidata->addr_base = cu->addr_base;
20074 aidata->addr_size = cu->header.addr_size;
20075}
20076
20077/* Given an index in .debug_addr, fetch the value.
20078 NOTE: This can be called during dwarf expression evaluation,
20079 long after the debug information has been read, and thus per_cu->cu
20080 may no longer exist. */
20081
20082CORE_ADDR
20083dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
20084 unsigned int addr_index)
20085{
ed2dc618
SM
20086 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
20087 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
20088 struct dwarf2_cu *cu = per_cu->cu;
20089 ULONGEST addr_base;
20090 int addr_size;
20091
3019eac3
DE
20092 /* We need addr_base and addr_size.
20093 If we don't have PER_CU->cu, we have to get it.
20094 Nasty, but the alternative is storing the needed info in PER_CU,
20095 which at this point doesn't seem justified: it's not clear how frequently
20096 it would get used and it would increase the size of every PER_CU.
20097 Entry points like dwarf2_per_cu_addr_size do a similar thing
20098 so we're not in uncharted territory here.
20099 Alas we need to be a bit more complicated as addr_base is contained
20100 in the DIE.
20101
20102 We don't need to read the entire CU(/TU).
20103 We just need the header and top level die.
a1b64ce1 20104
3019eac3 20105 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20106 For now we skip this optimization. */
3019eac3
DE
20107
20108 if (cu != NULL)
20109 {
20110 addr_base = cu->addr_base;
20111 addr_size = cu->header.addr_size;
20112 }
20113 else
20114 {
20115 struct dwarf2_read_addr_index_data aidata;
20116
a1b64ce1
DE
20117 /* Note: We can't use init_cutu_and_read_dies_simple here,
20118 we need addr_base. */
20119 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
20120 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
20121 addr_base = aidata.addr_base;
20122 addr_size = aidata.addr_size;
20123 }
20124
ed2dc618
SM
20125 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20126 addr_size);
3019eac3
DE
20127}
20128
57d63ce2
DE
20129/* Given a DW_FORM_GNU_str_index, fetch the string.
20130 This is only used by the Fission support. */
3019eac3 20131
d521ce57 20132static const char *
342587c4 20133read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3 20134{
ed2dc618 20135 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
20136 struct dwarf2_per_objfile *dwarf2_per_objfile
20137 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20138 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 20139 const char *objf_name = objfile_name (objfile);
3019eac3 20140 bfd *abfd = objfile->obfd;
73869dc2
DE
20141 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
20142 struct dwarf2_section_info *str_offsets_section =
20143 &reader->dwo_file->sections.str_offsets;
d521ce57 20144 const gdb_byte *info_ptr;
3019eac3 20145 ULONGEST str_offset;
57d63ce2 20146 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 20147
73869dc2
DE
20148 dwarf2_read_section (objfile, str_section);
20149 dwarf2_read_section (objfile, str_offsets_section);
20150 if (str_section->buffer == NULL)
57d63ce2 20151 error (_("%s used without .debug_str.dwo section"
9d8780f0
SM
20152 " in CU at offset %s [in module %s]"),
20153 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20154 if (str_offsets_section->buffer == NULL)
57d63ce2 20155 error (_("%s used without .debug_str_offsets.dwo section"
9d8780f0
SM
20156 " in CU at offset %s [in module %s]"),
20157 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20158 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 20159 error (_("%s pointing outside of .debug_str_offsets.dwo"
9d8780f0
SM
20160 " section in CU at offset %s [in module %s]"),
20161 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20162 info_ptr = (str_offsets_section->buffer
3019eac3
DE
20163 + str_index * cu->header.offset_size);
20164 if (cu->header.offset_size == 4)
20165 str_offset = bfd_get_32 (abfd, info_ptr);
20166 else
20167 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20168 if (str_offset >= str_section->size)
57d63ce2 20169 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20170 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20171 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20172 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20173}
20174
3019eac3
DE
20175/* Return the length of an LEB128 number in BUF. */
20176
20177static int
20178leb128_size (const gdb_byte *buf)
20179{
20180 const gdb_byte *begin = buf;
20181 gdb_byte byte;
20182
20183 while (1)
20184 {
20185 byte = *buf++;
20186 if ((byte & 128) == 0)
20187 return buf - begin;
20188 }
20189}
20190
c906108c 20191static void
e142c38c 20192set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20193{
20194 switch (lang)
20195 {
20196 case DW_LANG_C89:
76bee0cc 20197 case DW_LANG_C99:
0cfd832f 20198 case DW_LANG_C11:
c906108c 20199 case DW_LANG_C:
d1be3247 20200 case DW_LANG_UPC:
e142c38c 20201 cu->language = language_c;
c906108c 20202 break;
9c37b5ae 20203 case DW_LANG_Java:
c906108c 20204 case DW_LANG_C_plus_plus:
0cfd832f
MW
20205 case DW_LANG_C_plus_plus_11:
20206 case DW_LANG_C_plus_plus_14:
e142c38c 20207 cu->language = language_cplus;
c906108c 20208 break;
6aecb9c2
JB
20209 case DW_LANG_D:
20210 cu->language = language_d;
20211 break;
c906108c
SS
20212 case DW_LANG_Fortran77:
20213 case DW_LANG_Fortran90:
b21b22e0 20214 case DW_LANG_Fortran95:
f7de9aab
MW
20215 case DW_LANG_Fortran03:
20216 case DW_LANG_Fortran08:
e142c38c 20217 cu->language = language_fortran;
c906108c 20218 break;
a766d390
DE
20219 case DW_LANG_Go:
20220 cu->language = language_go;
20221 break;
c906108c 20222 case DW_LANG_Mips_Assembler:
e142c38c 20223 cu->language = language_asm;
c906108c
SS
20224 break;
20225 case DW_LANG_Ada83:
8aaf0b47 20226 case DW_LANG_Ada95:
bc5f45f8
JB
20227 cu->language = language_ada;
20228 break;
72019c9c
GM
20229 case DW_LANG_Modula2:
20230 cu->language = language_m2;
20231 break;
fe8e67fd
PM
20232 case DW_LANG_Pascal83:
20233 cu->language = language_pascal;
20234 break;
22566fbd
DJ
20235 case DW_LANG_ObjC:
20236 cu->language = language_objc;
20237 break;
c44af4eb
TT
20238 case DW_LANG_Rust:
20239 case DW_LANG_Rust_old:
20240 cu->language = language_rust;
20241 break;
c906108c
SS
20242 case DW_LANG_Cobol74:
20243 case DW_LANG_Cobol85:
c906108c 20244 default:
e142c38c 20245 cu->language = language_minimal;
c906108c
SS
20246 break;
20247 }
e142c38c 20248 cu->language_defn = language_def (cu->language);
c906108c
SS
20249}
20250
20251/* Return the named attribute or NULL if not there. */
20252
20253static struct attribute *
e142c38c 20254dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20255{
a48e046c 20256 for (;;)
c906108c 20257 {
a48e046c
TT
20258 unsigned int i;
20259 struct attribute *spec = NULL;
20260
20261 for (i = 0; i < die->num_attrs; ++i)
20262 {
20263 if (die->attrs[i].name == name)
20264 return &die->attrs[i];
20265 if (die->attrs[i].name == DW_AT_specification
20266 || die->attrs[i].name == DW_AT_abstract_origin)
20267 spec = &die->attrs[i];
20268 }
20269
20270 if (!spec)
20271 break;
c906108c 20272
f2f0e013 20273 die = follow_die_ref (die, spec, &cu);
f2f0e013 20274 }
c5aa993b 20275
c906108c
SS
20276 return NULL;
20277}
20278
348e048f
DE
20279/* Return the named attribute or NULL if not there,
20280 but do not follow DW_AT_specification, etc.
20281 This is for use in contexts where we're reading .debug_types dies.
20282 Following DW_AT_specification, DW_AT_abstract_origin will take us
20283 back up the chain, and we want to go down. */
20284
20285static struct attribute *
45e58e77 20286dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
20287{
20288 unsigned int i;
20289
20290 for (i = 0; i < die->num_attrs; ++i)
20291 if (die->attrs[i].name == name)
20292 return &die->attrs[i];
20293
20294 return NULL;
20295}
20296
7d45c7c3
KB
20297/* Return the string associated with a string-typed attribute, or NULL if it
20298 is either not found or is of an incorrect type. */
20299
20300static const char *
20301dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20302{
20303 struct attribute *attr;
20304 const char *str = NULL;
20305
20306 attr = dwarf2_attr (die, name, cu);
20307
20308 if (attr != NULL)
20309 {
43988095 20310 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438
L
20311 || attr->form == DW_FORM_string
20312 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 20313 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
20314 str = DW_STRING (attr);
20315 else
20316 complaint (&symfile_complaints,
20317 _("string type expected for attribute %s for "
9d8780f0
SM
20318 "DIE at %s in module %s"),
20319 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 20320 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
20321 }
20322
20323 return str;
20324}
20325
05cf31d1
JB
20326/* Return non-zero iff the attribute NAME is defined for the given DIE,
20327 and holds a non-zero value. This function should only be used for
2dc7f7b3 20328 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20329
20330static int
20331dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20332{
20333 struct attribute *attr = dwarf2_attr (die, name, cu);
20334
20335 return (attr && DW_UNSND (attr));
20336}
20337
3ca72b44 20338static int
e142c38c 20339die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20340{
05cf31d1
JB
20341 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20342 which value is non-zero. However, we have to be careful with
20343 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20344 (via dwarf2_flag_true_p) follows this attribute. So we may
20345 end up accidently finding a declaration attribute that belongs
20346 to a different DIE referenced by the specification attribute,
20347 even though the given DIE does not have a declaration attribute. */
20348 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20349 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20350}
20351
63d06c5c 20352/* Return the die giving the specification for DIE, if there is
f2f0e013 20353 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20354 containing the return value on output. If there is no
20355 specification, but there is an abstract origin, that is
20356 returned. */
63d06c5c
DC
20357
20358static struct die_info *
f2f0e013 20359die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20360{
f2f0e013
DJ
20361 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20362 *spec_cu);
63d06c5c 20363
edb3359d
DJ
20364 if (spec_attr == NULL)
20365 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20366
63d06c5c
DC
20367 if (spec_attr == NULL)
20368 return NULL;
20369 else
f2f0e013 20370 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20371}
c906108c 20372
527f3840
JK
20373/* Stub for free_line_header to match void * callback types. */
20374
20375static void
20376free_line_header_voidp (void *arg)
20377{
9a3c8263 20378 struct line_header *lh = (struct line_header *) arg;
527f3840 20379
fff8551c 20380 delete lh;
527f3840
JK
20381}
20382
fff8551c
PA
20383void
20384line_header::add_include_dir (const char *include_dir)
c906108c 20385{
27e0867f 20386 if (dwarf_line_debug >= 2)
fff8551c
PA
20387 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20388 include_dirs.size () + 1, include_dir);
27e0867f 20389
fff8551c 20390 include_dirs.push_back (include_dir);
debd256d 20391}
6e70227d 20392
fff8551c
PA
20393void
20394line_header::add_file_name (const char *name,
ecfb656c 20395 dir_index d_index,
fff8551c
PA
20396 unsigned int mod_time,
20397 unsigned int length)
debd256d 20398{
27e0867f
DE
20399 if (dwarf_line_debug >= 2)
20400 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 20401 (unsigned) file_names.size () + 1, name);
27e0867f 20402
ecfb656c 20403 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 20404}
6e70227d 20405
83769d0b 20406/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20407
20408static struct dwarf2_section_info *
20409get_debug_line_section (struct dwarf2_cu *cu)
20410{
20411 struct dwarf2_section_info *section;
518817b3
SM
20412 struct dwarf2_per_objfile *dwarf2_per_objfile
20413 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
20414
20415 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20416 DWO file. */
20417 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20418 section = &cu->dwo_unit->dwo_file->sections.line;
20419 else if (cu->per_cu->is_dwz)
20420 {
ed2dc618 20421 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
20422
20423 section = &dwz->line;
20424 }
20425 else
20426 section = &dwarf2_per_objfile->line;
20427
20428 return section;
20429}
20430
43988095
JK
20431/* Read directory or file name entry format, starting with byte of
20432 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20433 entries count and the entries themselves in the described entry
20434 format. */
20435
20436static void
ed2dc618
SM
20437read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20438 bfd *abfd, const gdb_byte **bufp,
43988095
JK
20439 struct line_header *lh,
20440 const struct comp_unit_head *cu_header,
20441 void (*callback) (struct line_header *lh,
20442 const char *name,
ecfb656c 20443 dir_index d_index,
43988095
JK
20444 unsigned int mod_time,
20445 unsigned int length))
20446{
20447 gdb_byte format_count, formati;
20448 ULONGEST data_count, datai;
20449 const gdb_byte *buf = *bufp;
20450 const gdb_byte *format_header_data;
43988095
JK
20451 unsigned int bytes_read;
20452
20453 format_count = read_1_byte (abfd, buf);
20454 buf += 1;
20455 format_header_data = buf;
20456 for (formati = 0; formati < format_count; formati++)
20457 {
20458 read_unsigned_leb128 (abfd, buf, &bytes_read);
20459 buf += bytes_read;
20460 read_unsigned_leb128 (abfd, buf, &bytes_read);
20461 buf += bytes_read;
20462 }
20463
20464 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20465 buf += bytes_read;
20466 for (datai = 0; datai < data_count; datai++)
20467 {
20468 const gdb_byte *format = format_header_data;
20469 struct file_entry fe;
20470
43988095
JK
20471 for (formati = 0; formati < format_count; formati++)
20472 {
ecfb656c 20473 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20474 format += bytes_read;
43988095 20475
ecfb656c 20476 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20477 format += bytes_read;
ecfb656c
PA
20478
20479 gdb::optional<const char *> string;
20480 gdb::optional<unsigned int> uint;
20481
43988095
JK
20482 switch (form)
20483 {
20484 case DW_FORM_string:
ecfb656c 20485 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
20486 buf += bytes_read;
20487 break;
20488
20489 case DW_FORM_line_strp:
ed2dc618
SM
20490 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20491 abfd, buf,
ecfb656c
PA
20492 cu_header,
20493 &bytes_read));
43988095
JK
20494 buf += bytes_read;
20495 break;
20496
20497 case DW_FORM_data1:
ecfb656c 20498 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
20499 buf += 1;
20500 break;
20501
20502 case DW_FORM_data2:
ecfb656c 20503 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
20504 buf += 2;
20505 break;
20506
20507 case DW_FORM_data4:
ecfb656c 20508 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
20509 buf += 4;
20510 break;
20511
20512 case DW_FORM_data8:
ecfb656c 20513 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
20514 buf += 8;
20515 break;
20516
20517 case DW_FORM_udata:
ecfb656c 20518 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
20519 buf += bytes_read;
20520 break;
20521
20522 case DW_FORM_block:
20523 /* It is valid only for DW_LNCT_timestamp which is ignored by
20524 current GDB. */
20525 break;
20526 }
ecfb656c
PA
20527
20528 switch (content_type)
20529 {
20530 case DW_LNCT_path:
20531 if (string.has_value ())
20532 fe.name = *string;
20533 break;
20534 case DW_LNCT_directory_index:
20535 if (uint.has_value ())
20536 fe.d_index = (dir_index) *uint;
20537 break;
20538 case DW_LNCT_timestamp:
20539 if (uint.has_value ())
20540 fe.mod_time = *uint;
20541 break;
20542 case DW_LNCT_size:
20543 if (uint.has_value ())
20544 fe.length = *uint;
20545 break;
20546 case DW_LNCT_MD5:
20547 break;
20548 default:
20549 complaint (&symfile_complaints,
20550 _("Unknown format content type %s"),
20551 pulongest (content_type));
20552 }
43988095
JK
20553 }
20554
ecfb656c 20555 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20556 }
20557
20558 *bufp = buf;
20559}
20560
debd256d 20561/* Read the statement program header starting at OFFSET in
3019eac3 20562 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20563 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20564 Returns NULL if there is a problem reading the header, e.g., if it
20565 has a version we don't understand.
debd256d
JB
20566
20567 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20568 the returned object point into the dwarf line section buffer,
20569 and must not be freed. */
ae2de4f8 20570
fff8551c 20571static line_header_up
9c541725 20572dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20573{
d521ce57 20574 const gdb_byte *line_ptr;
c764a876 20575 unsigned int bytes_read, offset_size;
debd256d 20576 int i;
d521ce57 20577 const char *cur_dir, *cur_file;
3019eac3
DE
20578 struct dwarf2_section_info *section;
20579 bfd *abfd;
518817b3
SM
20580 struct dwarf2_per_objfile *dwarf2_per_objfile
20581 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20582
36586728 20583 section = get_debug_line_section (cu);
3019eac3
DE
20584 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20585 if (section->buffer == NULL)
debd256d 20586 {
3019eac3
DE
20587 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20588 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20589 else
20590 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
20591 return 0;
20592 }
20593
fceca515
DE
20594 /* We can't do this until we know the section is non-empty.
20595 Only then do we know we have such a section. */
a32a8923 20596 abfd = get_section_bfd_owner (section);
fceca515 20597
a738430d
MK
20598 /* Make sure that at least there's room for the total_length field.
20599 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20600 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20601 {
4d3c2250 20602 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20603 return 0;
20604 }
20605
fff8551c 20606 line_header_up lh (new line_header ());
debd256d 20607
9c541725 20608 lh->sect_off = sect_off;
527f3840
JK
20609 lh->offset_in_dwz = cu->per_cu->is_dwz;
20610
9c541725 20611 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20612
a738430d 20613 /* Read in the header. */
6e70227d 20614 lh->total_length =
c764a876
DE
20615 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20616 &bytes_read, &offset_size);
debd256d 20617 line_ptr += bytes_read;
3019eac3 20618 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20619 {
4d3c2250 20620 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20621 return 0;
20622 }
20623 lh->statement_program_end = line_ptr + lh->total_length;
20624 lh->version = read_2_bytes (abfd, line_ptr);
20625 line_ptr += 2;
43988095 20626 if (lh->version > 5)
cd366ee8
DE
20627 {
20628 /* This is a version we don't understand. The format could have
20629 changed in ways we don't handle properly so just punt. */
20630 complaint (&symfile_complaints,
20631 _("unsupported version in .debug_line section"));
20632 return NULL;
20633 }
43988095
JK
20634 if (lh->version >= 5)
20635 {
20636 gdb_byte segment_selector_size;
20637
20638 /* Skip address size. */
20639 read_1_byte (abfd, line_ptr);
20640 line_ptr += 1;
20641
20642 segment_selector_size = read_1_byte (abfd, line_ptr);
20643 line_ptr += 1;
20644 if (segment_selector_size != 0)
20645 {
20646 complaint (&symfile_complaints,
20647 _("unsupported segment selector size %u "
20648 "in .debug_line section"),
20649 segment_selector_size);
20650 return NULL;
20651 }
20652 }
c764a876
DE
20653 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20654 line_ptr += offset_size;
debd256d
JB
20655 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20656 line_ptr += 1;
2dc7f7b3
TT
20657 if (lh->version >= 4)
20658 {
20659 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20660 line_ptr += 1;
20661 }
20662 else
20663 lh->maximum_ops_per_instruction = 1;
20664
20665 if (lh->maximum_ops_per_instruction == 0)
20666 {
20667 lh->maximum_ops_per_instruction = 1;
20668 complaint (&symfile_complaints,
3e43a32a
MS
20669 _("invalid maximum_ops_per_instruction "
20670 "in `.debug_line' section"));
2dc7f7b3
TT
20671 }
20672
debd256d
JB
20673 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20674 line_ptr += 1;
20675 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20676 line_ptr += 1;
20677 lh->line_range = read_1_byte (abfd, line_ptr);
20678 line_ptr += 1;
20679 lh->opcode_base = read_1_byte (abfd, line_ptr);
20680 line_ptr += 1;
fff8551c 20681 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20682
20683 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20684 for (i = 1; i < lh->opcode_base; ++i)
20685 {
20686 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20687 line_ptr += 1;
20688 }
20689
43988095 20690 if (lh->version >= 5)
debd256d 20691 {
43988095 20692 /* Read directory table. */
ed2dc618
SM
20693 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20694 &cu->header,
fff8551c 20695 [] (struct line_header *lh, const char *name,
ecfb656c 20696 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20697 unsigned int length)
20698 {
20699 lh->add_include_dir (name);
20700 });
debd256d 20701
43988095 20702 /* Read file name table. */
ed2dc618
SM
20703 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20704 &cu->header,
fff8551c 20705 [] (struct line_header *lh, const char *name,
ecfb656c 20706 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20707 unsigned int length)
20708 {
ecfb656c 20709 lh->add_file_name (name, d_index, mod_time, length);
fff8551c 20710 });
43988095
JK
20711 }
20712 else
debd256d 20713 {
43988095
JK
20714 /* Read directory table. */
20715 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20716 {
20717 line_ptr += bytes_read;
fff8551c 20718 lh->add_include_dir (cur_dir);
43988095 20719 }
debd256d
JB
20720 line_ptr += bytes_read;
20721
43988095
JK
20722 /* Read file name table. */
20723 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20724 {
ecfb656c
PA
20725 unsigned int mod_time, length;
20726 dir_index d_index;
43988095
JK
20727
20728 line_ptr += bytes_read;
ecfb656c 20729 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20730 line_ptr += bytes_read;
20731 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20732 line_ptr += bytes_read;
20733 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20734 line_ptr += bytes_read;
20735
ecfb656c 20736 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20737 }
20738 line_ptr += bytes_read;
debd256d 20739 }
6e70227d 20740 lh->statement_program_start = line_ptr;
debd256d 20741
3019eac3 20742 if (line_ptr > (section->buffer + section->size))
4d3c2250 20743 complaint (&symfile_complaints,
3e43a32a
MS
20744 _("line number info header doesn't "
20745 "fit in `.debug_line' section"));
debd256d 20746
debd256d
JB
20747 return lh;
20748}
c906108c 20749
c6da4cef
DE
20750/* Subroutine of dwarf_decode_lines to simplify it.
20751 Return the file name of the psymtab for included file FILE_INDEX
20752 in line header LH of PST.
20753 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20754 If space for the result is malloc'd, *NAME_HOLDER will be set.
20755 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20756
d521ce57 20757static const char *
c6da4cef
DE
20758psymtab_include_file_name (const struct line_header *lh, int file_index,
20759 const struct partial_symtab *pst,
c89b44cd
TT
20760 const char *comp_dir,
20761 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20762{
8c43009f 20763 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
20764 const char *include_name = fe.name;
20765 const char *include_name_to_compare = include_name;
72b9f47f 20766 const char *pst_filename;
c6da4cef
DE
20767 int file_is_pst;
20768
8c43009f 20769 const char *dir_name = fe.include_dir (lh);
c6da4cef 20770
c89b44cd 20771 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20772 if (!IS_ABSOLUTE_PATH (include_name)
20773 && (dir_name != NULL || comp_dir != NULL))
20774 {
20775 /* Avoid creating a duplicate psymtab for PST.
20776 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20777 Before we do the comparison, however, we need to account
20778 for DIR_NAME and COMP_DIR.
20779 First prepend dir_name (if non-NULL). If we still don't
20780 have an absolute path prepend comp_dir (if non-NULL).
20781 However, the directory we record in the include-file's
20782 psymtab does not contain COMP_DIR (to match the
20783 corresponding symtab(s)).
20784
20785 Example:
20786
20787 bash$ cd /tmp
20788 bash$ gcc -g ./hello.c
20789 include_name = "hello.c"
20790 dir_name = "."
20791 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20792 DW_AT_name = "./hello.c"
20793
20794 */
c6da4cef
DE
20795
20796 if (dir_name != NULL)
20797 {
c89b44cd
TT
20798 name_holder->reset (concat (dir_name, SLASH_STRING,
20799 include_name, (char *) NULL));
20800 include_name = name_holder->get ();
c6da4cef 20801 include_name_to_compare = include_name;
c6da4cef
DE
20802 }
20803 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20804 {
c89b44cd
TT
20805 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20806 include_name, (char *) NULL));
20807 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20808 }
20809 }
20810
20811 pst_filename = pst->filename;
c89b44cd 20812 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20813 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20814 {
c89b44cd
TT
20815 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20816 pst_filename, (char *) NULL));
20817 pst_filename = copied_name.get ();
c6da4cef
DE
20818 }
20819
1e3fad37 20820 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20821
c6da4cef
DE
20822 if (file_is_pst)
20823 return NULL;
20824 return include_name;
20825}
20826
d9b3de22
DE
20827/* State machine to track the state of the line number program. */
20828
6f77053d 20829class lnp_state_machine
d9b3de22 20830{
6f77053d
PA
20831public:
20832 /* Initialize a machine state for the start of a line number
20833 program. */
20834 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20835
8c43009f
PA
20836 file_entry *current_file ()
20837 {
20838 /* lh->file_names is 0-based, but the file name numbers in the
20839 statement program are 1-based. */
6f77053d
PA
20840 return m_line_header->file_name_at (m_file);
20841 }
20842
20843 /* Record the line in the state machine. END_SEQUENCE is true if
20844 we're processing the end of a sequence. */
20845 void record_line (bool end_sequence);
20846
20847 /* Check address and if invalid nop-out the rest of the lines in this
20848 sequence. */
20849 void check_line_address (struct dwarf2_cu *cu,
20850 const gdb_byte *line_ptr,
20851 CORE_ADDR lowpc, CORE_ADDR address);
20852
20853 void handle_set_discriminator (unsigned int discriminator)
20854 {
20855 m_discriminator = discriminator;
20856 m_line_has_non_zero_discriminator |= discriminator != 0;
20857 }
20858
20859 /* Handle DW_LNE_set_address. */
20860 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20861 {
20862 m_op_index = 0;
20863 address += baseaddr;
20864 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20865 }
20866
20867 /* Handle DW_LNS_advance_pc. */
20868 void handle_advance_pc (CORE_ADDR adjust);
20869
20870 /* Handle a special opcode. */
20871 void handle_special_opcode (unsigned char op_code);
20872
20873 /* Handle DW_LNS_advance_line. */
20874 void handle_advance_line (int line_delta)
20875 {
20876 advance_line (line_delta);
20877 }
20878
20879 /* Handle DW_LNS_set_file. */
20880 void handle_set_file (file_name_index file);
20881
20882 /* Handle DW_LNS_negate_stmt. */
20883 void handle_negate_stmt ()
20884 {
20885 m_is_stmt = !m_is_stmt;
20886 }
20887
20888 /* Handle DW_LNS_const_add_pc. */
20889 void handle_const_add_pc ();
20890
20891 /* Handle DW_LNS_fixed_advance_pc. */
20892 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20893 {
20894 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20895 m_op_index = 0;
20896 }
20897
20898 /* Handle DW_LNS_copy. */
20899 void handle_copy ()
20900 {
20901 record_line (false);
20902 m_discriminator = 0;
20903 }
20904
20905 /* Handle DW_LNE_end_sequence. */
20906 void handle_end_sequence ()
20907 {
20908 m_record_line_callback = ::record_line;
20909 }
20910
20911private:
20912 /* Advance the line by LINE_DELTA. */
20913 void advance_line (int line_delta)
20914 {
20915 m_line += line_delta;
20916
20917 if (line_delta != 0)
20918 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20919 }
20920
6f77053d
PA
20921 gdbarch *m_gdbarch;
20922
20923 /* True if we're recording lines.
20924 Otherwise we're building partial symtabs and are just interested in
20925 finding include files mentioned by the line number program. */
20926 bool m_record_lines_p;
20927
8c43009f 20928 /* The line number header. */
6f77053d 20929 line_header *m_line_header;
8c43009f 20930
6f77053d
PA
20931 /* These are part of the standard DWARF line number state machine,
20932 and initialized according to the DWARF spec. */
d9b3de22 20933
6f77053d 20934 unsigned char m_op_index = 0;
8c43009f 20935 /* The line table index (1-based) of the current file. */
6f77053d
PA
20936 file_name_index m_file = (file_name_index) 1;
20937 unsigned int m_line = 1;
20938
20939 /* These are initialized in the constructor. */
20940
20941 CORE_ADDR m_address;
20942 bool m_is_stmt;
20943 unsigned int m_discriminator;
d9b3de22
DE
20944
20945 /* Additional bits of state we need to track. */
20946
20947 /* The last file that we called dwarf2_start_subfile for.
20948 This is only used for TLLs. */
6f77053d 20949 unsigned int m_last_file = 0;
d9b3de22 20950 /* The last file a line number was recorded for. */
6f77053d 20951 struct subfile *m_last_subfile = NULL;
d9b3de22
DE
20952
20953 /* The function to call to record a line. */
6f77053d 20954 record_line_ftype *m_record_line_callback = NULL;
d9b3de22
DE
20955
20956 /* The last line number that was recorded, used to coalesce
20957 consecutive entries for the same line. This can happen, for
20958 example, when discriminators are present. PR 17276. */
6f77053d
PA
20959 unsigned int m_last_line = 0;
20960 bool m_line_has_non_zero_discriminator = false;
8c43009f 20961};
d9b3de22 20962
6f77053d
PA
20963void
20964lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20965{
20966 CORE_ADDR addr_adj = (((m_op_index + adjust)
20967 / m_line_header->maximum_ops_per_instruction)
20968 * m_line_header->minimum_instruction_length);
20969 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20970 m_op_index = ((m_op_index + adjust)
20971 % m_line_header->maximum_ops_per_instruction);
20972}
d9b3de22 20973
6f77053d
PA
20974void
20975lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20976{
6f77053d
PA
20977 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20978 CORE_ADDR addr_adj = (((m_op_index
20979 + (adj_opcode / m_line_header->line_range))
20980 / m_line_header->maximum_ops_per_instruction)
20981 * m_line_header->minimum_instruction_length);
20982 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20983 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20984 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20985
6f77053d
PA
20986 int line_delta = (m_line_header->line_base
20987 + (adj_opcode % m_line_header->line_range));
20988 advance_line (line_delta);
20989 record_line (false);
20990 m_discriminator = 0;
20991}
d9b3de22 20992
6f77053d
PA
20993void
20994lnp_state_machine::handle_set_file (file_name_index file)
20995{
20996 m_file = file;
20997
20998 const file_entry *fe = current_file ();
20999 if (fe == NULL)
21000 dwarf2_debug_line_missing_file_complaint ();
21001 else if (m_record_lines_p)
21002 {
21003 const char *dir = fe->include_dir (m_line_header);
21004
21005 m_last_subfile = current_subfile;
21006 m_line_has_non_zero_discriminator = m_discriminator != 0;
21007 dwarf2_start_subfile (fe->name, dir);
21008 }
21009}
21010
21011void
21012lnp_state_machine::handle_const_add_pc ()
21013{
21014 CORE_ADDR adjust
21015 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21016
21017 CORE_ADDR addr_adj
21018 = (((m_op_index + adjust)
21019 / m_line_header->maximum_ops_per_instruction)
21020 * m_line_header->minimum_instruction_length);
21021
21022 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21023 m_op_index = ((m_op_index + adjust)
21024 % m_line_header->maximum_ops_per_instruction);
21025}
d9b3de22 21026
c91513d8
PP
21027/* Ignore this record_line request. */
21028
21029static void
21030noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
21031{
21032 return;
21033}
21034
a05a36a5
DE
21035/* Return non-zero if we should add LINE to the line number table.
21036 LINE is the line to add, LAST_LINE is the last line that was added,
21037 LAST_SUBFILE is the subfile for LAST_LINE.
21038 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21039 had a non-zero discriminator.
21040
21041 We have to be careful in the presence of discriminators.
21042 E.g., for this line:
21043
21044 for (i = 0; i < 100000; i++);
21045
21046 clang can emit four line number entries for that one line,
21047 each with a different discriminator.
21048 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21049
21050 However, we want gdb to coalesce all four entries into one.
21051 Otherwise the user could stepi into the middle of the line and
21052 gdb would get confused about whether the pc really was in the
21053 middle of the line.
21054
21055 Things are further complicated by the fact that two consecutive
21056 line number entries for the same line is a heuristic used by gcc
21057 to denote the end of the prologue. So we can't just discard duplicate
21058 entries, we have to be selective about it. The heuristic we use is
21059 that we only collapse consecutive entries for the same line if at least
21060 one of those entries has a non-zero discriminator. PR 17276.
21061
21062 Note: Addresses in the line number state machine can never go backwards
21063 within one sequence, thus this coalescing is ok. */
21064
21065static int
21066dwarf_record_line_p (unsigned int line, unsigned int last_line,
21067 int line_has_non_zero_discriminator,
21068 struct subfile *last_subfile)
21069{
21070 if (current_subfile != last_subfile)
21071 return 1;
21072 if (line != last_line)
21073 return 1;
21074 /* Same line for the same file that we've seen already.
21075 As a last check, for pr 17276, only record the line if the line
21076 has never had a non-zero discriminator. */
21077 if (!line_has_non_zero_discriminator)
21078 return 1;
21079 return 0;
21080}
21081
252a6764
DE
21082/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
21083 in the line table of subfile SUBFILE. */
21084
21085static void
d9b3de22
DE
21086dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21087 unsigned int line, CORE_ADDR address,
21088 record_line_ftype p_record_line)
252a6764
DE
21089{
21090 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21091
27e0867f
DE
21092 if (dwarf_line_debug)
21093 {
21094 fprintf_unfiltered (gdb_stdlog,
21095 "Recording line %u, file %s, address %s\n",
21096 line, lbasename (subfile->name),
21097 paddress (gdbarch, address));
21098 }
21099
d5962de5 21100 (*p_record_line) (subfile, line, addr);
252a6764
DE
21101}
21102
21103/* Subroutine of dwarf_decode_lines_1 to simplify it.
21104 Mark the end of a set of line number records.
d9b3de22 21105 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
21106 If SUBFILE is NULL the request is ignored. */
21107
21108static void
21109dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21110 CORE_ADDR address, record_line_ftype p_record_line)
21111{
27e0867f
DE
21112 if (subfile == NULL)
21113 return;
21114
21115 if (dwarf_line_debug)
21116 {
21117 fprintf_unfiltered (gdb_stdlog,
21118 "Finishing current line, file %s, address %s\n",
21119 lbasename (subfile->name),
21120 paddress (gdbarch, address));
21121 }
21122
d9b3de22
DE
21123 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
21124}
21125
6f77053d
PA
21126void
21127lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21128{
d9b3de22
DE
21129 if (dwarf_line_debug)
21130 {
21131 fprintf_unfiltered (gdb_stdlog,
21132 "Processing actual line %u: file %u,"
21133 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
21134 m_line, to_underlying (m_file),
21135 paddress (m_gdbarch, m_address),
21136 m_is_stmt, m_discriminator);
d9b3de22
DE
21137 }
21138
6f77053d 21139 file_entry *fe = current_file ();
8c43009f
PA
21140
21141 if (fe == NULL)
d9b3de22
DE
21142 dwarf2_debug_line_missing_file_complaint ();
21143 /* For now we ignore lines not starting on an instruction boundary.
21144 But not when processing end_sequence for compatibility with the
21145 previous version of the code. */
6f77053d 21146 else if (m_op_index == 0 || end_sequence)
d9b3de22 21147 {
8c43009f 21148 fe->included_p = 1;
6f77053d 21149 if (m_record_lines_p && m_is_stmt)
d9b3de22 21150 {
6f77053d 21151 if (m_last_subfile != current_subfile || end_sequence)
d9b3de22 21152 {
6f77053d
PA
21153 dwarf_finish_line (m_gdbarch, m_last_subfile,
21154 m_address, m_record_line_callback);
d9b3de22
DE
21155 }
21156
21157 if (!end_sequence)
21158 {
6f77053d
PA
21159 if (dwarf_record_line_p (m_line, m_last_line,
21160 m_line_has_non_zero_discriminator,
21161 m_last_subfile))
d9b3de22 21162 {
6f77053d
PA
21163 dwarf_record_line_1 (m_gdbarch, current_subfile,
21164 m_line, m_address,
21165 m_record_line_callback);
d9b3de22 21166 }
6f77053d
PA
21167 m_last_subfile = current_subfile;
21168 m_last_line = m_line;
d9b3de22
DE
21169 }
21170 }
21171 }
21172}
21173
6f77053d
PA
21174lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
21175 bool record_lines_p)
d9b3de22 21176{
6f77053d
PA
21177 m_gdbarch = arch;
21178 m_record_lines_p = record_lines_p;
21179 m_line_header = lh;
d9b3de22 21180
6f77053d 21181 m_record_line_callback = ::record_line;
d9b3de22 21182
d9b3de22
DE
21183 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21184 was a line entry for it so that the backend has a chance to adjust it
21185 and also record it in case it needs it. This is currently used by MIPS
21186 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21187 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21188 m_is_stmt = lh->default_is_stmt;
21189 m_discriminator = 0;
252a6764
DE
21190}
21191
6f77053d
PA
21192void
21193lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21194 const gdb_byte *line_ptr,
21195 CORE_ADDR lowpc, CORE_ADDR address)
924c2928
DE
21196{
21197 /* If address < lowpc then it's not a usable value, it's outside the
21198 pc range of the CU. However, we restrict the test to only address
21199 values of zero to preserve GDB's previous behaviour which is to
21200 handle the specific case of a function being GC'd by the linker. */
21201
21202 if (address == 0 && address < lowpc)
21203 {
21204 /* This line table is for a function which has been
21205 GCd by the linker. Ignore it. PR gdb/12528 */
21206
518817b3 21207 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
21208 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21209
21210 complaint (&symfile_complaints,
21211 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21212 line_offset, objfile_name (objfile));
6f77053d
PA
21213 m_record_line_callback = noop_record_line;
21214 /* Note: record_line_callback is left as noop_record_line until
21215 we see DW_LNE_end_sequence. */
924c2928
DE
21216 }
21217}
21218
f3f5162e 21219/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21220 Process the line number information in LH.
21221 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21222 program in order to set included_p for every referenced header. */
debd256d 21223
c906108c 21224static void
43f3e411
DE
21225dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21226 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21227{
d521ce57
TT
21228 const gdb_byte *line_ptr, *extended_end;
21229 const gdb_byte *line_end;
a8c50c1f 21230 unsigned int bytes_read, extended_len;
699ca60a 21231 unsigned char op_code, extended_op;
e142c38c 21232 CORE_ADDR baseaddr;
518817b3 21233 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21234 bfd *abfd = objfile->obfd;
fbf65064 21235 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
21236 /* True if we're recording line info (as opposed to building partial
21237 symtabs and just interested in finding include files mentioned by
21238 the line number program). */
21239 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
21240
21241 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21242
debd256d
JB
21243 line_ptr = lh->statement_program_start;
21244 line_end = lh->statement_program_end;
c906108c
SS
21245
21246 /* Read the statement sequences until there's nothing left. */
21247 while (line_ptr < line_end)
21248 {
6f77053d
PA
21249 /* The DWARF line number program state machine. Reset the state
21250 machine at the start of each sequence. */
21251 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
21252 bool end_sequence = false;
d9b3de22 21253
8c43009f 21254 if (record_lines_p)
c906108c 21255 {
8c43009f
PA
21256 /* Start a subfile for the current file of the state
21257 machine. */
21258 const file_entry *fe = state_machine.current_file ();
21259
21260 if (fe != NULL)
21261 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
c906108c
SS
21262 }
21263
a738430d 21264 /* Decode the table. */
d9b3de22 21265 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21266 {
21267 op_code = read_1_byte (abfd, line_ptr);
21268 line_ptr += 1;
9aa1fe7e 21269
debd256d 21270 if (op_code >= lh->opcode_base)
6e70227d 21271 {
8e07a239 21272 /* Special opcode. */
6f77053d 21273 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21274 }
21275 else switch (op_code)
c906108c
SS
21276 {
21277 case DW_LNS_extended_op:
3e43a32a
MS
21278 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21279 &bytes_read);
473b7be6 21280 line_ptr += bytes_read;
a8c50c1f 21281 extended_end = line_ptr + extended_len;
c906108c
SS
21282 extended_op = read_1_byte (abfd, line_ptr);
21283 line_ptr += 1;
21284 switch (extended_op)
21285 {
21286 case DW_LNE_end_sequence:
6f77053d
PA
21287 state_machine.handle_end_sequence ();
21288 end_sequence = true;
c906108c
SS
21289 break;
21290 case DW_LNE_set_address:
d9b3de22
DE
21291 {
21292 CORE_ADDR address
21293 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 21294 line_ptr += bytes_read;
6f77053d
PA
21295
21296 state_machine.check_line_address (cu, line_ptr,
21297 lowpc, address);
21298 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21299 }
c906108c
SS
21300 break;
21301 case DW_LNE_define_file:
debd256d 21302 {
d521ce57 21303 const char *cur_file;
ecfb656c
PA
21304 unsigned int mod_time, length;
21305 dir_index dindex;
6e70227d 21306
3e43a32a
MS
21307 cur_file = read_direct_string (abfd, line_ptr,
21308 &bytes_read);
debd256d 21309 line_ptr += bytes_read;
ecfb656c 21310 dindex = (dir_index)
debd256d
JB
21311 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21312 line_ptr += bytes_read;
21313 mod_time =
21314 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21315 line_ptr += bytes_read;
21316 length =
21317 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21318 line_ptr += bytes_read;
ecfb656c 21319 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 21320 }
c906108c 21321 break;
d0c6ba3d 21322 case DW_LNE_set_discriminator:
6f77053d
PA
21323 {
21324 /* The discriminator is not interesting to the
21325 debugger; just ignore it. We still need to
21326 check its value though:
21327 if there are consecutive entries for the same
21328 (non-prologue) line we want to coalesce them.
21329 PR 17276. */
21330 unsigned int discr
21331 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21332 line_ptr += bytes_read;
21333
21334 state_machine.handle_set_discriminator (discr);
21335 }
d0c6ba3d 21336 break;
c906108c 21337 default:
4d3c2250 21338 complaint (&symfile_complaints,
e2e0b3e5 21339 _("mangled .debug_line section"));
debd256d 21340 return;
c906108c 21341 }
a8c50c1f
DJ
21342 /* Make sure that we parsed the extended op correctly. If e.g.
21343 we expected a different address size than the producer used,
21344 we may have read the wrong number of bytes. */
21345 if (line_ptr != extended_end)
21346 {
21347 complaint (&symfile_complaints,
21348 _("mangled .debug_line section"));
21349 return;
21350 }
c906108c
SS
21351 break;
21352 case DW_LNS_copy:
6f77053d 21353 state_machine.handle_copy ();
c906108c
SS
21354 break;
21355 case DW_LNS_advance_pc:
2dc7f7b3
TT
21356 {
21357 CORE_ADDR adjust
21358 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21359 line_ptr += bytes_read;
6f77053d
PA
21360
21361 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21362 }
c906108c
SS
21363 break;
21364 case DW_LNS_advance_line:
a05a36a5
DE
21365 {
21366 int line_delta
21367 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21368 line_ptr += bytes_read;
6f77053d
PA
21369
21370 state_machine.handle_advance_line (line_delta);
a05a36a5 21371 }
c906108c
SS
21372 break;
21373 case DW_LNS_set_file:
d9b3de22 21374 {
6f77053d 21375 file_name_index file
ecfb656c
PA
21376 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21377 &bytes_read);
d9b3de22 21378 line_ptr += bytes_read;
8c43009f 21379
6f77053d 21380 state_machine.handle_set_file (file);
d9b3de22 21381 }
c906108c
SS
21382 break;
21383 case DW_LNS_set_column:
0ad93d4f 21384 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21385 line_ptr += bytes_read;
21386 break;
21387 case DW_LNS_negate_stmt:
6f77053d 21388 state_machine.handle_negate_stmt ();
c906108c
SS
21389 break;
21390 case DW_LNS_set_basic_block:
c906108c 21391 break;
c2c6d25f
JM
21392 /* Add to the address register of the state machine the
21393 address increment value corresponding to special opcode
a738430d
MK
21394 255. I.e., this value is scaled by the minimum
21395 instruction length since special opcode 255 would have
b021a221 21396 scaled the increment. */
c906108c 21397 case DW_LNS_const_add_pc:
6f77053d 21398 state_machine.handle_const_add_pc ();
c906108c
SS
21399 break;
21400 case DW_LNS_fixed_advance_pc:
3e29f34a 21401 {
6f77053d 21402 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21403 line_ptr += 2;
6f77053d
PA
21404
21405 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21406 }
c906108c 21407 break;
9aa1fe7e 21408 default:
a738430d
MK
21409 {
21410 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21411 int i;
a738430d 21412
debd256d 21413 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21414 {
21415 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21416 line_ptr += bytes_read;
21417 }
21418 }
c906108c
SS
21419 }
21420 }
d9b3de22
DE
21421
21422 if (!end_sequence)
21423 dwarf2_debug_line_missing_end_sequence_complaint ();
21424
21425 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21426 in which case we still finish recording the last line). */
6f77053d 21427 state_machine.record_line (true);
c906108c 21428 }
f3f5162e
DE
21429}
21430
21431/* Decode the Line Number Program (LNP) for the given line_header
21432 structure and CU. The actual information extracted and the type
21433 of structures created from the LNP depends on the value of PST.
21434
21435 1. If PST is NULL, then this procedure uses the data from the program
21436 to create all necessary symbol tables, and their linetables.
21437
21438 2. If PST is not NULL, this procedure reads the program to determine
21439 the list of files included by the unit represented by PST, and
21440 builds all the associated partial symbol tables.
21441
21442 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21443 It is used for relative paths in the line table.
21444 NOTE: When processing partial symtabs (pst != NULL),
21445 comp_dir == pst->dirname.
21446
21447 NOTE: It is important that psymtabs have the same file name (via strcmp)
21448 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21449 symtab we don't use it in the name of the psymtabs we create.
21450 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21451 A good testcase for this is mb-inline.exp.
21452
527f3840
JK
21453 LOWPC is the lowest address in CU (or 0 if not known).
21454
21455 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21456 for its PC<->lines mapping information. Otherwise only the filename
21457 table is read in. */
f3f5162e
DE
21458
21459static void
21460dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 21461 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 21462 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21463{
518817b3 21464 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21465 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21466
527f3840
JK
21467 if (decode_mapping)
21468 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21469
21470 if (decode_for_pst_p)
21471 {
21472 int file_index;
21473
21474 /* Now that we're done scanning the Line Header Program, we can
21475 create the psymtab of each included file. */
fff8551c 21476 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
21477 if (lh->file_names[file_index].included_p == 1)
21478 {
c89b44cd 21479 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21480 const char *include_name =
c89b44cd
TT
21481 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21482 &name_holder);
c6da4cef 21483 if (include_name != NULL)
aaa75496
JB
21484 dwarf2_create_include_psymtab (include_name, pst, objfile);
21485 }
21486 }
cb1df416
DJ
21487 else
21488 {
21489 /* Make sure a symtab is created for every file, even files
21490 which contain only variables (i.e. no code with associated
21491 line numbers). */
43f3e411 21492 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 21493 int i;
cb1df416 21494
fff8551c 21495 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 21496 {
8c43009f 21497 file_entry &fe = lh->file_names[i];
9a619af0 21498
8c43009f 21499 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
cb1df416 21500
cb1df416 21501 if (current_subfile->symtab == NULL)
43f3e411
DE
21502 {
21503 current_subfile->symtab
21504 = allocate_symtab (cust, current_subfile->name);
21505 }
8c43009f 21506 fe.symtab = current_subfile->symtab;
cb1df416
DJ
21507 }
21508 }
c906108c
SS
21509}
21510
21511/* Start a subfile for DWARF. FILENAME is the name of the file and
21512 DIRNAME the name of the source directory which contains FILENAME
4d663531 21513 or NULL if not known.
c906108c
SS
21514 This routine tries to keep line numbers from identical absolute and
21515 relative file names in a common subfile.
21516
21517 Using the `list' example from the GDB testsuite, which resides in
21518 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21519 of /srcdir/list0.c yields the following debugging information for list0.c:
21520
c5aa993b 21521 DW_AT_name: /srcdir/list0.c
4d663531 21522 DW_AT_comp_dir: /compdir
357e46e7 21523 files.files[0].name: list0.h
c5aa993b 21524 files.files[0].dir: /srcdir
357e46e7 21525 files.files[1].name: list0.c
c5aa993b 21526 files.files[1].dir: /srcdir
c906108c
SS
21527
21528 The line number information for list0.c has to end up in a single
4f1520fb
FR
21529 subfile, so that `break /srcdir/list0.c:1' works as expected.
21530 start_subfile will ensure that this happens provided that we pass the
21531 concatenation of files.files[1].dir and files.files[1].name as the
21532 subfile's name. */
c906108c
SS
21533
21534static void
4d663531 21535dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 21536{
d521ce57 21537 char *copy = NULL;
4f1520fb 21538
4d663531 21539 /* In order not to lose the line information directory,
4f1520fb
FR
21540 we concatenate it to the filename when it makes sense.
21541 Note that the Dwarf3 standard says (speaking of filenames in line
21542 information): ``The directory index is ignored for file names
21543 that represent full path names''. Thus ignoring dirname in the
21544 `else' branch below isn't an issue. */
c906108c 21545
d5166ae1 21546 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
21547 {
21548 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21549 filename = copy;
21550 }
c906108c 21551
4d663531 21552 start_subfile (filename);
4f1520fb 21553
d521ce57
TT
21554 if (copy != NULL)
21555 xfree (copy);
c906108c
SS
21556}
21557
f4dc4d17
DE
21558/* Start a symtab for DWARF.
21559 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21560
43f3e411 21561static struct compunit_symtab *
f4dc4d17 21562dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 21563 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 21564{
43f3e411 21565 struct compunit_symtab *cust
518817b3
SM
21566 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21567 low_pc, cu->language);
43f3e411 21568
f4dc4d17
DE
21569 record_debugformat ("DWARF 2");
21570 record_producer (cu->producer);
21571
21572 /* We assume that we're processing GCC output. */
21573 processing_gcc_compilation = 2;
21574
4d4ec4e5 21575 cu->processing_has_namespace_info = 0;
43f3e411
DE
21576
21577 return cust;
f4dc4d17
DE
21578}
21579
4c2df51b
DJ
21580static void
21581var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21582 struct dwarf2_cu *cu)
4c2df51b 21583{
518817b3 21584 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21585 struct comp_unit_head *cu_header = &cu->header;
21586
4c2df51b
DJ
21587 /* NOTE drow/2003-01-30: There used to be a comment and some special
21588 code here to turn a symbol with DW_AT_external and a
21589 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21590 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21591 with some versions of binutils) where shared libraries could have
21592 relocations against symbols in their debug information - the
21593 minimal symbol would have the right address, but the debug info
21594 would not. It's no longer necessary, because we will explicitly
21595 apply relocations when we read in the debug information now. */
21596
21597 /* A DW_AT_location attribute with no contents indicates that a
21598 variable has been optimized away. */
21599 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21600 {
f1e6e072 21601 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21602 return;
21603 }
21604
21605 /* Handle one degenerate form of location expression specially, to
21606 preserve GDB's previous behavior when section offsets are
3019eac3
DE
21607 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21608 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21609
21610 if (attr_form_is_block (attr)
3019eac3
DE
21611 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21612 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21613 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21614 && (DW_BLOCK (attr)->size
21615 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21616 {
891d2f0b 21617 unsigned int dummy;
4c2df51b 21618
3019eac3
DE
21619 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21620 SYMBOL_VALUE_ADDRESS (sym) =
21621 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21622 else
21623 SYMBOL_VALUE_ADDRESS (sym) =
21624 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 21625 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
21626 fixup_symbol_section (sym, objfile);
21627 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21628 SYMBOL_SECTION (sym));
4c2df51b
DJ
21629 return;
21630 }
21631
21632 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21633 expression evaluator, and use LOC_COMPUTED only when necessary
21634 (i.e. when the value of a register or memory location is
21635 referenced, or a thread-local block, etc.). Then again, it might
21636 not be worthwhile. I'm assuming that it isn't unless performance
21637 or memory numbers show me otherwise. */
21638
f1e6e072 21639 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21640
f1e6e072 21641 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 21642 cu->has_loclist = 1;
4c2df51b
DJ
21643}
21644
c906108c
SS
21645/* Given a pointer to a DWARF information entry, figure out if we need
21646 to make a symbol table entry for it, and if so, create a new entry
21647 and return a pointer to it.
21648 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21649 used the passed type.
21650 If SPACE is not NULL, use it to hold the new symbol. If it is
21651 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21652
21653static struct symbol *
5e2db402
TT
21654new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21655 struct symbol *space)
c906108c 21656{
518817b3
SM
21657 struct dwarf2_per_objfile *dwarf2_per_objfile
21658 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21659 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21660 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21661 struct symbol *sym = NULL;
15d034d0 21662 const char *name;
c906108c
SS
21663 struct attribute *attr = NULL;
21664 struct attribute *attr2 = NULL;
e142c38c 21665 CORE_ADDR baseaddr;
e37fd15a
SW
21666 struct pending **list_to_add = NULL;
21667
edb3359d 21668 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
21669
21670 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21671
94af9270 21672 name = dwarf2_name (die, cu);
c906108c
SS
21673 if (name)
21674 {
94af9270 21675 const char *linkagename;
34eaf542 21676 int suppress_add = 0;
94af9270 21677
34eaf542
TT
21678 if (space)
21679 sym = space;
21680 else
e623cf5d 21681 sym = allocate_symbol (objfile);
c906108c 21682 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21683
21684 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 21685 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
21686 linkagename = dwarf2_physname (name, die, cu);
21687 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 21688
f55ee35c
JK
21689 /* Fortran does not have mangling standard and the mangling does differ
21690 between gfortran, iFort etc. */
21691 if (cu->language == language_fortran
b250c185 21692 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 21693 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 21694 dwarf2_full_name (name, die, cu),
29df156d 21695 NULL);
f55ee35c 21696
c906108c 21697 /* Default assumptions.
c5aa993b 21698 Use the passed type or decode it from the die. */
176620f1 21699 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21700 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21701 if (type != NULL)
21702 SYMBOL_TYPE (sym) = type;
21703 else
e7c27a73 21704 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21705 attr = dwarf2_attr (die,
21706 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21707 cu);
c906108c
SS
21708 if (attr)
21709 {
21710 SYMBOL_LINE (sym) = DW_UNSND (attr);
21711 }
cb1df416 21712
edb3359d
DJ
21713 attr = dwarf2_attr (die,
21714 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21715 cu);
cb1df416
DJ
21716 if (attr)
21717 {
ecfb656c 21718 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21719 struct file_entry *fe;
9a619af0 21720
ecfb656c
PA
21721 if (cu->line_header != NULL)
21722 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21723 else
21724 fe = NULL;
21725
21726 if (fe == NULL)
cb1df416
DJ
21727 complaint (&symfile_complaints,
21728 _("file index out of range"));
8c43009f
PA
21729 else
21730 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21731 }
21732
c906108c
SS
21733 switch (die->tag)
21734 {
21735 case DW_TAG_label:
e142c38c 21736 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 21737 if (attr)
3e29f34a
MR
21738 {
21739 CORE_ADDR addr;
21740
21741 addr = attr_value_as_address (attr);
21742 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21743 SYMBOL_VALUE_ADDRESS (sym) = addr;
21744 }
0f5238ed
TT
21745 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21746 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21747 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 21748 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21749 break;
21750 case DW_TAG_subprogram:
21751 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21752 finish_block. */
f1e6e072 21753 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21754 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
21755 if ((attr2 && (DW_UNSND (attr2) != 0))
21756 || cu->language == language_ada)
c906108c 21757 {
2cfa0c8d
JB
21758 /* Subprograms marked external are stored as a global symbol.
21759 Ada subprograms, whether marked external or not, are always
21760 stored as a global symbol, because we want to be able to
21761 access them globally. For instance, we want to be able
21762 to break on a nested subprogram without having to
21763 specify the context. */
e37fd15a 21764 list_to_add = &global_symbols;
c906108c
SS
21765 }
21766 else
21767 {
e37fd15a 21768 list_to_add = cu->list_in_scope;
c906108c
SS
21769 }
21770 break;
edb3359d
DJ
21771 case DW_TAG_inlined_subroutine:
21772 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21773 finish_block. */
f1e6e072 21774 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21775 SYMBOL_INLINED (sym) = 1;
481860b3 21776 list_to_add = cu->list_in_scope;
edb3359d 21777 break;
34eaf542
TT
21778 case DW_TAG_template_value_param:
21779 suppress_add = 1;
21780 /* Fall through. */
72929c62 21781 case DW_TAG_constant:
c906108c 21782 case DW_TAG_variable:
254e6b9e 21783 case DW_TAG_member:
0963b4bd
MS
21784 /* Compilation with minimal debug info may result in
21785 variables with missing type entries. Change the
21786 misleading `void' type to something sensible. */
c906108c 21787 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21788 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21789
e142c38c 21790 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21791 /* In the case of DW_TAG_member, we should only be called for
21792 static const members. */
21793 if (die->tag == DW_TAG_member)
21794 {
3863f96c
DE
21795 /* dwarf2_add_field uses die_is_declaration,
21796 so we do the same. */
254e6b9e
DE
21797 gdb_assert (die_is_declaration (die, cu));
21798 gdb_assert (attr);
21799 }
c906108c
SS
21800 if (attr)
21801 {
e7c27a73 21802 dwarf2_const_value (attr, sym, cu);
e142c38c 21803 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21804 if (!suppress_add)
34eaf542
TT
21805 {
21806 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 21807 list_to_add = &global_symbols;
34eaf542 21808 else
e37fd15a 21809 list_to_add = cu->list_in_scope;
34eaf542 21810 }
c906108c
SS
21811 break;
21812 }
e142c38c 21813 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21814 if (attr)
21815 {
e7c27a73 21816 var_decode_location (attr, sym, cu);
e142c38c 21817 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21818
21819 /* Fortran explicitly imports any global symbols to the local
21820 scope by DW_TAG_common_block. */
21821 if (cu->language == language_fortran && die->parent
21822 && die->parent->tag == DW_TAG_common_block)
21823 attr2 = NULL;
21824
caac4577
JG
21825 if (SYMBOL_CLASS (sym) == LOC_STATIC
21826 && SYMBOL_VALUE_ADDRESS (sym) == 0
21827 && !dwarf2_per_objfile->has_section_at_zero)
21828 {
21829 /* When a static variable is eliminated by the linker,
21830 the corresponding debug information is not stripped
21831 out, but the variable address is set to null;
21832 do not add such variables into symbol table. */
21833 }
21834 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21835 {
f55ee35c
JK
21836 /* Workaround gfortran PR debug/40040 - it uses
21837 DW_AT_location for variables in -fPIC libraries which may
21838 get overriden by other libraries/executable and get
21839 a different address. Resolve it by the minimal symbol
21840 which may come from inferior's executable using copy
21841 relocation. Make this workaround only for gfortran as for
21842 other compilers GDB cannot guess the minimal symbol
21843 Fortran mangling kind. */
21844 if (cu->language == language_fortran && die->parent
21845 && die->parent->tag == DW_TAG_module
21846 && cu->producer
28586665 21847 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 21848 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 21849
1c809c68
TT
21850 /* A variable with DW_AT_external is never static,
21851 but it may be block-scoped. */
21852 list_to_add = (cu->list_in_scope == &file_symbols
21853 ? &global_symbols : cu->list_in_scope);
1c809c68 21854 }
c906108c 21855 else
e37fd15a 21856 list_to_add = cu->list_in_scope;
c906108c
SS
21857 }
21858 else
21859 {
21860 /* We do not know the address of this symbol.
c5aa993b
JM
21861 If it is an external symbol and we have type information
21862 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21863 The address of the variable will then be determined from
21864 the minimal symbol table whenever the variable is
21865 referenced. */
e142c38c 21866 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21867
21868 /* Fortran explicitly imports any global symbols to the local
21869 scope by DW_TAG_common_block. */
21870 if (cu->language == language_fortran && die->parent
21871 && die->parent->tag == DW_TAG_common_block)
21872 {
21873 /* SYMBOL_CLASS doesn't matter here because
21874 read_common_block is going to reset it. */
21875 if (!suppress_add)
21876 list_to_add = cu->list_in_scope;
21877 }
21878 else if (attr2 && (DW_UNSND (attr2) != 0)
21879 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21880 {
0fe7935b
DJ
21881 /* A variable with DW_AT_external is never static, but it
21882 may be block-scoped. */
21883 list_to_add = (cu->list_in_scope == &file_symbols
21884 ? &global_symbols : cu->list_in_scope);
21885
f1e6e072 21886 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21887 }
442ddf59
JK
21888 else if (!die_is_declaration (die, cu))
21889 {
21890 /* Use the default LOC_OPTIMIZED_OUT class. */
21891 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21892 if (!suppress_add)
21893 list_to_add = cu->list_in_scope;
442ddf59 21894 }
c906108c
SS
21895 }
21896 break;
21897 case DW_TAG_formal_parameter:
edb3359d
DJ
21898 /* If we are inside a function, mark this as an argument. If
21899 not, we might be looking at an argument to an inlined function
21900 when we do not have enough information to show inlined frames;
21901 pretend it's a local variable in that case so that the user can
21902 still see it. */
21903 if (context_stack_depth > 0
21904 && context_stack[context_stack_depth - 1].name != NULL)
21905 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 21906 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21907 if (attr)
21908 {
e7c27a73 21909 var_decode_location (attr, sym, cu);
c906108c 21910 }
e142c38c 21911 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21912 if (attr)
21913 {
e7c27a73 21914 dwarf2_const_value (attr, sym, cu);
c906108c 21915 }
f346a30d 21916
e37fd15a 21917 list_to_add = cu->list_in_scope;
c906108c
SS
21918 break;
21919 case DW_TAG_unspecified_parameters:
21920 /* From varargs functions; gdb doesn't seem to have any
21921 interest in this information, so just ignore it for now.
21922 (FIXME?) */
21923 break;
34eaf542
TT
21924 case DW_TAG_template_type_param:
21925 suppress_add = 1;
21926 /* Fall through. */
c906108c 21927 case DW_TAG_class_type:
680b30c7 21928 case DW_TAG_interface_type:
c906108c
SS
21929 case DW_TAG_structure_type:
21930 case DW_TAG_union_type:
72019c9c 21931 case DW_TAG_set_type:
c906108c 21932 case DW_TAG_enumeration_type:
f1e6e072 21933 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21934 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21935
63d06c5c 21936 {
9c37b5ae 21937 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21938 really ever be static objects: otherwise, if you try
21939 to, say, break of a class's method and you're in a file
21940 which doesn't mention that class, it won't work unless
21941 the check for all static symbols in lookup_symbol_aux
21942 saves you. See the OtherFileClass tests in
21943 gdb.c++/namespace.exp. */
21944
e37fd15a 21945 if (!suppress_add)
34eaf542 21946 {
34eaf542 21947 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 21948 && cu->language == language_cplus
34eaf542 21949 ? &global_symbols : cu->list_in_scope);
63d06c5c 21950
64382290 21951 /* The semantics of C++ state that "struct foo {
9c37b5ae 21952 ... }" also defines a typedef for "foo". */
64382290 21953 if (cu->language == language_cplus
45280282 21954 || cu->language == language_ada
c44af4eb
TT
21955 || cu->language == language_d
21956 || cu->language == language_rust)
64382290
TT
21957 {
21958 /* The symbol's name is already allocated along
21959 with this objfile, so we don't need to
21960 duplicate it for the type. */
21961 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21962 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21963 }
63d06c5c
DC
21964 }
21965 }
c906108c
SS
21966 break;
21967 case DW_TAG_typedef:
f1e6e072 21968 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21969 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21970 list_to_add = cu->list_in_scope;
63d06c5c 21971 break;
c906108c 21972 case DW_TAG_base_type:
a02abb62 21973 case DW_TAG_subrange_type:
f1e6e072 21974 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21975 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21976 list_to_add = cu->list_in_scope;
c906108c
SS
21977 break;
21978 case DW_TAG_enumerator:
e142c38c 21979 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21980 if (attr)
21981 {
e7c27a73 21982 dwarf2_const_value (attr, sym, cu);
c906108c 21983 }
63d06c5c
DC
21984 {
21985 /* NOTE: carlton/2003-11-10: See comment above in the
21986 DW_TAG_class_type, etc. block. */
21987
e142c38c 21988 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 21989 && cu->language == language_cplus
e142c38c 21990 ? &global_symbols : cu->list_in_scope);
63d06c5c 21991 }
c906108c 21992 break;
74921315 21993 case DW_TAG_imported_declaration:
5c4e30ca 21994 case DW_TAG_namespace:
f1e6e072 21995 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 21996 list_to_add = &global_symbols;
5c4e30ca 21997 break;
530e8392
KB
21998 case DW_TAG_module:
21999 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22000 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22001 list_to_add = &global_symbols;
22002 break;
4357ac6c 22003 case DW_TAG_common_block:
f1e6e072 22004 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
22005 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22006 add_symbol_to_list (sym, cu->list_in_scope);
22007 break;
c906108c
SS
22008 default:
22009 /* Not a tag we recognize. Hopefully we aren't processing
22010 trash data, but since we must specifically ignore things
22011 we don't recognize, there is nothing else we should do at
0963b4bd 22012 this point. */
e2e0b3e5 22013 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 22014 dwarf_tag_name (die->tag));
c906108c
SS
22015 break;
22016 }
df8a16a1 22017
e37fd15a
SW
22018 if (suppress_add)
22019 {
22020 sym->hash_next = objfile->template_symbols;
22021 objfile->template_symbols = sym;
22022 list_to_add = NULL;
22023 }
22024
22025 if (list_to_add != NULL)
22026 add_symbol_to_list (sym, list_to_add);
22027
df8a16a1
DJ
22028 /* For the benefit of old versions of GCC, check for anonymous
22029 namespaces based on the demangled name. */
4d4ec4e5 22030 if (!cu->processing_has_namespace_info
94af9270 22031 && cu->language == language_cplus)
a10964d1 22032 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
22033 }
22034 return (sym);
22035}
22036
98bfdba5
PA
22037/* Given an attr with a DW_FORM_dataN value in host byte order,
22038 zero-extend it as appropriate for the symbol's type. The DWARF
22039 standard (v4) is not entirely clear about the meaning of using
22040 DW_FORM_dataN for a constant with a signed type, where the type is
22041 wider than the data. The conclusion of a discussion on the DWARF
22042 list was that this is unspecified. We choose to always zero-extend
22043 because that is the interpretation long in use by GCC. */
c906108c 22044
98bfdba5 22045static gdb_byte *
ff39bb5e 22046dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 22047 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 22048{
518817b3 22049 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
22050 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22051 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
22052 LONGEST l = DW_UNSND (attr);
22053
22054 if (bits < sizeof (*value) * 8)
22055 {
22056 l &= ((LONGEST) 1 << bits) - 1;
22057 *value = l;
22058 }
22059 else if (bits == sizeof (*value) * 8)
22060 *value = l;
22061 else
22062 {
224c3ddb 22063 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
22064 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22065 return bytes;
22066 }
22067
22068 return NULL;
22069}
22070
22071/* Read a constant value from an attribute. Either set *VALUE, or if
22072 the value does not fit in *VALUE, set *BYTES - either already
22073 allocated on the objfile obstack, or newly allocated on OBSTACK,
22074 or, set *BATON, if we translated the constant to a location
22075 expression. */
22076
22077static void
ff39bb5e 22078dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
22079 const char *name, struct obstack *obstack,
22080 struct dwarf2_cu *cu,
d521ce57 22081 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
22082 struct dwarf2_locexpr_baton **baton)
22083{
518817b3 22084 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 22085 struct comp_unit_head *cu_header = &cu->header;
c906108c 22086 struct dwarf_block *blk;
98bfdba5
PA
22087 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22088 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22089
22090 *value = 0;
22091 *bytes = NULL;
22092 *baton = NULL;
c906108c
SS
22093
22094 switch (attr->form)
22095 {
22096 case DW_FORM_addr:
3019eac3 22097 case DW_FORM_GNU_addr_index:
ac56253d 22098 {
ac56253d
TT
22099 gdb_byte *data;
22100
98bfdba5
PA
22101 if (TYPE_LENGTH (type) != cu_header->addr_size)
22102 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22103 cu_header->addr_size,
98bfdba5 22104 TYPE_LENGTH (type));
ac56253d
TT
22105 /* Symbols of this form are reasonably rare, so we just
22106 piggyback on the existing location code rather than writing
22107 a new implementation of symbol_computed_ops. */
8d749320 22108 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
22109 (*baton)->per_cu = cu->per_cu;
22110 gdb_assert ((*baton)->per_cu);
ac56253d 22111
98bfdba5 22112 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22113 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22114 (*baton)->data = data;
ac56253d
TT
22115
22116 data[0] = DW_OP_addr;
22117 store_unsigned_integer (&data[1], cu_header->addr_size,
22118 byte_order, DW_ADDR (attr));
22119 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22120 }
c906108c 22121 break;
4ac36638 22122 case DW_FORM_string:
93b5768b 22123 case DW_FORM_strp:
3019eac3 22124 case DW_FORM_GNU_str_index:
36586728 22125 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
22126 /* DW_STRING is already allocated on the objfile obstack, point
22127 directly to it. */
d521ce57 22128 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 22129 break;
c906108c
SS
22130 case DW_FORM_block1:
22131 case DW_FORM_block2:
22132 case DW_FORM_block4:
22133 case DW_FORM_block:
2dc7f7b3 22134 case DW_FORM_exprloc:
0224619f 22135 case DW_FORM_data16:
c906108c 22136 blk = DW_BLOCK (attr);
98bfdba5
PA
22137 if (TYPE_LENGTH (type) != blk->size)
22138 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22139 TYPE_LENGTH (type));
22140 *bytes = blk->data;
c906108c 22141 break;
2df3850c
JM
22142
22143 /* The DW_AT_const_value attributes are supposed to carry the
22144 symbol's value "represented as it would be on the target
22145 architecture." By the time we get here, it's already been
22146 converted to host endianness, so we just need to sign- or
22147 zero-extend it as appropriate. */
22148 case DW_FORM_data1:
3aef2284 22149 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22150 break;
c906108c 22151 case DW_FORM_data2:
3aef2284 22152 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22153 break;
c906108c 22154 case DW_FORM_data4:
3aef2284 22155 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22156 break;
c906108c 22157 case DW_FORM_data8:
3aef2284 22158 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22159 break;
22160
c906108c 22161 case DW_FORM_sdata:
663c44ac 22162 case DW_FORM_implicit_const:
98bfdba5 22163 *value = DW_SND (attr);
2df3850c
JM
22164 break;
22165
c906108c 22166 case DW_FORM_udata:
98bfdba5 22167 *value = DW_UNSND (attr);
c906108c 22168 break;
2df3850c 22169
c906108c 22170 default:
4d3c2250 22171 complaint (&symfile_complaints,
e2e0b3e5 22172 _("unsupported const value attribute form: '%s'"),
4d3c2250 22173 dwarf_form_name (attr->form));
98bfdba5 22174 *value = 0;
c906108c
SS
22175 break;
22176 }
22177}
22178
2df3850c 22179
98bfdba5
PA
22180/* Copy constant value from an attribute to a symbol. */
22181
2df3850c 22182static void
ff39bb5e 22183dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22184 struct dwarf2_cu *cu)
2df3850c 22185{
518817b3 22186 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 22187 LONGEST value;
d521ce57 22188 const gdb_byte *bytes;
98bfdba5 22189 struct dwarf2_locexpr_baton *baton;
2df3850c 22190
98bfdba5
PA
22191 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22192 SYMBOL_PRINT_NAME (sym),
22193 &objfile->objfile_obstack, cu,
22194 &value, &bytes, &baton);
2df3850c 22195
98bfdba5
PA
22196 if (baton != NULL)
22197 {
98bfdba5 22198 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22199 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22200 }
22201 else if (bytes != NULL)
22202 {
22203 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22204 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22205 }
22206 else
22207 {
22208 SYMBOL_VALUE (sym) = value;
f1e6e072 22209 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22210 }
2df3850c
JM
22211}
22212
c906108c
SS
22213/* Return the type of the die in question using its DW_AT_type attribute. */
22214
22215static struct type *
e7c27a73 22216die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22217{
c906108c 22218 struct attribute *type_attr;
c906108c 22219
e142c38c 22220 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22221 if (!type_attr)
22222 {
518817b3 22223 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22224 /* A missing DW_AT_type represents a void type. */
518817b3 22225 return objfile_type (objfile)->builtin_void;
c906108c 22226 }
348e048f 22227
673bfd45 22228 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22229}
22230
b4ba55a1
JB
22231/* True iff CU's producer generates GNAT Ada auxiliary information
22232 that allows to find parallel types through that information instead
22233 of having to do expensive parallel lookups by type name. */
22234
22235static int
22236need_gnat_info (struct dwarf2_cu *cu)
22237{
de4cb04a
JB
22238 /* Assume that the Ada compiler was GNAT, which always produces
22239 the auxiliary information. */
22240 return (cu->language == language_ada);
b4ba55a1
JB
22241}
22242
b4ba55a1
JB
22243/* Return the auxiliary type of the die in question using its
22244 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22245 attribute is not present. */
22246
22247static struct type *
22248die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22249{
b4ba55a1 22250 struct attribute *type_attr;
b4ba55a1
JB
22251
22252 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22253 if (!type_attr)
22254 return NULL;
22255
673bfd45 22256 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22257}
22258
22259/* If DIE has a descriptive_type attribute, then set the TYPE's
22260 descriptive type accordingly. */
22261
22262static void
22263set_descriptive_type (struct type *type, struct die_info *die,
22264 struct dwarf2_cu *cu)
22265{
22266 struct type *descriptive_type = die_descriptive_type (die, cu);
22267
22268 if (descriptive_type)
22269 {
22270 ALLOCATE_GNAT_AUX_TYPE (type);
22271 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22272 }
22273}
22274
c906108c
SS
22275/* Return the containing type of the die in question using its
22276 DW_AT_containing_type attribute. */
22277
22278static struct type *
e7c27a73 22279die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22280{
c906108c 22281 struct attribute *type_attr;
518817b3 22282 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22283
e142c38c 22284 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22285 if (!type_attr)
22286 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22287 "[in module %s]"), objfile_name (objfile));
33ac96f0 22288
673bfd45 22289 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22290}
22291
ac9ec31b
DE
22292/* Return an error marker type to use for the ill formed type in DIE/CU. */
22293
22294static struct type *
22295build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22296{
518817b3
SM
22297 struct dwarf2_per_objfile *dwarf2_per_objfile
22298 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
22299 struct objfile *objfile = dwarf2_per_objfile->objfile;
22300 char *message, *saved;
22301
9d8780f0 22302 message = xstrprintf (_("<unknown type in %s, CU %s, DIE %s>"),
4262abfb 22303 objfile_name (objfile),
9d8780f0
SM
22304 sect_offset_str (cu->header.sect_off),
22305 sect_offset_str (die->sect_off));
224c3ddb
SM
22306 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
22307 message, strlen (message));
ac9ec31b
DE
22308 xfree (message);
22309
19f392bc 22310 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22311}
22312
673bfd45 22313/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22314 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22315 DW_AT_containing_type.
673bfd45
DE
22316 If there is no type substitute an error marker. */
22317
c906108c 22318static struct type *
ff39bb5e 22319lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22320 struct dwarf2_cu *cu)
c906108c 22321{
518817b3
SM
22322 struct dwarf2_per_objfile *dwarf2_per_objfile
22323 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22324 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
22325 struct type *this_type;
22326
ac9ec31b
DE
22327 gdb_assert (attr->name == DW_AT_type
22328 || attr->name == DW_AT_GNAT_descriptive_type
22329 || attr->name == DW_AT_containing_type);
22330
673bfd45
DE
22331 /* First see if we have it cached. */
22332
36586728
TT
22333 if (attr->form == DW_FORM_GNU_ref_alt)
22334 {
22335 struct dwarf2_per_cu_data *per_cu;
9c541725 22336 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 22337
ed2dc618
SM
22338 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22339 dwarf2_per_objfile);
9c541725 22340 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 22341 }
7771576e 22342 else if (attr_form_is_ref (attr))
673bfd45 22343 {
9c541725 22344 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 22345
9c541725 22346 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 22347 }
55f1336d 22348 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22349 {
ac9ec31b 22350 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 22351
ac9ec31b 22352 return get_signatured_type (die, signature, cu);
673bfd45
DE
22353 }
22354 else
22355 {
ac9ec31b
DE
22356 complaint (&symfile_complaints,
22357 _("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22358 " at %s [in module %s]"),
22359 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22360 objfile_name (objfile));
ac9ec31b 22361 return build_error_marker_type (cu, die);
673bfd45
DE
22362 }
22363
22364 /* If not cached we need to read it in. */
22365
22366 if (this_type == NULL)
22367 {
ac9ec31b 22368 struct die_info *type_die = NULL;
673bfd45
DE
22369 struct dwarf2_cu *type_cu = cu;
22370
7771576e 22371 if (attr_form_is_ref (attr))
ac9ec31b
DE
22372 type_die = follow_die_ref (die, attr, &type_cu);
22373 if (type_die == NULL)
22374 return build_error_marker_type (cu, die);
22375 /* If we find the type now, it's probably because the type came
3019eac3
DE
22376 from an inter-CU reference and the type's CU got expanded before
22377 ours. */
ac9ec31b 22378 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22379 }
22380
22381 /* If we still don't have a type use an error marker. */
22382
22383 if (this_type == NULL)
ac9ec31b 22384 return build_error_marker_type (cu, die);
673bfd45 22385
f792889a 22386 return this_type;
c906108c
SS
22387}
22388
673bfd45
DE
22389/* Return the type in DIE, CU.
22390 Returns NULL for invalid types.
22391
02142a6c 22392 This first does a lookup in die_type_hash,
673bfd45
DE
22393 and only reads the die in if necessary.
22394
22395 NOTE: This can be called when reading in partial or full symbols. */
22396
f792889a 22397static struct type *
e7c27a73 22398read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22399{
f792889a
DJ
22400 struct type *this_type;
22401
22402 this_type = get_die_type (die, cu);
22403 if (this_type)
22404 return this_type;
22405
673bfd45
DE
22406 return read_type_die_1 (die, cu);
22407}
22408
22409/* Read the type in DIE, CU.
22410 Returns NULL for invalid types. */
22411
22412static struct type *
22413read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22414{
22415 struct type *this_type = NULL;
22416
c906108c
SS
22417 switch (die->tag)
22418 {
22419 case DW_TAG_class_type:
680b30c7 22420 case DW_TAG_interface_type:
c906108c
SS
22421 case DW_TAG_structure_type:
22422 case DW_TAG_union_type:
f792889a 22423 this_type = read_structure_type (die, cu);
c906108c
SS
22424 break;
22425 case DW_TAG_enumeration_type:
f792889a 22426 this_type = read_enumeration_type (die, cu);
c906108c
SS
22427 break;
22428 case DW_TAG_subprogram:
22429 case DW_TAG_subroutine_type:
edb3359d 22430 case DW_TAG_inlined_subroutine:
f792889a 22431 this_type = read_subroutine_type (die, cu);
c906108c
SS
22432 break;
22433 case DW_TAG_array_type:
f792889a 22434 this_type = read_array_type (die, cu);
c906108c 22435 break;
72019c9c 22436 case DW_TAG_set_type:
f792889a 22437 this_type = read_set_type (die, cu);
72019c9c 22438 break;
c906108c 22439 case DW_TAG_pointer_type:
f792889a 22440 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22441 break;
22442 case DW_TAG_ptr_to_member_type:
f792889a 22443 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22444 break;
22445 case DW_TAG_reference_type:
4297a3f0
AV
22446 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22447 break;
22448 case DW_TAG_rvalue_reference_type:
22449 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22450 break;
22451 case DW_TAG_const_type:
f792889a 22452 this_type = read_tag_const_type (die, cu);
c906108c
SS
22453 break;
22454 case DW_TAG_volatile_type:
f792889a 22455 this_type = read_tag_volatile_type (die, cu);
c906108c 22456 break;
06d66ee9
TT
22457 case DW_TAG_restrict_type:
22458 this_type = read_tag_restrict_type (die, cu);
22459 break;
c906108c 22460 case DW_TAG_string_type:
f792889a 22461 this_type = read_tag_string_type (die, cu);
c906108c
SS
22462 break;
22463 case DW_TAG_typedef:
f792889a 22464 this_type = read_typedef (die, cu);
c906108c 22465 break;
a02abb62 22466 case DW_TAG_subrange_type:
f792889a 22467 this_type = read_subrange_type (die, cu);
a02abb62 22468 break;
c906108c 22469 case DW_TAG_base_type:
f792889a 22470 this_type = read_base_type (die, cu);
c906108c 22471 break;
81a17f79 22472 case DW_TAG_unspecified_type:
f792889a 22473 this_type = read_unspecified_type (die, cu);
81a17f79 22474 break;
0114d602
DJ
22475 case DW_TAG_namespace:
22476 this_type = read_namespace_type (die, cu);
22477 break;
f55ee35c
JK
22478 case DW_TAG_module:
22479 this_type = read_module_type (die, cu);
22480 break;
a2c2acaf
MW
22481 case DW_TAG_atomic_type:
22482 this_type = read_tag_atomic_type (die, cu);
22483 break;
c906108c 22484 default:
3e43a32a
MS
22485 complaint (&symfile_complaints,
22486 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 22487 dwarf_tag_name (die->tag));
c906108c
SS
22488 break;
22489 }
63d06c5c 22490
f792889a 22491 return this_type;
63d06c5c
DC
22492}
22493
abc72ce4
DE
22494/* See if we can figure out if the class lives in a namespace. We do
22495 this by looking for a member function; its demangled name will
22496 contain namespace info, if there is any.
22497 Return the computed name or NULL.
22498 Space for the result is allocated on the objfile's obstack.
22499 This is the full-die version of guess_partial_die_structure_name.
22500 In this case we know DIE has no useful parent. */
22501
22502static char *
22503guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22504{
22505 struct die_info *spec_die;
22506 struct dwarf2_cu *spec_cu;
22507 struct die_info *child;
518817b3 22508 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
22509
22510 spec_cu = cu;
22511 spec_die = die_specification (die, &spec_cu);
22512 if (spec_die != NULL)
22513 {
22514 die = spec_die;
22515 cu = spec_cu;
22516 }
22517
22518 for (child = die->child;
22519 child != NULL;
22520 child = child->sibling)
22521 {
22522 if (child->tag == DW_TAG_subprogram)
22523 {
73b9be8b 22524 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22525
7d45c7c3 22526 if (linkage_name != NULL)
abc72ce4
DE
22527 {
22528 char *actual_name
22529 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 22530 linkage_name);
abc72ce4
DE
22531 char *name = NULL;
22532
22533 if (actual_name != NULL)
22534 {
15d034d0 22535 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22536
22537 if (die_name != NULL
22538 && strcmp (die_name, actual_name) != 0)
22539 {
22540 /* Strip off the class name from the full name.
22541 We want the prefix. */
22542 int die_name_len = strlen (die_name);
22543 int actual_name_len = strlen (actual_name);
22544
22545 /* Test for '::' as a sanity check. */
22546 if (actual_name_len > die_name_len + 2
3e43a32a
MS
22547 && actual_name[actual_name_len
22548 - die_name_len - 1] == ':')
224c3ddb 22549 name = (char *) obstack_copy0 (
e3b94546 22550 &objfile->per_bfd->storage_obstack,
224c3ddb 22551 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
22552 }
22553 }
22554 xfree (actual_name);
22555 return name;
22556 }
22557 }
22558 }
22559
22560 return NULL;
22561}
22562
96408a79
SA
22563/* GCC might emit a nameless typedef that has a linkage name. Determine the
22564 prefix part in such case. See
22565 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22566
a121b7c1 22567static const char *
96408a79
SA
22568anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22569{
22570 struct attribute *attr;
e6a959d6 22571 const char *base;
96408a79
SA
22572
22573 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22574 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22575 return NULL;
22576
7d45c7c3 22577 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22578 return NULL;
22579
73b9be8b 22580 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22581 if (attr == NULL || DW_STRING (attr) == NULL)
22582 return NULL;
22583
22584 /* dwarf2_name had to be already called. */
22585 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22586
22587 /* Strip the base name, keep any leading namespaces/classes. */
22588 base = strrchr (DW_STRING (attr), ':');
22589 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22590 return "";
22591
518817b3 22592 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e3b94546 22593 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb
SM
22594 DW_STRING (attr),
22595 &base[-1] - DW_STRING (attr));
96408a79
SA
22596}
22597
fdde2d81 22598/* Return the name of the namespace/class that DIE is defined within,
0114d602 22599 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22600
0114d602
DJ
22601 For example, if we're within the method foo() in the following
22602 code:
22603
22604 namespace N {
22605 class C {
22606 void foo () {
22607 }
22608 };
22609 }
22610
22611 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22612
0d5cff50 22613static const char *
e142c38c 22614determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22615{
518817b3
SM
22616 struct dwarf2_per_objfile *dwarf2_per_objfile
22617 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22618 struct die_info *parent, *spec_die;
22619 struct dwarf2_cu *spec_cu;
22620 struct type *parent_type;
a121b7c1 22621 const char *retval;
63d06c5c 22622
9c37b5ae 22623 if (cu->language != language_cplus
c44af4eb
TT
22624 && cu->language != language_fortran && cu->language != language_d
22625 && cu->language != language_rust)
0114d602
DJ
22626 return "";
22627
96408a79
SA
22628 retval = anonymous_struct_prefix (die, cu);
22629 if (retval)
22630 return retval;
22631
0114d602
DJ
22632 /* We have to be careful in the presence of DW_AT_specification.
22633 For example, with GCC 3.4, given the code
22634
22635 namespace N {
22636 void foo() {
22637 // Definition of N::foo.
22638 }
22639 }
22640
22641 then we'll have a tree of DIEs like this:
22642
22643 1: DW_TAG_compile_unit
22644 2: DW_TAG_namespace // N
22645 3: DW_TAG_subprogram // declaration of N::foo
22646 4: DW_TAG_subprogram // definition of N::foo
22647 DW_AT_specification // refers to die #3
22648
22649 Thus, when processing die #4, we have to pretend that we're in
22650 the context of its DW_AT_specification, namely the contex of die
22651 #3. */
22652 spec_cu = cu;
22653 spec_die = die_specification (die, &spec_cu);
22654 if (spec_die == NULL)
22655 parent = die->parent;
22656 else
63d06c5c 22657 {
0114d602
DJ
22658 parent = spec_die->parent;
22659 cu = spec_cu;
63d06c5c 22660 }
0114d602
DJ
22661
22662 if (parent == NULL)
22663 return "";
98bfdba5
PA
22664 else if (parent->building_fullname)
22665 {
22666 const char *name;
22667 const char *parent_name;
22668
22669 /* It has been seen on RealView 2.2 built binaries,
22670 DW_TAG_template_type_param types actually _defined_ as
22671 children of the parent class:
22672
22673 enum E {};
22674 template class <class Enum> Class{};
22675 Class<enum E> class_e;
22676
22677 1: DW_TAG_class_type (Class)
22678 2: DW_TAG_enumeration_type (E)
22679 3: DW_TAG_enumerator (enum1:0)
22680 3: DW_TAG_enumerator (enum2:1)
22681 ...
22682 2: DW_TAG_template_type_param
22683 DW_AT_type DW_FORM_ref_udata (E)
22684
22685 Besides being broken debug info, it can put GDB into an
22686 infinite loop. Consider:
22687
22688 When we're building the full name for Class<E>, we'll start
22689 at Class, and go look over its template type parameters,
22690 finding E. We'll then try to build the full name of E, and
22691 reach here. We're now trying to build the full name of E,
22692 and look over the parent DIE for containing scope. In the
22693 broken case, if we followed the parent DIE of E, we'd again
22694 find Class, and once again go look at its template type
22695 arguments, etc., etc. Simply don't consider such parent die
22696 as source-level parent of this die (it can't be, the language
22697 doesn't allow it), and break the loop here. */
22698 name = dwarf2_name (die, cu);
22699 parent_name = dwarf2_name (parent, cu);
22700 complaint (&symfile_complaints,
22701 _("template param type '%s' defined within parent '%s'"),
22702 name ? name : "<unknown>",
22703 parent_name ? parent_name : "<unknown>");
22704 return "";
22705 }
63d06c5c 22706 else
0114d602
DJ
22707 switch (parent->tag)
22708 {
63d06c5c 22709 case DW_TAG_namespace:
0114d602 22710 parent_type = read_type_die (parent, cu);
acebe513
UW
22711 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22712 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22713 Work around this problem here. */
22714 if (cu->language == language_cplus
22715 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22716 return "";
0114d602
DJ
22717 /* We give a name to even anonymous namespaces. */
22718 return TYPE_TAG_NAME (parent_type);
63d06c5c 22719 case DW_TAG_class_type:
680b30c7 22720 case DW_TAG_interface_type:
63d06c5c 22721 case DW_TAG_structure_type:
0114d602 22722 case DW_TAG_union_type:
f55ee35c 22723 case DW_TAG_module:
0114d602
DJ
22724 parent_type = read_type_die (parent, cu);
22725 if (TYPE_TAG_NAME (parent_type) != NULL)
22726 return TYPE_TAG_NAME (parent_type);
22727 else
22728 /* An anonymous structure is only allowed non-static data
22729 members; no typedefs, no member functions, et cetera.
22730 So it does not need a prefix. */
22731 return "";
abc72ce4 22732 case DW_TAG_compile_unit:
95554aad 22733 case DW_TAG_partial_unit:
abc72ce4
DE
22734 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22735 if (cu->language == language_cplus
8b70b953 22736 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
22737 && die->child != NULL
22738 && (die->tag == DW_TAG_class_type
22739 || die->tag == DW_TAG_structure_type
22740 || die->tag == DW_TAG_union_type))
22741 {
22742 char *name = guess_full_die_structure_name (die, cu);
22743 if (name != NULL)
22744 return name;
22745 }
22746 return "";
3d567982
TT
22747 case DW_TAG_enumeration_type:
22748 parent_type = read_type_die (parent, cu);
22749 if (TYPE_DECLARED_CLASS (parent_type))
22750 {
22751 if (TYPE_TAG_NAME (parent_type) != NULL)
22752 return TYPE_TAG_NAME (parent_type);
22753 return "";
22754 }
22755 /* Fall through. */
63d06c5c 22756 default:
8176b9b8 22757 return determine_prefix (parent, cu);
63d06c5c 22758 }
63d06c5c
DC
22759}
22760
3e43a32a
MS
22761/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22762 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22763 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22764 an obconcat, otherwise allocate storage for the result. The CU argument is
22765 used to determine the language and hence, the appropriate separator. */
987504bb 22766
f55ee35c 22767#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22768
22769static char *
f55ee35c
JK
22770typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22771 int physname, struct dwarf2_cu *cu)
63d06c5c 22772{
f55ee35c 22773 const char *lead = "";
5c315b68 22774 const char *sep;
63d06c5c 22775
3e43a32a
MS
22776 if (suffix == NULL || suffix[0] == '\0'
22777 || prefix == NULL || prefix[0] == '\0')
987504bb 22778 sep = "";
45280282
IB
22779 else if (cu->language == language_d)
22780 {
22781 /* For D, the 'main' function could be defined in any module, but it
22782 should never be prefixed. */
22783 if (strcmp (suffix, "D main") == 0)
22784 {
22785 prefix = "";
22786 sep = "";
22787 }
22788 else
22789 sep = ".";
22790 }
f55ee35c
JK
22791 else if (cu->language == language_fortran && physname)
22792 {
22793 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22794 DW_AT_MIPS_linkage_name is preferred and used instead. */
22795
22796 lead = "__";
22797 sep = "_MOD_";
22798 }
987504bb
JJ
22799 else
22800 sep = "::";
63d06c5c 22801
6dd47d34
DE
22802 if (prefix == NULL)
22803 prefix = "";
22804 if (suffix == NULL)
22805 suffix = "";
22806
987504bb
JJ
22807 if (obs == NULL)
22808 {
3e43a32a 22809 char *retval
224c3ddb
SM
22810 = ((char *)
22811 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22812
f55ee35c
JK
22813 strcpy (retval, lead);
22814 strcat (retval, prefix);
6dd47d34
DE
22815 strcat (retval, sep);
22816 strcat (retval, suffix);
63d06c5c
DC
22817 return retval;
22818 }
987504bb
JJ
22819 else
22820 {
22821 /* We have an obstack. */
f55ee35c 22822 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22823 }
63d06c5c
DC
22824}
22825
c906108c
SS
22826/* Return sibling of die, NULL if no sibling. */
22827
f9aca02d 22828static struct die_info *
fba45db2 22829sibling_die (struct die_info *die)
c906108c 22830{
639d11d3 22831 return die->sibling;
c906108c
SS
22832}
22833
71c25dea
TT
22834/* Get name of a die, return NULL if not found. */
22835
15d034d0
TT
22836static const char *
22837dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
22838 struct obstack *obstack)
22839{
22840 if (name && cu->language == language_cplus)
22841 {
2f408ecb 22842 std::string canon_name = cp_canonicalize_string (name);
71c25dea 22843
2f408ecb 22844 if (!canon_name.empty ())
71c25dea 22845 {
2f408ecb
PA
22846 if (canon_name != name)
22847 name = (const char *) obstack_copy0 (obstack,
22848 canon_name.c_str (),
22849 canon_name.length ());
71c25dea
TT
22850 }
22851 }
22852
22853 return name;
c906108c
SS
22854}
22855
96553a0c
DE
22856/* Get name of a die, return NULL if not found.
22857 Anonymous namespaces are converted to their magic string. */
9219021c 22858
15d034d0 22859static const char *
e142c38c 22860dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22861{
22862 struct attribute *attr;
518817b3 22863 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 22864
e142c38c 22865 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22866 if ((!attr || !DW_STRING (attr))
96553a0c 22867 && die->tag != DW_TAG_namespace
53832f31
TT
22868 && die->tag != DW_TAG_class_type
22869 && die->tag != DW_TAG_interface_type
22870 && die->tag != DW_TAG_structure_type
22871 && die->tag != DW_TAG_union_type)
71c25dea
TT
22872 return NULL;
22873
22874 switch (die->tag)
22875 {
22876 case DW_TAG_compile_unit:
95554aad 22877 case DW_TAG_partial_unit:
71c25dea
TT
22878 /* Compilation units have a DW_AT_name that is a filename, not
22879 a source language identifier. */
22880 case DW_TAG_enumeration_type:
22881 case DW_TAG_enumerator:
22882 /* These tags always have simple identifiers already; no need
22883 to canonicalize them. */
22884 return DW_STRING (attr);
907af001 22885
96553a0c
DE
22886 case DW_TAG_namespace:
22887 if (attr != NULL && DW_STRING (attr) != NULL)
22888 return DW_STRING (attr);
22889 return CP_ANONYMOUS_NAMESPACE_STR;
22890
907af001
UW
22891 case DW_TAG_class_type:
22892 case DW_TAG_interface_type:
22893 case DW_TAG_structure_type:
22894 case DW_TAG_union_type:
22895 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22896 structures or unions. These were of the form "._%d" in GCC 4.1,
22897 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22898 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22899 if (attr && DW_STRING (attr)
61012eef
GB
22900 && (startswith (DW_STRING (attr), "._")
22901 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22902 return NULL;
53832f31
TT
22903
22904 /* GCC might emit a nameless typedef that has a linkage name. See
22905 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22906 if (!attr || DW_STRING (attr) == NULL)
22907 {
df5c6c50 22908 char *demangled = NULL;
53832f31 22909
73b9be8b 22910 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22911 if (attr == NULL || DW_STRING (attr) == NULL)
22912 return NULL;
22913
df5c6c50
JK
22914 /* Avoid demangling DW_STRING (attr) the second time on a second
22915 call for the same DIE. */
22916 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 22917 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
22918
22919 if (demangled)
22920 {
e6a959d6 22921 const char *base;
96408a79 22922
53832f31 22923 /* FIXME: we already did this for the partial symbol... */
34a68019 22924 DW_STRING (attr)
224c3ddb 22925 = ((const char *)
e3b94546 22926 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb 22927 demangled, strlen (demangled)));
53832f31
TT
22928 DW_STRING_IS_CANONICAL (attr) = 1;
22929 xfree (demangled);
96408a79
SA
22930
22931 /* Strip any leading namespaces/classes, keep only the base name.
22932 DW_AT_name for named DIEs does not contain the prefixes. */
22933 base = strrchr (DW_STRING (attr), ':');
22934 if (base && base > DW_STRING (attr) && base[-1] == ':')
22935 return &base[1];
22936 else
22937 return DW_STRING (attr);
53832f31
TT
22938 }
22939 }
907af001
UW
22940 break;
22941
71c25dea 22942 default:
907af001
UW
22943 break;
22944 }
22945
22946 if (!DW_STRING_IS_CANONICAL (attr))
22947 {
22948 DW_STRING (attr)
22949 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 22950 &objfile->per_bfd->storage_obstack);
907af001 22951 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22952 }
907af001 22953 return DW_STRING (attr);
9219021c
DC
22954}
22955
22956/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22957 is none. *EXT_CU is the CU containing DIE on input, and the CU
22958 containing the return value on output. */
9219021c
DC
22959
22960static struct die_info *
f2f0e013 22961dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22962{
22963 struct attribute *attr;
9219021c 22964
f2f0e013 22965 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22966 if (attr == NULL)
22967 return NULL;
22968
f2f0e013 22969 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22970}
22971
c906108c
SS
22972/* Convert a DIE tag into its string name. */
22973
f39c6ffd 22974static const char *
aa1ee363 22975dwarf_tag_name (unsigned tag)
c906108c 22976{
f39c6ffd
TT
22977 const char *name = get_DW_TAG_name (tag);
22978
22979 if (name == NULL)
22980 return "DW_TAG_<unknown>";
22981
22982 return name;
c906108c
SS
22983}
22984
22985/* Convert a DWARF attribute code into its string name. */
22986
f39c6ffd 22987static const char *
aa1ee363 22988dwarf_attr_name (unsigned attr)
c906108c 22989{
f39c6ffd
TT
22990 const char *name;
22991
c764a876 22992#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
22993 if (attr == DW_AT_MIPS_fde)
22994 return "DW_AT_MIPS_fde";
22995#else
22996 if (attr == DW_AT_HP_block_index)
22997 return "DW_AT_HP_block_index";
c764a876 22998#endif
f39c6ffd
TT
22999
23000 name = get_DW_AT_name (attr);
23001
23002 if (name == NULL)
23003 return "DW_AT_<unknown>";
23004
23005 return name;
c906108c
SS
23006}
23007
23008/* Convert a DWARF value form code into its string name. */
23009
f39c6ffd 23010static const char *
aa1ee363 23011dwarf_form_name (unsigned form)
c906108c 23012{
f39c6ffd
TT
23013 const char *name = get_DW_FORM_name (form);
23014
23015 if (name == NULL)
23016 return "DW_FORM_<unknown>";
23017
23018 return name;
c906108c
SS
23019}
23020
a121b7c1 23021static const char *
fba45db2 23022dwarf_bool_name (unsigned mybool)
c906108c
SS
23023{
23024 if (mybool)
23025 return "TRUE";
23026 else
23027 return "FALSE";
23028}
23029
23030/* Convert a DWARF type code into its string name. */
23031
f39c6ffd 23032static const char *
aa1ee363 23033dwarf_type_encoding_name (unsigned enc)
c906108c 23034{
f39c6ffd 23035 const char *name = get_DW_ATE_name (enc);
c906108c 23036
f39c6ffd
TT
23037 if (name == NULL)
23038 return "DW_ATE_<unknown>";
c906108c 23039
f39c6ffd 23040 return name;
c906108c 23041}
c906108c 23042
f9aca02d 23043static void
d97bc12b 23044dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
23045{
23046 unsigned int i;
23047
d97bc12b 23048 print_spaces (indent, f);
9d8780f0 23049 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 23050 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 23051 sect_offset_str (die->sect_off));
d97bc12b
DE
23052
23053 if (die->parent != NULL)
23054 {
23055 print_spaces (indent, f);
9d8780f0
SM
23056 fprintf_unfiltered (f, " parent at offset: %s\n",
23057 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
23058 }
23059
23060 print_spaces (indent, f);
23061 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 23062 dwarf_bool_name (die->child != NULL));
c906108c 23063
d97bc12b
DE
23064 print_spaces (indent, f);
23065 fprintf_unfiltered (f, " attributes:\n");
23066
c906108c
SS
23067 for (i = 0; i < die->num_attrs; ++i)
23068 {
d97bc12b
DE
23069 print_spaces (indent, f);
23070 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
23071 dwarf_attr_name (die->attrs[i].name),
23072 dwarf_form_name (die->attrs[i].form));
d97bc12b 23073
c906108c
SS
23074 switch (die->attrs[i].form)
23075 {
c906108c 23076 case DW_FORM_addr:
3019eac3 23077 case DW_FORM_GNU_addr_index:
d97bc12b 23078 fprintf_unfiltered (f, "address: ");
5af949e3 23079 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
23080 break;
23081 case DW_FORM_block2:
23082 case DW_FORM_block4:
23083 case DW_FORM_block:
23084 case DW_FORM_block1:
56eb65bd
SP
23085 fprintf_unfiltered (f, "block: size %s",
23086 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 23087 break;
2dc7f7b3 23088 case DW_FORM_exprloc:
56eb65bd
SP
23089 fprintf_unfiltered (f, "expression: size %s",
23090 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 23091 break;
0224619f
JK
23092 case DW_FORM_data16:
23093 fprintf_unfiltered (f, "constant of 16 bytes");
23094 break;
4568ecf9
DE
23095 case DW_FORM_ref_addr:
23096 fprintf_unfiltered (f, "ref address: ");
23097 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23098 break;
36586728
TT
23099 case DW_FORM_GNU_ref_alt:
23100 fprintf_unfiltered (f, "alt ref address: ");
23101 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23102 break;
10b3939b
DJ
23103 case DW_FORM_ref1:
23104 case DW_FORM_ref2:
23105 case DW_FORM_ref4:
4568ecf9
DE
23106 case DW_FORM_ref8:
23107 case DW_FORM_ref_udata:
d97bc12b 23108 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 23109 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 23110 break;
c906108c
SS
23111 case DW_FORM_data1:
23112 case DW_FORM_data2:
23113 case DW_FORM_data4:
ce5d95e1 23114 case DW_FORM_data8:
c906108c
SS
23115 case DW_FORM_udata:
23116 case DW_FORM_sdata:
43bbcdc2
PH
23117 fprintf_unfiltered (f, "constant: %s",
23118 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 23119 break;
2dc7f7b3
TT
23120 case DW_FORM_sec_offset:
23121 fprintf_unfiltered (f, "section offset: %s",
23122 pulongest (DW_UNSND (&die->attrs[i])));
23123 break;
55f1336d 23124 case DW_FORM_ref_sig8:
ac9ec31b
DE
23125 fprintf_unfiltered (f, "signature: %s",
23126 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 23127 break;
c906108c 23128 case DW_FORM_string:
4bdf3d34 23129 case DW_FORM_strp:
43988095 23130 case DW_FORM_line_strp:
3019eac3 23131 case DW_FORM_GNU_str_index:
36586728 23132 case DW_FORM_GNU_strp_alt:
8285870a 23133 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 23134 DW_STRING (&die->attrs[i])
8285870a
JK
23135 ? DW_STRING (&die->attrs[i]) : "",
23136 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
23137 break;
23138 case DW_FORM_flag:
23139 if (DW_UNSND (&die->attrs[i]))
d97bc12b 23140 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23141 else
d97bc12b 23142 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23143 break;
2dc7f7b3
TT
23144 case DW_FORM_flag_present:
23145 fprintf_unfiltered (f, "flag: TRUE");
23146 break;
a8329558 23147 case DW_FORM_indirect:
0963b4bd
MS
23148 /* The reader will have reduced the indirect form to
23149 the "base form" so this form should not occur. */
3e43a32a
MS
23150 fprintf_unfiltered (f,
23151 "unexpected attribute form: DW_FORM_indirect");
a8329558 23152 break;
663c44ac
JK
23153 case DW_FORM_implicit_const:
23154 fprintf_unfiltered (f, "constant: %s",
23155 plongest (DW_SND (&die->attrs[i])));
23156 break;
c906108c 23157 default:
d97bc12b 23158 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23159 die->attrs[i].form);
d97bc12b 23160 break;
c906108c 23161 }
d97bc12b 23162 fprintf_unfiltered (f, "\n");
c906108c
SS
23163 }
23164}
23165
f9aca02d 23166static void
d97bc12b 23167dump_die_for_error (struct die_info *die)
c906108c 23168{
d97bc12b
DE
23169 dump_die_shallow (gdb_stderr, 0, die);
23170}
23171
23172static void
23173dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23174{
23175 int indent = level * 4;
23176
23177 gdb_assert (die != NULL);
23178
23179 if (level >= max_level)
23180 return;
23181
23182 dump_die_shallow (f, indent, die);
23183
23184 if (die->child != NULL)
c906108c 23185 {
d97bc12b
DE
23186 print_spaces (indent, f);
23187 fprintf_unfiltered (f, " Children:");
23188 if (level + 1 < max_level)
23189 {
23190 fprintf_unfiltered (f, "\n");
23191 dump_die_1 (f, level + 1, max_level, die->child);
23192 }
23193 else
23194 {
3e43a32a
MS
23195 fprintf_unfiltered (f,
23196 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23197 }
23198 }
23199
23200 if (die->sibling != NULL && level > 0)
23201 {
23202 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23203 }
23204}
23205
d97bc12b
DE
23206/* This is called from the pdie macro in gdbinit.in.
23207 It's not static so gcc will keep a copy callable from gdb. */
23208
23209void
23210dump_die (struct die_info *die, int max_level)
23211{
23212 dump_die_1 (gdb_stdlog, 0, max_level, die);
23213}
23214
f9aca02d 23215static void
51545339 23216store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23217{
51545339 23218 void **slot;
c906108c 23219
9c541725
PA
23220 slot = htab_find_slot_with_hash (cu->die_hash, die,
23221 to_underlying (die->sect_off),
b64f50a1 23222 INSERT);
51545339
DJ
23223
23224 *slot = die;
c906108c
SS
23225}
23226
b64f50a1
JK
23227/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23228 required kind. */
23229
23230static sect_offset
ff39bb5e 23231dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 23232{
7771576e 23233 if (attr_form_is_ref (attr))
9c541725 23234 return (sect_offset) DW_UNSND (attr);
93311388
DE
23235
23236 complaint (&symfile_complaints,
23237 _("unsupported die ref attribute form: '%s'"),
23238 dwarf_form_name (attr->form));
9c541725 23239 return {};
c906108c
SS
23240}
23241
43bbcdc2
PH
23242/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23243 * the value held by the attribute is not constant. */
a02abb62 23244
43bbcdc2 23245static LONGEST
ff39bb5e 23246dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 23247{
663c44ac 23248 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
23249 return DW_SND (attr);
23250 else if (attr->form == DW_FORM_udata
23251 || attr->form == DW_FORM_data1
23252 || attr->form == DW_FORM_data2
23253 || attr->form == DW_FORM_data4
23254 || attr->form == DW_FORM_data8)
23255 return DW_UNSND (attr);
23256 else
23257 {
0224619f 23258 /* For DW_FORM_data16 see attr_form_is_constant. */
3e43a32a
MS
23259 complaint (&symfile_complaints,
23260 _("Attribute value is not a constant (%s)"),
a02abb62
JB
23261 dwarf_form_name (attr->form));
23262 return default_value;
23263 }
23264}
23265
348e048f
DE
23266/* Follow reference or signature attribute ATTR of SRC_DIE.
23267 On entry *REF_CU is the CU of SRC_DIE.
23268 On exit *REF_CU is the CU of the result. */
23269
23270static struct die_info *
ff39bb5e 23271follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23272 struct dwarf2_cu **ref_cu)
23273{
23274 struct die_info *die;
23275
7771576e 23276 if (attr_form_is_ref (attr))
348e048f 23277 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23278 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23279 die = follow_die_sig (src_die, attr, ref_cu);
23280 else
23281 {
23282 dump_die_for_error (src_die);
23283 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 23284 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
23285 }
23286
23287 return die;
03dd20cc
DJ
23288}
23289
5c631832 23290/* Follow reference OFFSET.
673bfd45
DE
23291 On entry *REF_CU is the CU of the source die referencing OFFSET.
23292 On exit *REF_CU is the CU of the result.
23293 Returns NULL if OFFSET is invalid. */
f504f079 23294
f9aca02d 23295static struct die_info *
9c541725 23296follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23297 struct dwarf2_cu **ref_cu)
c906108c 23298{
10b3939b 23299 struct die_info temp_die;
f2f0e013 23300 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
23301 struct dwarf2_per_objfile *dwarf2_per_objfile
23302 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23303 struct objfile *objfile = dwarf2_per_objfile->objfile;
10b3939b 23304
348e048f
DE
23305 gdb_assert (cu->per_cu != NULL);
23306
98bfdba5
PA
23307 target_cu = cu;
23308
3019eac3 23309 if (cu->per_cu->is_debug_types)
348e048f
DE
23310 {
23311 /* .debug_types CUs cannot reference anything outside their CU.
23312 If they need to, they have to reference a signatured type via
55f1336d 23313 DW_FORM_ref_sig8. */
9c541725 23314 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 23315 return NULL;
348e048f 23316 }
36586728 23317 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 23318 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
23319 {
23320 struct dwarf2_per_cu_data *per_cu;
9a619af0 23321
9c541725 23322 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 23323 dwarf2_per_objfile);
03dd20cc
DJ
23324
23325 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
23326 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23327 load_full_comp_unit (per_cu, cu->language);
03dd20cc 23328
10b3939b
DJ
23329 target_cu = per_cu->cu;
23330 }
98bfdba5
PA
23331 else if (cu->dies == NULL)
23332 {
23333 /* We're loading full DIEs during partial symbol reading. */
23334 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 23335 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 23336 }
c906108c 23337
f2f0e013 23338 *ref_cu = target_cu;
9c541725 23339 temp_die.sect_off = sect_off;
9a3c8263 23340 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23341 &temp_die,
23342 to_underlying (sect_off));
5c631832 23343}
10b3939b 23344
5c631832
JK
23345/* Follow reference attribute ATTR of SRC_DIE.
23346 On entry *REF_CU is the CU of SRC_DIE.
23347 On exit *REF_CU is the CU of the result. */
23348
23349static struct die_info *
ff39bb5e 23350follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23351 struct dwarf2_cu **ref_cu)
23352{
9c541725 23353 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
23354 struct dwarf2_cu *cu = *ref_cu;
23355 struct die_info *die;
23356
9c541725 23357 die = follow_die_offset (sect_off,
36586728
TT
23358 (attr->form == DW_FORM_GNU_ref_alt
23359 || cu->per_cu->is_dwz),
23360 ref_cu);
5c631832 23361 if (!die)
9d8780f0
SM
23362 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23363 "at %s [in module %s]"),
23364 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 23365 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 23366
5c631832
JK
23367 return die;
23368}
23369
9c541725 23370/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 23371 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
23372 dwarf2_locexpr_baton->data has lifetime of
23373 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
23374
23375struct dwarf2_locexpr_baton
9c541725 23376dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
23377 struct dwarf2_per_cu_data *per_cu,
23378 CORE_ADDR (*get_frame_pc) (void *baton),
23379 void *baton)
5c631832 23380{
918dd910 23381 struct dwarf2_cu *cu;
5c631832
JK
23382 struct die_info *die;
23383 struct attribute *attr;
23384 struct dwarf2_locexpr_baton retval;
e3b94546 23385 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ed2dc618
SM
23386 struct dwarf2_per_objfile *dwarf2_per_objfile
23387 = get_dwarf2_per_objfile (objfile);
8cf6f0b1 23388
918dd910
JK
23389 if (per_cu->cu == NULL)
23390 load_cu (per_cu);
23391 cu = per_cu->cu;
cc12ce38
DE
23392 if (cu == NULL)
23393 {
23394 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23395 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23396 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23397 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23398 }
918dd910 23399
9c541725 23400 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23401 if (!die)
9d8780f0
SM
23402 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23403 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23404
23405 attr = dwarf2_attr (die, DW_AT_location, cu);
23406 if (!attr)
23407 {
e103e986
JK
23408 /* DWARF: "If there is no such attribute, then there is no effect.".
23409 DATA is ignored if SIZE is 0. */
5c631832 23410
e103e986 23411 retval.data = NULL;
5c631832
JK
23412 retval.size = 0;
23413 }
8cf6f0b1
TT
23414 else if (attr_form_is_section_offset (attr))
23415 {
23416 struct dwarf2_loclist_baton loclist_baton;
23417 CORE_ADDR pc = (*get_frame_pc) (baton);
23418 size_t size;
23419
23420 fill_in_loclist_baton (cu, &loclist_baton, attr);
23421
23422 retval.data = dwarf2_find_location_expression (&loclist_baton,
23423 &size, pc);
23424 retval.size = size;
23425 }
5c631832
JK
23426 else
23427 {
23428 if (!attr_form_is_block (attr))
9d8780f0 23429 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23430 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23431 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23432
23433 retval.data = DW_BLOCK (attr)->data;
23434 retval.size = DW_BLOCK (attr)->size;
23435 }
23436 retval.per_cu = cu->per_cu;
918dd910 23437
ed2dc618 23438 age_cached_comp_units (dwarf2_per_objfile);
918dd910 23439
5c631832 23440 return retval;
348e048f
DE
23441}
23442
8b9737bf
TT
23443/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23444 offset. */
23445
23446struct dwarf2_locexpr_baton
23447dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23448 struct dwarf2_per_cu_data *per_cu,
23449 CORE_ADDR (*get_frame_pc) (void *baton),
23450 void *baton)
23451{
9c541725 23452 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23453
9c541725 23454 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
23455}
23456
b6807d98
TT
23457/* Write a constant of a given type as target-ordered bytes into
23458 OBSTACK. */
23459
23460static const gdb_byte *
23461write_constant_as_bytes (struct obstack *obstack,
23462 enum bfd_endian byte_order,
23463 struct type *type,
23464 ULONGEST value,
23465 LONGEST *len)
23466{
23467 gdb_byte *result;
23468
23469 *len = TYPE_LENGTH (type);
224c3ddb 23470 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23471 store_unsigned_integer (result, *len, byte_order, value);
23472
23473 return result;
23474}
23475
23476/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23477 pointer to the constant bytes and set LEN to the length of the
23478 data. If memory is needed, allocate it on OBSTACK. If the DIE
23479 does not have a DW_AT_const_value, return NULL. */
23480
23481const gdb_byte *
9c541725 23482dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
23483 struct dwarf2_per_cu_data *per_cu,
23484 struct obstack *obstack,
23485 LONGEST *len)
23486{
23487 struct dwarf2_cu *cu;
23488 struct die_info *die;
23489 struct attribute *attr;
23490 const gdb_byte *result = NULL;
23491 struct type *type;
23492 LONGEST value;
23493 enum bfd_endian byte_order;
e3b94546 23494 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 23495
b6807d98
TT
23496 if (per_cu->cu == NULL)
23497 load_cu (per_cu);
23498 cu = per_cu->cu;
cc12ce38
DE
23499 if (cu == NULL)
23500 {
23501 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23502 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23503 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23504 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23505 }
b6807d98 23506
9c541725 23507 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23508 if (!die)
9d8780f0
SM
23509 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23510 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23511
23512 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23513 if (attr == NULL)
23514 return NULL;
23515
e3b94546 23516 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23517 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23518
23519 switch (attr->form)
23520 {
23521 case DW_FORM_addr:
23522 case DW_FORM_GNU_addr_index:
23523 {
23524 gdb_byte *tem;
23525
23526 *len = cu->header.addr_size;
224c3ddb 23527 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23528 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23529 result = tem;
23530 }
23531 break;
23532 case DW_FORM_string:
23533 case DW_FORM_strp:
23534 case DW_FORM_GNU_str_index:
23535 case DW_FORM_GNU_strp_alt:
23536 /* DW_STRING is already allocated on the objfile obstack, point
23537 directly to it. */
23538 result = (const gdb_byte *) DW_STRING (attr);
23539 *len = strlen (DW_STRING (attr));
23540 break;
23541 case DW_FORM_block1:
23542 case DW_FORM_block2:
23543 case DW_FORM_block4:
23544 case DW_FORM_block:
23545 case DW_FORM_exprloc:
0224619f 23546 case DW_FORM_data16:
b6807d98
TT
23547 result = DW_BLOCK (attr)->data;
23548 *len = DW_BLOCK (attr)->size;
23549 break;
23550
23551 /* The DW_AT_const_value attributes are supposed to carry the
23552 symbol's value "represented as it would be on the target
23553 architecture." By the time we get here, it's already been
23554 converted to host endianness, so we just need to sign- or
23555 zero-extend it as appropriate. */
23556 case DW_FORM_data1:
23557 type = die_type (die, cu);
23558 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23559 if (result == NULL)
23560 result = write_constant_as_bytes (obstack, byte_order,
23561 type, value, len);
23562 break;
23563 case DW_FORM_data2:
23564 type = die_type (die, cu);
23565 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23566 if (result == NULL)
23567 result = write_constant_as_bytes (obstack, byte_order,
23568 type, value, len);
23569 break;
23570 case DW_FORM_data4:
23571 type = die_type (die, cu);
23572 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23573 if (result == NULL)
23574 result = write_constant_as_bytes (obstack, byte_order,
23575 type, value, len);
23576 break;
23577 case DW_FORM_data8:
23578 type = die_type (die, cu);
23579 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23580 if (result == NULL)
23581 result = write_constant_as_bytes (obstack, byte_order,
23582 type, value, len);
23583 break;
23584
23585 case DW_FORM_sdata:
663c44ac 23586 case DW_FORM_implicit_const:
b6807d98
TT
23587 type = die_type (die, cu);
23588 result = write_constant_as_bytes (obstack, byte_order,
23589 type, DW_SND (attr), len);
23590 break;
23591
23592 case DW_FORM_udata:
23593 type = die_type (die, cu);
23594 result = write_constant_as_bytes (obstack, byte_order,
23595 type, DW_UNSND (attr), len);
23596 break;
23597
23598 default:
23599 complaint (&symfile_complaints,
23600 _("unsupported const value attribute form: '%s'"),
23601 dwarf_form_name (attr->form));
23602 break;
23603 }
23604
23605 return result;
23606}
23607
7942e96e
AA
23608/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23609 valid type for this die is found. */
23610
23611struct type *
9c541725 23612dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23613 struct dwarf2_per_cu_data *per_cu)
23614{
23615 struct dwarf2_cu *cu;
23616 struct die_info *die;
23617
7942e96e
AA
23618 if (per_cu->cu == NULL)
23619 load_cu (per_cu);
23620 cu = per_cu->cu;
23621 if (!cu)
23622 return NULL;
23623
9c541725 23624 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23625 if (!die)
23626 return NULL;
23627
23628 return die_type (die, cu);
23629}
23630
8a9b8146
TT
23631/* Return the type of the DIE at DIE_OFFSET in the CU named by
23632 PER_CU. */
23633
23634struct type *
b64f50a1 23635dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23636 struct dwarf2_per_cu_data *per_cu)
23637{
9c541725 23638 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23639 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23640}
23641
ac9ec31b 23642/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23643 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23644 On exit *REF_CU is the CU of the result.
23645 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23646
23647static struct die_info *
ac9ec31b
DE
23648follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23649 struct dwarf2_cu **ref_cu)
348e048f 23650{
348e048f 23651 struct die_info temp_die;
348e048f
DE
23652 struct dwarf2_cu *sig_cu;
23653 struct die_info *die;
23654
ac9ec31b
DE
23655 /* While it might be nice to assert sig_type->type == NULL here,
23656 we can get here for DW_AT_imported_declaration where we need
23657 the DIE not the type. */
348e048f
DE
23658
23659 /* If necessary, add it to the queue and load its DIEs. */
23660
95554aad 23661 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23662 read_signatured_type (sig_type);
348e048f 23663
348e048f 23664 sig_cu = sig_type->per_cu.cu;
69d751e3 23665 gdb_assert (sig_cu != NULL);
9c541725
PA
23666 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23667 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23668 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23669 to_underlying (temp_die.sect_off));
348e048f
DE
23670 if (die)
23671 {
ed2dc618 23672 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23673 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23674
796a7ff8
DE
23675 /* For .gdb_index version 7 keep track of included TUs.
23676 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23677 if (dwarf2_per_objfile->index_table != NULL
23678 && dwarf2_per_objfile->index_table->version <= 7)
23679 {
23680 VEC_safe_push (dwarf2_per_cu_ptr,
23681 (*ref_cu)->per_cu->imported_symtabs,
23682 sig_cu->per_cu);
23683 }
23684
348e048f
DE
23685 *ref_cu = sig_cu;
23686 return die;
23687 }
23688
ac9ec31b
DE
23689 return NULL;
23690}
23691
23692/* Follow signatured type referenced by ATTR in SRC_DIE.
23693 On entry *REF_CU is the CU of SRC_DIE.
23694 On exit *REF_CU is the CU of the result.
23695 The result is the DIE of the type.
23696 If the referenced type cannot be found an error is thrown. */
23697
23698static struct die_info *
ff39bb5e 23699follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23700 struct dwarf2_cu **ref_cu)
23701{
23702 ULONGEST signature = DW_SIGNATURE (attr);
23703 struct signatured_type *sig_type;
23704 struct die_info *die;
23705
23706 gdb_assert (attr->form == DW_FORM_ref_sig8);
23707
a2ce51a0 23708 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23709 /* sig_type will be NULL if the signatured type is missing from
23710 the debug info. */
23711 if (sig_type == NULL)
23712 {
23713 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23714 " from DIE at %s [in module %s]"),
23715 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23716 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23717 }
23718
23719 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23720 if (die == NULL)
23721 {
23722 dump_die_for_error (src_die);
23723 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23724 " from DIE at %s [in module %s]"),
23725 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23726 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23727 }
23728
23729 return die;
23730}
23731
23732/* Get the type specified by SIGNATURE referenced in DIE/CU,
23733 reading in and processing the type unit if necessary. */
23734
23735static struct type *
23736get_signatured_type (struct die_info *die, ULONGEST signature,
23737 struct dwarf2_cu *cu)
23738{
518817b3
SM
23739 struct dwarf2_per_objfile *dwarf2_per_objfile
23740 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23741 struct signatured_type *sig_type;
23742 struct dwarf2_cu *type_cu;
23743 struct die_info *type_die;
23744 struct type *type;
23745
a2ce51a0 23746 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23747 /* sig_type will be NULL if the signatured type is missing from
23748 the debug info. */
23749 if (sig_type == NULL)
23750 {
23751 complaint (&symfile_complaints,
23752 _("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23753 " from DIE at %s [in module %s]"),
23754 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23755 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23756 return build_error_marker_type (cu, die);
23757 }
23758
23759 /* If we already know the type we're done. */
23760 if (sig_type->type != NULL)
23761 return sig_type->type;
23762
23763 type_cu = cu;
23764 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23765 if (type_die != NULL)
23766 {
23767 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23768 is created. This is important, for example, because for c++ classes
23769 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23770 type = read_type_die (type_die, type_cu);
23771 if (type == NULL)
23772 {
23773 complaint (&symfile_complaints,
23774 _("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23775 " referenced from DIE at %s [in module %s]"),
23776 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23777 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23778 type = build_error_marker_type (cu, die);
23779 }
23780 }
23781 else
23782 {
23783 complaint (&symfile_complaints,
23784 _("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23785 " from DIE at %s [in module %s]"),
23786 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23787 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23788 type = build_error_marker_type (cu, die);
23789 }
23790 sig_type->type = type;
23791
23792 return type;
23793}
23794
23795/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23796 reading in and processing the type unit if necessary. */
23797
23798static struct type *
ff39bb5e 23799get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23800 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23801{
23802 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 23803 if (attr_form_is_ref (attr))
ac9ec31b
DE
23804 {
23805 struct dwarf2_cu *type_cu = cu;
23806 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23807
23808 return read_type_die (type_die, type_cu);
23809 }
23810 else if (attr->form == DW_FORM_ref_sig8)
23811 {
23812 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23813 }
23814 else
23815 {
518817b3
SM
23816 struct dwarf2_per_objfile *dwarf2_per_objfile
23817 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23818
ac9ec31b
DE
23819 complaint (&symfile_complaints,
23820 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23821 " at %s [in module %s]"),
23822 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 23823 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23824 return build_error_marker_type (cu, die);
23825 }
348e048f
DE
23826}
23827
e5fe5e75 23828/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23829
23830static void
e5fe5e75 23831load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 23832{
52dc124a 23833 struct signatured_type *sig_type;
348e048f 23834
f4dc4d17
DE
23835 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23836 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23837
6721b2ec
DE
23838 /* We have the per_cu, but we need the signatured_type.
23839 Fortunately this is an easy translation. */
23840 gdb_assert (per_cu->is_debug_types);
23841 sig_type = (struct signatured_type *) per_cu;
348e048f 23842
6721b2ec 23843 gdb_assert (per_cu->cu == NULL);
348e048f 23844
52dc124a 23845 read_signatured_type (sig_type);
348e048f 23846
6721b2ec 23847 gdb_assert (per_cu->cu != NULL);
348e048f
DE
23848}
23849
dee91e82
DE
23850/* die_reader_func for read_signatured_type.
23851 This is identical to load_full_comp_unit_reader,
23852 but is kept separate for now. */
348e048f
DE
23853
23854static void
dee91e82 23855read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 23856 const gdb_byte *info_ptr,
dee91e82
DE
23857 struct die_info *comp_unit_die,
23858 int has_children,
23859 void *data)
348e048f 23860{
dee91e82 23861 struct dwarf2_cu *cu = reader->cu;
348e048f 23862
dee91e82
DE
23863 gdb_assert (cu->die_hash == NULL);
23864 cu->die_hash =
23865 htab_create_alloc_ex (cu->header.length / 12,
23866 die_hash,
23867 die_eq,
23868 NULL,
23869 &cu->comp_unit_obstack,
23870 hashtab_obstack_allocate,
23871 dummy_obstack_deallocate);
348e048f 23872
dee91e82
DE
23873 if (has_children)
23874 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23875 &info_ptr, comp_unit_die);
23876 cu->dies = comp_unit_die;
23877 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
23878
23879 /* We try not to read any attributes in this function, because not
9cdd5dbd 23880 all CUs needed for references have been loaded yet, and symbol
348e048f 23881 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
23882 or we won't be able to build types correctly.
23883 Similarly, if we do not read the producer, we can not apply
23884 producer-specific interpretation. */
95554aad 23885 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 23886}
348e048f 23887
3019eac3
DE
23888/* Read in a signatured type and build its CU and DIEs.
23889 If the type is a stub for the real type in a DWO file,
23890 read in the real type from the DWO file as well. */
dee91e82
DE
23891
23892static void
23893read_signatured_type (struct signatured_type *sig_type)
23894{
23895 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 23896
3019eac3 23897 gdb_assert (per_cu->is_debug_types);
dee91e82 23898 gdb_assert (per_cu->cu == NULL);
348e048f 23899
f4dc4d17
DE
23900 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
23901 read_signatured_type_reader, NULL);
7ee85ab1 23902 sig_type->per_cu.tu_read = 1;
c906108c
SS
23903}
23904
c906108c
SS
23905/* Decode simple location descriptions.
23906 Given a pointer to a dwarf block that defines a location, compute
23907 the location and return the value.
23908
4cecd739
DJ
23909 NOTE drow/2003-11-18: This function is called in two situations
23910 now: for the address of static or global variables (partial symbols
23911 only) and for offsets into structures which are expected to be
23912 (more or less) constant. The partial symbol case should go away,
23913 and only the constant case should remain. That will let this
23914 function complain more accurately. A few special modes are allowed
23915 without complaint for global variables (for instance, global
23916 register values and thread-local values).
c906108c
SS
23917
23918 A location description containing no operations indicates that the
4cecd739 23919 object is optimized out. The return value is 0 for that case.
6b992462
DJ
23920 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23921 callers will only want a very basic result and this can become a
21ae7a4d
JK
23922 complaint.
23923
23924 Note that stack[0] is unused except as a default error return. */
c906108c
SS
23925
23926static CORE_ADDR
e7c27a73 23927decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 23928{
518817b3 23929 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
23930 size_t i;
23931 size_t size = blk->size;
d521ce57 23932 const gdb_byte *data = blk->data;
21ae7a4d
JK
23933 CORE_ADDR stack[64];
23934 int stacki;
23935 unsigned int bytes_read, unsnd;
23936 gdb_byte op;
c906108c 23937
21ae7a4d
JK
23938 i = 0;
23939 stacki = 0;
23940 stack[stacki] = 0;
23941 stack[++stacki] = 0;
23942
23943 while (i < size)
23944 {
23945 op = data[i++];
23946 switch (op)
23947 {
23948 case DW_OP_lit0:
23949 case DW_OP_lit1:
23950 case DW_OP_lit2:
23951 case DW_OP_lit3:
23952 case DW_OP_lit4:
23953 case DW_OP_lit5:
23954 case DW_OP_lit6:
23955 case DW_OP_lit7:
23956 case DW_OP_lit8:
23957 case DW_OP_lit9:
23958 case DW_OP_lit10:
23959 case DW_OP_lit11:
23960 case DW_OP_lit12:
23961 case DW_OP_lit13:
23962 case DW_OP_lit14:
23963 case DW_OP_lit15:
23964 case DW_OP_lit16:
23965 case DW_OP_lit17:
23966 case DW_OP_lit18:
23967 case DW_OP_lit19:
23968 case DW_OP_lit20:
23969 case DW_OP_lit21:
23970 case DW_OP_lit22:
23971 case DW_OP_lit23:
23972 case DW_OP_lit24:
23973 case DW_OP_lit25:
23974 case DW_OP_lit26:
23975 case DW_OP_lit27:
23976 case DW_OP_lit28:
23977 case DW_OP_lit29:
23978 case DW_OP_lit30:
23979 case DW_OP_lit31:
23980 stack[++stacki] = op - DW_OP_lit0;
23981 break;
f1bea926 23982
21ae7a4d
JK
23983 case DW_OP_reg0:
23984 case DW_OP_reg1:
23985 case DW_OP_reg2:
23986 case DW_OP_reg3:
23987 case DW_OP_reg4:
23988 case DW_OP_reg5:
23989 case DW_OP_reg6:
23990 case DW_OP_reg7:
23991 case DW_OP_reg8:
23992 case DW_OP_reg9:
23993 case DW_OP_reg10:
23994 case DW_OP_reg11:
23995 case DW_OP_reg12:
23996 case DW_OP_reg13:
23997 case DW_OP_reg14:
23998 case DW_OP_reg15:
23999 case DW_OP_reg16:
24000 case DW_OP_reg17:
24001 case DW_OP_reg18:
24002 case DW_OP_reg19:
24003 case DW_OP_reg20:
24004 case DW_OP_reg21:
24005 case DW_OP_reg22:
24006 case DW_OP_reg23:
24007 case DW_OP_reg24:
24008 case DW_OP_reg25:
24009 case DW_OP_reg26:
24010 case DW_OP_reg27:
24011 case DW_OP_reg28:
24012 case DW_OP_reg29:
24013 case DW_OP_reg30:
24014 case DW_OP_reg31:
24015 stack[++stacki] = op - DW_OP_reg0;
24016 if (i < size)
24017 dwarf2_complex_location_expr_complaint ();
24018 break;
c906108c 24019
21ae7a4d
JK
24020 case DW_OP_regx:
24021 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24022 i += bytes_read;
24023 stack[++stacki] = unsnd;
24024 if (i < size)
24025 dwarf2_complex_location_expr_complaint ();
24026 break;
c906108c 24027
21ae7a4d
JK
24028 case DW_OP_addr:
24029 stack[++stacki] = read_address (objfile->obfd, &data[i],
24030 cu, &bytes_read);
24031 i += bytes_read;
24032 break;
d53d4ac5 24033
21ae7a4d
JK
24034 case DW_OP_const1u:
24035 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24036 i += 1;
24037 break;
24038
24039 case DW_OP_const1s:
24040 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24041 i += 1;
24042 break;
24043
24044 case DW_OP_const2u:
24045 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24046 i += 2;
24047 break;
24048
24049 case DW_OP_const2s:
24050 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24051 i += 2;
24052 break;
d53d4ac5 24053
21ae7a4d
JK
24054 case DW_OP_const4u:
24055 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24056 i += 4;
24057 break;
24058
24059 case DW_OP_const4s:
24060 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24061 i += 4;
24062 break;
24063
585861ea
JK
24064 case DW_OP_const8u:
24065 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24066 i += 8;
24067 break;
24068
21ae7a4d
JK
24069 case DW_OP_constu:
24070 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24071 &bytes_read);
24072 i += bytes_read;
24073 break;
24074
24075 case DW_OP_consts:
24076 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24077 i += bytes_read;
24078 break;
24079
24080 case DW_OP_dup:
24081 stack[stacki + 1] = stack[stacki];
24082 stacki++;
24083 break;
24084
24085 case DW_OP_plus:
24086 stack[stacki - 1] += stack[stacki];
24087 stacki--;
24088 break;
24089
24090 case DW_OP_plus_uconst:
24091 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24092 &bytes_read);
24093 i += bytes_read;
24094 break;
24095
24096 case DW_OP_minus:
24097 stack[stacki - 1] -= stack[stacki];
24098 stacki--;
24099 break;
24100
24101 case DW_OP_deref:
24102 /* If we're not the last op, then we definitely can't encode
24103 this using GDB's address_class enum. This is valid for partial
24104 global symbols, although the variable's address will be bogus
24105 in the psymtab. */
24106 if (i < size)
24107 dwarf2_complex_location_expr_complaint ();
24108 break;
24109
24110 case DW_OP_GNU_push_tls_address:
4aa4e28b 24111 case DW_OP_form_tls_address:
21ae7a4d
JK
24112 /* The top of the stack has the offset from the beginning
24113 of the thread control block at which the variable is located. */
24114 /* Nothing should follow this operator, so the top of stack would
24115 be returned. */
24116 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24117 address will be bogus in the psymtab. Make it always at least
24118 non-zero to not look as a variable garbage collected by linker
24119 which have DW_OP_addr 0. */
21ae7a4d
JK
24120 if (i < size)
24121 dwarf2_complex_location_expr_complaint ();
585861ea 24122 stack[stacki]++;
21ae7a4d
JK
24123 break;
24124
24125 case DW_OP_GNU_uninit:
24126 break;
24127
3019eac3 24128 case DW_OP_GNU_addr_index:
49f6c839 24129 case DW_OP_GNU_const_index:
3019eac3
DE
24130 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24131 &bytes_read);
24132 i += bytes_read;
24133 break;
24134
21ae7a4d
JK
24135 default:
24136 {
f39c6ffd 24137 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
24138
24139 if (name)
24140 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
24141 name);
24142 else
24143 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
24144 op);
24145 }
24146
24147 return (stack[stacki]);
d53d4ac5 24148 }
3c6e0cb3 24149
21ae7a4d
JK
24150 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24151 outside of the allocated space. Also enforce minimum>0. */
24152 if (stacki >= ARRAY_SIZE (stack) - 1)
24153 {
24154 complaint (&symfile_complaints,
24155 _("location description stack overflow"));
24156 return 0;
24157 }
24158
24159 if (stacki <= 0)
24160 {
24161 complaint (&symfile_complaints,
24162 _("location description stack underflow"));
24163 return 0;
24164 }
24165 }
24166 return (stack[stacki]);
c906108c
SS
24167}
24168
24169/* memory allocation interface */
24170
c906108c 24171static struct dwarf_block *
7b5a2f43 24172dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24173{
8d749320 24174 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24175}
24176
c906108c 24177static struct die_info *
b60c80d6 24178dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24179{
24180 struct die_info *die;
b60c80d6
DJ
24181 size_t size = sizeof (struct die_info);
24182
24183 if (num_attrs > 1)
24184 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24185
b60c80d6 24186 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24187 memset (die, 0, sizeof (struct die_info));
24188 return (die);
24189}
2e276125
JB
24190
24191\f
24192/* Macro support. */
24193
233d95b5
JK
24194/* Return file name relative to the compilation directory of file number I in
24195 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 24196 responsible for freeing it. */
233d95b5 24197
2e276125 24198static char *
233d95b5 24199file_file_name (int file, struct line_header *lh)
2e276125 24200{
6a83a1e6
EZ
24201 /* Is the file number a valid index into the line header's file name
24202 table? Remember that file numbers start with one, not zero. */
fff8551c 24203 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 24204 {
8c43009f 24205 const file_entry &fe = lh->file_names[file - 1];
6e70227d 24206
8c43009f
PA
24207 if (!IS_ABSOLUTE_PATH (fe.name))
24208 {
24209 const char *dir = fe.include_dir (lh);
24210 if (dir != NULL)
24211 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
24212 }
24213 return xstrdup (fe.name);
6a83a1e6 24214 }
2e276125
JB
24215 else
24216 {
6a83a1e6
EZ
24217 /* The compiler produced a bogus file number. We can at least
24218 record the macro definitions made in the file, even if we
24219 won't be able to find the file by name. */
24220 char fake_name[80];
9a619af0 24221
8c042590
PM
24222 xsnprintf (fake_name, sizeof (fake_name),
24223 "<bad macro file number %d>", file);
2e276125 24224
6e70227d 24225 complaint (&symfile_complaints,
6a83a1e6
EZ
24226 _("bad file number in macro information (%d)"),
24227 file);
2e276125 24228
6a83a1e6 24229 return xstrdup (fake_name);
2e276125
JB
24230 }
24231}
24232
233d95b5
JK
24233/* Return the full name of file number I in *LH's file name table.
24234 Use COMP_DIR as the name of the current directory of the
24235 compilation. The result is allocated using xmalloc; the caller is
24236 responsible for freeing it. */
24237static char *
24238file_full_name (int file, struct line_header *lh, const char *comp_dir)
24239{
24240 /* Is the file number a valid index into the line header's file name
24241 table? Remember that file numbers start with one, not zero. */
fff8551c 24242 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
24243 {
24244 char *relative = file_file_name (file, lh);
24245
24246 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24247 return relative;
b36cec19
PA
24248 return reconcat (relative, comp_dir, SLASH_STRING,
24249 relative, (char *) NULL);
233d95b5
JK
24250 }
24251 else
24252 return file_file_name (file, lh);
24253}
24254
2e276125
JB
24255
24256static struct macro_source_file *
24257macro_start_file (int file, int line,
24258 struct macro_source_file *current_file,
43f3e411 24259 struct line_header *lh)
2e276125 24260{
233d95b5
JK
24261 /* File name relative to the compilation directory of this source file. */
24262 char *file_name = file_file_name (file, lh);
2e276125 24263
2e276125 24264 if (! current_file)
abc9d0dc 24265 {
fc474241
DE
24266 /* Note: We don't create a macro table for this compilation unit
24267 at all until we actually get a filename. */
43f3e411 24268 struct macro_table *macro_table = get_macro_table ();
fc474241 24269
abc9d0dc
TT
24270 /* If we have no current file, then this must be the start_file
24271 directive for the compilation unit's main source file. */
fc474241
DE
24272 current_file = macro_set_main (macro_table, file_name);
24273 macro_define_special (macro_table);
abc9d0dc 24274 }
2e276125 24275 else
233d95b5 24276 current_file = macro_include (current_file, line, file_name);
2e276125 24277
233d95b5 24278 xfree (file_name);
6e70227d 24279
2e276125
JB
24280 return current_file;
24281}
24282
2e276125
JB
24283static const char *
24284consume_improper_spaces (const char *p, const char *body)
24285{
24286 if (*p == ' ')
24287 {
4d3c2250 24288 complaint (&symfile_complaints,
3e43a32a
MS
24289 _("macro definition contains spaces "
24290 "in formal argument list:\n`%s'"),
4d3c2250 24291 body);
2e276125
JB
24292
24293 while (*p == ' ')
24294 p++;
24295 }
24296
24297 return p;
24298}
24299
24300
24301static void
24302parse_macro_definition (struct macro_source_file *file, int line,
24303 const char *body)
24304{
24305 const char *p;
24306
24307 /* The body string takes one of two forms. For object-like macro
24308 definitions, it should be:
24309
24310 <macro name> " " <definition>
24311
24312 For function-like macro definitions, it should be:
24313
24314 <macro name> "() " <definition>
24315 or
24316 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24317
24318 Spaces may appear only where explicitly indicated, and in the
24319 <definition>.
24320
24321 The Dwarf 2 spec says that an object-like macro's name is always
24322 followed by a space, but versions of GCC around March 2002 omit
6e70227d 24323 the space when the macro's definition is the empty string.
2e276125
JB
24324
24325 The Dwarf 2 spec says that there should be no spaces between the
24326 formal arguments in a function-like macro's formal argument list,
24327 but versions of GCC around March 2002 include spaces after the
24328 commas. */
24329
24330
24331 /* Find the extent of the macro name. The macro name is terminated
24332 by either a space or null character (for an object-like macro) or
24333 an opening paren (for a function-like macro). */
24334 for (p = body; *p; p++)
24335 if (*p == ' ' || *p == '(')
24336 break;
24337
24338 if (*p == ' ' || *p == '\0')
24339 {
24340 /* It's an object-like macro. */
24341 int name_len = p - body;
3f8a7804 24342 char *name = savestring (body, name_len);
2e276125
JB
24343 const char *replacement;
24344
24345 if (*p == ' ')
24346 replacement = body + name_len + 1;
24347 else
24348 {
4d3c2250 24349 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24350 replacement = body + name_len;
24351 }
6e70227d 24352
2e276125
JB
24353 macro_define_object (file, line, name, replacement);
24354
24355 xfree (name);
24356 }
24357 else if (*p == '(')
24358 {
24359 /* It's a function-like macro. */
3f8a7804 24360 char *name = savestring (body, p - body);
2e276125
JB
24361 int argc = 0;
24362 int argv_size = 1;
8d749320 24363 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
24364
24365 p++;
24366
24367 p = consume_improper_spaces (p, body);
24368
24369 /* Parse the formal argument list. */
24370 while (*p && *p != ')')
24371 {
24372 /* Find the extent of the current argument name. */
24373 const char *arg_start = p;
24374
24375 while (*p && *p != ',' && *p != ')' && *p != ' ')
24376 p++;
24377
24378 if (! *p || p == arg_start)
4d3c2250 24379 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24380 else
24381 {
24382 /* Make sure argv has room for the new argument. */
24383 if (argc >= argv_size)
24384 {
24385 argv_size *= 2;
224c3ddb 24386 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
24387 }
24388
3f8a7804 24389 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
24390 }
24391
24392 p = consume_improper_spaces (p, body);
24393
24394 /* Consume the comma, if present. */
24395 if (*p == ',')
24396 {
24397 p++;
24398
24399 p = consume_improper_spaces (p, body);
24400 }
24401 }
24402
24403 if (*p == ')')
24404 {
24405 p++;
24406
24407 if (*p == ' ')
24408 /* Perfectly formed definition, no complaints. */
24409 macro_define_function (file, line, name,
6e70227d 24410 argc, (const char **) argv,
2e276125
JB
24411 p + 1);
24412 else if (*p == '\0')
24413 {
24414 /* Complain, but do define it. */
4d3c2250 24415 dwarf2_macro_malformed_definition_complaint (body);
2e276125 24416 macro_define_function (file, line, name,
6e70227d 24417 argc, (const char **) argv,
2e276125
JB
24418 p);
24419 }
24420 else
24421 /* Just complain. */
4d3c2250 24422 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24423 }
24424 else
24425 /* Just complain. */
4d3c2250 24426 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24427
24428 xfree (name);
24429 {
24430 int i;
24431
24432 for (i = 0; i < argc; i++)
24433 xfree (argv[i]);
24434 }
24435 xfree (argv);
24436 }
24437 else
4d3c2250 24438 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24439}
24440
cf2c3c16
TT
24441/* Skip some bytes from BYTES according to the form given in FORM.
24442 Returns the new pointer. */
2e276125 24443
d521ce57
TT
24444static const gdb_byte *
24445skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
24446 enum dwarf_form form,
24447 unsigned int offset_size,
24448 struct dwarf2_section_info *section)
2e276125 24449{
cf2c3c16 24450 unsigned int bytes_read;
2e276125 24451
cf2c3c16 24452 switch (form)
2e276125 24453 {
cf2c3c16
TT
24454 case DW_FORM_data1:
24455 case DW_FORM_flag:
24456 ++bytes;
24457 break;
24458
24459 case DW_FORM_data2:
24460 bytes += 2;
24461 break;
24462
24463 case DW_FORM_data4:
24464 bytes += 4;
24465 break;
24466
24467 case DW_FORM_data8:
24468 bytes += 8;
24469 break;
24470
0224619f
JK
24471 case DW_FORM_data16:
24472 bytes += 16;
24473 break;
24474
cf2c3c16
TT
24475 case DW_FORM_string:
24476 read_direct_string (abfd, bytes, &bytes_read);
24477 bytes += bytes_read;
24478 break;
24479
24480 case DW_FORM_sec_offset:
24481 case DW_FORM_strp:
36586728 24482 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
24483 bytes += offset_size;
24484 break;
24485
24486 case DW_FORM_block:
24487 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24488 bytes += bytes_read;
24489 break;
24490
24491 case DW_FORM_block1:
24492 bytes += 1 + read_1_byte (abfd, bytes);
24493 break;
24494 case DW_FORM_block2:
24495 bytes += 2 + read_2_bytes (abfd, bytes);
24496 break;
24497 case DW_FORM_block4:
24498 bytes += 4 + read_4_bytes (abfd, bytes);
24499 break;
24500
24501 case DW_FORM_sdata:
24502 case DW_FORM_udata:
3019eac3
DE
24503 case DW_FORM_GNU_addr_index:
24504 case DW_FORM_GNU_str_index:
d521ce57 24505 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
24506 if (bytes == NULL)
24507 {
24508 dwarf2_section_buffer_overflow_complaint (section);
24509 return NULL;
24510 }
cf2c3c16
TT
24511 break;
24512
663c44ac
JK
24513 case DW_FORM_implicit_const:
24514 break;
24515
cf2c3c16
TT
24516 default:
24517 {
cf2c3c16
TT
24518 complaint (&symfile_complaints,
24519 _("invalid form 0x%x in `%s'"),
a32a8923 24520 form, get_section_name (section));
cf2c3c16
TT
24521 return NULL;
24522 }
2e276125
JB
24523 }
24524
cf2c3c16
TT
24525 return bytes;
24526}
757a13d0 24527
cf2c3c16
TT
24528/* A helper for dwarf_decode_macros that handles skipping an unknown
24529 opcode. Returns an updated pointer to the macro data buffer; or,
24530 on error, issues a complaint and returns NULL. */
757a13d0 24531
d521ce57 24532static const gdb_byte *
cf2c3c16 24533skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24534 const gdb_byte **opcode_definitions,
24535 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24536 bfd *abfd,
24537 unsigned int offset_size,
24538 struct dwarf2_section_info *section)
24539{
24540 unsigned int bytes_read, i;
24541 unsigned long arg;
d521ce57 24542 const gdb_byte *defn;
2e276125 24543
cf2c3c16 24544 if (opcode_definitions[opcode] == NULL)
2e276125 24545 {
cf2c3c16
TT
24546 complaint (&symfile_complaints,
24547 _("unrecognized DW_MACFINO opcode 0x%x"),
24548 opcode);
24549 return NULL;
24550 }
2e276125 24551
cf2c3c16
TT
24552 defn = opcode_definitions[opcode];
24553 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24554 defn += bytes_read;
2e276125 24555
cf2c3c16
TT
24556 for (i = 0; i < arg; ++i)
24557 {
aead7601
SM
24558 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24559 (enum dwarf_form) defn[i], offset_size,
f664829e 24560 section);
cf2c3c16
TT
24561 if (mac_ptr == NULL)
24562 {
24563 /* skip_form_bytes already issued the complaint. */
24564 return NULL;
24565 }
24566 }
757a13d0 24567
cf2c3c16
TT
24568 return mac_ptr;
24569}
757a13d0 24570
cf2c3c16
TT
24571/* A helper function which parses the header of a macro section.
24572 If the macro section is the extended (for now called "GNU") type,
24573 then this updates *OFFSET_SIZE. Returns a pointer to just after
24574 the header, or issues a complaint and returns NULL on error. */
757a13d0 24575
d521ce57
TT
24576static const gdb_byte *
24577dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24578 bfd *abfd,
d521ce57 24579 const gdb_byte *mac_ptr,
cf2c3c16
TT
24580 unsigned int *offset_size,
24581 int section_is_gnu)
24582{
24583 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24584
cf2c3c16
TT
24585 if (section_is_gnu)
24586 {
24587 unsigned int version, flags;
757a13d0 24588
cf2c3c16 24589 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24590 if (version != 4 && version != 5)
cf2c3c16
TT
24591 {
24592 complaint (&symfile_complaints,
24593 _("unrecognized version `%d' in .debug_macro section"),
24594 version);
24595 return NULL;
24596 }
24597 mac_ptr += 2;
757a13d0 24598
cf2c3c16
TT
24599 flags = read_1_byte (abfd, mac_ptr);
24600 ++mac_ptr;
24601 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24602
cf2c3c16
TT
24603 if ((flags & 2) != 0)
24604 /* We don't need the line table offset. */
24605 mac_ptr += *offset_size;
757a13d0 24606
cf2c3c16
TT
24607 /* Vendor opcode descriptions. */
24608 if ((flags & 4) != 0)
24609 {
24610 unsigned int i, count;
757a13d0 24611
cf2c3c16
TT
24612 count = read_1_byte (abfd, mac_ptr);
24613 ++mac_ptr;
24614 for (i = 0; i < count; ++i)
24615 {
24616 unsigned int opcode, bytes_read;
24617 unsigned long arg;
24618
24619 opcode = read_1_byte (abfd, mac_ptr);
24620 ++mac_ptr;
24621 opcode_definitions[opcode] = mac_ptr;
24622 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24623 mac_ptr += bytes_read;
24624 mac_ptr += arg;
24625 }
757a13d0 24626 }
cf2c3c16 24627 }
757a13d0 24628
cf2c3c16
TT
24629 return mac_ptr;
24630}
757a13d0 24631
cf2c3c16 24632/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24633 including DW_MACRO_import. */
cf2c3c16
TT
24634
24635static void
ed2dc618
SM
24636dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24637 bfd *abfd,
d521ce57 24638 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24639 struct macro_source_file *current_file,
43f3e411 24640 struct line_header *lh,
cf2c3c16 24641 struct dwarf2_section_info *section,
36586728 24642 int section_is_gnu, int section_is_dwz,
cf2c3c16 24643 unsigned int offset_size,
8fc3fc34 24644 htab_t include_hash)
cf2c3c16 24645{
4d663531 24646 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24647 enum dwarf_macro_record_type macinfo_type;
24648 int at_commandline;
d521ce57 24649 const gdb_byte *opcode_definitions[256];
757a13d0 24650
cf2c3c16
TT
24651 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24652 &offset_size, section_is_gnu);
24653 if (mac_ptr == NULL)
24654 {
24655 /* We already issued a complaint. */
24656 return;
24657 }
757a13d0
JK
24658
24659 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24660 GDB is still reading the definitions from command line. First
24661 DW_MACINFO_start_file will need to be ignored as it was already executed
24662 to create CURRENT_FILE for the main source holding also the command line
24663 definitions. On first met DW_MACINFO_start_file this flag is reset to
24664 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24665
24666 at_commandline = 1;
24667
24668 do
24669 {
24670 /* Do we at least have room for a macinfo type byte? */
24671 if (mac_ptr >= mac_end)
24672 {
f664829e 24673 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24674 break;
24675 }
24676
aead7601 24677 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24678 mac_ptr++;
24679
cf2c3c16
TT
24680 /* Note that we rely on the fact that the corresponding GNU and
24681 DWARF constants are the same. */
132448f8
SM
24682 DIAGNOSTIC_PUSH
24683 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24684 switch (macinfo_type)
24685 {
24686 /* A zero macinfo type indicates the end of the macro
24687 information. */
24688 case 0:
24689 break;
2e276125 24690
0af92d60
JK
24691 case DW_MACRO_define:
24692 case DW_MACRO_undef:
24693 case DW_MACRO_define_strp:
24694 case DW_MACRO_undef_strp:
24695 case DW_MACRO_define_sup:
24696 case DW_MACRO_undef_sup:
2e276125 24697 {
891d2f0b 24698 unsigned int bytes_read;
2e276125 24699 int line;
d521ce57 24700 const char *body;
cf2c3c16 24701 int is_define;
2e276125 24702
cf2c3c16
TT
24703 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24704 mac_ptr += bytes_read;
24705
0af92d60
JK
24706 if (macinfo_type == DW_MACRO_define
24707 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24708 {
24709 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24710 mac_ptr += bytes_read;
24711 }
24712 else
24713 {
24714 LONGEST str_offset;
24715
24716 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24717 mac_ptr += offset_size;
2e276125 24718
0af92d60
JK
24719 if (macinfo_type == DW_MACRO_define_sup
24720 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24721 || section_is_dwz)
36586728 24722 {
ed2dc618
SM
24723 struct dwz_file *dwz
24724 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24725
ed2dc618
SM
24726 body = read_indirect_string_from_dwz (objfile,
24727 dwz, str_offset);
36586728
TT
24728 }
24729 else
ed2dc618
SM
24730 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24731 abfd, str_offset);
cf2c3c16
TT
24732 }
24733
0af92d60
JK
24734 is_define = (macinfo_type == DW_MACRO_define
24735 || macinfo_type == DW_MACRO_define_strp
24736 || macinfo_type == DW_MACRO_define_sup);
2e276125 24737 if (! current_file)
757a13d0
JK
24738 {
24739 /* DWARF violation as no main source is present. */
24740 complaint (&symfile_complaints,
24741 _("debug info with no main source gives macro %s "
24742 "on line %d: %s"),
cf2c3c16
TT
24743 is_define ? _("definition") : _("undefinition"),
24744 line, body);
757a13d0
JK
24745 break;
24746 }
3e43a32a
MS
24747 if ((line == 0 && !at_commandline)
24748 || (line != 0 && at_commandline))
4d3c2250 24749 complaint (&symfile_complaints,
757a13d0
JK
24750 _("debug info gives %s macro %s with %s line %d: %s"),
24751 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24752 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24753 line == 0 ? _("zero") : _("non-zero"), line, body);
24754
cf2c3c16 24755 if (is_define)
757a13d0 24756 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24757 else
24758 {
0af92d60
JK
24759 gdb_assert (macinfo_type == DW_MACRO_undef
24760 || macinfo_type == DW_MACRO_undef_strp
24761 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24762 macro_undef (current_file, line, body);
24763 }
2e276125
JB
24764 }
24765 break;
24766
0af92d60 24767 case DW_MACRO_start_file:
2e276125 24768 {
891d2f0b 24769 unsigned int bytes_read;
2e276125
JB
24770 int line, file;
24771
24772 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24773 mac_ptr += bytes_read;
24774 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24775 mac_ptr += bytes_read;
24776
3e43a32a
MS
24777 if ((line == 0 && !at_commandline)
24778 || (line != 0 && at_commandline))
757a13d0
JK
24779 complaint (&symfile_complaints,
24780 _("debug info gives source %d included "
24781 "from %s at %s line %d"),
24782 file, at_commandline ? _("command-line") : _("file"),
24783 line == 0 ? _("zero") : _("non-zero"), line);
24784
24785 if (at_commandline)
24786 {
0af92d60 24787 /* This DW_MACRO_start_file was executed in the
cf2c3c16 24788 pass one. */
757a13d0
JK
24789 at_commandline = 0;
24790 }
24791 else
43f3e411 24792 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
24793 }
24794 break;
24795
0af92d60 24796 case DW_MACRO_end_file:
2e276125 24797 if (! current_file)
4d3c2250 24798 complaint (&symfile_complaints,
3e43a32a
MS
24799 _("macro debug info has an unmatched "
24800 "`close_file' directive"));
2e276125
JB
24801 else
24802 {
24803 current_file = current_file->included_by;
24804 if (! current_file)
24805 {
cf2c3c16 24806 enum dwarf_macro_record_type next_type;
2e276125
JB
24807
24808 /* GCC circa March 2002 doesn't produce the zero
24809 type byte marking the end of the compilation
24810 unit. Complain if it's not there, but exit no
24811 matter what. */
24812
24813 /* Do we at least have room for a macinfo type byte? */
24814 if (mac_ptr >= mac_end)
24815 {
f664829e 24816 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
24817 return;
24818 }
24819
24820 /* We don't increment mac_ptr here, so this is just
24821 a look-ahead. */
aead7601
SM
24822 next_type
24823 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24824 mac_ptr);
2e276125 24825 if (next_type != 0)
4d3c2250 24826 complaint (&symfile_complaints,
3e43a32a
MS
24827 _("no terminating 0-type entry for "
24828 "macros in `.debug_macinfo' section"));
2e276125
JB
24829
24830 return;
24831 }
24832 }
24833 break;
24834
0af92d60
JK
24835 case DW_MACRO_import:
24836 case DW_MACRO_import_sup:
cf2c3c16
TT
24837 {
24838 LONGEST offset;
8fc3fc34 24839 void **slot;
a036ba48
TT
24840 bfd *include_bfd = abfd;
24841 struct dwarf2_section_info *include_section = section;
d521ce57 24842 const gdb_byte *include_mac_end = mac_end;
a036ba48 24843 int is_dwz = section_is_dwz;
d521ce57 24844 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
24845
24846 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24847 mac_ptr += offset_size;
24848
0af92d60 24849 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 24850 {
ed2dc618 24851 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 24852
4d663531 24853 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 24854
a036ba48 24855 include_section = &dwz->macro;
a32a8923 24856 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
24857 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24858 is_dwz = 1;
24859 }
24860
24861 new_mac_ptr = include_section->buffer + offset;
24862 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24863
8fc3fc34
TT
24864 if (*slot != NULL)
24865 {
24866 /* This has actually happened; see
24867 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24868 complaint (&symfile_complaints,
0af92d60 24869 _("recursive DW_MACRO_import in "
8fc3fc34
TT
24870 ".debug_macro section"));
24871 }
24872 else
24873 {
d521ce57 24874 *slot = (void *) new_mac_ptr;
36586728 24875
ed2dc618
SM
24876 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24877 include_bfd, new_mac_ptr,
43f3e411 24878 include_mac_end, current_file, lh,
36586728 24879 section, section_is_gnu, is_dwz,
4d663531 24880 offset_size, include_hash);
8fc3fc34 24881
d521ce57 24882 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 24883 }
cf2c3c16
TT
24884 }
24885 break;
24886
2e276125 24887 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
24888 if (!section_is_gnu)
24889 {
24890 unsigned int bytes_read;
2e276125 24891
ac298888
TT
24892 /* This reads the constant, but since we don't recognize
24893 any vendor extensions, we ignore it. */
24894 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
24895 mac_ptr += bytes_read;
24896 read_direct_string (abfd, mac_ptr, &bytes_read);
24897 mac_ptr += bytes_read;
2e276125 24898
cf2c3c16
TT
24899 /* We don't recognize any vendor extensions. */
24900 break;
24901 }
24902 /* FALLTHROUGH */
24903
24904 default:
24905 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24906 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24907 section);
24908 if (mac_ptr == NULL)
24909 return;
24910 break;
2e276125 24911 }
132448f8 24912 DIAGNOSTIC_POP
757a13d0 24913 } while (macinfo_type != 0);
2e276125 24914}
8e19ed76 24915
cf2c3c16 24916static void
09262596 24917dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 24918 int section_is_gnu)
cf2c3c16 24919{
518817b3
SM
24920 struct dwarf2_per_objfile *dwarf2_per_objfile
24921 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24922 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
24923 struct line_header *lh = cu->line_header;
24924 bfd *abfd;
d521ce57 24925 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
24926 struct macro_source_file *current_file = 0;
24927 enum dwarf_macro_record_type macinfo_type;
24928 unsigned int offset_size = cu->header.offset_size;
d521ce57 24929 const gdb_byte *opcode_definitions[256];
8fc3fc34 24930 void **slot;
09262596
DE
24931 struct dwarf2_section_info *section;
24932 const char *section_name;
24933
24934 if (cu->dwo_unit != NULL)
24935 {
24936 if (section_is_gnu)
24937 {
24938 section = &cu->dwo_unit->dwo_file->sections.macro;
24939 section_name = ".debug_macro.dwo";
24940 }
24941 else
24942 {
24943 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24944 section_name = ".debug_macinfo.dwo";
24945 }
24946 }
24947 else
24948 {
24949 if (section_is_gnu)
24950 {
24951 section = &dwarf2_per_objfile->macro;
24952 section_name = ".debug_macro";
24953 }
24954 else
24955 {
24956 section = &dwarf2_per_objfile->macinfo;
24957 section_name = ".debug_macinfo";
24958 }
24959 }
cf2c3c16 24960
bb5ed363 24961 dwarf2_read_section (objfile, section);
cf2c3c16
TT
24962 if (section->buffer == NULL)
24963 {
fceca515 24964 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
24965 return;
24966 }
a32a8923 24967 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
24968
24969 /* First pass: Find the name of the base filename.
24970 This filename is needed in order to process all macros whose definition
24971 (or undefinition) comes from the command line. These macros are defined
24972 before the first DW_MACINFO_start_file entry, and yet still need to be
24973 associated to the base file.
24974
24975 To determine the base file name, we scan the macro definitions until we
24976 reach the first DW_MACINFO_start_file entry. We then initialize
24977 CURRENT_FILE accordingly so that any macro definition found before the
24978 first DW_MACINFO_start_file can still be associated to the base file. */
24979
24980 mac_ptr = section->buffer + offset;
24981 mac_end = section->buffer + section->size;
24982
24983 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24984 &offset_size, section_is_gnu);
24985 if (mac_ptr == NULL)
24986 {
24987 /* We already issued a complaint. */
24988 return;
24989 }
24990
24991 do
24992 {
24993 /* Do we at least have room for a macinfo type byte? */
24994 if (mac_ptr >= mac_end)
24995 {
24996 /* Complaint is printed during the second pass as GDB will probably
24997 stop the first pass earlier upon finding
24998 DW_MACINFO_start_file. */
24999 break;
25000 }
25001
aead7601 25002 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
25003 mac_ptr++;
25004
25005 /* Note that we rely on the fact that the corresponding GNU and
25006 DWARF constants are the same. */
132448f8
SM
25007 DIAGNOSTIC_PUSH
25008 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
25009 switch (macinfo_type)
25010 {
25011 /* A zero macinfo type indicates the end of the macro
25012 information. */
25013 case 0:
25014 break;
25015
0af92d60
JK
25016 case DW_MACRO_define:
25017 case DW_MACRO_undef:
cf2c3c16
TT
25018 /* Only skip the data by MAC_PTR. */
25019 {
25020 unsigned int bytes_read;
25021
25022 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25023 mac_ptr += bytes_read;
25024 read_direct_string (abfd, mac_ptr, &bytes_read);
25025 mac_ptr += bytes_read;
25026 }
25027 break;
25028
0af92d60 25029 case DW_MACRO_start_file:
cf2c3c16
TT
25030 {
25031 unsigned int bytes_read;
25032 int line, file;
25033
25034 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25035 mac_ptr += bytes_read;
25036 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25037 mac_ptr += bytes_read;
25038
43f3e411 25039 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
25040 }
25041 break;
25042
0af92d60 25043 case DW_MACRO_end_file:
cf2c3c16
TT
25044 /* No data to skip by MAC_PTR. */
25045 break;
25046
0af92d60
JK
25047 case DW_MACRO_define_strp:
25048 case DW_MACRO_undef_strp:
25049 case DW_MACRO_define_sup:
25050 case DW_MACRO_undef_sup:
cf2c3c16
TT
25051 {
25052 unsigned int bytes_read;
25053
25054 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25055 mac_ptr += bytes_read;
25056 mac_ptr += offset_size;
25057 }
25058 break;
25059
0af92d60
JK
25060 case DW_MACRO_import:
25061 case DW_MACRO_import_sup:
cf2c3c16 25062 /* Note that, according to the spec, a transparent include
0af92d60 25063 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
25064 skip this opcode. */
25065 mac_ptr += offset_size;
25066 break;
25067
25068 case DW_MACINFO_vendor_ext:
25069 /* Only skip the data by MAC_PTR. */
25070 if (!section_is_gnu)
25071 {
25072 unsigned int bytes_read;
25073
25074 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25075 mac_ptr += bytes_read;
25076 read_direct_string (abfd, mac_ptr, &bytes_read);
25077 mac_ptr += bytes_read;
25078 }
25079 /* FALLTHROUGH */
25080
25081 default:
25082 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 25083 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
25084 section);
25085 if (mac_ptr == NULL)
25086 return;
25087 break;
25088 }
132448f8 25089 DIAGNOSTIC_POP
cf2c3c16
TT
25090 } while (macinfo_type != 0 && current_file == NULL);
25091
25092 /* Second pass: Process all entries.
25093
25094 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25095 command-line macro definitions/undefinitions. This flag is unset when we
25096 reach the first DW_MACINFO_start_file entry. */
25097
fc4007c9
TT
25098 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25099 htab_eq_pointer,
25100 NULL, xcalloc, xfree));
8fc3fc34 25101 mac_ptr = section->buffer + offset;
fc4007c9 25102 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 25103 *slot = (void *) mac_ptr;
ed2dc618
SM
25104 dwarf_decode_macro_bytes (dwarf2_per_objfile,
25105 abfd, mac_ptr, mac_end,
43f3e411 25106 current_file, lh, section,
fc4007c9
TT
25107 section_is_gnu, 0, offset_size,
25108 include_hash.get ());
cf2c3c16
TT
25109}
25110
8e19ed76 25111/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 25112 if so return true else false. */
380bca97 25113
8e19ed76 25114static int
6e5a29e1 25115attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
25116{
25117 return (attr == NULL ? 0 :
25118 attr->form == DW_FORM_block1
25119 || attr->form == DW_FORM_block2
25120 || attr->form == DW_FORM_block4
2dc7f7b3
TT
25121 || attr->form == DW_FORM_block
25122 || attr->form == DW_FORM_exprloc);
8e19ed76 25123}
4c2df51b 25124
c6a0999f
JB
25125/* Return non-zero if ATTR's value is a section offset --- classes
25126 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25127 You may use DW_UNSND (attr) to retrieve such offsets.
25128
25129 Section 7.5.4, "Attribute Encodings", explains that no attribute
25130 may have a value that belongs to more than one of these classes; it
25131 would be ambiguous if we did, because we use the same forms for all
25132 of them. */
380bca97 25133
3690dd37 25134static int
6e5a29e1 25135attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
25136{
25137 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
25138 || attr->form == DW_FORM_data8
25139 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
25140}
25141
3690dd37
JB
25142/* Return non-zero if ATTR's value falls in the 'constant' class, or
25143 zero otherwise. When this function returns true, you can apply
25144 dwarf2_get_attr_constant_value to it.
25145
25146 However, note that for some attributes you must check
25147 attr_form_is_section_offset before using this test. DW_FORM_data4
25148 and DW_FORM_data8 are members of both the constant class, and of
25149 the classes that contain offsets into other debug sections
25150 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25151 that, if an attribute's can be either a constant or one of the
25152 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
25153 taken as section offsets, not constants.
25154
25155 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25156 cannot handle that. */
380bca97 25157
3690dd37 25158static int
6e5a29e1 25159attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
25160{
25161 switch (attr->form)
25162 {
25163 case DW_FORM_sdata:
25164 case DW_FORM_udata:
25165 case DW_FORM_data1:
25166 case DW_FORM_data2:
25167 case DW_FORM_data4:
25168 case DW_FORM_data8:
663c44ac 25169 case DW_FORM_implicit_const:
3690dd37
JB
25170 return 1;
25171 default:
25172 return 0;
25173 }
25174}
25175
7771576e
SA
25176
25177/* DW_ADDR is always stored already as sect_offset; despite for the forms
25178 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25179
25180static int
6e5a29e1 25181attr_form_is_ref (const struct attribute *attr)
7771576e
SA
25182{
25183 switch (attr->form)
25184 {
25185 case DW_FORM_ref_addr:
25186 case DW_FORM_ref1:
25187 case DW_FORM_ref2:
25188 case DW_FORM_ref4:
25189 case DW_FORM_ref8:
25190 case DW_FORM_ref_udata:
25191 case DW_FORM_GNU_ref_alt:
25192 return 1;
25193 default:
25194 return 0;
25195 }
25196}
25197
3019eac3
DE
25198/* Return the .debug_loc section to use for CU.
25199 For DWO files use .debug_loc.dwo. */
25200
25201static struct dwarf2_section_info *
25202cu_debug_loc_section (struct dwarf2_cu *cu)
25203{
518817b3
SM
25204 struct dwarf2_per_objfile *dwarf2_per_objfile
25205 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 25206
3019eac3 25207 if (cu->dwo_unit)
43988095
JK
25208 {
25209 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25210
25211 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25212 }
25213 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25214 : &dwarf2_per_objfile->loc);
3019eac3
DE
25215}
25216
8cf6f0b1
TT
25217/* A helper function that fills in a dwarf2_loclist_baton. */
25218
25219static void
25220fill_in_loclist_baton (struct dwarf2_cu *cu,
25221 struct dwarf2_loclist_baton *baton,
ff39bb5e 25222 const struct attribute *attr)
8cf6f0b1 25223{
518817b3
SM
25224 struct dwarf2_per_objfile *dwarf2_per_objfile
25225 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
25226 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25227
25228 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
25229
25230 baton->per_cu = cu->per_cu;
25231 gdb_assert (baton->per_cu);
25232 /* We don't know how long the location list is, but make sure we
25233 don't run off the edge of the section. */
3019eac3
DE
25234 baton->size = section->size - DW_UNSND (attr);
25235 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 25236 baton->base_address = cu->base_address;
f664829e 25237 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
25238}
25239
4c2df51b 25240static void
ff39bb5e 25241dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 25242 struct dwarf2_cu *cu, int is_block)
4c2df51b 25243{
518817b3
SM
25244 struct dwarf2_per_objfile *dwarf2_per_objfile
25245 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 25246 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 25247 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 25248
3690dd37 25249 if (attr_form_is_section_offset (attr)
3019eac3 25250 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
25251 the section. If so, fall through to the complaint in the
25252 other branch. */
3019eac3 25253 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 25254 {
0d53c4c4 25255 struct dwarf2_loclist_baton *baton;
4c2df51b 25256
8d749320 25257 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 25258
8cf6f0b1 25259 fill_in_loclist_baton (cu, baton, attr);
be391dca 25260
d00adf39 25261 if (cu->base_known == 0)
0d53c4c4 25262 complaint (&symfile_complaints,
3e43a32a
MS
25263 _("Location list used without "
25264 "specifying the CU base address."));
4c2df51b 25265
f1e6e072
TT
25266 SYMBOL_ACLASS_INDEX (sym) = (is_block
25267 ? dwarf2_loclist_block_index
25268 : dwarf2_loclist_index);
0d53c4c4
DJ
25269 SYMBOL_LOCATION_BATON (sym) = baton;
25270 }
25271 else
25272 {
25273 struct dwarf2_locexpr_baton *baton;
25274
8d749320 25275 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
25276 baton->per_cu = cu->per_cu;
25277 gdb_assert (baton->per_cu);
0d53c4c4
DJ
25278
25279 if (attr_form_is_block (attr))
25280 {
25281 /* Note that we're just copying the block's data pointer
25282 here, not the actual data. We're still pointing into the
6502dd73
DJ
25283 info_buffer for SYM's objfile; right now we never release
25284 that buffer, but when we do clean up properly this may
25285 need to change. */
0d53c4c4
DJ
25286 baton->size = DW_BLOCK (attr)->size;
25287 baton->data = DW_BLOCK (attr)->data;
25288 }
25289 else
25290 {
25291 dwarf2_invalid_attrib_class_complaint ("location description",
25292 SYMBOL_NATURAL_NAME (sym));
25293 baton->size = 0;
0d53c4c4 25294 }
6e70227d 25295
f1e6e072
TT
25296 SYMBOL_ACLASS_INDEX (sym) = (is_block
25297 ? dwarf2_locexpr_block_index
25298 : dwarf2_locexpr_index);
0d53c4c4
DJ
25299 SYMBOL_LOCATION_BATON (sym) = baton;
25300 }
4c2df51b 25301}
6502dd73 25302
9aa1f1e3
TT
25303/* Return the OBJFILE associated with the compilation unit CU. If CU
25304 came from a separate debuginfo file, then the master objfile is
25305 returned. */
ae0d2f24
UW
25306
25307struct objfile *
25308dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25309{
e3b94546 25310 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
25311
25312 /* Return the master objfile, so that we can report and look up the
25313 correct file containing this variable. */
25314 if (objfile->separate_debug_objfile_backlink)
25315 objfile = objfile->separate_debug_objfile_backlink;
25316
25317 return objfile;
25318}
25319
96408a79
SA
25320/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25321 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25322 CU_HEADERP first. */
25323
25324static const struct comp_unit_head *
25325per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25326 struct dwarf2_per_cu_data *per_cu)
25327{
d521ce57 25328 const gdb_byte *info_ptr;
96408a79
SA
25329
25330 if (per_cu->cu)
25331 return &per_cu->cu->header;
25332
9c541725 25333 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
25334
25335 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
25336 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25337 rcuh_kind::COMPILE);
96408a79
SA
25338
25339 return cu_headerp;
25340}
25341
ae0d2f24
UW
25342/* Return the address size given in the compilation unit header for CU. */
25343
98714339 25344int
ae0d2f24
UW
25345dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25346{
96408a79
SA
25347 struct comp_unit_head cu_header_local;
25348 const struct comp_unit_head *cu_headerp;
c471e790 25349
96408a79
SA
25350 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25351
25352 return cu_headerp->addr_size;
ae0d2f24
UW
25353}
25354
9eae7c52
TT
25355/* Return the offset size given in the compilation unit header for CU. */
25356
25357int
25358dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25359{
96408a79
SA
25360 struct comp_unit_head cu_header_local;
25361 const struct comp_unit_head *cu_headerp;
9c6c53f7 25362
96408a79
SA
25363 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25364
25365 return cu_headerp->offset_size;
25366}
25367
25368/* See its dwarf2loc.h declaration. */
25369
25370int
25371dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25372{
25373 struct comp_unit_head cu_header_local;
25374 const struct comp_unit_head *cu_headerp;
25375
25376 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25377
25378 if (cu_headerp->version == 2)
25379 return cu_headerp->addr_size;
25380 else
25381 return cu_headerp->offset_size;
181cebd4
JK
25382}
25383
9aa1f1e3
TT
25384/* Return the text offset of the CU. The returned offset comes from
25385 this CU's objfile. If this objfile came from a separate debuginfo
25386 file, then the offset may be different from the corresponding
25387 offset in the parent objfile. */
25388
25389CORE_ADDR
25390dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25391{
e3b94546 25392 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9aa1f1e3
TT
25393
25394 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25395}
25396
43988095
JK
25397/* Return DWARF version number of PER_CU. */
25398
25399short
25400dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25401{
25402 return per_cu->dwarf_version;
25403}
25404
348e048f
DE
25405/* Locate the .debug_info compilation unit from CU's objfile which contains
25406 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
25407
25408static struct dwarf2_per_cu_data *
9c541725 25409dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 25410 unsigned int offset_in_dwz,
ed2dc618 25411 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25412{
25413 struct dwarf2_per_cu_data *this_cu;
25414 int low, high;
36586728 25415 const sect_offset *cu_off;
ae038cb0 25416
ae038cb0
DJ
25417 low = 0;
25418 high = dwarf2_per_objfile->n_comp_units - 1;
25419 while (high > low)
25420 {
36586728 25421 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 25422 int mid = low + (high - low) / 2;
9a619af0 25423
36586728 25424 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
9c541725 25425 cu_off = &mid_cu->sect_off;
36586728 25426 if (mid_cu->is_dwz > offset_in_dwz
9c541725 25427 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
ae038cb0
DJ
25428 high = mid;
25429 else
25430 low = mid + 1;
25431 }
25432 gdb_assert (low == high);
36586728 25433 this_cu = dwarf2_per_objfile->all_comp_units[low];
9c541725
PA
25434 cu_off = &this_cu->sect_off;
25435 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
ae038cb0 25436 {
36586728 25437 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 25438 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
25439 "offset %s [in module %s]"),
25440 sect_offset_str (sect_off),
ed2dc618 25441 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 25442
9c541725
PA
25443 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25444 <= sect_off);
ae038cb0
DJ
25445 return dwarf2_per_objfile->all_comp_units[low-1];
25446 }
25447 else
25448 {
25449 this_cu = dwarf2_per_objfile->all_comp_units[low];
25450 if (low == dwarf2_per_objfile->n_comp_units - 1
9c541725 25451 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 25452 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 25453 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
25454 return this_cu;
25455 }
25456}
25457
23745b47 25458/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 25459
fcd3b13d
SM
25460dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25461 : per_cu (per_cu_),
25462 mark (0),
25463 has_loclist (0),
25464 checked_producer (0),
25465 producer_is_gxx_lt_4_6 (0),
25466 producer_is_gcc_lt_4_3 (0),
25467 producer_is_icc_lt_14 (0),
25468 processing_has_namespace_info (0)
93311388 25469{
fcd3b13d
SM
25470 per_cu->cu = this;
25471}
25472
25473/* Destroy a dwarf2_cu. */
25474
25475dwarf2_cu::~dwarf2_cu ()
25476{
25477 per_cu->cu = NULL;
9816fde3
JK
25478}
25479
25480/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25481
25482static void
95554aad
TT
25483prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25484 enum language pretend_language)
9816fde3
JK
25485{
25486 struct attribute *attr;
25487
25488 /* Set the language we're debugging. */
25489 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25490 if (attr)
25491 set_cu_language (DW_UNSND (attr), cu);
25492 else
9cded63f 25493 {
95554aad 25494 cu->language = pretend_language;
9cded63f
TT
25495 cu->language_defn = language_def (cu->language);
25496 }
dee91e82 25497
7d45c7c3 25498 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25499}
25500
ae038cb0
DJ
25501/* Free all cached compilation units. */
25502
25503static void
25504free_cached_comp_units (void *data)
25505{
ed2dc618
SM
25506 struct dwarf2_per_objfile *dwarf2_per_objfile
25507 = (struct dwarf2_per_objfile *) data;
25508
330cdd98 25509 dwarf2_per_objfile->free_cached_comp_units ();
ae038cb0
DJ
25510}
25511
25512/* Increase the age counter on each cached compilation unit, and free
25513 any that are too old. */
25514
25515static void
ed2dc618 25516age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25517{
25518 struct dwarf2_per_cu_data *per_cu, **last_chain;
25519
25520 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25521 per_cu = dwarf2_per_objfile->read_in_chain;
25522 while (per_cu != NULL)
25523 {
25524 per_cu->cu->last_used ++;
b4f54984 25525 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
25526 dwarf2_mark (per_cu->cu);
25527 per_cu = per_cu->cu->read_in_chain;
25528 }
25529
25530 per_cu = dwarf2_per_objfile->read_in_chain;
25531 last_chain = &dwarf2_per_objfile->read_in_chain;
25532 while (per_cu != NULL)
25533 {
25534 struct dwarf2_per_cu_data *next_cu;
25535
25536 next_cu = per_cu->cu->read_in_chain;
25537
25538 if (!per_cu->cu->mark)
25539 {
fcd3b13d 25540 delete per_cu->cu;
ae038cb0
DJ
25541 *last_chain = next_cu;
25542 }
25543 else
25544 last_chain = &per_cu->cu->read_in_chain;
25545
25546 per_cu = next_cu;
25547 }
25548}
25549
25550/* Remove a single compilation unit from the cache. */
25551
25552static void
dee91e82 25553free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25554{
25555 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
25556 struct dwarf2_per_objfile *dwarf2_per_objfile
25557 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
25558
25559 per_cu = dwarf2_per_objfile->read_in_chain;
25560 last_chain = &dwarf2_per_objfile->read_in_chain;
25561 while (per_cu != NULL)
25562 {
25563 struct dwarf2_per_cu_data *next_cu;
25564
25565 next_cu = per_cu->cu->read_in_chain;
25566
dee91e82 25567 if (per_cu == target_per_cu)
ae038cb0 25568 {
fcd3b13d 25569 delete per_cu->cu;
dee91e82 25570 per_cu->cu = NULL;
ae038cb0
DJ
25571 *last_chain = next_cu;
25572 break;
25573 }
25574 else
25575 last_chain = &per_cu->cu->read_in_chain;
25576
25577 per_cu = next_cu;
25578 }
25579}
25580
fe3e1990
DJ
25581/* Release all extra memory associated with OBJFILE. */
25582
25583void
25584dwarf2_free_objfile (struct objfile *objfile)
25585{
ed2dc618
SM
25586 struct dwarf2_per_objfile *dwarf2_per_objfile
25587 = get_dwarf2_per_objfile (objfile);
fe3e1990 25588
fd90ace4 25589 delete dwarf2_per_objfile;
fe3e1990
DJ
25590}
25591
dee91e82
DE
25592/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25593 We store these in a hash table separate from the DIEs, and preserve them
25594 when the DIEs are flushed out of cache.
25595
25596 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25597 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25598 or the type may come from a DWO file. Furthermore, while it's more logical
25599 to use per_cu->section+offset, with Fission the section with the data is in
25600 the DWO file but we don't know that section at the point we need it.
25601 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25602 because we can enter the lookup routine, get_die_type_at_offset, from
25603 outside this file, and thus won't necessarily have PER_CU->cu.
25604 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25605
dee91e82 25606struct dwarf2_per_cu_offset_and_type
1c379e20 25607{
dee91e82 25608 const struct dwarf2_per_cu_data *per_cu;
9c541725 25609 sect_offset sect_off;
1c379e20
DJ
25610 struct type *type;
25611};
25612
dee91e82 25613/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25614
25615static hashval_t
dee91e82 25616per_cu_offset_and_type_hash (const void *item)
1c379e20 25617{
9a3c8263
SM
25618 const struct dwarf2_per_cu_offset_and_type *ofs
25619 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25620
9c541725 25621 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25622}
25623
dee91e82 25624/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25625
25626static int
dee91e82 25627per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25628{
9a3c8263
SM
25629 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25630 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25631 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25632 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25633
dee91e82 25634 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25635 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25636}
25637
25638/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25639 table if necessary. For convenience, return TYPE.
25640
25641 The DIEs reading must have careful ordering to:
25642 * Not cause infite loops trying to read in DIEs as a prerequisite for
25643 reading current DIE.
25644 * Not trying to dereference contents of still incompletely read in types
25645 while reading in other DIEs.
25646 * Enable referencing still incompletely read in types just by a pointer to
25647 the type without accessing its fields.
25648
25649 Therefore caller should follow these rules:
25650 * Try to fetch any prerequisite types we may need to build this DIE type
25651 before building the type and calling set_die_type.
e71ec853 25652 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25653 possible before fetching more types to complete the current type.
25654 * Make the type as complete as possible before fetching more types. */
1c379e20 25655
f792889a 25656static struct type *
1c379e20
DJ
25657set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25658{
518817b3
SM
25659 struct dwarf2_per_objfile *dwarf2_per_objfile
25660 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25661 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25662 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25663 struct attribute *attr;
25664 struct dynamic_prop prop;
1c379e20 25665
b4ba55a1
JB
25666 /* For Ada types, make sure that the gnat-specific data is always
25667 initialized (if not already set). There are a few types where
25668 we should not be doing so, because the type-specific area is
25669 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25670 where the type-specific area is used to store the floatformat).
25671 But this is not a problem, because the gnat-specific information
25672 is actually not needed for these types. */
25673 if (need_gnat_info (cu)
25674 && TYPE_CODE (type) != TYPE_CODE_FUNC
25675 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25676 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25677 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25678 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25679 && !HAVE_GNAT_AUX_INFO (type))
25680 INIT_GNAT_SPECIFIC (type);
25681
3f2f83dd
KB
25682 /* Read DW_AT_allocated and set in type. */
25683 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25684 if (attr_form_is_block (attr))
25685 {
25686 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25687 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25688 }
25689 else if (attr != NULL)
25690 {
25691 complaint (&symfile_complaints,
9d8780f0 25692 _("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 25693 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25694 sect_offset_str (die->sect_off));
3f2f83dd
KB
25695 }
25696
25697 /* Read DW_AT_associated and set in type. */
25698 attr = dwarf2_attr (die, DW_AT_associated, cu);
25699 if (attr_form_is_block (attr))
25700 {
25701 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25702 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25703 }
25704 else if (attr != NULL)
25705 {
25706 complaint (&symfile_complaints,
9d8780f0 25707 _("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 25708 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25709 sect_offset_str (die->sect_off));
3f2f83dd
KB
25710 }
25711
3cdcd0ce
JB
25712 /* Read DW_AT_data_location and set in type. */
25713 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25714 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25715 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25716
dee91e82 25717 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25718 {
dee91e82
DE
25719 dwarf2_per_objfile->die_type_hash =
25720 htab_create_alloc_ex (127,
25721 per_cu_offset_and_type_hash,
25722 per_cu_offset_and_type_eq,
25723 NULL,
25724 &objfile->objfile_obstack,
25725 hashtab_obstack_allocate,
25726 dummy_obstack_deallocate);
f792889a 25727 }
1c379e20 25728
dee91e82 25729 ofs.per_cu = cu->per_cu;
9c541725 25730 ofs.sect_off = die->sect_off;
1c379e20 25731 ofs.type = type;
dee91e82
DE
25732 slot = (struct dwarf2_per_cu_offset_and_type **)
25733 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
25734 if (*slot)
25735 complaint (&symfile_complaints,
9d8780f0
SM
25736 _("A problem internal to GDB: DIE %s has type already set"),
25737 sect_offset_str (die->sect_off));
8d749320
SM
25738 *slot = XOBNEW (&objfile->objfile_obstack,
25739 struct dwarf2_per_cu_offset_and_type);
1c379e20 25740 **slot = ofs;
f792889a 25741 return type;
1c379e20
DJ
25742}
25743
9c541725 25744/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25745 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25746
25747static struct type *
9c541725 25748get_die_type_at_offset (sect_offset sect_off,
673bfd45 25749 struct dwarf2_per_cu_data *per_cu)
1c379e20 25750{
dee91e82 25751 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 25752 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 25753
dee91e82 25754 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25755 return NULL;
1c379e20 25756
dee91e82 25757 ofs.per_cu = per_cu;
9c541725 25758 ofs.sect_off = sect_off;
9a3c8263
SM
25759 slot = ((struct dwarf2_per_cu_offset_and_type *)
25760 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25761 if (slot)
25762 return slot->type;
25763 else
25764 return NULL;
25765}
25766
02142a6c 25767/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25768 or return NULL if DIE does not have a saved type. */
25769
25770static struct type *
25771get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25772{
9c541725 25773 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
25774}
25775
10b3939b
DJ
25776/* Add a dependence relationship from CU to REF_PER_CU. */
25777
25778static void
25779dwarf2_add_dependence (struct dwarf2_cu *cu,
25780 struct dwarf2_per_cu_data *ref_per_cu)
25781{
25782 void **slot;
25783
25784 if (cu->dependencies == NULL)
25785 cu->dependencies
25786 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25787 NULL, &cu->comp_unit_obstack,
25788 hashtab_obstack_allocate,
25789 dummy_obstack_deallocate);
25790
25791 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25792 if (*slot == NULL)
25793 *slot = ref_per_cu;
25794}
1c379e20 25795
f504f079
DE
25796/* Subroutine of dwarf2_mark to pass to htab_traverse.
25797 Set the mark field in every compilation unit in the
ae038cb0
DJ
25798 cache that we must keep because we are keeping CU. */
25799
10b3939b
DJ
25800static int
25801dwarf2_mark_helper (void **slot, void *data)
25802{
25803 struct dwarf2_per_cu_data *per_cu;
25804
25805 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
25806
25807 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25808 reading of the chain. As such dependencies remain valid it is not much
25809 useful to track and undo them during QUIT cleanups. */
25810 if (per_cu->cu == NULL)
25811 return 1;
25812
10b3939b
DJ
25813 if (per_cu->cu->mark)
25814 return 1;
25815 per_cu->cu->mark = 1;
25816
25817 if (per_cu->cu->dependencies != NULL)
25818 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25819
25820 return 1;
25821}
25822
f504f079
DE
25823/* Set the mark field in CU and in every other compilation unit in the
25824 cache that we must keep because we are keeping CU. */
25825
ae038cb0
DJ
25826static void
25827dwarf2_mark (struct dwarf2_cu *cu)
25828{
25829 if (cu->mark)
25830 return;
25831 cu->mark = 1;
10b3939b
DJ
25832 if (cu->dependencies != NULL)
25833 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
25834}
25835
25836static void
25837dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25838{
25839 while (per_cu)
25840 {
25841 per_cu->cu->mark = 0;
25842 per_cu = per_cu->cu->read_in_chain;
25843 }
72bf9492
DJ
25844}
25845
72bf9492
DJ
25846/* Trivial hash function for partial_die_info: the hash value of a DIE
25847 is its offset in .debug_info for this objfile. */
25848
25849static hashval_t
25850partial_die_hash (const void *item)
25851{
9a3c8263
SM
25852 const struct partial_die_info *part_die
25853 = (const struct partial_die_info *) item;
9a619af0 25854
9c541725 25855 return to_underlying (part_die->sect_off);
72bf9492
DJ
25856}
25857
25858/* Trivial comparison function for partial_die_info structures: two DIEs
25859 are equal if they have the same offset. */
25860
25861static int
25862partial_die_eq (const void *item_lhs, const void *item_rhs)
25863{
9a3c8263
SM
25864 const struct partial_die_info *part_die_lhs
25865 = (const struct partial_die_info *) item_lhs;
25866 const struct partial_die_info *part_die_rhs
25867 = (const struct partial_die_info *) item_rhs;
9a619af0 25868
9c541725 25869 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25870}
25871
b4f54984
DE
25872static struct cmd_list_element *set_dwarf_cmdlist;
25873static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
25874
25875static void
981a3fb3 25876set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 25877{
b4f54984 25878 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 25879 gdb_stdout);
ae038cb0
DJ
25880}
25881
25882static void
981a3fb3 25883show_dwarf_cmd (const char *args, int from_tty)
6e70227d 25884{
b4f54984 25885 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
25886}
25887
ae2de4f8 25888/* The "save gdb-index" command. */
9291a0cd 25889
437afbb8
JK
25890/* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
25891 error checking. */
25892
25893static void
25894file_write (FILE *file, const void *data, size_t size)
25895{
25896 if (fwrite (data, 1, size, file) != size)
25897 error (_("couldn't data write to file"));
25898}
25899
25900/* Write the contents of VEC to FILE, with error checking. */
25901
25902template<typename Elem, typename Alloc>
25903static void
25904file_write (FILE *file, const std::vector<Elem, Alloc> &vec)
25905{
25906 file_write (file, vec.data (), vec.size () * sizeof (vec[0]));
25907}
25908
bc8f2430
JK
25909/* In-memory buffer to prepare data to be written later to a file. */
25910class data_buf
9291a0cd 25911{
bc8f2430 25912public:
bc8f2430
JK
25913 /* Copy DATA to the end of the buffer. */
25914 template<typename T>
25915 void append_data (const T &data)
25916 {
25917 std::copy (reinterpret_cast<const gdb_byte *> (&data),
25918 reinterpret_cast<const gdb_byte *> (&data + 1),
c2f134ac 25919 grow (sizeof (data)));
bc8f2430 25920 }
b89be57b 25921
c2f134ac
PA
25922 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
25923 terminating zero is appended too. */
bc8f2430
JK
25924 void append_cstr0 (const char *cstr)
25925 {
25926 const size_t size = strlen (cstr) + 1;
c2f134ac
PA
25927 std::copy (cstr, cstr + size, grow (size));
25928 }
25929
437afbb8
JK
25930 /* Store INPUT as ULEB128 to the end of buffer. */
25931 void append_unsigned_leb128 (ULONGEST input)
25932 {
25933 for (;;)
25934 {
25935 gdb_byte output = input & 0x7f;
25936 input >>= 7;
25937 if (input)
25938 output |= 0x80;
25939 append_data (output);
25940 if (input == 0)
25941 break;
25942 }
25943 }
25944
c2f134ac
PA
25945 /* Accept a host-format integer in VAL and append it to the buffer
25946 as a target-format integer which is LEN bytes long. */
25947 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
25948 {
25949 ::store_unsigned_integer (grow (len), len, byte_order, val);
bc8f2430 25950 }
9291a0cd 25951
bc8f2430
JK
25952 /* Return the size of the buffer. */
25953 size_t size () const
25954 {
25955 return m_vec.size ();
25956 }
25957
437afbb8
JK
25958 /* Return true iff the buffer is empty. */
25959 bool empty () const
25960 {
25961 return m_vec.empty ();
25962 }
25963
bc8f2430
JK
25964 /* Write the buffer to FILE. */
25965 void file_write (FILE *file) const
25966 {
437afbb8 25967 ::file_write (file, m_vec);
bc8f2430
JK
25968 }
25969
25970private:
c2f134ac
PA
25971 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
25972 the start of the new block. */
25973 gdb_byte *grow (size_t size)
25974 {
25975 m_vec.resize (m_vec.size () + size);
25976 return &*m_vec.end () - size;
25977 }
25978
d5722aa2 25979 gdb::byte_vector m_vec;
bc8f2430 25980};
9291a0cd
TT
25981
25982/* An entry in the symbol table. */
25983struct symtab_index_entry
25984{
25985 /* The name of the symbol. */
25986 const char *name;
25987 /* The offset of the name in the constant pool. */
25988 offset_type index_offset;
25989 /* A sorted vector of the indices of all the CUs that hold an object
25990 of this name. */
bc8f2430 25991 std::vector<offset_type> cu_indices;
9291a0cd
TT
25992};
25993
25994/* The symbol table. This is a power-of-2-sized hash table. */
25995struct mapped_symtab
25996{
bc8f2430
JK
25997 mapped_symtab ()
25998 {
25999 data.resize (1024);
26000 }
b89be57b 26001
bc8f2430 26002 offset_type n_elements = 0;
4b76cda9 26003 std::vector<symtab_index_entry> data;
bc8f2430 26004};
9291a0cd 26005
bc8f2430 26006/* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
559a7a62
JK
26007 the slot.
26008
26009 Function is used only during write_hash_table so no index format backward
26010 compatibility is needed. */
b89be57b 26011
4b76cda9 26012static symtab_index_entry &
9291a0cd
TT
26013find_slot (struct mapped_symtab *symtab, const char *name)
26014{
559a7a62 26015 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd 26016
bc8f2430
JK
26017 index = hash & (symtab->data.size () - 1);
26018 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
9291a0cd
TT
26019
26020 for (;;)
26021 {
4b76cda9
PA
26022 if (symtab->data[index].name == NULL
26023 || strcmp (name, symtab->data[index].name) == 0)
bc8f2430
JK
26024 return symtab->data[index];
26025 index = (index + step) & (symtab->data.size () - 1);
9291a0cd
TT
26026 }
26027}
26028
26029/* Expand SYMTAB's hash table. */
b89be57b 26030
9291a0cd
TT
26031static void
26032hash_expand (struct mapped_symtab *symtab)
26033{
bc8f2430 26034 auto old_entries = std::move (symtab->data);
9291a0cd 26035
bc8f2430
JK
26036 symtab->data.clear ();
26037 symtab->data.resize (old_entries.size () * 2);
9291a0cd 26038
bc8f2430 26039 for (auto &it : old_entries)
4b76cda9 26040 if (it.name != NULL)
bc8f2430 26041 {
4b76cda9 26042 auto &ref = find_slot (symtab, it.name);
bc8f2430
JK
26043 ref = std::move (it);
26044 }
9291a0cd
TT
26045}
26046
156942c7
DE
26047/* Add an entry to SYMTAB. NAME is the name of the symbol.
26048 CU_INDEX is the index of the CU in which the symbol appears.
26049 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 26050
9291a0cd
TT
26051static void
26052add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 26053 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
26054 offset_type cu_index)
26055{
156942c7 26056 offset_type cu_index_and_attrs;
9291a0cd
TT
26057
26058 ++symtab->n_elements;
bc8f2430 26059 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
9291a0cd
TT
26060 hash_expand (symtab);
26061
4b76cda9
PA
26062 symtab_index_entry &slot = find_slot (symtab, name);
26063 if (slot.name == NULL)
9291a0cd 26064 {
4b76cda9 26065 slot.name = name;
156942c7 26066 /* index_offset is set later. */
9291a0cd 26067 }
156942c7
DE
26068
26069 cu_index_and_attrs = 0;
26070 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
26071 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
26072 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
26073
26074 /* We don't want to record an index value twice as we want to avoid the
26075 duplication.
26076 We process all global symbols and then all static symbols
26077 (which would allow us to avoid the duplication by only having to check
26078 the last entry pushed), but a symbol could have multiple kinds in one CU.
26079 To keep things simple we don't worry about the duplication here and
26080 sort and uniqufy the list after we've processed all symbols. */
4b76cda9 26081 slot.cu_indices.push_back (cu_index_and_attrs);
156942c7
DE
26082}
26083
26084/* Sort and remove duplicates of all symbols' cu_indices lists. */
26085
26086static void
26087uniquify_cu_indices (struct mapped_symtab *symtab)
26088{
4b76cda9 26089 for (auto &entry : symtab->data)
156942c7 26090 {
4b76cda9 26091 if (entry.name != NULL && !entry.cu_indices.empty ())
156942c7 26092 {
4b76cda9 26093 auto &cu_indices = entry.cu_indices;
6fd931f2
PA
26094 std::sort (cu_indices.begin (), cu_indices.end ());
26095 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
26096 cu_indices.erase (from, cu_indices.end ());
156942c7
DE
26097 }
26098 }
9291a0cd
TT
26099}
26100
bc8f2430
JK
26101/* A form of 'const char *' suitable for container keys. Only the
26102 pointer is stored. The strings themselves are compared, not the
26103 pointers. */
26104class c_str_view
9291a0cd 26105{
bc8f2430
JK
26106public:
26107 c_str_view (const char *cstr)
26108 : m_cstr (cstr)
26109 {}
9291a0cd 26110
bc8f2430
JK
26111 bool operator== (const c_str_view &other) const
26112 {
26113 return strcmp (m_cstr, other.m_cstr) == 0;
26114 }
9291a0cd 26115
437afbb8
JK
26116 /* Return the underlying C string. Note, the returned string is
26117 only a reference with lifetime of this object. */
26118 const char *c_str () const
26119 {
26120 return m_cstr;
26121 }
26122
bc8f2430
JK
26123private:
26124 friend class c_str_view_hasher;
26125 const char *const m_cstr;
26126};
9291a0cd 26127
bc8f2430
JK
26128/* A std::unordered_map::hasher for c_str_view that uses the right
26129 hash function for strings in a mapped index. */
26130class c_str_view_hasher
26131{
26132public:
26133 size_t operator () (const c_str_view &x) const
26134 {
26135 return mapped_index_string_hash (INT_MAX, x.m_cstr);
26136 }
26137};
b89be57b 26138
bc8f2430
JK
26139/* A std::unordered_map::hasher for std::vector<>. */
26140template<typename T>
26141class vector_hasher
9291a0cd 26142{
bc8f2430
JK
26143public:
26144 size_t operator () (const std::vector<T> &key) const
26145 {
26146 return iterative_hash (key.data (),
26147 sizeof (key.front ()) * key.size (), 0);
26148 }
26149};
9291a0cd 26150
bc8f2430
JK
26151/* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
26152 constant pool entries going into the data buffer CPOOL. */
3876f04e 26153
bc8f2430
JK
26154static void
26155write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
26156{
26157 {
26158 /* Elements are sorted vectors of the indices of all the CUs that
26159 hold an object of this name. */
26160 std::unordered_map<std::vector<offset_type>, offset_type,
26161 vector_hasher<offset_type>>
26162 symbol_hash_table;
26163
26164 /* We add all the index vectors to the constant pool first, to
26165 ensure alignment is ok. */
4b76cda9 26166 for (symtab_index_entry &entry : symtab->data)
bc8f2430 26167 {
4b76cda9 26168 if (entry.name == NULL)
bc8f2430 26169 continue;
4b76cda9 26170 gdb_assert (entry.index_offset == 0);
70a1152b
PA
26171
26172 /* Finding before inserting is faster than always trying to
26173 insert, because inserting always allocates a node, does the
26174 lookup, and then destroys the new node if another node
26175 already had the same key. C++17 try_emplace will avoid
26176 this. */
26177 const auto found
4b76cda9 26178 = symbol_hash_table.find (entry.cu_indices);
70a1152b
PA
26179 if (found != symbol_hash_table.end ())
26180 {
4b76cda9 26181 entry.index_offset = found->second;
70a1152b
PA
26182 continue;
26183 }
26184
4b76cda9
PA
26185 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
26186 entry.index_offset = cpool.size ();
26187 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
26188 for (const auto index : entry.cu_indices)
26189 cpool.append_data (MAYBE_SWAP (index));
bc8f2430
JK
26190 }
26191 }
9291a0cd
TT
26192
26193 /* Now write out the hash table. */
bc8f2430 26194 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
4b76cda9 26195 for (const auto &entry : symtab->data)
9291a0cd
TT
26196 {
26197 offset_type str_off, vec_off;
26198
4b76cda9 26199 if (entry.name != NULL)
9291a0cd 26200 {
4b76cda9 26201 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
bc8f2430 26202 if (insertpair.second)
4b76cda9 26203 cpool.append_cstr0 (entry.name);
bc8f2430 26204 str_off = insertpair.first->second;
4b76cda9 26205 vec_off = entry.index_offset;
9291a0cd
TT
26206 }
26207 else
26208 {
26209 /* While 0 is a valid constant pool index, it is not valid
26210 to have 0 for both offsets. */
26211 str_off = 0;
26212 vec_off = 0;
26213 }
26214
bc8f2430
JK
26215 output.append_data (MAYBE_SWAP (str_off));
26216 output.append_data (MAYBE_SWAP (vec_off));
9291a0cd 26217 }
9291a0cd
TT
26218}
26219
bc8f2430 26220typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
0a5429f6
DE
26221
26222/* Helper struct for building the address table. */
26223struct addrmap_index_data
26224{
bc8f2430
JK
26225 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
26226 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
26227 {}
26228
0a5429f6 26229 struct objfile *objfile;
bc8f2430
JK
26230 data_buf &addr_vec;
26231 psym_index_map &cu_index_htab;
0a5429f6
DE
26232
26233 /* Non-zero if the previous_* fields are valid.
26234 We can't write an entry until we see the next entry (since it is only then
26235 that we know the end of the entry). */
26236 int previous_valid;
26237 /* Index of the CU in the table of all CUs in the index file. */
26238 unsigned int previous_cu_index;
0963b4bd 26239 /* Start address of the CU. */
0a5429f6
DE
26240 CORE_ADDR previous_cu_start;
26241};
26242
bc8f2430 26243/* Write an address entry to ADDR_VEC. */
b89be57b 26244
9291a0cd 26245static void
bc8f2430 26246add_address_entry (struct objfile *objfile, data_buf &addr_vec,
0a5429f6 26247 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 26248{
9291a0cd
TT
26249 CORE_ADDR baseaddr;
26250
26251 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
26252
c2f134ac
PA
26253 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
26254 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
bc8f2430 26255 addr_vec.append_data (MAYBE_SWAP (cu_index));
0a5429f6
DE
26256}
26257
26258/* Worker function for traversing an addrmap to build the address table. */
26259
26260static int
26261add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
26262{
9a3c8263
SM
26263 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
26264 struct partial_symtab *pst = (struct partial_symtab *) obj;
0a5429f6
DE
26265
26266 if (data->previous_valid)
bc8f2430 26267 add_address_entry (data->objfile, data->addr_vec,
0a5429f6
DE
26268 data->previous_cu_start, start_addr,
26269 data->previous_cu_index);
26270
26271 data->previous_cu_start = start_addr;
26272 if (pst != NULL)
26273 {
bc8f2430
JK
26274 const auto it = data->cu_index_htab.find (pst);
26275 gdb_assert (it != data->cu_index_htab.cend ());
26276 data->previous_cu_index = it->second;
0a5429f6
DE
26277 data->previous_valid = 1;
26278 }
26279 else
bc8f2430 26280 data->previous_valid = 0;
0a5429f6
DE
26281
26282 return 0;
26283}
26284
bc8f2430 26285/* Write OBJFILE's address map to ADDR_VEC.
0a5429f6
DE
26286 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
26287 in the index file. */
26288
26289static void
bc8f2430
JK
26290write_address_map (struct objfile *objfile, data_buf &addr_vec,
26291 psym_index_map &cu_index_htab)
0a5429f6 26292{
bc8f2430 26293 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
0a5429f6
DE
26294
26295 /* When writing the address table, we have to cope with the fact that
26296 the addrmap iterator only provides the start of a region; we have to
26297 wait until the next invocation to get the start of the next region. */
26298
26299 addrmap_index_data.objfile = objfile;
0a5429f6
DE
26300 addrmap_index_data.previous_valid = 0;
26301
26302 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
26303 &addrmap_index_data);
26304
26305 /* It's highly unlikely the last entry (end address = 0xff...ff)
26306 is valid, but we should still handle it.
26307 The end address is recorded as the start of the next region, but that
26308 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
26309 anyway. */
26310 if (addrmap_index_data.previous_valid)
bc8f2430 26311 add_address_entry (objfile, addr_vec,
0a5429f6
DE
26312 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
26313 addrmap_index_data.previous_cu_index);
9291a0cd
TT
26314}
26315
156942c7
DE
26316/* Return the symbol kind of PSYM. */
26317
26318static gdb_index_symbol_kind
26319symbol_kind (struct partial_symbol *psym)
26320{
26321 domain_enum domain = PSYMBOL_DOMAIN (psym);
26322 enum address_class aclass = PSYMBOL_CLASS (psym);
26323
26324 switch (domain)
26325 {
26326 case VAR_DOMAIN:
26327 switch (aclass)
26328 {
26329 case LOC_BLOCK:
26330 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
26331 case LOC_TYPEDEF:
26332 return GDB_INDEX_SYMBOL_KIND_TYPE;
26333 case LOC_COMPUTED:
26334 case LOC_CONST_BYTES:
26335 case LOC_OPTIMIZED_OUT:
26336 case LOC_STATIC:
26337 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
26338 case LOC_CONST:
26339 /* Note: It's currently impossible to recognize psyms as enum values
26340 short of reading the type info. For now punt. */
26341 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
26342 default:
26343 /* There are other LOC_FOO values that one might want to classify
26344 as variables, but dwarf2read.c doesn't currently use them. */
26345 return GDB_INDEX_SYMBOL_KIND_OTHER;
26346 }
26347 case STRUCT_DOMAIN:
26348 return GDB_INDEX_SYMBOL_KIND_TYPE;
26349 default:
26350 return GDB_INDEX_SYMBOL_KIND_OTHER;
26351 }
26352}
26353
9291a0cd 26354/* Add a list of partial symbols to SYMTAB. */
b89be57b 26355
9291a0cd
TT
26356static void
26357write_psymbols (struct mapped_symtab *symtab,
bc8f2430 26358 std::unordered_set<partial_symbol *> &psyms_seen,
9291a0cd
TT
26359 struct partial_symbol **psymp,
26360 int count,
987d643c
TT
26361 offset_type cu_index,
26362 int is_static)
9291a0cd
TT
26363{
26364 for (; count-- > 0; ++psymp)
26365 {
156942c7 26366 struct partial_symbol *psym = *psymp;
987d643c 26367
156942c7 26368 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 26369 error (_("Ada is not currently supported by the index"));
987d643c 26370
987d643c 26371 /* Only add a given psymbol once. */
bc8f2430 26372 if (psyms_seen.insert (psym).second)
987d643c 26373 {
156942c7
DE
26374 gdb_index_symbol_kind kind = symbol_kind (psym);
26375
156942c7
DE
26376 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
26377 is_static, kind, cu_index);
987d643c 26378 }
9291a0cd
TT
26379 }
26380}
26381
1fd400ff
TT
26382/* A helper struct used when iterating over debug_types. */
26383struct signatured_type_index_data
26384{
bc8f2430
JK
26385 signatured_type_index_data (data_buf &types_list_,
26386 std::unordered_set<partial_symbol *> &psyms_seen_)
26387 : types_list (types_list_), psyms_seen (psyms_seen_)
26388 {}
26389
1fd400ff
TT
26390 struct objfile *objfile;
26391 struct mapped_symtab *symtab;
bc8f2430
JK
26392 data_buf &types_list;
26393 std::unordered_set<partial_symbol *> &psyms_seen;
1fd400ff
TT
26394 int cu_index;
26395};
26396
26397/* A helper function that writes a single signatured_type to an
26398 obstack. */
b89be57b 26399
1fd400ff
TT
26400static int
26401write_one_signatured_type (void **slot, void *d)
26402{
9a3c8263
SM
26403 struct signatured_type_index_data *info
26404 = (struct signatured_type_index_data *) d;
1fd400ff 26405 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 26406 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
26407
26408 write_psymbols (info->symtab,
987d643c 26409 info->psyms_seen,
af5bf4ad 26410 &info->objfile->global_psymbols[psymtab->globals_offset],
987d643c
TT
26411 psymtab->n_global_syms, info->cu_index,
26412 0);
1fd400ff 26413 write_psymbols (info->symtab,
987d643c 26414 info->psyms_seen,
af5bf4ad 26415 &info->objfile->static_psymbols[psymtab->statics_offset],
987d643c
TT
26416 psymtab->n_static_syms, info->cu_index,
26417 1);
1fd400ff 26418
c2f134ac
PA
26419 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26420 to_underlying (entry->per_cu.sect_off));
26421 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26422 to_underlying (entry->type_offset_in_tu));
26423 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
1fd400ff
TT
26424
26425 ++info->cu_index;
26426
26427 return 1;
26428}
26429
e8f8bcb3
PA
26430/* Recurse into all "included" dependencies and count their symbols as
26431 if they appeared in this psymtab. */
26432
26433static void
26434recursively_count_psymbols (struct partial_symtab *psymtab,
26435 size_t &psyms_seen)
26436{
26437 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26438 if (psymtab->dependencies[i]->user != NULL)
26439 recursively_count_psymbols (psymtab->dependencies[i],
26440 psyms_seen);
26441
26442 psyms_seen += psymtab->n_global_syms;
26443 psyms_seen += psymtab->n_static_syms;
26444}
26445
95554aad
TT
26446/* Recurse into all "included" dependencies and write their symbols as
26447 if they appeared in this psymtab. */
26448
26449static void
26450recursively_write_psymbols (struct objfile *objfile,
26451 struct partial_symtab *psymtab,
26452 struct mapped_symtab *symtab,
bc8f2430 26453 std::unordered_set<partial_symbol *> &psyms_seen,
95554aad
TT
26454 offset_type cu_index)
26455{
26456 int i;
26457
26458 for (i = 0; i < psymtab->number_of_dependencies; ++i)
26459 if (psymtab->dependencies[i]->user != NULL)
26460 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26461 symtab, psyms_seen, cu_index);
26462
26463 write_psymbols (symtab,
26464 psyms_seen,
af5bf4ad 26465 &objfile->global_psymbols[psymtab->globals_offset],
95554aad
TT
26466 psymtab->n_global_syms, cu_index,
26467 0);
26468 write_psymbols (symtab,
26469 psyms_seen,
af5bf4ad 26470 &objfile->static_psymbols[psymtab->statics_offset],
95554aad
TT
26471 psymtab->n_static_syms, cu_index,
26472 1);
26473}
26474
437afbb8
JK
26475/* DWARF-5 .debug_names builder. */
26476class debug_names
26477{
26478public:
ed2dc618
SM
26479 debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile, bool is_dwarf64,
26480 bfd_endian dwarf5_byte_order)
437afbb8
JK
26481 : m_dwarf5_byte_order (dwarf5_byte_order),
26482 m_dwarf32 (dwarf5_byte_order),
26483 m_dwarf64 (dwarf5_byte_order),
26484 m_dwarf (is_dwarf64
26485 ? static_cast<dwarf &> (m_dwarf64)
26486 : static_cast<dwarf &> (m_dwarf32)),
26487 m_name_table_string_offs (m_dwarf.name_table_string_offs),
ed2dc618
SM
26488 m_name_table_entry_offs (m_dwarf.name_table_entry_offs),
26489 m_debugstrlookup (dwarf2_per_objfile)
437afbb8 26490 {}
9291a0cd 26491
8af5c486
JK
26492 int dwarf5_offset_size () const
26493 {
26494 const bool dwarf5_is_dwarf64 = &m_dwarf == &m_dwarf64;
26495 return dwarf5_is_dwarf64 ? 8 : 4;
26496 }
26497
26498 /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit? */
26499 enum class unit_kind { cu, tu };
26500
437afbb8 26501 /* Insert one symbol. */
8af5c486
JK
26502 void insert (const partial_symbol *psym, int cu_index, bool is_static,
26503 unit_kind kind)
437afbb8
JK
26504 {
26505 const int dwarf_tag = psymbol_tag (psym);
26506 if (dwarf_tag == 0)
26507 return;
26508 const char *const name = SYMBOL_SEARCH_NAME (psym);
26509 const auto insertpair
26510 = m_name_to_value_set.emplace (c_str_view (name),
26511 std::set<symbol_value> ());
26512 std::set<symbol_value> &value_set = insertpair.first->second;
8af5c486 26513 value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static, kind));
437afbb8 26514 }
9291a0cd 26515
437afbb8
JK
26516 /* Build all the tables. All symbols must be already inserted.
26517 This function does not call file_write, caller has to do it
26518 afterwards. */
26519 void build ()
26520 {
26521 /* Verify the build method has not be called twice. */
26522 gdb_assert (m_abbrev_table.empty ());
26523 const size_t name_count = m_name_to_value_set.size ();
26524 m_bucket_table.resize
26525 (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3))));
26526 m_hash_table.reserve (name_count);
26527 m_name_table_string_offs.reserve (name_count);
26528 m_name_table_entry_offs.reserve (name_count);
26529
26530 /* Map each hash of symbol to its name and value. */
26531 struct hash_it_pair
26532 {
26533 uint32_t hash;
26534 decltype (m_name_to_value_set)::const_iterator it;
26535 };
26536 std::vector<std::forward_list<hash_it_pair>> bucket_hash;
26537 bucket_hash.resize (m_bucket_table.size ());
26538 for (decltype (m_name_to_value_set)::const_iterator it
26539 = m_name_to_value_set.cbegin ();
26540 it != m_name_to_value_set.cend ();
26541 ++it)
26542 {
26543 const char *const name = it->first.c_str ();
26544 const uint32_t hash = dwarf5_djb_hash (name);
26545 hash_it_pair hashitpair;
26546 hashitpair.hash = hash;
26547 hashitpair.it = it;
26548 auto &slot = bucket_hash[hash % bucket_hash.size()];
26549 slot.push_front (std::move (hashitpair));
26550 }
26551 for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix)
26552 {
26553 const std::forward_list<hash_it_pair> &hashitlist
26554 = bucket_hash[bucket_ix];
26555 if (hashitlist.empty ())
26556 continue;
26557 uint32_t &bucket_slot = m_bucket_table[bucket_ix];
26558 /* The hashes array is indexed starting at 1. */
26559 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot),
26560 sizeof (bucket_slot), m_dwarf5_byte_order,
26561 m_hash_table.size () + 1);
26562 for (const hash_it_pair &hashitpair : hashitlist)
26563 {
26564 m_hash_table.push_back (0);
26565 store_unsigned_integer (reinterpret_cast<gdb_byte *>
26566 (&m_hash_table.back ()),
26567 sizeof (m_hash_table.back ()),
26568 m_dwarf5_byte_order, hashitpair.hash);
26569 const c_str_view &name = hashitpair.it->first;
26570 const std::set<symbol_value> &value_set = hashitpair.it->second;
26571 m_name_table_string_offs.push_back_reorder
26572 (m_debugstrlookup.lookup (name.c_str ()));
26573 m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
26574 gdb_assert (!value_set.empty ());
26575 for (const symbol_value &value : value_set)
26576 {
26577 int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag,
8af5c486
JK
26578 value.is_static,
26579 value.kind)];
437afbb8
JK
26580 if (idx == 0)
26581 {
26582 idx = m_idx_next++;
26583 m_abbrev_table.append_unsigned_leb128 (idx);
26584 m_abbrev_table.append_unsigned_leb128 (value.dwarf_tag);
8af5c486
JK
26585 m_abbrev_table.append_unsigned_leb128
26586 (value.kind == unit_kind::cu ? DW_IDX_compile_unit
26587 : DW_IDX_type_unit);
437afbb8
JK
26588 m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
26589 m_abbrev_table.append_unsigned_leb128 (value.is_static
26590 ? DW_IDX_GNU_internal
26591 : DW_IDX_GNU_external);
26592 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
26593
26594 /* Terminate attributes list. */
26595 m_abbrev_table.append_unsigned_leb128 (0);
26596 m_abbrev_table.append_unsigned_leb128 (0);
26597 }
9291a0cd 26598
437afbb8
JK
26599 m_entry_pool.append_unsigned_leb128 (idx);
26600 m_entry_pool.append_unsigned_leb128 (value.cu_index);
26601 }
9291a0cd 26602
437afbb8
JK
26603 /* Terminate the list of CUs. */
26604 m_entry_pool.append_unsigned_leb128 (0);
26605 }
26606 }
26607 gdb_assert (m_hash_table.size () == name_count);
987d643c 26608
437afbb8
JK
26609 /* Terminate tags list. */
26610 m_abbrev_table.append_unsigned_leb128 (0);
26611 }
0a5429f6 26612
437afbb8
JK
26613 /* Return .debug_names bucket count. This must be called only after
26614 calling the build method. */
26615 uint32_t bucket_count () const
26616 {
26617 /* Verify the build method has been already called. */
26618 gdb_assert (!m_abbrev_table.empty ());
26619 const uint32_t retval = m_bucket_table.size ();
26620
26621 /* Check for overflow. */
26622 gdb_assert (retval == m_bucket_table.size ());
26623 return retval;
26624 }
26625
26626 /* Return .debug_names names count. This must be called only after
26627 calling the build method. */
26628 uint32_t name_count () const
26629 {
26630 /* Verify the build method has been already called. */
26631 gdb_assert (!m_abbrev_table.empty ());
26632 const uint32_t retval = m_hash_table.size ();
26633
26634 /* Check for overflow. */
26635 gdb_assert (retval == m_hash_table.size ());
26636 return retval;
26637 }
26638
26639 /* Return number of bytes of .debug_names abbreviation table. This
26640 must be called only after calling the build method. */
26641 uint32_t abbrev_table_bytes () const
26642 {
26643 gdb_assert (!m_abbrev_table.empty ());
26644 return m_abbrev_table.size ();
26645 }
26646
26647 /* Recurse into all "included" dependencies and store their symbols
26648 as if they appeared in this psymtab. */
26649 void recursively_write_psymbols
26650 (struct objfile *objfile,
26651 struct partial_symtab *psymtab,
26652 std::unordered_set<partial_symbol *> &psyms_seen,
26653 int cu_index)
26654 {
26655 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26656 if (psymtab->dependencies[i]->user != NULL)
26657 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26658 psyms_seen, cu_index);
26659
26660 write_psymbols (psyms_seen,
26661 &objfile->global_psymbols[psymtab->globals_offset],
8af5c486 26662 psymtab->n_global_syms, cu_index, false, unit_kind::cu);
437afbb8
JK
26663 write_psymbols (psyms_seen,
26664 &objfile->static_psymbols[psymtab->statics_offset],
8af5c486 26665 psymtab->n_static_syms, cu_index, true, unit_kind::cu);
437afbb8
JK
26666 }
26667
26668 /* Return number of bytes the .debug_names section will have. This
26669 must be called only after calling the build method. */
26670 size_t bytes () const
26671 {
26672 /* Verify the build method has been already called. */
26673 gdb_assert (!m_abbrev_table.empty ());
26674 size_t expected_bytes = 0;
26675 expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]);
26676 expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]);
26677 expected_bytes += m_name_table_string_offs.bytes ();
26678 expected_bytes += m_name_table_entry_offs.bytes ();
26679 expected_bytes += m_abbrev_table.size ();
26680 expected_bytes += m_entry_pool.size ();
26681 return expected_bytes;
26682 }
26683
26684 /* Write .debug_names to FILE_NAMES and .debug_str addition to
26685 FILE_STR. This must be called only after calling the build
26686 method. */
26687 void file_write (FILE *file_names, FILE *file_str) const
26688 {
26689 /* Verify the build method has been already called. */
26690 gdb_assert (!m_abbrev_table.empty ());
26691 ::file_write (file_names, m_bucket_table);
26692 ::file_write (file_names, m_hash_table);
26693 m_name_table_string_offs.file_write (file_names);
26694 m_name_table_entry_offs.file_write (file_names);
26695 m_abbrev_table.file_write (file_names);
26696 m_entry_pool.file_write (file_names);
26697 m_debugstrlookup.file_write (file_str);
26698 }
26699
8af5c486
JK
26700 /* A helper user data for write_one_signatured_type. */
26701 class write_one_signatured_type_data
26702 {
26703 public:
26704 write_one_signatured_type_data (debug_names &nametable_,
26705 signatured_type_index_data &&info_)
26706 : nametable (nametable_), info (std::move (info_))
26707 {}
26708 debug_names &nametable;
26709 struct signatured_type_index_data info;
26710 };
26711
26712 /* A helper function to pass write_one_signatured_type to
26713 htab_traverse_noresize. */
26714 static int
26715 write_one_signatured_type (void **slot, void *d)
26716 {
26717 write_one_signatured_type_data *data = (write_one_signatured_type_data *) d;
26718 struct signatured_type_index_data *info = &data->info;
26719 struct signatured_type *entry = (struct signatured_type *) *slot;
26720
26721 data->nametable.write_one_signatured_type (entry, info);
26722
26723 return 1;
26724 }
26725
437afbb8
JK
26726private:
26727
26728 /* Storage for symbol names mapping them to their .debug_str section
26729 offsets. */
26730 class debug_str_lookup
26731 {
26732 public:
26733
26734 /* Object costructor to be called for current DWARF2_PER_OBJFILE.
26735 All .debug_str section strings are automatically stored. */
ed2dc618
SM
26736 debug_str_lookup (struct dwarf2_per_objfile *dwarf2_per_objfile)
26737 : m_abfd (dwarf2_per_objfile->objfile->obfd),
26738 m_dwarf2_per_objfile (dwarf2_per_objfile)
437afbb8
JK
26739 {
26740 dwarf2_read_section (dwarf2_per_objfile->objfile,
26741 &dwarf2_per_objfile->str);
26742 if (dwarf2_per_objfile->str.buffer == NULL)
26743 return;
26744 for (const gdb_byte *data = dwarf2_per_objfile->str.buffer;
26745 data < (dwarf2_per_objfile->str.buffer
26746 + dwarf2_per_objfile->str.size);)
26747 {
26748 const char *const s = reinterpret_cast<const char *> (data);
26749 const auto insertpair
26750 = m_str_table.emplace (c_str_view (s),
26751 data - dwarf2_per_objfile->str.buffer);
26752 if (!insertpair.second)
26753 complaint (&symfile_complaints,
26754 _("Duplicate string \"%s\" in "
26755 ".debug_str section [in module %s]"),
26756 s, bfd_get_filename (m_abfd));
26757 data += strlen (s) + 1;
26758 }
26759 }
26760
26761 /* Return offset of symbol name S in the .debug_str section. Add
26762 such symbol to the section's end if it does not exist there
26763 yet. */
26764 size_t lookup (const char *s)
26765 {
26766 const auto it = m_str_table.find (c_str_view (s));
26767 if (it != m_str_table.end ())
26768 return it->second;
ed2dc618 26769 const size_t offset = (m_dwarf2_per_objfile->str.size
437afbb8
JK
26770 + m_str_add_buf.size ());
26771 m_str_table.emplace (c_str_view (s), offset);
26772 m_str_add_buf.append_cstr0 (s);
26773 return offset;
26774 }
26775
26776 /* Append the end of the .debug_str section to FILE. */
26777 void file_write (FILE *file) const
26778 {
26779 m_str_add_buf.file_write (file);
26780 }
26781
26782 private:
26783 std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
26784 bfd *const m_abfd;
ed2dc618 26785 struct dwarf2_per_objfile *m_dwarf2_per_objfile;
437afbb8
JK
26786
26787 /* Data to add at the end of .debug_str for new needed symbol names. */
26788 data_buf m_str_add_buf;
26789 };
26790
26791 /* Container to map used DWARF tags to their .debug_names abbreviation
26792 tags. */
26793 class index_key
26794 {
26795 public:
8af5c486
JK
26796 index_key (int dwarf_tag_, bool is_static_, unit_kind kind_)
26797 : dwarf_tag (dwarf_tag_), is_static (is_static_), kind (kind_)
437afbb8
JK
26798 {
26799 }
26800
26801 bool
26802 operator== (const index_key &other) const
26803 {
8af5c486
JK
26804 return (dwarf_tag == other.dwarf_tag && is_static == other.is_static
26805 && kind == other.kind);
437afbb8
JK
26806 }
26807
26808 const int dwarf_tag;
26809 const bool is_static;
8af5c486 26810 const unit_kind kind;
437afbb8
JK
26811 };
26812
26813 /* Provide std::unordered_map::hasher for index_key. */
26814 class index_key_hasher
26815 {
26816 public:
26817 size_t
26818 operator () (const index_key &key) const
26819 {
26820 return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static;
26821 }
26822 };
26823
26824 /* Parameters of one symbol entry. */
26825 class symbol_value
26826 {
26827 public:
26828 const int dwarf_tag, cu_index;
26829 const bool is_static;
8af5c486 26830 const unit_kind kind;
437afbb8 26831
8af5c486
JK
26832 symbol_value (int dwarf_tag_, int cu_index_, bool is_static_,
26833 unit_kind kind_)
26834 : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_),
26835 kind (kind_)
437afbb8
JK
26836 {}
26837
26838 bool
26839 operator< (const symbol_value &other) const
26840 {
26841#define X(n) \
26842 do \
26843 { \
26844 if (n < other.n) \
26845 return true; \
26846 if (n > other.n) \
26847 return false; \
26848 } \
26849 while (0)
26850 X (dwarf_tag);
26851 X (is_static);
8af5c486 26852 X (kind);
437afbb8
JK
26853 X (cu_index);
26854#undef X
26855 return false;
26856 }
26857 };
26858
26859 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
26860 output. */
26861 class offset_vec
26862 {
26863 protected:
26864 const bfd_endian dwarf5_byte_order;
26865 public:
26866 explicit offset_vec (bfd_endian dwarf5_byte_order_)
26867 : dwarf5_byte_order (dwarf5_byte_order_)
26868 {}
26869
26870 /* Call std::vector::reserve for NELEM elements. */
26871 virtual void reserve (size_t nelem) = 0;
26872
26873 /* Call std::vector::push_back with store_unsigned_integer byte
26874 reordering for ELEM. */
26875 virtual void push_back_reorder (size_t elem) = 0;
26876
26877 /* Return expected output size in bytes. */
26878 virtual size_t bytes () const = 0;
26879
26880 /* Write name table to FILE. */
26881 virtual void file_write (FILE *file) const = 0;
26882 };
26883
26884 /* Template to unify DWARF-32 and DWARF-64 output. */
26885 template<typename OffsetSize>
26886 class offset_vec_tmpl : public offset_vec
26887 {
26888 public:
26889 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
26890 : offset_vec (dwarf5_byte_order_)
26891 {}
26892
26893 /* Implement offset_vec::reserve. */
26894 void reserve (size_t nelem) override
26895 {
26896 m_vec.reserve (nelem);
26897 }
26898
26899 /* Implement offset_vec::push_back_reorder. */
26900 void push_back_reorder (size_t elem) override
26901 {
26902 m_vec.push_back (elem);
26903 /* Check for overflow. */
26904 gdb_assert (m_vec.back () == elem);
26905 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
26906 sizeof (m_vec.back ()), dwarf5_byte_order, elem);
26907 }
26908
26909 /* Implement offset_vec::bytes. */
26910 size_t bytes () const override
26911 {
26912 return m_vec.size () * sizeof (m_vec[0]);
26913 }
26914
26915 /* Implement offset_vec::file_write. */
26916 void file_write (FILE *file) const override
26917 {
26918 ::file_write (file, m_vec);
26919 }
26920
26921 private:
26922 std::vector<OffsetSize> m_vec;
26923 };
26924
26925 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
26926 respecting name table width. */
26927 class dwarf
26928 {
26929 public:
26930 offset_vec &name_table_string_offs, &name_table_entry_offs;
26931
26932 dwarf (offset_vec &name_table_string_offs_,
26933 offset_vec &name_table_entry_offs_)
26934 : name_table_string_offs (name_table_string_offs_),
26935 name_table_entry_offs (name_table_entry_offs_)
26936 {
26937 }
26938 };
e8f8bcb3 26939
437afbb8
JK
26940 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
26941 respecting name table width. */
26942 template<typename OffsetSize>
26943 class dwarf_tmpl : public dwarf
26944 {
26945 public:
26946 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
26947 : dwarf (m_name_table_string_offs, m_name_table_entry_offs),
26948 m_name_table_string_offs (dwarf5_byte_order_),
26949 m_name_table_entry_offs (dwarf5_byte_order_)
26950 {}
26951
26952 private:
26953 offset_vec_tmpl<OffsetSize> m_name_table_string_offs;
26954 offset_vec_tmpl<OffsetSize> m_name_table_entry_offs;
26955 };
26956
26957 /* Try to reconstruct original DWARF tag for given partial_symbol.
26958 This function is not DWARF-5 compliant but it is sufficient for
26959 GDB as a DWARF-5 index consumer. */
26960 static int psymbol_tag (const struct partial_symbol *psym)
26961 {
26962 domain_enum domain = PSYMBOL_DOMAIN (psym);
26963 enum address_class aclass = PSYMBOL_CLASS (psym);
26964
26965 switch (domain)
26966 {
26967 case VAR_DOMAIN:
26968 switch (aclass)
26969 {
26970 case LOC_BLOCK:
26971 return DW_TAG_subprogram;
26972 case LOC_TYPEDEF:
26973 return DW_TAG_typedef;
26974 case LOC_COMPUTED:
26975 case LOC_CONST_BYTES:
26976 case LOC_OPTIMIZED_OUT:
26977 case LOC_STATIC:
26978 return DW_TAG_variable;
26979 case LOC_CONST:
26980 /* Note: It's currently impossible to recognize psyms as enum values
26981 short of reading the type info. For now punt. */
26982 return DW_TAG_variable;
26983 default:
26984 /* There are other LOC_FOO values that one might want to classify
26985 as variables, but dwarf2read.c doesn't currently use them. */
26986 return DW_TAG_variable;
26987 }
26988 case STRUCT_DOMAIN:
26989 return DW_TAG_structure_type;
26990 default:
26991 return 0;
26992 }
26993 }
26994
26995 /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */
26996 void write_psymbols (std::unordered_set<partial_symbol *> &psyms_seen,
26997 struct partial_symbol **psymp, int count, int cu_index,
8af5c486 26998 bool is_static, unit_kind kind)
437afbb8
JK
26999 {
27000 for (; count-- > 0; ++psymp)
27001 {
27002 struct partial_symbol *psym = *psymp;
27003
27004 if (SYMBOL_LANGUAGE (psym) == language_ada)
27005 error (_("Ada is not currently supported by the index"));
27006
27007 /* Only add a given psymbol once. */
27008 if (psyms_seen.insert (psym).second)
8af5c486 27009 insert (psym, cu_index, is_static, kind);
437afbb8
JK
27010 }
27011 }
27012
8af5c486
JK
27013 /* A helper function that writes a single signatured_type
27014 to a debug_names. */
27015 void
27016 write_one_signatured_type (struct signatured_type *entry,
27017 struct signatured_type_index_data *info)
27018 {
27019 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
27020
27021 write_psymbols (info->psyms_seen,
27022 &info->objfile->global_psymbols[psymtab->globals_offset],
27023 psymtab->n_global_syms, info->cu_index, false,
27024 unit_kind::tu);
27025 write_psymbols (info->psyms_seen,
27026 &info->objfile->static_psymbols[psymtab->statics_offset],
27027 psymtab->n_static_syms, info->cu_index, true,
27028 unit_kind::tu);
27029
27030 info->types_list.append_uint (dwarf5_offset_size (), m_dwarf5_byte_order,
27031 to_underlying (entry->per_cu.sect_off));
27032
27033 ++info->cu_index;
27034 }
27035
437afbb8
JK
27036 /* Store value of each symbol. */
27037 std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher>
27038 m_name_to_value_set;
27039
27040 /* Tables of DWARF-5 .debug_names. They are in object file byte
27041 order. */
27042 std::vector<uint32_t> m_bucket_table;
27043 std::vector<uint32_t> m_hash_table;
27044
27045 const bfd_endian m_dwarf5_byte_order;
27046 dwarf_tmpl<uint32_t> m_dwarf32;
27047 dwarf_tmpl<uint64_t> m_dwarf64;
27048 dwarf &m_dwarf;
27049 offset_vec &m_name_table_string_offs, &m_name_table_entry_offs;
27050 debug_str_lookup m_debugstrlookup;
27051
27052 /* Map each used .debug_names abbreviation tag parameter to its
27053 index value. */
27054 std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
27055
27056 /* Next unused .debug_names abbreviation tag for
27057 m_indexkey_to_idx. */
27058 int m_idx_next = 1;
27059
27060 /* .debug_names abbreviation table. */
27061 data_buf m_abbrev_table;
27062
27063 /* .debug_names entry pool. */
27064 data_buf m_entry_pool;
27065};
27066
27067/* Return iff any of the needed offsets does not fit into 32-bit
27068 .debug_names section. */
27069
27070static bool
ed2dc618 27071check_dwarf64_offsets (struct dwarf2_per_objfile *dwarf2_per_objfile)
437afbb8
JK
27072{
27073 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27074 {
27075 const dwarf2_per_cu_data &per_cu = *dwarf2_per_objfile->all_comp_units[i];
27076
27077 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
27078 return true;
27079 }
27080 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
27081 {
27082 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
27083 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
27084
27085 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
27086 return true;
27087 }
27088 return false;
27089}
27090
27091/* The psyms_seen set is potentially going to be largish (~40k
27092 elements when indexing a -g3 build of GDB itself). Estimate the
27093 number of elements in order to avoid too many rehashes, which
27094 require rebuilding buckets and thus many trips to
27095 malloc/free. */
27096
27097static size_t
ed2dc618 27098psyms_seen_size (struct dwarf2_per_objfile *dwarf2_per_objfile)
437afbb8 27099{
e8f8bcb3
PA
27100 size_t psyms_count = 0;
27101 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27102 {
27103 struct dwarf2_per_cu_data *per_cu
27104 = dwarf2_per_objfile->all_comp_units[i];
27105 struct partial_symtab *psymtab = per_cu->v.psymtab;
27106
27107 if (psymtab != NULL && psymtab->user == NULL)
27108 recursively_count_psymbols (psymtab, psyms_count);
27109 }
27110 /* Generating an index for gdb itself shows a ratio of
27111 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
437afbb8
JK
27112 return psyms_count / 4;
27113}
27114
27115/* Write new .gdb_index section for OBJFILE into OUT_FILE.
27116 Return how many bytes were expected to be written into OUT_FILE. */
27117
27118static size_t
ed2dc618 27119write_gdbindex (struct dwarf2_per_objfile *dwarf2_per_objfile, FILE *out_file)
437afbb8 27120{
ed2dc618 27121 struct objfile *objfile = dwarf2_per_objfile->objfile;
437afbb8
JK
27122 mapped_symtab symtab;
27123 data_buf cu_list;
27124
27125 /* While we're scanning CU's create a table that maps a psymtab pointer
27126 (which is what addrmap records) to its index (which is what is recorded
27127 in the index file). This will later be needed to write the address
27128 table. */
27129 psym_index_map cu_index_htab;
27130 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
27131
27132 /* The CU list is already sorted, so we don't need to do additional
27133 work here. Also, the debug_types entries do not appear in
27134 all_comp_units, but only in their own hash table. */
27135
ed2dc618
SM
27136 std::unordered_set<partial_symbol *> psyms_seen
27137 (psyms_seen_size (dwarf2_per_objfile));
bc8f2430 27138 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd 27139 {
3e43a32a
MS
27140 struct dwarf2_per_cu_data *per_cu
27141 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 27142 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 27143
92fac807
JK
27144 /* CU of a shared file from 'dwz -m' may be unused by this main file.
27145 It may be referenced from a local scope but in such case it does not
27146 need to be present in .gdb_index. */
27147 if (psymtab == NULL)
27148 continue;
27149
95554aad 27150 if (psymtab->user == NULL)
bc8f2430
JK
27151 recursively_write_psymbols (objfile, psymtab, &symtab,
27152 psyms_seen, i);
9291a0cd 27153
bc8f2430
JK
27154 const auto insertpair = cu_index_htab.emplace (psymtab, i);
27155 gdb_assert (insertpair.second);
9291a0cd 27156
c2f134ac
PA
27157 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
27158 to_underlying (per_cu->sect_off));
27159 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
27160 }
27161
0a5429f6 27162 /* Dump the address map. */
bc8f2430
JK
27163 data_buf addr_vec;
27164 write_address_map (objfile, addr_vec, cu_index_htab);
0a5429f6 27165
1fd400ff 27166 /* Write out the .debug_type entries, if any. */
bc8f2430 27167 data_buf types_cu_list;
1fd400ff
TT
27168 if (dwarf2_per_objfile->signatured_types)
27169 {
bc8f2430
JK
27170 signatured_type_index_data sig_data (types_cu_list,
27171 psyms_seen);
1fd400ff
TT
27172
27173 sig_data.objfile = objfile;
bc8f2430 27174 sig_data.symtab = &symtab;
1fd400ff
TT
27175 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
27176 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
27177 write_one_signatured_type, &sig_data);
27178 }
27179
156942c7
DE
27180 /* Now that we've processed all symbols we can shrink their cu_indices
27181 lists. */
bc8f2430 27182 uniquify_cu_indices (&symtab);
156942c7 27183
bc8f2430
JK
27184 data_buf symtab_vec, constant_pool;
27185 write_hash_table (&symtab, symtab_vec, constant_pool);
9291a0cd 27186
bc8f2430
JK
27187 data_buf contents;
27188 const offset_type size_of_contents = 6 * sizeof (offset_type);
27189 offset_type total_len = size_of_contents;
9291a0cd
TT
27190
27191 /* The version number. */
bc8f2430 27192 contents.append_data (MAYBE_SWAP (8));
9291a0cd
TT
27193
27194 /* The offset of the CU list from the start of the file. */
bc8f2430
JK
27195 contents.append_data (MAYBE_SWAP (total_len));
27196 total_len += cu_list.size ();
9291a0cd 27197
1fd400ff 27198 /* The offset of the types CU list from the start of the file. */
bc8f2430
JK
27199 contents.append_data (MAYBE_SWAP (total_len));
27200 total_len += types_cu_list.size ();
1fd400ff 27201
9291a0cd 27202 /* The offset of the address table from the start of the file. */
bc8f2430
JK
27203 contents.append_data (MAYBE_SWAP (total_len));
27204 total_len += addr_vec.size ();
9291a0cd
TT
27205
27206 /* The offset of the symbol table from the start of the file. */
bc8f2430
JK
27207 contents.append_data (MAYBE_SWAP (total_len));
27208 total_len += symtab_vec.size ();
9291a0cd
TT
27209
27210 /* The offset of the constant pool from the start of the file. */
bc8f2430
JK
27211 contents.append_data (MAYBE_SWAP (total_len));
27212 total_len += constant_pool.size ();
9291a0cd 27213
bc8f2430 27214 gdb_assert (contents.size () == size_of_contents);
9291a0cd 27215
bc8f2430
JK
27216 contents.file_write (out_file);
27217 cu_list.file_write (out_file);
27218 types_cu_list.file_write (out_file);
27219 addr_vec.file_write (out_file);
27220 symtab_vec.file_write (out_file);
27221 constant_pool.file_write (out_file);
9291a0cd 27222
437afbb8
JK
27223 return total_len;
27224}
27225
27226/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
27227static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
27228
27229/* Write a new .debug_names section for OBJFILE into OUT_FILE, write
27230 needed addition to .debug_str section to OUT_FILE_STR. Return how
27231 many bytes were expected to be written into OUT_FILE. */
27232
27233static size_t
ed2dc618
SM
27234write_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
27235 FILE *out_file, FILE *out_file_str)
437afbb8 27236{
ed2dc618
SM
27237 const bool dwarf5_is_dwarf64 = check_dwarf64_offsets (dwarf2_per_objfile);
27238 struct objfile *objfile = dwarf2_per_objfile->objfile;
437afbb8
JK
27239 const enum bfd_endian dwarf5_byte_order
27240 = gdbarch_byte_order (get_objfile_arch (objfile));
27241
27242 /* The CU list is already sorted, so we don't need to do additional
27243 work here. Also, the debug_types entries do not appear in
27244 all_comp_units, but only in their own hash table. */
27245 data_buf cu_list;
ed2dc618
SM
27246 debug_names nametable (dwarf2_per_objfile, dwarf5_is_dwarf64,
27247 dwarf5_byte_order);
27248 std::unordered_set<partial_symbol *>
27249 psyms_seen (psyms_seen_size (dwarf2_per_objfile));
437afbb8
JK
27250 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27251 {
27252 const dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
27253 partial_symtab *psymtab = per_cu->v.psymtab;
27254
27255 /* CU of a shared file from 'dwz -m' may be unused by this main
27256 file. It may be referenced from a local scope but in such
27257 case it does not need to be present in .debug_names. */
27258 if (psymtab == NULL)
27259 continue;
27260
27261 if (psymtab->user == NULL)
27262 nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i);
27263
8af5c486 27264 cu_list.append_uint (nametable.dwarf5_offset_size (), dwarf5_byte_order,
437afbb8
JK
27265 to_underlying (per_cu->sect_off));
27266 }
437afbb8 27267
8af5c486 27268 /* Write out the .debug_type entries, if any. */
437afbb8 27269 data_buf types_cu_list;
8af5c486 27270 if (dwarf2_per_objfile->signatured_types)
437afbb8 27271 {
8af5c486
JK
27272 debug_names::write_one_signatured_type_data sig_data (nametable,
27273 signatured_type_index_data (types_cu_list, psyms_seen));
437afbb8 27274
8af5c486
JK
27275 sig_data.info.objfile = objfile;
27276 /* It is used only for gdb_index. */
27277 sig_data.info.symtab = nullptr;
27278 sig_data.info.cu_index = 0;
27279 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
27280 debug_names::write_one_signatured_type,
27281 &sig_data);
437afbb8
JK
27282 }
27283
8af5c486
JK
27284 nametable.build ();
27285
27286 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
27287
437afbb8
JK
27288 const offset_type bytes_of_header
27289 = ((dwarf5_is_dwarf64 ? 12 : 4)
27290 + 2 + 2 + 7 * 4
27291 + sizeof (dwarf5_gdb_augmentation));
27292 size_t expected_bytes = 0;
27293 expected_bytes += bytes_of_header;
27294 expected_bytes += cu_list.size ();
27295 expected_bytes += types_cu_list.size ();
27296 expected_bytes += nametable.bytes ();
27297 data_buf header;
27298
27299 if (!dwarf5_is_dwarf64)
27300 {
27301 const uint64_t size64 = expected_bytes - 4;
27302 gdb_assert (size64 < 0xfffffff0);
27303 header.append_uint (4, dwarf5_byte_order, size64);
27304 }
27305 else
27306 {
27307 header.append_uint (4, dwarf5_byte_order, 0xffffffff);
27308 header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
27309 }
27310
27311 /* The version number. */
27312 header.append_uint (2, dwarf5_byte_order, 5);
27313
27314 /* Padding. */
27315 header.append_uint (2, dwarf5_byte_order, 0);
27316
27317 /* comp_unit_count - The number of CUs in the CU list. */
27318 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_comp_units);
27319
27320 /* local_type_unit_count - The number of TUs in the local TU
27321 list. */
27322 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_type_units);
27323
27324 /* foreign_type_unit_count - The number of TUs in the foreign TU
27325 list. */
27326 header.append_uint (4, dwarf5_byte_order, 0);
27327
27328 /* bucket_count - The number of hash buckets in the hash lookup
27329 table. */
27330 header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ());
27331
27332 /* name_count - The number of unique names in the index. */
27333 header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
27334
27335 /* abbrev_table_size - The size in bytes of the abbreviations
27336 table. */
27337 header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
27338
27339 /* augmentation_string_size - The size in bytes of the augmentation
27340 string. This value is rounded up to a multiple of 4. */
27341 static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, "");
27342 header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation));
27343 header.append_data (dwarf5_gdb_augmentation);
27344
27345 gdb_assert (header.size () == bytes_of_header);
27346
27347 header.file_write (out_file);
27348 cu_list.file_write (out_file);
27349 types_cu_list.file_write (out_file);
27350 nametable.file_write (out_file, out_file_str);
27351
27352 return expected_bytes;
27353}
27354
27355/* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
27356 position is at the end of the file. */
27357
27358static void
27359assert_file_size (FILE *file, const char *filename, size_t expected_size)
27360{
27361 const auto file_size = ftell (file);
27362 if (file_size == -1)
27363 error (_("Can't get `%s' size"), filename);
27364 gdb_assert (file_size == expected_size);
27365}
27366
437afbb8
JK
27367/* Create an index file for OBJFILE in the directory DIR. */
27368
27369static void
ed2dc618
SM
27370write_psymtabs_to_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
27371 const char *dir,
437afbb8
JK
27372 dw_index_kind index_kind)
27373{
ed2dc618
SM
27374 struct objfile *objfile = dwarf2_per_objfile->objfile;
27375
437afbb8
JK
27376 if (dwarf2_per_objfile->using_index)
27377 error (_("Cannot use an index to create the index"));
27378
27379 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
27380 error (_("Cannot make an index when the file has multiple .debug_types sections"));
27381
27382 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
27383 return;
27384
27385 struct stat st;
27386 if (stat (objfile_name (objfile), &st) < 0)
27387 perror_with_name (objfile_name (objfile));
27388
27389 std::string filename (std::string (dir) + SLASH_STRING
27390 + lbasename (objfile_name (objfile))
27391 + (index_kind == dw_index_kind::DEBUG_NAMES
27392 ? INDEX5_SUFFIX : INDEX4_SUFFIX));
27393
27394 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
27395 if (!out_file)
27396 error (_("Can't open `%s' for writing"), filename.c_str ());
27397
27398 /* Order matters here; we want FILE to be closed before FILENAME is
27399 unlinked, because on MS-Windows one cannot delete a file that is
27400 still open. (Don't call anything here that might throw until
27401 file_closer is created.) */
27402 gdb::unlinker unlink_file (filename.c_str ());
27403 gdb_file_up close_out_file (out_file);
27404
27405 if (index_kind == dw_index_kind::DEBUG_NAMES)
27406 {
27407 std::string filename_str (std::string (dir) + SLASH_STRING
27408 + lbasename (objfile_name (objfile))
27409 + DEBUG_STR_SUFFIX);
27410 FILE *out_file_str
27411 = gdb_fopen_cloexec (filename_str.c_str (), "wb").release ();
27412 if (!out_file_str)
27413 error (_("Can't open `%s' for writing"), filename_str.c_str ());
27414 gdb::unlinker unlink_file_str (filename_str.c_str ());
27415 gdb_file_up close_out_file_str (out_file_str);
27416
27417 const size_t total_len
ed2dc618 27418 = write_debug_names (dwarf2_per_objfile, out_file, out_file_str);
437afbb8
JK
27419 assert_file_size (out_file, filename.c_str (), total_len);
27420
27421 /* We want to keep the file .debug_str file too. */
27422 unlink_file_str.keep ();
27423 }
27424 else
27425 {
27426 const size_t total_len
ed2dc618 27427 = write_gdbindex (dwarf2_per_objfile, out_file);
437afbb8
JK
27428 assert_file_size (out_file, filename.c_str (), total_len);
27429 }
27430
bef155c3
TT
27431 /* We want to keep the file. */
27432 unlink_file.keep ();
9291a0cd
TT
27433}
27434
90476074
TT
27435/* Implementation of the `save gdb-index' command.
27436
437afbb8
JK
27437 Note that the .gdb_index file format used by this command is
27438 documented in the GDB manual. Any changes here must be documented
27439 there. */
11570e71 27440
9291a0cd 27441static void
8384c356 27442save_gdb_index_command (const char *arg, int from_tty)
9291a0cd
TT
27443{
27444 struct objfile *objfile;
437afbb8
JK
27445 const char dwarf5space[] = "-dwarf-5 ";
27446 dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
27447
27448 if (!arg)
27449 arg = "";
27450
27451 arg = skip_spaces (arg);
27452 if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
27453 {
27454 index_kind = dw_index_kind::DEBUG_NAMES;
27455 arg += strlen (dwarf5space);
27456 arg = skip_spaces (arg);
27457 }
9291a0cd 27458
437afbb8
JK
27459 if (!*arg)
27460 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
9291a0cd
TT
27461
27462 ALL_OBJFILES (objfile)
27463 {
27464 struct stat st;
27465
27466 /* If the objfile does not correspond to an actual file, skip it. */
4262abfb 27467 if (stat (objfile_name (objfile), &st) < 0)
9291a0cd
TT
27468 continue;
27469
ed2dc618
SM
27470 struct dwarf2_per_objfile *dwarf2_per_objfile
27471 = get_dwarf2_per_objfile (objfile);
9291a0cd 27472
ed2dc618
SM
27473 if (dwarf2_per_objfile != NULL)
27474 {
492d29ea 27475 TRY
9291a0cd 27476 {
ed2dc618 27477 write_psymtabs_to_index (dwarf2_per_objfile, arg, index_kind);
9291a0cd 27478 }
492d29ea
PA
27479 CATCH (except, RETURN_MASK_ERROR)
27480 {
27481 exception_fprintf (gdb_stderr, except,
27482 _("Error while writing index for `%s': "),
27483 objfile_name (objfile));
27484 }
27485 END_CATCH
9291a0cd 27486 }
ed2dc618 27487
9291a0cd 27488 }
dce234bc
PP
27489}
27490
9291a0cd
TT
27491\f
27492
b4f54984 27493int dwarf_always_disassemble;
9eae7c52
TT
27494
27495static void
b4f54984
DE
27496show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
27497 struct cmd_list_element *c, const char *value)
9eae7c52 27498{
3e43a32a
MS
27499 fprintf_filtered (file,
27500 _("Whether to always disassemble "
27501 "DWARF expressions is %s.\n"),
9eae7c52
TT
27502 value);
27503}
27504
900e11f9
JK
27505static void
27506show_check_physname (struct ui_file *file, int from_tty,
27507 struct cmd_list_element *c, const char *value)
27508{
27509 fprintf_filtered (file,
27510 _("Whether to check \"physname\" is %s.\n"),
27511 value);
27512}
27513
6502dd73
DJ
27514void
27515_initialize_dwarf2_read (void)
27516{
96d19272
JK
27517 struct cmd_list_element *c;
27518
fc8e7e75 27519 dwarf2_objfile_data_key = register_objfile_data ();
ae038cb0 27520
b4f54984
DE
27521 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
27522Set DWARF specific variables.\n\
27523Configure DWARF variables such as the cache size"),
27524 &set_dwarf_cmdlist, "maintenance set dwarf ",
ae038cb0
DJ
27525 0/*allow-unknown*/, &maintenance_set_cmdlist);
27526
b4f54984
DE
27527 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
27528Show DWARF specific variables\n\
27529Show DWARF variables such as the cache size"),
27530 &show_dwarf_cmdlist, "maintenance show dwarf ",
ae038cb0
DJ
27531 0/*allow-unknown*/, &maintenance_show_cmdlist);
27532
27533 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
b4f54984
DE
27534 &dwarf_max_cache_age, _("\
27535Set the upper bound on the age of cached DWARF compilation units."), _("\
27536Show the upper bound on the age of cached DWARF compilation units."), _("\
7915a72c
AC
27537A higher limit means that cached compilation units will be stored\n\
27538in memory longer, and more total memory will be used. Zero disables\n\
27539caching, which can slow down startup."),
2c5b56ce 27540 NULL,
b4f54984
DE
27541 show_dwarf_max_cache_age,
27542 &set_dwarf_cmdlist,
27543 &show_dwarf_cmdlist);
d97bc12b 27544
9eae7c52 27545 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
b4f54984 27546 &dwarf_always_disassemble, _("\
9eae7c52
TT
27547Set whether `info address' always disassembles DWARF expressions."), _("\
27548Show whether `info address' always disassembles DWARF expressions."), _("\
27549When enabled, DWARF expressions are always printed in an assembly-like\n\
27550syntax. When disabled, expressions will be printed in a more\n\
27551conversational style, when possible."),
27552 NULL,
b4f54984
DE
27553 show_dwarf_always_disassemble,
27554 &set_dwarf_cmdlist,
27555 &show_dwarf_cmdlist);
27556
27557 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
27558Set debugging of the DWARF reader."), _("\
27559Show debugging of the DWARF reader."), _("\
27560When enabled (non-zero), debugging messages are printed during DWARF\n\
73be47f5
DE
27561reading and symtab expansion. A value of 1 (one) provides basic\n\
27562information. A value greater than 1 provides more verbose information."),
45cfd468
DE
27563 NULL,
27564 NULL,
27565 &setdebuglist, &showdebuglist);
27566
b4f54984
DE
27567 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
27568Set debugging of the DWARF DIE reader."), _("\
27569Show debugging of the DWARF DIE reader."), _("\
d97bc12b
DE
27570When enabled (non-zero), DIEs are dumped after they are read in.\n\
27571The value is the maximum depth to print."),
ccce17b0
YQ
27572 NULL,
27573 NULL,
27574 &setdebuglist, &showdebuglist);
9291a0cd 27575
27e0867f
DE
27576 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
27577Set debugging of the dwarf line reader."), _("\
27578Show debugging of the dwarf line reader."), _("\
27579When enabled (non-zero), line number entries are dumped as they are read in.\n\
27580A value of 1 (one) provides basic information.\n\
27581A value greater than 1 provides more verbose information."),
27582 NULL,
27583 NULL,
27584 &setdebuglist, &showdebuglist);
27585
900e11f9
JK
27586 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
27587Set cross-checking of \"physname\" code against demangler."), _("\
27588Show cross-checking of \"physname\" code against demangler."), _("\
27589When enabled, GDB's internal \"physname\" code is checked against\n\
27590the demangler."),
27591 NULL, show_check_physname,
27592 &setdebuglist, &showdebuglist);
27593
e615022a
DE
27594 add_setshow_boolean_cmd ("use-deprecated-index-sections",
27595 no_class, &use_deprecated_index_sections, _("\
27596Set whether to use deprecated gdb_index sections."), _("\
27597Show whether to use deprecated gdb_index sections."), _("\
27598When enabled, deprecated .gdb_index sections are used anyway.\n\
27599Normally they are ignored either because of a missing feature or\n\
27600performance issue.\n\
27601Warning: This option must be enabled before gdb reads the file."),
27602 NULL,
27603 NULL,
27604 &setlist, &showlist);
27605
96d19272 27606 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 27607 _("\
fc1a9d6e 27608Save a gdb-index file.\n\
437afbb8
JK
27609Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
27610\n\
27611No options create one file with .gdb-index extension for pre-DWARF-5\n\
27612compatible .gdb_index section. With -dwarf-5 creates two files with\n\
27613extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
96d19272
JK
27614 &save_cmdlist);
27615 set_cmd_completer (c, filename_completer);
f1e6e072
TT
27616
27617 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
27618 &dwarf2_locexpr_funcs);
27619 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
27620 &dwarf2_loclist_funcs);
27621
27622 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
27623 &dwarf2_block_frame_base_locexpr_funcs);
27624 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
27625 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
27626
27627#if GDB_SELF_TEST
27628 selftests::register_test ("dw2_expand_symtabs_matching",
27629 selftests::dw2_expand_symtabs_matching::run_test);
27630#endif
6502dd73 27631}
This page took 5.110859 seconds and 4 git commands to generate.