Remove verbose code from backtrace command
[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
156942c7
DE
165/* Ensure only legit values are used. */
166#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
167 do { \
168 gdb_assert ((unsigned int) (value) <= 1); \
169 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
170 } while (0)
171
172/* Ensure only legit values are used. */
173#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
174 do { \
175 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
176 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
177 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
178 } while (0)
179
180/* Ensure we don't use more than the alloted nuber of bits for the CU. */
181#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
182 do { \
183 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
184 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
185 } while (0)
186
3f563c84
PA
187#if WORDS_BIGENDIAN
188
189/* Convert VALUE between big- and little-endian. */
190
191static offset_type
192byte_swap (offset_type value)
193{
194 offset_type result;
195
196 result = (value & 0xff) << 24;
197 result |= (value & 0xff00) << 8;
198 result |= (value & 0xff0000) >> 8;
199 result |= (value & 0xff000000) >> 24;
200 return result;
201}
202
203#define MAYBE_SWAP(V) byte_swap (V)
204
205#else
206#define MAYBE_SWAP(V) static_cast<offset_type> (V)
207#endif /* WORDS_BIGENDIAN */
208
209/* An index into a (C++) symbol name component in a symbol name as
210 recorded in the mapped_index's symbol table. For each C++ symbol
211 in the symbol table, we record one entry for the start of each
212 component in the symbol in a table of name components, and then
213 sort the table, in order to be able to binary search symbol names,
214 ignoring leading namespaces, both completion and regular look up.
215 For example, for symbol "A::B::C", we'll have an entry that points
216 to "A::B::C", another that points to "B::C", and another for "C".
217 Note that function symbols in GDB index have no parameter
218 information, just the function/method names. You can convert a
219 name_component to a "const char *" using the
220 'mapped_index::symbol_name_at(offset_type)' method. */
221
222struct name_component
223{
224 /* Offset in the symbol name where the component starts. Stored as
225 a (32-bit) offset instead of a pointer to save memory and improve
226 locality on 64-bit architectures. */
227 offset_type name_offset;
228
229 /* The symbol's index in the symbol and constant pool tables of a
230 mapped_index. */
231 offset_type idx;
232};
233
44ed8f3e
PA
234/* Base class containing bits shared by both .gdb_index and
235 .debug_name indexes. */
236
237struct mapped_index_base
238{
239 /* The name_component table (a sorted vector). See name_component's
240 description above. */
241 std::vector<name_component> name_components;
242
243 /* How NAME_COMPONENTS is sorted. */
244 enum case_sensitivity name_components_casing;
245
246 /* Return the number of names in the symbol table. */
247 virtual size_t symbol_name_count () const = 0;
248
249 /* Get the name of the symbol at IDX in the symbol table. */
250 virtual const char *symbol_name_at (offset_type idx) const = 0;
251
252 /* Return whether the name at IDX in the symbol table should be
253 ignored. */
254 virtual bool symbol_name_slot_invalid (offset_type idx) const
255 {
256 return false;
257 }
258
259 /* Build the symbol name component sorted vector, if we haven't
260 yet. */
261 void build_name_components ();
262
263 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
264 possible matches for LN_NO_PARAMS in the name component
265 vector. */
266 std::pair<std::vector<name_component>::const_iterator,
267 std::vector<name_component>::const_iterator>
268 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
269
270 /* Prevent deleting/destroying via a base class pointer. */
271protected:
272 ~mapped_index_base() = default;
273};
274
9291a0cd
TT
275/* A description of the mapped index. The file format is described in
276 a comment by the code that writes the index. */
fc898b42 277struct mapped_index final : public mapped_index_base
9291a0cd 278{
f00a2de2
PA
279 /* A slot/bucket in the symbol table hash. */
280 struct symbol_table_slot
281 {
282 const offset_type name;
283 const offset_type vec;
284 };
285
559a7a62
JK
286 /* Index data format version. */
287 int version;
288
9291a0cd
TT
289 /* The total length of the buffer. */
290 off_t total_size;
b11b1f88 291
f00a2de2
PA
292 /* The address table data. */
293 gdb::array_view<const gdb_byte> address_table;
b11b1f88 294
3876f04e 295 /* The symbol table, implemented as a hash table. */
f00a2de2 296 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 297
9291a0cd
TT
298 /* A pointer to the constant pool. */
299 const char *constant_pool;
3f563c84 300
44ed8f3e
PA
301 bool symbol_name_slot_invalid (offset_type idx) const override
302 {
303 const auto &bucket = this->symbol_table[idx];
304 return bucket.name == 0 && bucket.vec;
305 }
5c58de74 306
3f563c84
PA
307 /* Convenience method to get at the name of the symbol at IDX in the
308 symbol table. */
44ed8f3e 309 const char *symbol_name_at (offset_type idx) const override
f00a2de2 310 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 311
44ed8f3e
PA
312 size_t symbol_name_count () const override
313 { return this->symbol_table.size (); }
9291a0cd
TT
314};
315
927aa2e7
JK
316/* A description of the mapped .debug_names.
317 Uninitialized map has CU_COUNT 0. */
fc898b42 318struct mapped_debug_names final : public mapped_index_base
927aa2e7 319{
ed2dc618
SM
320 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
321 : dwarf2_per_objfile (dwarf2_per_objfile_)
322 {}
323
324 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
325 bfd_endian dwarf5_byte_order;
326 bool dwarf5_is_dwarf64;
327 bool augmentation_is_gdb;
328 uint8_t offset_size;
329 uint32_t cu_count = 0;
330 uint32_t tu_count, bucket_count, name_count;
331 const gdb_byte *cu_table_reordered, *tu_table_reordered;
332 const uint32_t *bucket_table_reordered, *hash_table_reordered;
333 const gdb_byte *name_table_string_offs_reordered;
334 const gdb_byte *name_table_entry_offs_reordered;
335 const gdb_byte *entry_pool;
336
337 struct index_val
338 {
339 ULONGEST dwarf_tag;
340 struct attr
341 {
342 /* Attribute name DW_IDX_*. */
343 ULONGEST dw_idx;
344
345 /* Attribute form DW_FORM_*. */
346 ULONGEST form;
347
348 /* Value if FORM is DW_FORM_implicit_const. */
349 LONGEST implicit_const;
350 };
351 std::vector<attr> attr_vec;
352 };
353
354 std::unordered_map<ULONGEST, index_val> abbrev_map;
355
356 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
357
358 /* Implementation of the mapped_index_base virtual interface, for
359 the name_components cache. */
360
361 const char *symbol_name_at (offset_type idx) const override
362 { return namei_to_name (idx); }
363
364 size_t symbol_name_count () const override
365 { return this->name_count; }
927aa2e7
JK
366};
367
95554aad
TT
368typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
369DEF_VEC_P (dwarf2_per_cu_ptr);
370
52059ffd
TT
371struct tu_stats
372{
373 int nr_uniq_abbrev_tables;
374 int nr_symtabs;
375 int nr_symtab_sharers;
376 int nr_stmt_less_type_units;
377 int nr_all_type_units_reallocs;
378};
379
9cdd5dbd
DE
380/* Collection of data recorded per objfile.
381 This hangs off of dwarf2_objfile_data_key. */
382
fd90ace4 383struct dwarf2_per_objfile : public allocate_on_obstack
6502dd73 384{
330cdd98
PA
385 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
386 dwarf2 section names, or is NULL if the standard ELF names are
387 used. */
388 dwarf2_per_objfile (struct objfile *objfile,
389 const dwarf2_debug_sections *names);
ae038cb0 390
330cdd98
PA
391 ~dwarf2_per_objfile ();
392
d6541620 393 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
330cdd98
PA
394
395 /* Free all cached compilation units. */
396 void free_cached_comp_units ();
397private:
398 /* This function is mapped across the sections and remembers the
399 offset and size of each of the debugging sections we are
400 interested in. */
401 void locate_sections (bfd *abfd, asection *sectp,
402 const dwarf2_debug_sections &names);
403
404public:
405 dwarf2_section_info info {};
406 dwarf2_section_info abbrev {};
407 dwarf2_section_info line {};
408 dwarf2_section_info loc {};
409 dwarf2_section_info loclists {};
410 dwarf2_section_info macinfo {};
411 dwarf2_section_info macro {};
412 dwarf2_section_info str {};
413 dwarf2_section_info line_str {};
414 dwarf2_section_info ranges {};
415 dwarf2_section_info rnglists {};
416 dwarf2_section_info addr {};
417 dwarf2_section_info frame {};
418 dwarf2_section_info eh_frame {};
419 dwarf2_section_info gdb_index {};
927aa2e7
JK
420 dwarf2_section_info debug_names {};
421 dwarf2_section_info debug_aranges {};
330cdd98
PA
422
423 VEC (dwarf2_section_info_def) *types = NULL;
8b70b953 424
be391dca 425 /* Back link. */
330cdd98 426 struct objfile *objfile = NULL;
be391dca 427
d467dd73 428 /* Table of all the compilation units. This is used to locate
10b3939b 429 the target compilation unit of a particular reference. */
330cdd98 430 struct dwarf2_per_cu_data **all_comp_units = NULL;
ae038cb0
DJ
431
432 /* The number of compilation units in ALL_COMP_UNITS. */
330cdd98 433 int n_comp_units = 0;
ae038cb0 434
1fd400ff 435 /* The number of .debug_types-related CUs. */
330cdd98 436 int n_type_units = 0;
1fd400ff 437
6aa5f3a6
DE
438 /* The number of elements allocated in all_type_units.
439 If there are skeleton-less TUs, we add them to all_type_units lazily. */
330cdd98 440 int n_allocated_type_units = 0;
6aa5f3a6 441
a2ce51a0
DE
442 /* The .debug_types-related CUs (TUs).
443 This is stored in malloc space because we may realloc it. */
330cdd98 444 struct signatured_type **all_type_units = NULL;
1fd400ff 445
f4dc4d17
DE
446 /* Table of struct type_unit_group objects.
447 The hash key is the DW_AT_stmt_list value. */
330cdd98 448 htab_t type_unit_groups {};
72dca2f5 449
348e048f
DE
450 /* A table mapping .debug_types signatures to its signatured_type entry.
451 This is NULL if the .debug_types section hasn't been read in yet. */
330cdd98 452 htab_t signatured_types {};
348e048f 453
f4dc4d17
DE
454 /* Type unit statistics, to see how well the scaling improvements
455 are doing. */
330cdd98 456 struct tu_stats tu_stats {};
f4dc4d17
DE
457
458 /* A chain of compilation units that are currently read in, so that
459 they can be freed later. */
330cdd98 460 dwarf2_per_cu_data *read_in_chain = NULL;
f4dc4d17 461
3019eac3
DE
462 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
463 This is NULL if the table hasn't been allocated yet. */
330cdd98 464 htab_t dwo_files {};
3019eac3 465
330cdd98
PA
466 /* True if we've checked for whether there is a DWP file. */
467 bool dwp_checked = false;
80626a55
DE
468
469 /* The DWP file if there is one, or NULL. */
330cdd98 470 struct dwp_file *dwp_file = NULL;
80626a55 471
36586728
TT
472 /* The shared '.dwz' file, if one exists. This is used when the
473 original data was compressed using 'dwz -m'. */
330cdd98 474 struct dwz_file *dwz_file = NULL;
36586728 475
330cdd98 476 /* A flag indicating whether this objfile has a section loaded at a
72dca2f5 477 VMA of 0. */
330cdd98 478 bool has_section_at_zero = false;
9291a0cd 479
ae2de4f8
DE
480 /* True if we are using the mapped index,
481 or we are faking it for OBJF_READNOW's sake. */
330cdd98 482 bool using_index = false;
9291a0cd 483
ae2de4f8 484 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
330cdd98 485 mapped_index *index_table = NULL;
98bfdba5 486
927aa2e7
JK
487 /* The mapped index, or NULL if .debug_names is missing or not being used. */
488 std::unique_ptr<mapped_debug_names> debug_names_table;
489
7b9f3c50 490 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
491 TUs typically share line table entries with a CU, so we maintain a
492 separate table of all line table entries to support the sharing.
493 Note that while there can be way more TUs than CUs, we've already
494 sorted all the TUs into "type unit groups", grouped by their
495 DW_AT_stmt_list value. Therefore the only sharing done here is with a
496 CU and its associated TU group if there is one. */
330cdd98 497 htab_t quick_file_names_table {};
7b9f3c50 498
98bfdba5
PA
499 /* Set during partial symbol reading, to prevent queueing of full
500 symbols. */
330cdd98 501 bool reading_partial_symbols = false;
673bfd45 502
dee91e82 503 /* Table mapping type DIEs to their struct type *.
673bfd45 504 This is NULL if not allocated yet.
02142a6c 505 The mapping is done via (CU/TU + DIE offset) -> type. */
330cdd98 506 htab_t die_type_hash {};
95554aad
TT
507
508 /* The CUs we recently read. */
330cdd98 509 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
527f3840
JK
510
511 /* Table containing line_header indexed by offset and offset_in_dwz. */
330cdd98 512 htab_t line_header_hash {};
bbf2f4df
PA
513
514 /* Table containing all filenames. This is an optional because the
515 table is lazily constructed on first access. */
516 gdb::optional<filename_seen_cache> filenames_cache;
6502dd73
DJ
517};
518
ed2dc618
SM
519/* Get the dwarf2_per_objfile associated to OBJFILE. */
520
521struct dwarf2_per_objfile *
522get_dwarf2_per_objfile (struct objfile *objfile)
523{
524 return ((struct dwarf2_per_objfile *)
525 objfile_data (objfile, dwarf2_objfile_data_key));
526}
527
528/* Set the dwarf2_per_objfile associated to OBJFILE. */
529
530void
531set_dwarf2_per_objfile (struct objfile *objfile,
532 struct dwarf2_per_objfile *dwarf2_per_objfile)
533{
534 gdb_assert (get_dwarf2_per_objfile (objfile) == NULL);
535 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
536}
c906108c 537
251d32d9 538/* Default names of the debugging sections. */
c906108c 539
233a11ab
CS
540/* Note that if the debugging section has been compressed, it might
541 have a name like .zdebug_info. */
542
9cdd5dbd
DE
543static const struct dwarf2_debug_sections dwarf2_elf_names =
544{
251d32d9
TG
545 { ".debug_info", ".zdebug_info" },
546 { ".debug_abbrev", ".zdebug_abbrev" },
547 { ".debug_line", ".zdebug_line" },
548 { ".debug_loc", ".zdebug_loc" },
43988095 549 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 550 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 551 { ".debug_macro", ".zdebug_macro" },
251d32d9 552 { ".debug_str", ".zdebug_str" },
43988095 553 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 554 { ".debug_ranges", ".zdebug_ranges" },
43988095 555 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 556 { ".debug_types", ".zdebug_types" },
3019eac3 557 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
558 { ".debug_frame", ".zdebug_frame" },
559 { ".eh_frame", NULL },
24d3216f 560 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
561 { ".debug_names", ".zdebug_names" },
562 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 563 23
251d32d9 564};
c906108c 565
80626a55 566/* List of DWO/DWP sections. */
3019eac3 567
80626a55 568static const struct dwop_section_names
3019eac3
DE
569{
570 struct dwarf2_section_names abbrev_dwo;
571 struct dwarf2_section_names info_dwo;
572 struct dwarf2_section_names line_dwo;
573 struct dwarf2_section_names loc_dwo;
43988095 574 struct dwarf2_section_names loclists_dwo;
09262596
DE
575 struct dwarf2_section_names macinfo_dwo;
576 struct dwarf2_section_names macro_dwo;
3019eac3
DE
577 struct dwarf2_section_names str_dwo;
578 struct dwarf2_section_names str_offsets_dwo;
579 struct dwarf2_section_names types_dwo;
80626a55
DE
580 struct dwarf2_section_names cu_index;
581 struct dwarf2_section_names tu_index;
3019eac3 582}
80626a55 583dwop_section_names =
3019eac3
DE
584{
585 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
586 { ".debug_info.dwo", ".zdebug_info.dwo" },
587 { ".debug_line.dwo", ".zdebug_line.dwo" },
588 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 589 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
590 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
591 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
592 { ".debug_str.dwo", ".zdebug_str.dwo" },
593 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
594 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
595 { ".debug_cu_index", ".zdebug_cu_index" },
596 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
597};
598
c906108c
SS
599/* local data types */
600
107d2387
AC
601/* The data in a compilation unit header, after target2host
602 translation, looks like this. */
c906108c 603struct comp_unit_head
a738430d 604{
c764a876 605 unsigned int length;
a738430d 606 short version;
a738430d
MK
607 unsigned char addr_size;
608 unsigned char signed_addr_p;
9c541725 609 sect_offset abbrev_sect_off;
57349743 610
a738430d
MK
611 /* Size of file offsets; either 4 or 8. */
612 unsigned int offset_size;
57349743 613
a738430d
MK
614 /* Size of the length field; either 4 or 12. */
615 unsigned int initial_length_size;
57349743 616
43988095
JK
617 enum dwarf_unit_type unit_type;
618
a738430d
MK
619 /* Offset to the first byte of this compilation unit header in the
620 .debug_info section, for resolving relative reference dies. */
9c541725 621 sect_offset sect_off;
57349743 622
d00adf39
DE
623 /* Offset to first die in this cu from the start of the cu.
624 This will be the first byte following the compilation unit header. */
9c541725 625 cu_offset first_die_cu_offset;
43988095
JK
626
627 /* 64-bit signature of this type unit - it is valid only for
628 UNIT_TYPE DW_UT_type. */
629 ULONGEST signature;
630
631 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 632 cu_offset type_cu_offset_in_tu;
a738430d 633};
c906108c 634
3da10d80
KS
635/* Type used for delaying computation of method physnames.
636 See comments for compute_delayed_physnames. */
637struct delayed_method_info
638{
639 /* The type to which the method is attached, i.e., its parent class. */
640 struct type *type;
641
642 /* The index of the method in the type's function fieldlists. */
643 int fnfield_index;
644
645 /* The index of the method in the fieldlist. */
646 int index;
647
648 /* The name of the DIE. */
649 const char *name;
650
651 /* The DIE associated with this method. */
652 struct die_info *die;
653};
654
e7c27a73
DJ
655/* Internal state when decoding a particular compilation unit. */
656struct dwarf2_cu
657{
fcd3b13d
SM
658 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
659 ~dwarf2_cu ();
660
661 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
662
d00adf39 663 /* The header of the compilation unit. */
fcd3b13d 664 struct comp_unit_head header {};
e142c38c 665
d00adf39 666 /* Base address of this compilation unit. */
fcd3b13d 667 CORE_ADDR base_address = 0;
d00adf39
DE
668
669 /* Non-zero if base_address has been set. */
fcd3b13d 670 int base_known = 0;
d00adf39 671
e142c38c 672 /* The language we are debugging. */
fcd3b13d
SM
673 enum language language = language_unknown;
674 const struct language_defn *language_defn = nullptr;
e142c38c 675
fcd3b13d 676 const char *producer = nullptr;
b0f35d58 677
e142c38c
DJ
678 /* The generic symbol table building routines have separate lists for
679 file scope symbols and all all other scopes (local scopes). So
680 we need to select the right one to pass to add_symbol_to_list().
681 We do it by keeping a pointer to the correct list in list_in_scope.
682
683 FIXME: The original dwarf code just treated the file scope as the
684 first local scope, and all other local scopes as nested local
685 scopes, and worked fine. Check to see if we really need to
686 distinguish these in buildsym.c. */
fcd3b13d 687 struct pending **list_in_scope = nullptr;
e142c38c 688
b64f50a1
JK
689 /* Hash table holding all the loaded partial DIEs
690 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 691 htab_t partial_dies = nullptr;
72bf9492
DJ
692
693 /* Storage for things with the same lifetime as this read-in compilation
694 unit, including partial DIEs. */
fcd3b13d 695 auto_obstack comp_unit_obstack;
72bf9492 696
ae038cb0
DJ
697 /* When multiple dwarf2_cu structures are living in memory, this field
698 chains them all together, so that they can be released efficiently.
699 We will probably also want a generation counter so that most-recently-used
700 compilation units are cached... */
fcd3b13d 701 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 702
69d751e3 703 /* Backlink to our per_cu entry. */
ae038cb0
DJ
704 struct dwarf2_per_cu_data *per_cu;
705
706 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 707 int last_used = 0;
ae038cb0 708
b64f50a1
JK
709 /* A hash table of DIE cu_offset for following references with
710 die_info->offset.sect_off as hash. */
fcd3b13d 711 htab_t die_hash = nullptr;
10b3939b
DJ
712
713 /* Full DIEs if read in. */
fcd3b13d 714 struct die_info *dies = nullptr;
10b3939b
DJ
715
716 /* A set of pointers to dwarf2_per_cu_data objects for compilation
717 units referenced by this one. Only set during full symbol processing;
718 partial symbol tables do not have dependencies. */
fcd3b13d 719 htab_t dependencies = nullptr;
10b3939b 720
cb1df416 721 /* Header data from the line table, during full symbol processing. */
fcd3b13d 722 struct line_header *line_header = nullptr;
4c8aa72d
PA
723 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
724 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
725 this is the DW_TAG_compile_unit die for this CU. We'll hold on
726 to the line header as long as this DIE is being processed. See
727 process_die_scope. */
fcd3b13d 728 die_info *line_header_die_owner = nullptr;
cb1df416 729
3da10d80
KS
730 /* A list of methods which need to have physnames computed
731 after all type information has been read. */
c89b44cd 732 std::vector<delayed_method_info> method_list;
3da10d80 733
96408a79 734 /* To be copied to symtab->call_site_htab. */
fcd3b13d 735 htab_t call_site_htab = nullptr;
96408a79 736
034e5797
DE
737 /* Non-NULL if this CU came from a DWO file.
738 There is an invariant here that is important to remember:
739 Except for attributes copied from the top level DIE in the "main"
740 (or "stub") file in preparation for reading the DWO file
741 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
742 Either there isn't a DWO file (in which case this is NULL and the point
743 is moot), or there is and either we're not going to read it (in which
744 case this is NULL) or there is and we are reading it (in which case this
745 is non-NULL). */
fcd3b13d 746 struct dwo_unit *dwo_unit = nullptr;
3019eac3
DE
747
748 /* The DW_AT_addr_base attribute if present, zero otherwise
749 (zero is a valid value though).
1dbab08b 750 Note this value comes from the Fission stub CU/TU's DIE. */
fcd3b13d 751 ULONGEST addr_base = 0;
3019eac3 752
2e3cf129
DE
753 /* The DW_AT_ranges_base attribute if present, zero otherwise
754 (zero is a valid value though).
1dbab08b 755 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 756 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
757 be used without needing to know whether DWO files are in use or not.
758 N.B. This does not apply to DW_AT_ranges appearing in
759 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
760 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
761 DW_AT_ranges_base *would* have to be applied, and we'd have to care
762 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 763 ULONGEST ranges_base = 0;
2e3cf129 764
c9317f21
TT
765 /* When reading debug info generated by older versions of rustc, we
766 have to rewrite some union types to be struct types with a
767 variant part. This rewriting must be done after the CU is fully
768 read in, because otherwise at the point of rewriting some struct
769 type might not have been fully processed. So, we keep a list of
770 all such types here and process them after expansion. */
771 std::vector<struct type *> rust_unions;
772
ae038cb0
DJ
773 /* Mark used when releasing cached dies. */
774 unsigned int mark : 1;
775
8be455d7
JK
776 /* This CU references .debug_loc. See the symtab->locations_valid field.
777 This test is imperfect as there may exist optimized debug code not using
778 any location list and still facing inlining issues if handled as
779 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 780 unsigned int has_loclist : 1;
ba919b58 781
1b80a9fa
JK
782 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
783 if all the producer_is_* fields are valid. This information is cached
784 because profiling CU expansion showed excessive time spent in
785 producer_is_gxx_lt_4_6. */
ba919b58
TT
786 unsigned int checked_producer : 1;
787 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 788 unsigned int producer_is_gcc_lt_4_3 : 1;
5230b05a 789 unsigned int producer_is_icc_lt_14 : 1;
4d4ec4e5
TT
790
791 /* When set, the file that we're processing is known to have
792 debugging info for C++ namespaces. GCC 3.3.x did not produce
793 this information, but later versions do. */
794
795 unsigned int processing_has_namespace_info : 1;
d590ff25
YQ
796
797 struct partial_die_info *find_partial_die (sect_offset sect_off);
e7c27a73
DJ
798};
799
10b3939b
DJ
800/* Persistent data held for a compilation unit, even when not
801 processing it. We put a pointer to this structure in the
28dee7f5 802 read_symtab_private field of the psymtab. */
10b3939b 803
ae038cb0
DJ
804struct dwarf2_per_cu_data
805{
36586728 806 /* The start offset and length of this compilation unit.
45452591 807 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
808 initial_length_size.
809 If the DIE refers to a DWO file, this is always of the original die,
810 not the DWO file. */
9c541725 811 sect_offset sect_off;
36586728 812 unsigned int length;
ae038cb0 813
43988095
JK
814 /* DWARF standard version this data has been read from (such as 4 or 5). */
815 short dwarf_version;
816
ae038cb0
DJ
817 /* Flag indicating this compilation unit will be read in before
818 any of the current compilation units are processed. */
c764a876 819 unsigned int queued : 1;
ae038cb0 820
0d99eb77
DE
821 /* This flag will be set when reading partial DIEs if we need to load
822 absolutely all DIEs for this compilation unit, instead of just the ones
823 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
824 hash table and don't find it. */
825 unsigned int load_all_dies : 1;
826
0186c6a7
DE
827 /* Non-zero if this CU is from .debug_types.
828 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
829 this is non-zero. */
3019eac3
DE
830 unsigned int is_debug_types : 1;
831
36586728
TT
832 /* Non-zero if this CU is from the .dwz file. */
833 unsigned int is_dwz : 1;
834
a2ce51a0
DE
835 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
836 This flag is only valid if is_debug_types is true.
837 We can't read a CU directly from a DWO file: There are required
838 attributes in the stub. */
839 unsigned int reading_dwo_directly : 1;
840
7ee85ab1
DE
841 /* Non-zero if the TU has been read.
842 This is used to assist the "Stay in DWO Optimization" for Fission:
843 When reading a DWO, it's faster to read TUs from the DWO instead of
844 fetching them from random other DWOs (due to comdat folding).
845 If the TU has already been read, the optimization is unnecessary
846 (and unwise - we don't want to change where gdb thinks the TU lives
847 "midflight").
848 This flag is only valid if is_debug_types is true. */
849 unsigned int tu_read : 1;
850
3019eac3
DE
851 /* The section this CU/TU lives in.
852 If the DIE refers to a DWO file, this is always the original die,
853 not the DWO file. */
8a0459fd 854 struct dwarf2_section_info *section;
348e048f 855
17ea53c3 856 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
cc12ce38
DE
857 of the CU cache it gets reset to NULL again. This is left as NULL for
858 dummy CUs (a CU header, but nothing else). */
ae038cb0 859 struct dwarf2_cu *cu;
1c379e20 860
e3b94546
SM
861 /* The corresponding dwarf2_per_objfile. */
862 struct dwarf2_per_objfile *dwarf2_per_objfile;
9291a0cd 863
fffbe6a8
YQ
864 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
865 is active. Otherwise, the 'psymtab' field is active. */
9291a0cd
TT
866 union
867 {
868 /* The partial symbol table associated with this compilation unit,
95554aad 869 or NULL for unread partial units. */
9291a0cd
TT
870 struct partial_symtab *psymtab;
871
872 /* Data needed by the "quick" functions. */
873 struct dwarf2_per_cu_quick_data *quick;
874 } v;
95554aad 875
796a7ff8
DE
876 /* The CUs we import using DW_TAG_imported_unit. This is filled in
877 while reading psymtabs, used to compute the psymtab dependencies,
878 and then cleared. Then it is filled in again while reading full
879 symbols, and only deleted when the objfile is destroyed.
880
881 This is also used to work around a difference between the way gold
882 generates .gdb_index version <=7 and the way gdb does. Arguably this
883 is a gold bug. For symbols coming from TUs, gold records in the index
884 the CU that includes the TU instead of the TU itself. This breaks
885 dw2_lookup_symbol: It assumes that if the index says symbol X lives
886 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
887 will find X. Alas TUs live in their own symtab, so after expanding CU Y
888 we need to look in TU Z to find X. Fortunately, this is akin to
889 DW_TAG_imported_unit, so we just use the same mechanism: For
890 .gdb_index version <=7 this also records the TUs that the CU referred
891 to. Concurrently with this change gdb was modified to emit version 8
69d751e3
DE
892 indices so we only pay a price for gold generated indices.
893 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
796a7ff8 894 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
895};
896
348e048f
DE
897/* Entry in the signatured_types hash table. */
898
899struct signatured_type
900{
42e7ad6c 901 /* The "per_cu" object of this type.
ac9ec31b 902 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
903 N.B.: This is the first member so that it's easy to convert pointers
904 between them. */
905 struct dwarf2_per_cu_data per_cu;
906
3019eac3 907 /* The type's signature. */
348e048f
DE
908 ULONGEST signature;
909
3019eac3 910 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
911 If this TU is a DWO stub and the definition lives in a DWO file
912 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
913 cu_offset type_offset_in_tu;
914
915 /* Offset in the section of the type's DIE.
916 If the definition lives in a DWO file, this is the offset in the
917 .debug_types.dwo section.
918 The value is zero until the actual value is known.
919 Zero is otherwise not a valid section offset. */
920 sect_offset type_offset_in_section;
0186c6a7
DE
921
922 /* Type units are grouped by their DW_AT_stmt_list entry so that they
923 can share them. This points to the containing symtab. */
924 struct type_unit_group *type_unit_group;
ac9ec31b
DE
925
926 /* The type.
927 The first time we encounter this type we fully read it in and install it
928 in the symbol tables. Subsequent times we only need the type. */
929 struct type *type;
a2ce51a0
DE
930
931 /* Containing DWO unit.
932 This field is valid iff per_cu.reading_dwo_directly. */
933 struct dwo_unit *dwo_unit;
348e048f
DE
934};
935
0186c6a7
DE
936typedef struct signatured_type *sig_type_ptr;
937DEF_VEC_P (sig_type_ptr);
938
094b34ac
DE
939/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
940 This includes type_unit_group and quick_file_names. */
941
942struct stmt_list_hash
943{
944 /* The DWO unit this table is from or NULL if there is none. */
945 struct dwo_unit *dwo_unit;
946
947 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 948 sect_offset line_sect_off;
094b34ac
DE
949};
950
f4dc4d17
DE
951/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
952 an object of this type. */
953
954struct type_unit_group
955{
0186c6a7 956 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
957 To simplify things we create an artificial CU that "includes" all the
958 type units using this stmt_list so that the rest of the code still has
959 a "per_cu" handle on the symtab.
960 This PER_CU is recognized by having no section. */
8a0459fd 961#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
962 struct dwarf2_per_cu_data per_cu;
963
0186c6a7
DE
964 /* The TUs that share this DW_AT_stmt_list entry.
965 This is added to while parsing type units to build partial symtabs,
966 and is deleted afterwards and not used again. */
967 VEC (sig_type_ptr) *tus;
f4dc4d17 968
43f3e411 969 /* The compunit symtab.
094b34ac 970 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
971 so we create an essentially anonymous symtab as the compunit symtab. */
972 struct compunit_symtab *compunit_symtab;
f4dc4d17 973
094b34ac
DE
974 /* The data used to construct the hash key. */
975 struct stmt_list_hash hash;
f4dc4d17
DE
976
977 /* The number of symtabs from the line header.
978 The value here must match line_header.num_file_names. */
979 unsigned int num_symtabs;
980
981 /* The symbol tables for this TU (obtained from the files listed in
982 DW_AT_stmt_list).
983 WARNING: The order of entries here must match the order of entries
984 in the line header. After the first TU using this type_unit_group, the
985 line header for the subsequent TUs is recreated from this. This is done
986 because we need to use the same symtabs for each TU using the same
987 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
988 there's no guarantee the line header doesn't have duplicate entries. */
989 struct symtab **symtabs;
990};
991
73869dc2 992/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
993
994struct dwo_sections
995{
996 struct dwarf2_section_info abbrev;
3019eac3
DE
997 struct dwarf2_section_info line;
998 struct dwarf2_section_info loc;
43988095 999 struct dwarf2_section_info loclists;
09262596
DE
1000 struct dwarf2_section_info macinfo;
1001 struct dwarf2_section_info macro;
3019eac3
DE
1002 struct dwarf2_section_info str;
1003 struct dwarf2_section_info str_offsets;
80626a55
DE
1004 /* In the case of a virtual DWO file, these two are unused. */
1005 struct dwarf2_section_info info;
3019eac3
DE
1006 VEC (dwarf2_section_info_def) *types;
1007};
1008
c88ee1f0 1009/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
1010
1011struct dwo_unit
1012{
1013 /* Backlink to the containing struct dwo_file. */
1014 struct dwo_file *dwo_file;
1015
1016 /* The "id" that distinguishes this CU/TU.
1017 .debug_info calls this "dwo_id", .debug_types calls this "signature".
1018 Since signatures came first, we stick with it for consistency. */
1019 ULONGEST signature;
1020
1021 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 1022 struct dwarf2_section_info *section;
3019eac3 1023
9c541725
PA
1024 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
1025 sect_offset sect_off;
3019eac3
DE
1026 unsigned int length;
1027
1028 /* For types, offset in the type's DIE of the type defined by this TU. */
1029 cu_offset type_offset_in_tu;
1030};
1031
73869dc2
DE
1032/* include/dwarf2.h defines the DWP section codes.
1033 It defines a max value but it doesn't define a min value, which we
1034 use for error checking, so provide one. */
1035
1036enum dwp_v2_section_ids
1037{
1038 DW_SECT_MIN = 1
1039};
1040
80626a55 1041/* Data for one DWO file.
57d63ce2
DE
1042
1043 This includes virtual DWO files (a virtual DWO file is a DWO file as it
1044 appears in a DWP file). DWP files don't really have DWO files per se -
1045 comdat folding of types "loses" the DWO file they came from, and from
1046 a high level view DWP files appear to contain a mass of random types.
1047 However, to maintain consistency with the non-DWP case we pretend DWP
1048 files contain virtual DWO files, and we assign each TU with one virtual
1049 DWO file (generally based on the line and abbrev section offsets -
1050 a heuristic that seems to work in practice). */
3019eac3
DE
1051
1052struct dwo_file
1053{
0ac5b59e 1054 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
1055 For virtual DWO files the name is constructed from the section offsets
1056 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
1057 from related CU+TUs. */
0ac5b59e
DE
1058 const char *dwo_name;
1059
1060 /* The DW_AT_comp_dir attribute. */
1061 const char *comp_dir;
3019eac3 1062
80626a55
DE
1063 /* The bfd, when the file is open. Otherwise this is NULL.
1064 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
1065 bfd *dbfd;
3019eac3 1066
73869dc2
DE
1067 /* The sections that make up this DWO file.
1068 Remember that for virtual DWO files in DWP V2, these are virtual
1069 sections (for lack of a better name). */
3019eac3
DE
1070 struct dwo_sections sections;
1071
33c5cd75
DB
1072 /* The CUs in the file.
1073 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
1074 an extension to handle LLVM's Link Time Optimization output (where
1075 multiple source files may be compiled into a single object/dwo pair). */
1076 htab_t cus;
3019eac3
DE
1077
1078 /* Table of TUs in the file.
1079 Each element is a struct dwo_unit. */
1080 htab_t tus;
1081};
1082
80626a55
DE
1083/* These sections are what may appear in a DWP file. */
1084
1085struct dwp_sections
1086{
73869dc2 1087 /* These are used by both DWP version 1 and 2. */
80626a55
DE
1088 struct dwarf2_section_info str;
1089 struct dwarf2_section_info cu_index;
1090 struct dwarf2_section_info tu_index;
73869dc2
DE
1091
1092 /* These are only used by DWP version 2 files.
1093 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
1094 sections are referenced by section number, and are not recorded here.
1095 In DWP version 2 there is at most one copy of all these sections, each
1096 section being (effectively) comprised of the concatenation of all of the
1097 individual sections that exist in the version 1 format.
1098 To keep the code simple we treat each of these concatenated pieces as a
1099 section itself (a virtual section?). */
1100 struct dwarf2_section_info abbrev;
1101 struct dwarf2_section_info info;
1102 struct dwarf2_section_info line;
1103 struct dwarf2_section_info loc;
1104 struct dwarf2_section_info macinfo;
1105 struct dwarf2_section_info macro;
1106 struct dwarf2_section_info str_offsets;
1107 struct dwarf2_section_info types;
80626a55
DE
1108};
1109
73869dc2
DE
1110/* These sections are what may appear in a virtual DWO file in DWP version 1.
1111 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 1112
73869dc2 1113struct virtual_v1_dwo_sections
80626a55
DE
1114{
1115 struct dwarf2_section_info abbrev;
1116 struct dwarf2_section_info line;
1117 struct dwarf2_section_info loc;
1118 struct dwarf2_section_info macinfo;
1119 struct dwarf2_section_info macro;
1120 struct dwarf2_section_info str_offsets;
1121 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 1122 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
1123 struct dwarf2_section_info info_or_types;
1124};
1125
73869dc2
DE
1126/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
1127 In version 2, the sections of the DWO files are concatenated together
1128 and stored in one section of that name. Thus each ELF section contains
1129 several "virtual" sections. */
1130
1131struct virtual_v2_dwo_sections
1132{
1133 bfd_size_type abbrev_offset;
1134 bfd_size_type abbrev_size;
1135
1136 bfd_size_type line_offset;
1137 bfd_size_type line_size;
1138
1139 bfd_size_type loc_offset;
1140 bfd_size_type loc_size;
1141
1142 bfd_size_type macinfo_offset;
1143 bfd_size_type macinfo_size;
1144
1145 bfd_size_type macro_offset;
1146 bfd_size_type macro_size;
1147
1148 bfd_size_type str_offsets_offset;
1149 bfd_size_type str_offsets_size;
1150
1151 /* Each DWP hash table entry records one CU or one TU.
1152 That is recorded here, and copied to dwo_unit.section. */
1153 bfd_size_type info_or_types_offset;
1154 bfd_size_type info_or_types_size;
1155};
1156
80626a55
DE
1157/* Contents of DWP hash tables. */
1158
1159struct dwp_hash_table
1160{
73869dc2 1161 uint32_t version, nr_columns;
80626a55 1162 uint32_t nr_units, nr_slots;
73869dc2
DE
1163 const gdb_byte *hash_table, *unit_table;
1164 union
1165 {
1166 struct
1167 {
1168 const gdb_byte *indices;
1169 } v1;
1170 struct
1171 {
1172 /* This is indexed by column number and gives the id of the section
1173 in that column. */
1174#define MAX_NR_V2_DWO_SECTIONS \
1175 (1 /* .debug_info or .debug_types */ \
1176 + 1 /* .debug_abbrev */ \
1177 + 1 /* .debug_line */ \
1178 + 1 /* .debug_loc */ \
1179 + 1 /* .debug_str_offsets */ \
1180 + 1 /* .debug_macro or .debug_macinfo */)
1181 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1182 const gdb_byte *offsets;
1183 const gdb_byte *sizes;
1184 } v2;
1185 } section_pool;
80626a55
DE
1186};
1187
1188/* Data for one DWP file. */
1189
1190struct dwp_file
1191{
1192 /* Name of the file. */
1193 const char *name;
1194
73869dc2
DE
1195 /* File format version. */
1196 int version;
1197
93417882 1198 /* The bfd. */
80626a55
DE
1199 bfd *dbfd;
1200
1201 /* Section info for this file. */
1202 struct dwp_sections sections;
1203
57d63ce2 1204 /* Table of CUs in the file. */
80626a55
DE
1205 const struct dwp_hash_table *cus;
1206
1207 /* Table of TUs in the file. */
1208 const struct dwp_hash_table *tus;
1209
19ac8c2e
DE
1210 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1211 htab_t loaded_cus;
1212 htab_t loaded_tus;
80626a55 1213
73869dc2
DE
1214 /* Table to map ELF section numbers to their sections.
1215 This is only needed for the DWP V1 file format. */
80626a55
DE
1216 unsigned int num_sections;
1217 asection **elf_sections;
1218};
1219
36586728
TT
1220/* This represents a '.dwz' file. */
1221
1222struct dwz_file
1223{
1224 /* A dwz file can only contain a few sections. */
1225 struct dwarf2_section_info abbrev;
1226 struct dwarf2_section_info info;
1227 struct dwarf2_section_info str;
1228 struct dwarf2_section_info line;
1229 struct dwarf2_section_info macro;
2ec9a5e0 1230 struct dwarf2_section_info gdb_index;
927aa2e7 1231 struct dwarf2_section_info debug_names;
36586728
TT
1232
1233 /* The dwz's BFD. */
1234 bfd *dwz_bfd;
1235};
1236
0963b4bd
MS
1237/* Struct used to pass misc. parameters to read_die_and_children, et
1238 al. which are used for both .debug_info and .debug_types dies.
1239 All parameters here are unchanging for the life of the call. This
dee91e82 1240 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
1241
1242struct die_reader_specs
1243{
a32a8923 1244 /* The bfd of die_section. */
93311388
DE
1245 bfd* abfd;
1246
1247 /* The CU of the DIE we are parsing. */
1248 struct dwarf2_cu *cu;
1249
80626a55 1250 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
1251 struct dwo_file *dwo_file;
1252
dee91e82 1253 /* The section the die comes from.
3019eac3 1254 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
1255 struct dwarf2_section_info *die_section;
1256
1257 /* die_section->buffer. */
d521ce57 1258 const gdb_byte *buffer;
f664829e
DE
1259
1260 /* The end of the buffer. */
1261 const gdb_byte *buffer_end;
a2ce51a0
DE
1262
1263 /* The value of the DW_AT_comp_dir attribute. */
1264 const char *comp_dir;
685af9cd
TT
1265
1266 /* The abbreviation table to use when reading the DIEs. */
1267 struct abbrev_table *abbrev_table;
93311388
DE
1268};
1269
fd820528 1270/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 1271typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 1272 const gdb_byte *info_ptr,
dee91e82
DE
1273 struct die_info *comp_unit_die,
1274 int has_children,
1275 void *data);
1276
ecfb656c
PA
1277/* A 1-based directory index. This is a strong typedef to prevent
1278 accidentally using a directory index as a 0-based index into an
1279 array/vector. */
1280enum class dir_index : unsigned int {};
1281
1282/* Likewise, a 1-based file name index. */
1283enum class file_name_index : unsigned int {};
1284
52059ffd
TT
1285struct file_entry
1286{
fff8551c
PA
1287 file_entry () = default;
1288
ecfb656c 1289 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
1290 unsigned int mod_time_, unsigned int length_)
1291 : name (name_),
ecfb656c 1292 d_index (d_index_),
fff8551c
PA
1293 mod_time (mod_time_),
1294 length (length_)
1295 {}
1296
ecfb656c
PA
1297 /* Return the include directory at D_INDEX stored in LH. Returns
1298 NULL if D_INDEX is out of bounds. */
8c43009f
PA
1299 const char *include_dir (const line_header *lh) const;
1300
fff8551c
PA
1301 /* The file name. Note this is an observing pointer. The memory is
1302 owned by debug_line_buffer. */
1303 const char *name {};
1304
8c43009f 1305 /* The directory index (1-based). */
ecfb656c 1306 dir_index d_index {};
fff8551c
PA
1307
1308 unsigned int mod_time {};
1309
1310 unsigned int length {};
1311
1312 /* True if referenced by the Line Number Program. */
1313 bool included_p {};
1314
83769d0b 1315 /* The associated symbol table, if any. */
fff8551c 1316 struct symtab *symtab {};
52059ffd
TT
1317};
1318
debd256d
JB
1319/* The line number information for a compilation unit (found in the
1320 .debug_line section) begins with a "statement program header",
1321 which contains the following information. */
1322struct line_header
1323{
fff8551c
PA
1324 line_header ()
1325 : offset_in_dwz {}
1326 {}
1327
1328 /* Add an entry to the include directory table. */
1329 void add_include_dir (const char *include_dir);
1330
1331 /* Add an entry to the file name table. */
ecfb656c 1332 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
1333 unsigned int mod_time, unsigned int length);
1334
ecfb656c 1335 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
8c43009f 1336 is out of bounds. */
ecfb656c 1337 const char *include_dir_at (dir_index index) const
8c43009f 1338 {
ecfb656c
PA
1339 /* Convert directory index number (1-based) to vector index
1340 (0-based). */
1341 size_t vec_index = to_underlying (index) - 1;
1342
1343 if (vec_index >= include_dirs.size ())
8c43009f 1344 return NULL;
ecfb656c 1345 return include_dirs[vec_index];
8c43009f
PA
1346 }
1347
ecfb656c 1348 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
8c43009f 1349 is out of bounds. */
ecfb656c 1350 file_entry *file_name_at (file_name_index index)
8c43009f 1351 {
ecfb656c
PA
1352 /* Convert file name index number (1-based) to vector index
1353 (0-based). */
1354 size_t vec_index = to_underlying (index) - 1;
1355
1356 if (vec_index >= file_names.size ())
fff8551c 1357 return NULL;
ecfb656c 1358 return &file_names[vec_index];
fff8551c
PA
1359 }
1360
1361 /* Const version of the above. */
1362 const file_entry *file_name_at (unsigned int index) const
1363 {
1364 if (index >= file_names.size ())
8c43009f
PA
1365 return NULL;
1366 return &file_names[index];
1367 }
1368
527f3840 1369 /* Offset of line number information in .debug_line section. */
9c541725 1370 sect_offset sect_off {};
527f3840
JK
1371
1372 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1373 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1374
1375 unsigned int total_length {};
1376 unsigned short version {};
1377 unsigned int header_length {};
1378 unsigned char minimum_instruction_length {};
1379 unsigned char maximum_ops_per_instruction {};
1380 unsigned char default_is_stmt {};
1381 int line_base {};
1382 unsigned char line_range {};
1383 unsigned char opcode_base {};
debd256d
JB
1384
1385 /* standard_opcode_lengths[i] is the number of operands for the
1386 standard opcode whose value is i. This means that
1387 standard_opcode_lengths[0] is unused, and the last meaningful
1388 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1389 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1390
fff8551c
PA
1391 /* The include_directories table. Note these are observing
1392 pointers. The memory is owned by debug_line_buffer. */
1393 std::vector<const char *> include_dirs;
debd256d 1394
fff8551c
PA
1395 /* The file_names table. */
1396 std::vector<file_entry> file_names;
debd256d
JB
1397
1398 /* The start and end of the statement program following this
6502dd73 1399 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1400 const gdb_byte *statement_program_start {}, *statement_program_end {};
debd256d 1401};
c906108c 1402
fff8551c
PA
1403typedef std::unique_ptr<line_header> line_header_up;
1404
8c43009f
PA
1405const char *
1406file_entry::include_dir (const line_header *lh) const
1407{
ecfb656c 1408 return lh->include_dir_at (d_index);
8c43009f
PA
1409}
1410
c906108c 1411/* When we construct a partial symbol table entry we only
0963b4bd 1412 need this much information. */
6f06d47b 1413struct partial_die_info : public allocate_on_obstack
c906108c 1414 {
6f06d47b
YQ
1415 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1416
1417 /* Disable assign but still keep copy ctor, which is needed
1418 load_partial_dies. */
1419 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1420
52356b79
YQ
1421 /* Adjust the partial die before generating a symbol for it. This
1422 function may set the is_external flag or change the DIE's
1423 name. */
1424 void fixup (struct dwarf2_cu *cu);
1425
48fbe735
YQ
1426 /* Read a minimal amount of information into the minimal die
1427 structure. */
1428 const gdb_byte *read (const struct die_reader_specs *reader,
1429 const struct abbrev_info &abbrev,
1430 const gdb_byte *info_ptr);
1431
72bf9492 1432 /* Offset of this DIE. */
6f06d47b 1433 const sect_offset sect_off;
72bf9492
DJ
1434
1435 /* DWARF-2 tag for this DIE. */
6f06d47b 1436 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1437
72bf9492 1438 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1439 const unsigned int has_children : 1;
1440
72bf9492
DJ
1441 unsigned int is_external : 1;
1442 unsigned int is_declaration : 1;
1443 unsigned int has_type : 1;
1444 unsigned int has_specification : 1;
1445 unsigned int has_pc_info : 1;
481860b3 1446 unsigned int may_be_inlined : 1;
72bf9492 1447
0c1b455e
TT
1448 /* This DIE has been marked DW_AT_main_subprogram. */
1449 unsigned int main_subprogram : 1;
1450
72bf9492
DJ
1451 /* Flag set if the SCOPE field of this structure has been
1452 computed. */
1453 unsigned int scope_set : 1;
1454
fa4028e9
JB
1455 /* Flag set if the DIE has a byte_size attribute. */
1456 unsigned int has_byte_size : 1;
1457
ff908ebf
AW
1458 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1459 unsigned int has_const_value : 1;
1460
98bfdba5
PA
1461 /* Flag set if any of the DIE's children are template arguments. */
1462 unsigned int has_template_arguments : 1;
1463
52356b79 1464 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1465 unsigned int fixup_called : 1;
1466
36586728
TT
1467 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1468 unsigned int is_dwz : 1;
1469
1470 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1471 unsigned int spec_is_dwz : 1;
1472
72bf9492 1473 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1474 sometimes a default name for unnamed DIEs. */
6f06d47b 1475 const char *name = nullptr;
72bf9492 1476
abc72ce4 1477 /* The linkage name, if present. */
6f06d47b 1478 const char *linkage_name = nullptr;
abc72ce4 1479
72bf9492
DJ
1480 /* The scope to prepend to our children. This is generally
1481 allocated on the comp_unit_obstack, so will disappear
1482 when this compilation unit leaves the cache. */
6f06d47b 1483 const char *scope = nullptr;
72bf9492 1484
95554aad
TT
1485 /* Some data associated with the partial DIE. The tag determines
1486 which field is live. */
1487 union
1488 {
1489 /* The location description associated with this DIE, if any. */
1490 struct dwarf_block *locdesc;
1491 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1492 sect_offset sect_off;
6f06d47b 1493 } d {};
72bf9492
DJ
1494
1495 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1496 CORE_ADDR lowpc = 0;
1497 CORE_ADDR highpc = 0;
72bf9492 1498
93311388 1499 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1500 DW_AT_sibling, if any. */
48fbe735
YQ
1501 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1502 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1503 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1504
1505 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1506 DW_AT_specification (or DW_AT_abstract_origin or
1507 DW_AT_extension). */
6f06d47b 1508 sect_offset spec_offset {};
72bf9492
DJ
1509
1510 /* Pointers to this DIE's parent, first child, and next sibling,
1511 if any. */
6f06d47b
YQ
1512 struct partial_die_info *die_parent = nullptr;
1513 struct partial_die_info *die_child = nullptr;
1514 struct partial_die_info *die_sibling = nullptr;
1515
1516 friend struct partial_die_info *
1517 dwarf2_cu::find_partial_die (sect_offset sect_off);
1518
1519 private:
1520 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1521 partial_die_info (sect_offset sect_off)
1522 : partial_die_info (sect_off, DW_TAG_padding, 0)
1523 {
1524 }
1525
1526 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1527 int has_children_)
1528 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1529 {
1530 is_external = 0;
1531 is_declaration = 0;
1532 has_type = 0;
1533 has_specification = 0;
1534 has_pc_info = 0;
1535 may_be_inlined = 0;
1536 main_subprogram = 0;
1537 scope_set = 0;
1538 has_byte_size = 0;
1539 has_const_value = 0;
1540 has_template_arguments = 0;
1541 fixup_called = 0;
1542 is_dwz = 0;
1543 spec_is_dwz = 0;
1544 }
c906108c
SS
1545 };
1546
0963b4bd 1547/* This data structure holds the information of an abbrev. */
c906108c
SS
1548struct abbrev_info
1549 {
1550 unsigned int number; /* number identifying abbrev */
1551 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1552 unsigned short has_children; /* boolean */
1553 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1554 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1555 struct abbrev_info *next; /* next in chain */
1556 };
1557
1558struct attr_abbrev
1559 {
9d25dd43
DE
1560 ENUM_BITFIELD(dwarf_attribute) name : 16;
1561 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1562
1563 /* It is valid only if FORM is DW_FORM_implicit_const. */
1564 LONGEST implicit_const;
c906108c
SS
1565 };
1566
433df2d4
DE
1567/* Size of abbrev_table.abbrev_hash_table. */
1568#define ABBREV_HASH_SIZE 121
1569
1570/* Top level data structure to contain an abbreviation table. */
1571
1572struct abbrev_table
1573{
685af9cd
TT
1574 explicit abbrev_table (sect_offset off)
1575 : sect_off (off)
1576 {
4a17f768 1577 m_abbrevs =
685af9cd 1578 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
4a17f768 1579 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
685af9cd
TT
1580 }
1581
1582 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1583
1584 /* Allocate space for a struct abbrev_info object in
1585 ABBREV_TABLE. */
1586 struct abbrev_info *alloc_abbrev ();
1587
1588 /* Add an abbreviation to the table. */
1589 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1590
1591 /* Look up an abbrev in the table.
1592 Returns NULL if the abbrev is not found. */
1593
1594 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1595
1596
f4dc4d17
DE
1597 /* Where the abbrev table came from.
1598 This is used as a sanity check when the table is used. */
685af9cd 1599 const sect_offset sect_off;
433df2d4
DE
1600
1601 /* Storage for the abbrev table. */
685af9cd 1602 auto_obstack abbrev_obstack;
433df2d4 1603
4a17f768
YQ
1604private:
1605
433df2d4
DE
1606 /* Hash table of abbrevs.
1607 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1608 It could be statically allocated, but the previous code didn't so we
1609 don't either. */
4a17f768 1610 struct abbrev_info **m_abbrevs;
433df2d4
DE
1611};
1612
685af9cd
TT
1613typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1614
0963b4bd 1615/* Attributes have a name and a value. */
b60c80d6
DJ
1616struct attribute
1617 {
9d25dd43 1618 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1619 ENUM_BITFIELD(dwarf_form) form : 15;
1620
1621 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1622 field should be in u.str (existing only for DW_STRING) but it is kept
1623 here for better struct attribute alignment. */
1624 unsigned int string_is_canonical : 1;
1625
b60c80d6
DJ
1626 union
1627 {
15d034d0 1628 const char *str;
b60c80d6 1629 struct dwarf_block *blk;
43bbcdc2
PH
1630 ULONGEST unsnd;
1631 LONGEST snd;
b60c80d6 1632 CORE_ADDR addr;
ac9ec31b 1633 ULONGEST signature;
b60c80d6
DJ
1634 }
1635 u;
1636 };
1637
0963b4bd 1638/* This data structure holds a complete die structure. */
c906108c
SS
1639struct die_info
1640 {
76815b17
DE
1641 /* DWARF-2 tag for this DIE. */
1642 ENUM_BITFIELD(dwarf_tag) tag : 16;
1643
1644 /* Number of attributes */
98bfdba5
PA
1645 unsigned char num_attrs;
1646
1647 /* True if we're presently building the full type name for the
1648 type derived from this DIE. */
1649 unsigned char building_fullname : 1;
76815b17 1650
adde2bff
DE
1651 /* True if this die is in process. PR 16581. */
1652 unsigned char in_process : 1;
1653
76815b17
DE
1654 /* Abbrev number */
1655 unsigned int abbrev;
1656
93311388 1657 /* Offset in .debug_info or .debug_types section. */
9c541725 1658 sect_offset sect_off;
78ba4af6
JB
1659
1660 /* The dies in a compilation unit form an n-ary tree. PARENT
1661 points to this die's parent; CHILD points to the first child of
1662 this node; and all the children of a given node are chained
4950bc1c 1663 together via their SIBLING fields. */
639d11d3
DC
1664 struct die_info *child; /* Its first child, if any. */
1665 struct die_info *sibling; /* Its next sibling, if any. */
1666 struct die_info *parent; /* Its parent, if any. */
c906108c 1667
b60c80d6
DJ
1668 /* An array of attributes, with NUM_ATTRS elements. There may be
1669 zero, but it's not common and zero-sized arrays are not
1670 sufficiently portable C. */
1671 struct attribute attrs[1];
c906108c
SS
1672 };
1673
0963b4bd 1674/* Get at parts of an attribute structure. */
c906108c
SS
1675
1676#define DW_STRING(attr) ((attr)->u.str)
8285870a 1677#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1678#define DW_UNSND(attr) ((attr)->u.unsnd)
1679#define DW_BLOCK(attr) ((attr)->u.blk)
1680#define DW_SND(attr) ((attr)->u.snd)
1681#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1682#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1683
0963b4bd 1684/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1685struct dwarf_block
1686 {
56eb65bd 1687 size_t size;
1d6edc3c
JK
1688
1689 /* Valid only if SIZE is not zero. */
d521ce57 1690 const gdb_byte *data;
c906108c
SS
1691 };
1692
c906108c
SS
1693#ifndef ATTR_ALLOC_CHUNK
1694#define ATTR_ALLOC_CHUNK 4
1695#endif
1696
c906108c
SS
1697/* Allocate fields for structs, unions and enums in this size. */
1698#ifndef DW_FIELD_ALLOC_CHUNK
1699#define DW_FIELD_ALLOC_CHUNK 4
1700#endif
1701
c906108c
SS
1702/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1703 but this would require a corresponding change in unpack_field_as_long
1704 and friends. */
1705static int bits_per_byte = 8;
1706
2ddeaf8a
TT
1707/* When reading a variant or variant part, we track a bit more
1708 information about the field, and store it in an object of this
1709 type. */
1710
1711struct variant_field
1712{
1713 /* If we see a DW_TAG_variant, then this will be the discriminant
1714 value. */
1715 ULONGEST discriminant_value;
1716 /* If we see a DW_TAG_variant, then this will be set if this is the
1717 default branch. */
1718 bool default_branch;
1719 /* While reading a DW_TAG_variant_part, this will be set if this
1720 field is the discriminant. */
1721 bool is_discriminant;
1722};
1723
52059ffd
TT
1724struct nextfield
1725{
be2daae6
TT
1726 int accessibility = 0;
1727 int virtuality = 0;
2ddeaf8a 1728 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1729 struct variant_field variant {};
1730 struct field field {};
52059ffd
TT
1731};
1732
1733struct fnfieldlist
1734{
be2daae6
TT
1735 const char *name = nullptr;
1736 std::vector<struct fn_field> fnfields;
52059ffd
TT
1737};
1738
c906108c
SS
1739/* The routines that read and process dies for a C struct or C++ class
1740 pass lists of data member fields and lists of member function fields
1741 in an instance of a field_info structure, as defined below. */
1742struct field_info
c5aa993b 1743 {
0963b4bd 1744 /* List of data member and baseclasses fields. */
be2daae6
TT
1745 std::vector<struct nextfield> fields;
1746 std::vector<struct nextfield> baseclasses;
c906108c 1747
7d0ccb61 1748 /* Number of fields (including baseclasses). */
be2daae6 1749 int nfields = 0;
c906108c 1750
c5aa993b 1751 /* Set if the accesibility of one of the fields is not public. */
be2daae6 1752 int non_public_fields = 0;
c906108c 1753
c5aa993b
JM
1754 /* Member function fieldlist array, contains name of possibly overloaded
1755 member function, number of overloaded member functions and a pointer
1756 to the head of the member function field chain. */
be2daae6 1757 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1758
1759 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1760 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1761 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1762
1763 /* Nested types defined by this class and the number of elements in this
1764 list. */
be2daae6 1765 std::vector<struct decl_field> nested_types_list;
c5aa993b 1766 };
c906108c 1767
10b3939b
DJ
1768/* One item on the queue of compilation units to read in full symbols
1769 for. */
1770struct dwarf2_queue_item
1771{
1772 struct dwarf2_per_cu_data *per_cu;
95554aad 1773 enum language pretend_language;
10b3939b
DJ
1774 struct dwarf2_queue_item *next;
1775};
1776
1777/* The current queue. */
1778static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1779
ae038cb0
DJ
1780/* Loaded secondary compilation units are kept in memory until they
1781 have not been referenced for the processing of this many
1782 compilation units. Set this to zero to disable caching. Cache
1783 sizes of up to at least twenty will improve startup time for
1784 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1785static int dwarf_max_cache_age = 5;
920d2a44 1786static void
b4f54984
DE
1787show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1788 struct cmd_list_element *c, const char *value)
920d2a44 1789{
3e43a32a 1790 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1791 "DWARF compilation units is %s.\n"),
920d2a44
AC
1792 value);
1793}
4390d890 1794\f
c906108c
SS
1795/* local function prototypes */
1796
a32a8923
DE
1797static const char *get_section_name (const struct dwarf2_section_info *);
1798
1799static const char *get_section_file_name (const struct dwarf2_section_info *);
1800
918dd910
JK
1801static void dwarf2_find_base_address (struct die_info *die,
1802 struct dwarf2_cu *cu);
1803
0018ea6f
DE
1804static struct partial_symtab *create_partial_symtab
1805 (struct dwarf2_per_cu_data *per_cu, const char *name);
1806
f1902523
JK
1807static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1808 const gdb_byte *info_ptr,
1809 struct die_info *type_unit_die,
1810 int has_children, void *data);
1811
ed2dc618
SM
1812static void dwarf2_build_psymtabs_hard
1813 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1814
72bf9492
DJ
1815static void scan_partial_symbols (struct partial_die_info *,
1816 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1817 int, struct dwarf2_cu *);
c906108c 1818
72bf9492
DJ
1819static void add_partial_symbol (struct partial_die_info *,
1820 struct dwarf2_cu *);
63d06c5c 1821
72bf9492
DJ
1822static void add_partial_namespace (struct partial_die_info *pdi,
1823 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1824 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1825
5d7cb8df 1826static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1827 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1828 struct dwarf2_cu *cu);
1829
72bf9492
DJ
1830static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1831 struct dwarf2_cu *cu);
91c24f0a 1832
bc30ff58
JB
1833static void add_partial_subprogram (struct partial_die_info *pdi,
1834 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1835 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1836
257e7a09
YQ
1837static void dwarf2_read_symtab (struct partial_symtab *,
1838 struct objfile *);
c906108c 1839
a14ed312 1840static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1841
685af9cd 1842static abbrev_table_up abbrev_table_read_table
ed2dc618
SM
1843 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1844 sect_offset);
433df2d4 1845
d521ce57 1846static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1847
dee91e82 1848static struct partial_die_info *load_partial_dies
d521ce57 1849 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1850
36586728 1851static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1852 struct dwarf2_cu *);
72bf9492 1853
d521ce57
TT
1854static const gdb_byte *read_attribute (const struct die_reader_specs *,
1855 struct attribute *, struct attr_abbrev *,
1856 const gdb_byte *);
a8329558 1857
a1855c1d 1858static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1859
a1855c1d 1860static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1861
a1855c1d 1862static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1863
a1855c1d 1864static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1865
a1855c1d 1866static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1867
d521ce57 1868static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1869 unsigned int *);
c906108c 1870
d521ce57 1871static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1872
1873static LONGEST read_checked_initial_length_and_offset
d521ce57 1874 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1875 unsigned int *, unsigned int *);
613e1657 1876
d521ce57
TT
1877static LONGEST read_offset (bfd *, const gdb_byte *,
1878 const struct comp_unit_head *,
c764a876
DE
1879 unsigned int *);
1880
d521ce57 1881static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1882
ed2dc618
SM
1883static sect_offset read_abbrev_offset
1884 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1885 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1886
d521ce57 1887static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1888
d521ce57 1889static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1890
ed2dc618
SM
1891static const char *read_indirect_string
1892 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1893 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1894
ed2dc618
SM
1895static const char *read_indirect_line_string
1896 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1897 const struct comp_unit_head *, unsigned int *);
36586728 1898
ed2dc618
SM
1899static const char *read_indirect_string_at_offset
1900 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1901 LONGEST str_offset);
927aa2e7 1902
ed2dc618
SM
1903static const char *read_indirect_string_from_dwz
1904 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1905
d521ce57 1906static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1907
d521ce57
TT
1908static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1909 const gdb_byte *,
3019eac3
DE
1910 unsigned int *);
1911
d521ce57 1912static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1913 ULONGEST str_index);
3019eac3 1914
e142c38c 1915static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1916
e142c38c
DJ
1917static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1918 struct dwarf2_cu *);
c906108c 1919
348e048f 1920static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1921 unsigned int);
348e048f 1922
7d45c7c3
KB
1923static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1924 struct dwarf2_cu *cu);
1925
05cf31d1
JB
1926static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1927 struct dwarf2_cu *cu);
1928
e142c38c 1929static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1930
e142c38c 1931static struct die_info *die_specification (struct die_info *die,
f2f0e013 1932 struct dwarf2_cu **);
63d06c5c 1933
9c541725 1934static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1935 struct dwarf2_cu *cu);
debd256d 1936
f3f5162e 1937static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1938 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1939 CORE_ADDR, int decode_mapping);
c906108c 1940
4d663531 1941static void dwarf2_start_subfile (const char *, const char *);
c906108c 1942
43f3e411
DE
1943static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1944 const char *, const char *,
1945 CORE_ADDR);
f4dc4d17 1946
a14ed312 1947static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1948 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1949
ff39bb5e 1950static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1951 struct dwarf2_cu *);
c906108c 1952
ff39bb5e 1953static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1954 struct type *type,
1955 const char *name,
1956 struct obstack *obstack,
12df843f 1957 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1958 const gdb_byte **bytes,
98bfdba5 1959 struct dwarf2_locexpr_baton **baton);
2df3850c 1960
e7c27a73 1961static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1962
b4ba55a1
JB
1963static int need_gnat_info (struct dwarf2_cu *);
1964
3e43a32a
MS
1965static struct type *die_descriptive_type (struct die_info *,
1966 struct dwarf2_cu *);
b4ba55a1
JB
1967
1968static void set_descriptive_type (struct type *, struct die_info *,
1969 struct dwarf2_cu *);
1970
e7c27a73
DJ
1971static struct type *die_containing_type (struct die_info *,
1972 struct dwarf2_cu *);
c906108c 1973
ff39bb5e 1974static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1975 struct dwarf2_cu *);
c906108c 1976
f792889a 1977static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1978
673bfd45
DE
1979static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1980
0d5cff50 1981static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1982
6e70227d 1983static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1984 const char *suffix, int physname,
1985 struct dwarf2_cu *cu);
63d06c5c 1986
e7c27a73 1987static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1988
348e048f
DE
1989static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1990
e7c27a73 1991static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1992
e7c27a73 1993static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1994
96408a79
SA
1995static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1996
71a3c369
TT
1997static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1998
ff013f42
JK
1999static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
2000 struct dwarf2_cu *, struct partial_symtab *);
2001
3a2b436a 2002/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 2003 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
2004enum pc_bounds_kind
2005{
e385593e 2006 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
2007 PC_BOUNDS_NOT_PRESENT,
2008
e385593e
JK
2009 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
2010 were present but they do not form a valid range of PC addresses. */
2011 PC_BOUNDS_INVALID,
2012
3a2b436a
JK
2013 /* Discontiguous range was found - that is DW_AT_ranges was found. */
2014 PC_BOUNDS_RANGES,
2015
2016 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
2017 PC_BOUNDS_HIGH_LOW,
2018};
2019
2020static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
2021 CORE_ADDR *, CORE_ADDR *,
2022 struct dwarf2_cu *,
2023 struct partial_symtab *);
c906108c 2024
fae299cd
DC
2025static void get_scope_pc_bounds (struct die_info *,
2026 CORE_ADDR *, CORE_ADDR *,
2027 struct dwarf2_cu *);
2028
801e3a5b
JB
2029static void dwarf2_record_block_ranges (struct die_info *, struct block *,
2030 CORE_ADDR, struct dwarf2_cu *);
2031
a14ed312 2032static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 2033 struct dwarf2_cu *);
c906108c 2034
a14ed312 2035static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 2036 struct type *, struct dwarf2_cu *);
c906108c 2037
a14ed312 2038static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 2039 struct die_info *, struct type *,
e7c27a73 2040 struct dwarf2_cu *);
c906108c 2041
a14ed312 2042static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
2043 struct type *,
2044 struct dwarf2_cu *);
c906108c 2045
134d01f1 2046static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 2047
e7c27a73 2048static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 2049
e7c27a73 2050static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 2051
5d7cb8df
JK
2052static void read_module (struct die_info *die, struct dwarf2_cu *cu);
2053
22cee43f
PMR
2054static struct using_direct **using_directives (enum language);
2055
27aa8d6a
SW
2056static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
2057
74921315
KS
2058static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
2059
f55ee35c
JK
2060static struct type *read_module_type (struct die_info *die,
2061 struct dwarf2_cu *cu);
2062
38d518c9 2063static const char *namespace_name (struct die_info *die,
e142c38c 2064 int *is_anonymous, struct dwarf2_cu *);
38d518c9 2065
134d01f1 2066static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 2067
e7c27a73 2068static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 2069
6e70227d 2070static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
2071 struct dwarf2_cu *);
2072
bf6af496 2073static struct die_info *read_die_and_siblings_1
d521ce57 2074 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 2075 struct die_info *);
639d11d3 2076
dee91e82 2077static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
2078 const gdb_byte *info_ptr,
2079 const gdb_byte **new_info_ptr,
639d11d3
DC
2080 struct die_info *parent);
2081
d521ce57
TT
2082static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
2083 struct die_info **, const gdb_byte *,
2084 int *, int);
3019eac3 2085
d521ce57
TT
2086static const gdb_byte *read_full_die (const struct die_reader_specs *,
2087 struct die_info **, const gdb_byte *,
2088 int *);
93311388 2089
e7c27a73 2090static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 2091
15d034d0
TT
2092static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
2093 struct obstack *);
71c25dea 2094
15d034d0 2095static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 2096
15d034d0 2097static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
2098 struct die_info *die,
2099 struct dwarf2_cu *cu);
2100
ca69b9e6
DE
2101static const char *dwarf2_physname (const char *name, struct die_info *die,
2102 struct dwarf2_cu *cu);
2103
e142c38c 2104static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 2105 struct dwarf2_cu **);
9219021c 2106
f39c6ffd 2107static const char *dwarf_tag_name (unsigned int);
c906108c 2108
f39c6ffd 2109static const char *dwarf_attr_name (unsigned int);
c906108c 2110
f39c6ffd 2111static const char *dwarf_form_name (unsigned int);
c906108c 2112
a121b7c1 2113static const char *dwarf_bool_name (unsigned int);
c906108c 2114
f39c6ffd 2115static const char *dwarf_type_encoding_name (unsigned int);
c906108c 2116
f9aca02d 2117static struct die_info *sibling_die (struct die_info *);
c906108c 2118
d97bc12b
DE
2119static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
2120
2121static void dump_die_for_error (struct die_info *);
2122
2123static void dump_die_1 (struct ui_file *, int level, int max_level,
2124 struct die_info *);
c906108c 2125
d97bc12b 2126/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 2127
51545339 2128static void store_in_ref_table (struct die_info *,
10b3939b 2129 struct dwarf2_cu *);
c906108c 2130
ff39bb5e 2131static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 2132
ff39bb5e 2133static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 2134
348e048f 2135static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 2136 const struct attribute *,
348e048f
DE
2137 struct dwarf2_cu **);
2138
10b3939b 2139static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 2140 const struct attribute *,
f2f0e013 2141 struct dwarf2_cu **);
c906108c 2142
348e048f 2143static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 2144 const struct attribute *,
348e048f
DE
2145 struct dwarf2_cu **);
2146
ac9ec31b
DE
2147static struct type *get_signatured_type (struct die_info *, ULONGEST,
2148 struct dwarf2_cu *);
2149
2150static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 2151 const struct attribute *,
ac9ec31b
DE
2152 struct dwarf2_cu *);
2153
e5fe5e75 2154static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 2155
52dc124a 2156static void read_signatured_type (struct signatured_type *);
348e048f 2157
63e43d3a
PMR
2158static int attr_to_dynamic_prop (const struct attribute *attr,
2159 struct die_info *die, struct dwarf2_cu *cu,
2160 struct dynamic_prop *prop);
2161
c906108c
SS
2162/* memory allocation interface */
2163
7b5a2f43 2164static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 2165
b60c80d6 2166static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 2167
43f3e411 2168static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 2169
6e5a29e1 2170static int attr_form_is_block (const struct attribute *);
8e19ed76 2171
6e5a29e1 2172static int attr_form_is_section_offset (const struct attribute *);
3690dd37 2173
6e5a29e1 2174static int attr_form_is_constant (const struct attribute *);
3690dd37 2175
6e5a29e1 2176static int attr_form_is_ref (const struct attribute *);
7771576e 2177
8cf6f0b1
TT
2178static void fill_in_loclist_baton (struct dwarf2_cu *cu,
2179 struct dwarf2_loclist_baton *baton,
ff39bb5e 2180 const struct attribute *attr);
8cf6f0b1 2181
ff39bb5e 2182static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 2183 struct symbol *sym,
f1e6e072
TT
2184 struct dwarf2_cu *cu,
2185 int is_block);
4c2df51b 2186
d521ce57
TT
2187static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
2188 const gdb_byte *info_ptr,
2189 struct abbrev_info *abbrev);
4bb7a0a7 2190
72bf9492
DJ
2191static hashval_t partial_die_hash (const void *item);
2192
2193static int partial_die_eq (const void *item_lhs, const void *item_rhs);
2194
ae038cb0 2195static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
2196 (sect_offset sect_off, unsigned int offset_in_dwz,
2197 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 2198
9816fde3 2199static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
2200 struct die_info *comp_unit_die,
2201 enum language pretend_language);
93311388 2202
ae038cb0
DJ
2203static void free_cached_comp_units (void *);
2204
ed2dc618 2205static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 2206
dee91e82 2207static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 2208
f792889a
DJ
2209static struct type *set_die_type (struct die_info *, struct type *,
2210 struct dwarf2_cu *);
1c379e20 2211
ed2dc618 2212static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 2213
ed2dc618 2214static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 2215
95554aad
TT
2216static void load_full_comp_unit (struct dwarf2_per_cu_data *,
2217 enum language);
10b3939b 2218
95554aad
TT
2219static void process_full_comp_unit (struct dwarf2_per_cu_data *,
2220 enum language);
10b3939b 2221
f4dc4d17
DE
2222static void process_full_type_unit (struct dwarf2_per_cu_data *,
2223 enum language);
2224
10b3939b
DJ
2225static void dwarf2_add_dependence (struct dwarf2_cu *,
2226 struct dwarf2_per_cu_data *);
2227
ae038cb0
DJ
2228static void dwarf2_mark (struct dwarf2_cu *);
2229
2230static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
2231
b64f50a1 2232static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 2233 struct dwarf2_per_cu_data *);
673bfd45 2234
f792889a 2235static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 2236
95554aad
TT
2237static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
2238 enum language pretend_language);
2239
ed2dc618 2240static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 2241
b303c6f6
AB
2242/* Class, the destructor of which frees all allocated queue entries. This
2243 will only have work to do if an error was thrown while processing the
2244 dwarf. If no error was thrown then the queue entries should have all
2245 been processed, and freed, as we went along. */
2246
2247class dwarf2_queue_guard
2248{
2249public:
2250 dwarf2_queue_guard () = default;
2251
2252 /* Free any entries remaining on the queue. There should only be
2253 entries left if we hit an error while processing the dwarf. */
2254 ~dwarf2_queue_guard ()
2255 {
2256 struct dwarf2_queue_item *item, *last;
2257
2258 item = dwarf2_queue;
2259 while (item)
2260 {
2261 /* Anything still marked queued is likely to be in an
2262 inconsistent state, so discard it. */
2263 if (item->per_cu->queued)
2264 {
2265 if (item->per_cu->cu != NULL)
2266 free_one_cached_comp_unit (item->per_cu);
2267 item->per_cu->queued = 0;
2268 }
2269
2270 last = item;
2271 item = item->next;
2272 xfree (last);
2273 }
2274
2275 dwarf2_queue = dwarf2_queue_tail = NULL;
2276 }
2277};
2278
d721ba37
PA
2279/* The return type of find_file_and_directory. Note, the enclosed
2280 string pointers are only valid while this object is valid. */
2281
2282struct file_and_directory
2283{
2284 /* The filename. This is never NULL. */
2285 const char *name;
2286
2287 /* The compilation directory. NULL if not known. If we needed to
2288 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2289 points directly to the DW_AT_comp_dir string attribute owned by
2290 the obstack that owns the DIE. */
2291 const char *comp_dir;
2292
2293 /* If we needed to build a new string for comp_dir, this is what
2294 owns the storage. */
2295 std::string comp_dir_storage;
2296};
2297
2298static file_and_directory find_file_and_directory (struct die_info *die,
2299 struct dwarf2_cu *cu);
9291a0cd
TT
2300
2301static char *file_full_name (int file, struct line_header *lh,
2302 const char *comp_dir);
2303
43988095
JK
2304/* Expected enum dwarf_unit_type for read_comp_unit_head. */
2305enum class rcuh_kind { COMPILE, TYPE };
2306
d521ce57 2307static const gdb_byte *read_and_check_comp_unit_head
ed2dc618
SM
2308 (struct dwarf2_per_objfile* dwarf2_per_objfile,
2309 struct comp_unit_head *header,
36586728 2310 struct dwarf2_section_info *section,
d521ce57 2311 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 2312 rcuh_kind section_kind);
36586728 2313
fd820528 2314static void init_cutu_and_read_dies
f4dc4d17
DE
2315 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2316 int use_existing_cu, int keep,
3019eac3
DE
2317 die_reader_func_ftype *die_reader_func, void *data);
2318
dee91e82
DE
2319static void init_cutu_and_read_dies_simple
2320 (struct dwarf2_per_cu_data *this_cu,
2321 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 2322
673bfd45 2323static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 2324
3019eac3
DE
2325static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2326
57d63ce2 2327static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
2328 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2329 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 2330 ULONGEST signature, int is_debug_types);
a2ce51a0 2331
ed2dc618
SM
2332static struct dwp_file *get_dwp_file
2333 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 2334
3019eac3 2335static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 2336 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
2337
2338static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 2339 (struct signatured_type *, const char *, const char *);
3019eac3 2340
89e63ee4
DE
2341static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2342
3019eac3
DE
2343static void free_dwo_file_cleanup (void *);
2344
ed2dc618
SM
2345struct free_dwo_file_cleanup_data
2346{
2347 struct dwo_file *dwo_file;
2348 struct dwarf2_per_objfile *dwarf2_per_objfile;
2349};
2350
2351static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 2352
1b80a9fa 2353static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2354
2355static void free_line_header_voidp (void *arg);
4390d890
DE
2356\f
2357/* Various complaints about symbol reading that don't abort the process. */
2358
2359static void
2360dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2361{
2362 complaint (&symfile_complaints,
2363 _("statement list doesn't fit in .debug_line section"));
2364}
2365
2366static void
2367dwarf2_debug_line_missing_file_complaint (void)
2368{
2369 complaint (&symfile_complaints,
2370 _(".debug_line section has line data without a file"));
2371}
2372
2373static void
2374dwarf2_debug_line_missing_end_sequence_complaint (void)
2375{
2376 complaint (&symfile_complaints,
2377 _(".debug_line section has line "
2378 "program sequence without an end"));
2379}
2380
2381static void
2382dwarf2_complex_location_expr_complaint (void)
2383{
2384 complaint (&symfile_complaints, _("location expression too complex"));
2385}
2386
2387static void
2388dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2389 int arg3)
2390{
2391 complaint (&symfile_complaints,
2392 _("const value length mismatch for '%s', got %d, expected %d"),
2393 arg1, arg2, arg3);
2394}
2395
2396static void
2397dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2398{
2399 complaint (&symfile_complaints,
2400 _("debug info runs off end of %s section"
2401 " [in module %s]"),
a32a8923
DE
2402 get_section_name (section),
2403 get_section_file_name (section));
4390d890 2404}
1b80a9fa 2405
4390d890
DE
2406static void
2407dwarf2_macro_malformed_definition_complaint (const char *arg1)
2408{
2409 complaint (&symfile_complaints,
2410 _("macro debug info contains a "
2411 "malformed macro definition:\n`%s'"),
2412 arg1);
2413}
2414
2415static void
2416dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2417{
2418 complaint (&symfile_complaints,
2419 _("invalid attribute class or form for '%s' in '%s'"),
2420 arg1, arg2);
2421}
527f3840
JK
2422
2423/* Hash function for line_header_hash. */
2424
2425static hashval_t
2426line_header_hash (const struct line_header *ofs)
2427{
9c541725 2428 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2429}
2430
2431/* Hash function for htab_create_alloc_ex for line_header_hash. */
2432
2433static hashval_t
2434line_header_hash_voidp (const void *item)
2435{
9a3c8263 2436 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2437
2438 return line_header_hash (ofs);
2439}
2440
2441/* Equality function for line_header_hash. */
2442
2443static int
2444line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2445{
9a3c8263
SM
2446 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2447 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2448
9c541725 2449 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2450 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2451}
2452
4390d890 2453\f
9291a0cd 2454
31aa7e4e
JB
2455/* Read the given attribute value as an address, taking the attribute's
2456 form into account. */
2457
2458static CORE_ADDR
2459attr_value_as_address (struct attribute *attr)
2460{
2461 CORE_ADDR addr;
2462
2463 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2464 {
2465 /* Aside from a few clearly defined exceptions, attributes that
2466 contain an address must always be in DW_FORM_addr form.
2467 Unfortunately, some compilers happen to be violating this
2468 requirement by encoding addresses using other forms, such
2469 as DW_FORM_data4 for example. For those broken compilers,
2470 we try to do our best, without any guarantee of success,
2471 to interpret the address correctly. It would also be nice
2472 to generate a complaint, but that would require us to maintain
2473 a list of legitimate cases where a non-address form is allowed,
2474 as well as update callers to pass in at least the CU's DWARF
2475 version. This is more overhead than what we're willing to
2476 expand for a pretty rare case. */
2477 addr = DW_UNSND (attr);
2478 }
2479 else
2480 addr = DW_ADDR (attr);
2481
2482 return addr;
2483}
2484
9291a0cd 2485/* The suffix for an index file. */
437afbb8
JK
2486#define INDEX4_SUFFIX ".gdb-index"
2487#define INDEX5_SUFFIX ".debug_names"
2488#define DEBUG_STR_SUFFIX ".debug_str"
9291a0cd 2489
330cdd98
PA
2490/* See declaration. */
2491
2492dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2493 const dwarf2_debug_sections *names)
2494 : objfile (objfile_)
2495{
2496 if (names == NULL)
2497 names = &dwarf2_elf_names;
2498
2499 bfd *obfd = objfile->obfd;
2500
2501 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2502 locate_sections (obfd, sec, *names);
2503}
2504
fc8e7e75
SM
2505static void free_dwo_files (htab_t dwo_files, struct objfile *objfile);
2506
330cdd98
PA
2507dwarf2_per_objfile::~dwarf2_per_objfile ()
2508{
2509 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2510 free_cached_comp_units ();
2511
2512 if (quick_file_names_table)
2513 htab_delete (quick_file_names_table);
2514
2515 if (line_header_hash)
2516 htab_delete (line_header_hash);
2517
fc8e7e75
SM
2518 for (int ix = 0; ix < n_comp_units; ++ix)
2519 VEC_free (dwarf2_per_cu_ptr, all_comp_units[ix]->imported_symtabs);
2520
2521 for (int ix = 0; ix < n_type_units; ++ix)
2522 VEC_free (dwarf2_per_cu_ptr,
2523 all_type_units[ix]->per_cu.imported_symtabs);
2524 xfree (all_type_units);
2525
2526 VEC_free (dwarf2_section_info_def, types);
2527
2528 if (dwo_files != NULL)
2529 free_dwo_files (dwo_files, objfile);
2530 if (dwp_file != NULL)
2531 gdb_bfd_unref (dwp_file->dbfd);
2532
2533 if (dwz_file != NULL && dwz_file->dwz_bfd)
2534 gdb_bfd_unref (dwz_file->dwz_bfd);
2535
2536 if (index_table != NULL)
2537 index_table->~mapped_index ();
2538
330cdd98
PA
2539 /* Everything else should be on the objfile obstack. */
2540}
2541
2542/* See declaration. */
2543
2544void
2545dwarf2_per_objfile::free_cached_comp_units ()
2546{
2547 dwarf2_per_cu_data *per_cu = read_in_chain;
2548 dwarf2_per_cu_data **last_chain = &read_in_chain;
2549 while (per_cu != NULL)
2550 {
2551 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2552
fcd3b13d 2553 delete per_cu->cu;
330cdd98
PA
2554 *last_chain = next_cu;
2555 per_cu = next_cu;
2556 }
2557}
2558
c906108c 2559/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2560 information and return true if we have enough to do something.
2561 NAMES points to the dwarf2 section names, or is NULL if the standard
2562 ELF names are used. */
c906108c
SS
2563
2564int
251d32d9
TG
2565dwarf2_has_info (struct objfile *objfile,
2566 const struct dwarf2_debug_sections *names)
c906108c 2567{
97cbe998
SDJ
2568 if (objfile->flags & OBJF_READNEVER)
2569 return 0;
2570
ed2dc618
SM
2571 struct dwarf2_per_objfile *dwarf2_per_objfile
2572 = get_dwarf2_per_objfile (objfile);
2573
2574 if (dwarf2_per_objfile == NULL)
be391dca
TT
2575 {
2576 /* Initialize per-objfile state. */
fd90ace4
YQ
2577 dwarf2_per_objfile
2578 = new (&objfile->objfile_obstack) struct dwarf2_per_objfile (objfile,
2579 names);
ed2dc618 2580 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
be391dca 2581 }
73869dc2 2582 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2583 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2584 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2585 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2586}
2587
2588/* Return the containing section of virtual section SECTION. */
2589
2590static struct dwarf2_section_info *
2591get_containing_section (const struct dwarf2_section_info *section)
2592{
2593 gdb_assert (section->is_virtual);
2594 return section->s.containing_section;
c906108c
SS
2595}
2596
a32a8923
DE
2597/* Return the bfd owner of SECTION. */
2598
2599static struct bfd *
2600get_section_bfd_owner (const struct dwarf2_section_info *section)
2601{
73869dc2
DE
2602 if (section->is_virtual)
2603 {
2604 section = get_containing_section (section);
2605 gdb_assert (!section->is_virtual);
2606 }
049412e3 2607 return section->s.section->owner;
a32a8923
DE
2608}
2609
2610/* Return the bfd section of SECTION.
2611 Returns NULL if the section is not present. */
2612
2613static asection *
2614get_section_bfd_section (const struct dwarf2_section_info *section)
2615{
73869dc2
DE
2616 if (section->is_virtual)
2617 {
2618 section = get_containing_section (section);
2619 gdb_assert (!section->is_virtual);
2620 }
049412e3 2621 return section->s.section;
a32a8923
DE
2622}
2623
2624/* Return the name of SECTION. */
2625
2626static const char *
2627get_section_name (const struct dwarf2_section_info *section)
2628{
2629 asection *sectp = get_section_bfd_section (section);
2630
2631 gdb_assert (sectp != NULL);
2632 return bfd_section_name (get_section_bfd_owner (section), sectp);
2633}
2634
2635/* Return the name of the file SECTION is in. */
2636
2637static const char *
2638get_section_file_name (const struct dwarf2_section_info *section)
2639{
2640 bfd *abfd = get_section_bfd_owner (section);
2641
2642 return bfd_get_filename (abfd);
2643}
2644
2645/* Return the id of SECTION.
2646 Returns 0 if SECTION doesn't exist. */
2647
2648static int
2649get_section_id (const struct dwarf2_section_info *section)
2650{
2651 asection *sectp = get_section_bfd_section (section);
2652
2653 if (sectp == NULL)
2654 return 0;
2655 return sectp->id;
2656}
2657
2658/* Return the flags of SECTION.
73869dc2 2659 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2660
2661static int
2662get_section_flags (const struct dwarf2_section_info *section)
2663{
2664 asection *sectp = get_section_bfd_section (section);
2665
2666 gdb_assert (sectp != NULL);
2667 return bfd_get_section_flags (sectp->owner, sectp);
2668}
2669
251d32d9
TG
2670/* When loading sections, we look either for uncompressed section or for
2671 compressed section names. */
233a11ab
CS
2672
2673static int
251d32d9
TG
2674section_is_p (const char *section_name,
2675 const struct dwarf2_section_names *names)
233a11ab 2676{
251d32d9
TG
2677 if (names->normal != NULL
2678 && strcmp (section_name, names->normal) == 0)
2679 return 1;
2680 if (names->compressed != NULL
2681 && strcmp (section_name, names->compressed) == 0)
2682 return 1;
2683 return 0;
233a11ab
CS
2684}
2685
330cdd98 2686/* See declaration. */
c906108c 2687
330cdd98
PA
2688void
2689dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2690 const dwarf2_debug_sections &names)
c906108c 2691{
dc7650b8 2692 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9 2693
dc7650b8
JK
2694 if ((aflag & SEC_HAS_CONTENTS) == 0)
2695 {
2696 }
330cdd98 2697 else if (section_is_p (sectp->name, &names.info))
c906108c 2698 {
330cdd98
PA
2699 this->info.s.section = sectp;
2700 this->info.size = bfd_get_section_size (sectp);
c906108c 2701 }
330cdd98 2702 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2703 {
330cdd98
PA
2704 this->abbrev.s.section = sectp;
2705 this->abbrev.size = bfd_get_section_size (sectp);
c906108c 2706 }
330cdd98 2707 else if (section_is_p (sectp->name, &names.line))
c906108c 2708 {
330cdd98
PA
2709 this->line.s.section = sectp;
2710 this->line.size = bfd_get_section_size (sectp);
c906108c 2711 }
330cdd98 2712 else if (section_is_p (sectp->name, &names.loc))
c906108c 2713 {
330cdd98
PA
2714 this->loc.s.section = sectp;
2715 this->loc.size = bfd_get_section_size (sectp);
c906108c 2716 }
330cdd98 2717 else if (section_is_p (sectp->name, &names.loclists))
43988095 2718 {
330cdd98
PA
2719 this->loclists.s.section = sectp;
2720 this->loclists.size = bfd_get_section_size (sectp);
43988095 2721 }
330cdd98 2722 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2723 {
330cdd98
PA
2724 this->macinfo.s.section = sectp;
2725 this->macinfo.size = bfd_get_section_size (sectp);
c906108c 2726 }
330cdd98 2727 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2728 {
330cdd98
PA
2729 this->macro.s.section = sectp;
2730 this->macro.size = bfd_get_section_size (sectp);
cf2c3c16 2731 }
330cdd98 2732 else if (section_is_p (sectp->name, &names.str))
c906108c 2733 {
330cdd98
PA
2734 this->str.s.section = sectp;
2735 this->str.size = bfd_get_section_size (sectp);
c906108c 2736 }
330cdd98 2737 else if (section_is_p (sectp->name, &names.line_str))
43988095 2738 {
330cdd98
PA
2739 this->line_str.s.section = sectp;
2740 this->line_str.size = bfd_get_section_size (sectp);
43988095 2741 }
330cdd98 2742 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2743 {
330cdd98
PA
2744 this->addr.s.section = sectp;
2745 this->addr.size = bfd_get_section_size (sectp);
3019eac3 2746 }
330cdd98 2747 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2748 {
330cdd98
PA
2749 this->frame.s.section = sectp;
2750 this->frame.size = bfd_get_section_size (sectp);
b6af0555 2751 }
330cdd98 2752 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2753 {
330cdd98
PA
2754 this->eh_frame.s.section = sectp;
2755 this->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2756 }
330cdd98 2757 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2758 {
330cdd98
PA
2759 this->ranges.s.section = sectp;
2760 this->ranges.size = bfd_get_section_size (sectp);
af34e669 2761 }
330cdd98 2762 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2763 {
330cdd98
PA
2764 this->rnglists.s.section = sectp;
2765 this->rnglists.size = bfd_get_section_size (sectp);
43988095 2766 }
330cdd98 2767 else if (section_is_p (sectp->name, &names.types))
348e048f 2768 {
8b70b953
TT
2769 struct dwarf2_section_info type_section;
2770
2771 memset (&type_section, 0, sizeof (type_section));
049412e3 2772 type_section.s.section = sectp;
8b70b953
TT
2773 type_section.size = bfd_get_section_size (sectp);
2774
330cdd98 2775 VEC_safe_push (dwarf2_section_info_def, this->types,
8b70b953 2776 &type_section);
348e048f 2777 }
330cdd98 2778 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2779 {
330cdd98
PA
2780 this->gdb_index.s.section = sectp;
2781 this->gdb_index.size = bfd_get_section_size (sectp);
9291a0cd 2782 }
927aa2e7
JK
2783 else if (section_is_p (sectp->name, &names.debug_names))
2784 {
2785 this->debug_names.s.section = sectp;
2786 this->debug_names.size = bfd_get_section_size (sectp);
2787 }
2788 else if (section_is_p (sectp->name, &names.debug_aranges))
2789 {
2790 this->debug_aranges.s.section = sectp;
2791 this->debug_aranges.size = bfd_get_section_size (sectp);
2792 }
dce234bc 2793
b4e1fd61 2794 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5 2795 && bfd_section_vma (abfd, sectp) == 0)
330cdd98 2796 this->has_section_at_zero = true;
c906108c
SS
2797}
2798
fceca515
DE
2799/* A helper function that decides whether a section is empty,
2800 or not present. */
9e0ac564
TT
2801
2802static int
19ac8c2e 2803dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2804{
73869dc2
DE
2805 if (section->is_virtual)
2806 return section->size == 0;
049412e3 2807 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2808}
2809
3019eac3
DE
2810/* Read the contents of the section INFO.
2811 OBJFILE is the main object file, but not necessarily the file where
a32a8923
DE
2812 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2813 of the DWO file.
dce234bc 2814 If the section is compressed, uncompress it before returning. */
c906108c 2815
dce234bc
PP
2816static void
2817dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 2818{
a32a8923 2819 asection *sectp;
3019eac3 2820 bfd *abfd;
dce234bc 2821 gdb_byte *buf, *retbuf;
c906108c 2822
be391dca
TT
2823 if (info->readin)
2824 return;
dce234bc 2825 info->buffer = NULL;
be391dca 2826 info->readin = 1;
188dd5d6 2827
9e0ac564 2828 if (dwarf2_section_empty_p (info))
dce234bc 2829 return;
c906108c 2830
a32a8923 2831 sectp = get_section_bfd_section (info);
3019eac3 2832
73869dc2
DE
2833 /* If this is a virtual section we need to read in the real one first. */
2834 if (info->is_virtual)
2835 {
2836 struct dwarf2_section_info *containing_section =
2837 get_containing_section (info);
2838
2839 gdb_assert (sectp != NULL);
2840 if ((sectp->flags & SEC_RELOC) != 0)
2841 {
2842 error (_("Dwarf Error: DWP format V2 with relocations is not"
2843 " supported in section %s [in module %s]"),
2844 get_section_name (info), get_section_file_name (info));
2845 }
2846 dwarf2_read_section (objfile, containing_section);
2847 /* Other code should have already caught virtual sections that don't
2848 fit. */
2849 gdb_assert (info->virtual_offset + info->size
2850 <= containing_section->size);
2851 /* If the real section is empty or there was a problem reading the
2852 section we shouldn't get here. */
2853 gdb_assert (containing_section->buffer != NULL);
2854 info->buffer = containing_section->buffer + info->virtual_offset;
2855 return;
2856 }
2857
4bf44c1c
TT
2858 /* If the section has relocations, we must read it ourselves.
2859 Otherwise we attach it to the BFD. */
2860 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2861 {
d521ce57 2862 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2863 return;
dce234bc 2864 }
dce234bc 2865
224c3ddb 2866 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2867 info->buffer = buf;
dce234bc
PP
2868
2869 /* When debugging .o files, we may need to apply relocations; see
2870 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2871 We never compress sections in .o files, so we only need to
2872 try this when the section is not compressed. */
ac8035ab 2873 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2874 if (retbuf != NULL)
2875 {
2876 info->buffer = retbuf;
2877 return;
2878 }
2879
a32a8923
DE
2880 abfd = get_section_bfd_owner (info);
2881 gdb_assert (abfd != NULL);
2882
dce234bc
PP
2883 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2884 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2885 {
2886 error (_("Dwarf Error: Can't read DWARF data"
2887 " in section %s [in module %s]"),
2888 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2889 }
dce234bc
PP
2890}
2891
9e0ac564
TT
2892/* A helper function that returns the size of a section in a safe way.
2893 If you are positive that the section has been read before using the
2894 size, then it is safe to refer to the dwarf2_section_info object's
2895 "size" field directly. In other cases, you must call this
2896 function, because for compressed sections the size field is not set
2897 correctly until the section has been read. */
2898
2899static bfd_size_type
2900dwarf2_section_size (struct objfile *objfile,
2901 struct dwarf2_section_info *info)
2902{
2903 if (!info->readin)
2904 dwarf2_read_section (objfile, info);
2905 return info->size;
2906}
2907
dce234bc 2908/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2909 SECTION_NAME. */
af34e669 2910
dce234bc 2911void
3017a003
TG
2912dwarf2_get_section_info (struct objfile *objfile,
2913 enum dwarf2_section_enum sect,
d521ce57 2914 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2915 bfd_size_type *sizep)
2916{
2917 struct dwarf2_per_objfile *data
9a3c8263
SM
2918 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2919 dwarf2_objfile_data_key);
dce234bc 2920 struct dwarf2_section_info *info;
a3b2a86b
TT
2921
2922 /* We may see an objfile without any DWARF, in which case we just
2923 return nothing. */
2924 if (data == NULL)
2925 {
2926 *sectp = NULL;
2927 *bufp = NULL;
2928 *sizep = 0;
2929 return;
2930 }
3017a003
TG
2931 switch (sect)
2932 {
2933 case DWARF2_DEBUG_FRAME:
2934 info = &data->frame;
2935 break;
2936 case DWARF2_EH_FRAME:
2937 info = &data->eh_frame;
2938 break;
2939 default:
2940 gdb_assert_not_reached ("unexpected section");
2941 }
dce234bc 2942
9e0ac564 2943 dwarf2_read_section (objfile, info);
dce234bc 2944
a32a8923 2945 *sectp = get_section_bfd_section (info);
dce234bc
PP
2946 *bufp = info->buffer;
2947 *sizep = info->size;
2948}
2949
36586728
TT
2950/* A helper function to find the sections for a .dwz file. */
2951
2952static void
2953locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2954{
9a3c8263 2955 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2956
2957 /* Note that we only support the standard ELF names, because .dwz
2958 is ELF-only (at the time of writing). */
2959 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2960 {
049412e3 2961 dwz_file->abbrev.s.section = sectp;
36586728
TT
2962 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2963 }
2964 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2965 {
049412e3 2966 dwz_file->info.s.section = sectp;
36586728
TT
2967 dwz_file->info.size = bfd_get_section_size (sectp);
2968 }
2969 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2970 {
049412e3 2971 dwz_file->str.s.section = sectp;
36586728
TT
2972 dwz_file->str.size = bfd_get_section_size (sectp);
2973 }
2974 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2975 {
049412e3 2976 dwz_file->line.s.section = sectp;
36586728
TT
2977 dwz_file->line.size = bfd_get_section_size (sectp);
2978 }
2979 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2980 {
049412e3 2981 dwz_file->macro.s.section = sectp;
36586728
TT
2982 dwz_file->macro.size = bfd_get_section_size (sectp);
2983 }
2ec9a5e0
TT
2984 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2985 {
049412e3 2986 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2987 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2988 }
927aa2e7
JK
2989 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2990 {
2991 dwz_file->debug_names.s.section = sectp;
2992 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2993 }
36586728
TT
2994}
2995
4db1a1dc
TT
2996/* Open the separate '.dwz' debug file, if needed. Return NULL if
2997 there is no .gnu_debugaltlink section in the file. Error if there
2998 is such a section but the file cannot be found. */
36586728
TT
2999
3000static struct dwz_file *
ed2dc618 3001dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 3002{
36586728
TT
3003 const char *filename;
3004 struct dwz_file *result;
acd13123 3005 bfd_size_type buildid_len_arg;
dc294be5
TT
3006 size_t buildid_len;
3007 bfd_byte *buildid;
36586728
TT
3008
3009 if (dwarf2_per_objfile->dwz_file != NULL)
3010 return dwarf2_per_objfile->dwz_file;
3011
4db1a1dc 3012 bfd_set_error (bfd_error_no_error);
791afaa2
TT
3013 gdb::unique_xmalloc_ptr<char> data
3014 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
3015 &buildid_len_arg, &buildid));
4db1a1dc
TT
3016 if (data == NULL)
3017 {
3018 if (bfd_get_error () == bfd_error_no_error)
3019 return NULL;
3020 error (_("could not read '.gnu_debugaltlink' section: %s"),
3021 bfd_errmsg (bfd_get_error ()));
3022 }
791afaa2
TT
3023
3024 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 3025
acd13123
TT
3026 buildid_len = (size_t) buildid_len_arg;
3027
791afaa2 3028 filename = data.get ();
d721ba37
PA
3029
3030 std::string abs_storage;
36586728
TT
3031 if (!IS_ABSOLUTE_PATH (filename))
3032 {
14278e1f
TT
3033 gdb::unique_xmalloc_ptr<char> abs
3034 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 3035
14278e1f 3036 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 3037 filename = abs_storage.c_str ();
36586728
TT
3038 }
3039
dc294be5
TT
3040 /* First try the file name given in the section. If that doesn't
3041 work, try to use the build-id instead. */
192b62ce 3042 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 3043 if (dwz_bfd != NULL)
36586728 3044 {
192b62ce
TT
3045 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
3046 dwz_bfd.release ();
36586728
TT
3047 }
3048
dc294be5
TT
3049 if (dwz_bfd == NULL)
3050 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
3051
3052 if (dwz_bfd == NULL)
3053 error (_("could not find '.gnu_debugaltlink' file for %s"),
3054 objfile_name (dwarf2_per_objfile->objfile));
3055
36586728
TT
3056 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
3057 struct dwz_file);
192b62ce 3058 result->dwz_bfd = dwz_bfd.release ();
36586728 3059
192b62ce 3060 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
36586728 3061
192b62ce 3062 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
8d2cc612 3063 dwarf2_per_objfile->dwz_file = result;
36586728
TT
3064 return result;
3065}
9291a0cd 3066\f
7b9f3c50
DE
3067/* DWARF quick_symbols_functions support. */
3068
3069/* TUs can share .debug_line entries, and there can be a lot more TUs than
3070 unique line tables, so we maintain a separate table of all .debug_line
3071 derived entries to support the sharing.
3072 All the quick functions need is the list of file names. We discard the
3073 line_header when we're done and don't need to record it here. */
3074struct quick_file_names
3075{
094b34ac
DE
3076 /* The data used to construct the hash key. */
3077 struct stmt_list_hash hash;
7b9f3c50
DE
3078
3079 /* The number of entries in file_names, real_names. */
3080 unsigned int num_file_names;
3081
3082 /* The file names from the line table, after being run through
3083 file_full_name. */
3084 const char **file_names;
3085
3086 /* The file names from the line table after being run through
3087 gdb_realpath. These are computed lazily. */
3088 const char **real_names;
3089};
3090
3091/* When using the index (and thus not using psymtabs), each CU has an
3092 object of this type. This is used to hold information needed by
3093 the various "quick" methods. */
3094struct dwarf2_per_cu_quick_data
3095{
3096 /* The file table. This can be NULL if there was no file table
3097 or it's currently not read in.
3098 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
3099 struct quick_file_names *file_names;
3100
3101 /* The corresponding symbol table. This is NULL if symbols for this
3102 CU have not yet been read. */
43f3e411 3103 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
3104
3105 /* A temporary mark bit used when iterating over all CUs in
3106 expand_symtabs_matching. */
3107 unsigned int mark : 1;
3108
3109 /* True if we've tried to read the file table and found there isn't one.
3110 There will be no point in trying to read it again next time. */
3111 unsigned int no_file_data : 1;
3112};
3113
094b34ac
DE
3114/* Utility hash function for a stmt_list_hash. */
3115
3116static hashval_t
3117hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
3118{
3119 hashval_t v = 0;
3120
3121 if (stmt_list_hash->dwo_unit != NULL)
3122 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 3123 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
3124 return v;
3125}
3126
3127/* Utility equality function for a stmt_list_hash. */
3128
3129static int
3130eq_stmt_list_entry (const struct stmt_list_hash *lhs,
3131 const struct stmt_list_hash *rhs)
3132{
3133 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
3134 return 0;
3135 if (lhs->dwo_unit != NULL
3136 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
3137 return 0;
3138
9c541725 3139 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
3140}
3141
7b9f3c50
DE
3142/* Hash function for a quick_file_names. */
3143
3144static hashval_t
3145hash_file_name_entry (const void *e)
3146{
9a3c8263
SM
3147 const struct quick_file_names *file_data
3148 = (const struct quick_file_names *) e;
7b9f3c50 3149
094b34ac 3150 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
3151}
3152
3153/* Equality function for a quick_file_names. */
3154
3155static int
3156eq_file_name_entry (const void *a, const void *b)
3157{
9a3c8263
SM
3158 const struct quick_file_names *ea = (const struct quick_file_names *) a;
3159 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 3160
094b34ac 3161 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
3162}
3163
3164/* Delete function for a quick_file_names. */
3165
3166static void
3167delete_file_name_entry (void *e)
3168{
9a3c8263 3169 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
3170 int i;
3171
3172 for (i = 0; i < file_data->num_file_names; ++i)
3173 {
3174 xfree ((void*) file_data->file_names[i]);
3175 if (file_data->real_names)
3176 xfree ((void*) file_data->real_names[i]);
3177 }
3178
3179 /* The space for the struct itself lives on objfile_obstack,
3180 so we don't free it here. */
3181}
3182
3183/* Create a quick_file_names hash table. */
3184
3185static htab_t
3186create_quick_file_names_table (unsigned int nr_initial_entries)
3187{
3188 return htab_create_alloc (nr_initial_entries,
3189 hash_file_name_entry, eq_file_name_entry,
3190 delete_file_name_entry, xcalloc, xfree);
3191}
9291a0cd 3192
918dd910
JK
3193/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
3194 have to be created afterwards. You should call age_cached_comp_units after
3195 processing PER_CU->CU. dw2_setup must have been already called. */
3196
3197static void
3198load_cu (struct dwarf2_per_cu_data *per_cu)
3199{
3019eac3 3200 if (per_cu->is_debug_types)
e5fe5e75 3201 load_full_type_unit (per_cu);
918dd910 3202 else
95554aad 3203 load_full_comp_unit (per_cu, language_minimal);
918dd910 3204
cc12ce38
DE
3205 if (per_cu->cu == NULL)
3206 return; /* Dummy CU. */
2dc860c0
DE
3207
3208 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
3209}
3210
a0f42c21 3211/* Read in the symbols for PER_CU. */
2fdf6df6 3212
9291a0cd 3213static void
a0f42c21 3214dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 3215{
ed2dc618 3216 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 3217
f4dc4d17
DE
3218 /* Skip type_unit_groups, reading the type units they contain
3219 is handled elsewhere. */
3220 if (IS_TYPE_UNIT_GROUP (per_cu))
3221 return;
3222
b303c6f6
AB
3223 /* The destructor of dwarf2_queue_guard frees any entries left on
3224 the queue. After this point we're guaranteed to leave this function
3225 with the dwarf queue empty. */
3226 dwarf2_queue_guard q_guard;
9291a0cd 3227
95554aad 3228 if (dwarf2_per_objfile->using_index
43f3e411 3229 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
3230 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
3231 {
3232 queue_comp_unit (per_cu, language_minimal);
3233 load_cu (per_cu);
89e63ee4
DE
3234
3235 /* If we just loaded a CU from a DWO, and we're working with an index
3236 that may badly handle TUs, load all the TUs in that DWO as well.
3237 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
3238 if (!per_cu->is_debug_types
cc12ce38 3239 && per_cu->cu != NULL
89e63ee4
DE
3240 && per_cu->cu->dwo_unit != NULL
3241 && dwarf2_per_objfile->index_table != NULL
3242 && dwarf2_per_objfile->index_table->version <= 7
3243 /* DWP files aren't supported yet. */
ed2dc618 3244 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 3245 queue_and_load_all_dwo_tus (per_cu);
95554aad 3246 }
9291a0cd 3247
ed2dc618 3248 process_queue (dwarf2_per_objfile);
9291a0cd
TT
3249
3250 /* Age the cache, releasing compilation units that have not
3251 been used recently. */
ed2dc618 3252 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
3253}
3254
3255/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
3256 the objfile from which this CU came. Returns the resulting symbol
3257 table. */
2fdf6df6 3258
43f3e411 3259static struct compunit_symtab *
a0f42c21 3260dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 3261{
ed2dc618
SM
3262 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3263
95554aad 3264 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 3265 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 3266 {
ed2dc618
SM
3267 struct cleanup *back_to = make_cleanup (free_cached_comp_units,
3268 dwarf2_per_objfile);
c83dd867 3269 scoped_restore decrementer = increment_reading_symtab ();
a0f42c21 3270 dw2_do_instantiate_symtab (per_cu);
ed2dc618 3271 process_cu_includes (dwarf2_per_objfile);
9291a0cd
TT
3272 do_cleanups (back_to);
3273 }
f194fefb 3274
43f3e411 3275 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
3276}
3277
8832e7e3 3278/* Return the CU/TU given its index.
f4dc4d17
DE
3279
3280 This is intended for loops like:
3281
3282 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3283 + dwarf2_per_objfile->n_type_units); ++i)
3284 {
8832e7e3 3285 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
f4dc4d17
DE
3286
3287 ...;
3288 }
3289*/
2fdf6df6 3290
1fd400ff 3291static struct dwarf2_per_cu_data *
ed2dc618
SM
3292dw2_get_cutu (struct dwarf2_per_objfile *dwarf2_per_objfile,
3293 int index)
1fd400ff
TT
3294{
3295 if (index >= dwarf2_per_objfile->n_comp_units)
3296 {
f4dc4d17 3297 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
3298 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3299 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
3300 }
3301
3302 return dwarf2_per_objfile->all_comp_units[index];
3303}
3304
8832e7e3
DE
3305/* Return the CU given its index.
3306 This differs from dw2_get_cutu in that it's for when you know INDEX
3307 refers to a CU. */
f4dc4d17
DE
3308
3309static struct dwarf2_per_cu_data *
ed2dc618 3310dw2_get_cu (struct dwarf2_per_objfile *dwarf2_per_objfile, int index)
f4dc4d17 3311{
8832e7e3 3312 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
f4dc4d17 3313
1fd400ff
TT
3314 return dwarf2_per_objfile->all_comp_units[index];
3315}
3316
4b514bc8
JK
3317/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
3318 objfile_obstack, and constructed with the specified field
3319 values. */
3320
3321static dwarf2_per_cu_data *
ed2dc618 3322create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
3323 struct dwarf2_section_info *section,
3324 int is_dwz,
3325 sect_offset sect_off, ULONGEST length)
3326{
ed2dc618 3327 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
3328 dwarf2_per_cu_data *the_cu
3329 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3330 struct dwarf2_per_cu_data);
3331 the_cu->sect_off = sect_off;
3332 the_cu->length = length;
e3b94546 3333 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
3334 the_cu->section = section;
3335 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3336 struct dwarf2_per_cu_quick_data);
3337 the_cu->is_dwz = is_dwz;
3338 return the_cu;
3339}
3340
2ec9a5e0
TT
3341/* A helper for create_cus_from_index that handles a given list of
3342 CUs. */
2fdf6df6 3343
74a0d9f6 3344static void
2ec9a5e0
TT
3345create_cus_from_index_list (struct objfile *objfile,
3346 const gdb_byte *cu_list, offset_type n_elements,
3347 struct dwarf2_section_info *section,
3348 int is_dwz,
3349 int base_offset)
9291a0cd
TT
3350{
3351 offset_type i;
ed2dc618
SM
3352 struct dwarf2_per_objfile *dwarf2_per_objfile
3353 = get_dwarf2_per_objfile (objfile);
9291a0cd 3354
2ec9a5e0 3355 for (i = 0; i < n_elements; i += 2)
9291a0cd 3356 {
74a0d9f6 3357 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3358
3359 sect_offset sect_off
3360 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3361 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
3362 cu_list += 2 * 8;
3363
4b514bc8 3364 dwarf2_per_objfile->all_comp_units[base_offset + i / 2]
ed2dc618
SM
3365 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3366 sect_off, length);
9291a0cd 3367 }
9291a0cd
TT
3368}
3369
2ec9a5e0 3370/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3371 the CU objects for this objfile. */
2ec9a5e0 3372
74a0d9f6 3373static void
2ec9a5e0
TT
3374create_cus_from_index (struct objfile *objfile,
3375 const gdb_byte *cu_list, offset_type cu_list_elements,
3376 const gdb_byte *dwz_list, offset_type dwz_elements)
3377{
3378 struct dwz_file *dwz;
ed2dc618
SM
3379 struct dwarf2_per_objfile *dwarf2_per_objfile
3380 = get_dwarf2_per_objfile (objfile);
2ec9a5e0
TT
3381
3382 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
8d749320
SM
3383 dwarf2_per_objfile->all_comp_units =
3384 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3385 dwarf2_per_objfile->n_comp_units);
2ec9a5e0 3386
74a0d9f6
JK
3387 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3388 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
3389
3390 if (dwz_elements == 0)
74a0d9f6 3391 return;
2ec9a5e0 3392
ed2dc618 3393 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
74a0d9f6
JK
3394 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3395 cu_list_elements / 2);
2ec9a5e0
TT
3396}
3397
1fd400ff 3398/* Create the signatured type hash table from the index. */
673bfd45 3399
74a0d9f6 3400static void
673bfd45 3401create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 3402 struct dwarf2_section_info *section,
673bfd45
DE
3403 const gdb_byte *bytes,
3404 offset_type elements)
1fd400ff
TT
3405{
3406 offset_type i;
673bfd45 3407 htab_t sig_types_hash;
ed2dc618
SM
3408 struct dwarf2_per_objfile *dwarf2_per_objfile
3409 = get_dwarf2_per_objfile (objfile);
1fd400ff 3410
6aa5f3a6
DE
3411 dwarf2_per_objfile->n_type_units
3412 = dwarf2_per_objfile->n_allocated_type_units
3413 = elements / 3;
8d749320
SM
3414 dwarf2_per_objfile->all_type_units =
3415 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
1fd400ff 3416
673bfd45 3417 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
3418
3419 for (i = 0; i < elements; i += 3)
3420 {
52dc124a 3421 struct signatured_type *sig_type;
9c541725 3422 ULONGEST signature;
1fd400ff 3423 void **slot;
9c541725 3424 cu_offset type_offset_in_tu;
1fd400ff 3425
74a0d9f6 3426 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3427 sect_offset sect_off
3428 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3429 type_offset_in_tu
3430 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3431 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3432 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3433 bytes += 3 * 8;
3434
52dc124a 3435 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3436 struct signatured_type);
52dc124a 3437 sig_type->signature = signature;
9c541725 3438 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3439 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3440 sig_type->per_cu.section = section;
9c541725 3441 sig_type->per_cu.sect_off = sect_off;
e3b94546 3442 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 3443 sig_type->per_cu.v.quick
1fd400ff
TT
3444 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3445 struct dwarf2_per_cu_quick_data);
3446
52dc124a
DE
3447 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3448 *slot = sig_type;
1fd400ff 3449
b4dd5633 3450 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
3451 }
3452
673bfd45 3453 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3454}
3455
927aa2e7
JK
3456/* Create the signatured type hash table from .debug_names. */
3457
3458static void
3459create_signatured_type_table_from_debug_names
ed2dc618 3460 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3461 const mapped_debug_names &map,
3462 struct dwarf2_section_info *section,
3463 struct dwarf2_section_info *abbrev_section)
3464{
ed2dc618
SM
3465 struct objfile *objfile = dwarf2_per_objfile->objfile;
3466
927aa2e7
JK
3467 dwarf2_read_section (objfile, section);
3468 dwarf2_read_section (objfile, abbrev_section);
3469
3470 dwarf2_per_objfile->n_type_units
3471 = dwarf2_per_objfile->n_allocated_type_units
3472 = map.tu_count;
3473 dwarf2_per_objfile->all_type_units
3474 = XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3475
3476 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3477
3478 for (uint32_t i = 0; i < map.tu_count; ++i)
3479 {
3480 struct signatured_type *sig_type;
3481 ULONGEST signature;
3482 void **slot;
3483 cu_offset type_offset_in_tu;
3484
3485 sect_offset sect_off
3486 = (sect_offset) (extract_unsigned_integer
3487 (map.tu_table_reordered + i * map.offset_size,
3488 map.offset_size,
3489 map.dwarf5_byte_order));
3490
3491 comp_unit_head cu_header;
ed2dc618
SM
3492 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3493 abbrev_section,
927aa2e7
JK
3494 section->buffer + to_underlying (sect_off),
3495 rcuh_kind::TYPE);
3496
3497 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3498 struct signatured_type);
3499 sig_type->signature = cu_header.signature;
3500 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3501 sig_type->per_cu.is_debug_types = 1;
3502 sig_type->per_cu.section = section;
3503 sig_type->per_cu.sect_off = sect_off;
e3b94546 3504 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
3505 sig_type->per_cu.v.quick
3506 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3507 struct dwarf2_per_cu_quick_data);
3508
3509 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3510 *slot = sig_type;
3511
3512 dwarf2_per_objfile->all_type_units[i] = sig_type;
3513 }
3514
3515 dwarf2_per_objfile->signatured_types = sig_types_hash;
3516}
3517
9291a0cd
TT
3518/* Read the address map data from the mapped index, and use it to
3519 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3520
9291a0cd 3521static void
ed2dc618
SM
3522create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3523 struct mapped_index *index)
9291a0cd 3524{
ed2dc618 3525 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 3526 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3527 const gdb_byte *iter, *end;
9291a0cd 3528 struct addrmap *mutable_map;
9291a0cd
TT
3529 CORE_ADDR baseaddr;
3530
8268c778
PA
3531 auto_obstack temp_obstack;
3532
9291a0cd
TT
3533 mutable_map = addrmap_create_mutable (&temp_obstack);
3534
f00a2de2
PA
3535 iter = index->address_table.data ();
3536 end = iter + index->address_table.size ();
9291a0cd
TT
3537
3538 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3539
3540 while (iter < end)
3541 {
3542 ULONGEST hi, lo, cu_index;
3543 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3544 iter += 8;
3545 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3546 iter += 8;
3547 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3548 iter += 4;
f652bce2 3549
24a55014 3550 if (lo > hi)
f652bce2 3551 {
24a55014
DE
3552 complaint (&symfile_complaints,
3553 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3554 hex_string (lo), hex_string (hi));
24a55014 3555 continue;
f652bce2 3556 }
24a55014
DE
3557
3558 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
3559 {
3560 complaint (&symfile_complaints,
3561 _(".gdb_index address table has invalid CU number %u"),
3562 (unsigned) cu_index);
24a55014 3563 continue;
f652bce2 3564 }
24a55014 3565
3e29f34a
MR
3566 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3567 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
ed2dc618
SM
3568 addrmap_set_empty (mutable_map, lo, hi - 1,
3569 dw2_get_cutu (dwarf2_per_objfile, cu_index));
9291a0cd
TT
3570 }
3571
3572 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3573 &objfile->objfile_obstack);
9291a0cd
TT
3574}
3575
927aa2e7
JK
3576/* Read the address map data from DWARF-5 .debug_aranges, and use it to
3577 populate the objfile's psymtabs_addrmap. */
3578
3579static void
ed2dc618 3580create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3581 struct dwarf2_section_info *section)
3582{
ed2dc618 3583 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
3584 bfd *abfd = objfile->obfd;
3585 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3586 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3587 SECT_OFF_TEXT (objfile));
3588
3589 auto_obstack temp_obstack;
3590 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3591
3592 std::unordered_map<sect_offset,
3593 dwarf2_per_cu_data *,
3594 gdb::hash_enum<sect_offset>>
3595 debug_info_offset_to_per_cu;
3596 for (int cui = 0; cui < dwarf2_per_objfile->n_comp_units; ++cui)
3597 {
ed2dc618 3598 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, cui);
927aa2e7
JK
3599 const auto insertpair
3600 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3601 if (!insertpair.second)
3602 {
3603 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
3604 "debug_info_offset %s, ignoring .debug_aranges."),
3605 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
3606 return;
3607 }
3608 }
3609
3610 dwarf2_read_section (objfile, section);
3611
3612 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3613
3614 const gdb_byte *addr = section->buffer;
3615
3616 while (addr < section->buffer + section->size)
3617 {
3618 const gdb_byte *const entry_addr = addr;
3619 unsigned int bytes_read;
3620
3621 const LONGEST entry_length = read_initial_length (abfd, addr,
3622 &bytes_read);
3623 addr += bytes_read;
3624
3625 const gdb_byte *const entry_end = addr + entry_length;
3626 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3627 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3628 if (addr + entry_length > section->buffer + section->size)
3629 {
3630 warning (_("Section .debug_aranges in %s entry at offset %zu "
3631 "length %s exceeds section length %s, "
3632 "ignoring .debug_aranges."),
3633 objfile_name (objfile), entry_addr - section->buffer,
3634 plongest (bytes_read + entry_length),
3635 pulongest (section->size));
3636 return;
3637 }
3638
3639 /* The version number. */
3640 const uint16_t version = read_2_bytes (abfd, addr);
3641 addr += 2;
3642 if (version != 2)
3643 {
3644 warning (_("Section .debug_aranges in %s entry at offset %zu "
3645 "has unsupported version %d, ignoring .debug_aranges."),
3646 objfile_name (objfile), entry_addr - section->buffer,
3647 version);
3648 return;
3649 }
3650
3651 const uint64_t debug_info_offset
3652 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3653 addr += offset_size;
3654 const auto per_cu_it
3655 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3656 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3657 {
3658 warning (_("Section .debug_aranges in %s entry at offset %zu "
3659 "debug_info_offset %s does not exists, "
3660 "ignoring .debug_aranges."),
3661 objfile_name (objfile), entry_addr - section->buffer,
3662 pulongest (debug_info_offset));
3663 return;
3664 }
3665 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3666
3667 const uint8_t address_size = *addr++;
3668 if (address_size < 1 || address_size > 8)
3669 {
3670 warning (_("Section .debug_aranges in %s entry at offset %zu "
3671 "address_size %u is invalid, ignoring .debug_aranges."),
3672 objfile_name (objfile), entry_addr - section->buffer,
3673 address_size);
3674 return;
3675 }
3676
3677 const uint8_t segment_selector_size = *addr++;
3678 if (segment_selector_size != 0)
3679 {
3680 warning (_("Section .debug_aranges in %s entry at offset %zu "
3681 "segment_selector_size %u is not supported, "
3682 "ignoring .debug_aranges."),
3683 objfile_name (objfile), entry_addr - section->buffer,
3684 segment_selector_size);
3685 return;
3686 }
3687
3688 /* Must pad to an alignment boundary that is twice the address
3689 size. It is undocumented by the DWARF standard but GCC does
3690 use it. */
3691 for (size_t padding = ((-(addr - section->buffer))
3692 & (2 * address_size - 1));
3693 padding > 0; padding--)
3694 if (*addr++ != 0)
3695 {
3696 warning (_("Section .debug_aranges in %s entry at offset %zu "
3697 "padding is not zero, ignoring .debug_aranges."),
3698 objfile_name (objfile), entry_addr - section->buffer);
3699 return;
3700 }
3701
3702 for (;;)
3703 {
3704 if (addr + 2 * address_size > entry_end)
3705 {
3706 warning (_("Section .debug_aranges in %s entry at offset %zu "
3707 "address list is not properly terminated, "
3708 "ignoring .debug_aranges."),
3709 objfile_name (objfile), entry_addr - section->buffer);
3710 return;
3711 }
3712 ULONGEST start = extract_unsigned_integer (addr, address_size,
3713 dwarf5_byte_order);
3714 addr += address_size;
3715 ULONGEST length = extract_unsigned_integer (addr, address_size,
3716 dwarf5_byte_order);
3717 addr += address_size;
3718 if (start == 0 && length == 0)
3719 break;
3720 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3721 {
3722 /* Symbol was eliminated due to a COMDAT group. */
3723 continue;
3724 }
3725 ULONGEST end = start + length;
3726 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3727 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3728 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3729 }
3730 }
3731
3732 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3733 &objfile->objfile_obstack);
3734}
3735
59d7bcaf
JK
3736/* The hash function for strings in the mapped index. This is the same as
3737 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3738 implementation. This is necessary because the hash function is tied to the
3739 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
3740 SYMBOL_HASH_NEXT.
3741
3742 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 3743
9291a0cd 3744static hashval_t
559a7a62 3745mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
3746{
3747 const unsigned char *str = (const unsigned char *) p;
3748 hashval_t r = 0;
3749 unsigned char c;
3750
3751 while ((c = *str++) != 0)
559a7a62
JK
3752 {
3753 if (index_version >= 5)
3754 c = tolower (c);
3755 r = r * 67 + c - 113;
3756 }
9291a0cd
TT
3757
3758 return r;
3759}
3760
3761/* Find a slot in the mapped index INDEX for the object named NAME.
3762 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3763 constant pool and return true. If NAME cannot be found, return
3764 false. */
2fdf6df6 3765
109483d9 3766static bool
9291a0cd
TT
3767find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3768 offset_type **vec_out)
3769{
0cf03b49 3770 offset_type hash;
9291a0cd 3771 offset_type slot, step;
559a7a62 3772 int (*cmp) (const char *, const char *);
9291a0cd 3773
791afaa2 3774 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3775 if (current_language->la_language == language_cplus
45280282
IB
3776 || current_language->la_language == language_fortran
3777 || current_language->la_language == language_d)
0cf03b49
JK
3778 {
3779 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3780 not contain any. */
a8719064 3781
72998fb3 3782 if (strchr (name, '(') != NULL)
0cf03b49 3783 {
109483d9 3784 without_params = cp_remove_params (name);
0cf03b49 3785
72998fb3 3786 if (without_params != NULL)
791afaa2 3787 name = without_params.get ();
0cf03b49
JK
3788 }
3789 }
3790
559a7a62 3791 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3792 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3793 simulate our NAME being searched is also lowercased. */
3794 hash = mapped_index_string_hash ((index->version == 4
3795 && case_sensitivity == case_sensitive_off
3796 ? 5 : index->version),
3797 name);
3798
f00a2de2
PA
3799 slot = hash & (index->symbol_table.size () - 1);
3800 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3801 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3802
3803 for (;;)
3804 {
9291a0cd 3805 const char *str;
f00a2de2
PA
3806
3807 const auto &bucket = index->symbol_table[slot];
3808 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3809 return false;
9291a0cd 3810
f00a2de2 3811 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3812 if (!cmp (name, str))
9291a0cd
TT
3813 {
3814 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3815 + MAYBE_SWAP (bucket.vec));
109483d9 3816 return true;
9291a0cd
TT
3817 }
3818
f00a2de2 3819 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3820 }
3821}
3822
2ec9a5e0
TT
3823/* A helper function that reads the .gdb_index from SECTION and fills
3824 in MAP. FILENAME is the name of the file containing the section;
3825 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3826 ok to use deprecated sections.
3827
3828 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3829 out parameters that are filled in with information about the CU and
3830 TU lists in the section.
3831
3832 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3833
9291a0cd 3834static int
2ec9a5e0
TT
3835read_index_from_section (struct objfile *objfile,
3836 const char *filename,
3837 int deprecated_ok,
3838 struct dwarf2_section_info *section,
3839 struct mapped_index *map,
3840 const gdb_byte **cu_list,
3841 offset_type *cu_list_elements,
3842 const gdb_byte **types_list,
3843 offset_type *types_list_elements)
9291a0cd 3844{
948f8e3d 3845 const gdb_byte *addr;
2ec9a5e0 3846 offset_type version;
b3b272e1 3847 offset_type *metadata;
1fd400ff 3848 int i;
9291a0cd 3849
2ec9a5e0 3850 if (dwarf2_section_empty_p (section))
9291a0cd 3851 return 0;
82430852
JK
3852
3853 /* Older elfutils strip versions could keep the section in the main
3854 executable while splitting it for the separate debug info file. */
a32a8923 3855 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3856 return 0;
3857
2ec9a5e0 3858 dwarf2_read_section (objfile, section);
9291a0cd 3859
2ec9a5e0 3860 addr = section->buffer;
9291a0cd 3861 /* Version check. */
1fd400ff 3862 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3863 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3864 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3865 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3866 indices. */
831adc1f 3867 if (version < 4)
481860b3
GB
3868 {
3869 static int warning_printed = 0;
3870 if (!warning_printed)
3871 {
3872 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3873 filename);
481860b3
GB
3874 warning_printed = 1;
3875 }
3876 return 0;
3877 }
3878 /* Index version 4 uses a different hash function than index version
3879 5 and later.
3880
3881 Versions earlier than 6 did not emit psymbols for inlined
3882 functions. Using these files will cause GDB not to be able to
3883 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3884 indices unless the user has done
3885 "set use-deprecated-index-sections on". */
2ec9a5e0 3886 if (version < 6 && !deprecated_ok)
481860b3
GB
3887 {
3888 static int warning_printed = 0;
3889 if (!warning_printed)
3890 {
e615022a
DE
3891 warning (_("\
3892Skipping deprecated .gdb_index section in %s.\n\
3893Do \"set use-deprecated-index-sections on\" before the file is read\n\
3894to use the section anyway."),
2ec9a5e0 3895 filename);
481860b3
GB
3896 warning_printed = 1;
3897 }
3898 return 0;
3899 }
796a7ff8 3900 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3901 of the TU (for symbols coming from TUs),
3902 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3903 Plus gold-generated indices can have duplicate entries for global symbols,
3904 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3905 These are just performance bugs, and we can't distinguish gdb-generated
3906 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3907
481860b3 3908 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3909 longer backward compatible. */
796a7ff8 3910 if (version > 8)
594e8718 3911 return 0;
9291a0cd 3912
559a7a62 3913 map->version = version;
2ec9a5e0 3914 map->total_size = section->size;
9291a0cd
TT
3915
3916 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3917
3918 i = 0;
2ec9a5e0
TT
3919 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3920 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3921 / 8);
1fd400ff
TT
3922 ++i;
3923
2ec9a5e0
TT
3924 *types_list = addr + MAYBE_SWAP (metadata[i]);
3925 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3926 - MAYBE_SWAP (metadata[i]))
3927 / 8);
987d643c 3928 ++i;
1fd400ff 3929
f00a2de2
PA
3930 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3931 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3932 map->address_table
3933 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3934 ++i;
3935
f00a2de2
PA
3936 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3937 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3938 map->symbol_table
3939 = gdb::array_view<mapped_index::symbol_table_slot>
3940 ((mapped_index::symbol_table_slot *) symbol_table,
3941 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3942
f00a2de2 3943 ++i;
f9d83a0b 3944 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3945
2ec9a5e0
TT
3946 return 1;
3947}
3948
927aa2e7 3949/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3950 elements of all the CUs and return 1. Otherwise, return 0. */
3951
3952static int
3953dwarf2_read_index (struct objfile *objfile)
3954{
3955 struct mapped_index local_map, *map;
3956 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3957 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3958 struct dwz_file *dwz;
ed2dc618
SM
3959 struct dwarf2_per_objfile *dwarf2_per_objfile
3960 = get_dwarf2_per_objfile (objfile);
2ec9a5e0 3961
4262abfb 3962 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0
TT
3963 use_deprecated_index_sections,
3964 &dwarf2_per_objfile->gdb_index, &local_map,
3965 &cu_list, &cu_list_elements,
3966 &types_list, &types_list_elements))
3967 return 0;
3968
0fefef59 3969 /* Don't use the index if it's empty. */
f00a2de2 3970 if (local_map.symbol_table.empty ())
0fefef59
DE
3971 return 0;
3972
2ec9a5e0
TT
3973 /* If there is a .dwz file, read it so we can get its CU list as
3974 well. */
ed2dc618 3975 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3976 if (dwz != NULL)
2ec9a5e0 3977 {
2ec9a5e0
TT
3978 struct mapped_index dwz_map;
3979 const gdb_byte *dwz_types_ignore;
3980 offset_type dwz_types_elements_ignore;
3981
3982 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3983 1,
3984 &dwz->gdb_index, &dwz_map,
3985 &dwz_list, &dwz_list_elements,
3986 &dwz_types_ignore,
3987 &dwz_types_elements_ignore))
3988 {
3989 warning (_("could not read '.gdb_index' section from %s; skipping"),
3990 bfd_get_filename (dwz->dwz_bfd));
3991 return 0;
3992 }
3993 }
3994
74a0d9f6
JK
3995 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3996 dwz_list_elements);
1fd400ff 3997
8b70b953
TT
3998 if (types_list_elements)
3999 {
4000 struct dwarf2_section_info *section;
4001
4002 /* We can only handle a single .debug_types when we have an
4003 index. */
4004 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
4005 return 0;
4006
4007 section = VEC_index (dwarf2_section_info_def,
4008 dwarf2_per_objfile->types, 0);
4009
74a0d9f6
JK
4010 create_signatured_type_table_from_index (objfile, section, types_list,
4011 types_list_elements);
8b70b953 4012 }
9291a0cd 4013
ed2dc618 4014 create_addrmap_from_index (dwarf2_per_objfile, &local_map);
2ec9a5e0 4015
8d749320 4016 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3f563c84 4017 map = new (map) mapped_index ();
2ec9a5e0 4018 *map = local_map;
9291a0cd
TT
4019
4020 dwarf2_per_objfile->index_table = map;
4021 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
4022 dwarf2_per_objfile->quick_file_names_table =
4023 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
4024
4025 return 1;
4026}
4027
dee91e82 4028/* die_reader_func for dw2_get_file_names. */
2fdf6df6 4029
dee91e82
DE
4030static void
4031dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 4032 const gdb_byte *info_ptr,
dee91e82
DE
4033 struct die_info *comp_unit_die,
4034 int has_children,
4035 void *data)
9291a0cd 4036{
dee91e82 4037 struct dwarf2_cu *cu = reader->cu;
ed2dc618 4038 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
4039 struct dwarf2_per_objfile *dwarf2_per_objfile
4040 = cu->per_cu->dwarf2_per_objfile;
dee91e82 4041 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 4042 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 4043 struct attribute *attr;
dee91e82 4044 int i;
7b9f3c50
DE
4045 void **slot;
4046 struct quick_file_names *qfn;
9291a0cd 4047
0186c6a7
DE
4048 gdb_assert (! this_cu->is_debug_types);
4049
07261596
TT
4050 /* Our callers never want to match partial units -- instead they
4051 will match the enclosing full CU. */
4052 if (comp_unit_die->tag == DW_TAG_partial_unit)
4053 {
4054 this_cu->v.quick->no_file_data = 1;
4055 return;
4056 }
4057
0186c6a7 4058 lh_cu = this_cu;
7b9f3c50 4059 slot = NULL;
dee91e82 4060
fff8551c 4061 line_header_up lh;
9c541725 4062 sect_offset line_offset {};
fff8551c 4063
dee91e82 4064 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
4065 if (attr)
4066 {
7b9f3c50
DE
4067 struct quick_file_names find_entry;
4068
9c541725 4069 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
4070
4071 /* We may have already read in this line header (TU line header sharing).
4072 If we have we're done. */
094b34ac 4073 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 4074 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
4075 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
4076 &find_entry, INSERT);
4077 if (*slot != NULL)
4078 {
9a3c8263 4079 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 4080 return;
7b9f3c50
DE
4081 }
4082
3019eac3 4083 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
4084 }
4085 if (lh == NULL)
4086 {
094b34ac 4087 lh_cu->v.quick->no_file_data = 1;
dee91e82 4088 return;
9291a0cd
TT
4089 }
4090
8d749320 4091 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 4092 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 4093 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
4094 gdb_assert (slot != NULL);
4095 *slot = qfn;
9291a0cd 4096
d721ba37 4097 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 4098
fff8551c 4099 qfn->num_file_names = lh->file_names.size ();
8d749320 4100 qfn->file_names =
fff8551c
PA
4101 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
4102 for (i = 0; i < lh->file_names.size (); ++i)
4103 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 4104 qfn->real_names = NULL;
9291a0cd 4105
094b34ac 4106 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
4107}
4108
4109/* A helper for the "quick" functions which attempts to read the line
4110 table for THIS_CU. */
4111
4112static struct quick_file_names *
e4a48d9d 4113dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 4114{
0186c6a7
DE
4115 /* This should never be called for TUs. */
4116 gdb_assert (! this_cu->is_debug_types);
4117 /* Nor type unit groups. */
4118 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 4119
dee91e82
DE
4120 if (this_cu->v.quick->file_names != NULL)
4121 return this_cu->v.quick->file_names;
4122 /* If we know there is no line data, no point in looking again. */
4123 if (this_cu->v.quick->no_file_data)
4124 return NULL;
4125
0186c6a7 4126 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
4127
4128 if (this_cu->v.quick->no_file_data)
4129 return NULL;
4130 return this_cu->v.quick->file_names;
9291a0cd
TT
4131}
4132
4133/* A helper for the "quick" functions which computes and caches the
7b9f3c50 4134 real path for a given file name from the line table. */
2fdf6df6 4135
9291a0cd 4136static const char *
7b9f3c50
DE
4137dw2_get_real_path (struct objfile *objfile,
4138 struct quick_file_names *qfn, int index)
9291a0cd 4139{
7b9f3c50
DE
4140 if (qfn->real_names == NULL)
4141 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 4142 qfn->num_file_names, const char *);
9291a0cd 4143
7b9f3c50 4144 if (qfn->real_names[index] == NULL)
14278e1f 4145 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 4146
7b9f3c50 4147 return qfn->real_names[index];
9291a0cd
TT
4148}
4149
4150static struct symtab *
4151dw2_find_last_source_symtab (struct objfile *objfile)
4152{
ed2dc618
SM
4153 struct dwarf2_per_objfile *dwarf2_per_objfile
4154 = get_dwarf2_per_objfile (objfile);
4155 int index = dwarf2_per_objfile->n_comp_units - 1;
4156 dwarf2_per_cu_data *dwarf_cu = dw2_get_cutu (dwarf2_per_objfile, index);
4157 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu);
ae2de4f8 4158
43f3e411
DE
4159 if (cust == NULL)
4160 return NULL;
ed2dc618 4161
43f3e411 4162 return compunit_primary_filetab (cust);
9291a0cd
TT
4163}
4164
7b9f3c50
DE
4165/* Traversal function for dw2_forget_cached_source_info. */
4166
4167static int
4168dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 4169{
7b9f3c50 4170 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 4171
7b9f3c50 4172 if (file_data->real_names)
9291a0cd 4173 {
7b9f3c50 4174 int i;
9291a0cd 4175
7b9f3c50 4176 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 4177 {
7b9f3c50
DE
4178 xfree ((void*) file_data->real_names[i]);
4179 file_data->real_names[i] = NULL;
9291a0cd
TT
4180 }
4181 }
7b9f3c50
DE
4182
4183 return 1;
4184}
4185
4186static void
4187dw2_forget_cached_source_info (struct objfile *objfile)
4188{
ed2dc618
SM
4189 struct dwarf2_per_objfile *dwarf2_per_objfile
4190 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
4191
4192 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
4193 dw2_free_cached_file_names, NULL);
9291a0cd
TT
4194}
4195
f8eba3c6
TT
4196/* Helper function for dw2_map_symtabs_matching_filename that expands
4197 the symtabs and calls the iterator. */
4198
4199static int
4200dw2_map_expand_apply (struct objfile *objfile,
4201 struct dwarf2_per_cu_data *per_cu,
f5b95b50 4202 const char *name, const char *real_path,
14bc53a8 4203 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 4204{
43f3e411 4205 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
4206
4207 /* Don't visit already-expanded CUs. */
43f3e411 4208 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
4209 return 0;
4210
4211 /* This may expand more than one symtab, and we want to iterate over
4212 all of them. */
a0f42c21 4213 dw2_instantiate_symtab (per_cu);
f8eba3c6 4214
14bc53a8
PA
4215 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
4216 last_made, callback);
f8eba3c6
TT
4217}
4218
4219/* Implementation of the map_symtabs_matching_filename method. */
4220
14bc53a8
PA
4221static bool
4222dw2_map_symtabs_matching_filename
4223 (struct objfile *objfile, const char *name, const char *real_path,
4224 gdb::function_view<bool (symtab *)> callback)
9291a0cd
TT
4225{
4226 int i;
c011a4f4 4227 const char *name_basename = lbasename (name);
ed2dc618
SM
4228 struct dwarf2_per_objfile *dwarf2_per_objfile
4229 = get_dwarf2_per_objfile (objfile);
ae2de4f8 4230
848e3e78
DE
4231 /* The rule is CUs specify all the files, including those used by
4232 any TU, so there's no need to scan TUs here. */
f4dc4d17 4233
ed2dc618 4234 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4235 {
4236 int j;
ed2dc618 4237 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
7b9f3c50 4238 struct quick_file_names *file_data;
9291a0cd 4239
3d7bb9d9 4240 /* We only need to look at symtabs not already expanded. */
43f3e411 4241 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4242 continue;
4243
e4a48d9d 4244 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4245 if (file_data == NULL)
9291a0cd
TT
4246 continue;
4247
7b9f3c50 4248 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4249 {
7b9f3c50 4250 const char *this_name = file_data->file_names[j];
da235a7c 4251 const char *this_real_name;
9291a0cd 4252
af529f8f 4253 if (compare_filenames_for_search (this_name, name))
9291a0cd 4254 {
f5b95b50 4255 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
4256 callback))
4257 return true;
288e77a7 4258 continue;
4aac40c8 4259 }
9291a0cd 4260
c011a4f4
DE
4261 /* Before we invoke realpath, which can get expensive when many
4262 files are involved, do a quick comparison of the basenames. */
4263 if (! basenames_may_differ
4264 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
4265 continue;
4266
da235a7c
JK
4267 this_real_name = dw2_get_real_path (objfile, file_data, j);
4268 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 4269 {
da235a7c 4270 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
4271 callback))
4272 return true;
288e77a7 4273 continue;
da235a7c 4274 }
9291a0cd 4275
da235a7c
JK
4276 if (real_path != NULL)
4277 {
af529f8f
JK
4278 gdb_assert (IS_ABSOLUTE_PATH (real_path));
4279 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 4280 if (this_real_name != NULL
af529f8f 4281 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 4282 {
f5b95b50 4283 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
4284 callback))
4285 return true;
288e77a7 4286 continue;
9291a0cd
TT
4287 }
4288 }
4289 }
4290 }
4291
14bc53a8 4292 return false;
9291a0cd
TT
4293}
4294
da51c347
DE
4295/* Struct used to manage iterating over all CUs looking for a symbol. */
4296
4297struct dw2_symtab_iterator
9291a0cd 4298{
ed2dc618
SM
4299 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
4300 struct dwarf2_per_objfile *dwarf2_per_objfile;
da51c347
DE
4301 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
4302 int want_specific_block;
4303 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
4304 Unused if !WANT_SPECIFIC_BLOCK. */
4305 int block_index;
4306 /* The kind of symbol we're looking for. */
4307 domain_enum domain;
4308 /* The list of CUs from the index entry of the symbol,
4309 or NULL if not found. */
4310 offset_type *vec;
4311 /* The next element in VEC to look at. */
4312 int next;
4313 /* The number of elements in VEC, or zero if there is no match. */
4314 int length;
8943b874
DE
4315 /* Have we seen a global version of the symbol?
4316 If so we can ignore all further global instances.
4317 This is to work around gold/15646, inefficient gold-generated
4318 indices. */
4319 int global_seen;
da51c347 4320};
9291a0cd 4321
da51c347
DE
4322/* Initialize the index symtab iterator ITER.
4323 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
4324 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 4325
9291a0cd 4326static void
da51c347 4327dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 4328 struct dwarf2_per_objfile *dwarf2_per_objfile,
da51c347
DE
4329 int want_specific_block,
4330 int block_index,
4331 domain_enum domain,
4332 const char *name)
4333{
ed2dc618 4334 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
4335 iter->want_specific_block = want_specific_block;
4336 iter->block_index = block_index;
4337 iter->domain = domain;
4338 iter->next = 0;
8943b874 4339 iter->global_seen = 0;
da51c347 4340
ed2dc618
SM
4341 mapped_index *index = dwarf2_per_objfile->index_table;
4342
4343 /* index is NULL if OBJF_READNOW. */
4344 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
4345 iter->length = MAYBE_SWAP (*iter->vec);
4346 else
4347 {
4348 iter->vec = NULL;
4349 iter->length = 0;
4350 }
4351}
4352
4353/* Return the next matching CU or NULL if there are no more. */
4354
4355static struct dwarf2_per_cu_data *
4356dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
4357{
ed2dc618
SM
4358 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
4359
da51c347
DE
4360 for ( ; iter->next < iter->length; ++iter->next)
4361 {
4362 offset_type cu_index_and_attrs =
4363 MAYBE_SWAP (iter->vec[iter->next + 1]);
4364 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 4365 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
4366 int want_static = iter->block_index != GLOBAL_BLOCK;
4367 /* This value is only valid for index versions >= 7. */
4368 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4369 gdb_index_symbol_kind symbol_kind =
4370 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4371 /* Only check the symbol attributes if they're present.
4372 Indices prior to version 7 don't record them,
4373 and indices >= 7 may elide them for certain symbols
4374 (gold does this). */
4375 int attrs_valid =
ed2dc618 4376 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
4377 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4378
3190f0c6
DE
4379 /* Don't crash on bad data. */
4380 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4381 + dwarf2_per_objfile->n_type_units))
4382 {
4383 complaint (&symfile_complaints,
4384 _(".gdb_index entry has bad CU index"
4262abfb
JK
4385 " [in module %s]"),
4386 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
4387 continue;
4388 }
4389
ed2dc618 4390 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
3190f0c6 4391
da51c347 4392 /* Skip if already read in. */
43f3e411 4393 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
4394 continue;
4395
8943b874
DE
4396 /* Check static vs global. */
4397 if (attrs_valid)
4398 {
4399 if (iter->want_specific_block
4400 && want_static != is_static)
4401 continue;
4402 /* Work around gold/15646. */
4403 if (!is_static && iter->global_seen)
4404 continue;
4405 if (!is_static)
4406 iter->global_seen = 1;
4407 }
da51c347
DE
4408
4409 /* Only check the symbol's kind if it has one. */
4410 if (attrs_valid)
4411 {
4412 switch (iter->domain)
4413 {
4414 case VAR_DOMAIN:
4415 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4416 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4417 /* Some types are also in VAR_DOMAIN. */
4418 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4419 continue;
4420 break;
4421 case STRUCT_DOMAIN:
4422 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4423 continue;
4424 break;
4425 case LABEL_DOMAIN:
4426 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4427 continue;
4428 break;
4429 default:
4430 break;
4431 }
4432 }
4433
4434 ++iter->next;
4435 return per_cu;
4436 }
4437
4438 return NULL;
4439}
4440
43f3e411 4441static struct compunit_symtab *
da51c347
DE
4442dw2_lookup_symbol (struct objfile *objfile, int block_index,
4443 const char *name, domain_enum domain)
9291a0cd 4444{
43f3e411 4445 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
4446 struct dwarf2_per_objfile *dwarf2_per_objfile
4447 = get_dwarf2_per_objfile (objfile);
9291a0cd 4448
b5ec771e
PA
4449 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4450
ed2dc618
SM
4451 struct dw2_symtab_iterator iter;
4452 struct dwarf2_per_cu_data *per_cu;
da51c347 4453
ed2dc618 4454 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
9291a0cd 4455
ed2dc618
SM
4456 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4457 {
4458 struct symbol *sym, *with_opaque = NULL;
4459 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
4460 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4461 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4462
ed2dc618
SM
4463 sym = block_find_symbol (block, name, domain,
4464 block_find_non_opaque_type_preferred,
4465 &with_opaque);
b2e2f908 4466
ed2dc618
SM
4467 /* Some caution must be observed with overloaded functions
4468 and methods, since the index will not contain any overload
4469 information (but NAME might contain it). */
da51c347 4470
ed2dc618
SM
4471 if (sym != NULL
4472 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4473 return stab;
4474 if (with_opaque != NULL
4475 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4476 stab_best = stab;
da51c347 4477
ed2dc618 4478 /* Keep looking through other CUs. */
9291a0cd 4479 }
9291a0cd 4480
da51c347 4481 return stab_best;
9291a0cd
TT
4482}
4483
4484static void
4485dw2_print_stats (struct objfile *objfile)
4486{
ed2dc618
SM
4487 struct dwarf2_per_objfile *dwarf2_per_objfile
4488 = get_dwarf2_per_objfile (objfile);
4489 int total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
4490 int count = 0;
9291a0cd 4491
ed2dc618 4492 for (int i = 0; i < total; ++i)
9291a0cd 4493 {
ed2dc618 4494 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
9291a0cd 4495
43f3e411 4496 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4497 ++count;
4498 }
e4a48d9d 4499 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4500 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4501}
4502
779bd270
DE
4503/* This dumps minimal information about the index.
4504 It is called via "mt print objfiles".
4505 One use is to verify .gdb_index has been loaded by the
4506 gdb.dwarf2/gdb-index.exp testcase. */
4507
9291a0cd
TT
4508static void
4509dw2_dump (struct objfile *objfile)
4510{
ed2dc618
SM
4511 struct dwarf2_per_objfile *dwarf2_per_objfile
4512 = get_dwarf2_per_objfile (objfile);
4513
779bd270
DE
4514 gdb_assert (dwarf2_per_objfile->using_index);
4515 printf_filtered (".gdb_index:");
4516 if (dwarf2_per_objfile->index_table != NULL)
4517 {
4518 printf_filtered (" version %d\n",
4519 dwarf2_per_objfile->index_table->version);
4520 }
4521 else
4522 printf_filtered (" faked for \"readnow\"\n");
4523 printf_filtered ("\n");
9291a0cd
TT
4524}
4525
4526static void
3189cb12
DE
4527dw2_relocate (struct objfile *objfile,
4528 const struct section_offsets *new_offsets,
4529 const struct section_offsets *delta)
9291a0cd
TT
4530{
4531 /* There's nothing to relocate here. */
4532}
4533
4534static void
4535dw2_expand_symtabs_for_function (struct objfile *objfile,
4536 const char *func_name)
4537{
ed2dc618
SM
4538 struct dwarf2_per_objfile *dwarf2_per_objfile
4539 = get_dwarf2_per_objfile (objfile);
da51c347 4540
ed2dc618
SM
4541 struct dw2_symtab_iterator iter;
4542 struct dwarf2_per_cu_data *per_cu;
da51c347 4543
ed2dc618
SM
4544 /* Note: It doesn't matter what we pass for block_index here. */
4545 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4546 func_name);
da51c347 4547
ed2dc618
SM
4548 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4549 dw2_instantiate_symtab (per_cu);
da51c347 4550
9291a0cd
TT
4551}
4552
4553static void
4554dw2_expand_all_symtabs (struct objfile *objfile)
4555{
ed2dc618
SM
4556 struct dwarf2_per_objfile *dwarf2_per_objfile
4557 = get_dwarf2_per_objfile (objfile);
4558 int total_units = (dwarf2_per_objfile->n_comp_units
4559 + dwarf2_per_objfile->n_type_units);
9291a0cd 4560
ed2dc618 4561 for (int i = 0; i < total_units; ++i)
9291a0cd 4562 {
ed2dc618
SM
4563 struct dwarf2_per_cu_data *per_cu
4564 = dw2_get_cutu (dwarf2_per_objfile, i);
9291a0cd 4565
a0f42c21 4566 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4567 }
4568}
4569
4570static void
652a8996
JK
4571dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4572 const char *fullname)
9291a0cd 4573{
ed2dc618
SM
4574 struct dwarf2_per_objfile *dwarf2_per_objfile
4575 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
4576
4577 /* We don't need to consider type units here.
4578 This is only called for examining code, e.g. expand_line_sal.
4579 There can be an order of magnitude (or more) more type units
4580 than comp units, and we avoid them if we can. */
4581
ed2dc618 4582 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4583 {
4584 int j;
ed2dc618 4585 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
7b9f3c50 4586 struct quick_file_names *file_data;
9291a0cd 4587
3d7bb9d9 4588 /* We only need to look at symtabs not already expanded. */
43f3e411 4589 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4590 continue;
4591
e4a48d9d 4592 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4593 if (file_data == NULL)
9291a0cd
TT
4594 continue;
4595
7b9f3c50 4596 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4597 {
652a8996
JK
4598 const char *this_fullname = file_data->file_names[j];
4599
4600 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4601 {
a0f42c21 4602 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4603 break;
4604 }
4605 }
4606 }
4607}
4608
9291a0cd 4609static void
ade7ed9e 4610dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 4611 const char * name, domain_enum domain,
ade7ed9e 4612 int global,
40658b94
PH
4613 int (*callback) (struct block *,
4614 struct symbol *, void *),
b5ec771e 4615 void *data, symbol_name_match_type match,
2edb89d3 4616 symbol_compare_ftype *ordered_compare)
9291a0cd 4617{
40658b94 4618 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4619 current language is Ada for a non-Ada objfile using GNU index. As Ada
4620 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4621}
4622
b5ec771e
PA
4623/* Symbol name matcher for .gdb_index names.
4624
4625 Symbol names in .gdb_index have a few particularities:
4626
4627 - There's no indication of which is the language of each symbol.
4628
4629 Since each language has its own symbol name matching algorithm,
4630 and we don't know which language is the right one, we must match
3f563c84
PA
4631 each symbol against all languages. This would be a potential
4632 performance problem if it were not mitigated by the
4633 mapped_index::name_components lookup table, which significantly
4634 reduces the number of times we need to call into this matcher,
4635 making it a non-issue.
b5ec771e
PA
4636
4637 - Symbol names in the index have no overload (parameter)
4638 information. I.e., in C++, "foo(int)" and "foo(long)" both
4639 appear as "foo" in the index, for example.
4640
4641 This means that the lookup names passed to the symbol name
4642 matcher functions must have no parameter information either
4643 because (e.g.) symbol search name "foo" does not match
4644 lookup-name "foo(int)" [while swapping search name for lookup
4645 name would match].
4646*/
4647class gdb_index_symbol_name_matcher
4648{
4649public:
4650 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4651 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4652
4653 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4654 Returns true if any matcher matches. */
4655 bool matches (const char *symbol_name);
4656
4657private:
4658 /* A reference to the lookup name we're matching against. */
4659 const lookup_name_info &m_lookup_name;
4660
4661 /* A vector holding all the different symbol name matchers, for all
4662 languages. */
4663 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4664};
4665
4666gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4667 (const lookup_name_info &lookup_name)
4668 : m_lookup_name (lookup_name)
4669{
4670 /* Prepare the vector of comparison functions upfront, to avoid
4671 doing the same work for each symbol. Care is taken to avoid
4672 matching with the same matcher more than once if/when multiple
4673 languages use the same matcher function. */
4674 auto &matchers = m_symbol_name_matcher_funcs;
4675 matchers.reserve (nr_languages);
4676
4677 matchers.push_back (default_symbol_name_matcher);
4678
4679 for (int i = 0; i < nr_languages; i++)
4680 {
4681 const language_defn *lang = language_def ((enum language) i);
c63d3e8d 4682 symbol_name_matcher_ftype *name_matcher
618daa93 4683 = get_symbol_name_matcher (lang, m_lookup_name);
c63d3e8d
PA
4684
4685 /* Don't insert the same comparison routine more than once.
4686 Note that we do this linear walk instead of a seemingly
4687 cheaper sorted insert, or use a std::set or something like
4688 that, because relative order of function addresses is not
4689 stable. This is not a problem in practice because the number
4690 of supported languages is low, and the cost here is tiny
4691 compared to the number of searches we'll do afterwards using
4692 this object. */
4693 if (name_matcher != default_symbol_name_matcher
4694 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4695 == matchers.end ()))
4696 matchers.push_back (name_matcher);
b5ec771e
PA
4697 }
4698}
4699
4700bool
4701gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4702{
4703 for (auto matches_name : m_symbol_name_matcher_funcs)
4704 if (matches_name (symbol_name, m_lookup_name, NULL))
4705 return true;
4706
4707 return false;
4708}
4709
e1ef7d7a
PA
4710/* Starting from a search name, return the string that finds the upper
4711 bound of all strings that start with SEARCH_NAME in a sorted name
4712 list. Returns the empty string to indicate that the upper bound is
4713 the end of the list. */
4714
4715static std::string
4716make_sort_after_prefix_name (const char *search_name)
4717{
4718 /* When looking to complete "func", we find the upper bound of all
4719 symbols that start with "func" by looking for where we'd insert
4720 the closest string that would follow "func" in lexicographical
4721 order. Usually, that's "func"-with-last-character-incremented,
4722 i.e. "fund". Mind non-ASCII characters, though. Usually those
4723 will be UTF-8 multi-byte sequences, but we can't be certain.
4724 Especially mind the 0xff character, which is a valid character in
4725 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4726 rule out compilers allowing it in identifiers. Note that
4727 conveniently, strcmp/strcasecmp are specified to compare
4728 characters interpreted as unsigned char. So what we do is treat
4729 the whole string as a base 256 number composed of a sequence of
4730 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4731 to 0, and carries 1 to the following more-significant position.
4732 If the very first character in SEARCH_NAME ends up incremented
4733 and carries/overflows, then the upper bound is the end of the
4734 list. The string after the empty string is also the empty
4735 string.
4736
4737 Some examples of this operation:
4738
4739 SEARCH_NAME => "+1" RESULT
4740
4741 "abc" => "abd"
4742 "ab\xff" => "ac"
4743 "\xff" "a" "\xff" => "\xff" "b"
4744 "\xff" => ""
4745 "\xff\xff" => ""
4746 "" => ""
4747
4748 Then, with these symbols for example:
4749
4750 func
4751 func1
4752 fund
4753
4754 completing "func" looks for symbols between "func" and
4755 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4756 which finds "func" and "func1", but not "fund".
4757
4758 And with:
4759
4760 funcÿ (Latin1 'ÿ' [0xff])
4761 funcÿ1
4762 fund
4763
4764 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4765 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4766
4767 And with:
4768
4769 ÿÿ (Latin1 'ÿ' [0xff])
4770 ÿÿ1
4771
4772 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4773 the end of the list.
4774 */
4775 std::string after = search_name;
4776 while (!after.empty () && (unsigned char) after.back () == 0xff)
4777 after.pop_back ();
4778 if (!after.empty ())
4779 after.back () = (unsigned char) after.back () + 1;
4780 return after;
4781}
4782
5c58de74 4783/* See declaration. */
61d96d7e 4784
5c58de74
PA
4785std::pair<std::vector<name_component>::const_iterator,
4786 std::vector<name_component>::const_iterator>
44ed8f3e 4787mapped_index_base::find_name_components_bounds
5c58de74 4788 (const lookup_name_info &lookup_name_without_params) const
3f563c84 4789{
5c58de74
PA
4790 auto *name_cmp
4791 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84
PA
4792
4793 const char *cplus
c62446b1 4794 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
9291a0cd 4795
3f563c84
PA
4796 /* Comparison function object for lower_bound that matches against a
4797 given symbol name. */
4798 auto lookup_compare_lower = [&] (const name_component &elem,
4799 const char *name)
4800 {
5c58de74 4801 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4802 const char *elem_name = elem_qualified + elem.name_offset;
4803 return name_cmp (elem_name, name) < 0;
4804 };
4805
4806 /* Comparison function object for upper_bound that matches against a
4807 given symbol name. */
4808 auto lookup_compare_upper = [&] (const char *name,
4809 const name_component &elem)
4810 {
5c58de74 4811 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4812 const char *elem_name = elem_qualified + elem.name_offset;
4813 return name_cmp (name, elem_name) < 0;
4814 };
4815
5c58de74
PA
4816 auto begin = this->name_components.begin ();
4817 auto end = this->name_components.end ();
3f563c84
PA
4818
4819 /* Find the lower bound. */
4820 auto lower = [&] ()
4821 {
5c58de74 4822 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
3f563c84
PA
4823 return begin;
4824 else
4825 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4826 } ();
4827
4828 /* Find the upper bound. */
4829 auto upper = [&] ()
4830 {
5c58de74 4831 if (lookup_name_without_params.completion_mode ())
3f563c84 4832 {
e1ef7d7a
PA
4833 /* In completion mode, we want UPPER to point past all
4834 symbols names that have the same prefix. I.e., with
4835 these symbols, and completing "func":
4836
4837 function << lower bound
4838 function1
4839 other_function << upper bound
4840
4841 We find the upper bound by looking for the insertion
4842 point of "func"-with-last-character-incremented,
4843 i.e. "fund". */
4844 std::string after = make_sort_after_prefix_name (cplus);
4845 if (after.empty ())
3f563c84 4846 return end;
e6b2f5ef
PA
4847 return std::lower_bound (lower, end, after.c_str (),
4848 lookup_compare_lower);
3f563c84
PA
4849 }
4850 else
4851 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4852 } ();
4853
5c58de74
PA
4854 return {lower, upper};
4855}
4856
4857/* See declaration. */
4858
4859void
44ed8f3e 4860mapped_index_base::build_name_components ()
5c58de74
PA
4861{
4862 if (!this->name_components.empty ())
4863 return;
4864
4865 this->name_components_casing = case_sensitivity;
4866 auto *name_cmp
4867 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4868
4869 /* The code below only knows how to break apart components of C++
4870 symbol names (and other languages that use '::' as
4871 namespace/module separator). If we add support for wild matching
4872 to some language that uses some other operator (E.g., Ada, Go and
4873 D use '.'), then we'll need to try splitting the symbol name
4874 according to that language too. Note that Ada does support wild
4875 matching, but doesn't currently support .gdb_index. */
44ed8f3e
PA
4876 auto count = this->symbol_name_count ();
4877 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4878 {
44ed8f3e 4879 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4880 continue;
4881
4882 const char *name = this->symbol_name_at (idx);
4883
4884 /* Add each name component to the name component table. */
4885 unsigned int previous_len = 0;
4886 for (unsigned int current_len = cp_find_first_component (name);
4887 name[current_len] != '\0';
4888 current_len += cp_find_first_component (name + current_len))
4889 {
4890 gdb_assert (name[current_len] == ':');
4891 this->name_components.push_back ({previous_len, idx});
4892 /* Skip the '::'. */
4893 current_len += 2;
4894 previous_len = current_len;
4895 }
4896 this->name_components.push_back ({previous_len, idx});
4897 }
4898
4899 /* Sort name_components elements by name. */
4900 auto name_comp_compare = [&] (const name_component &left,
4901 const name_component &right)
4902 {
4903 const char *left_qualified = this->symbol_name_at (left.idx);
4904 const char *right_qualified = this->symbol_name_at (right.idx);
4905
4906 const char *left_name = left_qualified + left.name_offset;
4907 const char *right_name = right_qualified + right.name_offset;
4908
4909 return name_cmp (left_name, right_name) < 0;
4910 };
4911
4912 std::sort (this->name_components.begin (),
4913 this->name_components.end (),
4914 name_comp_compare);
4915}
4916
4917/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4918 mapped_index_base instead of the containing objfile. This is split
4919 to a separate function in order to be able to unit test the
4920 name_components matching using a mock mapped_index_base. For each
5c58de74 4921 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4922 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4923
4924static void
4925dw2_expand_symtabs_matching_symbol
44ed8f3e 4926 (mapped_index_base &index,
5c58de74
PA
4927 const lookup_name_info &lookup_name_in,
4928 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4929 enum search_domain kind,
4930 gdb::function_view<void (offset_type)> match_callback)
4931{
4932 lookup_name_info lookup_name_without_params
4933 = lookup_name_in.make_ignore_params ();
4934 gdb_index_symbol_name_matcher lookup_name_matcher
4935 (lookup_name_without_params);
4936
4937 /* Build the symbol name component sorted vector, if we haven't
4938 yet. */
4939 index.build_name_components ();
4940
4941 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4942
3f563c84
PA
4943 /* Now for each symbol name in range, check to see if we have a name
4944 match, and if so, call the MATCH_CALLBACK callback. */
4945
4946 /* The same symbol may appear more than once in the range though.
4947 E.g., if we're looking for symbols that complete "w", and we have
4948 a symbol named "w1::w2", we'll find the two name components for
4949 that same symbol in the range. To be sure we only call the
4950 callback once per symbol, we first collect the symbol name
4951 indexes that matched in a temporary vector and ignore
4952 duplicates. */
4953 std::vector<offset_type> matches;
5c58de74 4954 matches.reserve (std::distance (bounds.first, bounds.second));
3f563c84 4955
5c58de74 4956 for (; bounds.first != bounds.second; ++bounds.first)
3f563c84 4957 {
5c58de74 4958 const char *qualified = index.symbol_name_at (bounds.first->idx);
3f563c84
PA
4959
4960 if (!lookup_name_matcher.matches (qualified)
4961 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
9291a0cd
TT
4962 continue;
4963
5c58de74 4964 matches.push_back (bounds.first->idx);
3f563c84
PA
4965 }
4966
4967 std::sort (matches.begin (), matches.end ());
4968
4969 /* Finally call the callback, once per match. */
4970 ULONGEST prev = -1;
4971 for (offset_type idx : matches)
4972 {
4973 if (prev != idx)
4974 {
4975 match_callback (idx);
4976 prev = idx;
4977 }
4978 }
4979
4980 /* Above we use a type wider than idx's for 'prev', since 0 and
4981 (offset_type)-1 are both possible values. */
4982 static_assert (sizeof (prev) > sizeof (offset_type), "");
4983}
4984
c62446b1
PA
4985#if GDB_SELF_TEST
4986
4987namespace selftests { namespace dw2_expand_symtabs_matching {
4988
a3c5fafd
PA
4989/* A mock .gdb_index/.debug_names-like name index table, enough to
4990 exercise dw2_expand_symtabs_matching_symbol, which works with the
4991 mapped_index_base interface. Builds an index from the symbol list
4992 passed as parameter to the constructor. */
4993class mock_mapped_index : public mapped_index_base
c62446b1
PA
4994{
4995public:
a3c5fafd
PA
4996 mock_mapped_index (gdb::array_view<const char *> symbols)
4997 : m_symbol_table (symbols)
c62446b1
PA
4998 {}
4999
a3c5fafd 5000 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 5001
a3c5fafd
PA
5002 /* Return the number of names in the symbol table. */
5003 virtual size_t symbol_name_count () const
c62446b1 5004 {
a3c5fafd 5005 return m_symbol_table.size ();
c62446b1
PA
5006 }
5007
a3c5fafd
PA
5008 /* Get the name of the symbol at IDX in the symbol table. */
5009 virtual const char *symbol_name_at (offset_type idx) const
5010 {
5011 return m_symbol_table[idx];
5012 }
c62446b1 5013
a3c5fafd
PA
5014private:
5015 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
5016};
5017
5018/* Convenience function that converts a NULL pointer to a "<null>"
5019 string, to pass to print routines. */
5020
5021static const char *
5022string_or_null (const char *str)
5023{
5024 return str != NULL ? str : "<null>";
5025}
5026
5027/* Check if a lookup_name_info built from
5028 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
5029 index. EXPECTED_LIST is the list of expected matches, in expected
5030 matching order. If no match expected, then an empty list is
5031 specified. Returns true on success. On failure prints a warning
5032 indicating the file:line that failed, and returns false. */
5033
5034static bool
5035check_match (const char *file, int line,
5036 mock_mapped_index &mock_index,
5037 const char *name, symbol_name_match_type match_type,
5038 bool completion_mode,
5039 std::initializer_list<const char *> expected_list)
5040{
5041 lookup_name_info lookup_name (name, match_type, completion_mode);
5042
5043 bool matched = true;
5044
5045 auto mismatch = [&] (const char *expected_str,
5046 const char *got)
5047 {
5048 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
5049 "expected=\"%s\", got=\"%s\"\n"),
5050 file, line,
5051 (match_type == symbol_name_match_type::FULL
5052 ? "FULL" : "WILD"),
5053 name, string_or_null (expected_str), string_or_null (got));
5054 matched = false;
5055 };
5056
5057 auto expected_it = expected_list.begin ();
5058 auto expected_end = expected_list.end ();
5059
a3c5fafd 5060 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
5061 NULL, ALL_DOMAIN,
5062 [&] (offset_type idx)
5063 {
a3c5fafd 5064 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
5065 const char *expected_str
5066 = expected_it == expected_end ? NULL : *expected_it++;
5067
5068 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
5069 mismatch (expected_str, matched_name);
5070 });
5071
5072 const char *expected_str
5073 = expected_it == expected_end ? NULL : *expected_it++;
5074 if (expected_str != NULL)
5075 mismatch (expected_str, NULL);
5076
5077 return matched;
5078}
5079
5080/* The symbols added to the mock mapped_index for testing (in
5081 canonical form). */
5082static const char *test_symbols[] = {
5083 "function",
5084 "std::bar",
5085 "std::zfunction",
5086 "std::zfunction2",
5087 "w1::w2",
5088 "ns::foo<char*>",
5089 "ns::foo<int>",
5090 "ns::foo<long>",
a20714ff
PA
5091 "ns2::tmpl<int>::foo2",
5092 "(anonymous namespace)::A::B::C",
c62446b1 5093
e1ef7d7a
PA
5094 /* These are used to check that the increment-last-char in the
5095 matching algorithm for completion doesn't match "t1_fund" when
5096 completing "t1_func". */
5097 "t1_func",
5098 "t1_func1",
5099 "t1_fund",
5100 "t1_fund1",
5101
5102 /* A UTF-8 name with multi-byte sequences to make sure that
5103 cp-name-parser understands this as a single identifier ("função"
5104 is "function" in PT). */
5105 u8"u8função",
5106
5107 /* \377 (0xff) is Latin1 'ÿ'. */
5108 "yfunc\377",
5109
5110 /* \377 (0xff) is Latin1 'ÿ'. */
5111 "\377",
5112 "\377\377123",
5113
c62446b1
PA
5114 /* A name with all sorts of complications. Starts with "z" to make
5115 it easier for the completion tests below. */
5116#define Z_SYM_NAME \
5117 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
5118 "::tuple<(anonymous namespace)::ui*, " \
5119 "std::default_delete<(anonymous namespace)::ui>, void>"
5120
5121 Z_SYM_NAME
5122};
5123
a3c5fafd
PA
5124/* Returns true if the mapped_index_base::find_name_component_bounds
5125 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
5126 in completion mode. */
5c58de74
PA
5127
5128static bool
a3c5fafd 5129check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
5130 const char *search_name,
5131 gdb::array_view<const char *> expected_syms)
5132{
5133 lookup_name_info lookup_name (search_name,
5134 symbol_name_match_type::FULL, true);
5135
5136 auto bounds = index.find_name_components_bounds (lookup_name);
5137
5138 size_t distance = std::distance (bounds.first, bounds.second);
5139 if (distance != expected_syms.size ())
5140 return false;
5141
5142 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
5143 {
5144 auto nc_elem = bounds.first + exp_elem;
5145 const char *qualified = index.symbol_name_at (nc_elem->idx);
5146 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
5147 return false;
5148 }
5149
5150 return true;
5151}
5152
5153/* Test the lower-level mapped_index::find_name_component_bounds
5154 method. */
5155
c62446b1 5156static void
5c58de74
PA
5157test_mapped_index_find_name_component_bounds ()
5158{
5159 mock_mapped_index mock_index (test_symbols);
5160
a3c5fafd 5161 mock_index.build_name_components ();
5c58de74
PA
5162
5163 /* Test the lower-level mapped_index::find_name_component_bounds
5164 method in completion mode. */
5165 {
5166 static const char *expected_syms[] = {
5167 "t1_func",
5168 "t1_func1",
5c58de74
PA
5169 };
5170
a3c5fafd 5171 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
5172 "t1_func", expected_syms));
5173 }
5174
5175 /* Check that the increment-last-char in the name matching algorithm
5176 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
5177 {
5178 static const char *expected_syms1[] = {
5179 "\377",
5180 "\377\377123",
5181 };
a3c5fafd 5182 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
5183 "\377", expected_syms1));
5184
5185 static const char *expected_syms2[] = {
5186 "\377\377123",
5187 };
a3c5fafd 5188 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
5189 "\377\377", expected_syms2));
5190 }
5191}
5192
5193/* Test dw2_expand_symtabs_matching_symbol. */
5194
5195static void
5196test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
5197{
5198 mock_mapped_index mock_index (test_symbols);
5199
5200 /* We let all tests run until the end even if some fails, for debug
5201 convenience. */
5202 bool any_mismatch = false;
5203
5204 /* Create the expected symbols list (an initializer_list). Needed
5205 because lists have commas, and we need to pass them to CHECK,
5206 which is a macro. */
5207#define EXPECT(...) { __VA_ARGS__ }
5208
5209 /* Wrapper for check_match that passes down the current
5210 __FILE__/__LINE__. */
5211#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
5212 any_mismatch |= !check_match (__FILE__, __LINE__, \
5213 mock_index, \
5214 NAME, MATCH_TYPE, COMPLETION_MODE, \
5215 EXPECTED_LIST)
5216
5217 /* Identity checks. */
5218 for (const char *sym : test_symbols)
5219 {
5220 /* Should be able to match all existing symbols. */
5221 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
5222 EXPECT (sym));
5223
5224 /* Should be able to match all existing symbols with
5225 parameters. */
5226 std::string with_params = std::string (sym) + "(int)";
5227 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5228 EXPECT (sym));
5229
5230 /* Should be able to match all existing symbols with
5231 parameters and qualifiers. */
5232 with_params = std::string (sym) + " ( int ) const";
5233 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5234 EXPECT (sym));
5235
5236 /* This should really find sym, but cp-name-parser.y doesn't
5237 know about lvalue/rvalue qualifiers yet. */
5238 with_params = std::string (sym) + " ( int ) &&";
5239 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5240 {});
5241 }
5242
e1ef7d7a
PA
5243 /* Check that the name matching algorithm for completion doesn't get
5244 confused with Latin1 'ÿ' / 0xff. */
5245 {
5246 static const char str[] = "\377";
5247 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5248 EXPECT ("\377", "\377\377123"));
5249 }
5250
5251 /* Check that the increment-last-char in the matching algorithm for
5252 completion doesn't match "t1_fund" when completing "t1_func". */
5253 {
5254 static const char str[] = "t1_func";
5255 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5256 EXPECT ("t1_func", "t1_func1"));
5257 }
5258
c62446b1
PA
5259 /* Check that completion mode works at each prefix of the expected
5260 symbol name. */
5261 {
5262 static const char str[] = "function(int)";
5263 size_t len = strlen (str);
5264 std::string lookup;
5265
5266 for (size_t i = 1; i < len; i++)
5267 {
5268 lookup.assign (str, i);
5269 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5270 EXPECT ("function"));
5271 }
5272 }
5273
5274 /* While "w" is a prefix of both components, the match function
5275 should still only be called once. */
5276 {
5277 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
5278 EXPECT ("w1::w2"));
a20714ff
PA
5279 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
5280 EXPECT ("w1::w2"));
c62446b1
PA
5281 }
5282
5283 /* Same, with a "complicated" symbol. */
5284 {
5285 static const char str[] = Z_SYM_NAME;
5286 size_t len = strlen (str);
5287 std::string lookup;
5288
5289 for (size_t i = 1; i < len; i++)
5290 {
5291 lookup.assign (str, i);
5292 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5293 EXPECT (Z_SYM_NAME));
5294 }
5295 }
5296
5297 /* In FULL mode, an incomplete symbol doesn't match. */
5298 {
5299 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
5300 {});
5301 }
5302
5303 /* A complete symbol with parameters matches any overload, since the
5304 index has no overload info. */
5305 {
5306 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
5307 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
5308 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
5309 EXPECT ("std::zfunction", "std::zfunction2"));
5310 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
5311 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
5312 }
5313
5314 /* Check that whitespace is ignored appropriately. A symbol with a
5315 template argument list. */
5316 {
5317 static const char expected[] = "ns::foo<int>";
5318 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
5319 EXPECT (expected));
a20714ff
PA
5320 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
5321 EXPECT (expected));
c62446b1
PA
5322 }
5323
5324 /* Check that whitespace is ignored appropriately. A symbol with a
5325 template argument list that includes a pointer. */
5326 {
5327 static const char expected[] = "ns::foo<char*>";
5328 /* Try both completion and non-completion modes. */
5329 static const bool completion_mode[2] = {false, true};
5330 for (size_t i = 0; i < 2; i++)
5331 {
5332 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
5333 completion_mode[i], EXPECT (expected));
a20714ff
PA
5334 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
5335 completion_mode[i], EXPECT (expected));
c62446b1
PA
5336
5337 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
5338 completion_mode[i], EXPECT (expected));
a20714ff
PA
5339 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
5340 completion_mode[i], EXPECT (expected));
c62446b1
PA
5341 }
5342 }
5343
5344 {
5345 /* Check method qualifiers are ignored. */
5346 static const char expected[] = "ns::foo<char*>";
5347 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
5348 symbol_name_match_type::FULL, true, EXPECT (expected));
5349 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
5350 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
5351 CHECK_MATCH ("foo < char * > ( int ) const",
5352 symbol_name_match_type::WILD, true, EXPECT (expected));
5353 CHECK_MATCH ("foo < char * > ( int ) &&",
5354 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
5355 }
5356
5357 /* Test lookup names that don't match anything. */
5358 {
a20714ff
PA
5359 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
5360 {});
5361
c62446b1
PA
5362 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
5363 {});
5364 }
5365
a20714ff
PA
5366 /* Some wild matching tests, exercising "(anonymous namespace)",
5367 which should not be confused with a parameter list. */
5368 {
5369 static const char *syms[] = {
5370 "A::B::C",
5371 "B::C",
5372 "C",
5373 "A :: B :: C ( int )",
5374 "B :: C ( int )",
5375 "C ( int )",
5376 };
5377
5378 for (const char *s : syms)
5379 {
5380 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
5381 EXPECT ("(anonymous namespace)::A::B::C"));
5382 }
5383 }
5384
5385 {
5386 static const char expected[] = "ns2::tmpl<int>::foo2";
5387 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
5388 EXPECT (expected));
5389 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5390 EXPECT (expected));
5391 }
5392
c62446b1
PA
5393 SELF_CHECK (!any_mismatch);
5394
5395#undef EXPECT
5396#undef CHECK_MATCH
5397}
5398
5c58de74
PA
5399static void
5400run_test ()
5401{
5402 test_mapped_index_find_name_component_bounds ();
5403 test_dw2_expand_symtabs_matching_symbol ();
5404}
5405
c62446b1
PA
5406}} // namespace selftests::dw2_expand_symtabs_matching
5407
5408#endif /* GDB_SELF_TEST */
5409
4b514bc8
JK
5410/* If FILE_MATCHER is NULL or if PER_CU has
5411 dwarf2_per_cu_quick_data::MARK set (see
5412 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5413 EXPANSION_NOTIFY on it. */
5414
5415static void
5416dw2_expand_symtabs_matching_one
5417 (struct dwarf2_per_cu_data *per_cu,
5418 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5419 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5420{
5421 if (file_matcher == NULL || per_cu->v.quick->mark)
5422 {
5423 bool symtab_was_null
5424 = (per_cu->v.quick->compunit_symtab == NULL);
5425
5426 dw2_instantiate_symtab (per_cu);
5427
5428 if (expansion_notify != NULL
5429 && symtab_was_null
5430 && per_cu->v.quick->compunit_symtab != NULL)
5431 expansion_notify (per_cu->v.quick->compunit_symtab);
5432 }
5433}
5434
3f563c84
PA
5435/* Helper for dw2_expand_matching symtabs. Called on each symbol
5436 matched, to expand corresponding CUs that were marked. IDX is the
5437 index of the symbol name that matched. */
5438
5439static void
5440dw2_expand_marked_cus
ed2dc618 5441 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
5442 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5443 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5444 search_domain kind)
5445{
3f563c84
PA
5446 offset_type *vec, vec_len, vec_idx;
5447 bool global_seen = false;
ed2dc618 5448 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 5449
61920122 5450 vec = (offset_type *) (index.constant_pool
f00a2de2 5451 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5452 vec_len = MAYBE_SWAP (vec[0]);
5453 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5454 {
5455 struct dwarf2_per_cu_data *per_cu;
5456 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5457 /* This value is only valid for index versions >= 7. */
5458 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5459 gdb_index_symbol_kind symbol_kind =
5460 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5461 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5462 /* Only check the symbol attributes if they're present.
5463 Indices prior to version 7 don't record them,
5464 and indices >= 7 may elide them for certain symbols
5465 (gold does this). */
5466 int attrs_valid =
5467 (index.version >= 7
5468 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5469
5470 /* Work around gold/15646. */
5471 if (attrs_valid)
9291a0cd 5472 {
61920122
PA
5473 if (!is_static && global_seen)
5474 continue;
5475 if (!is_static)
5476 global_seen = true;
5477 }
3190f0c6 5478
61920122
PA
5479 /* Only check the symbol's kind if it has one. */
5480 if (attrs_valid)
5481 {
5482 switch (kind)
8943b874 5483 {
61920122
PA
5484 case VARIABLES_DOMAIN:
5485 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5486 continue;
5487 break;
5488 case FUNCTIONS_DOMAIN:
5489 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5490 continue;
61920122
PA
5491 break;
5492 case TYPES_DOMAIN:
5493 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5494 continue;
5495 break;
5496 default:
5497 break;
8943b874 5498 }
61920122 5499 }
8943b874 5500
61920122
PA
5501 /* Don't crash on bad data. */
5502 if (cu_index >= (dwarf2_per_objfile->n_comp_units
5503 + dwarf2_per_objfile->n_type_units))
5504 {
5505 complaint (&symfile_complaints,
5506 _(".gdb_index entry has bad CU index"
ed2dc618
SM
5507 " [in module %s]"),
5508 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
5509 continue;
5510 }
5511
ed2dc618 5512 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
4b514bc8
JK
5513 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5514 expansion_notify);
61920122
PA
5515 }
5516}
5517
4b514bc8
JK
5518/* If FILE_MATCHER is non-NULL, set all the
5519 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5520 that match FILE_MATCHER. */
5521
61920122 5522static void
4b514bc8 5523dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
5524 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5525 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5526{
4b514bc8 5527 if (file_matcher == NULL)
61920122
PA
5528 return;
5529
4b514bc8
JK
5530 objfile *const objfile = dwarf2_per_objfile->objfile;
5531
5532 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5533 htab_eq_pointer,
5534 NULL, xcalloc, xfree));
5535 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5536 htab_eq_pointer,
5537 NULL, xcalloc, xfree));
61920122 5538
4b514bc8
JK
5539 /* The rule is CUs specify all the files, including those used by
5540 any TU, so there's no need to scan TUs here. */
61920122 5541
927aa2e7
JK
5542 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5543 {
5544 int j;
ed2dc618 5545 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
927aa2e7
JK
5546 struct quick_file_names *file_data;
5547 void **slot;
5548
5549 QUIT;
5550
5551 per_cu->v.quick->mark = 0;
5552
5553 /* We only need to look at symtabs not already expanded. */
5554 if (per_cu->v.quick->compunit_symtab)
5555 continue;
5556
5557 file_data = dw2_get_file_names (per_cu);
5558 if (file_data == NULL)
5559 continue;
5560
5561 if (htab_find (visited_not_found.get (), file_data) != NULL)
5562 continue;
5563 else if (htab_find (visited_found.get (), file_data) != NULL)
5564 {
5565 per_cu->v.quick->mark = 1;
5566 continue;
5567 }
5568
5569 for (j = 0; j < file_data->num_file_names; ++j)
5570 {
5571 const char *this_real_name;
5572
5573 if (file_matcher (file_data->file_names[j], false))
5574 {
5575 per_cu->v.quick->mark = 1;
5576 break;
5577 }
5578
5579 /* Before we invoke realpath, which can get expensive when many
5580 files are involved, do a quick comparison of the basenames. */
5581 if (!basenames_may_differ
5582 && !file_matcher (lbasename (file_data->file_names[j]),
5583 true))
5584 continue;
5585
5586 this_real_name = dw2_get_real_path (objfile, file_data, j);
5587 if (file_matcher (this_real_name, false))
5588 {
5589 per_cu->v.quick->mark = 1;
5590 break;
5591 }
5592 }
5593
5594 slot = htab_find_slot (per_cu->v.quick->mark
5595 ? visited_found.get ()
5596 : visited_not_found.get (),
5597 file_data, INSERT);
5598 *slot = file_data;
5599 }
5600}
5601
5602static void
5603dw2_expand_symtabs_matching
5604 (struct objfile *objfile,
5605 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5606 const lookup_name_info &lookup_name,
5607 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5608 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5609 enum search_domain kind)
5610{
ed2dc618
SM
5611 struct dwarf2_per_objfile *dwarf2_per_objfile
5612 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5613
5614 /* index_table is NULL if OBJF_READNOW. */
5615 if (!dwarf2_per_objfile->index_table)
5616 return;
5617
ed2dc618 5618 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
5619
5620 mapped_index &index = *dwarf2_per_objfile->index_table;
5621
5622 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5623 symbol_matcher,
5624 kind, [&] (offset_type idx)
5625 {
ed2dc618 5626 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7
JK
5627 expansion_notify, kind);
5628 });
5629}
5630
5631/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5632 symtab. */
5633
5634static struct compunit_symtab *
5635recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5636 CORE_ADDR pc)
5637{
5638 int i;
5639
5640 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5641 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5642 return cust;
5643
5644 if (cust->includes == NULL)
5645 return NULL;
5646
5647 for (i = 0; cust->includes[i]; ++i)
5648 {
5649 struct compunit_symtab *s = cust->includes[i];
5650
5651 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5652 if (s != NULL)
5653 return s;
5654 }
5655
5656 return NULL;
5657}
5658
5659static struct compunit_symtab *
5660dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5661 struct bound_minimal_symbol msymbol,
5662 CORE_ADDR pc,
5663 struct obj_section *section,
5664 int warn_if_readin)
5665{
5666 struct dwarf2_per_cu_data *data;
5667 struct compunit_symtab *result;
5668
927aa2e7
JK
5669 if (!objfile->psymtabs_addrmap)
5670 return NULL;
5671
5672 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5673 pc);
5674 if (!data)
5675 return NULL;
5676
5677 if (warn_if_readin && data->v.quick->compunit_symtab)
5678 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5679 paddress (get_objfile_arch (objfile), pc));
5680
5681 result
5682 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5683 pc);
5684 gdb_assert (result != NULL);
5685 return result;
5686}
5687
5688static void
5689dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5690 void *data, int need_fullname)
5691{
ed2dc618
SM
5692 struct dwarf2_per_objfile *dwarf2_per_objfile
5693 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5694
5695 if (!dwarf2_per_objfile->filenames_cache)
5696 {
5697 dwarf2_per_objfile->filenames_cache.emplace ();
5698
5699 htab_up visited (htab_create_alloc (10,
5700 htab_hash_pointer, htab_eq_pointer,
5701 NULL, xcalloc, xfree));
5702
5703 /* The rule is CUs specify all the files, including those used
5704 by any TU, so there's no need to scan TUs here. We can
5705 ignore file names coming from already-expanded CUs. */
5706
5707 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5708 {
ed2dc618 5709 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
927aa2e7
JK
5710
5711 if (per_cu->v.quick->compunit_symtab)
5712 {
5713 void **slot = htab_find_slot (visited.get (),
5714 per_cu->v.quick->file_names,
5715 INSERT);
5716
5717 *slot = per_cu->v.quick->file_names;
5718 }
5719 }
5720
5721 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5722 {
ed2dc618 5723 dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
927aa2e7
JK
5724 struct quick_file_names *file_data;
5725 void **slot;
5726
5727 /* We only need to look at symtabs not already expanded. */
5728 if (per_cu->v.quick->compunit_symtab)
5729 continue;
5730
5731 file_data = dw2_get_file_names (per_cu);
5732 if (file_data == NULL)
5733 continue;
5734
5735 slot = htab_find_slot (visited.get (), file_data, INSERT);
5736 if (*slot)
5737 {
5738 /* Already visited. */
5739 continue;
5740 }
5741 *slot = file_data;
5742
5743 for (int j = 0; j < file_data->num_file_names; ++j)
5744 {
5745 const char *filename = file_data->file_names[j];
5746 dwarf2_per_objfile->filenames_cache->seen (filename);
5747 }
5748 }
5749 }
5750
5751 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5752 {
5753 gdb::unique_xmalloc_ptr<char> this_real_name;
5754
5755 if (need_fullname)
5756 this_real_name = gdb_realpath (filename);
5757 (*fun) (filename, this_real_name.get (), data);
5758 });
5759}
5760
5761static int
5762dw2_has_symbols (struct objfile *objfile)
5763{
5764 return 1;
5765}
5766
5767const struct quick_symbol_functions dwarf2_gdb_index_functions =
5768{
5769 dw2_has_symbols,
5770 dw2_find_last_source_symtab,
5771 dw2_forget_cached_source_info,
5772 dw2_map_symtabs_matching_filename,
5773 dw2_lookup_symbol,
5774 dw2_print_stats,
5775 dw2_dump,
5776 dw2_relocate,
5777 dw2_expand_symtabs_for_function,
5778 dw2_expand_all_symtabs,
5779 dw2_expand_symtabs_with_fullname,
5780 dw2_map_matching_symbols,
5781 dw2_expand_symtabs_matching,
5782 dw2_find_pc_sect_compunit_symtab,
5783 NULL,
5784 dw2_map_symbol_filenames
5785};
5786
5787/* DWARF-5 debug_names reader. */
5788
5789/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5790static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5791
5792/* A helper function that reads the .debug_names section in SECTION
5793 and fills in MAP. FILENAME is the name of the file containing the
5794 section; it is used for error reporting.
5795
5796 Returns true if all went well, false otherwise. */
5797
5798static bool
5799read_debug_names_from_section (struct objfile *objfile,
5800 const char *filename,
5801 struct dwarf2_section_info *section,
5802 mapped_debug_names &map)
5803{
5804 if (dwarf2_section_empty_p (section))
5805 return false;
5806
5807 /* Older elfutils strip versions could keep the section in the main
5808 executable while splitting it for the separate debug info file. */
5809 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5810 return false;
5811
5812 dwarf2_read_section (objfile, section);
5813
5814 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5815
5816 const gdb_byte *addr = section->buffer;
5817
5818 bfd *const abfd = get_section_bfd_owner (section);
5819
5820 unsigned int bytes_read;
5821 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5822 addr += bytes_read;
5823
5824 map.dwarf5_is_dwarf64 = bytes_read != 4;
5825 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5826 if (bytes_read + length != section->size)
5827 {
5828 /* There may be multiple per-CU indices. */
5829 warning (_("Section .debug_names in %s length %s does not match "
5830 "section length %s, ignoring .debug_names."),
5831 filename, plongest (bytes_read + length),
5832 pulongest (section->size));
5833 return false;
5834 }
5835
5836 /* The version number. */
5837 uint16_t version = read_2_bytes (abfd, addr);
5838 addr += 2;
5839 if (version != 5)
5840 {
5841 warning (_("Section .debug_names in %s has unsupported version %d, "
5842 "ignoring .debug_names."),
5843 filename, version);
5844 return false;
5845 }
5846
5847 /* Padding. */
5848 uint16_t padding = read_2_bytes (abfd, addr);
5849 addr += 2;
5850 if (padding != 0)
5851 {
5852 warning (_("Section .debug_names in %s has unsupported padding %d, "
5853 "ignoring .debug_names."),
5854 filename, padding);
5855 return false;
5856 }
5857
5858 /* comp_unit_count - The number of CUs in the CU list. */
5859 map.cu_count = read_4_bytes (abfd, addr);
5860 addr += 4;
5861
5862 /* local_type_unit_count - The number of TUs in the local TU
5863 list. */
5864 map.tu_count = read_4_bytes (abfd, addr);
5865 addr += 4;
5866
5867 /* foreign_type_unit_count - The number of TUs in the foreign TU
5868 list. */
5869 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5870 addr += 4;
5871 if (foreign_tu_count != 0)
5872 {
5873 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5874 "ignoring .debug_names."),
5875 filename, static_cast<unsigned long> (foreign_tu_count));
5876 return false;
5877 }
5878
5879 /* bucket_count - The number of hash buckets in the hash lookup
5880 table. */
5881 map.bucket_count = read_4_bytes (abfd, addr);
5882 addr += 4;
5883
5884 /* name_count - The number of unique names in the index. */
5885 map.name_count = read_4_bytes (abfd, addr);
5886 addr += 4;
5887
5888 /* abbrev_table_size - The size in bytes of the abbreviations
5889 table. */
5890 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5891 addr += 4;
5892
5893 /* augmentation_string_size - The size in bytes of the augmentation
5894 string. This value is rounded up to a multiple of 4. */
5895 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5896 addr += 4;
5897 map.augmentation_is_gdb = ((augmentation_string_size
5898 == sizeof (dwarf5_augmentation))
5899 && memcmp (addr, dwarf5_augmentation,
5900 sizeof (dwarf5_augmentation)) == 0);
5901 augmentation_string_size += (-augmentation_string_size) & 3;
5902 addr += augmentation_string_size;
5903
5904 /* List of CUs */
5905 map.cu_table_reordered = addr;
5906 addr += map.cu_count * map.offset_size;
5907
5908 /* List of Local TUs */
5909 map.tu_table_reordered = addr;
5910 addr += map.tu_count * map.offset_size;
5911
5912 /* Hash Lookup Table */
5913 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5914 addr += map.bucket_count * 4;
5915 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5916 addr += map.name_count * 4;
5917
5918 /* Name Table */
5919 map.name_table_string_offs_reordered = addr;
5920 addr += map.name_count * map.offset_size;
5921 map.name_table_entry_offs_reordered = addr;
5922 addr += map.name_count * map.offset_size;
5923
5924 const gdb_byte *abbrev_table_start = addr;
5925 for (;;)
5926 {
5927 unsigned int bytes_read;
5928 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5929 addr += bytes_read;
5930 if (index_num == 0)
5931 break;
5932
5933 const auto insertpair
5934 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5935 if (!insertpair.second)
5936 {
5937 warning (_("Section .debug_names in %s has duplicate index %s, "
5938 "ignoring .debug_names."),
5939 filename, pulongest (index_num));
5940 return false;
5941 }
5942 mapped_debug_names::index_val &indexval = insertpair.first->second;
5943 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5944 addr += bytes_read;
5945
5946 for (;;)
5947 {
5948 mapped_debug_names::index_val::attr attr;
5949 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5950 addr += bytes_read;
5951 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5952 addr += bytes_read;
5953 if (attr.form == DW_FORM_implicit_const)
5954 {
5955 attr.implicit_const = read_signed_leb128 (abfd, addr,
5956 &bytes_read);
5957 addr += bytes_read;
5958 }
5959 if (attr.dw_idx == 0 && attr.form == 0)
5960 break;
5961 indexval.attr_vec.push_back (std::move (attr));
5962 }
5963 }
5964 if (addr != abbrev_table_start + abbrev_table_size)
5965 {
5966 warning (_("Section .debug_names in %s has abbreviation_table "
5967 "of size %zu vs. written as %u, ignoring .debug_names."),
5968 filename, addr - abbrev_table_start, abbrev_table_size);
5969 return false;
5970 }
5971 map.entry_pool = addr;
5972
5973 return true;
5974}
5975
5976/* A helper for create_cus_from_debug_names that handles the MAP's CU
5977 list. */
5978
5979static void
ed2dc618 5980create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5981 const mapped_debug_names &map,
5982 dwarf2_section_info &section,
5983 bool is_dwz, int base_offset)
5984{
5985 sect_offset sect_off_prev;
5986 for (uint32_t i = 0; i <= map.cu_count; ++i)
5987 {
5988 sect_offset sect_off_next;
5989 if (i < map.cu_count)
5990 {
5991 sect_off_next
5992 = (sect_offset) (extract_unsigned_integer
5993 (map.cu_table_reordered + i * map.offset_size,
5994 map.offset_size,
5995 map.dwarf5_byte_order));
5996 }
5997 else
5998 sect_off_next = (sect_offset) section.size;
5999 if (i >= 1)
6000 {
6001 const ULONGEST length = sect_off_next - sect_off_prev;
6002 dwarf2_per_objfile->all_comp_units[base_offset + (i - 1)]
ed2dc618 6003 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7
JK
6004 sect_off_prev, length);
6005 }
6006 sect_off_prev = sect_off_next;
6007 }
6008}
6009
6010/* Read the CU list from the mapped index, and use it to create all
ed2dc618 6011 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
6012
6013static void
ed2dc618 6014create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
6015 const mapped_debug_names &map,
6016 const mapped_debug_names &dwz_map)
6017{
ed2dc618 6018 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
6019
6020 dwarf2_per_objfile->n_comp_units = map.cu_count + dwz_map.cu_count;
6021 dwarf2_per_objfile->all_comp_units
6022 = XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
6023 dwarf2_per_objfile->n_comp_units);
6024
ed2dc618
SM
6025 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
6026 dwarf2_per_objfile->info,
927aa2e7
JK
6027 false /* is_dwz */,
6028 0 /* base_offset */);
6029
6030 if (dwz_map.cu_count == 0)
6031 return;
6032
ed2dc618
SM
6033 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
6034 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
927aa2e7
JK
6035 true /* is_dwz */,
6036 map.cu_count /* base_offset */);
6037}
6038
6039/* Read .debug_names. If everything went ok, initialize the "quick"
6040 elements of all the CUs and return true. Otherwise, return false. */
6041
6042static bool
ed2dc618 6043dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 6044{
ed2dc618
SM
6045 mapped_debug_names local_map (dwarf2_per_objfile);
6046 mapped_debug_names dwz_map (dwarf2_per_objfile);
6047 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
6048
6049 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
6050 &dwarf2_per_objfile->debug_names,
6051 local_map))
6052 return false;
6053
6054 /* Don't use the index if it's empty. */
6055 if (local_map.name_count == 0)
6056 return false;
6057
6058 /* If there is a .dwz file, read it so we can get its CU list as
6059 well. */
ed2dc618 6060 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
6061 if (dwz != NULL)
6062 {
6063 if (!read_debug_names_from_section (objfile,
6064 bfd_get_filename (dwz->dwz_bfd),
6065 &dwz->debug_names, dwz_map))
6066 {
6067 warning (_("could not read '.debug_names' section from %s; skipping"),
6068 bfd_get_filename (dwz->dwz_bfd));
6069 return false;
6070 }
6071 }
6072
ed2dc618 6073 create_cus_from_debug_names (dwarf2_per_objfile, local_map, dwz_map);
927aa2e7
JK
6074
6075 if (local_map.tu_count != 0)
6076 {
6077 /* We can only handle a single .debug_types when we have an
6078 index. */
6079 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
6080 return false;
6081
6082 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
6083 dwarf2_per_objfile->types, 0);
6084
6085 create_signatured_type_table_from_debug_names
ed2dc618 6086 (dwarf2_per_objfile, local_map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
6087 }
6088
ed2dc618
SM
6089 create_addrmap_from_aranges (dwarf2_per_objfile,
6090 &dwarf2_per_objfile->debug_aranges);
927aa2e7 6091
ed2dc618
SM
6092 dwarf2_per_objfile->debug_names_table.reset
6093 (new mapped_debug_names (dwarf2_per_objfile));
927aa2e7
JK
6094 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
6095 dwarf2_per_objfile->using_index = 1;
6096 dwarf2_per_objfile->quick_file_names_table =
6097 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6098
6099 return true;
6100}
6101
6102/* Symbol name hashing function as specified by DWARF-5. */
6103
6104static uint32_t
6105dwarf5_djb_hash (const char *str_)
6106{
6107 const unsigned char *str = (const unsigned char *) str_;
6108
6109 /* Note: tolower here ignores UTF-8, which isn't fully compliant.
6110 See http://dwarfstd.org/ShowIssue.php?issue=161027.1. */
6111
6112 uint32_t hash = 5381;
6113 while (int c = *str++)
6114 hash = hash * 33 + tolower (c);
6115 return hash;
6116}
6117
6118/* Type used to manage iterating over all CUs looking for a symbol for
6119 .debug_names. */
6120
6121class dw2_debug_names_iterator
6122{
6123public:
6124 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
6125 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
6126 dw2_debug_names_iterator (const mapped_debug_names &map,
6127 bool want_specific_block,
6128 block_enum block_index, domain_enum domain,
6129 const char *name)
6130 : m_map (map), m_want_specific_block (want_specific_block),
6131 m_block_index (block_index), m_domain (domain),
6132 m_addr (find_vec_in_debug_names (map, name))
6133 {}
6134
6135 dw2_debug_names_iterator (const mapped_debug_names &map,
6136 search_domain search, uint32_t namei)
6137 : m_map (map),
6138 m_search (search),
6139 m_addr (find_vec_in_debug_names (map, namei))
6140 {}
6141
6142 /* Return the next matching CU or NULL if there are no more. */
6143 dwarf2_per_cu_data *next ();
6144
6145private:
6146 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6147 const char *name);
6148 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6149 uint32_t namei);
6150
6151 /* The internalized form of .debug_names. */
6152 const mapped_debug_names &m_map;
6153
6154 /* If true, only look for symbols that match BLOCK_INDEX. */
6155 const bool m_want_specific_block = false;
6156
6157 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
6158 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
6159 value. */
6160 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
6161
6162 /* The kind of symbol we're looking for. */
6163 const domain_enum m_domain = UNDEF_DOMAIN;
6164 const search_domain m_search = ALL_DOMAIN;
6165
6166 /* The list of CUs from the index entry of the symbol, or NULL if
6167 not found. */
6168 const gdb_byte *m_addr;
6169};
6170
6171const char *
6172mapped_debug_names::namei_to_name (uint32_t namei) const
6173{
6174 const ULONGEST namei_string_offs
6175 = extract_unsigned_integer ((name_table_string_offs_reordered
6176 + namei * offset_size),
6177 offset_size,
6178 dwarf5_byte_order);
6179 return read_indirect_string_at_offset
ed2dc618 6180 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
6181}
6182
6183/* Find a slot in .debug_names for the object named NAME. If NAME is
6184 found, return pointer to its pool data. If NAME cannot be found,
6185 return NULL. */
6186
6187const gdb_byte *
6188dw2_debug_names_iterator::find_vec_in_debug_names
6189 (const mapped_debug_names &map, const char *name)
6190{
6191 int (*cmp) (const char *, const char *);
6192
6193 if (current_language->la_language == language_cplus
6194 || current_language->la_language == language_fortran
6195 || current_language->la_language == language_d)
6196 {
6197 /* NAME is already canonical. Drop any qualifiers as
6198 .debug_names does not contain any. */
6199
6200 if (strchr (name, '(') != NULL)
6201 {
6202 gdb::unique_xmalloc_ptr<char> without_params
6203 = cp_remove_params (name);
6204
6205 if (without_params != NULL)
6206 {
6207 name = without_params.get();
6208 }
6209 }
6210 }
6211
6212 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
6213
6214 const uint32_t full_hash = dwarf5_djb_hash (name);
6215 uint32_t namei
6216 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6217 (map.bucket_table_reordered
6218 + (full_hash % map.bucket_count)), 4,
6219 map.dwarf5_byte_order);
6220 if (namei == 0)
6221 return NULL;
6222 --namei;
6223 if (namei >= map.name_count)
6224 {
6225 complaint (&symfile_complaints,
6226 _("Wrong .debug_names with name index %u but name_count=%u "
6227 "[in module %s]"),
6228 namei, map.name_count,
ed2dc618 6229 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
6230 return NULL;
6231 }
6232
6233 for (;;)
6234 {
6235 const uint32_t namei_full_hash
6236 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6237 (map.hash_table_reordered + namei), 4,
6238 map.dwarf5_byte_order);
6239 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
6240 return NULL;
6241
6242 if (full_hash == namei_full_hash)
6243 {
6244 const char *const namei_string = map.namei_to_name (namei);
6245
6246#if 0 /* An expensive sanity check. */
6247 if (namei_full_hash != dwarf5_djb_hash (namei_string))
6248 {
6249 complaint (&symfile_complaints,
6250 _("Wrong .debug_names hash for string at index %u "
6251 "[in module %s]"),
6252 namei, objfile_name (dwarf2_per_objfile->objfile));
6253 return NULL;
6254 }
6255#endif
6256
6257 if (cmp (namei_string, name) == 0)
6258 {
6259 const ULONGEST namei_entry_offs
6260 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6261 + namei * map.offset_size),
6262 map.offset_size, map.dwarf5_byte_order);
6263 return map.entry_pool + namei_entry_offs;
6264 }
6265 }
6266
6267 ++namei;
6268 if (namei >= map.name_count)
6269 return NULL;
6270 }
6271}
6272
6273const gdb_byte *
6274dw2_debug_names_iterator::find_vec_in_debug_names
6275 (const mapped_debug_names &map, uint32_t namei)
6276{
6277 if (namei >= map.name_count)
6278 {
6279 complaint (&symfile_complaints,
6280 _("Wrong .debug_names with name index %u but name_count=%u "
6281 "[in module %s]"),
6282 namei, map.name_count,
ed2dc618 6283 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
6284 return NULL;
6285 }
6286
6287 const ULONGEST namei_entry_offs
6288 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6289 + namei * map.offset_size),
6290 map.offset_size, map.dwarf5_byte_order);
6291 return map.entry_pool + namei_entry_offs;
6292}
6293
6294/* See dw2_debug_names_iterator. */
6295
6296dwarf2_per_cu_data *
6297dw2_debug_names_iterator::next ()
6298{
6299 if (m_addr == NULL)
6300 return NULL;
6301
ed2dc618
SM
6302 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
6303 struct objfile *objfile = dwarf2_per_objfile->objfile;
6304 bfd *const abfd = objfile->obfd;
927aa2e7
JK
6305
6306 again:
6307
6308 unsigned int bytes_read;
6309 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6310 m_addr += bytes_read;
6311 if (abbrev == 0)
6312 return NULL;
6313
6314 const auto indexval_it = m_map.abbrev_map.find (abbrev);
6315 if (indexval_it == m_map.abbrev_map.cend ())
6316 {
6317 complaint (&symfile_complaints,
6318 _("Wrong .debug_names undefined abbrev code %s "
6319 "[in module %s]"),
ed2dc618 6320 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
6321 return NULL;
6322 }
6323 const mapped_debug_names::index_val &indexval = indexval_it->second;
6324 bool have_is_static = false;
6325 bool is_static;
6326 dwarf2_per_cu_data *per_cu = NULL;
6327 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
6328 {
6329 ULONGEST ull;
6330 switch (attr.form)
6331 {
6332 case DW_FORM_implicit_const:
6333 ull = attr.implicit_const;
6334 break;
6335 case DW_FORM_flag_present:
6336 ull = 1;
6337 break;
6338 case DW_FORM_udata:
6339 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6340 m_addr += bytes_read;
6341 break;
6342 default:
6343 complaint (&symfile_complaints,
6344 _("Unsupported .debug_names form %s [in module %s]"),
6345 dwarf_form_name (attr.form),
ed2dc618 6346 objfile_name (objfile));
927aa2e7
JK
6347 return NULL;
6348 }
6349 switch (attr.dw_idx)
6350 {
6351 case DW_IDX_compile_unit:
6352 /* Don't crash on bad data. */
8af5c486 6353 if (ull >= dwarf2_per_objfile->n_comp_units)
927aa2e7
JK
6354 {
6355 complaint (&symfile_complaints,
6356 _(".debug_names entry has bad CU index %s"
6357 " [in module %s]"),
6358 pulongest (ull),
6359 objfile_name (dwarf2_per_objfile->objfile));
6360 continue;
6361 }
ed2dc618 6362 per_cu = dw2_get_cutu (dwarf2_per_objfile, ull);
927aa2e7 6363 break;
8af5c486
JK
6364 case DW_IDX_type_unit:
6365 /* Don't crash on bad data. */
6366 if (ull >= dwarf2_per_objfile->n_type_units)
6367 {
6368 complaint (&symfile_complaints,
6369 _(".debug_names entry has bad TU index %s"
6370 " [in module %s]"),
6371 pulongest (ull),
6372 objfile_name (dwarf2_per_objfile->objfile));
6373 continue;
6374 }
ed2dc618
SM
6375 per_cu = dw2_get_cutu (dwarf2_per_objfile,
6376 dwarf2_per_objfile->n_comp_units + ull);
8af5c486 6377 break;
927aa2e7
JK
6378 case DW_IDX_GNU_internal:
6379 if (!m_map.augmentation_is_gdb)
6380 break;
6381 have_is_static = true;
6382 is_static = true;
6383 break;
6384 case DW_IDX_GNU_external:
6385 if (!m_map.augmentation_is_gdb)
6386 break;
6387 have_is_static = true;
6388 is_static = false;
6389 break;
6390 }
6391 }
6392
6393 /* Skip if already read in. */
6394 if (per_cu->v.quick->compunit_symtab)
6395 goto again;
6396
6397 /* Check static vs global. */
6398 if (have_is_static)
6399 {
6400 const bool want_static = m_block_index != GLOBAL_BLOCK;
6401 if (m_want_specific_block && want_static != is_static)
6402 goto again;
6403 }
6404
6405 /* Match dw2_symtab_iter_next, symbol_kind
6406 and debug_names::psymbol_tag. */
6407 switch (m_domain)
6408 {
6409 case VAR_DOMAIN:
6410 switch (indexval.dwarf_tag)
6411 {
6412 case DW_TAG_variable:
6413 case DW_TAG_subprogram:
6414 /* Some types are also in VAR_DOMAIN. */
6415 case DW_TAG_typedef:
6416 case DW_TAG_structure_type:
6417 break;
6418 default:
6419 goto again;
6420 }
6421 break;
6422 case STRUCT_DOMAIN:
6423 switch (indexval.dwarf_tag)
6424 {
6425 case DW_TAG_typedef:
6426 case DW_TAG_structure_type:
6427 break;
6428 default:
6429 goto again;
6430 }
6431 break;
6432 case LABEL_DOMAIN:
6433 switch (indexval.dwarf_tag)
6434 {
6435 case 0:
6436 case DW_TAG_variable:
6437 break;
6438 default:
6439 goto again;
6440 }
6441 break;
6442 default:
6443 break;
6444 }
6445
6446 /* Match dw2_expand_symtabs_matching, symbol_kind and
6447 debug_names::psymbol_tag. */
6448 switch (m_search)
4b514bc8 6449 {
927aa2e7
JK
6450 case VARIABLES_DOMAIN:
6451 switch (indexval.dwarf_tag)
4b514bc8 6452 {
927aa2e7
JK
6453 case DW_TAG_variable:
6454 break;
6455 default:
6456 goto again;
4b514bc8 6457 }
927aa2e7
JK
6458 break;
6459 case FUNCTIONS_DOMAIN:
6460 switch (indexval.dwarf_tag)
4b514bc8 6461 {
927aa2e7
JK
6462 case DW_TAG_subprogram:
6463 break;
6464 default:
6465 goto again;
4b514bc8 6466 }
927aa2e7
JK
6467 break;
6468 case TYPES_DOMAIN:
6469 switch (indexval.dwarf_tag)
6470 {
6471 case DW_TAG_typedef:
6472 case DW_TAG_structure_type:
6473 break;
6474 default:
6475 goto again;
6476 }
6477 break;
6478 default:
6479 break;
4b514bc8 6480 }
927aa2e7
JK
6481
6482 return per_cu;
4b514bc8 6483}
61920122 6484
927aa2e7
JK
6485static struct compunit_symtab *
6486dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6487 const char *name, domain_enum domain)
4b514bc8 6488{
927aa2e7 6489 const block_enum block_index = static_cast<block_enum> (block_index_int);
ed2dc618
SM
6490 struct dwarf2_per_objfile *dwarf2_per_objfile
6491 = get_dwarf2_per_objfile (objfile);
61920122 6492
927aa2e7
JK
6493 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6494 if (!mapp)
61920122 6495 {
927aa2e7
JK
6496 /* index is NULL if OBJF_READNOW. */
6497 return NULL;
6498 }
6499 const auto &map = *mapp;
9291a0cd 6500
927aa2e7
JK
6501 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6502 block_index, domain, name);
9703b513 6503
927aa2e7
JK
6504 struct compunit_symtab *stab_best = NULL;
6505 struct dwarf2_per_cu_data *per_cu;
6506 while ((per_cu = iter.next ()) != NULL)
6507 {
6508 struct symbol *sym, *with_opaque = NULL;
6509 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6510 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6511 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6512
927aa2e7
JK
6513 sym = block_find_symbol (block, name, domain,
6514 block_find_non_opaque_type_preferred,
6515 &with_opaque);
9703b513 6516
927aa2e7
JK
6517 /* Some caution must be observed with overloaded functions and
6518 methods, since the index will not contain any overload
6519 information (but NAME might contain it). */
a3ec0bb1 6520
927aa2e7
JK
6521 if (sym != NULL
6522 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6523 return stab;
6524 if (with_opaque != NULL
6525 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6526 stab_best = stab;
9703b513 6527
927aa2e7 6528 /* Keep looking through other CUs. */
9703b513
TT
6529 }
6530
927aa2e7 6531 return stab_best;
9703b513
TT
6532}
6533
927aa2e7
JK
6534/* This dumps minimal information about .debug_names. It is called
6535 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6536 uses this to verify that .debug_names has been loaded. */
9291a0cd 6537
927aa2e7
JK
6538static void
6539dw2_debug_names_dump (struct objfile *objfile)
6540{
ed2dc618
SM
6541 struct dwarf2_per_objfile *dwarf2_per_objfile
6542 = get_dwarf2_per_objfile (objfile);
6543
927aa2e7
JK
6544 gdb_assert (dwarf2_per_objfile->using_index);
6545 printf_filtered (".debug_names:");
6546 if (dwarf2_per_objfile->debug_names_table)
6547 printf_filtered (" exists\n");
6548 else
6549 printf_filtered (" faked for \"readnow\"\n");
6550 printf_filtered ("\n");
9291a0cd
TT
6551}
6552
9291a0cd 6553static void
927aa2e7
JK
6554dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6555 const char *func_name)
9291a0cd 6556{
ed2dc618
SM
6557 struct dwarf2_per_objfile *dwarf2_per_objfile
6558 = get_dwarf2_per_objfile (objfile);
ae2de4f8 6559
927aa2e7
JK
6560 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6561 if (dwarf2_per_objfile->debug_names_table)
24c79950 6562 {
927aa2e7 6563 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6564
927aa2e7
JK
6565 /* Note: It doesn't matter what we pass for block_index here. */
6566 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6567 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
24c79950 6568
927aa2e7
JK
6569 struct dwarf2_per_cu_data *per_cu;
6570 while ((per_cu = iter.next ()) != NULL)
6571 dw2_instantiate_symtab (per_cu);
6572 }
6573}
24c79950 6574
927aa2e7
JK
6575static void
6576dw2_debug_names_expand_symtabs_matching
6577 (struct objfile *objfile,
6578 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6579 const lookup_name_info &lookup_name,
6580 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6581 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6582 enum search_domain kind)
6583{
ed2dc618
SM
6584 struct dwarf2_per_objfile *dwarf2_per_objfile
6585 = get_dwarf2_per_objfile (objfile);
9291a0cd 6586
927aa2e7
JK
6587 /* debug_names_table is NULL if OBJF_READNOW. */
6588 if (!dwarf2_per_objfile->debug_names_table)
6589 return;
9291a0cd 6590
ed2dc618 6591 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 6592
44ed8f3e 6593 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6594
44ed8f3e
PA
6595 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6596 symbol_matcher,
6597 kind, [&] (offset_type namei)
927aa2e7 6598 {
927aa2e7
JK
6599 /* The name was matched, now expand corresponding CUs that were
6600 marked. */
6601 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6602
927aa2e7
JK
6603 struct dwarf2_per_cu_data *per_cu;
6604 while ((per_cu = iter.next ()) != NULL)
6605 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6606 expansion_notify);
44ed8f3e 6607 });
9291a0cd
TT
6608}
6609
927aa2e7 6610const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6611{
6612 dw2_has_symbols,
6613 dw2_find_last_source_symtab,
6614 dw2_forget_cached_source_info,
f8eba3c6 6615 dw2_map_symtabs_matching_filename,
927aa2e7 6616 dw2_debug_names_lookup_symbol,
9291a0cd 6617 dw2_print_stats,
927aa2e7 6618 dw2_debug_names_dump,
9291a0cd 6619 dw2_relocate,
927aa2e7 6620 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6621 dw2_expand_all_symtabs,
652a8996 6622 dw2_expand_symtabs_with_fullname,
40658b94 6623 dw2_map_matching_symbols,
927aa2e7 6624 dw2_debug_names_expand_symtabs_matching,
43f3e411 6625 dw2_find_pc_sect_compunit_symtab,
71a3c369 6626 NULL,
9291a0cd
TT
6627 dw2_map_symbol_filenames
6628};
6629
3c0aa29a 6630/* See symfile.h. */
9291a0cd 6631
3c0aa29a
PA
6632bool
6633dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6634{
ed2dc618
SM
6635 struct dwarf2_per_objfile *dwarf2_per_objfile
6636 = get_dwarf2_per_objfile (objfile);
6637
9291a0cd
TT
6638 /* If we're about to read full symbols, don't bother with the
6639 indices. In this case we also don't care if some other debug
6640 format is making psymtabs, because they are all about to be
6641 expanded anyway. */
6642 if ((objfile->flags & OBJF_READNOW))
6643 {
6644 int i;
6645
6646 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
6647 create_all_comp_units (dwarf2_per_objfile);
6648 create_all_type_units (dwarf2_per_objfile);
7b9f3c50
DE
6649 dwarf2_per_objfile->quick_file_names_table =
6650 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 6651
1fd400ff 6652 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 6653 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 6654 {
ed2dc618 6655 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
9291a0cd 6656
e254ef6a
DE
6657 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6658 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6659 }
6660
6661 /* Return 1 so that gdb sees the "quick" functions. However,
6662 these functions will be no-ops because we will have expanded
6663 all symtabs. */
3c0aa29a
PA
6664 *index_kind = dw_index_kind::GDB_INDEX;
6665 return true;
9291a0cd
TT
6666 }
6667
ed2dc618 6668 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6669 {
6670 *index_kind = dw_index_kind::DEBUG_NAMES;
6671 return true;
6672 }
927aa2e7 6673
9291a0cd 6674 if (dwarf2_read_index (objfile))
3c0aa29a
PA
6675 {
6676 *index_kind = dw_index_kind::GDB_INDEX;
6677 return true;
6678 }
9291a0cd 6679
3c0aa29a 6680 return false;
9291a0cd
TT
6681}
6682
6683\f
6684
dce234bc
PP
6685/* Build a partial symbol table. */
6686
6687void
f29dff0a 6688dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6689{
ed2dc618
SM
6690 struct dwarf2_per_objfile *dwarf2_per_objfile
6691 = get_dwarf2_per_objfile (objfile);
c9bf0622 6692
af5bf4ad
SM
6693 if (objfile->global_psymbols.capacity () == 0
6694 && objfile->static_psymbols.capacity () == 0)
6695 init_psymbol_list (objfile, 1024);
c906108c 6696
492d29ea 6697 TRY
c9bf0622
TT
6698 {
6699 /* This isn't really ideal: all the data we allocate on the
6700 objfile's obstack is still uselessly kept around. However,
6701 freeing it seems unsafe. */
906768f9 6702 psymtab_discarder psymtabs (objfile);
ed2dc618 6703 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6704 psymtabs.keep ();
c9bf0622 6705 }
492d29ea
PA
6706 CATCH (except, RETURN_MASK_ERROR)
6707 {
6708 exception_print (gdb_stderr, except);
6709 }
6710 END_CATCH
c906108c 6711}
c906108c 6712
1ce1cefd
DE
6713/* Return the total length of the CU described by HEADER. */
6714
6715static unsigned int
6716get_cu_length (const struct comp_unit_head *header)
6717{
6718 return header->initial_length_size + header->length;
6719}
6720
9c541725 6721/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6722
9c541725
PA
6723static inline bool
6724offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6725{
9c541725
PA
6726 sect_offset bottom = cu_header->sect_off;
6727 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6728
9c541725 6729 return sect_off >= bottom && sect_off < top;
45452591
DE
6730}
6731
3b80fe9b
DE
6732/* Find the base address of the compilation unit for range lists and
6733 location lists. It will normally be specified by DW_AT_low_pc.
6734 In DWARF-3 draft 4, the base address could be overridden by
6735 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6736 compilation units with discontinuous ranges. */
6737
6738static void
6739dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6740{
6741 struct attribute *attr;
6742
6743 cu->base_known = 0;
6744 cu->base_address = 0;
6745
6746 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6747 if (attr)
6748 {
31aa7e4e 6749 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6750 cu->base_known = 1;
6751 }
6752 else
6753 {
6754 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6755 if (attr)
6756 {
31aa7e4e 6757 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6758 cu->base_known = 1;
6759 }
6760 }
6761}
6762
93311388 6763/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6764 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6765 NOTE: This leaves members offset, first_die_offset to be filled in
6766 by the caller. */
107d2387 6767
d521ce57 6768static const gdb_byte *
107d2387 6769read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6770 const gdb_byte *info_ptr,
6771 struct dwarf2_section_info *section,
6772 rcuh_kind section_kind)
107d2387
AC
6773{
6774 int signed_addr;
891d2f0b 6775 unsigned int bytes_read;
43988095
JK
6776 const char *filename = get_section_file_name (section);
6777 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6778
6779 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6780 cu_header->initial_length_size = bytes_read;
6781 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6782 info_ptr += bytes_read;
107d2387
AC
6783 cu_header->version = read_2_bytes (abfd, info_ptr);
6784 info_ptr += 2;
43988095
JK
6785 if (cu_header->version < 5)
6786 switch (section_kind)
6787 {
6788 case rcuh_kind::COMPILE:
6789 cu_header->unit_type = DW_UT_compile;
6790 break;
6791 case rcuh_kind::TYPE:
6792 cu_header->unit_type = DW_UT_type;
6793 break;
6794 default:
6795 internal_error (__FILE__, __LINE__,
6796 _("read_comp_unit_head: invalid section_kind"));
6797 }
6798 else
6799 {
6800 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6801 (read_1_byte (abfd, info_ptr));
6802 info_ptr += 1;
6803 switch (cu_header->unit_type)
6804 {
6805 case DW_UT_compile:
6806 if (section_kind != rcuh_kind::COMPILE)
6807 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6808 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6809 filename);
6810 break;
6811 case DW_UT_type:
6812 section_kind = rcuh_kind::TYPE;
6813 break;
6814 default:
6815 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6816 "(is %d, should be %d or %d) [in module %s]"),
6817 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6818 }
6819
6820 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6821 info_ptr += 1;
6822 }
9c541725
PA
6823 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6824 cu_header,
6825 &bytes_read);
613e1657 6826 info_ptr += bytes_read;
43988095
JK
6827 if (cu_header->version < 5)
6828 {
6829 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6830 info_ptr += 1;
6831 }
107d2387
AC
6832 signed_addr = bfd_get_sign_extend_vma (abfd);
6833 if (signed_addr < 0)
8e65ff28 6834 internal_error (__FILE__, __LINE__,
e2e0b3e5 6835 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6836 cu_header->signed_addr_p = signed_addr;
c764a876 6837
43988095
JK
6838 if (section_kind == rcuh_kind::TYPE)
6839 {
6840 LONGEST type_offset;
6841
6842 cu_header->signature = read_8_bytes (abfd, info_ptr);
6843 info_ptr += 8;
6844
6845 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6846 info_ptr += bytes_read;
9c541725
PA
6847 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6848 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6849 error (_("Dwarf Error: Too big type_offset in compilation unit "
6850 "header (is %s) [in module %s]"), plongest (type_offset),
6851 filename);
6852 }
6853
107d2387
AC
6854 return info_ptr;
6855}
6856
36586728
TT
6857/* Helper function that returns the proper abbrev section for
6858 THIS_CU. */
6859
6860static struct dwarf2_section_info *
6861get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6862{
6863 struct dwarf2_section_info *abbrev;
ed2dc618 6864 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6865
6866 if (this_cu->is_dwz)
ed2dc618 6867 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6868 else
6869 abbrev = &dwarf2_per_objfile->abbrev;
6870
6871 return abbrev;
6872}
6873
9ff913ba
DE
6874/* Subroutine of read_and_check_comp_unit_head and
6875 read_and_check_type_unit_head to simplify them.
6876 Perform various error checking on the header. */
6877
6878static void
ed2dc618
SM
6879error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6880 struct comp_unit_head *header,
4bdcc0c1
DE
6881 struct dwarf2_section_info *section,
6882 struct dwarf2_section_info *abbrev_section)
9ff913ba 6883{
a32a8923 6884 const char *filename = get_section_file_name (section);
9ff913ba 6885
43988095 6886 if (header->version < 2 || header->version > 5)
9ff913ba 6887 error (_("Dwarf Error: wrong version in compilation unit header "
43988095 6888 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
9ff913ba
DE
6889 filename);
6890
9c541725 6891 if (to_underlying (header->abbrev_sect_off)
36586728 6892 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9d8780f0
SM
6893 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6894 "(offset %s + 6) [in module %s]"),
6895 sect_offset_str (header->abbrev_sect_off),
6896 sect_offset_str (header->sect_off),
9ff913ba
DE
6897 filename);
6898
9c541725 6899 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6900 avoid potential 32-bit overflow. */
9c541725 6901 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6902 > section->size)
9c541725 6903 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
9d8780f0
SM
6904 "(offset %s + 0) [in module %s]"),
6905 header->length, sect_offset_str (header->sect_off),
9ff913ba
DE
6906 filename);
6907}
6908
6909/* Read in a CU/TU header and perform some basic error checking.
6910 The contents of the header are stored in HEADER.
6911 The result is a pointer to the start of the first DIE. */
adabb602 6912
d521ce57 6913static const gdb_byte *
ed2dc618
SM
6914read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6915 struct comp_unit_head *header,
9ff913ba 6916 struct dwarf2_section_info *section,
4bdcc0c1 6917 struct dwarf2_section_info *abbrev_section,
d521ce57 6918 const gdb_byte *info_ptr,
43988095 6919 rcuh_kind section_kind)
72bf9492 6920{
d521ce57 6921 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6922
9c541725 6923 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6924
43988095 6925 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6926
9c541725 6927 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6928
ed2dc618
SM
6929 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6930 abbrev_section);
9ff913ba
DE
6931
6932 return info_ptr;
348e048f
DE
6933}
6934
f4dc4d17
DE
6935/* Fetch the abbreviation table offset from a comp or type unit header. */
6936
6937static sect_offset
ed2dc618
SM
6938read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6939 struct dwarf2_section_info *section,
9c541725 6940 sect_offset sect_off)
f4dc4d17 6941{
a32a8923 6942 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6943 const gdb_byte *info_ptr;
ac298888 6944 unsigned int initial_length_size, offset_size;
43988095 6945 uint16_t version;
f4dc4d17
DE
6946
6947 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6948 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6949 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6950 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6951 info_ptr += initial_length_size;
6952
6953 version = read_2_bytes (abfd, info_ptr);
6954 info_ptr += 2;
6955 if (version >= 5)
6956 {
6957 /* Skip unit type and address size. */
6958 info_ptr += 2;
6959 }
6960
9c541725 6961 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6962}
6963
aaa75496
JB
6964/* Allocate a new partial symtab for file named NAME and mark this new
6965 partial symtab as being an include of PST. */
6966
6967static void
d521ce57 6968dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
6969 struct objfile *objfile)
6970{
6971 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6972
fbd9ab74
JK
6973 if (!IS_ABSOLUTE_PATH (subpst->filename))
6974 {
6975 /* It shares objfile->objfile_obstack. */
6976 subpst->dirname = pst->dirname;
6977 }
6978
aaa75496
JB
6979 subpst->textlow = 0;
6980 subpst->texthigh = 0;
6981
8d749320
SM
6982 subpst->dependencies
6983 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
6984 subpst->dependencies[0] = pst;
6985 subpst->number_of_dependencies = 1;
6986
6987 subpst->globals_offset = 0;
6988 subpst->n_global_syms = 0;
6989 subpst->statics_offset = 0;
6990 subpst->n_static_syms = 0;
43f3e411 6991 subpst->compunit_symtab = NULL;
aaa75496
JB
6992 subpst->read_symtab = pst->read_symtab;
6993 subpst->readin = 0;
6994
6995 /* No private part is necessary for include psymtabs. This property
6996 can be used to differentiate between such include psymtabs and
10b3939b 6997 the regular ones. */
58a9656e 6998 subpst->read_symtab_private = NULL;
aaa75496
JB
6999}
7000
7001/* Read the Line Number Program data and extract the list of files
7002 included by the source file represented by PST. Build an include
d85a05f0 7003 partial symtab for each of these included files. */
aaa75496
JB
7004
7005static void
7006dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
7007 struct die_info *die,
7008 struct partial_symtab *pst)
aaa75496 7009{
fff8551c 7010 line_header_up lh;
d85a05f0 7011 struct attribute *attr;
aaa75496 7012
d85a05f0
DJ
7013 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7014 if (attr)
9c541725 7015 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
7016 if (lh == NULL)
7017 return; /* No linetable, so no includes. */
7018
c6da4cef 7019 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
fff8551c 7020 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
7021}
7022
348e048f 7023static hashval_t
52dc124a 7024hash_signatured_type (const void *item)
348e048f 7025{
9a3c8263
SM
7026 const struct signatured_type *sig_type
7027 = (const struct signatured_type *) item;
9a619af0 7028
348e048f 7029 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 7030 return sig_type->signature;
348e048f
DE
7031}
7032
7033static int
52dc124a 7034eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 7035{
9a3c8263
SM
7036 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
7037 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 7038
348e048f
DE
7039 return lhs->signature == rhs->signature;
7040}
7041
1fd400ff
TT
7042/* Allocate a hash table for signatured types. */
7043
7044static htab_t
673bfd45 7045allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
7046{
7047 return htab_create_alloc_ex (41,
52dc124a
DE
7048 hash_signatured_type,
7049 eq_signatured_type,
1fd400ff
TT
7050 NULL,
7051 &objfile->objfile_obstack,
7052 hashtab_obstack_allocate,
7053 dummy_obstack_deallocate);
7054}
7055
d467dd73 7056/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
7057
7058static int
d467dd73 7059add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 7060{
9a3c8263
SM
7061 struct signatured_type *sigt = (struct signatured_type *) *slot;
7062 struct signatured_type ***datap = (struct signatured_type ***) datum;
1fd400ff 7063
b4dd5633 7064 **datap = sigt;
1fd400ff
TT
7065 ++*datap;
7066
7067 return 1;
7068}
7069
78d4d2c5 7070/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
7071 and fill them into TYPES_HTAB. It will process only type units,
7072 therefore DW_UT_type. */
c88ee1f0 7073
78d4d2c5 7074static void
ed2dc618
SM
7075create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
7076 struct dwo_file *dwo_file,
43988095
JK
7077 dwarf2_section_info *section, htab_t &types_htab,
7078 rcuh_kind section_kind)
348e048f 7079{
3019eac3 7080 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 7081 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
7082 bfd *abfd;
7083 const gdb_byte *info_ptr, *end_ptr;
348e048f 7084
4bdcc0c1
DE
7085 abbrev_section = (dwo_file != NULL
7086 ? &dwo_file->sections.abbrev
7087 : &dwarf2_per_objfile->abbrev);
7088
b4f54984 7089 if (dwarf_read_debug)
43988095
JK
7090 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
7091 get_section_name (section),
a32a8923 7092 get_section_file_name (abbrev_section));
09406207 7093
78d4d2c5
JK
7094 dwarf2_read_section (objfile, section);
7095 info_ptr = section->buffer;
348e048f 7096
78d4d2c5
JK
7097 if (info_ptr == NULL)
7098 return;
348e048f 7099
78d4d2c5
JK
7100 /* We can't set abfd until now because the section may be empty or
7101 not present, in which case the bfd is unknown. */
7102 abfd = get_section_bfd_owner (section);
348e048f 7103
78d4d2c5
JK
7104 /* We don't use init_cutu_and_read_dies_simple, or some such, here
7105 because we don't need to read any dies: the signature is in the
7106 header. */
3019eac3 7107
78d4d2c5
JK
7108 end_ptr = info_ptr + section->size;
7109 while (info_ptr < end_ptr)
7110 {
78d4d2c5
JK
7111 struct signatured_type *sig_type;
7112 struct dwo_unit *dwo_tu;
7113 void **slot;
7114 const gdb_byte *ptr = info_ptr;
7115 struct comp_unit_head header;
7116 unsigned int length;
8b70b953 7117
9c541725 7118 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 7119
a49dd8dd
JK
7120 /* Initialize it due to a false compiler warning. */
7121 header.signature = -1;
9c541725 7122 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 7123
78d4d2c5
JK
7124 /* We need to read the type's signature in order to build the hash
7125 table, but we don't need anything else just yet. */
348e048f 7126
ed2dc618 7127 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 7128 abbrev_section, ptr, section_kind);
348e048f 7129
78d4d2c5 7130 length = get_cu_length (&header);
6caca83c 7131
78d4d2c5
JK
7132 /* Skip dummy type units. */
7133 if (ptr >= info_ptr + length
43988095
JK
7134 || peek_abbrev_code (abfd, ptr) == 0
7135 || header.unit_type != DW_UT_type)
78d4d2c5
JK
7136 {
7137 info_ptr += length;
7138 continue;
7139 }
dee91e82 7140
78d4d2c5
JK
7141 if (types_htab == NULL)
7142 {
7143 if (dwo_file)
7144 types_htab = allocate_dwo_unit_table (objfile);
7145 else
7146 types_htab = allocate_signatured_type_table (objfile);
7147 }
8b70b953 7148
78d4d2c5
JK
7149 if (dwo_file)
7150 {
7151 sig_type = NULL;
7152 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7153 struct dwo_unit);
7154 dwo_tu->dwo_file = dwo_file;
43988095 7155 dwo_tu->signature = header.signature;
9c541725 7156 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 7157 dwo_tu->section = section;
9c541725 7158 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
7159 dwo_tu->length = length;
7160 }
7161 else
7162 {
7163 /* N.B.: type_offset is not usable if this type uses a DWO file.
7164 The real type_offset is in the DWO file. */
7165 dwo_tu = NULL;
7166 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7167 struct signatured_type);
43988095 7168 sig_type->signature = header.signature;
9c541725 7169 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 7170 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
7171 sig_type->per_cu.is_debug_types = 1;
7172 sig_type->per_cu.section = section;
9c541725 7173 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
7174 sig_type->per_cu.length = length;
7175 }
7176
7177 slot = htab_find_slot (types_htab,
7178 dwo_file ? (void*) dwo_tu : (void *) sig_type,
7179 INSERT);
7180 gdb_assert (slot != NULL);
7181 if (*slot != NULL)
7182 {
9c541725 7183 sect_offset dup_sect_off;
0349ea22 7184
3019eac3
DE
7185 if (dwo_file)
7186 {
78d4d2c5
JK
7187 const struct dwo_unit *dup_tu
7188 = (const struct dwo_unit *) *slot;
7189
9c541725 7190 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
7191 }
7192 else
7193 {
78d4d2c5
JK
7194 const struct signatured_type *dup_tu
7195 = (const struct signatured_type *) *slot;
7196
9c541725 7197 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 7198 }
8b70b953 7199
78d4d2c5 7200 complaint (&symfile_complaints,
9d8780f0
SM
7201 _("debug type entry at offset %s is duplicate to"
7202 " the entry at offset %s, signature %s"),
7203 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 7204 hex_string (header.signature));
78d4d2c5
JK
7205 }
7206 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 7207
78d4d2c5 7208 if (dwarf_read_debug > 1)
9d8780f0
SM
7209 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
7210 sect_offset_str (sect_off),
43988095 7211 hex_string (header.signature));
3019eac3 7212
78d4d2c5
JK
7213 info_ptr += length;
7214 }
7215}
3019eac3 7216
78d4d2c5
JK
7217/* Create the hash table of all entries in the .debug_types
7218 (or .debug_types.dwo) section(s).
7219 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
7220 otherwise it is NULL.
b3c8eb43 7221
78d4d2c5 7222 The result is a pointer to the hash table or NULL if there are no types.
348e048f 7223
78d4d2c5 7224 Note: This function processes DWO files only, not DWP files. */
348e048f 7225
78d4d2c5 7226static void
ed2dc618
SM
7227create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
7228 struct dwo_file *dwo_file,
78d4d2c5
JK
7229 VEC (dwarf2_section_info_def) *types,
7230 htab_t &types_htab)
7231{
7232 int ix;
7233 struct dwarf2_section_info *section;
7234
7235 if (VEC_empty (dwarf2_section_info_def, types))
7236 return;
348e048f 7237
78d4d2c5
JK
7238 for (ix = 0;
7239 VEC_iterate (dwarf2_section_info_def, types, ix, section);
7240 ++ix)
ed2dc618
SM
7241 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
7242 types_htab, rcuh_kind::TYPE);
3019eac3
DE
7243}
7244
7245/* Create the hash table of all entries in the .debug_types section,
7246 and initialize all_type_units.
7247 The result is zero if there is an error (e.g. missing .debug_types section),
7248 otherwise non-zero. */
7249
7250static int
ed2dc618 7251create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 7252{
78d4d2c5 7253 htab_t types_htab = NULL;
b4dd5633 7254 struct signatured_type **iter;
3019eac3 7255
ed2dc618
SM
7256 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
7257 &dwarf2_per_objfile->info, types_htab,
43988095 7258 rcuh_kind::COMPILE);
ed2dc618
SM
7259 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
7260 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
7261 if (types_htab == NULL)
7262 {
7263 dwarf2_per_objfile->signatured_types = NULL;
7264 return 0;
7265 }
7266
348e048f
DE
7267 dwarf2_per_objfile->signatured_types = types_htab;
7268
6aa5f3a6
DE
7269 dwarf2_per_objfile->n_type_units
7270 = dwarf2_per_objfile->n_allocated_type_units
7271 = htab_elements (types_htab);
8d749320
SM
7272 dwarf2_per_objfile->all_type_units =
7273 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
d467dd73
DE
7274 iter = &dwarf2_per_objfile->all_type_units[0];
7275 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
7276 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
7277 == dwarf2_per_objfile->n_type_units);
1fd400ff 7278
348e048f
DE
7279 return 1;
7280}
7281
6aa5f3a6
DE
7282/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
7283 If SLOT is non-NULL, it is the entry to use in the hash table.
7284 Otherwise we find one. */
7285
7286static struct signatured_type *
ed2dc618
SM
7287add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
7288 void **slot)
6aa5f3a6
DE
7289{
7290 struct objfile *objfile = dwarf2_per_objfile->objfile;
7291 int n_type_units = dwarf2_per_objfile->n_type_units;
7292 struct signatured_type *sig_type;
7293
7294 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
7295 ++n_type_units;
7296 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
7297 {
7298 if (dwarf2_per_objfile->n_allocated_type_units == 0)
7299 dwarf2_per_objfile->n_allocated_type_units = 1;
7300 dwarf2_per_objfile->n_allocated_type_units *= 2;
7301 dwarf2_per_objfile->all_type_units
224c3ddb
SM
7302 = XRESIZEVEC (struct signatured_type *,
7303 dwarf2_per_objfile->all_type_units,
7304 dwarf2_per_objfile->n_allocated_type_units);
6aa5f3a6
DE
7305 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
7306 }
7307 dwarf2_per_objfile->n_type_units = n_type_units;
7308
7309 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7310 struct signatured_type);
7311 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
7312 sig_type->signature = sig;
7313 sig_type->per_cu.is_debug_types = 1;
7314 if (dwarf2_per_objfile->using_index)
7315 {
7316 sig_type->per_cu.v.quick =
7317 OBSTACK_ZALLOC (&objfile->objfile_obstack,
7318 struct dwarf2_per_cu_quick_data);
7319 }
7320
7321 if (slot == NULL)
7322 {
7323 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7324 sig_type, INSERT);
7325 }
7326 gdb_assert (*slot == NULL);
7327 *slot = sig_type;
7328 /* The rest of sig_type must be filled in by the caller. */
7329 return sig_type;
7330}
7331
a2ce51a0
DE
7332/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7333 Fill in SIG_ENTRY with DWO_ENTRY. */
7334
7335static void
ed2dc618 7336fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
7337 struct signatured_type *sig_entry,
7338 struct dwo_unit *dwo_entry)
7339{
7ee85ab1 7340 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
7341 gdb_assert (! sig_entry->per_cu.queued);
7342 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
7343 if (dwarf2_per_objfile->using_index)
7344 {
7345 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 7346 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
7347 }
7348 else
7349 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 7350 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 7351 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 7352 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
7353 gdb_assert (sig_entry->dwo_unit == NULL);
7354
7355 sig_entry->per_cu.section = dwo_entry->section;
9c541725 7356 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
7357 sig_entry->per_cu.length = dwo_entry->length;
7358 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 7359 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
7360 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7361 sig_entry->dwo_unit = dwo_entry;
7362}
7363
7364/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
7365 If we haven't read the TU yet, create the signatured_type data structure
7366 for a TU to be read in directly from a DWO file, bypassing the stub.
7367 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7368 using .gdb_index, then when reading a CU we want to stay in the DWO file
7369 containing that CU. Otherwise we could end up reading several other DWO
7370 files (due to comdat folding) to process the transitive closure of all the
7371 mentioned TUs, and that can be slow. The current DWO file will have every
7372 type signature that it needs.
a2ce51a0
DE
7373 We only do this for .gdb_index because in the psymtab case we already have
7374 to read all the DWOs to build the type unit groups. */
7375
7376static struct signatured_type *
7377lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7378{
518817b3
SM
7379 struct dwarf2_per_objfile *dwarf2_per_objfile
7380 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
7381 struct objfile *objfile = dwarf2_per_objfile->objfile;
7382 struct dwo_file *dwo_file;
7383 struct dwo_unit find_dwo_entry, *dwo_entry;
7384 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7385 void **slot;
a2ce51a0
DE
7386
7387 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7388
6aa5f3a6
DE
7389 /* If TU skeletons have been removed then we may not have read in any
7390 TUs yet. */
7391 if (dwarf2_per_objfile->signatured_types == NULL)
7392 {
7393 dwarf2_per_objfile->signatured_types
7394 = allocate_signatured_type_table (objfile);
7395 }
a2ce51a0
DE
7396
7397 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
7398 Use the global signatured_types array to do our own comdat-folding
7399 of types. If this is the first time we're reading this TU, and
7400 the TU has an entry in .gdb_index, replace the recorded data from
7401 .gdb_index with this TU. */
a2ce51a0 7402
a2ce51a0 7403 find_sig_entry.signature = sig;
6aa5f3a6
DE
7404 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7405 &find_sig_entry, INSERT);
9a3c8263 7406 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
7407
7408 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
7409 read. Don't reassign the global entry to point to this DWO if that's
7410 the case. Also note that if the TU is already being read, it may not
7411 have come from a DWO, the program may be a mix of Fission-compiled
7412 code and non-Fission-compiled code. */
7413
7414 /* Have we already tried to read this TU?
7415 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7416 needn't exist in the global table yet). */
7417 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
7418 return sig_entry;
7419
6aa5f3a6
DE
7420 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7421 dwo_unit of the TU itself. */
7422 dwo_file = cu->dwo_unit->dwo_file;
7423
a2ce51a0
DE
7424 /* Ok, this is the first time we're reading this TU. */
7425 if (dwo_file->tus == NULL)
7426 return NULL;
7427 find_dwo_entry.signature = sig;
9a3c8263 7428 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
7429 if (dwo_entry == NULL)
7430 return NULL;
7431
6aa5f3a6
DE
7432 /* If the global table doesn't have an entry for this TU, add one. */
7433 if (sig_entry == NULL)
ed2dc618 7434 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 7435
ed2dc618 7436 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 7437 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
7438 return sig_entry;
7439}
7440
a2ce51a0
DE
7441/* Subroutine of lookup_signatured_type.
7442 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
7443 then try the DWP file. If the TU stub (skeleton) has been removed then
7444 it won't be in .gdb_index. */
a2ce51a0
DE
7445
7446static struct signatured_type *
7447lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7448{
518817b3
SM
7449 struct dwarf2_per_objfile *dwarf2_per_objfile
7450 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 7451 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 7452 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
7453 struct dwo_unit *dwo_entry;
7454 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7455 void **slot;
a2ce51a0
DE
7456
7457 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7458 gdb_assert (dwp_file != NULL);
7459
6aa5f3a6
DE
7460 /* If TU skeletons have been removed then we may not have read in any
7461 TUs yet. */
7462 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 7463 {
6aa5f3a6
DE
7464 dwarf2_per_objfile->signatured_types
7465 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
7466 }
7467
6aa5f3a6
DE
7468 find_sig_entry.signature = sig;
7469 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7470 &find_sig_entry, INSERT);
9a3c8263 7471 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
7472
7473 /* Have we already tried to read this TU?
7474 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7475 needn't exist in the global table yet). */
7476 if (sig_entry != NULL)
7477 return sig_entry;
7478
a2ce51a0
DE
7479 if (dwp_file->tus == NULL)
7480 return NULL;
ed2dc618 7481 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 7482 sig, 1 /* is_debug_types */);
a2ce51a0
DE
7483 if (dwo_entry == NULL)
7484 return NULL;
7485
ed2dc618
SM
7486 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7487 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 7488
a2ce51a0
DE
7489 return sig_entry;
7490}
7491
380bca97 7492/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7493 Returns NULL if signature SIG is not present in the table.
7494 It is up to the caller to complain about this. */
348e048f
DE
7495
7496static struct signatured_type *
a2ce51a0 7497lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7498{
518817b3
SM
7499 struct dwarf2_per_objfile *dwarf2_per_objfile
7500 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 7501
a2ce51a0
DE
7502 if (cu->dwo_unit
7503 && dwarf2_per_objfile->using_index)
7504 {
7505 /* We're in a DWO/DWP file, and we're using .gdb_index.
7506 These cases require special processing. */
ed2dc618 7507 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
7508 return lookup_dwo_signatured_type (cu, sig);
7509 else
7510 return lookup_dwp_signatured_type (cu, sig);
7511 }
7512 else
7513 {
7514 struct signatured_type find_entry, *entry;
348e048f 7515
a2ce51a0
DE
7516 if (dwarf2_per_objfile->signatured_types == NULL)
7517 return NULL;
7518 find_entry.signature = sig;
9a3c8263
SM
7519 entry = ((struct signatured_type *)
7520 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7521 return entry;
7522 }
348e048f 7523}
42e7ad6c
DE
7524\f
7525/* Low level DIE reading support. */
348e048f 7526
d85a05f0
DJ
7527/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7528
7529static void
7530init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7531 struct dwarf2_cu *cu,
3019eac3 7532 struct dwarf2_section_info *section,
685af9cd
TT
7533 struct dwo_file *dwo_file,
7534 struct abbrev_table *abbrev_table)
d85a05f0 7535{
fceca515 7536 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7537 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7538 reader->cu = cu;
3019eac3 7539 reader->dwo_file = dwo_file;
dee91e82
DE
7540 reader->die_section = section;
7541 reader->buffer = section->buffer;
f664829e 7542 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7543 reader->comp_dir = NULL;
685af9cd 7544 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
7545}
7546
b0c7bfa9
DE
7547/* Subroutine of init_cutu_and_read_dies to simplify it.
7548 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7549 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7550 already.
7551
7552 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7553 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7554 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7555 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7556 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7557 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7558 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7559 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
7560 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7561 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7562 kept around for at least as long as *RESULT_READER.
7563
b0c7bfa9
DE
7564 The result is non-zero if a valid (non-dummy) DIE was found. */
7565
7566static int
7567read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7568 struct dwo_unit *dwo_unit,
b0c7bfa9 7569 struct die_info *stub_comp_unit_die,
a2ce51a0 7570 const char *stub_comp_dir,
b0c7bfa9 7571 struct die_reader_specs *result_reader,
d521ce57 7572 const gdb_byte **result_info_ptr,
b0c7bfa9 7573 struct die_info **result_comp_unit_die,
685af9cd
TT
7574 int *result_has_children,
7575 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 7576{
ed2dc618 7577 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
7578 struct objfile *objfile = dwarf2_per_objfile->objfile;
7579 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7580 bfd *abfd;
d521ce57 7581 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7582 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7583 int i,num_extra_attrs;
7584 struct dwarf2_section_info *dwo_abbrev_section;
7585 struct attribute *attr;
7586 struct die_info *comp_unit_die;
7587
b0aeadb3
DE
7588 /* At most one of these may be provided. */
7589 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7590
b0c7bfa9
DE
7591 /* These attributes aren't processed until later:
7592 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7593 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7594 referenced later. However, these attributes are found in the stub
7595 which we won't have later. In order to not impose this complication
7596 on the rest of the code, we read them here and copy them to the
7597 DWO CU/TU die. */
b0c7bfa9
DE
7598
7599 stmt_list = NULL;
7600 low_pc = NULL;
7601 high_pc = NULL;
7602 ranges = NULL;
7603 comp_dir = NULL;
7604
7605 if (stub_comp_unit_die != NULL)
7606 {
7607 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7608 DWO file. */
7609 if (! this_cu->is_debug_types)
7610 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7611 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7612 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7613 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7614 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7615
7616 /* There should be a DW_AT_addr_base attribute here (if needed).
7617 We need the value before we can process DW_FORM_GNU_addr_index. */
7618 cu->addr_base = 0;
7619 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7620 if (attr)
7621 cu->addr_base = DW_UNSND (attr);
7622
7623 /* There should be a DW_AT_ranges_base attribute here (if needed).
7624 We need the value before we can process DW_AT_ranges. */
7625 cu->ranges_base = 0;
7626 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7627 if (attr)
7628 cu->ranges_base = DW_UNSND (attr);
7629 }
a2ce51a0
DE
7630 else if (stub_comp_dir != NULL)
7631 {
7632 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7633 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7634 comp_dir->name = DW_AT_comp_dir;
7635 comp_dir->form = DW_FORM_string;
7636 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7637 DW_STRING (comp_dir) = stub_comp_dir;
7638 }
b0c7bfa9
DE
7639
7640 /* Set up for reading the DWO CU/TU. */
7641 cu->dwo_unit = dwo_unit;
685af9cd 7642 dwarf2_section_info *section = dwo_unit->section;
b0c7bfa9 7643 dwarf2_read_section (objfile, section);
a32a8923 7644 abfd = get_section_bfd_owner (section);
9c541725
PA
7645 begin_info_ptr = info_ptr = (section->buffer
7646 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7647 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
7648
7649 if (this_cu->is_debug_types)
7650 {
b0c7bfa9
DE
7651 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7652
ed2dc618
SM
7653 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7654 &cu->header, section,
b0c7bfa9 7655 dwo_abbrev_section,
43988095 7656 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7657 /* This is not an assert because it can be caused by bad debug info. */
43988095 7658 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7659 {
7660 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7661 " TU at offset %s [in module %s]"),
a2ce51a0 7662 hex_string (sig_type->signature),
43988095 7663 hex_string (cu->header.signature),
9d8780f0 7664 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7665 bfd_get_filename (abfd));
7666 }
9c541725 7667 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7668 /* For DWOs coming from DWP files, we don't know the CU length
7669 nor the type's offset in the TU until now. */
7670 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7671 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7672
7673 /* Establish the type offset that can be used to lookup the type.
7674 For DWO files, we don't know it until now. */
9c541725
PA
7675 sig_type->type_offset_in_section
7676 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7677 }
7678 else
7679 {
ed2dc618
SM
7680 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7681 &cu->header, section,
b0c7bfa9 7682 dwo_abbrev_section,
43988095 7683 info_ptr, rcuh_kind::COMPILE);
9c541725 7684 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7685 /* For DWOs coming from DWP files, we don't know the CU length
7686 until now. */
7687 dwo_unit->length = get_cu_length (&cu->header);
7688 }
7689
685af9cd
TT
7690 *result_dwo_abbrev_table
7691 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7692 cu->header.abbrev_sect_off);
7693 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7694 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7695
7696 /* Read in the die, but leave space to copy over the attributes
7697 from the stub. This has the benefit of simplifying the rest of
7698 the code - all the work to maintain the illusion of a single
7699 DW_TAG_{compile,type}_unit DIE is done here. */
7700 num_extra_attrs = ((stmt_list != NULL)
7701 + (low_pc != NULL)
7702 + (high_pc != NULL)
7703 + (ranges != NULL)
7704 + (comp_dir != NULL));
7705 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7706 result_has_children, num_extra_attrs);
7707
7708 /* Copy over the attributes from the stub to the DIE we just read in. */
7709 comp_unit_die = *result_comp_unit_die;
7710 i = comp_unit_die->num_attrs;
7711 if (stmt_list != NULL)
7712 comp_unit_die->attrs[i++] = *stmt_list;
7713 if (low_pc != NULL)
7714 comp_unit_die->attrs[i++] = *low_pc;
7715 if (high_pc != NULL)
7716 comp_unit_die->attrs[i++] = *high_pc;
7717 if (ranges != NULL)
7718 comp_unit_die->attrs[i++] = *ranges;
7719 if (comp_dir != NULL)
7720 comp_unit_die->attrs[i++] = *comp_dir;
7721 comp_unit_die->num_attrs += num_extra_attrs;
7722
b4f54984 7723 if (dwarf_die_debug)
bf6af496
DE
7724 {
7725 fprintf_unfiltered (gdb_stdlog,
7726 "Read die from %s@0x%x of %s:\n",
a32a8923 7727 get_section_name (section),
bf6af496
DE
7728 (unsigned) (begin_info_ptr - section->buffer),
7729 bfd_get_filename (abfd));
b4f54984 7730 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7731 }
7732
a2ce51a0
DE
7733 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7734 TUs by skipping the stub and going directly to the entry in the DWO file.
7735 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7736 to get it via circuitous means. Blech. */
7737 if (comp_dir != NULL)
7738 result_reader->comp_dir = DW_STRING (comp_dir);
7739
b0c7bfa9
DE
7740 /* Skip dummy compilation units. */
7741 if (info_ptr >= begin_info_ptr + dwo_unit->length
7742 || peek_abbrev_code (abfd, info_ptr) == 0)
7743 return 0;
7744
7745 *result_info_ptr = info_ptr;
7746 return 1;
7747}
7748
7749/* Subroutine of init_cutu_and_read_dies to simplify it.
7750 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7751 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7752
7753static struct dwo_unit *
7754lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7755 struct die_info *comp_unit_die)
7756{
7757 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9
DE
7758 ULONGEST signature;
7759 struct dwo_unit *dwo_unit;
7760 const char *comp_dir, *dwo_name;
7761
a2ce51a0
DE
7762 gdb_assert (cu != NULL);
7763
b0c7bfa9 7764 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
7765 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7766 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7767
7768 if (this_cu->is_debug_types)
7769 {
7770 struct signatured_type *sig_type;
7771
7772 /* Since this_cu is the first member of struct signatured_type,
7773 we can go from a pointer to one to a pointer to the other. */
7774 sig_type = (struct signatured_type *) this_cu;
7775 signature = sig_type->signature;
7776 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7777 }
7778 else
7779 {
7780 struct attribute *attr;
7781
7782 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7783 if (! attr)
7784 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7785 " [in module %s]"),
e3b94546 7786 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9
DE
7787 signature = DW_UNSND (attr);
7788 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7789 signature);
7790 }
7791
b0c7bfa9
DE
7792 return dwo_unit;
7793}
7794
a2ce51a0 7795/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6 7796 See it for a description of the parameters.
fcd3b13d 7797 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0
DE
7798
7799static void
6aa5f3a6
DE
7800init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7801 int use_existing_cu, int keep,
a2ce51a0
DE
7802 die_reader_func_ftype *die_reader_func,
7803 void *data)
7804{
fcd3b13d 7805 std::unique_ptr<dwarf2_cu> new_cu;
a2ce51a0 7806 struct signatured_type *sig_type;
a2ce51a0
DE
7807 struct die_reader_specs reader;
7808 const gdb_byte *info_ptr;
7809 struct die_info *comp_unit_die;
7810 int has_children;
ed2dc618 7811 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
a2ce51a0
DE
7812
7813 /* Verify we can do the following downcast, and that we have the
7814 data we need. */
7815 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7816 sig_type = (struct signatured_type *) this_cu;
7817 gdb_assert (sig_type->dwo_unit != NULL);
7818
6aa5f3a6
DE
7819 if (use_existing_cu && this_cu->cu != NULL)
7820 {
7821 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6
DE
7822 /* There's no need to do the rereading_dwo_cu handling that
7823 init_cutu_and_read_dies does since we don't read the stub. */
7824 }
7825 else
7826 {
7827 /* If !use_existing_cu, this_cu->cu must be NULL. */
7828 gdb_assert (this_cu->cu == NULL);
fcd3b13d 7829 new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7830 }
7831
7832 /* A future optimization, if needed, would be to use an existing
7833 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7834 could share abbrev tables. */
a2ce51a0 7835
685af9cd
TT
7836 /* The abbreviation table used by READER, this must live at least as long as
7837 READER. */
7838 abbrev_table_up dwo_abbrev_table;
7839
a2ce51a0 7840 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7841 NULL /* stub_comp_unit_die */,
7842 sig_type->dwo_unit->dwo_file->comp_dir,
7843 &reader, &info_ptr,
685af9cd
TT
7844 &comp_unit_die, &has_children,
7845 &dwo_abbrev_table) == 0)
a2ce51a0
DE
7846 {
7847 /* Dummy die. */
a2ce51a0
DE
7848 return;
7849 }
7850
7851 /* All the "real" work is done here. */
7852 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7853
6aa5f3a6 7854 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
7855 but the alternative is making the latter more complex.
7856 This function is only for the special case of using DWO files directly:
7857 no point in overly complicating the general case just to handle this. */
fcd3b13d 7858 if (new_cu != NULL && keep)
a2ce51a0 7859 {
fcd3b13d
SM
7860 /* Link this CU into read_in_chain. */
7861 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7862 dwarf2_per_objfile->read_in_chain = this_cu;
7863 /* The chain owns it now. */
7864 new_cu.release ();
a2ce51a0 7865 }
a2ce51a0
DE
7866}
7867
fd820528 7868/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7869 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7870
f4dc4d17
DE
7871 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7872 Otherwise the table specified in the comp unit header is read in and used.
7873 This is an optimization for when we already have the abbrev table.
7874
dee91e82
DE
7875 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7876 Otherwise, a new CU is allocated with xmalloc.
7877
7878 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7879 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7880
7881 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 7882 linker) then DIE_READER_FUNC will not get called. */
aaa75496 7883
70221824 7884static void
fd820528 7885init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 7886 struct abbrev_table *abbrev_table,
fd820528
DE
7887 int use_existing_cu, int keep,
7888 die_reader_func_ftype *die_reader_func,
7889 void *data)
c906108c 7890{
ed2dc618 7891 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7892 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7893 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7894 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7895 struct dwarf2_cu *cu;
d521ce57 7896 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7897 struct die_reader_specs reader;
d85a05f0 7898 struct die_info *comp_unit_die;
dee91e82 7899 int has_children;
d85a05f0 7900 struct attribute *attr;
dee91e82 7901 struct signatured_type *sig_type = NULL;
4bdcc0c1 7902 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7903 /* Non-zero if CU currently points to a DWO file and we need to
7904 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7905 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7906 int rereading_dwo_cu = 0;
c906108c 7907
b4f54984 7908 if (dwarf_die_debug)
9d8780f0 7909 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7910 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7911 sect_offset_str (this_cu->sect_off));
09406207 7912
dee91e82
DE
7913 if (use_existing_cu)
7914 gdb_assert (keep);
23745b47 7915
a2ce51a0
DE
7916 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7917 file (instead of going through the stub), short-circuit all of this. */
7918 if (this_cu->reading_dwo_directly)
7919 {
7920 /* Narrow down the scope of possibilities to have to understand. */
7921 gdb_assert (this_cu->is_debug_types);
7922 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
7923 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7924 die_reader_func, data);
a2ce51a0
DE
7925 return;
7926 }
7927
dee91e82
DE
7928 /* This is cheap if the section is already read in. */
7929 dwarf2_read_section (objfile, section);
7930
9c541725 7931 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7932
7933 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7934
fcd3b13d 7935 std::unique_ptr<dwarf2_cu> new_cu;
dee91e82
DE
7936 if (use_existing_cu && this_cu->cu != NULL)
7937 {
7938 cu = this_cu->cu;
42e7ad6c
DE
7939 /* If this CU is from a DWO file we need to start over, we need to
7940 refetch the attributes from the skeleton CU.
7941 This could be optimized by retrieving those attributes from when we
7942 were here the first time: the previous comp_unit_die was stored in
7943 comp_unit_obstack. But there's no data yet that we need this
7944 optimization. */
7945 if (cu->dwo_unit != NULL)
7946 rereading_dwo_cu = 1;
dee91e82
DE
7947 }
7948 else
7949 {
7950 /* If !use_existing_cu, this_cu->cu must be NULL. */
7951 gdb_assert (this_cu->cu == NULL);
fcd3b13d
SM
7952 new_cu.reset (new dwarf2_cu (this_cu));
7953 cu = new_cu.get ();
42e7ad6c 7954 }
dee91e82 7955
b0c7bfa9 7956 /* Get the header. */
9c541725 7957 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7958 {
7959 /* We already have the header, there's no need to read it in again. */
9c541725 7960 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7961 }
7962 else
7963 {
3019eac3 7964 if (this_cu->is_debug_types)
dee91e82 7965 {
ed2dc618
SM
7966 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7967 &cu->header, section,
4bdcc0c1 7968 abbrev_section, info_ptr,
43988095 7969 rcuh_kind::TYPE);
dee91e82 7970
42e7ad6c
DE
7971 /* Since per_cu is the first member of struct signatured_type,
7972 we can go from a pointer to one to a pointer to the other. */
7973 sig_type = (struct signatured_type *) this_cu;
43988095 7974 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7975 gdb_assert (sig_type->type_offset_in_tu
7976 == cu->header.type_cu_offset_in_tu);
7977 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7978
42e7ad6c
DE
7979 /* LENGTH has not been set yet for type units if we're
7980 using .gdb_index. */
1ce1cefd 7981 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7982
7983 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7984 sig_type->type_offset_in_section =
7985 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7986
7987 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7988 }
7989 else
7990 {
ed2dc618
SM
7991 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7992 &cu->header, section,
4bdcc0c1 7993 abbrev_section,
43988095
JK
7994 info_ptr,
7995 rcuh_kind::COMPILE);
dee91e82 7996
9c541725 7997 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7998 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7999 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
8000 }
8001 }
10b3939b 8002
6caca83c 8003 /* Skip dummy compilation units. */
dee91e82 8004 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 8005 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 8006 return;
6caca83c 8007
433df2d4
DE
8008 /* If we don't have them yet, read the abbrevs for this compilation unit.
8009 And if we need to read them now, make sure they're freed when we're
685af9cd
TT
8010 done (own the table through ABBREV_TABLE_HOLDER). */
8011 abbrev_table_up abbrev_table_holder;
f4dc4d17 8012 if (abbrev_table != NULL)
685af9cd
TT
8013 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
8014 else
f4dc4d17 8015 {
685af9cd
TT
8016 abbrev_table_holder
8017 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
8018 cu->header.abbrev_sect_off);
8019 abbrev_table = abbrev_table_holder.get ();
42e7ad6c 8020 }
af703f96 8021
dee91e82 8022 /* Read the top level CU/TU die. */
685af9cd 8023 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
dee91e82 8024 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 8025
b0c7bfa9 8026 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
8027 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
8028 table from the DWO file and pass the ownership over to us. It will be
8029 referenced from READER, so we must make sure to free it after we're done
8030 with READER.
8031
b0c7bfa9
DE
8032 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
8033 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3 8034 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
685af9cd 8035 abbrev_table_up dwo_abbrev_table;
3019eac3
DE
8036 if (attr)
8037 {
3019eac3 8038 struct dwo_unit *dwo_unit;
b0c7bfa9 8039 struct die_info *dwo_comp_unit_die;
3019eac3
DE
8040
8041 if (has_children)
6a506a2d
DE
8042 {
8043 complaint (&symfile_complaints,
8044 _("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
8045 " has children (offset %s) [in module %s]"),
8046 sect_offset_str (this_cu->sect_off),
8047 bfd_get_filename (abfd));
6a506a2d 8048 }
b0c7bfa9 8049 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 8050 if (dwo_unit != NULL)
3019eac3 8051 {
6a506a2d 8052 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 8053 comp_unit_die, NULL,
6a506a2d 8054 &reader, &info_ptr,
685af9cd
TT
8055 &dwo_comp_unit_die, &has_children,
8056 &dwo_abbrev_table) == 0)
6a506a2d
DE
8057 {
8058 /* Dummy die. */
6a506a2d
DE
8059 return;
8060 }
8061 comp_unit_die = dwo_comp_unit_die;
8062 }
8063 else
8064 {
8065 /* Yikes, we couldn't find the rest of the DIE, we only have
8066 the stub. A complaint has already been logged. There's
8067 not much more we can do except pass on the stub DIE to
8068 die_reader_func. We don't want to throw an error on bad
8069 debug info. */
3019eac3
DE
8070 }
8071 }
8072
b0c7bfa9 8073 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
8074 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8075
b0c7bfa9 8076 /* Done, clean up. */
fcd3b13d 8077 if (new_cu != NULL && keep)
348e048f 8078 {
fcd3b13d
SM
8079 /* Link this CU into read_in_chain. */
8080 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
8081 dwarf2_per_objfile->read_in_chain = this_cu;
8082 /* The chain owns it now. */
8083 new_cu.release ();
348e048f 8084 }
dee91e82
DE
8085}
8086
33e80786
DE
8087/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
8088 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
8089 to have already done the lookup to find the DWO file).
dee91e82
DE
8090
8091 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 8092 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
8093
8094 We fill in THIS_CU->length.
8095
8096 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
8097 linker) then DIE_READER_FUNC will not get called.
8098
8099 THIS_CU->cu is always freed when done.
3019eac3
DE
8100 This is done in order to not leave THIS_CU->cu in a state where we have
8101 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
8102
8103static void
8104init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 8105 struct dwo_file *dwo_file,
dee91e82
DE
8106 die_reader_func_ftype *die_reader_func,
8107 void *data)
8108{
ed2dc618 8109 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 8110 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 8111 struct dwarf2_section_info *section = this_cu->section;
a32a8923 8112 bfd *abfd = get_section_bfd_owner (section);
33e80786 8113 struct dwarf2_section_info *abbrev_section;
d521ce57 8114 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 8115 struct die_reader_specs reader;
dee91e82
DE
8116 struct die_info *comp_unit_die;
8117 int has_children;
8118
b4f54984 8119 if (dwarf_die_debug)
9d8780f0 8120 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 8121 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 8122 sect_offset_str (this_cu->sect_off));
09406207 8123
dee91e82
DE
8124 gdb_assert (this_cu->cu == NULL);
8125
33e80786
DE
8126 abbrev_section = (dwo_file != NULL
8127 ? &dwo_file->sections.abbrev
8128 : get_abbrev_section_for_cu (this_cu));
8129
dee91e82
DE
8130 /* This is cheap if the section is already read in. */
8131 dwarf2_read_section (objfile, section);
8132
fcd3b13d 8133 struct dwarf2_cu cu (this_cu);
dee91e82 8134
9c541725 8135 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618
SM
8136 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
8137 &cu.header, section,
4bdcc0c1 8138 abbrev_section, info_ptr,
43988095
JK
8139 (this_cu->is_debug_types
8140 ? rcuh_kind::TYPE
8141 : rcuh_kind::COMPILE));
dee91e82 8142
1ce1cefd 8143 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
8144
8145 /* Skip dummy compilation units. */
8146 if (info_ptr >= begin_info_ptr + this_cu->length
8147 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 8148 return;
72bf9492 8149
685af9cd
TT
8150 abbrev_table_up abbrev_table
8151 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
8152 cu.header.abbrev_sect_off);
dee91e82 8153
685af9cd 8154 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
dee91e82
DE
8155 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
8156
8157 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
dee91e82
DE
8158}
8159
3019eac3
DE
8160/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
8161 does not lookup the specified DWO file.
8162 This cannot be used to read DWO files.
dee91e82
DE
8163
8164 THIS_CU->cu is always freed when done.
3019eac3
DE
8165 This is done in order to not leave THIS_CU->cu in a state where we have
8166 to care whether it refers to the "main" CU or the DWO CU.
8167 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
8168
8169static void
8170init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
8171 die_reader_func_ftype *die_reader_func,
8172 void *data)
8173{
33e80786 8174 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 8175}
0018ea6f
DE
8176\f
8177/* Type Unit Groups.
dee91e82 8178
0018ea6f
DE
8179 Type Unit Groups are a way to collapse the set of all TUs (type units) into
8180 a more manageable set. The grouping is done by DW_AT_stmt_list entry
8181 so that all types coming from the same compilation (.o file) are grouped
8182 together. A future step could be to put the types in the same symtab as
8183 the CU the types ultimately came from. */
ff013f42 8184
f4dc4d17
DE
8185static hashval_t
8186hash_type_unit_group (const void *item)
8187{
9a3c8263
SM
8188 const struct type_unit_group *tu_group
8189 = (const struct type_unit_group *) item;
f4dc4d17 8190
094b34ac 8191 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 8192}
348e048f
DE
8193
8194static int
f4dc4d17 8195eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 8196{
9a3c8263
SM
8197 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
8198 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 8199
094b34ac 8200 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 8201}
348e048f 8202
f4dc4d17
DE
8203/* Allocate a hash table for type unit groups. */
8204
8205static htab_t
ed2dc618 8206allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
8207{
8208 return htab_create_alloc_ex (3,
8209 hash_type_unit_group,
8210 eq_type_unit_group,
8211 NULL,
ed2dc618 8212 &objfile->objfile_obstack,
f4dc4d17
DE
8213 hashtab_obstack_allocate,
8214 dummy_obstack_deallocate);
8215}
dee91e82 8216
f4dc4d17
DE
8217/* Type units that don't have DW_AT_stmt_list are grouped into their own
8218 partial symtabs. We combine several TUs per psymtab to not let the size
8219 of any one psymtab grow too big. */
8220#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
8221#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 8222
094b34ac 8223/* Helper routine for get_type_unit_group.
f4dc4d17
DE
8224 Create the type_unit_group object used to hold one or more TUs. */
8225
8226static struct type_unit_group *
094b34ac 8227create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 8228{
518817b3
SM
8229 struct dwarf2_per_objfile *dwarf2_per_objfile
8230 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 8231 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 8232 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 8233 struct type_unit_group *tu_group;
f4dc4d17
DE
8234
8235 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8236 struct type_unit_group);
094b34ac 8237 per_cu = &tu_group->per_cu;
518817b3 8238 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 8239
094b34ac
DE
8240 if (dwarf2_per_objfile->using_index)
8241 {
8242 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8243 struct dwarf2_per_cu_quick_data);
094b34ac
DE
8244 }
8245 else
8246 {
9c541725 8247 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac
DE
8248 struct partial_symtab *pst;
8249 char *name;
8250
8251 /* Give the symtab a useful name for debug purposes. */
8252 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
8253 name = xstrprintf ("<type_units_%d>",
8254 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
8255 else
8256 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
8257
8258 pst = create_partial_symtab (per_cu, name);
8259 pst->anonymous = 1;
f4dc4d17 8260
094b34ac
DE
8261 xfree (name);
8262 }
f4dc4d17 8263
094b34ac 8264 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 8265 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
8266
8267 return tu_group;
8268}
8269
094b34ac
DE
8270/* Look up the type_unit_group for type unit CU, and create it if necessary.
8271 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
8272
8273static struct type_unit_group *
ff39bb5e 8274get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 8275{
518817b3
SM
8276 struct dwarf2_per_objfile *dwarf2_per_objfile
8277 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8278 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8279 struct type_unit_group *tu_group;
8280 void **slot;
8281 unsigned int line_offset;
8282 struct type_unit_group type_unit_group_for_lookup;
8283
8284 if (dwarf2_per_objfile->type_unit_groups == NULL)
8285 {
8286 dwarf2_per_objfile->type_unit_groups =
ed2dc618 8287 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
8288 }
8289
8290 /* Do we need to create a new group, or can we use an existing one? */
8291
8292 if (stmt_list)
8293 {
8294 line_offset = DW_UNSND (stmt_list);
8295 ++tu_stats->nr_symtab_sharers;
8296 }
8297 else
8298 {
8299 /* Ugh, no stmt_list. Rare, but we have to handle it.
8300 We can do various things here like create one group per TU or
8301 spread them over multiple groups to split up the expansion work.
8302 To avoid worst case scenarios (too many groups or too large groups)
8303 we, umm, group them in bunches. */
8304 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
8305 | (tu_stats->nr_stmt_less_type_units
8306 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
8307 ++tu_stats->nr_stmt_less_type_units;
8308 }
8309
094b34ac 8310 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 8311 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
8312 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
8313 &type_unit_group_for_lookup, INSERT);
8314 if (*slot != NULL)
8315 {
9a3c8263 8316 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
8317 gdb_assert (tu_group != NULL);
8318 }
8319 else
8320 {
9c541725 8321 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 8322 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
8323 *slot = tu_group;
8324 ++tu_stats->nr_symtabs;
8325 }
8326
8327 return tu_group;
8328}
0018ea6f
DE
8329\f
8330/* Partial symbol tables. */
8331
8332/* Create a psymtab named NAME and assign it to PER_CU.
8333
8334 The caller must fill in the following details:
8335 dirname, textlow, texthigh. */
8336
8337static struct partial_symtab *
8338create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8339{
e3b94546 8340 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
0018ea6f
DE
8341 struct partial_symtab *pst;
8342
18a94d75 8343 pst = start_psymtab_common (objfile, name, 0,
af5bf4ad
SM
8344 objfile->global_psymbols,
8345 objfile->static_psymbols);
0018ea6f
DE
8346
8347 pst->psymtabs_addrmap_supported = 1;
8348
8349 /* This is the glue that links PST into GDB's symbol API. */
8350 pst->read_symtab_private = per_cu;
8351 pst->read_symtab = dwarf2_read_symtab;
8352 per_cu->v.psymtab = pst;
8353
8354 return pst;
8355}
8356
b93601f3
TT
8357/* The DATA object passed to process_psymtab_comp_unit_reader has this
8358 type. */
8359
8360struct process_psymtab_comp_unit_data
8361{
8362 /* True if we are reading a DW_TAG_partial_unit. */
8363
8364 int want_partial_unit;
8365
8366 /* The "pretend" language that is used if the CU doesn't declare a
8367 language. */
8368
8369 enum language pretend_language;
8370};
8371
0018ea6f
DE
8372/* die_reader_func for process_psymtab_comp_unit. */
8373
8374static void
8375process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8376 const gdb_byte *info_ptr,
0018ea6f
DE
8377 struct die_info *comp_unit_die,
8378 int has_children,
8379 void *data)
8380{
8381 struct dwarf2_cu *cu = reader->cu;
518817b3 8382 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 8383 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 8384 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
8385 CORE_ADDR baseaddr;
8386 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8387 struct partial_symtab *pst;
3a2b436a 8388 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 8389 const char *filename;
9a3c8263
SM
8390 struct process_psymtab_comp_unit_data *info
8391 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 8392
b93601f3 8393 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
8394 return;
8395
8396 gdb_assert (! per_cu->is_debug_types);
8397
b93601f3 8398 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
8399
8400 cu->list_in_scope = &file_symbols;
8401
8402 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
8403 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8404 if (filename == NULL)
0018ea6f 8405 filename = "";
0018ea6f
DE
8406
8407 pst = create_partial_symtab (per_cu, filename);
8408
8409 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 8410 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
8411
8412 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8413
8414 dwarf2_find_base_address (comp_unit_die, cu);
8415
8416 /* Possibly set the default values of LOWPC and HIGHPC from
8417 `DW_AT_ranges'. */
3a2b436a
JK
8418 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8419 &best_highpc, cu, pst);
8420 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
0018ea6f
DE
8421 /* Store the contiguous range if it is not empty; it can be empty for
8422 CUs with no code. */
8423 addrmap_set_empty (objfile->psymtabs_addrmap,
3e29f34a
MR
8424 gdbarch_adjust_dwarf2_addr (gdbarch,
8425 best_lowpc + baseaddr),
8426 gdbarch_adjust_dwarf2_addr (gdbarch,
8427 best_highpc + baseaddr) - 1,
8428 pst);
0018ea6f
DE
8429
8430 /* Check if comp unit has_children.
8431 If so, read the rest of the partial symbols from this comp unit.
8432 If not, there's no more debug_info for this comp unit. */
8433 if (has_children)
8434 {
8435 struct partial_die_info *first_die;
8436 CORE_ADDR lowpc, highpc;
8437
8438 lowpc = ((CORE_ADDR) -1);
8439 highpc = ((CORE_ADDR) 0);
8440
8441 first_die = load_partial_dies (reader, info_ptr, 1);
8442
8443 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 8444 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
8445
8446 /* If we didn't find a lowpc, set it to highpc to avoid
8447 complaints from `maint check'. */
8448 if (lowpc == ((CORE_ADDR) -1))
8449 lowpc = highpc;
8450
8451 /* If the compilation unit didn't have an explicit address range,
8452 then use the information extracted from its child dies. */
e385593e 8453 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
8454 {
8455 best_lowpc = lowpc;
8456 best_highpc = highpc;
8457 }
8458 }
3e29f34a
MR
8459 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
8460 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 8461
8763cede 8462 end_psymtab_common (objfile, pst);
0018ea6f
DE
8463
8464 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8465 {
8466 int i;
8467 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8468 struct dwarf2_per_cu_data *iter;
8469
8470 /* Fill in 'dependencies' here; we fill in 'users' in a
8471 post-pass. */
8472 pst->number_of_dependencies = len;
8d749320
SM
8473 pst->dependencies =
8474 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
8475 for (i = 0;
8476 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8477 i, iter);
8478 ++i)
8479 pst->dependencies[i] = iter->v.psymtab;
8480
8481 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8482 }
8483
8484 /* Get the list of files included in the current compilation unit,
8485 and build a psymtab for each of them. */
8486 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8487
b4f54984 8488 if (dwarf_read_debug)
0018ea6f
DE
8489 {
8490 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8491
8492 fprintf_unfiltered (gdb_stdlog,
9d8780f0 8493 "Psymtab for %s unit @%s: %s - %s"
0018ea6f
DE
8494 ", %d global, %d static syms\n",
8495 per_cu->is_debug_types ? "type" : "comp",
9d8780f0 8496 sect_offset_str (per_cu->sect_off),
0018ea6f
DE
8497 paddress (gdbarch, pst->textlow),
8498 paddress (gdbarch, pst->texthigh),
8499 pst->n_global_syms, pst->n_static_syms);
8500 }
8501}
8502
8503/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8504 Process compilation unit THIS_CU for a psymtab. */
8505
8506static void
8507process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8508 int want_partial_unit,
8509 enum language pretend_language)
0018ea6f
DE
8510{
8511 /* If this compilation unit was already read in, free the
8512 cached copy in order to read it in again. This is
8513 necessary because we skipped some symbols when we first
8514 read in the compilation unit (see load_partial_dies).
8515 This problem could be avoided, but the benefit is unclear. */
8516 if (this_cu->cu != NULL)
8517 free_one_cached_comp_unit (this_cu);
8518
f1902523
JK
8519 if (this_cu->is_debug_types)
8520 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8521 NULL);
8522 else
8523 {
8524 process_psymtab_comp_unit_data info;
8525 info.want_partial_unit = want_partial_unit;
8526 info.pretend_language = pretend_language;
8527 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8528 process_psymtab_comp_unit_reader, &info);
8529 }
0018ea6f
DE
8530
8531 /* Age out any secondary CUs. */
ed2dc618 8532 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 8533}
f4dc4d17
DE
8534
8535/* Reader function for build_type_psymtabs. */
8536
8537static void
8538build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8539 const gdb_byte *info_ptr,
f4dc4d17
DE
8540 struct die_info *type_unit_die,
8541 int has_children,
8542 void *data)
8543{
ed2dc618 8544 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 8545 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8546 struct objfile *objfile = dwarf2_per_objfile->objfile;
8547 struct dwarf2_cu *cu = reader->cu;
8548 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8549 struct signatured_type *sig_type;
f4dc4d17
DE
8550 struct type_unit_group *tu_group;
8551 struct attribute *attr;
8552 struct partial_die_info *first_die;
8553 CORE_ADDR lowpc, highpc;
8554 struct partial_symtab *pst;
8555
8556 gdb_assert (data == NULL);
0186c6a7
DE
8557 gdb_assert (per_cu->is_debug_types);
8558 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8559
8560 if (! has_children)
8561 return;
8562
8563 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8564 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8565
0186c6a7 8566 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
8567
8568 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8569 cu->list_in_scope = &file_symbols;
8570 pst = create_partial_symtab (per_cu, "");
8571 pst->anonymous = 1;
8572
8573 first_die = load_partial_dies (reader, info_ptr, 1);
8574
8575 lowpc = (CORE_ADDR) -1;
8576 highpc = (CORE_ADDR) 0;
8577 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8578
8763cede 8579 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8580}
8581
73051182
DE
8582/* Struct used to sort TUs by their abbreviation table offset. */
8583
8584struct tu_abbrev_offset
8585{
8586 struct signatured_type *sig_type;
8587 sect_offset abbrev_offset;
8588};
8589
484cf504 8590/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 8591
484cf504
TT
8592static bool
8593sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8594 const struct tu_abbrev_offset &b)
73051182 8595{
484cf504 8596 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
8597}
8598
8599/* Efficiently read all the type units.
8600 This does the bulk of the work for build_type_psymtabs.
8601
8602 The efficiency is because we sort TUs by the abbrev table they use and
8603 only read each abbrev table once. In one program there are 200K TUs
8604 sharing 8K abbrev tables.
8605
8606 The main purpose of this function is to support building the
8607 dwarf2_per_objfile->type_unit_groups table.
8608 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8609 can collapse the search space by grouping them by stmt_list.
8610 The savings can be significant, in the same program from above the 200K TUs
8611 share 8K stmt_list tables.
8612
8613 FUNC is expected to call get_type_unit_group, which will create the
8614 struct type_unit_group if necessary and add it to
8615 dwarf2_per_objfile->type_unit_groups. */
8616
8617static void
ed2dc618 8618build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 8619{
73051182 8620 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 8621 abbrev_table_up abbrev_table;
73051182 8622 sect_offset abbrev_offset;
73051182
DE
8623 int i;
8624
8625 /* It's up to the caller to not call us multiple times. */
8626 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8627
8628 if (dwarf2_per_objfile->n_type_units == 0)
8629 return;
8630
8631 /* TUs typically share abbrev tables, and there can be way more TUs than
8632 abbrev tables. Sort by abbrev table to reduce the number of times we
8633 read each abbrev table in.
8634 Alternatives are to punt or to maintain a cache of abbrev tables.
8635 This is simpler and efficient enough for now.
8636
8637 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8638 symtab to use). Typically TUs with the same abbrev offset have the same
8639 stmt_list value too so in practice this should work well.
8640
8641 The basic algorithm here is:
8642
8643 sort TUs by abbrev table
8644 for each TU with same abbrev table:
8645 read abbrev table if first user
8646 read TU top level DIE
8647 [IWBN if DWO skeletons had DW_AT_stmt_list]
8648 call FUNC */
8649
b4f54984 8650 if (dwarf_read_debug)
73051182
DE
8651 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8652
8653 /* Sort in a separate table to maintain the order of all_type_units
8654 for .gdb_index: TU indices directly index all_type_units. */
484cf504
TT
8655 std::vector<struct tu_abbrev_offset> sorted_by_abbrev
8656 (dwarf2_per_objfile->n_type_units);
73051182
DE
8657 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8658 {
8659 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
8660
8661 sorted_by_abbrev[i].sig_type = sig_type;
8662 sorted_by_abbrev[i].abbrev_offset =
ed2dc618
SM
8663 read_abbrev_offset (dwarf2_per_objfile,
8664 sig_type->per_cu.section,
9c541725 8665 sig_type->per_cu.sect_off);
73051182 8666 }
484cf504
TT
8667 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8668 sort_tu_by_abbrev_offset);
73051182 8669
9c541725 8670 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182
DE
8671
8672 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8673 {
8674 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
8675
8676 /* Switch to the next abbrev table if necessary. */
8677 if (abbrev_table == NULL
9c541725 8678 || tu->abbrev_offset != abbrev_offset)
73051182 8679 {
73051182
DE
8680 abbrev_offset = tu->abbrev_offset;
8681 abbrev_table =
ed2dc618
SM
8682 abbrev_table_read_table (dwarf2_per_objfile,
8683 &dwarf2_per_objfile->abbrev,
73051182
DE
8684 abbrev_offset);
8685 ++tu_stats->nr_uniq_abbrev_tables;
8686 }
8687
685af9cd
TT
8688 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table.get (),
8689 0, 0, build_type_psymtabs_reader, NULL);
73051182 8690 }
6aa5f3a6 8691}
73051182 8692
6aa5f3a6
DE
8693/* Print collected type unit statistics. */
8694
8695static void
ed2dc618 8696print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8697{
8698 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8699
8700 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8701 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
8702 dwarf2_per_objfile->n_type_units);
8703 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8704 tu_stats->nr_uniq_abbrev_tables);
8705 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8706 tu_stats->nr_symtabs);
8707 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8708 tu_stats->nr_symtab_sharers);
8709 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8710 tu_stats->nr_stmt_less_type_units);
8711 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8712 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8713}
8714
f4dc4d17
DE
8715/* Traversal function for build_type_psymtabs. */
8716
8717static int
8718build_type_psymtab_dependencies (void **slot, void *info)
8719{
ed2dc618
SM
8720 struct dwarf2_per_objfile *dwarf2_per_objfile
8721 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8722 struct objfile *objfile = dwarf2_per_objfile->objfile;
8723 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8724 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 8725 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
8726 int len = VEC_length (sig_type_ptr, tu_group->tus);
8727 struct signatured_type *iter;
f4dc4d17
DE
8728 int i;
8729
8730 gdb_assert (len > 0);
0186c6a7 8731 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8732
8733 pst->number_of_dependencies = len;
8d749320
SM
8734 pst->dependencies =
8735 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 8736 for (i = 0;
0186c6a7 8737 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
8738 ++i)
8739 {
0186c6a7
DE
8740 gdb_assert (iter->per_cu.is_debug_types);
8741 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8742 iter->type_unit_group = tu_group;
f4dc4d17
DE
8743 }
8744
0186c6a7 8745 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
8746
8747 return 1;
8748}
8749
8750/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8751 Build partial symbol tables for the .debug_types comp-units. */
8752
8753static void
ed2dc618 8754build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8755{
ed2dc618 8756 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8757 return;
8758
ed2dc618 8759 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8760}
f4dc4d17 8761
6aa5f3a6
DE
8762/* Traversal function for process_skeletonless_type_unit.
8763 Read a TU in a DWO file and build partial symbols for it. */
8764
8765static int
8766process_skeletonless_type_unit (void **slot, void *info)
8767{
8768 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8769 struct dwarf2_per_objfile *dwarf2_per_objfile
8770 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8771 struct signatured_type find_entry, *entry;
8772
8773 /* If this TU doesn't exist in the global table, add it and read it in. */
8774
8775 if (dwarf2_per_objfile->signatured_types == NULL)
8776 {
8777 dwarf2_per_objfile->signatured_types
ed2dc618 8778 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8779 }
8780
8781 find_entry.signature = dwo_unit->signature;
8782 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8783 INSERT);
8784 /* If we've already seen this type there's nothing to do. What's happening
8785 is we're doing our own version of comdat-folding here. */
8786 if (*slot != NULL)
8787 return 1;
8788
8789 /* This does the job that create_all_type_units would have done for
8790 this TU. */
ed2dc618
SM
8791 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8792 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8793 *slot = entry;
8794
8795 /* This does the job that build_type_psymtabs_1 would have done. */
8796 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8797 build_type_psymtabs_reader, NULL);
8798
8799 return 1;
8800}
8801
8802/* Traversal function for process_skeletonless_type_units. */
8803
8804static int
8805process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8806{
8807 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8808
8809 if (dwo_file->tus != NULL)
8810 {
8811 htab_traverse_noresize (dwo_file->tus,
8812 process_skeletonless_type_unit, info);
8813 }
8814
8815 return 1;
8816}
8817
8818/* Scan all TUs of DWO files, verifying we've processed them.
8819 This is needed in case a TU was emitted without its skeleton.
8820 Note: This can't be done until we know what all the DWO files are. */
8821
8822static void
ed2dc618 8823process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8824{
8825 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8826 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8827 && dwarf2_per_objfile->dwo_files != NULL)
8828 {
8829 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8830 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8831 dwarf2_per_objfile);
6aa5f3a6 8832 }
348e048f
DE
8833}
8834
ed2dc618 8835/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8836
8837static void
ed2dc618 8838set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad
TT
8839{
8840 int i;
8841
8842 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8843 {
ed2dc618 8844 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
95554aad
TT
8845 struct partial_symtab *pst = per_cu->v.psymtab;
8846 int j;
8847
36586728
TT
8848 if (pst == NULL)
8849 continue;
8850
95554aad
TT
8851 for (j = 0; j < pst->number_of_dependencies; ++j)
8852 {
8853 /* Set the 'user' field only if it is not already set. */
8854 if (pst->dependencies[j]->user == NULL)
8855 pst->dependencies[j]->user = pst;
8856 }
8857 }
8858}
8859
93311388
DE
8860/* Build the partial symbol table by doing a quick pass through the
8861 .debug_info and .debug_abbrev sections. */
72bf9492 8862
93311388 8863static void
ed2dc618 8864dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8865{
791afaa2 8866 struct cleanup *back_to;
21b2bd31 8867 int i;
ed2dc618 8868 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8869
b4f54984 8870 if (dwarf_read_debug)
45cfd468
DE
8871 {
8872 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8873 objfile_name (objfile));
45cfd468
DE
8874 }
8875
98bfdba5
PA
8876 dwarf2_per_objfile->reading_partial_symbols = 1;
8877
be391dca 8878 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8879
93311388
DE
8880 /* Any cached compilation units will be linked by the per-objfile
8881 read_in_chain. Make sure to free them when we're done. */
ed2dc618 8882 back_to = make_cleanup (free_cached_comp_units, dwarf2_per_objfile);
72bf9492 8883
ed2dc618 8884 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8885
ed2dc618 8886 create_all_comp_units (dwarf2_per_objfile);
c906108c 8887
60606b2c
TT
8888 /* Create a temporary address map on a temporary obstack. We later
8889 copy this to the final obstack. */
8268c778 8890 auto_obstack temp_obstack;
791afaa2
TT
8891
8892 scoped_restore save_psymtabs_addrmap
8893 = make_scoped_restore (&objfile->psymtabs_addrmap,
8894 addrmap_create_mutable (&temp_obstack));
72bf9492 8895
21b2bd31 8896 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 8897 {
ed2dc618 8898 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
aaa75496 8899
b93601f3 8900 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 8901 }
ff013f42 8902
6aa5f3a6 8903 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8904 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8905
8906 /* Now that all TUs have been processed we can fill in the dependencies. */
8907 if (dwarf2_per_objfile->type_unit_groups != NULL)
8908 {
8909 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8910 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8911 }
8912
b4f54984 8913 if (dwarf_read_debug)
ed2dc618 8914 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8915
ed2dc618 8916 set_partial_user (dwarf2_per_objfile);
95554aad 8917
ff013f42
JK
8918 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8919 &objfile->objfile_obstack);
791afaa2
TT
8920 /* At this point we want to keep the address map. */
8921 save_psymtabs_addrmap.release ();
ff013f42 8922
ae038cb0 8923 do_cleanups (back_to);
45cfd468 8924
b4f54984 8925 if (dwarf_read_debug)
45cfd468 8926 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8927 objfile_name (objfile));
ae038cb0
DJ
8928}
8929
3019eac3 8930/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
8931
8932static void
dee91e82 8933load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8934 const gdb_byte *info_ptr,
dee91e82
DE
8935 struct die_info *comp_unit_die,
8936 int has_children,
8937 void *data)
ae038cb0 8938{
dee91e82 8939 struct dwarf2_cu *cu = reader->cu;
ae038cb0 8940
95554aad 8941 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 8942
ae038cb0
DJ
8943 /* Check if comp unit has_children.
8944 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 8945 If not, there's no more debug_info for this comp unit. */
d85a05f0 8946 if (has_children)
dee91e82
DE
8947 load_partial_dies (reader, info_ptr, 0);
8948}
98bfdba5 8949
dee91e82
DE
8950/* Load the partial DIEs for a secondary CU into memory.
8951 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8952
dee91e82
DE
8953static void
8954load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8955{
f4dc4d17
DE
8956 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8957 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
8958}
8959
ae038cb0 8960static void
ed2dc618 8961read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8962 struct dwarf2_section_info *section,
f1902523 8963 struct dwarf2_section_info *abbrev_section,
36586728
TT
8964 unsigned int is_dwz,
8965 int *n_allocated,
8966 int *n_comp_units,
8967 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 8968{
d521ce57 8969 const gdb_byte *info_ptr;
ed2dc618 8970 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8971
b4f54984 8972 if (dwarf_read_debug)
bf6af496 8973 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8974 get_section_name (section),
8975 get_section_file_name (section));
bf6af496 8976
36586728 8977 dwarf2_read_section (objfile, section);
ae038cb0 8978
36586728 8979 info_ptr = section->buffer;
6e70227d 8980
36586728 8981 while (info_ptr < section->buffer + section->size)
ae038cb0 8982 {
ae038cb0 8983 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8984
9c541725 8985 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8986
f1902523 8987 comp_unit_head cu_header;
ed2dc618
SM
8988 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8989 abbrev_section, info_ptr,
8990 rcuh_kind::COMPILE);
ae038cb0
DJ
8991
8992 /* Save the compilation unit for later lookup. */
f1902523
JK
8993 if (cu_header.unit_type != DW_UT_type)
8994 {
8995 this_cu = XOBNEW (&objfile->objfile_obstack,
8996 struct dwarf2_per_cu_data);
8997 memset (this_cu, 0, sizeof (*this_cu));
8998 }
8999 else
9000 {
9001 auto sig_type = XOBNEW (&objfile->objfile_obstack,
9002 struct signatured_type);
9003 memset (sig_type, 0, sizeof (*sig_type));
9004 sig_type->signature = cu_header.signature;
9005 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
9006 this_cu = &sig_type->per_cu;
9007 }
9008 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 9009 this_cu->sect_off = sect_off;
f1902523 9010 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 9011 this_cu->is_dwz = is_dwz;
e3b94546 9012 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 9013 this_cu->section = section;
ae038cb0 9014
36586728 9015 if (*n_comp_units == *n_allocated)
ae038cb0 9016 {
36586728 9017 *n_allocated *= 2;
224c3ddb
SM
9018 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
9019 *all_comp_units, *n_allocated);
ae038cb0 9020 }
36586728
TT
9021 (*all_comp_units)[*n_comp_units] = this_cu;
9022 ++*n_comp_units;
ae038cb0
DJ
9023
9024 info_ptr = info_ptr + this_cu->length;
9025 }
36586728
TT
9026}
9027
9028/* Create a list of all compilation units in OBJFILE.
9029 This is only done for -readnow and building partial symtabs. */
9030
9031static void
ed2dc618 9032create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728
TT
9033{
9034 int n_allocated;
9035 int n_comp_units;
9036 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 9037 struct dwz_file *dwz;
ed2dc618 9038 struct objfile *objfile = dwarf2_per_objfile->objfile;
36586728
TT
9039
9040 n_comp_units = 0;
9041 n_allocated = 10;
8d749320 9042 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
36586728 9043
ed2dc618 9044 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
f1902523 9045 &dwarf2_per_objfile->abbrev, 0,
36586728
TT
9046 &n_allocated, &n_comp_units, &all_comp_units);
9047
ed2dc618 9048 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 9049 if (dwz != NULL)
ed2dc618
SM
9050 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
9051 1, &n_allocated, &n_comp_units,
4db1a1dc 9052 &all_comp_units);
ae038cb0 9053
8d749320
SM
9054 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
9055 struct dwarf2_per_cu_data *,
9056 n_comp_units);
ae038cb0
DJ
9057 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
9058 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
9059 xfree (all_comp_units);
9060 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
9061}
9062
5734ee8b 9063/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 9064 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 9065 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
9066 DW_AT_ranges). See the comments of add_partial_subprogram on how
9067 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 9068
72bf9492
DJ
9069static void
9070scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
9071 CORE_ADDR *highpc, int set_addrmap,
9072 struct dwarf2_cu *cu)
c906108c 9073{
72bf9492 9074 struct partial_die_info *pdi;
c906108c 9075
91c24f0a
DC
9076 /* Now, march along the PDI's, descending into ones which have
9077 interesting children but skipping the children of the other ones,
9078 until we reach the end of the compilation unit. */
c906108c 9079
72bf9492 9080 pdi = first_die;
91c24f0a 9081
72bf9492
DJ
9082 while (pdi != NULL)
9083 {
52356b79 9084 pdi->fixup (cu);
c906108c 9085
f55ee35c 9086 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
9087 children, so we need to look at them. Ditto for anonymous
9088 enums. */
933c6fe4 9089
72bf9492 9090 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 9091 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
9092 || pdi->tag == DW_TAG_imported_unit
9093 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 9094 {
72bf9492 9095 switch (pdi->tag)
c906108c
SS
9096 {
9097 case DW_TAG_subprogram:
b1dc1806 9098 case DW_TAG_inlined_subroutine:
cdc07690 9099 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 9100 break;
72929c62 9101 case DW_TAG_constant:
c906108c
SS
9102 case DW_TAG_variable:
9103 case DW_TAG_typedef:
91c24f0a 9104 case DW_TAG_union_type:
72bf9492 9105 if (!pdi->is_declaration)
63d06c5c 9106 {
72bf9492 9107 add_partial_symbol (pdi, cu);
63d06c5c
DC
9108 }
9109 break;
c906108c 9110 case DW_TAG_class_type:
680b30c7 9111 case DW_TAG_interface_type:
c906108c 9112 case DW_TAG_structure_type:
72bf9492 9113 if (!pdi->is_declaration)
c906108c 9114 {
72bf9492 9115 add_partial_symbol (pdi, cu);
c906108c 9116 }
b7fee5a3
KS
9117 if ((cu->language == language_rust
9118 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
9119 scan_partial_symbols (pdi->die_child, lowpc, highpc,
9120 set_addrmap, cu);
c906108c 9121 break;
91c24f0a 9122 case DW_TAG_enumeration_type:
72bf9492
DJ
9123 if (!pdi->is_declaration)
9124 add_partial_enumeration (pdi, cu);
c906108c
SS
9125 break;
9126 case DW_TAG_base_type:
a02abb62 9127 case DW_TAG_subrange_type:
c906108c 9128 /* File scope base type definitions are added to the partial
c5aa993b 9129 symbol table. */
72bf9492 9130 add_partial_symbol (pdi, cu);
c906108c 9131 break;
d9fa45fe 9132 case DW_TAG_namespace:
cdc07690 9133 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 9134 break;
5d7cb8df 9135 case DW_TAG_module:
cdc07690 9136 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 9137 break;
95554aad
TT
9138 case DW_TAG_imported_unit:
9139 {
9140 struct dwarf2_per_cu_data *per_cu;
9141
f4dc4d17
DE
9142 /* For now we don't handle imported units in type units. */
9143 if (cu->per_cu->is_debug_types)
9144 {
9145 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9146 " supported in type units [in module %s]"),
518817b3 9147 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
9148 }
9149
e3b94546
SM
9150 per_cu = dwarf2_find_containing_comp_unit
9151 (pdi->d.sect_off, pdi->is_dwz,
518817b3 9152 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
9153
9154 /* Go read the partial unit, if needed. */
9155 if (per_cu->v.psymtab == NULL)
b93601f3 9156 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 9157
f4dc4d17 9158 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 9159 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
9160 }
9161 break;
74921315
KS
9162 case DW_TAG_imported_declaration:
9163 add_partial_symbol (pdi, cu);
9164 break;
c906108c
SS
9165 default:
9166 break;
9167 }
9168 }
9169
72bf9492
DJ
9170 /* If the die has a sibling, skip to the sibling. */
9171
9172 pdi = pdi->die_sibling;
9173 }
9174}
9175
9176/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 9177
72bf9492 9178 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 9179 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
9180 Enumerators are an exception; they use the scope of their parent
9181 enumeration type, i.e. the name of the enumeration type is not
9182 prepended to the enumerator.
91c24f0a 9183
72bf9492
DJ
9184 There are two complexities. One is DW_AT_specification; in this
9185 case "parent" means the parent of the target of the specification,
9186 instead of the direct parent of the DIE. The other is compilers
9187 which do not emit DW_TAG_namespace; in this case we try to guess
9188 the fully qualified name of structure types from their members'
9189 linkage names. This must be done using the DIE's children rather
9190 than the children of any DW_AT_specification target. We only need
9191 to do this for structures at the top level, i.e. if the target of
9192 any DW_AT_specification (if any; otherwise the DIE itself) does not
9193 have a parent. */
9194
9195/* Compute the scope prefix associated with PDI's parent, in
9196 compilation unit CU. The result will be allocated on CU's
9197 comp_unit_obstack, or a copy of the already allocated PDI->NAME
9198 field. NULL is returned if no prefix is necessary. */
15d034d0 9199static const char *
72bf9492
DJ
9200partial_die_parent_scope (struct partial_die_info *pdi,
9201 struct dwarf2_cu *cu)
9202{
15d034d0 9203 const char *grandparent_scope;
72bf9492 9204 struct partial_die_info *parent, *real_pdi;
91c24f0a 9205
72bf9492
DJ
9206 /* We need to look at our parent DIE; if we have a DW_AT_specification,
9207 then this means the parent of the specification DIE. */
9208
9209 real_pdi = pdi;
72bf9492 9210 while (real_pdi->has_specification)
36586728
TT
9211 real_pdi = find_partial_die (real_pdi->spec_offset,
9212 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
9213
9214 parent = real_pdi->die_parent;
9215 if (parent == NULL)
9216 return NULL;
9217
9218 if (parent->scope_set)
9219 return parent->scope;
9220
52356b79 9221 parent->fixup (cu);
72bf9492 9222
10b3939b 9223 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 9224
acebe513
UW
9225 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
9226 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
9227 Work around this problem here. */
9228 if (cu->language == language_cplus
6e70227d 9229 && parent->tag == DW_TAG_namespace
acebe513
UW
9230 && strcmp (parent->name, "::") == 0
9231 && grandparent_scope == NULL)
9232 {
9233 parent->scope = NULL;
9234 parent->scope_set = 1;
9235 return NULL;
9236 }
9237
9c6c53f7
SA
9238 if (pdi->tag == DW_TAG_enumerator)
9239 /* Enumerators should not get the name of the enumeration as a prefix. */
9240 parent->scope = grandparent_scope;
9241 else if (parent->tag == DW_TAG_namespace
f55ee35c 9242 || parent->tag == DW_TAG_module
72bf9492
DJ
9243 || parent->tag == DW_TAG_structure_type
9244 || parent->tag == DW_TAG_class_type
680b30c7 9245 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
9246 || parent->tag == DW_TAG_union_type
9247 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
9248 {
9249 if (grandparent_scope == NULL)
9250 parent->scope = parent->name;
9251 else
3e43a32a
MS
9252 parent->scope = typename_concat (&cu->comp_unit_obstack,
9253 grandparent_scope,
f55ee35c 9254 parent->name, 0, cu);
72bf9492 9255 }
72bf9492
DJ
9256 else
9257 {
9258 /* FIXME drow/2004-04-01: What should we be doing with
9259 function-local names? For partial symbols, we should probably be
9260 ignoring them. */
9261 complaint (&symfile_complaints,
9d8780f0
SM
9262 _("unhandled containing DIE tag %d for DIE at %s"),
9263 parent->tag, sect_offset_str (pdi->sect_off));
72bf9492 9264 parent->scope = grandparent_scope;
c906108c
SS
9265 }
9266
72bf9492
DJ
9267 parent->scope_set = 1;
9268 return parent->scope;
9269}
9270
9271/* Return the fully scoped name associated with PDI, from compilation unit
9272 CU. The result will be allocated with malloc. */
4568ecf9 9273
72bf9492
DJ
9274static char *
9275partial_die_full_name (struct partial_die_info *pdi,
9276 struct dwarf2_cu *cu)
9277{
15d034d0 9278 const char *parent_scope;
72bf9492 9279
98bfdba5
PA
9280 /* If this is a template instantiation, we can not work out the
9281 template arguments from partial DIEs. So, unfortunately, we have
9282 to go through the full DIEs. At least any work we do building
9283 types here will be reused if full symbols are loaded later. */
9284 if (pdi->has_template_arguments)
9285 {
52356b79 9286 pdi->fixup (cu);
98bfdba5
PA
9287
9288 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
9289 {
9290 struct die_info *die;
9291 struct attribute attr;
9292 struct dwarf2_cu *ref_cu = cu;
9293
b64f50a1 9294 /* DW_FORM_ref_addr is using section offset. */
b4069958 9295 attr.name = (enum dwarf_attribute) 0;
98bfdba5 9296 attr.form = DW_FORM_ref_addr;
9c541725 9297 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
9298 die = follow_die_ref (NULL, &attr, &ref_cu);
9299
9300 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
9301 }
9302 }
9303
72bf9492
DJ
9304 parent_scope = partial_die_parent_scope (pdi, cu);
9305 if (parent_scope == NULL)
9306 return NULL;
9307 else
f55ee35c 9308 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
9309}
9310
9311static void
72bf9492 9312add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 9313{
518817b3
SM
9314 struct dwarf2_per_objfile *dwarf2_per_objfile
9315 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 9316 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9317 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 9318 CORE_ADDR addr = 0;
15d034d0 9319 const char *actual_name = NULL;
e142c38c 9320 CORE_ADDR baseaddr;
15d034d0 9321 char *built_actual_name;
e142c38c
DJ
9322
9323 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9324
15d034d0
TT
9325 built_actual_name = partial_die_full_name (pdi, cu);
9326 if (built_actual_name != NULL)
9327 actual_name = built_actual_name;
63d06c5c 9328
72bf9492
DJ
9329 if (actual_name == NULL)
9330 actual_name = pdi->name;
9331
c906108c
SS
9332 switch (pdi->tag)
9333 {
b1dc1806 9334 case DW_TAG_inlined_subroutine:
c906108c 9335 case DW_TAG_subprogram:
3e29f34a 9336 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 9337 if (pdi->is_external || cu->language == language_ada)
c906108c 9338 {
2cfa0c8d
JB
9339 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
9340 of the global scope. But in Ada, we want to be able to access
9341 nested procedures globally. So all Ada subprograms are stored
9342 in the global scope. */
f47fb265 9343 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9344 built_actual_name != NULL,
f47fb265
MS
9345 VAR_DOMAIN, LOC_BLOCK,
9346 &objfile->global_psymbols,
1762568f 9347 addr, cu->language, objfile);
c906108c
SS
9348 }
9349 else
9350 {
f47fb265 9351 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9352 built_actual_name != NULL,
f47fb265
MS
9353 VAR_DOMAIN, LOC_BLOCK,
9354 &objfile->static_psymbols,
1762568f 9355 addr, cu->language, objfile);
c906108c 9356 }
0c1b455e
TT
9357
9358 if (pdi->main_subprogram && actual_name != NULL)
9359 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 9360 break;
72929c62
JB
9361 case DW_TAG_constant:
9362 {
af5bf4ad 9363 std::vector<partial_symbol *> *list;
72929c62
JB
9364
9365 if (pdi->is_external)
9366 list = &objfile->global_psymbols;
9367 else
9368 list = &objfile->static_psymbols;
f47fb265 9369 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9370 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 9371 list, 0, cu->language, objfile);
72929c62
JB
9372 }
9373 break;
c906108c 9374 case DW_TAG_variable:
95554aad
TT
9375 if (pdi->d.locdesc)
9376 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 9377
95554aad 9378 if (pdi->d.locdesc
caac4577
JG
9379 && addr == 0
9380 && !dwarf2_per_objfile->has_section_at_zero)
9381 {
9382 /* A global or static variable may also have been stripped
9383 out by the linker if unused, in which case its address
9384 will be nullified; do not add such variables into partial
9385 symbol table then. */
9386 }
9387 else if (pdi->is_external)
c906108c
SS
9388 {
9389 /* Global Variable.
9390 Don't enter into the minimal symbol tables as there is
9391 a minimal symbol table entry from the ELF symbols already.
9392 Enter into partial symbol table if it has a location
9393 descriptor or a type.
9394 If the location descriptor is missing, new_symbol will create
9395 a LOC_UNRESOLVED symbol, the address of the variable will then
9396 be determined from the minimal symbol table whenever the variable
9397 is referenced.
9398 The address for the partial symbol table entry is not
9399 used by GDB, but it comes in handy for debugging partial symbol
9400 table building. */
9401
95554aad 9402 if (pdi->d.locdesc || pdi->has_type)
f47fb265 9403 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9404 built_actual_name != NULL,
f47fb265
MS
9405 VAR_DOMAIN, LOC_STATIC,
9406 &objfile->global_psymbols,
1762568f 9407 addr + baseaddr,
f47fb265 9408 cu->language, objfile);
c906108c
SS
9409 }
9410 else
9411 {
ff908ebf
AW
9412 int has_loc = pdi->d.locdesc != NULL;
9413
9414 /* Static Variable. Skip symbols whose value we cannot know (those
9415 without location descriptors or constant values). */
9416 if (!has_loc && !pdi->has_const_value)
decbce07 9417 {
15d034d0 9418 xfree (built_actual_name);
decbce07
MS
9419 return;
9420 }
ff908ebf 9421
f47fb265 9422 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9423 built_actual_name != NULL,
f47fb265
MS
9424 VAR_DOMAIN, LOC_STATIC,
9425 &objfile->static_psymbols,
ff908ebf 9426 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 9427 cu->language, objfile);
c906108c
SS
9428 }
9429 break;
9430 case DW_TAG_typedef:
9431 case DW_TAG_base_type:
a02abb62 9432 case DW_TAG_subrange_type:
38d518c9 9433 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9434 built_actual_name != NULL,
176620f1 9435 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 9436 &objfile->static_psymbols,
1762568f 9437 0, cu->language, objfile);
c906108c 9438 break;
74921315 9439 case DW_TAG_imported_declaration:
72bf9492
DJ
9440 case DW_TAG_namespace:
9441 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9442 built_actual_name != NULL,
72bf9492
DJ
9443 VAR_DOMAIN, LOC_TYPEDEF,
9444 &objfile->global_psymbols,
1762568f 9445 0, cu->language, objfile);
72bf9492 9446 break;
530e8392
KB
9447 case DW_TAG_module:
9448 add_psymbol_to_list (actual_name, strlen (actual_name),
9449 built_actual_name != NULL,
9450 MODULE_DOMAIN, LOC_TYPEDEF,
9451 &objfile->global_psymbols,
1762568f 9452 0, cu->language, objfile);
530e8392 9453 break;
c906108c 9454 case DW_TAG_class_type:
680b30c7 9455 case DW_TAG_interface_type:
c906108c
SS
9456 case DW_TAG_structure_type:
9457 case DW_TAG_union_type:
9458 case DW_TAG_enumeration_type:
fa4028e9
JB
9459 /* Skip external references. The DWARF standard says in the section
9460 about "Structure, Union, and Class Type Entries": "An incomplete
9461 structure, union or class type is represented by a structure,
9462 union or class entry that does not have a byte size attribute
9463 and that has a DW_AT_declaration attribute." */
9464 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 9465 {
15d034d0 9466 xfree (built_actual_name);
decbce07
MS
9467 return;
9468 }
fa4028e9 9469
63d06c5c
DC
9470 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9471 static vs. global. */
38d518c9 9472 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9473 built_actual_name != NULL,
176620f1 9474 STRUCT_DOMAIN, LOC_TYPEDEF,
9c37b5ae 9475 cu->language == language_cplus
63d06c5c
DC
9476 ? &objfile->global_psymbols
9477 : &objfile->static_psymbols,
1762568f 9478 0, cu->language, objfile);
c906108c 9479
c906108c
SS
9480 break;
9481 case DW_TAG_enumerator:
38d518c9 9482 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9483 built_actual_name != NULL,
176620f1 9484 VAR_DOMAIN, LOC_CONST,
9c37b5ae 9485 cu->language == language_cplus
f6fe98ef
DJ
9486 ? &objfile->global_psymbols
9487 : &objfile->static_psymbols,
1762568f 9488 0, cu->language, objfile);
c906108c
SS
9489 break;
9490 default:
9491 break;
9492 }
5c4e30ca 9493
15d034d0 9494 xfree (built_actual_name);
c906108c
SS
9495}
9496
5c4e30ca
DC
9497/* Read a partial die corresponding to a namespace; also, add a symbol
9498 corresponding to that namespace to the symbol table. NAMESPACE is
9499 the name of the enclosing namespace. */
91c24f0a 9500
72bf9492
DJ
9501static void
9502add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 9503 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9504 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 9505{
72bf9492 9506 /* Add a symbol for the namespace. */
e7c27a73 9507
72bf9492 9508 add_partial_symbol (pdi, cu);
5c4e30ca
DC
9509
9510 /* Now scan partial symbols in that namespace. */
9511
91c24f0a 9512 if (pdi->has_children)
cdc07690 9513 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
9514}
9515
5d7cb8df
JK
9516/* Read a partial die corresponding to a Fortran module. */
9517
9518static void
9519add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 9520 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 9521{
530e8392
KB
9522 /* Add a symbol for the namespace. */
9523
9524 add_partial_symbol (pdi, cu);
9525
f55ee35c 9526 /* Now scan partial symbols in that module. */
5d7cb8df
JK
9527
9528 if (pdi->has_children)
cdc07690 9529 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
9530}
9531
b1dc1806
XR
9532/* Read a partial die corresponding to a subprogram or an inlined
9533 subprogram and create a partial symbol for that subprogram.
9534 When the CU language allows it, this routine also defines a partial
9535 symbol for each nested subprogram that this subprogram contains.
9536 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9537 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 9538
cdc07690
YQ
9539 PDI may also be a lexical block, in which case we simply search
9540 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9541 Again, this is only performed when the CU language allows this
9542 type of definitions. */
9543
9544static void
9545add_partial_subprogram (struct partial_die_info *pdi,
9546 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9547 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 9548{
b1dc1806 9549 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
9550 {
9551 if (pdi->has_pc_info)
9552 {
9553 if (pdi->lowpc < *lowpc)
9554 *lowpc = pdi->lowpc;
9555 if (pdi->highpc > *highpc)
9556 *highpc = pdi->highpc;
cdc07690 9557 if (set_addrmap)
5734ee8b 9558 {
518817b3 9559 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
9560 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9561 CORE_ADDR baseaddr;
9562 CORE_ADDR highpc;
9563 CORE_ADDR lowpc;
5734ee8b
DJ
9564
9565 baseaddr = ANOFFSET (objfile->section_offsets,
9566 SECT_OFF_TEXT (objfile));
3e29f34a
MR
9567 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9568 pdi->lowpc + baseaddr);
9569 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9570 pdi->highpc + baseaddr);
9571 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 9572 cu->per_cu->v.psymtab);
5734ee8b 9573 }
481860b3
GB
9574 }
9575
9576 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9577 {
bc30ff58 9578 if (!pdi->is_declaration)
e8d05480
JB
9579 /* Ignore subprogram DIEs that do not have a name, they are
9580 illegal. Do not emit a complaint at this point, we will
9581 do so when we convert this psymtab into a symtab. */
9582 if (pdi->name)
9583 add_partial_symbol (pdi, cu);
bc30ff58
JB
9584 }
9585 }
6e70227d 9586
bc30ff58
JB
9587 if (! pdi->has_children)
9588 return;
9589
9590 if (cu->language == language_ada)
9591 {
9592 pdi = pdi->die_child;
9593 while (pdi != NULL)
9594 {
52356b79 9595 pdi->fixup (cu);
bc30ff58 9596 if (pdi->tag == DW_TAG_subprogram
b1dc1806 9597 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 9598 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9599 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9600 pdi = pdi->die_sibling;
9601 }
9602 }
9603}
9604
91c24f0a
DC
9605/* Read a partial die corresponding to an enumeration type. */
9606
72bf9492
DJ
9607static void
9608add_partial_enumeration (struct partial_die_info *enum_pdi,
9609 struct dwarf2_cu *cu)
91c24f0a 9610{
72bf9492 9611 struct partial_die_info *pdi;
91c24f0a
DC
9612
9613 if (enum_pdi->name != NULL)
72bf9492
DJ
9614 add_partial_symbol (enum_pdi, cu);
9615
9616 pdi = enum_pdi->die_child;
9617 while (pdi)
91c24f0a 9618 {
72bf9492 9619 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 9620 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 9621 else
72bf9492
DJ
9622 add_partial_symbol (pdi, cu);
9623 pdi = pdi->die_sibling;
91c24f0a 9624 }
91c24f0a
DC
9625}
9626
6caca83c
CC
9627/* Return the initial uleb128 in the die at INFO_PTR. */
9628
9629static unsigned int
d521ce57 9630peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9631{
9632 unsigned int bytes_read;
9633
9634 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9635}
9636
685af9cd
TT
9637/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9638 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9639
4bb7a0a7
DJ
9640 Return the corresponding abbrev, or NULL if the number is zero (indicating
9641 an empty DIE). In either case *BYTES_READ will be set to the length of
9642 the initial number. */
9643
9644static struct abbrev_info *
685af9cd
TT
9645peek_die_abbrev (const die_reader_specs &reader,
9646 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 9647{
685af9cd 9648 dwarf2_cu *cu = reader.cu;
518817b3 9649 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
9650 unsigned int abbrev_number
9651 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
9652
9653 if (abbrev_number == 0)
9654 return NULL;
9655
685af9cd 9656 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
9657 if (!abbrev)
9658 {
422b9917 9659 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 9660 " at offset %s [in module %s]"),
422b9917 9661 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 9662 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9663 }
9664
9665 return abbrev;
9666}
9667
93311388
DE
9668/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9669 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9670 DIE. Any children of the skipped DIEs will also be skipped. */
9671
d521ce57
TT
9672static const gdb_byte *
9673skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9674{
4bb7a0a7
DJ
9675 while (1)
9676 {
685af9cd
TT
9677 unsigned int bytes_read;
9678 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9679
4bb7a0a7
DJ
9680 if (abbrev == NULL)
9681 return info_ptr + bytes_read;
9682 else
dee91e82 9683 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9684 }
9685}
9686
93311388
DE
9687/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9688 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9689 abbrev corresponding to that skipped uleb128 should be passed in
9690 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9691 children. */
9692
d521ce57
TT
9693static const gdb_byte *
9694skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9695 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9696{
9697 unsigned int bytes_read;
9698 struct attribute attr;
dee91e82
DE
9699 bfd *abfd = reader->abfd;
9700 struct dwarf2_cu *cu = reader->cu;
d521ce57 9701 const gdb_byte *buffer = reader->buffer;
f664829e 9702 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9703 unsigned int form, i;
9704
9705 for (i = 0; i < abbrev->num_attrs; i++)
9706 {
9707 /* The only abbrev we care about is DW_AT_sibling. */
9708 if (abbrev->attrs[i].name == DW_AT_sibling)
9709 {
dee91e82 9710 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9711 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9712 complaint (&symfile_complaints,
9713 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9714 else
b9502d3f 9715 {
9c541725
PA
9716 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9717 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9718
9719 if (sibling_ptr < info_ptr)
9720 complaint (&symfile_complaints,
9721 _("DW_AT_sibling points backwards"));
22869d73
KS
9722 else if (sibling_ptr > reader->buffer_end)
9723 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9724 else
9725 return sibling_ptr;
9726 }
4bb7a0a7
DJ
9727 }
9728
9729 /* If it isn't DW_AT_sibling, skip this attribute. */
9730 form = abbrev->attrs[i].form;
9731 skip_attribute:
9732 switch (form)
9733 {
4bb7a0a7 9734 case DW_FORM_ref_addr:
ae411497
TT
9735 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9736 and later it is offset sized. */
9737 if (cu->header.version == 2)
9738 info_ptr += cu->header.addr_size;
9739 else
9740 info_ptr += cu->header.offset_size;
9741 break;
36586728
TT
9742 case DW_FORM_GNU_ref_alt:
9743 info_ptr += cu->header.offset_size;
9744 break;
ae411497 9745 case DW_FORM_addr:
4bb7a0a7
DJ
9746 info_ptr += cu->header.addr_size;
9747 break;
9748 case DW_FORM_data1:
9749 case DW_FORM_ref1:
9750 case DW_FORM_flag:
9751 info_ptr += 1;
9752 break;
2dc7f7b3 9753 case DW_FORM_flag_present:
43988095 9754 case DW_FORM_implicit_const:
2dc7f7b3 9755 break;
4bb7a0a7
DJ
9756 case DW_FORM_data2:
9757 case DW_FORM_ref2:
9758 info_ptr += 2;
9759 break;
9760 case DW_FORM_data4:
9761 case DW_FORM_ref4:
9762 info_ptr += 4;
9763 break;
9764 case DW_FORM_data8:
9765 case DW_FORM_ref8:
55f1336d 9766 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9767 info_ptr += 8;
9768 break;
0224619f
JK
9769 case DW_FORM_data16:
9770 info_ptr += 16;
9771 break;
4bb7a0a7 9772 case DW_FORM_string:
9b1c24c8 9773 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9774 info_ptr += bytes_read;
9775 break;
2dc7f7b3 9776 case DW_FORM_sec_offset:
4bb7a0a7 9777 case DW_FORM_strp:
36586728 9778 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9779 info_ptr += cu->header.offset_size;
9780 break;
2dc7f7b3 9781 case DW_FORM_exprloc:
4bb7a0a7
DJ
9782 case DW_FORM_block:
9783 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9784 info_ptr += bytes_read;
9785 break;
9786 case DW_FORM_block1:
9787 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9788 break;
9789 case DW_FORM_block2:
9790 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9791 break;
9792 case DW_FORM_block4:
9793 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9794 break;
9795 case DW_FORM_sdata:
9796 case DW_FORM_udata:
9797 case DW_FORM_ref_udata:
3019eac3
DE
9798 case DW_FORM_GNU_addr_index:
9799 case DW_FORM_GNU_str_index:
d521ce57 9800 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9801 break;
9802 case DW_FORM_indirect:
9803 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9804 info_ptr += bytes_read;
9805 /* We need to continue parsing from here, so just go back to
9806 the top. */
9807 goto skip_attribute;
9808
9809 default:
3e43a32a
MS
9810 error (_("Dwarf Error: Cannot handle %s "
9811 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9812 dwarf_form_name (form),
9813 bfd_get_filename (abfd));
9814 }
9815 }
9816
9817 if (abbrev->has_children)
dee91e82 9818 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9819 else
9820 return info_ptr;
9821}
9822
93311388 9823/* Locate ORIG_PDI's sibling.
dee91e82 9824 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9825
d521ce57 9826static const gdb_byte *
dee91e82
DE
9827locate_pdi_sibling (const struct die_reader_specs *reader,
9828 struct partial_die_info *orig_pdi,
d521ce57 9829 const gdb_byte *info_ptr)
91c24f0a
DC
9830{
9831 /* Do we know the sibling already? */
72bf9492 9832
91c24f0a
DC
9833 if (orig_pdi->sibling)
9834 return orig_pdi->sibling;
9835
9836 /* Are there any children to deal with? */
9837
9838 if (!orig_pdi->has_children)
9839 return info_ptr;
9840
4bb7a0a7 9841 /* Skip the children the long way. */
91c24f0a 9842
dee91e82 9843 return skip_children (reader, info_ptr);
91c24f0a
DC
9844}
9845
257e7a09 9846/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9847 not NULL. */
c906108c
SS
9848
9849static void
257e7a09
YQ
9850dwarf2_read_symtab (struct partial_symtab *self,
9851 struct objfile *objfile)
c906108c 9852{
ed2dc618
SM
9853 struct dwarf2_per_objfile *dwarf2_per_objfile
9854 = get_dwarf2_per_objfile (objfile);
9855
257e7a09 9856 if (self->readin)
c906108c 9857 {
442e4d9c 9858 warning (_("bug: psymtab for %s is already read in."),
257e7a09 9859 self->filename);
442e4d9c
YQ
9860 }
9861 else
9862 {
9863 if (info_verbose)
c906108c 9864 {
442e4d9c 9865 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 9866 self->filename);
442e4d9c 9867 gdb_flush (gdb_stdout);
c906108c 9868 }
c906108c 9869
442e4d9c
YQ
9870 /* If this psymtab is constructed from a debug-only objfile, the
9871 has_section_at_zero flag will not necessarily be correct. We
9872 can get the correct value for this flag by looking at the data
9873 associated with the (presumably stripped) associated objfile. */
9874 if (objfile->separate_debug_objfile_backlink)
9875 {
9876 struct dwarf2_per_objfile *dpo_backlink
ed2dc618 9877 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9a619af0 9878
442e4d9c
YQ
9879 dwarf2_per_objfile->has_section_at_zero
9880 = dpo_backlink->has_section_at_zero;
9881 }
b2ab525c 9882
442e4d9c 9883 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 9884
257e7a09 9885 psymtab_to_symtab_1 (self);
c906108c 9886
442e4d9c
YQ
9887 /* Finish up the debug error message. */
9888 if (info_verbose)
9889 printf_filtered (_("done.\n"));
c906108c 9890 }
95554aad 9891
ed2dc618 9892 process_cu_includes (dwarf2_per_objfile);
c906108c 9893}
9cdd5dbd
DE
9894\f
9895/* Reading in full CUs. */
c906108c 9896
10b3939b
DJ
9897/* Add PER_CU to the queue. */
9898
9899static void
95554aad
TT
9900queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9901 enum language pretend_language)
10b3939b
DJ
9902{
9903 struct dwarf2_queue_item *item;
9904
9905 per_cu->queued = 1;
8d749320 9906 item = XNEW (struct dwarf2_queue_item);
10b3939b 9907 item->per_cu = per_cu;
95554aad 9908 item->pretend_language = pretend_language;
10b3939b
DJ
9909 item->next = NULL;
9910
9911 if (dwarf2_queue == NULL)
9912 dwarf2_queue = item;
9913 else
9914 dwarf2_queue_tail->next = item;
9915
9916 dwarf2_queue_tail = item;
9917}
9918
89e63ee4
DE
9919/* If PER_CU is not yet queued, add it to the queue.
9920 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9921 dependency.
0907af0c 9922 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9923 meaning either PER_CU is already queued or it is already loaded.
9924
9925 N.B. There is an invariant here that if a CU is queued then it is loaded.
9926 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9927
9928static int
89e63ee4 9929maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9930 struct dwarf2_per_cu_data *per_cu,
9931 enum language pretend_language)
9932{
9933 /* We may arrive here during partial symbol reading, if we need full
9934 DIEs to process an unusual case (e.g. template arguments). Do
9935 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9936 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9937 {
9938 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9939 return 1;
9940 return 0;
9941 }
9942
9943 /* Mark the dependence relation so that we don't flush PER_CU
9944 too early. */
89e63ee4
DE
9945 if (dependent_cu != NULL)
9946 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9947
9948 /* If it's already on the queue, we have nothing to do. */
9949 if (per_cu->queued)
9950 return 0;
9951
9952 /* If the compilation unit is already loaded, just mark it as
9953 used. */
9954 if (per_cu->cu != NULL)
9955 {
9956 per_cu->cu->last_used = 0;
9957 return 0;
9958 }
9959
9960 /* Add it to the queue. */
9961 queue_comp_unit (per_cu, pretend_language);
9962
9963 return 1;
9964}
9965
10b3939b
DJ
9966/* Process the queue. */
9967
9968static void
ed2dc618 9969process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
9970{
9971 struct dwarf2_queue_item *item, *next_item;
9972
b4f54984 9973 if (dwarf_read_debug)
45cfd468
DE
9974 {
9975 fprintf_unfiltered (gdb_stdlog,
9976 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9977 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9978 }
9979
03dd20cc
DJ
9980 /* The queue starts out with one item, but following a DIE reference
9981 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9982 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9983 {
cc12ce38
DE
9984 if ((dwarf2_per_objfile->using_index
9985 ? !item->per_cu->v.quick->compunit_symtab
9986 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9987 /* Skip dummy CUs. */
9988 && item->per_cu->cu != NULL)
f4dc4d17
DE
9989 {
9990 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9991 unsigned int debug_print_threshold;
247f5c4f 9992 char buf[100];
f4dc4d17 9993
247f5c4f 9994 if (per_cu->is_debug_types)
f4dc4d17 9995 {
247f5c4f
DE
9996 struct signatured_type *sig_type =
9997 (struct signatured_type *) per_cu;
9998
9d8780f0 9999 sprintf (buf, "TU %s at offset %s",
73be47f5 10000 hex_string (sig_type->signature),
9d8780f0 10001 sect_offset_str (per_cu->sect_off));
73be47f5
DE
10002 /* There can be 100s of TUs.
10003 Only print them in verbose mode. */
10004 debug_print_threshold = 2;
f4dc4d17 10005 }
247f5c4f 10006 else
73be47f5 10007 {
9d8780f0
SM
10008 sprintf (buf, "CU at offset %s",
10009 sect_offset_str (per_cu->sect_off));
73be47f5
DE
10010 debug_print_threshold = 1;
10011 }
247f5c4f 10012
b4f54984 10013 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 10014 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
10015
10016 if (per_cu->is_debug_types)
10017 process_full_type_unit (per_cu, item->pretend_language);
10018 else
10019 process_full_comp_unit (per_cu, item->pretend_language);
10020
b4f54984 10021 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 10022 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 10023 }
10b3939b
DJ
10024
10025 item->per_cu->queued = 0;
10026 next_item = item->next;
10027 xfree (item);
10028 }
10029
10030 dwarf2_queue_tail = NULL;
45cfd468 10031
b4f54984 10032 if (dwarf_read_debug)
45cfd468
DE
10033 {
10034 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 10035 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 10036 }
10b3939b
DJ
10037}
10038
10b3939b
DJ
10039/* Read in full symbols for PST, and anything it depends on. */
10040
c906108c 10041static void
fba45db2 10042psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 10043{
10b3939b 10044 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
10045 int i;
10046
95554aad
TT
10047 if (pst->readin)
10048 return;
10049
aaa75496 10050 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
10051 if (!pst->dependencies[i]->readin
10052 && pst->dependencies[i]->user == NULL)
aaa75496
JB
10053 {
10054 /* Inform about additional files that need to be read in. */
10055 if (info_verbose)
10056 {
a3f17187 10057 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
10058 fputs_filtered (" ", gdb_stdout);
10059 wrap_here ("");
10060 fputs_filtered ("and ", gdb_stdout);
10061 wrap_here ("");
10062 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 10063 wrap_here (""); /* Flush output. */
aaa75496
JB
10064 gdb_flush (gdb_stdout);
10065 }
10066 psymtab_to_symtab_1 (pst->dependencies[i]);
10067 }
10068
9a3c8263 10069 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
10070
10071 if (per_cu == NULL)
aaa75496
JB
10072 {
10073 /* It's an include file, no symbols to read for it.
10074 Everything is in the parent symtab. */
10075 pst->readin = 1;
10076 return;
10077 }
c906108c 10078
a0f42c21 10079 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
10080}
10081
dee91e82
DE
10082/* Trivial hash function for die_info: the hash value of a DIE
10083 is its offset in .debug_info for this objfile. */
10b3939b 10084
dee91e82
DE
10085static hashval_t
10086die_hash (const void *item)
10b3939b 10087{
9a3c8263 10088 const struct die_info *die = (const struct die_info *) item;
6502dd73 10089
9c541725 10090 return to_underlying (die->sect_off);
dee91e82 10091}
63d06c5c 10092
dee91e82
DE
10093/* Trivial comparison function for die_info structures: two DIEs
10094 are equal if they have the same offset. */
98bfdba5 10095
dee91e82
DE
10096static int
10097die_eq (const void *item_lhs, const void *item_rhs)
10098{
9a3c8263
SM
10099 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
10100 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 10101
9c541725 10102 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 10103}
c906108c 10104
dee91e82
DE
10105/* die_reader_func for load_full_comp_unit.
10106 This is identical to read_signatured_type_reader,
10107 but is kept separate for now. */
c906108c 10108
dee91e82
DE
10109static void
10110load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 10111 const gdb_byte *info_ptr,
dee91e82
DE
10112 struct die_info *comp_unit_die,
10113 int has_children,
10114 void *data)
10115{
10116 struct dwarf2_cu *cu = reader->cu;
9a3c8263 10117 enum language *language_ptr = (enum language *) data;
6caca83c 10118
dee91e82
DE
10119 gdb_assert (cu->die_hash == NULL);
10120 cu->die_hash =
10121 htab_create_alloc_ex (cu->header.length / 12,
10122 die_hash,
10123 die_eq,
10124 NULL,
10125 &cu->comp_unit_obstack,
10126 hashtab_obstack_allocate,
10127 dummy_obstack_deallocate);
e142c38c 10128
dee91e82
DE
10129 if (has_children)
10130 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
10131 &info_ptr, comp_unit_die);
10132 cu->dies = comp_unit_die;
10133 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
10134
10135 /* We try not to read any attributes in this function, because not
9cdd5dbd 10136 all CUs needed for references have been loaded yet, and symbol
10b3939b 10137 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
10138 or we won't be able to build types correctly.
10139 Similarly, if we do not read the producer, we can not apply
10140 producer-specific interpretation. */
95554aad 10141 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 10142}
10b3939b 10143
dee91e82 10144/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 10145
dee91e82 10146static void
95554aad
TT
10147load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
10148 enum language pretend_language)
dee91e82 10149{
3019eac3 10150 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 10151
f4dc4d17
DE
10152 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
10153 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
10154}
10155
3da10d80
KS
10156/* Add a DIE to the delayed physname list. */
10157
10158static void
10159add_to_method_list (struct type *type, int fnfield_index, int index,
10160 const char *name, struct die_info *die,
10161 struct dwarf2_cu *cu)
10162{
10163 struct delayed_method_info mi;
10164 mi.type = type;
10165 mi.fnfield_index = fnfield_index;
10166 mi.index = index;
10167 mi.name = name;
10168 mi.die = die;
c89b44cd 10169 cu->method_list.push_back (mi);
3da10d80
KS
10170}
10171
3693fdb3
PA
10172/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
10173 "const" / "volatile". If so, decrements LEN by the length of the
10174 modifier and return true. Otherwise return false. */
10175
10176template<size_t N>
10177static bool
10178check_modifier (const char *physname, size_t &len, const char (&mod)[N])
10179{
10180 size_t mod_len = sizeof (mod) - 1;
10181 if (len > mod_len && startswith (physname + (len - mod_len), mod))
10182 {
10183 len -= mod_len;
10184 return true;
10185 }
10186 return false;
10187}
10188
3da10d80
KS
10189/* Compute the physnames of any methods on the CU's method list.
10190
10191 The computation of method physnames is delayed in order to avoid the
10192 (bad) condition that one of the method's formal parameters is of an as yet
10193 incomplete type. */
10194
10195static void
10196compute_delayed_physnames (struct dwarf2_cu *cu)
10197{
3693fdb3 10198 /* Only C++ delays computing physnames. */
c89b44cd 10199 if (cu->method_list.empty ())
3693fdb3
PA
10200 return;
10201 gdb_assert (cu->language == language_cplus);
10202
c89b44cd 10203 for (struct delayed_method_info &mi : cu->method_list)
3da10d80 10204 {
1d06ead6 10205 const char *physname;
3da10d80 10206 struct fn_fieldlist *fn_flp
c89b44cd
TT
10207 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
10208 physname = dwarf2_physname (mi.name, mi.die, cu);
10209 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 10210 = physname ? physname : "";
3693fdb3
PA
10211
10212 /* Since there's no tag to indicate whether a method is a
10213 const/volatile overload, extract that information out of the
10214 demangled name. */
10215 if (physname != NULL)
10216 {
10217 size_t len = strlen (physname);
10218
10219 while (1)
10220 {
10221 if (physname[len] == ')') /* shortcut */
10222 break;
10223 else if (check_modifier (physname, len, " const"))
c89b44cd 10224 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 10225 else if (check_modifier (physname, len, " volatile"))
c89b44cd 10226 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
10227 else
10228 break;
10229 }
10230 }
3da10d80 10231 }
c89b44cd
TT
10232
10233 /* The list is no longer needed. */
10234 cu->method_list.clear ();
3da10d80
KS
10235}
10236
a766d390
DE
10237/* Go objects should be embedded in a DW_TAG_module DIE,
10238 and it's not clear if/how imported objects will appear.
10239 To keep Go support simple until that's worked out,
10240 go back through what we've read and create something usable.
10241 We could do this while processing each DIE, and feels kinda cleaner,
10242 but that way is more invasive.
10243 This is to, for example, allow the user to type "p var" or "b main"
10244 without having to specify the package name, and allow lookups
10245 of module.object to work in contexts that use the expression
10246 parser. */
10247
10248static void
10249fixup_go_packaging (struct dwarf2_cu *cu)
10250{
10251 char *package_name = NULL;
10252 struct pending *list;
10253 int i;
10254
10255 for (list = global_symbols; list != NULL; list = list->next)
10256 {
10257 for (i = 0; i < list->nsyms; ++i)
10258 {
10259 struct symbol *sym = list->symbol[i];
10260
10261 if (SYMBOL_LANGUAGE (sym) == language_go
10262 && SYMBOL_CLASS (sym) == LOC_BLOCK)
10263 {
10264 char *this_package_name = go_symbol_package_name (sym);
10265
10266 if (this_package_name == NULL)
10267 continue;
10268 if (package_name == NULL)
10269 package_name = this_package_name;
10270 else
10271 {
518817b3
SM
10272 struct objfile *objfile
10273 = cu->per_cu->dwarf2_per_objfile->objfile;
a766d390
DE
10274 if (strcmp (package_name, this_package_name) != 0)
10275 complaint (&symfile_complaints,
10276 _("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
10277 (symbol_symtab (sym) != NULL
10278 ? symtab_to_filename_for_display
10279 (symbol_symtab (sym))
e3b94546 10280 : objfile_name (objfile)),
a766d390
DE
10281 this_package_name, package_name);
10282 xfree (this_package_name);
10283 }
10284 }
10285 }
10286 }
10287
10288 if (package_name != NULL)
10289 {
518817b3 10290 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 10291 const char *saved_package_name
224c3ddb
SM
10292 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
10293 package_name,
10294 strlen (package_name));
19f392bc
UW
10295 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
10296 saved_package_name);
a766d390
DE
10297 struct symbol *sym;
10298
10299 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10300
e623cf5d 10301 sym = allocate_symbol (objfile);
f85f34ed 10302 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
10303 SYMBOL_SET_NAMES (sym, saved_package_name,
10304 strlen (saved_package_name), 0, objfile);
a766d390
DE
10305 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
10306 e.g., "main" finds the "main" module and not C's main(). */
10307 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 10308 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
10309 SYMBOL_TYPE (sym) = type;
10310
10311 add_symbol_to_list (sym, &global_symbols);
10312
10313 xfree (package_name);
10314 }
10315}
10316
c9317f21
TT
10317/* Allocate a fully-qualified name consisting of the two parts on the
10318 obstack. */
10319
10320static const char *
10321rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
10322{
10323 return obconcat (obstack, p1, "::", p2, (char *) NULL);
10324}
10325
10326/* A helper that allocates a struct discriminant_info to attach to a
10327 union type. */
10328
10329static struct discriminant_info *
10330alloc_discriminant_info (struct type *type, int discriminant_index,
10331 int default_index)
10332{
10333 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
10334 gdb_assert (discriminant_index == -1
10335 || (discriminant_index >= 0
10336 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 10337 gdb_assert (default_index == -1
c7b15a66 10338 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
10339
10340 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
10341
10342 struct discriminant_info *disc
10343 = ((struct discriminant_info *)
10344 TYPE_ZALLOC (type,
10345 offsetof (struct discriminant_info, discriminants)
10346 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
10347 disc->default_index = default_index;
10348 disc->discriminant_index = discriminant_index;
10349
10350 struct dynamic_prop prop;
10351 prop.kind = PROP_UNDEFINED;
10352 prop.data.baton = disc;
10353
10354 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
10355
10356 return disc;
10357}
10358
10359/* Some versions of rustc emitted enums in an unusual way.
10360
10361 Ordinary enums were emitted as unions. The first element of each
10362 structure in the union was named "RUST$ENUM$DISR". This element
10363 held the discriminant.
10364
10365 These versions of Rust also implemented the "non-zero"
10366 optimization. When the enum had two values, and one is empty and
10367 the other holds a pointer that cannot be zero, the pointer is used
10368 as the discriminant, with a zero value meaning the empty variant.
10369 Here, the union's first member is of the form
10370 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
10371 where the fieldnos are the indices of the fields that should be
10372 traversed in order to find the field (which may be several fields deep)
10373 and the variantname is the name of the variant of the case when the
10374 field is zero.
10375
10376 This function recognizes whether TYPE is of one of these forms,
10377 and, if so, smashes it to be a variant type. */
10378
10379static void
10380quirk_rust_enum (struct type *type, struct objfile *objfile)
10381{
10382 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10383
10384 /* We don't need to deal with empty enums. */
10385 if (TYPE_NFIELDS (type) == 0)
10386 return;
10387
10388#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
10389 if (TYPE_NFIELDS (type) == 1
10390 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
10391 {
10392 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
10393
10394 /* Decode the field name to find the offset of the
10395 discriminant. */
10396 ULONGEST bit_offset = 0;
10397 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10398 while (name[0] >= '0' && name[0] <= '9')
10399 {
10400 char *tail;
10401 unsigned long index = strtoul (name, &tail, 10);
10402 name = tail;
10403 if (*name != '$'
10404 || index >= TYPE_NFIELDS (field_type)
10405 || (TYPE_FIELD_LOC_KIND (field_type, index)
10406 != FIELD_LOC_KIND_BITPOS))
10407 {
10408 complaint (&symfile_complaints,
10409 _("Could not parse Rust enum encoding string \"%s\""
10410 "[in module %s]"),
10411 TYPE_FIELD_NAME (type, 0),
10412 objfile_name (objfile));
10413 return;
10414 }
10415 ++name;
10416
10417 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10418 field_type = TYPE_FIELD_TYPE (field_type, index);
10419 }
10420
10421 /* Make a union to hold the variants. */
10422 struct type *union_type = alloc_type (objfile);
10423 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10424 TYPE_NFIELDS (union_type) = 3;
10425 TYPE_FIELDS (union_type)
10426 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10427 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10428
10429 /* Put the discriminant must at index 0. */
10430 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10431 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10432 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10433 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10434
10435 /* The order of fields doesn't really matter, so put the real
10436 field at index 1 and the data-less field at index 2. */
10437 struct discriminant_info *disc
10438 = alloc_discriminant_info (union_type, 0, 1);
10439 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10440 TYPE_FIELD_NAME (union_type, 1)
10441 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10442 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10443 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10444 TYPE_FIELD_NAME (union_type, 1));
10445
10446 const char *dataless_name
10447 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10448 name);
10449 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10450 dataless_name);
10451 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10452 /* NAME points into the original discriminant name, which
10453 already has the correct lifetime. */
10454 TYPE_FIELD_NAME (union_type, 2) = name;
10455 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10456 disc->discriminants[2] = 0;
10457
10458 /* Smash this type to be a structure type. We have to do this
10459 because the type has already been recorded. */
10460 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10461 TYPE_NFIELDS (type) = 1;
10462 TYPE_FIELDS (type)
10463 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10464
10465 /* Install the variant part. */
10466 TYPE_FIELD_TYPE (type, 0) = union_type;
10467 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10468 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10469 }
10470 else if (TYPE_NFIELDS (type) == 1)
10471 {
10472 /* We assume that a union with a single field is a univariant
10473 enum. */
10474 /* Smash this type to be a structure type. We have to do this
10475 because the type has already been recorded. */
10476 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10477
10478 /* Make a union to hold the variants. */
10479 struct type *union_type = alloc_type (objfile);
10480 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10481 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10482 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10483 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10484
10485 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10486 const char *variant_name
10487 = rust_last_path_segment (TYPE_NAME (field_type));
10488 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10489 TYPE_NAME (field_type)
10490 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 10491 TYPE_NAME (type), variant_name);
c9317f21
TT
10492
10493 /* Install the union in the outer struct type. */
10494 TYPE_NFIELDS (type) = 1;
10495 TYPE_FIELDS (type)
10496 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10497 TYPE_FIELD_TYPE (type, 0) = union_type;
10498 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10499 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10500
10501 alloc_discriminant_info (union_type, -1, 0);
10502 }
10503 else
10504 {
10505 struct type *disr_type = nullptr;
10506 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10507 {
10508 disr_type = TYPE_FIELD_TYPE (type, i);
10509
10510 if (TYPE_NFIELDS (disr_type) == 0)
10511 {
10512 /* Could be data-less variant, so keep going. */
10513 }
10514 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10515 "RUST$ENUM$DISR") != 0)
10516 {
10517 /* Not a Rust enum. */
10518 return;
10519 }
10520 else
10521 {
10522 /* Found one. */
10523 break;
10524 }
10525 }
10526
10527 /* If we got here without a discriminant, then it's probably
10528 just a union. */
10529 if (disr_type == nullptr)
10530 return;
10531
10532 /* Smash this type to be a structure type. We have to do this
10533 because the type has already been recorded. */
10534 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10535
10536 /* Make a union to hold the variants. */
10537 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10538 struct type *union_type = alloc_type (objfile);
10539 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10540 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10541 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10542 TYPE_FIELDS (union_type)
10543 = (struct field *) TYPE_ZALLOC (union_type,
10544 (TYPE_NFIELDS (union_type)
10545 * sizeof (struct field)));
10546
10547 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10548 TYPE_NFIELDS (type) * sizeof (struct field));
10549
10550 /* Install the discriminant at index 0 in the union. */
10551 TYPE_FIELD (union_type, 0) = *disr_field;
10552 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10553 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10554
10555 /* Install the union in the outer struct type. */
10556 TYPE_FIELD_TYPE (type, 0) = union_type;
10557 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10558 TYPE_NFIELDS (type) = 1;
10559
10560 /* Set the size and offset of the union type. */
10561 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10562
10563 /* We need a way to find the correct discriminant given a
10564 variant name. For convenience we build a map here. */
10565 struct type *enum_type = FIELD_TYPE (*disr_field);
10566 std::unordered_map<std::string, ULONGEST> discriminant_map;
10567 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10568 {
10569 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10570 {
10571 const char *name
10572 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10573 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10574 }
10575 }
10576
10577 int n_fields = TYPE_NFIELDS (union_type);
10578 struct discriminant_info *disc
10579 = alloc_discriminant_info (union_type, 0, -1);
10580 /* Skip the discriminant here. */
10581 for (int i = 1; i < n_fields; ++i)
10582 {
10583 /* Find the final word in the name of this variant's type.
10584 That name can be used to look up the correct
10585 discriminant. */
10586 const char *variant_name
10587 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10588 i)));
10589
10590 auto iter = discriminant_map.find (variant_name);
10591 if (iter != discriminant_map.end ())
10592 disc->discriminants[i] = iter->second;
10593
10594 /* Remove the discriminant field. */
10595 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10596 --TYPE_NFIELDS (sub_type);
10597 ++TYPE_FIELDS (sub_type);
10598 TYPE_FIELD_NAME (union_type, i) = variant_name;
10599 TYPE_NAME (sub_type)
10600 = rust_fully_qualify (&objfile->objfile_obstack,
10601 TYPE_NAME (type), variant_name);
10602 }
10603 }
10604}
10605
10606/* Rewrite some Rust unions to be structures with variants parts. */
10607
10608static void
10609rust_union_quirks (struct dwarf2_cu *cu)
10610{
10611 gdb_assert (cu->language == language_rust);
10612 for (struct type *type : cu->rust_unions)
10613 quirk_rust_enum (type, cu->per_cu->dwarf2_per_objfile->objfile);
10614}
10615
95554aad
TT
10616/* Return the symtab for PER_CU. This works properly regardless of
10617 whether we're using the index or psymtabs. */
10618
43f3e411
DE
10619static struct compunit_symtab *
10620get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 10621{
ed2dc618 10622 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
10623 ? per_cu->v.quick->compunit_symtab
10624 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10625}
10626
10627/* A helper function for computing the list of all symbol tables
10628 included by PER_CU. */
10629
10630static void
43f3e411 10631recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 10632 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10633 struct dwarf2_per_cu_data *per_cu,
43f3e411 10634 struct compunit_symtab *immediate_parent)
95554aad
TT
10635{
10636 void **slot;
10637 int ix;
43f3e411 10638 struct compunit_symtab *cust;
95554aad
TT
10639 struct dwarf2_per_cu_data *iter;
10640
10641 slot = htab_find_slot (all_children, per_cu, INSERT);
10642 if (*slot != NULL)
10643 {
10644 /* This inclusion and its children have been processed. */
10645 return;
10646 }
10647
10648 *slot = per_cu;
10649 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10650 cust = get_compunit_symtab (per_cu);
10651 if (cust != NULL)
ec94af83
DE
10652 {
10653 /* If this is a type unit only add its symbol table if we haven't
10654 seen it yet (type unit per_cu's can share symtabs). */
10655 if (per_cu->is_debug_types)
10656 {
43f3e411 10657 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10658 if (*slot == NULL)
10659 {
43f3e411
DE
10660 *slot = cust;
10661 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10662 if (cust->user == NULL)
10663 cust->user = immediate_parent;
ec94af83
DE
10664 }
10665 }
10666 else
f9125b6c 10667 {
43f3e411
DE
10668 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10669 if (cust->user == NULL)
10670 cust->user = immediate_parent;
f9125b6c 10671 }
ec94af83 10672 }
95554aad
TT
10673
10674 for (ix = 0;
796a7ff8 10675 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 10676 ++ix)
ec94af83
DE
10677 {
10678 recursively_compute_inclusions (result, all_children,
43f3e411 10679 all_type_symtabs, iter, cust);
ec94af83 10680 }
95554aad
TT
10681}
10682
43f3e411 10683/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10684 PER_CU. */
10685
10686static void
43f3e411 10687compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10688{
f4dc4d17
DE
10689 gdb_assert (! per_cu->is_debug_types);
10690
796a7ff8 10691 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
10692 {
10693 int ix, len;
ec94af83 10694 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
10695 struct compunit_symtab *compunit_symtab_iter;
10696 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 10697 htab_t all_children, all_type_symtabs;
43f3e411 10698 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10699
10700 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10701 if (cust == NULL)
95554aad
TT
10702 return;
10703
10704 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10705 NULL, xcalloc, xfree);
ec94af83
DE
10706 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10707 NULL, xcalloc, xfree);
95554aad
TT
10708
10709 for (ix = 0;
796a7ff8 10710 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 10711 ix, per_cu_iter);
95554aad 10712 ++ix)
ec94af83
DE
10713 {
10714 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 10715 all_type_symtabs, per_cu_iter,
43f3e411 10716 cust);
ec94af83 10717 }
95554aad 10718
ec94af83 10719 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
10720 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10721 cust->includes
ed2dc618 10722 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 10723 struct compunit_symtab *, len + 1);
95554aad 10724 for (ix = 0;
43f3e411
DE
10725 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10726 compunit_symtab_iter);
95554aad 10727 ++ix)
43f3e411
DE
10728 cust->includes[ix] = compunit_symtab_iter;
10729 cust->includes[len] = NULL;
95554aad 10730
43f3e411 10731 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 10732 htab_delete (all_children);
ec94af83 10733 htab_delete (all_type_symtabs);
95554aad
TT
10734 }
10735}
10736
10737/* Compute the 'includes' field for the symtabs of all the CUs we just
10738 read. */
10739
10740static void
ed2dc618 10741process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad
TT
10742{
10743 int ix;
10744 struct dwarf2_per_cu_data *iter;
10745
10746 for (ix = 0;
10747 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10748 ix, iter);
10749 ++ix)
f4dc4d17
DE
10750 {
10751 if (! iter->is_debug_types)
43f3e411 10752 compute_compunit_symtab_includes (iter);
f4dc4d17 10753 }
95554aad
TT
10754
10755 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10756}
10757
9cdd5dbd 10758/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10759 already been loaded into memory. */
10760
10761static void
95554aad
TT
10762process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10763 enum language pretend_language)
10b3939b 10764{
10b3939b 10765 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10766 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10767 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10768 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10769 CORE_ADDR lowpc, highpc;
43f3e411 10770 struct compunit_symtab *cust;
10b3939b 10771 CORE_ADDR baseaddr;
4359dff1 10772 struct block *static_block;
3e29f34a 10773 CORE_ADDR addr;
10b3939b
DJ
10774
10775 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10776
10b3939b 10777 buildsym_init ();
33c7c59d 10778 scoped_free_pendings free_pending;
c89b44cd
TT
10779
10780 /* Clear the list here in case something was left over. */
10781 cu->method_list.clear ();
10b3939b
DJ
10782
10783 cu->list_in_scope = &file_symbols;
c906108c 10784
95554aad
TT
10785 cu->language = pretend_language;
10786 cu->language_defn = language_def (cu->language);
10787
c906108c 10788 /* Do line number decoding in read_file_scope () */
10b3939b 10789 process_die (cu->dies, cu);
c906108c 10790
a766d390
DE
10791 /* For now fudge the Go package. */
10792 if (cu->language == language_go)
10793 fixup_go_packaging (cu);
10794
3da10d80
KS
10795 /* Now that we have processed all the DIEs in the CU, all the types
10796 should be complete, and it should now be safe to compute all of the
10797 physnames. */
10798 compute_delayed_physnames (cu);
3da10d80 10799
c9317f21
TT
10800 if (cu->language == language_rust)
10801 rust_union_quirks (cu);
10802
fae299cd
DC
10803 /* Some compilers don't define a DW_AT_high_pc attribute for the
10804 compilation unit. If the DW_AT_high_pc is missing, synthesize
10805 it, by scanning the DIE's below the compilation unit. */
10b3939b 10806 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10807
3e29f34a
MR
10808 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10809 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10810
10811 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10812 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10813 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10814 addrmap to help ensure it has an accurate map of pc values belonging to
10815 this comp unit. */
10816 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10817
43f3e411
DE
10818 cust = end_symtab_from_static_block (static_block,
10819 SECT_OFF_TEXT (objfile), 0);
c906108c 10820
43f3e411 10821 if (cust != NULL)
c906108c 10822 {
df15bd07 10823 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10824
8be455d7
JK
10825 /* Set symtab language to language from DW_AT_language. If the
10826 compilation is from a C file generated by language preprocessors, do
10827 not set the language if it was already deduced by start_subfile. */
43f3e411 10828 if (!(cu->language == language_c
40e3ad0e 10829 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10830 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10831
10832 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10833 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10834 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10835 there were bugs in prologue debug info, fixed later in GCC-4.5
10836 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10837
10838 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10839 needed, it would be wrong due to missing DW_AT_producer there.
10840
10841 Still one can confuse GDB by using non-standard GCC compilation
10842 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10843 */
ab260dad 10844 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10845 cust->locations_valid = 1;
e0d00bc7
JK
10846
10847 if (gcc_4_minor >= 5)
43f3e411 10848 cust->epilogue_unwind_valid = 1;
96408a79 10849
43f3e411 10850 cust->call_site_htab = cu->call_site_htab;
c906108c 10851 }
9291a0cd
TT
10852
10853 if (dwarf2_per_objfile->using_index)
43f3e411 10854 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10855 else
10856 {
10857 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10858 pst->compunit_symtab = cust;
9291a0cd
TT
10859 pst->readin = 1;
10860 }
c906108c 10861
95554aad
TT
10862 /* Push it for inclusion processing later. */
10863 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
f4dc4d17 10864}
45cfd468 10865
f4dc4d17
DE
10866/* Generate full symbol information for type unit PER_CU, whose DIEs have
10867 already been loaded into memory. */
10868
10869static void
10870process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10871 enum language pretend_language)
10872{
10873 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10874 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10875 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10876 struct compunit_symtab *cust;
0186c6a7
DE
10877 struct signatured_type *sig_type;
10878
10879 gdb_assert (per_cu->is_debug_types);
10880 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
10881
10882 buildsym_init ();
33c7c59d 10883 scoped_free_pendings free_pending;
c89b44cd
TT
10884
10885 /* Clear the list here in case something was left over. */
10886 cu->method_list.clear ();
f4dc4d17
DE
10887
10888 cu->list_in_scope = &file_symbols;
10889
10890 cu->language = pretend_language;
10891 cu->language_defn = language_def (cu->language);
10892
10893 /* The symbol tables are set up in read_type_unit_scope. */
10894 process_die (cu->dies, cu);
10895
10896 /* For now fudge the Go package. */
10897 if (cu->language == language_go)
10898 fixup_go_packaging (cu);
10899
10900 /* Now that we have processed all the DIEs in the CU, all the types
10901 should be complete, and it should now be safe to compute all of the
10902 physnames. */
10903 compute_delayed_physnames (cu);
f4dc4d17 10904
c9317f21
TT
10905 if (cu->language == language_rust)
10906 rust_union_quirks (cu);
10907
f4dc4d17
DE
10908 /* TUs share symbol tables.
10909 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10910 of it with end_expandable_symtab. Otherwise, complete the addition of
10911 this TU's symbols to the existing symtab. */
43f3e411 10912 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10913 {
43f3e411
DE
10914 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10915 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10916
43f3e411 10917 if (cust != NULL)
f4dc4d17
DE
10918 {
10919 /* Set symtab language to language from DW_AT_language. If the
10920 compilation is from a C file generated by language preprocessors,
10921 do not set the language if it was already deduced by
10922 start_subfile. */
43f3e411
DE
10923 if (!(cu->language == language_c
10924 && COMPUNIT_FILETABS (cust)->language != language_c))
10925 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10926 }
10927 }
10928 else
10929 {
0ab9ce85 10930 augment_type_symtab ();
43f3e411 10931 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10932 }
10933
10934 if (dwarf2_per_objfile->using_index)
43f3e411 10935 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10936 else
10937 {
10938 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10939 pst->compunit_symtab = cust;
f4dc4d17 10940 pst->readin = 1;
45cfd468 10941 }
c906108c
SS
10942}
10943
95554aad
TT
10944/* Process an imported unit DIE. */
10945
10946static void
10947process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10948{
10949 struct attribute *attr;
10950
f4dc4d17
DE
10951 /* For now we don't handle imported units in type units. */
10952 if (cu->per_cu->is_debug_types)
10953 {
10954 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10955 " supported in type units [in module %s]"),
518817b3 10956 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10957 }
10958
95554aad
TT
10959 attr = dwarf2_attr (die, DW_AT_import, cu);
10960 if (attr != NULL)
10961 {
9c541725
PA
10962 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10963 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10964 dwarf2_per_cu_data *per_cu
e3b94546 10965 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10966 cu->per_cu->dwarf2_per_objfile);
95554aad 10967
69d751e3 10968 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
10969 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10970 load_full_comp_unit (per_cu, cu->language);
10971
796a7ff8 10972 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
10973 per_cu);
10974 }
10975}
10976
4c8aa72d
PA
10977/* RAII object that represents a process_die scope: i.e.,
10978 starts/finishes processing a DIE. */
10979class process_die_scope
adde2bff 10980{
4c8aa72d
PA
10981public:
10982 process_die_scope (die_info *die, dwarf2_cu *cu)
10983 : m_die (die), m_cu (cu)
10984 {
10985 /* We should only be processing DIEs not already in process. */
10986 gdb_assert (!m_die->in_process);
10987 m_die->in_process = true;
10988 }
8c3cb9fa 10989
4c8aa72d
PA
10990 ~process_die_scope ()
10991 {
10992 m_die->in_process = false;
10993
10994 /* If we're done processing the DIE for the CU that owns the line
10995 header, we don't need the line header anymore. */
10996 if (m_cu->line_header_die_owner == m_die)
10997 {
10998 delete m_cu->line_header;
10999 m_cu->line_header = NULL;
11000 m_cu->line_header_die_owner = NULL;
11001 }
11002 }
11003
11004private:
11005 die_info *m_die;
11006 dwarf2_cu *m_cu;
11007};
adde2bff 11008
c906108c
SS
11009/* Process a die and its children. */
11010
11011static void
e7c27a73 11012process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11013{
4c8aa72d 11014 process_die_scope scope (die, cu);
adde2bff 11015
c906108c
SS
11016 switch (die->tag)
11017 {
11018 case DW_TAG_padding:
11019 break;
11020 case DW_TAG_compile_unit:
95554aad 11021 case DW_TAG_partial_unit:
e7c27a73 11022 read_file_scope (die, cu);
c906108c 11023 break;
348e048f
DE
11024 case DW_TAG_type_unit:
11025 read_type_unit_scope (die, cu);
11026 break;
c906108c 11027 case DW_TAG_subprogram:
c906108c 11028 case DW_TAG_inlined_subroutine:
edb3359d 11029 read_func_scope (die, cu);
c906108c
SS
11030 break;
11031 case DW_TAG_lexical_block:
14898363
L
11032 case DW_TAG_try_block:
11033 case DW_TAG_catch_block:
e7c27a73 11034 read_lexical_block_scope (die, cu);
c906108c 11035 break;
216f72a1 11036 case DW_TAG_call_site:
96408a79
SA
11037 case DW_TAG_GNU_call_site:
11038 read_call_site_scope (die, cu);
11039 break;
c906108c 11040 case DW_TAG_class_type:
680b30c7 11041 case DW_TAG_interface_type:
c906108c
SS
11042 case DW_TAG_structure_type:
11043 case DW_TAG_union_type:
134d01f1 11044 process_structure_scope (die, cu);
c906108c
SS
11045 break;
11046 case DW_TAG_enumeration_type:
134d01f1 11047 process_enumeration_scope (die, cu);
c906108c 11048 break;
134d01f1 11049
f792889a
DJ
11050 /* These dies have a type, but processing them does not create
11051 a symbol or recurse to process the children. Therefore we can
11052 read them on-demand through read_type_die. */
c906108c 11053 case DW_TAG_subroutine_type:
72019c9c 11054 case DW_TAG_set_type:
c906108c 11055 case DW_TAG_array_type:
c906108c 11056 case DW_TAG_pointer_type:
c906108c 11057 case DW_TAG_ptr_to_member_type:
c906108c 11058 case DW_TAG_reference_type:
4297a3f0 11059 case DW_TAG_rvalue_reference_type:
c906108c 11060 case DW_TAG_string_type:
c906108c 11061 break;
134d01f1 11062
c906108c 11063 case DW_TAG_base_type:
a02abb62 11064 case DW_TAG_subrange_type:
cb249c71 11065 case DW_TAG_typedef:
134d01f1
DJ
11066 /* Add a typedef symbol for the type definition, if it has a
11067 DW_AT_name. */
f792889a 11068 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 11069 break;
c906108c 11070 case DW_TAG_common_block:
e7c27a73 11071 read_common_block (die, cu);
c906108c
SS
11072 break;
11073 case DW_TAG_common_inclusion:
11074 break;
d9fa45fe 11075 case DW_TAG_namespace:
4d4ec4e5 11076 cu->processing_has_namespace_info = 1;
e7c27a73 11077 read_namespace (die, cu);
d9fa45fe 11078 break;
5d7cb8df 11079 case DW_TAG_module:
4d4ec4e5 11080 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
11081 read_module (die, cu);
11082 break;
d9fa45fe 11083 case DW_TAG_imported_declaration:
74921315
KS
11084 cu->processing_has_namespace_info = 1;
11085 if (read_namespace_alias (die, cu))
11086 break;
11087 /* The declaration is not a global namespace alias: fall through. */
d9fa45fe 11088 case DW_TAG_imported_module:
4d4ec4e5 11089 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
11090 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
11091 || cu->language != language_fortran))
11092 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
11093 dwarf_tag_name (die->tag));
11094 read_import_statement (die, cu);
d9fa45fe 11095 break;
95554aad
TT
11096
11097 case DW_TAG_imported_unit:
11098 process_imported_unit_die (die, cu);
11099 break;
11100
71a3c369
TT
11101 case DW_TAG_variable:
11102 read_variable (die, cu);
11103 break;
11104
c906108c 11105 default:
e7c27a73 11106 new_symbol (die, NULL, cu);
c906108c
SS
11107 break;
11108 }
11109}
ca69b9e6
DE
11110\f
11111/* DWARF name computation. */
c906108c 11112
94af9270
KS
11113/* A helper function for dwarf2_compute_name which determines whether DIE
11114 needs to have the name of the scope prepended to the name listed in the
11115 die. */
11116
11117static int
11118die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
11119{
1c809c68
TT
11120 struct attribute *attr;
11121
94af9270
KS
11122 switch (die->tag)
11123 {
11124 case DW_TAG_namespace:
11125 case DW_TAG_typedef:
11126 case DW_TAG_class_type:
11127 case DW_TAG_interface_type:
11128 case DW_TAG_structure_type:
11129 case DW_TAG_union_type:
11130 case DW_TAG_enumeration_type:
11131 case DW_TAG_enumerator:
11132 case DW_TAG_subprogram:
08a76f8a 11133 case DW_TAG_inlined_subroutine:
94af9270 11134 case DW_TAG_member:
74921315 11135 case DW_TAG_imported_declaration:
94af9270
KS
11136 return 1;
11137
11138 case DW_TAG_variable:
c2b0a229 11139 case DW_TAG_constant:
94af9270
KS
11140 /* We only need to prefix "globally" visible variables. These include
11141 any variable marked with DW_AT_external or any variable that
11142 lives in a namespace. [Variables in anonymous namespaces
11143 require prefixing, but they are not DW_AT_external.] */
11144
11145 if (dwarf2_attr (die, DW_AT_specification, cu))
11146 {
11147 struct dwarf2_cu *spec_cu = cu;
9a619af0 11148
94af9270
KS
11149 return die_needs_namespace (die_specification (die, &spec_cu),
11150 spec_cu);
11151 }
11152
1c809c68 11153 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
11154 if (attr == NULL && die->parent->tag != DW_TAG_namespace
11155 && die->parent->tag != DW_TAG_module)
1c809c68
TT
11156 return 0;
11157 /* A variable in a lexical block of some kind does not need a
11158 namespace, even though in C++ such variables may be external
11159 and have a mangled name. */
11160 if (die->parent->tag == DW_TAG_lexical_block
11161 || die->parent->tag == DW_TAG_try_block
1054b214
TT
11162 || die->parent->tag == DW_TAG_catch_block
11163 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
11164 return 0;
11165 return 1;
94af9270
KS
11166
11167 default:
11168 return 0;
11169 }
11170}
11171
73b9be8b
KS
11172/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
11173 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
11174 defined for the given DIE. */
11175
11176static struct attribute *
11177dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
11178{
11179 struct attribute *attr;
11180
11181 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
11182 if (attr == NULL)
11183 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
11184
11185 return attr;
11186}
11187
11188/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
11189 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
11190 defined for the given DIE. */
11191
11192static const char *
11193dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
11194{
11195 const char *linkage_name;
11196
11197 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
11198 if (linkage_name == NULL)
11199 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
11200
11201 return linkage_name;
11202}
11203
94af9270 11204/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 11205 compute the physname for the object, which include a method's:
9c37b5ae 11206 - formal parameters (C++),
a766d390 11207 - receiver type (Go),
a766d390
DE
11208
11209 The term "physname" is a bit confusing.
11210 For C++, for example, it is the demangled name.
11211 For Go, for example, it's the mangled name.
94af9270 11212
af6b7be1
JB
11213 For Ada, return the DIE's linkage name rather than the fully qualified
11214 name. PHYSNAME is ignored..
11215
94af9270
KS
11216 The result is allocated on the objfile_obstack and canonicalized. */
11217
11218static const char *
15d034d0
TT
11219dwarf2_compute_name (const char *name,
11220 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
11221 int physname)
11222{
518817b3 11223 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 11224
94af9270
KS
11225 if (name == NULL)
11226 name = dwarf2_name (die, cu);
11227
2ee7123e
DE
11228 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
11229 but otherwise compute it by typename_concat inside GDB.
11230 FIXME: Actually this is not really true, or at least not always true.
11231 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
5e2db402 11232 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
11233 will set the demangled name to the result of dwarf2_full_name, and it is
11234 the demangled name that GDB uses if it exists. */
f55ee35c
JK
11235 if (cu->language == language_ada
11236 || (cu->language == language_fortran && physname))
11237 {
11238 /* For Ada unit, we prefer the linkage name over the name, as
11239 the former contains the exported name, which the user expects
11240 to be able to reference. Ideally, we want the user to be able
11241 to reference this entity using either natural or linkage name,
11242 but we haven't started looking at this enhancement yet. */
73b9be8b 11243 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 11244
2ee7123e
DE
11245 if (linkage_name != NULL)
11246 return linkage_name;
f55ee35c
JK
11247 }
11248
94af9270
KS
11249 /* These are the only languages we know how to qualify names in. */
11250 if (name != NULL
9c37b5ae 11251 && (cu->language == language_cplus
c44af4eb
TT
11252 || cu->language == language_fortran || cu->language == language_d
11253 || cu->language == language_rust))
94af9270
KS
11254 {
11255 if (die_needs_namespace (die, cu))
11256 {
0d5cff50 11257 const char *prefix;
34a68019 11258 const char *canonical_name = NULL;
94af9270 11259
d7e74731
PA
11260 string_file buf;
11261
94af9270 11262 prefix = determine_prefix (die, cu);
94af9270
KS
11263 if (*prefix != '\0')
11264 {
f55ee35c
JK
11265 char *prefixed_name = typename_concat (NULL, prefix, name,
11266 physname, cu);
9a619af0 11267
d7e74731 11268 buf.puts (prefixed_name);
94af9270
KS
11269 xfree (prefixed_name);
11270 }
11271 else
d7e74731 11272 buf.puts (name);
94af9270 11273
98bfdba5
PA
11274 /* Template parameters may be specified in the DIE's DW_AT_name, or
11275 as children with DW_TAG_template_type_param or
11276 DW_TAG_value_type_param. If the latter, add them to the name
11277 here. If the name already has template parameters, then
11278 skip this step; some versions of GCC emit both, and
11279 it is more efficient to use the pre-computed name.
11280
11281 Something to keep in mind about this process: it is very
11282 unlikely, or in some cases downright impossible, to produce
11283 something that will match the mangled name of a function.
11284 If the definition of the function has the same debug info,
11285 we should be able to match up with it anyway. But fallbacks
11286 using the minimal symbol, for instance to find a method
11287 implemented in a stripped copy of libstdc++, will not work.
11288 If we do not have debug info for the definition, we will have to
11289 match them up some other way.
11290
11291 When we do name matching there is a related problem with function
11292 templates; two instantiated function templates are allowed to
11293 differ only by their return types, which we do not add here. */
11294
11295 if (cu->language == language_cplus && strchr (name, '<') == NULL)
11296 {
11297 struct attribute *attr;
11298 struct die_info *child;
11299 int first = 1;
11300
11301 die->building_fullname = 1;
11302
11303 for (child = die->child; child != NULL; child = child->sibling)
11304 {
11305 struct type *type;
12df843f 11306 LONGEST value;
d521ce57 11307 const gdb_byte *bytes;
98bfdba5
PA
11308 struct dwarf2_locexpr_baton *baton;
11309 struct value *v;
11310
11311 if (child->tag != DW_TAG_template_type_param
11312 && child->tag != DW_TAG_template_value_param)
11313 continue;
11314
11315 if (first)
11316 {
d7e74731 11317 buf.puts ("<");
98bfdba5
PA
11318 first = 0;
11319 }
11320 else
d7e74731 11321 buf.puts (", ");
98bfdba5
PA
11322
11323 attr = dwarf2_attr (child, DW_AT_type, cu);
11324 if (attr == NULL)
11325 {
11326 complaint (&symfile_complaints,
11327 _("template parameter missing DW_AT_type"));
d7e74731 11328 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
11329 continue;
11330 }
11331 type = die_type (child, cu);
11332
11333 if (child->tag == DW_TAG_template_type_param)
11334 {
d7e74731 11335 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
11336 continue;
11337 }
11338
11339 attr = dwarf2_attr (child, DW_AT_const_value, cu);
11340 if (attr == NULL)
11341 {
11342 complaint (&symfile_complaints,
3e43a32a
MS
11343 _("template parameter missing "
11344 "DW_AT_const_value"));
d7e74731 11345 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
11346 continue;
11347 }
11348
11349 dwarf2_const_value_attr (attr, type, name,
11350 &cu->comp_unit_obstack, cu,
11351 &value, &bytes, &baton);
11352
11353 if (TYPE_NOSIGN (type))
11354 /* GDB prints characters as NUMBER 'CHAR'. If that's
11355 changed, this can use value_print instead. */
d7e74731 11356 c_printchar (value, type, &buf);
98bfdba5
PA
11357 else
11358 {
11359 struct value_print_options opts;
11360
11361 if (baton != NULL)
11362 v = dwarf2_evaluate_loc_desc (type, NULL,
11363 baton->data,
11364 baton->size,
11365 baton->per_cu);
11366 else if (bytes != NULL)
11367 {
11368 v = allocate_value (type);
11369 memcpy (value_contents_writeable (v), bytes,
11370 TYPE_LENGTH (type));
11371 }
11372 else
11373 v = value_from_longest (type, value);
11374
3e43a32a
MS
11375 /* Specify decimal so that we do not depend on
11376 the radix. */
98bfdba5
PA
11377 get_formatted_print_options (&opts, 'd');
11378 opts.raw = 1;
d7e74731 11379 value_print (v, &buf, &opts);
98bfdba5
PA
11380 release_value (v);
11381 value_free (v);
11382 }
11383 }
11384
11385 die->building_fullname = 0;
11386
11387 if (!first)
11388 {
11389 /* Close the argument list, with a space if necessary
11390 (nested templates). */
d7e74731
PA
11391 if (!buf.empty () && buf.string ().back () == '>')
11392 buf.puts (" >");
98bfdba5 11393 else
d7e74731 11394 buf.puts (">");
98bfdba5
PA
11395 }
11396 }
11397
9c37b5ae 11398 /* For C++ methods, append formal parameter type
94af9270 11399 information, if PHYSNAME. */
6e70227d 11400
94af9270 11401 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 11402 && cu->language == language_cplus)
94af9270
KS
11403 {
11404 struct type *type = read_type_die (die, cu);
11405
d7e74731 11406 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 11407 &type_print_raw_options);
94af9270 11408
9c37b5ae 11409 if (cu->language == language_cplus)
94af9270 11410 {
60430eff
DJ
11411 /* Assume that an artificial first parameter is
11412 "this", but do not crash if it is not. RealView
11413 marks unnamed (and thus unused) parameters as
11414 artificial; there is no way to differentiate
11415 the two cases. */
94af9270
KS
11416 if (TYPE_NFIELDS (type) > 0
11417 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 11418 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
11419 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11420 0))))
d7e74731 11421 buf.puts (" const");
94af9270
KS
11422 }
11423 }
11424
d7e74731 11425 const std::string &intermediate_name = buf.string ();
94af9270
KS
11426
11427 if (cu->language == language_cplus)
34a68019 11428 canonical_name
322a8516 11429 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
11430 &objfile->per_bfd->storage_obstack);
11431
11432 /* If we only computed INTERMEDIATE_NAME, or if
11433 INTERMEDIATE_NAME is already canonical, then we need to
11434 copy it to the appropriate obstack. */
322a8516 11435 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
11436 name = ((const char *)
11437 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
11438 intermediate_name.c_str (),
11439 intermediate_name.length ()));
34a68019
TT
11440 else
11441 name = canonical_name;
94af9270
KS
11442 }
11443 }
11444
11445 return name;
11446}
11447
0114d602
DJ
11448/* Return the fully qualified name of DIE, based on its DW_AT_name.
11449 If scope qualifiers are appropriate they will be added. The result
34a68019 11450 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
11451 not have a name. NAME may either be from a previous call to
11452 dwarf2_name or NULL.
11453
9c37b5ae 11454 The output string will be canonicalized (if C++). */
0114d602
DJ
11455
11456static const char *
15d034d0 11457dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 11458{
94af9270
KS
11459 return dwarf2_compute_name (name, die, cu, 0);
11460}
0114d602 11461
94af9270
KS
11462/* Construct a physname for the given DIE in CU. NAME may either be
11463 from a previous call to dwarf2_name or NULL. The result will be
11464 allocated on the objfile_objstack or NULL if the DIE does not have a
11465 name.
0114d602 11466
9c37b5ae 11467 The output string will be canonicalized (if C++). */
0114d602 11468
94af9270 11469static const char *
15d034d0 11470dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 11471{
518817b3 11472 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 11473 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
11474 int need_copy = 1;
11475
11476 /* In this case dwarf2_compute_name is just a shortcut not building anything
11477 on its own. */
11478 if (!die_needs_namespace (die, cu))
11479 return dwarf2_compute_name (name, die, cu, 1);
11480
73b9be8b 11481 mangled = dw2_linkage_name (die, cu);
900e11f9 11482
e98c9e7c
TT
11483 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11484 See https://github.com/rust-lang/rust/issues/32925. */
11485 if (cu->language == language_rust && mangled != NULL
11486 && strchr (mangled, '{') != NULL)
11487 mangled = NULL;
11488
900e11f9
JK
11489 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11490 has computed. */
791afaa2 11491 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 11492 if (mangled != NULL)
900e11f9 11493 {
900e11f9 11494
a766d390
DE
11495 if (cu->language == language_go)
11496 {
5e2db402
TT
11497 /* This is a lie, but we already lie to the caller new_symbol.
11498 new_symbol assumes we return the mangled name.
a766d390 11499 This just undoes that lie until things are cleaned up. */
a766d390
DE
11500 }
11501 else
11502 {
0eb876f5
JB
11503 /* Use DMGL_RET_DROP for C++ template functions to suppress
11504 their return type. It is easier for GDB users to search
11505 for such functions as `name(params)' than `long name(params)'.
11506 In such case the minimal symbol names do not match the full
11507 symbol names but for template functions there is never a need
11508 to look up their definition from their declaration so
11509 the only disadvantage remains the minimal symbol variant
11510 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
11511 demangled.reset (gdb_demangle (mangled,
11512 (DMGL_PARAMS | DMGL_ANSI
11513 | DMGL_RET_DROP)));
a766d390 11514 }
900e11f9 11515 if (demangled)
791afaa2 11516 canon = demangled.get ();
900e11f9
JK
11517 else
11518 {
11519 canon = mangled;
11520 need_copy = 0;
11521 }
11522 }
11523
11524 if (canon == NULL || check_physname)
11525 {
11526 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11527
11528 if (canon != NULL && strcmp (physname, canon) != 0)
11529 {
11530 /* It may not mean a bug in GDB. The compiler could also
11531 compute DW_AT_linkage_name incorrectly. But in such case
11532 GDB would need to be bug-to-bug compatible. */
11533
11534 complaint (&symfile_complaints,
11535 _("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
11536 "(from linkage <%s>) - DIE at %s [in module %s]"),
11537 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 11538 objfile_name (objfile));
900e11f9
JK
11539
11540 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11541 is available here - over computed PHYSNAME. It is safer
11542 against both buggy GDB and buggy compilers. */
11543
11544 retval = canon;
11545 }
11546 else
11547 {
11548 retval = physname;
11549 need_copy = 0;
11550 }
11551 }
11552 else
11553 retval = canon;
11554
11555 if (need_copy)
224c3ddb
SM
11556 retval = ((const char *)
11557 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11558 retval, strlen (retval)));
900e11f9 11559
900e11f9 11560 return retval;
0114d602
DJ
11561}
11562
74921315
KS
11563/* Inspect DIE in CU for a namespace alias. If one exists, record
11564 a new symbol for it.
11565
11566 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11567
11568static int
11569read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11570{
11571 struct attribute *attr;
11572
11573 /* If the die does not have a name, this is not a namespace
11574 alias. */
11575 attr = dwarf2_attr (die, DW_AT_name, cu);
11576 if (attr != NULL)
11577 {
11578 int num;
11579 struct die_info *d = die;
11580 struct dwarf2_cu *imported_cu = cu;
11581
11582 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11583 keep inspecting DIEs until we hit the underlying import. */
11584#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11585 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11586 {
11587 attr = dwarf2_attr (d, DW_AT_import, cu);
11588 if (attr == NULL)
11589 break;
11590
11591 d = follow_die_ref (d, attr, &imported_cu);
11592 if (d->tag != DW_TAG_imported_declaration)
11593 break;
11594 }
11595
11596 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11597 {
11598 complaint (&symfile_complaints,
9d8780f0
SM
11599 _("DIE at %s has too many recursively imported "
11600 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
11601 return 0;
11602 }
11603
11604 if (attr != NULL)
11605 {
11606 struct type *type;
9c541725 11607 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11608
9c541725 11609 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11610 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11611 {
11612 /* This declaration is a global namespace alias. Add
11613 a symbol for it whose type is the aliased namespace. */
11614 new_symbol (die, type, cu);
11615 return 1;
11616 }
11617 }
11618 }
11619
11620 return 0;
11621}
11622
22cee43f
PMR
11623/* Return the using directives repository (global or local?) to use in the
11624 current context for LANGUAGE.
11625
11626 For Ada, imported declarations can materialize renamings, which *may* be
11627 global. However it is impossible (for now?) in DWARF to distinguish
11628 "external" imported declarations and "static" ones. As all imported
11629 declarations seem to be static in all other languages, make them all CU-wide
11630 global only in Ada. */
11631
11632static struct using_direct **
11633using_directives (enum language language)
11634{
11635 if (language == language_ada && context_stack_depth == 0)
11636 return &global_using_directives;
11637 else
11638 return &local_using_directives;
11639}
11640
27aa8d6a
SW
11641/* Read the import statement specified by the given die and record it. */
11642
11643static void
11644read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11645{
518817b3 11646 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 11647 struct attribute *import_attr;
32019081 11648 struct die_info *imported_die, *child_die;
de4affc9 11649 struct dwarf2_cu *imported_cu;
27aa8d6a 11650 const char *imported_name;
794684b6 11651 const char *imported_name_prefix;
13387711
SW
11652 const char *canonical_name;
11653 const char *import_alias;
11654 const char *imported_declaration = NULL;
794684b6 11655 const char *import_prefix;
eb1e02fd 11656 std::vector<const char *> excludes;
13387711 11657
27aa8d6a
SW
11658 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11659 if (import_attr == NULL)
11660 {
11661 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11662 dwarf_tag_name (die->tag));
11663 return;
11664 }
11665
de4affc9
CC
11666 imported_cu = cu;
11667 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11668 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11669 if (imported_name == NULL)
11670 {
11671 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11672
11673 The import in the following code:
11674 namespace A
11675 {
11676 typedef int B;
11677 }
11678
11679 int main ()
11680 {
11681 using A::B;
11682 B b;
11683 return b;
11684 }
11685
11686 ...
11687 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11688 <52> DW_AT_decl_file : 1
11689 <53> DW_AT_decl_line : 6
11690 <54> DW_AT_import : <0x75>
11691 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11692 <59> DW_AT_name : B
11693 <5b> DW_AT_decl_file : 1
11694 <5c> DW_AT_decl_line : 2
11695 <5d> DW_AT_type : <0x6e>
11696 ...
11697 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11698 <76> DW_AT_byte_size : 4
11699 <77> DW_AT_encoding : 5 (signed)
11700
11701 imports the wrong die ( 0x75 instead of 0x58 ).
11702 This case will be ignored until the gcc bug is fixed. */
11703 return;
11704 }
11705
82856980
SW
11706 /* Figure out the local name after import. */
11707 import_alias = dwarf2_name (die, cu);
27aa8d6a 11708
794684b6
SW
11709 /* Figure out where the statement is being imported to. */
11710 import_prefix = determine_prefix (die, cu);
11711
11712 /* Figure out what the scope of the imported die is and prepend it
11713 to the name of the imported die. */
de4affc9 11714 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11715
f55ee35c
JK
11716 if (imported_die->tag != DW_TAG_namespace
11717 && imported_die->tag != DW_TAG_module)
794684b6 11718 {
13387711
SW
11719 imported_declaration = imported_name;
11720 canonical_name = imported_name_prefix;
794684b6 11721 }
13387711 11722 else if (strlen (imported_name_prefix) > 0)
12aaed36 11723 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11724 imported_name_prefix,
11725 (cu->language == language_d ? "." : "::"),
11726 imported_name, (char *) NULL);
13387711
SW
11727 else
11728 canonical_name = imported_name;
794684b6 11729
32019081
JK
11730 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11731 for (child_die = die->child; child_die && child_die->tag;
11732 child_die = sibling_die (child_die))
11733 {
11734 /* DWARF-4: A Fortran use statement with a “rename list” may be
11735 represented by an imported module entry with an import attribute
11736 referring to the module and owned entries corresponding to those
11737 entities that are renamed as part of being imported. */
11738
11739 if (child_die->tag != DW_TAG_imported_declaration)
11740 {
11741 complaint (&symfile_complaints,
11742 _("child DW_TAG_imported_declaration expected "
9d8780f0
SM
11743 "- DIE at %s [in module %s]"),
11744 sect_offset_str (child_die->sect_off),
11745 objfile_name (objfile));
32019081
JK
11746 continue;
11747 }
11748
11749 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11750 if (import_attr == NULL)
11751 {
11752 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11753 dwarf_tag_name (child_die->tag));
11754 continue;
11755 }
11756
11757 imported_cu = cu;
11758 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11759 &imported_cu);
11760 imported_name = dwarf2_name (imported_die, imported_cu);
11761 if (imported_name == NULL)
11762 {
11763 complaint (&symfile_complaints,
11764 _("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11765 "imported name - DIE at %s [in module %s]"),
11766 sect_offset_str (child_die->sect_off),
11767 objfile_name (objfile));
32019081
JK
11768 continue;
11769 }
11770
eb1e02fd 11771 excludes.push_back (imported_name);
32019081
JK
11772
11773 process_die (child_die, cu);
11774 }
11775
22cee43f
PMR
11776 add_using_directive (using_directives (cu->language),
11777 import_prefix,
11778 canonical_name,
11779 import_alias,
11780 imported_declaration,
11781 excludes,
11782 0,
11783 &objfile->objfile_obstack);
27aa8d6a
SW
11784}
11785
5230b05a
WT
11786/* ICC<14 does not output the required DW_AT_declaration on incomplete
11787 types, but gives them a size of zero. Starting with version 14,
11788 ICC is compatible with GCC. */
11789
11790static int
11791producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11792{
11793 if (!cu->checked_producer)
11794 check_producer (cu);
11795
11796 return cu->producer_is_icc_lt_14;
11797}
11798
1b80a9fa
JK
11799/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11800 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11801 this, it was first present in GCC release 4.3.0. */
11802
11803static int
11804producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11805{
11806 if (!cu->checked_producer)
11807 check_producer (cu);
11808
11809 return cu->producer_is_gcc_lt_4_3;
11810}
11811
d721ba37
PA
11812static file_and_directory
11813find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11814{
d721ba37
PA
11815 file_and_directory res;
11816
9291a0cd
TT
11817 /* Find the filename. Do not use dwarf2_name here, since the filename
11818 is not a source language identifier. */
d721ba37
PA
11819 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11820 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11821
d721ba37
PA
11822 if (res.comp_dir == NULL
11823 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11824 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11825 {
d721ba37
PA
11826 res.comp_dir_storage = ldirname (res.name);
11827 if (!res.comp_dir_storage.empty ())
11828 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11829 }
d721ba37 11830 if (res.comp_dir != NULL)
9291a0cd
TT
11831 {
11832 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11833 directory, get rid of it. */
d721ba37 11834 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11835
d721ba37
PA
11836 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11837 res.comp_dir = cp + 1;
9291a0cd
TT
11838 }
11839
d721ba37
PA
11840 if (res.name == NULL)
11841 res.name = "<unknown>";
11842
11843 return res;
9291a0cd
TT
11844}
11845
f4dc4d17
DE
11846/* Handle DW_AT_stmt_list for a compilation unit.
11847 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11848 COMP_DIR is the compilation directory. LOWPC is passed to
11849 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11850
11851static void
11852handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11853 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11854{
518817b3
SM
11855 struct dwarf2_per_objfile *dwarf2_per_objfile
11856 = cu->per_cu->dwarf2_per_objfile;
527f3840 11857 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11858 struct attribute *attr;
527f3840
JK
11859 struct line_header line_header_local;
11860 hashval_t line_header_local_hash;
527f3840
JK
11861 void **slot;
11862 int decode_mapping;
2ab95328 11863
f4dc4d17
DE
11864 gdb_assert (! cu->per_cu->is_debug_types);
11865
2ab95328 11866 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11867 if (attr == NULL)
11868 return;
11869
9c541725 11870 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11871
11872 /* The line header hash table is only created if needed (it exists to
11873 prevent redundant reading of the line table for partial_units).
11874 If we're given a partial_unit, we'll need it. If we're given a
11875 compile_unit, then use the line header hash table if it's already
11876 created, but don't create one just yet. */
11877
11878 if (dwarf2_per_objfile->line_header_hash == NULL
11879 && die->tag == DW_TAG_partial_unit)
2ab95328 11880 {
527f3840
JK
11881 dwarf2_per_objfile->line_header_hash
11882 = htab_create_alloc_ex (127, line_header_hash_voidp,
11883 line_header_eq_voidp,
11884 free_line_header_voidp,
11885 &objfile->objfile_obstack,
11886 hashtab_obstack_allocate,
11887 dummy_obstack_deallocate);
11888 }
2ab95328 11889
9c541725 11890 line_header_local.sect_off = line_offset;
527f3840
JK
11891 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11892 line_header_local_hash = line_header_hash (&line_header_local);
11893 if (dwarf2_per_objfile->line_header_hash != NULL)
11894 {
11895 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11896 &line_header_local,
11897 line_header_local_hash, NO_INSERT);
11898
11899 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11900 is not present in *SLOT (since if there is something in *SLOT then
11901 it will be for a partial_unit). */
11902 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11903 {
527f3840 11904 gdb_assert (*slot != NULL);
9a3c8263 11905 cu->line_header = (struct line_header *) *slot;
527f3840 11906 return;
dee91e82 11907 }
2ab95328 11908 }
527f3840
JK
11909
11910 /* dwarf_decode_line_header does not yet provide sufficient information.
11911 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11912 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11913 if (lh == NULL)
527f3840 11914 return;
4c8aa72d
PA
11915
11916 cu->line_header = lh.release ();
11917 cu->line_header_die_owner = die;
527f3840
JK
11918
11919 if (dwarf2_per_objfile->line_header_hash == NULL)
11920 slot = NULL;
11921 else
11922 {
11923 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11924 &line_header_local,
11925 line_header_local_hash, INSERT);
11926 gdb_assert (slot != NULL);
11927 }
11928 if (slot != NULL && *slot == NULL)
11929 {
11930 /* This newly decoded line number information unit will be owned
11931 by line_header_hash hash table. */
11932 *slot = cu->line_header;
4c8aa72d 11933 cu->line_header_die_owner = NULL;
527f3840
JK
11934 }
11935 else
11936 {
11937 /* We cannot free any current entry in (*slot) as that struct line_header
11938 may be already used by multiple CUs. Create only temporary decoded
11939 line_header for this CU - it may happen at most once for each line
11940 number information unit. And if we're not using line_header_hash
11941 then this is what we want as well. */
11942 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11943 }
11944 decode_mapping = (die->tag != DW_TAG_partial_unit);
11945 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11946 decode_mapping);
fff8551c 11947
2ab95328
TT
11948}
11949
95554aad 11950/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11951
c906108c 11952static void
e7c27a73 11953read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11954{
518817b3
SM
11955 struct dwarf2_per_objfile *dwarf2_per_objfile
11956 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11957 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11958 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11959 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11960 CORE_ADDR highpc = ((CORE_ADDR) 0);
11961 struct attribute *attr;
c906108c 11962 struct die_info *child_die;
e142c38c 11963 CORE_ADDR baseaddr;
6e70227d 11964
e142c38c 11965 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11966
fae299cd 11967 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11968
11969 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11970 from finish_block. */
2acceee2 11971 if (lowpc == ((CORE_ADDR) -1))
c906108c 11972 lowpc = highpc;
3e29f34a 11973 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11974
d721ba37 11975 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11976
95554aad 11977 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 11978
f4b8a18d
KW
11979 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11980 standardised yet. As a workaround for the language detection we fall
11981 back to the DW_AT_producer string. */
11982 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11983 cu->language = language_opencl;
11984
3019eac3
DE
11985 /* Similar hack for Go. */
11986 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11987 set_cu_language (DW_LANG_Go, cu);
11988
d721ba37 11989 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11990
11991 /* Decode line number information if present. We do this before
11992 processing child DIEs, so that the line header table is available
11993 for DW_AT_decl_file. */
d721ba37 11994 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11995
11996 /* Process all dies in compilation unit. */
11997 if (die->child != NULL)
11998 {
11999 child_die = die->child;
12000 while (child_die && child_die->tag)
12001 {
12002 process_die (child_die, cu);
12003 child_die = sibling_die (child_die);
12004 }
12005 }
12006
12007 /* Decode macro information, if present. Dwarf 2 macro information
12008 refers to information in the line number info statement program
12009 header, so we can only read it if we've read the header
12010 successfully. */
0af92d60
JK
12011 attr = dwarf2_attr (die, DW_AT_macros, cu);
12012 if (attr == NULL)
12013 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
12014 if (attr && cu->line_header)
12015 {
12016 if (dwarf2_attr (die, DW_AT_macro_info, cu))
12017 complaint (&symfile_complaints,
0af92d60 12018 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 12019
43f3e411 12020 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
12021 }
12022 else
12023 {
12024 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
12025 if (attr && cu->line_header)
12026 {
12027 unsigned int macro_offset = DW_UNSND (attr);
12028
43f3e411 12029 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
12030 }
12031 }
3019eac3
DE
12032}
12033
f4dc4d17
DE
12034/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
12035 Create the set of symtabs used by this TU, or if this TU is sharing
12036 symtabs with another TU and the symtabs have already been created
12037 then restore those symtabs in the line header.
12038 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
12039
12040static void
f4dc4d17 12041setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 12042{
f4dc4d17
DE
12043 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
12044 struct type_unit_group *tu_group;
12045 int first_time;
3019eac3 12046 struct attribute *attr;
9c541725 12047 unsigned int i;
0186c6a7 12048 struct signatured_type *sig_type;
3019eac3 12049
f4dc4d17 12050 gdb_assert (per_cu->is_debug_types);
0186c6a7 12051 sig_type = (struct signatured_type *) per_cu;
3019eac3 12052
f4dc4d17 12053 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 12054
f4dc4d17 12055 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 12056 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
12057 if (sig_type->type_unit_group == NULL)
12058 sig_type->type_unit_group = get_type_unit_group (cu, attr);
12059 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
12060
12061 /* If we've already processed this stmt_list there's no real need to
12062 do it again, we could fake it and just recreate the part we need
12063 (file name,index -> symtab mapping). If data shows this optimization
12064 is useful we can do it then. */
43f3e411 12065 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
12066
12067 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
12068 debug info. */
fff8551c 12069 line_header_up lh;
f4dc4d17 12070 if (attr != NULL)
3019eac3 12071 {
9c541725 12072 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
f4dc4d17
DE
12073 lh = dwarf_decode_line_header (line_offset, cu);
12074 }
12075 if (lh == NULL)
12076 {
12077 if (first_time)
12078 dwarf2_start_symtab (cu, "", NULL, 0);
12079 else
12080 {
12081 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 12082 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 12083 }
f4dc4d17 12084 return;
3019eac3
DE
12085 }
12086
4c8aa72d
PA
12087 cu->line_header = lh.release ();
12088 cu->line_header_die_owner = die;
3019eac3 12089
f4dc4d17
DE
12090 if (first_time)
12091 {
43f3e411 12092 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 12093
1fd60fc0
DE
12094 /* Note: We don't assign tu_group->compunit_symtab yet because we're
12095 still initializing it, and our caller (a few levels up)
12096 process_full_type_unit still needs to know if this is the first
12097 time. */
12098
4c8aa72d
PA
12099 tu_group->num_symtabs = cu->line_header->file_names.size ();
12100 tu_group->symtabs = XNEWVEC (struct symtab *,
12101 cu->line_header->file_names.size ());
3019eac3 12102
4c8aa72d 12103 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 12104 {
4c8aa72d 12105 file_entry &fe = cu->line_header->file_names[i];
3019eac3 12106
4c8aa72d 12107 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
3019eac3 12108
f4dc4d17
DE
12109 if (current_subfile->symtab == NULL)
12110 {
4c8aa72d
PA
12111 /* NOTE: start_subfile will recognize when it's been
12112 passed a file it has already seen. So we can't
12113 assume there's a simple mapping from
12114 cu->line_header->file_names to subfiles, plus
12115 cu->line_header->file_names may contain dups. */
43f3e411
DE
12116 current_subfile->symtab
12117 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
12118 }
12119
8c43009f
PA
12120 fe.symtab = current_subfile->symtab;
12121 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
12122 }
12123 }
12124 else
3019eac3 12125 {
0ab9ce85 12126 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 12127
4c8aa72d 12128 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 12129 {
4c8aa72d 12130 file_entry &fe = cu->line_header->file_names[i];
f4dc4d17 12131
4c8aa72d 12132 fe.symtab = tu_group->symtabs[i];
f4dc4d17 12133 }
3019eac3
DE
12134 }
12135
f4dc4d17
DE
12136 /* The main symtab is allocated last. Type units don't have DW_AT_name
12137 so they don't have a "real" (so to speak) symtab anyway.
12138 There is later code that will assign the main symtab to all symbols
12139 that don't have one. We need to handle the case of a symbol with a
12140 missing symtab (DW_AT_decl_file) anyway. */
12141}
3019eac3 12142
f4dc4d17
DE
12143/* Process DW_TAG_type_unit.
12144 For TUs we want to skip the first top level sibling if it's not the
12145 actual type being defined by this TU. In this case the first top
12146 level sibling is there to provide context only. */
3019eac3 12147
f4dc4d17
DE
12148static void
12149read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
12150{
12151 struct die_info *child_die;
3019eac3 12152
f4dc4d17
DE
12153 prepare_one_comp_unit (cu, die, language_minimal);
12154
12155 /* Initialize (or reinitialize) the machinery for building symtabs.
12156 We do this before processing child DIEs, so that the line header table
12157 is available for DW_AT_decl_file. */
12158 setup_type_unit_groups (die, cu);
12159
12160 if (die->child != NULL)
12161 {
12162 child_die = die->child;
12163 while (child_die && child_die->tag)
12164 {
12165 process_die (child_die, cu);
12166 child_die = sibling_die (child_die);
12167 }
12168 }
3019eac3
DE
12169}
12170\f
80626a55
DE
12171/* DWO/DWP files.
12172
12173 http://gcc.gnu.org/wiki/DebugFission
12174 http://gcc.gnu.org/wiki/DebugFissionDWP
12175
12176 To simplify handling of both DWO files ("object" files with the DWARF info)
12177 and DWP files (a file with the DWOs packaged up into one file), we treat
12178 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
12179
12180static hashval_t
12181hash_dwo_file (const void *item)
12182{
9a3c8263 12183 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 12184 hashval_t hash;
3019eac3 12185
a2ce51a0
DE
12186 hash = htab_hash_string (dwo_file->dwo_name);
12187 if (dwo_file->comp_dir != NULL)
12188 hash += htab_hash_string (dwo_file->comp_dir);
12189 return hash;
3019eac3
DE
12190}
12191
12192static int
12193eq_dwo_file (const void *item_lhs, const void *item_rhs)
12194{
9a3c8263
SM
12195 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
12196 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 12197
a2ce51a0
DE
12198 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
12199 return 0;
12200 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
12201 return lhs->comp_dir == rhs->comp_dir;
12202 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
12203}
12204
12205/* Allocate a hash table for DWO files. */
12206
12207static htab_t
ed2dc618 12208allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 12209{
3019eac3
DE
12210 return htab_create_alloc_ex (41,
12211 hash_dwo_file,
12212 eq_dwo_file,
12213 NULL,
12214 &objfile->objfile_obstack,
12215 hashtab_obstack_allocate,
12216 dummy_obstack_deallocate);
12217}
12218
80626a55
DE
12219/* Lookup DWO file DWO_NAME. */
12220
12221static void **
ed2dc618
SM
12222lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
12223 const char *dwo_name,
12224 const char *comp_dir)
80626a55
DE
12225{
12226 struct dwo_file find_entry;
12227 void **slot;
12228
12229 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
12230 dwarf2_per_objfile->dwo_files
12231 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55
DE
12232
12233 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
12234 find_entry.dwo_name = dwo_name;
12235 find_entry.comp_dir = comp_dir;
80626a55
DE
12236 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
12237
12238 return slot;
12239}
12240
3019eac3
DE
12241static hashval_t
12242hash_dwo_unit (const void *item)
12243{
9a3c8263 12244 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
12245
12246 /* This drops the top 32 bits of the id, but is ok for a hash. */
12247 return dwo_unit->signature;
12248}
12249
12250static int
12251eq_dwo_unit (const void *item_lhs, const void *item_rhs)
12252{
9a3c8263
SM
12253 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
12254 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
12255
12256 /* The signature is assumed to be unique within the DWO file.
12257 So while object file CU dwo_id's always have the value zero,
12258 that's OK, assuming each object file DWO file has only one CU,
12259 and that's the rule for now. */
12260 return lhs->signature == rhs->signature;
12261}
12262
12263/* Allocate a hash table for DWO CUs,TUs.
12264 There is one of these tables for each of CUs,TUs for each DWO file. */
12265
12266static htab_t
12267allocate_dwo_unit_table (struct objfile *objfile)
12268{
12269 /* Start out with a pretty small number.
12270 Generally DWO files contain only one CU and maybe some TUs. */
12271 return htab_create_alloc_ex (3,
12272 hash_dwo_unit,
12273 eq_dwo_unit,
12274 NULL,
12275 &objfile->objfile_obstack,
12276 hashtab_obstack_allocate,
12277 dummy_obstack_deallocate);
12278}
12279
80626a55 12280/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 12281
19c3d4c9 12282struct create_dwo_cu_data
3019eac3
DE
12283{
12284 struct dwo_file *dwo_file;
19c3d4c9 12285 struct dwo_unit dwo_unit;
3019eac3
DE
12286};
12287
19c3d4c9 12288/* die_reader_func for create_dwo_cu. */
3019eac3
DE
12289
12290static void
19c3d4c9
DE
12291create_dwo_cu_reader (const struct die_reader_specs *reader,
12292 const gdb_byte *info_ptr,
12293 struct die_info *comp_unit_die,
12294 int has_children,
12295 void *datap)
3019eac3
DE
12296{
12297 struct dwarf2_cu *cu = reader->cu;
9c541725 12298 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 12299 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 12300 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 12301 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 12302 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 12303 struct attribute *attr;
3019eac3
DE
12304
12305 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
12306 if (attr == NULL)
12307 {
19c3d4c9 12308 complaint (&symfile_complaints,
9d8780f0 12309 _("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 12310 " its dwo_id [in module %s]"),
9d8780f0 12311 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
12312 return;
12313 }
12314
3019eac3
DE
12315 dwo_unit->dwo_file = dwo_file;
12316 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 12317 dwo_unit->section = section;
9c541725 12318 dwo_unit->sect_off = sect_off;
3019eac3
DE
12319 dwo_unit->length = cu->per_cu->length;
12320
b4f54984 12321 if (dwarf_read_debug)
9d8780f0
SM
12322 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
12323 sect_offset_str (sect_off),
9c541725 12324 hex_string (dwo_unit->signature));
3019eac3
DE
12325}
12326
33c5cd75 12327/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 12328 Note: This function processes DWO files only, not DWP files. */
3019eac3 12329
33c5cd75 12330static void
ed2dc618
SM
12331create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12332 struct dwo_file &dwo_file, dwarf2_section_info &section,
33c5cd75 12333 htab_t &cus_htab)
3019eac3
DE
12334{
12335 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 12336 const gdb_byte *info_ptr, *end_ptr;
3019eac3 12337
33c5cd75
DB
12338 dwarf2_read_section (objfile, &section);
12339 info_ptr = section.buffer;
3019eac3
DE
12340
12341 if (info_ptr == NULL)
33c5cd75 12342 return;
3019eac3 12343
b4f54984 12344 if (dwarf_read_debug)
19c3d4c9
DE
12345 {
12346 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
12347 get_section_name (&section),
12348 get_section_file_name (&section));
19c3d4c9 12349 }
3019eac3 12350
33c5cd75 12351 end_ptr = info_ptr + section.size;
3019eac3
DE
12352 while (info_ptr < end_ptr)
12353 {
12354 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
12355 struct create_dwo_cu_data create_dwo_cu_data;
12356 struct dwo_unit *dwo_unit;
12357 void **slot;
12358 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 12359
19c3d4c9
DE
12360 memset (&create_dwo_cu_data.dwo_unit, 0,
12361 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3 12362 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 12363 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 12364 per_cu.is_debug_types = 0;
33c5cd75
DB
12365 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12366 per_cu.section = &section;
c5ed0576 12367 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
12368
12369 init_cutu_and_read_dies_no_follow (
12370 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12371 info_ptr += per_cu.length;
12372
12373 // If the unit could not be parsed, skip it.
12374 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12375 continue;
3019eac3 12376
33c5cd75
DB
12377 if (cus_htab == NULL)
12378 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 12379
33c5cd75
DB
12380 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12381 *dwo_unit = create_dwo_cu_data.dwo_unit;
12382 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12383 gdb_assert (slot != NULL);
12384 if (*slot != NULL)
19c3d4c9 12385 {
33c5cd75
DB
12386 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12387 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 12388
33c5cd75 12389 complaint (&symfile_complaints,
9d8780f0
SM
12390 _("debug cu entry at offset %s is duplicate to"
12391 " the entry at offset %s, signature %s"),
12392 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 12393 hex_string (dwo_unit->signature));
19c3d4c9 12394 }
33c5cd75 12395 *slot = (void *)dwo_unit;
3019eac3 12396 }
3019eac3
DE
12397}
12398
80626a55
DE
12399/* DWP file .debug_{cu,tu}_index section format:
12400 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12401
d2415c6c
DE
12402 DWP Version 1:
12403
80626a55
DE
12404 Both index sections have the same format, and serve to map a 64-bit
12405 signature to a set of section numbers. Each section begins with a header,
12406 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12407 indexes, and a pool of 32-bit section numbers. The index sections will be
12408 aligned at 8-byte boundaries in the file.
12409
d2415c6c
DE
12410 The index section header consists of:
12411
12412 V, 32 bit version number
12413 -, 32 bits unused
12414 N, 32 bit number of compilation units or type units in the index
12415 M, 32 bit number of slots in the hash table
80626a55 12416
d2415c6c 12417 Numbers are recorded using the byte order of the application binary.
80626a55 12418
d2415c6c
DE
12419 The hash table begins at offset 16 in the section, and consists of an array
12420 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12421 order of the application binary). Unused slots in the hash table are 0.
12422 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 12423
d2415c6c
DE
12424 The parallel table begins immediately after the hash table
12425 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12426 array of 32-bit indexes (using the byte order of the application binary),
12427 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12428 table contains a 32-bit index into the pool of section numbers. For unused
12429 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 12430
73869dc2
DE
12431 The pool of section numbers begins immediately following the hash table
12432 (at offset 16 + 12 * M from the beginning of the section). The pool of
12433 section numbers consists of an array of 32-bit words (using the byte order
12434 of the application binary). Each item in the array is indexed starting
12435 from 0. The hash table entry provides the index of the first section
12436 number in the set. Additional section numbers in the set follow, and the
12437 set is terminated by a 0 entry (section number 0 is not used in ELF).
12438
12439 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12440 section must be the first entry in the set, and the .debug_abbrev.dwo must
12441 be the second entry. Other members of the set may follow in any order.
12442
12443 ---
12444
12445 DWP Version 2:
12446
12447 DWP Version 2 combines all the .debug_info, etc. sections into one,
12448 and the entries in the index tables are now offsets into these sections.
12449 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12450 section.
12451
12452 Index Section Contents:
12453 Header
12454 Hash Table of Signatures dwp_hash_table.hash_table
12455 Parallel Table of Indices dwp_hash_table.unit_table
12456 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12457 Table of Section Sizes dwp_hash_table.v2.sizes
12458
12459 The index section header consists of:
12460
12461 V, 32 bit version number
12462 L, 32 bit number of columns in the table of section offsets
12463 N, 32 bit number of compilation units or type units in the index
12464 M, 32 bit number of slots in the hash table
12465
12466 Numbers are recorded using the byte order of the application binary.
12467
12468 The hash table has the same format as version 1.
12469 The parallel table of indices has the same format as version 1,
12470 except that the entries are origin-1 indices into the table of sections
12471 offsets and the table of section sizes.
12472
12473 The table of offsets begins immediately following the parallel table
12474 (at offset 16 + 12 * M from the beginning of the section). The table is
12475 a two-dimensional array of 32-bit words (using the byte order of the
12476 application binary), with L columns and N+1 rows, in row-major order.
12477 Each row in the array is indexed starting from 0. The first row provides
12478 a key to the remaining rows: each column in this row provides an identifier
12479 for a debug section, and the offsets in the same column of subsequent rows
12480 refer to that section. The section identifiers are:
12481
12482 DW_SECT_INFO 1 .debug_info.dwo
12483 DW_SECT_TYPES 2 .debug_types.dwo
12484 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12485 DW_SECT_LINE 4 .debug_line.dwo
12486 DW_SECT_LOC 5 .debug_loc.dwo
12487 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12488 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12489 DW_SECT_MACRO 8 .debug_macro.dwo
12490
12491 The offsets provided by the CU and TU index sections are the base offsets
12492 for the contributions made by each CU or TU to the corresponding section
12493 in the package file. Each CU and TU header contains an abbrev_offset
12494 field, used to find the abbreviations table for that CU or TU within the
12495 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12496 be interpreted as relative to the base offset given in the index section.
12497 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12498 should be interpreted as relative to the base offset for .debug_line.dwo,
12499 and offsets into other debug sections obtained from DWARF attributes should
12500 also be interpreted as relative to the corresponding base offset.
12501
12502 The table of sizes begins immediately following the table of offsets.
12503 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12504 with L columns and N rows, in row-major order. Each row in the array is
12505 indexed starting from 1 (row 0 is shared by the two tables).
12506
12507 ---
12508
12509 Hash table lookup is handled the same in version 1 and 2:
12510
12511 We assume that N and M will not exceed 2^32 - 1.
12512 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12513
d2415c6c
DE
12514 Given a 64-bit compilation unit signature or a type signature S, an entry
12515 in the hash table is located as follows:
80626a55 12516
d2415c6c
DE
12517 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12518 the low-order k bits all set to 1.
80626a55 12519
d2415c6c 12520 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12521
d2415c6c
DE
12522 3) If the hash table entry at index H matches the signature, use that
12523 entry. If the hash table entry at index H is unused (all zeroes),
12524 terminate the search: the signature is not present in the table.
80626a55 12525
d2415c6c 12526 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12527
d2415c6c 12528 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12529 to stop at an unused slot or find the match. */
80626a55
DE
12530
12531/* Create a hash table to map DWO IDs to their CU/TU entry in
12532 .debug_{info,types}.dwo in DWP_FILE.
12533 Returns NULL if there isn't one.
12534 Note: This function processes DWP files only, not DWO files. */
12535
12536static struct dwp_hash_table *
ed2dc618
SM
12537create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12538 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
12539{
12540 struct objfile *objfile = dwarf2_per_objfile->objfile;
12541 bfd *dbfd = dwp_file->dbfd;
948f8e3d 12542 const gdb_byte *index_ptr, *index_end;
80626a55 12543 struct dwarf2_section_info *index;
73869dc2 12544 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12545 struct dwp_hash_table *htab;
12546
12547 if (is_debug_types)
12548 index = &dwp_file->sections.tu_index;
12549 else
12550 index = &dwp_file->sections.cu_index;
12551
12552 if (dwarf2_section_empty_p (index))
12553 return NULL;
12554 dwarf2_read_section (objfile, index);
12555
12556 index_ptr = index->buffer;
12557 index_end = index_ptr + index->size;
12558
12559 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12560 index_ptr += 4;
12561 if (version == 2)
12562 nr_columns = read_4_bytes (dbfd, index_ptr);
12563 else
12564 nr_columns = 0;
12565 index_ptr += 4;
80626a55
DE
12566 nr_units = read_4_bytes (dbfd, index_ptr);
12567 index_ptr += 4;
12568 nr_slots = read_4_bytes (dbfd, index_ptr);
12569 index_ptr += 4;
12570
73869dc2 12571 if (version != 1 && version != 2)
80626a55 12572 {
21aa081e 12573 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12574 " [in module %s]"),
21aa081e 12575 pulongest (version), dwp_file->name);
80626a55
DE
12576 }
12577 if (nr_slots != (nr_slots & -nr_slots))
12578 {
21aa081e 12579 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12580 " is not power of 2 [in module %s]"),
21aa081e 12581 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12582 }
12583
12584 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12585 htab->version = version;
12586 htab->nr_columns = nr_columns;
80626a55
DE
12587 htab->nr_units = nr_units;
12588 htab->nr_slots = nr_slots;
12589 htab->hash_table = index_ptr;
12590 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12591
12592 /* Exit early if the table is empty. */
12593 if (nr_slots == 0 || nr_units == 0
12594 || (version == 2 && nr_columns == 0))
12595 {
12596 /* All must be zero. */
12597 if (nr_slots != 0 || nr_units != 0
12598 || (version == 2 && nr_columns != 0))
12599 {
12600 complaint (&symfile_complaints,
12601 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12602 " all zero [in modules %s]"),
12603 dwp_file->name);
12604 }
12605 return htab;
12606 }
12607
12608 if (version == 1)
12609 {
12610 htab->section_pool.v1.indices =
12611 htab->unit_table + sizeof (uint32_t) * nr_slots;
12612 /* It's harder to decide whether the section is too small in v1.
12613 V1 is deprecated anyway so we punt. */
12614 }
12615 else
12616 {
12617 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12618 int *ids = htab->section_pool.v2.section_ids;
12619 /* Reverse map for error checking. */
12620 int ids_seen[DW_SECT_MAX + 1];
12621 int i;
12622
12623 if (nr_columns < 2)
12624 {
12625 error (_("Dwarf Error: bad DWP hash table, too few columns"
12626 " in section table [in module %s]"),
12627 dwp_file->name);
12628 }
12629 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12630 {
12631 error (_("Dwarf Error: bad DWP hash table, too many columns"
12632 " in section table [in module %s]"),
12633 dwp_file->name);
12634 }
12635 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12636 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12637 for (i = 0; i < nr_columns; ++i)
12638 {
12639 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12640
12641 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12642 {
12643 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12644 " in section table [in module %s]"),
12645 id, dwp_file->name);
12646 }
12647 if (ids_seen[id] != -1)
12648 {
12649 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12650 " id %d in section table [in module %s]"),
12651 id, dwp_file->name);
12652 }
12653 ids_seen[id] = i;
12654 ids[i] = id;
12655 }
12656 /* Must have exactly one info or types section. */
12657 if (((ids_seen[DW_SECT_INFO] != -1)
12658 + (ids_seen[DW_SECT_TYPES] != -1))
12659 != 1)
12660 {
12661 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12662 " DWO info/types section [in module %s]"),
12663 dwp_file->name);
12664 }
12665 /* Must have an abbrev section. */
12666 if (ids_seen[DW_SECT_ABBREV] == -1)
12667 {
12668 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12669 " section [in module %s]"),
12670 dwp_file->name);
12671 }
12672 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12673 htab->section_pool.v2.sizes =
12674 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12675 * nr_units * nr_columns);
12676 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12677 * nr_units * nr_columns))
12678 > index_end)
12679 {
12680 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12681 " [in module %s]"),
12682 dwp_file->name);
12683 }
12684 }
80626a55
DE
12685
12686 return htab;
12687}
12688
12689/* Update SECTIONS with the data from SECTP.
12690
12691 This function is like the other "locate" section routines that are
12692 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12693 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12694
12695 The result is non-zero for success, or zero if an error was found. */
12696
12697static int
73869dc2
DE
12698locate_v1_virtual_dwo_sections (asection *sectp,
12699 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12700{
12701 const struct dwop_section_names *names = &dwop_section_names;
12702
12703 if (section_is_p (sectp->name, &names->abbrev_dwo))
12704 {
12705 /* There can be only one. */
049412e3 12706 if (sections->abbrev.s.section != NULL)
80626a55 12707 return 0;
049412e3 12708 sections->abbrev.s.section = sectp;
80626a55
DE
12709 sections->abbrev.size = bfd_get_section_size (sectp);
12710 }
12711 else if (section_is_p (sectp->name, &names->info_dwo)
12712 || section_is_p (sectp->name, &names->types_dwo))
12713 {
12714 /* There can be only one. */
049412e3 12715 if (sections->info_or_types.s.section != NULL)
80626a55 12716 return 0;
049412e3 12717 sections->info_or_types.s.section = sectp;
80626a55
DE
12718 sections->info_or_types.size = bfd_get_section_size (sectp);
12719 }
12720 else if (section_is_p (sectp->name, &names->line_dwo))
12721 {
12722 /* There can be only one. */
049412e3 12723 if (sections->line.s.section != NULL)
80626a55 12724 return 0;
049412e3 12725 sections->line.s.section = sectp;
80626a55
DE
12726 sections->line.size = bfd_get_section_size (sectp);
12727 }
12728 else if (section_is_p (sectp->name, &names->loc_dwo))
12729 {
12730 /* There can be only one. */
049412e3 12731 if (sections->loc.s.section != NULL)
80626a55 12732 return 0;
049412e3 12733 sections->loc.s.section = sectp;
80626a55
DE
12734 sections->loc.size = bfd_get_section_size (sectp);
12735 }
12736 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12737 {
12738 /* There can be only one. */
049412e3 12739 if (sections->macinfo.s.section != NULL)
80626a55 12740 return 0;
049412e3 12741 sections->macinfo.s.section = sectp;
80626a55
DE
12742 sections->macinfo.size = bfd_get_section_size (sectp);
12743 }
12744 else if (section_is_p (sectp->name, &names->macro_dwo))
12745 {
12746 /* There can be only one. */
049412e3 12747 if (sections->macro.s.section != NULL)
80626a55 12748 return 0;
049412e3 12749 sections->macro.s.section = sectp;
80626a55
DE
12750 sections->macro.size = bfd_get_section_size (sectp);
12751 }
12752 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12753 {
12754 /* There can be only one. */
049412e3 12755 if (sections->str_offsets.s.section != NULL)
80626a55 12756 return 0;
049412e3 12757 sections->str_offsets.s.section = sectp;
80626a55
DE
12758 sections->str_offsets.size = bfd_get_section_size (sectp);
12759 }
12760 else
12761 {
12762 /* No other kind of section is valid. */
12763 return 0;
12764 }
12765
12766 return 1;
12767}
12768
73869dc2
DE
12769/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12770 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12771 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12772 This is for DWP version 1 files. */
80626a55
DE
12773
12774static struct dwo_unit *
ed2dc618
SM
12775create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12776 struct dwp_file *dwp_file,
73869dc2
DE
12777 uint32_t unit_index,
12778 const char *comp_dir,
12779 ULONGEST signature, int is_debug_types)
80626a55
DE
12780{
12781 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12782 const struct dwp_hash_table *dwp_htab =
12783 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
12784 bfd *dbfd = dwp_file->dbfd;
12785 const char *kind = is_debug_types ? "TU" : "CU";
12786 struct dwo_file *dwo_file;
12787 struct dwo_unit *dwo_unit;
73869dc2 12788 struct virtual_v1_dwo_sections sections;
80626a55 12789 void **dwo_file_slot;
80626a55
DE
12790 int i;
12791
73869dc2
DE
12792 gdb_assert (dwp_file->version == 1);
12793
b4f54984 12794 if (dwarf_read_debug)
80626a55 12795 {
73869dc2 12796 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12797 kind,
73869dc2 12798 pulongest (unit_index), hex_string (signature),
80626a55
DE
12799 dwp_file->name);
12800 }
12801
19ac8c2e 12802 /* Fetch the sections of this DWO unit.
80626a55
DE
12803 Put a limit on the number of sections we look for so that bad data
12804 doesn't cause us to loop forever. */
12805
73869dc2 12806#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12807 (1 /* .debug_info or .debug_types */ \
12808 + 1 /* .debug_abbrev */ \
12809 + 1 /* .debug_line */ \
12810 + 1 /* .debug_loc */ \
12811 + 1 /* .debug_str_offsets */ \
19ac8c2e 12812 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12813 + 1 /* trailing zero */)
12814
12815 memset (&sections, 0, sizeof (sections));
80626a55 12816
73869dc2 12817 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12818 {
12819 asection *sectp;
12820 uint32_t section_nr =
12821 read_4_bytes (dbfd,
73869dc2
DE
12822 dwp_htab->section_pool.v1.indices
12823 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12824
12825 if (section_nr == 0)
12826 break;
12827 if (section_nr >= dwp_file->num_sections)
12828 {
12829 error (_("Dwarf Error: bad DWP hash table, section number too large"
12830 " [in module %s]"),
12831 dwp_file->name);
12832 }
12833
12834 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12835 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12836 {
12837 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12838 " [in module %s]"),
12839 dwp_file->name);
12840 }
12841 }
12842
12843 if (i < 2
a32a8923
DE
12844 || dwarf2_section_empty_p (&sections.info_or_types)
12845 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12846 {
12847 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12848 " [in module %s]"),
12849 dwp_file->name);
12850 }
73869dc2 12851 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12852 {
12853 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12854 " [in module %s]"),
12855 dwp_file->name);
12856 }
12857
12858 /* It's easier for the rest of the code if we fake a struct dwo_file and
12859 have dwo_unit "live" in that. At least for now.
12860
12861 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12862 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12863 file, we can combine them back into a virtual DWO file to save space
12864 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12865 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12866
791afaa2
TT
12867 std::string virtual_dwo_name =
12868 string_printf ("virtual-dwo/%d-%d-%d-%d",
12869 get_section_id (&sections.abbrev),
12870 get_section_id (&sections.line),
12871 get_section_id (&sections.loc),
12872 get_section_id (&sections.str_offsets));
80626a55 12873 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12874 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12875 virtual_dwo_name.c_str (),
12876 comp_dir);
80626a55
DE
12877 /* Create one if necessary. */
12878 if (*dwo_file_slot == NULL)
12879 {
b4f54984 12880 if (dwarf_read_debug)
80626a55
DE
12881 {
12882 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12883 virtual_dwo_name.c_str ());
80626a55
DE
12884 }
12885 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12886 dwo_file->dwo_name
12887 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12888 virtual_dwo_name.c_str (),
12889 virtual_dwo_name.size ());
0ac5b59e 12890 dwo_file->comp_dir = comp_dir;
80626a55
DE
12891 dwo_file->sections.abbrev = sections.abbrev;
12892 dwo_file->sections.line = sections.line;
12893 dwo_file->sections.loc = sections.loc;
12894 dwo_file->sections.macinfo = sections.macinfo;
12895 dwo_file->sections.macro = sections.macro;
12896 dwo_file->sections.str_offsets = sections.str_offsets;
12897 /* The "str" section is global to the entire DWP file. */
12898 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12899 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12900 there's no need to record it in dwo_file.
12901 Also, we can't simply record type sections in dwo_file because
12902 we record a pointer into the vector in dwo_unit. As we collect more
12903 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12904 for it, invalidating all copies of pointers into the previous
12905 contents. */
80626a55
DE
12906 *dwo_file_slot = dwo_file;
12907 }
12908 else
12909 {
b4f54984 12910 if (dwarf_read_debug)
80626a55
DE
12911 {
12912 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12913 virtual_dwo_name.c_str ());
80626a55 12914 }
9a3c8263 12915 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12916 }
80626a55
DE
12917
12918 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12919 dwo_unit->dwo_file = dwo_file;
12920 dwo_unit->signature = signature;
8d749320
SM
12921 dwo_unit->section =
12922 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12923 *dwo_unit->section = sections.info_or_types;
57d63ce2 12924 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12925
12926 return dwo_unit;
12927}
12928
73869dc2
DE
12929/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12930 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12931 piece within that section used by a TU/CU, return a virtual section
12932 of just that piece. */
12933
12934static struct dwarf2_section_info
ed2dc618
SM
12935create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12936 struct dwarf2_section_info *section,
73869dc2
DE
12937 bfd_size_type offset, bfd_size_type size)
12938{
12939 struct dwarf2_section_info result;
12940 asection *sectp;
12941
12942 gdb_assert (section != NULL);
12943 gdb_assert (!section->is_virtual);
12944
12945 memset (&result, 0, sizeof (result));
12946 result.s.containing_section = section;
12947 result.is_virtual = 1;
12948
12949 if (size == 0)
12950 return result;
12951
12952 sectp = get_section_bfd_section (section);
12953
12954 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12955 bounds of the real section. This is a pretty-rare event, so just
12956 flag an error (easier) instead of a warning and trying to cope. */
12957 if (sectp == NULL
12958 || offset + size > bfd_get_section_size (sectp))
12959 {
73869dc2
DE
12960 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12961 " in section %s [in module %s]"),
12962 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12963 objfile_name (dwarf2_per_objfile->objfile));
12964 }
12965
12966 result.virtual_offset = offset;
12967 result.size = size;
12968 return result;
12969}
12970
12971/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12972 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12973 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12974 This is for DWP version 2 files. */
12975
12976static struct dwo_unit *
ed2dc618
SM
12977create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12978 struct dwp_file *dwp_file,
73869dc2
DE
12979 uint32_t unit_index,
12980 const char *comp_dir,
12981 ULONGEST signature, int is_debug_types)
12982{
12983 struct objfile *objfile = dwarf2_per_objfile->objfile;
12984 const struct dwp_hash_table *dwp_htab =
12985 is_debug_types ? dwp_file->tus : dwp_file->cus;
12986 bfd *dbfd = dwp_file->dbfd;
12987 const char *kind = is_debug_types ? "TU" : "CU";
12988 struct dwo_file *dwo_file;
12989 struct dwo_unit *dwo_unit;
12990 struct virtual_v2_dwo_sections sections;
12991 void **dwo_file_slot;
73869dc2
DE
12992 int i;
12993
12994 gdb_assert (dwp_file->version == 2);
12995
b4f54984 12996 if (dwarf_read_debug)
73869dc2
DE
12997 {
12998 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12999 kind,
13000 pulongest (unit_index), hex_string (signature),
13001 dwp_file->name);
13002 }
13003
13004 /* Fetch the section offsets of this DWO unit. */
13005
13006 memset (&sections, 0, sizeof (sections));
73869dc2
DE
13007
13008 for (i = 0; i < dwp_htab->nr_columns; ++i)
13009 {
13010 uint32_t offset = read_4_bytes (dbfd,
13011 dwp_htab->section_pool.v2.offsets
13012 + (((unit_index - 1) * dwp_htab->nr_columns
13013 + i)
13014 * sizeof (uint32_t)));
13015 uint32_t size = read_4_bytes (dbfd,
13016 dwp_htab->section_pool.v2.sizes
13017 + (((unit_index - 1) * dwp_htab->nr_columns
13018 + i)
13019 * sizeof (uint32_t)));
13020
13021 switch (dwp_htab->section_pool.v2.section_ids[i])
13022 {
13023 case DW_SECT_INFO:
13024 case DW_SECT_TYPES:
13025 sections.info_or_types_offset = offset;
13026 sections.info_or_types_size = size;
13027 break;
13028 case DW_SECT_ABBREV:
13029 sections.abbrev_offset = offset;
13030 sections.abbrev_size = size;
13031 break;
13032 case DW_SECT_LINE:
13033 sections.line_offset = offset;
13034 sections.line_size = size;
13035 break;
13036 case DW_SECT_LOC:
13037 sections.loc_offset = offset;
13038 sections.loc_size = size;
13039 break;
13040 case DW_SECT_STR_OFFSETS:
13041 sections.str_offsets_offset = offset;
13042 sections.str_offsets_size = size;
13043 break;
13044 case DW_SECT_MACINFO:
13045 sections.macinfo_offset = offset;
13046 sections.macinfo_size = size;
13047 break;
13048 case DW_SECT_MACRO:
13049 sections.macro_offset = offset;
13050 sections.macro_size = size;
13051 break;
13052 }
13053 }
13054
13055 /* It's easier for the rest of the code if we fake a struct dwo_file and
13056 have dwo_unit "live" in that. At least for now.
13057
13058 The DWP file can be made up of a random collection of CUs and TUs.
13059 However, for each CU + set of TUs that came from the same original DWO
13060 file, we can combine them back into a virtual DWO file to save space
13061 (fewer struct dwo_file objects to allocate). Remember that for really
13062 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
13063
791afaa2
TT
13064 std::string virtual_dwo_name =
13065 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
13066 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
13067 (long) (sections.line_size ? sections.line_offset : 0),
13068 (long) (sections.loc_size ? sections.loc_offset : 0),
13069 (long) (sections.str_offsets_size
13070 ? sections.str_offsets_offset : 0));
73869dc2 13071 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
13072 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13073 virtual_dwo_name.c_str (),
13074 comp_dir);
73869dc2
DE
13075 /* Create one if necessary. */
13076 if (*dwo_file_slot == NULL)
13077 {
b4f54984 13078 if (dwarf_read_debug)
73869dc2
DE
13079 {
13080 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 13081 virtual_dwo_name.c_str ());
73869dc2
DE
13082 }
13083 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
13084 dwo_file->dwo_name
13085 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
13086 virtual_dwo_name.c_str (),
13087 virtual_dwo_name.size ());
73869dc2
DE
13088 dwo_file->comp_dir = comp_dir;
13089 dwo_file->sections.abbrev =
ed2dc618 13090 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
13091 sections.abbrev_offset, sections.abbrev_size);
13092 dwo_file->sections.line =
ed2dc618 13093 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
13094 sections.line_offset, sections.line_size);
13095 dwo_file->sections.loc =
ed2dc618 13096 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
13097 sections.loc_offset, sections.loc_size);
13098 dwo_file->sections.macinfo =
ed2dc618 13099 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
13100 sections.macinfo_offset, sections.macinfo_size);
13101 dwo_file->sections.macro =
ed2dc618 13102 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
13103 sections.macro_offset, sections.macro_size);
13104 dwo_file->sections.str_offsets =
ed2dc618
SM
13105 create_dwp_v2_section (dwarf2_per_objfile,
13106 &dwp_file->sections.str_offsets,
73869dc2
DE
13107 sections.str_offsets_offset,
13108 sections.str_offsets_size);
13109 /* The "str" section is global to the entire DWP file. */
13110 dwo_file->sections.str = dwp_file->sections.str;
13111 /* The info or types section is assigned below to dwo_unit,
13112 there's no need to record it in dwo_file.
13113 Also, we can't simply record type sections in dwo_file because
13114 we record a pointer into the vector in dwo_unit. As we collect more
13115 types we'll grow the vector and eventually have to reallocate space
13116 for it, invalidating all copies of pointers into the previous
13117 contents. */
13118 *dwo_file_slot = dwo_file;
13119 }
13120 else
13121 {
b4f54984 13122 if (dwarf_read_debug)
73869dc2
DE
13123 {
13124 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 13125 virtual_dwo_name.c_str ());
73869dc2 13126 }
9a3c8263 13127 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 13128 }
73869dc2
DE
13129
13130 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
13131 dwo_unit->dwo_file = dwo_file;
13132 dwo_unit->signature = signature;
8d749320
SM
13133 dwo_unit->section =
13134 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
13135 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
13136 is_debug_types
73869dc2
DE
13137 ? &dwp_file->sections.types
13138 : &dwp_file->sections.info,
13139 sections.info_or_types_offset,
13140 sections.info_or_types_size);
13141 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
13142
13143 return dwo_unit;
13144}
13145
57d63ce2
DE
13146/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
13147 Returns NULL if the signature isn't found. */
80626a55
DE
13148
13149static struct dwo_unit *
ed2dc618
SM
13150lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
13151 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 13152 ULONGEST signature, int is_debug_types)
80626a55 13153{
57d63ce2
DE
13154 const struct dwp_hash_table *dwp_htab =
13155 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 13156 bfd *dbfd = dwp_file->dbfd;
57d63ce2 13157 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
13158 uint32_t hash = signature & mask;
13159 uint32_t hash2 = ((signature >> 32) & mask) | 1;
13160 unsigned int i;
13161 void **slot;
870f88f7 13162 struct dwo_unit find_dwo_cu;
80626a55
DE
13163
13164 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
13165 find_dwo_cu.signature = signature;
19ac8c2e
DE
13166 slot = htab_find_slot (is_debug_types
13167 ? dwp_file->loaded_tus
13168 : dwp_file->loaded_cus,
13169 &find_dwo_cu, INSERT);
80626a55
DE
13170
13171 if (*slot != NULL)
9a3c8263 13172 return (struct dwo_unit *) *slot;
80626a55
DE
13173
13174 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 13175 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
13176 {
13177 ULONGEST signature_in_table;
13178
13179 signature_in_table =
57d63ce2 13180 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
13181 if (signature_in_table == signature)
13182 {
57d63ce2
DE
13183 uint32_t unit_index =
13184 read_4_bytes (dbfd,
13185 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 13186
73869dc2
DE
13187 if (dwp_file->version == 1)
13188 {
ed2dc618
SM
13189 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
13190 dwp_file, unit_index,
73869dc2
DE
13191 comp_dir, signature,
13192 is_debug_types);
13193 }
13194 else
13195 {
ed2dc618
SM
13196 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
13197 dwp_file, unit_index,
73869dc2
DE
13198 comp_dir, signature,
13199 is_debug_types);
13200 }
9a3c8263 13201 return (struct dwo_unit *) *slot;
80626a55
DE
13202 }
13203 if (signature_in_table == 0)
13204 return NULL;
13205 hash = (hash + hash2) & mask;
13206 }
13207
13208 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
13209 " [in module %s]"),
13210 dwp_file->name);
13211}
13212
ab5088bf 13213/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
13214 Open the file specified by FILE_NAME and hand it off to BFD for
13215 preliminary analysis. Return a newly initialized bfd *, which
13216 includes a canonicalized copy of FILE_NAME.
80626a55 13217 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
13218 SEARCH_CWD is true if the current directory is to be searched.
13219 It will be searched before debug-file-directory.
13aaf454
DE
13220 If successful, the file is added to the bfd include table of the
13221 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 13222 If unable to find/open the file, return NULL.
3019eac3
DE
13223 NOTE: This function is derived from symfile_bfd_open. */
13224
192b62ce 13225static gdb_bfd_ref_ptr
ed2dc618
SM
13226try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13227 const char *file_name, int is_dwp, int search_cwd)
3019eac3 13228{
24b9144d 13229 int desc;
9c02c129
DE
13230 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
13231 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
13232 to debug_file_directory. */
e0cc99a6 13233 const char *search_path;
9c02c129
DE
13234 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
13235
e0cc99a6 13236 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
13237 if (search_cwd)
13238 {
13239 if (*debug_file_directory != '\0')
e0cc99a6
TT
13240 {
13241 search_path_holder.reset (concat (".", dirname_separator_string,
13242 debug_file_directory,
13243 (char *) NULL));
13244 search_path = search_path_holder.get ();
13245 }
6ac97d4c 13246 else
e0cc99a6 13247 search_path = ".";
6ac97d4c 13248 }
9c02c129 13249 else
e0cc99a6 13250 search_path = debug_file_directory;
3019eac3 13251
24b9144d 13252 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
13253 if (is_dwp)
13254 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
13255
13256 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 13257 desc = openp (search_path, flags, file_name,
3019eac3
DE
13258 O_RDONLY | O_BINARY, &absolute_name);
13259 if (desc < 0)
13260 return NULL;
13261
e0cc99a6
TT
13262 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
13263 gnutarget, desc));
9c02c129
DE
13264 if (sym_bfd == NULL)
13265 return NULL;
192b62ce 13266 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 13267
192b62ce
TT
13268 if (!bfd_check_format (sym_bfd.get (), bfd_object))
13269 return NULL;
3019eac3 13270
13aaf454
DE
13271 /* Success. Record the bfd as having been included by the objfile's bfd.
13272 This is important because things like demangled_names_hash lives in the
13273 objfile's per_bfd space and may have references to things like symbol
13274 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 13275 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 13276
3019eac3
DE
13277 return sym_bfd;
13278}
13279
ab5088bf 13280/* Try to open DWO file FILE_NAME.
3019eac3
DE
13281 COMP_DIR is the DW_AT_comp_dir attribute.
13282 The result is the bfd handle of the file.
13283 If there is a problem finding or opening the file, return NULL.
13284 Upon success, the canonicalized path of the file is stored in the bfd,
13285 same as symfile_bfd_open. */
13286
192b62ce 13287static gdb_bfd_ref_ptr
ed2dc618
SM
13288open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13289 const char *file_name, const char *comp_dir)
3019eac3 13290{
80626a55 13291 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
13292 return try_open_dwop_file (dwarf2_per_objfile, file_name,
13293 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
13294
13295 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
13296
13297 if (comp_dir != NULL)
13298 {
b36cec19
PA
13299 char *path_to_try = concat (comp_dir, SLASH_STRING,
13300 file_name, (char *) NULL);
3019eac3
DE
13301
13302 /* NOTE: If comp_dir is a relative path, this will also try the
13303 search path, which seems useful. */
ed2dc618
SM
13304 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
13305 path_to_try,
13306 0 /*is_dwp*/,
192b62ce 13307 1 /*search_cwd*/));
3019eac3
DE
13308 xfree (path_to_try);
13309 if (abfd != NULL)
13310 return abfd;
13311 }
13312
13313 /* That didn't work, try debug-file-directory, which, despite its name,
13314 is a list of paths. */
13315
13316 if (*debug_file_directory == '\0')
13317 return NULL;
13318
ed2dc618
SM
13319 return try_open_dwop_file (dwarf2_per_objfile, file_name,
13320 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
13321}
13322
80626a55
DE
13323/* This function is mapped across the sections and remembers the offset and
13324 size of each of the DWO debugging sections we are interested in. */
13325
13326static void
13327dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
13328{
9a3c8263 13329 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
13330 const struct dwop_section_names *names = &dwop_section_names;
13331
13332 if (section_is_p (sectp->name, &names->abbrev_dwo))
13333 {
049412e3 13334 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
13335 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
13336 }
13337 else if (section_is_p (sectp->name, &names->info_dwo))
13338 {
049412e3 13339 dwo_sections->info.s.section = sectp;
80626a55
DE
13340 dwo_sections->info.size = bfd_get_section_size (sectp);
13341 }
13342 else if (section_is_p (sectp->name, &names->line_dwo))
13343 {
049412e3 13344 dwo_sections->line.s.section = sectp;
80626a55
DE
13345 dwo_sections->line.size = bfd_get_section_size (sectp);
13346 }
13347 else if (section_is_p (sectp->name, &names->loc_dwo))
13348 {
049412e3 13349 dwo_sections->loc.s.section = sectp;
80626a55
DE
13350 dwo_sections->loc.size = bfd_get_section_size (sectp);
13351 }
13352 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13353 {
049412e3 13354 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
13355 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
13356 }
13357 else if (section_is_p (sectp->name, &names->macro_dwo))
13358 {
049412e3 13359 dwo_sections->macro.s.section = sectp;
80626a55
DE
13360 dwo_sections->macro.size = bfd_get_section_size (sectp);
13361 }
13362 else if (section_is_p (sectp->name, &names->str_dwo))
13363 {
049412e3 13364 dwo_sections->str.s.section = sectp;
80626a55
DE
13365 dwo_sections->str.size = bfd_get_section_size (sectp);
13366 }
13367 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13368 {
049412e3 13369 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
13370 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
13371 }
13372 else if (section_is_p (sectp->name, &names->types_dwo))
13373 {
13374 struct dwarf2_section_info type_section;
13375
13376 memset (&type_section, 0, sizeof (type_section));
049412e3 13377 type_section.s.section = sectp;
80626a55
DE
13378 type_section.size = bfd_get_section_size (sectp);
13379 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
13380 &type_section);
13381 }
13382}
13383
ab5088bf 13384/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 13385 by PER_CU. This is for the non-DWP case.
80626a55 13386 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
13387
13388static struct dwo_file *
0ac5b59e
DE
13389open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13390 const char *dwo_name, const char *comp_dir)
3019eac3 13391{
ed2dc618 13392 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 13393 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 13394 struct dwo_file *dwo_file;
3019eac3
DE
13395 struct cleanup *cleanups;
13396
ed2dc618 13397 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
80626a55
DE
13398 if (dbfd == NULL)
13399 {
b4f54984 13400 if (dwarf_read_debug)
80626a55
DE
13401 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13402 return NULL;
13403 }
13404 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
13405 dwo_file->dwo_name = dwo_name;
13406 dwo_file->comp_dir = comp_dir;
192b62ce 13407 dwo_file->dbfd = dbfd.release ();
3019eac3 13408
ed2dc618
SM
13409 free_dwo_file_cleanup_data *cleanup_data = XNEW (free_dwo_file_cleanup_data);
13410 cleanup_data->dwo_file = dwo_file;
13411 cleanup_data->dwarf2_per_objfile = dwarf2_per_objfile;
13412
13413 cleanups = make_cleanup (free_dwo_file_cleanup, cleanup_data);
3019eac3 13414
192b62ce
TT
13415 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
13416 &dwo_file->sections);
3019eac3 13417
ed2dc618
SM
13418 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13419 dwo_file->cus);
3019eac3 13420
ed2dc618
SM
13421 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file,
13422 dwo_file->sections.types, dwo_file->tus);
3019eac3
DE
13423
13424 discard_cleanups (cleanups);
13425
b4f54984 13426 if (dwarf_read_debug)
80626a55
DE
13427 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13428
3019eac3
DE
13429 return dwo_file;
13430}
13431
80626a55 13432/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
13433 size of each of the DWP debugging sections common to version 1 and 2 that
13434 we are interested in. */
3019eac3 13435
80626a55 13436static void
73869dc2
DE
13437dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13438 void *dwp_file_ptr)
3019eac3 13439{
9a3c8263 13440 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
13441 const struct dwop_section_names *names = &dwop_section_names;
13442 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 13443
80626a55 13444 /* Record the ELF section number for later lookup: this is what the
73869dc2 13445 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
13446 gdb_assert (elf_section_nr < dwp_file->num_sections);
13447 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 13448
80626a55
DE
13449 /* Look for specific sections that we need. */
13450 if (section_is_p (sectp->name, &names->str_dwo))
13451 {
049412e3 13452 dwp_file->sections.str.s.section = sectp;
80626a55
DE
13453 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13454 }
13455 else if (section_is_p (sectp->name, &names->cu_index))
13456 {
049412e3 13457 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
13458 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13459 }
13460 else if (section_is_p (sectp->name, &names->tu_index))
13461 {
049412e3 13462 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
13463 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13464 }
13465}
3019eac3 13466
73869dc2
DE
13467/* This function is mapped across the sections and remembers the offset and
13468 size of each of the DWP version 2 debugging sections that we are interested
13469 in. This is split into a separate function because we don't know if we
13470 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13471
13472static void
13473dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13474{
9a3c8263 13475 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
13476 const struct dwop_section_names *names = &dwop_section_names;
13477 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13478
13479 /* Record the ELF section number for later lookup: this is what the
13480 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13481 gdb_assert (elf_section_nr < dwp_file->num_sections);
13482 dwp_file->elf_sections[elf_section_nr] = sectp;
13483
13484 /* Look for specific sections that we need. */
13485 if (section_is_p (sectp->name, &names->abbrev_dwo))
13486 {
049412e3 13487 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
13488 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13489 }
13490 else if (section_is_p (sectp->name, &names->info_dwo))
13491 {
049412e3 13492 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
13493 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13494 }
13495 else if (section_is_p (sectp->name, &names->line_dwo))
13496 {
049412e3 13497 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
13498 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13499 }
13500 else if (section_is_p (sectp->name, &names->loc_dwo))
13501 {
049412e3 13502 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
13503 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13504 }
13505 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13506 {
049412e3 13507 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
13508 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13509 }
13510 else if (section_is_p (sectp->name, &names->macro_dwo))
13511 {
049412e3 13512 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
13513 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13514 }
13515 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13516 {
049412e3 13517 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
13518 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13519 }
13520 else if (section_is_p (sectp->name, &names->types_dwo))
13521 {
049412e3 13522 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
13523 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13524 }
13525}
13526
80626a55 13527/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13528
80626a55
DE
13529static hashval_t
13530hash_dwp_loaded_cutus (const void *item)
13531{
9a3c8263 13532 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13533
80626a55
DE
13534 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13535 return dwo_unit->signature;
3019eac3
DE
13536}
13537
80626a55 13538/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13539
80626a55
DE
13540static int
13541eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13542{
9a3c8263
SM
13543 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13544 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13545
80626a55
DE
13546 return dua->signature == dub->signature;
13547}
3019eac3 13548
80626a55 13549/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13550
80626a55
DE
13551static htab_t
13552allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13553{
13554 return htab_create_alloc_ex (3,
13555 hash_dwp_loaded_cutus,
13556 eq_dwp_loaded_cutus,
13557 NULL,
13558 &objfile->objfile_obstack,
13559 hashtab_obstack_allocate,
13560 dummy_obstack_deallocate);
13561}
3019eac3 13562
ab5088bf
DE
13563/* Try to open DWP file FILE_NAME.
13564 The result is the bfd handle of the file.
13565 If there is a problem finding or opening the file, return NULL.
13566 Upon success, the canonicalized path of the file is stored in the bfd,
13567 same as symfile_bfd_open. */
13568
192b62ce 13569static gdb_bfd_ref_ptr
ed2dc618
SM
13570open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13571 const char *file_name)
ab5088bf 13572{
ed2dc618
SM
13573 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13574 1 /*is_dwp*/,
192b62ce 13575 1 /*search_cwd*/));
6ac97d4c
DE
13576 if (abfd != NULL)
13577 return abfd;
13578
13579 /* Work around upstream bug 15652.
13580 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13581 [Whether that's a "bug" is debatable, but it is getting in our way.]
13582 We have no real idea where the dwp file is, because gdb's realpath-ing
13583 of the executable's path may have discarded the needed info.
13584 [IWBN if the dwp file name was recorded in the executable, akin to
13585 .gnu_debuglink, but that doesn't exist yet.]
13586 Strip the directory from FILE_NAME and search again. */
13587 if (*debug_file_directory != '\0')
13588 {
13589 /* Don't implicitly search the current directory here.
13590 If the user wants to search "." to handle this case,
13591 it must be added to debug-file-directory. */
ed2dc618
SM
13592 return try_open_dwop_file (dwarf2_per_objfile,
13593 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
13594 0 /*search_cwd*/);
13595 }
13596
13597 return NULL;
ab5088bf
DE
13598}
13599
80626a55
DE
13600/* Initialize the use of the DWP file for the current objfile.
13601 By convention the name of the DWP file is ${objfile}.dwp.
13602 The result is NULL if it can't be found. */
a766d390 13603
80626a55 13604static struct dwp_file *
ed2dc618 13605open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
13606{
13607 struct objfile *objfile = dwarf2_per_objfile->objfile;
13608 struct dwp_file *dwp_file;
80626a55 13609
82bf32bc
JK
13610 /* Try to find first .dwp for the binary file before any symbolic links
13611 resolving. */
6c447423
DE
13612
13613 /* If the objfile is a debug file, find the name of the real binary
13614 file and get the name of dwp file from there. */
d721ba37 13615 std::string dwp_name;
6c447423
DE
13616 if (objfile->separate_debug_objfile_backlink != NULL)
13617 {
13618 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13619 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13620
d721ba37 13621 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13622 }
13623 else
d721ba37
PA
13624 dwp_name = objfile->original_name;
13625
13626 dwp_name += ".dwp";
80626a55 13627
ed2dc618 13628 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13629 if (dbfd == NULL
13630 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13631 {
13632 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13633 dwp_name = objfile_name (objfile);
13634 dwp_name += ".dwp";
ed2dc618 13635 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
13636 }
13637
80626a55
DE
13638 if (dbfd == NULL)
13639 {
b4f54984 13640 if (dwarf_read_debug)
d721ba37 13641 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
80626a55 13642 return NULL;
3019eac3 13643 }
80626a55 13644 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
192b62ce
TT
13645 dwp_file->name = bfd_get_filename (dbfd.get ());
13646 dwp_file->dbfd = dbfd.release ();
c906108c 13647
80626a55 13648 /* +1: section 0 is unused */
192b62ce 13649 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
13650 dwp_file->elf_sections =
13651 OBSTACK_CALLOC (&objfile->objfile_obstack,
13652 dwp_file->num_sections, asection *);
13653
192b62ce
TT
13654 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13655 dwp_file);
80626a55 13656
ed2dc618 13657 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 0);
80626a55 13658
ed2dc618 13659 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 1);
80626a55 13660
73869dc2 13661 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13662 if (dwp_file->cus && dwp_file->tus
13663 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13664 {
13665 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13666 pretty bizarre. We use pulongest here because that's the established
4d65956b 13667 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13668 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13669 " TU version %s [in DWP file %s]"),
13670 pulongest (dwp_file->cus->version),
d721ba37 13671 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13672 }
08302ed2
DE
13673
13674 if (dwp_file->cus)
13675 dwp_file->version = dwp_file->cus->version;
13676 else if (dwp_file->tus)
13677 dwp_file->version = dwp_file->tus->version;
13678 else
13679 dwp_file->version = 2;
73869dc2
DE
13680
13681 if (dwp_file->version == 2)
192b62ce
TT
13682 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13683 dwp_file);
73869dc2 13684
19ac8c2e
DE
13685 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13686 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13687
b4f54984 13688 if (dwarf_read_debug)
80626a55
DE
13689 {
13690 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13691 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13692 " %s CUs, %s TUs\n",
13693 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13694 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13695 }
13696
13697 return dwp_file;
3019eac3 13698}
c906108c 13699
ab5088bf
DE
13700/* Wrapper around open_and_init_dwp_file, only open it once. */
13701
13702static struct dwp_file *
ed2dc618 13703get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
13704{
13705 if (! dwarf2_per_objfile->dwp_checked)
13706 {
ed2dc618
SM
13707 dwarf2_per_objfile->dwp_file
13708 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
13709 dwarf2_per_objfile->dwp_checked = 1;
13710 }
13711 return dwarf2_per_objfile->dwp_file;
13712}
13713
80626a55
DE
13714/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13715 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13716 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13717 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13718 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13719
13720 This is called, for example, when wanting to read a variable with a
13721 complex location. Therefore we don't want to do file i/o for every call.
13722 Therefore we don't want to look for a DWO file on every call.
13723 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13724 then we check if we've already seen DWO_NAME, and only THEN do we check
13725 for a DWO file.
13726
1c658ad5 13727 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13728 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13729
3019eac3 13730static struct dwo_unit *
80626a55
DE
13731lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13732 const char *dwo_name, const char *comp_dir,
13733 ULONGEST signature, int is_debug_types)
3019eac3 13734{
ed2dc618 13735 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 13736 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13737 const char *kind = is_debug_types ? "TU" : "CU";
13738 void **dwo_file_slot;
3019eac3 13739 struct dwo_file *dwo_file;
80626a55 13740 struct dwp_file *dwp_file;
cb1df416 13741
6a506a2d
DE
13742 /* First see if there's a DWP file.
13743 If we have a DWP file but didn't find the DWO inside it, don't
13744 look for the original DWO file. It makes gdb behave differently
13745 depending on whether one is debugging in the build tree. */
cf2c3c16 13746
ed2dc618 13747 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 13748 if (dwp_file != NULL)
cf2c3c16 13749 {
80626a55
DE
13750 const struct dwp_hash_table *dwp_htab =
13751 is_debug_types ? dwp_file->tus : dwp_file->cus;
13752
13753 if (dwp_htab != NULL)
13754 {
13755 struct dwo_unit *dwo_cutu =
ed2dc618 13756 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 13757 signature, is_debug_types);
80626a55
DE
13758
13759 if (dwo_cutu != NULL)
13760 {
b4f54984 13761 if (dwarf_read_debug)
80626a55
DE
13762 {
13763 fprintf_unfiltered (gdb_stdlog,
13764 "Virtual DWO %s %s found: @%s\n",
13765 kind, hex_string (signature),
13766 host_address_to_string (dwo_cutu));
13767 }
13768 return dwo_cutu;
13769 }
13770 }
13771 }
6a506a2d 13772 else
80626a55 13773 {
6a506a2d 13774 /* No DWP file, look for the DWO file. */
80626a55 13775
ed2dc618
SM
13776 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13777 dwo_name, comp_dir);
6a506a2d 13778 if (*dwo_file_slot == NULL)
80626a55 13779 {
6a506a2d
DE
13780 /* Read in the file and build a table of the CUs/TUs it contains. */
13781 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13782 }
6a506a2d 13783 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13784 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13785
6a506a2d 13786 if (dwo_file != NULL)
19c3d4c9 13787 {
6a506a2d
DE
13788 struct dwo_unit *dwo_cutu = NULL;
13789
13790 if (is_debug_types && dwo_file->tus)
13791 {
13792 struct dwo_unit find_dwo_cutu;
13793
13794 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13795 find_dwo_cutu.signature = signature;
9a3c8263
SM
13796 dwo_cutu
13797 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13798 }
33c5cd75 13799 else if (!is_debug_types && dwo_file->cus)
80626a55 13800 {
33c5cd75
DB
13801 struct dwo_unit find_dwo_cutu;
13802
13803 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13804 find_dwo_cutu.signature = signature;
13805 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13806 &find_dwo_cutu);
6a506a2d
DE
13807 }
13808
13809 if (dwo_cutu != NULL)
13810 {
b4f54984 13811 if (dwarf_read_debug)
6a506a2d
DE
13812 {
13813 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13814 kind, dwo_name, hex_string (signature),
13815 host_address_to_string (dwo_cutu));
13816 }
13817 return dwo_cutu;
80626a55
DE
13818 }
13819 }
2e276125 13820 }
9cdd5dbd 13821
80626a55
DE
13822 /* We didn't find it. This could mean a dwo_id mismatch, or
13823 someone deleted the DWO/DWP file, or the search path isn't set up
13824 correctly to find the file. */
13825
b4f54984 13826 if (dwarf_read_debug)
80626a55
DE
13827 {
13828 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13829 kind, dwo_name, hex_string (signature));
13830 }
3019eac3 13831
6656a72d
DE
13832 /* This is a warning and not a complaint because it can be caused by
13833 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13834 {
13835 /* Print the name of the DWP file if we looked there, helps the user
13836 better diagnose the problem. */
791afaa2 13837 std::string dwp_text;
43942612
DE
13838
13839 if (dwp_file != NULL)
791afaa2
TT
13840 dwp_text = string_printf (" [in DWP file %s]",
13841 lbasename (dwp_file->name));
43942612 13842
9d8780f0 13843 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
13844 " [in module %s]"),
13845 kind, dwo_name, hex_string (signature),
791afaa2 13846 dwp_text.c_str (),
43942612 13847 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 13848 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 13849 }
3019eac3 13850 return NULL;
5fb290d7
DJ
13851}
13852
80626a55
DE
13853/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13854 See lookup_dwo_cutu_unit for details. */
13855
13856static struct dwo_unit *
13857lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13858 const char *dwo_name, const char *comp_dir,
13859 ULONGEST signature)
13860{
13861 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13862}
13863
13864/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13865 See lookup_dwo_cutu_unit for details. */
13866
13867static struct dwo_unit *
13868lookup_dwo_type_unit (struct signatured_type *this_tu,
13869 const char *dwo_name, const char *comp_dir)
13870{
13871 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13872}
13873
89e63ee4
DE
13874/* Traversal function for queue_and_load_all_dwo_tus. */
13875
13876static int
13877queue_and_load_dwo_tu (void **slot, void *info)
13878{
13879 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13880 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13881 ULONGEST signature = dwo_unit->signature;
13882 struct signatured_type *sig_type =
13883 lookup_dwo_signatured_type (per_cu->cu, signature);
13884
13885 if (sig_type != NULL)
13886 {
13887 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13888
13889 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13890 a real dependency of PER_CU on SIG_TYPE. That is detected later
13891 while processing PER_CU. */
13892 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13893 load_full_type_unit (sig_cu);
13894 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13895 }
13896
13897 return 1;
13898}
13899
13900/* Queue all TUs contained in the DWO of PER_CU to be read in.
13901 The DWO may have the only definition of the type, though it may not be
13902 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13903 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13904
13905static void
13906queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13907{
13908 struct dwo_unit *dwo_unit;
13909 struct dwo_file *dwo_file;
13910
13911 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13912 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13913 gdb_assert (per_cu->cu != NULL);
13914
13915 dwo_unit = per_cu->cu->dwo_unit;
13916 gdb_assert (dwo_unit != NULL);
13917
13918 dwo_file = dwo_unit->dwo_file;
13919 if (dwo_file->tus != NULL)
13920 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13921}
13922
3019eac3
DE
13923/* Free all resources associated with DWO_FILE.
13924 Close the DWO file and munmap the sections.
13925 All memory should be on the objfile obstack. */
348e048f
DE
13926
13927static void
3019eac3 13928free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 13929{
348e048f 13930
5c6fa7ab 13931 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 13932 gdb_bfd_unref (dwo_file->dbfd);
348e048f 13933
3019eac3
DE
13934 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13935}
348e048f 13936
3019eac3 13937/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 13938
3019eac3
DE
13939static void
13940free_dwo_file_cleanup (void *arg)
13941{
ed2dc618
SM
13942 struct free_dwo_file_cleanup_data *data
13943 = (struct free_dwo_file_cleanup_data *) arg;
13944 struct objfile *objfile = data->dwarf2_per_objfile->objfile;
348e048f 13945
ed2dc618
SM
13946 free_dwo_file (data->dwo_file, objfile);
13947
13948 xfree (data);
3019eac3 13949}
348e048f 13950
3019eac3 13951/* Traversal function for free_dwo_files. */
2ab95328 13952
3019eac3
DE
13953static int
13954free_dwo_file_from_slot (void **slot, void *info)
13955{
13956 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13957 struct objfile *objfile = (struct objfile *) info;
348e048f 13958
3019eac3 13959 free_dwo_file (dwo_file, objfile);
348e048f 13960
3019eac3
DE
13961 return 1;
13962}
348e048f 13963
3019eac3 13964/* Free all resources associated with DWO_FILES. */
348e048f 13965
3019eac3
DE
13966static void
13967free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13968{
13969 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 13970}
3019eac3
DE
13971\f
13972/* Read in various DIEs. */
348e048f 13973
d389af10 13974/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13975 Inherit only the children of the DW_AT_abstract_origin DIE not being
13976 already referenced by DW_AT_abstract_origin from the children of the
13977 current DIE. */
d389af10
JK
13978
13979static void
13980inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13981{
13982 struct die_info *child_die;
791afaa2 13983 sect_offset *offsetp;
d389af10
JK
13984 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13985 struct die_info *origin_die;
13986 /* Iterator of the ORIGIN_DIE children. */
13987 struct die_info *origin_child_die;
d389af10 13988 struct attribute *attr;
cd02d79d
PA
13989 struct dwarf2_cu *origin_cu;
13990 struct pending **origin_previous_list_in_scope;
d389af10
JK
13991
13992 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13993 if (!attr)
13994 return;
13995
cd02d79d
PA
13996 /* Note that following die references may follow to a die in a
13997 different cu. */
13998
13999 origin_cu = cu;
14000 origin_die = follow_die_ref (die, attr, &origin_cu);
14001
14002 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
14003 symbols in. */
14004 origin_previous_list_in_scope = origin_cu->list_in_scope;
14005 origin_cu->list_in_scope = cu->list_in_scope;
14006
edb3359d
DJ
14007 if (die->tag != origin_die->tag
14008 && !(die->tag == DW_TAG_inlined_subroutine
14009 && origin_die->tag == DW_TAG_subprogram))
d389af10 14010 complaint (&symfile_complaints,
9d8780f0
SM
14011 _("DIE %s and its abstract origin %s have different tags"),
14012 sect_offset_str (die->sect_off),
14013 sect_offset_str (origin_die->sect_off));
d389af10 14014
791afaa2 14015 std::vector<sect_offset> offsets;
d389af10 14016
3ea89b92
PMR
14017 for (child_die = die->child;
14018 child_die && child_die->tag;
14019 child_die = sibling_die (child_die))
14020 {
14021 struct die_info *child_origin_die;
14022 struct dwarf2_cu *child_origin_cu;
14023
14024 /* We are trying to process concrete instance entries:
216f72a1 14025 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
14026 it's not relevant to our analysis here. i.e. detecting DIEs that are
14027 present in the abstract instance but not referenced in the concrete
14028 one. */
216f72a1
JK
14029 if (child_die->tag == DW_TAG_call_site
14030 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
14031 continue;
14032
c38f313d
DJ
14033 /* For each CHILD_DIE, find the corresponding child of
14034 ORIGIN_DIE. If there is more than one layer of
14035 DW_AT_abstract_origin, follow them all; there shouldn't be,
14036 but GCC versions at least through 4.4 generate this (GCC PR
14037 40573). */
3ea89b92
PMR
14038 child_origin_die = child_die;
14039 child_origin_cu = cu;
c38f313d
DJ
14040 while (1)
14041 {
cd02d79d
PA
14042 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
14043 child_origin_cu);
c38f313d
DJ
14044 if (attr == NULL)
14045 break;
cd02d79d
PA
14046 child_origin_die = follow_die_ref (child_origin_die, attr,
14047 &child_origin_cu);
c38f313d
DJ
14048 }
14049
d389af10
JK
14050 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
14051 counterpart may exist. */
c38f313d 14052 if (child_origin_die != child_die)
d389af10 14053 {
edb3359d
DJ
14054 if (child_die->tag != child_origin_die->tag
14055 && !(child_die->tag == DW_TAG_inlined_subroutine
14056 && child_origin_die->tag == DW_TAG_subprogram))
d389af10 14057 complaint (&symfile_complaints,
9d8780f0 14058 _("Child DIE %s and its abstract origin %s have "
9c541725 14059 "different tags"),
9d8780f0
SM
14060 sect_offset_str (child_die->sect_off),
14061 sect_offset_str (child_origin_die->sect_off));
c38f313d
DJ
14062 if (child_origin_die->parent != origin_die)
14063 complaint (&symfile_complaints,
9d8780f0 14064 _("Child DIE %s and its abstract origin %s have "
9c541725 14065 "different parents"),
9d8780f0
SM
14066 sect_offset_str (child_die->sect_off),
14067 sect_offset_str (child_origin_die->sect_off));
c38f313d 14068 else
791afaa2 14069 offsets.push_back (child_origin_die->sect_off);
d389af10 14070 }
d389af10 14071 }
791afaa2
TT
14072 std::sort (offsets.begin (), offsets.end ());
14073 sect_offset *offsets_end = offsets.data () + offsets.size ();
14074 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 14075 if (offsetp[-1] == *offsetp)
3e43a32a 14076 complaint (&symfile_complaints,
9d8780f0
SM
14077 _("Multiple children of DIE %s refer "
14078 "to DIE %s as their abstract origin"),
14079 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 14080
791afaa2 14081 offsetp = offsets.data ();
d389af10
JK
14082 origin_child_die = origin_die->child;
14083 while (origin_child_die && origin_child_die->tag)
14084 {
14085 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 14086 while (offsetp < offsets_end
9c541725 14087 && *offsetp < origin_child_die->sect_off)
d389af10 14088 offsetp++;
b64f50a1 14089 if (offsetp >= offsets_end
9c541725 14090 || *offsetp > origin_child_die->sect_off)
d389af10 14091 {
adde2bff
DE
14092 /* Found that ORIGIN_CHILD_DIE is really not referenced.
14093 Check whether we're already processing ORIGIN_CHILD_DIE.
14094 This can happen with mutually referenced abstract_origins.
14095 PR 16581. */
14096 if (!origin_child_die->in_process)
14097 process_die (origin_child_die, origin_cu);
d389af10
JK
14098 }
14099 origin_child_die = sibling_die (origin_child_die);
14100 }
cd02d79d 14101 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
14102}
14103
c906108c 14104static void
e7c27a73 14105read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14106{
518817b3 14107 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14108 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 14109 struct context_stack *newobj;
c906108c
SS
14110 CORE_ADDR lowpc;
14111 CORE_ADDR highpc;
14112 struct die_info *child_die;
edb3359d 14113 struct attribute *attr, *call_line, *call_file;
15d034d0 14114 const char *name;
e142c38c 14115 CORE_ADDR baseaddr;
801e3a5b 14116 struct block *block;
edb3359d 14117 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 14118 std::vector<struct symbol *> template_args;
34eaf542 14119 struct template_symbol *templ_func = NULL;
edb3359d
DJ
14120
14121 if (inlined_func)
14122 {
14123 /* If we do not have call site information, we can't show the
14124 caller of this inlined function. That's too confusing, so
14125 only use the scope for local variables. */
14126 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
14127 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
14128 if (call_line == NULL || call_file == NULL)
14129 {
14130 read_lexical_block_scope (die, cu);
14131 return;
14132 }
14133 }
c906108c 14134
e142c38c
DJ
14135 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14136
94af9270 14137 name = dwarf2_name (die, cu);
c906108c 14138
e8d05480
JB
14139 /* Ignore functions with missing or empty names. These are actually
14140 illegal according to the DWARF standard. */
14141 if (name == NULL)
14142 {
14143 complaint (&symfile_complaints,
9d8780f0
SM
14144 _("missing name for subprogram DIE at %s"),
14145 sect_offset_str (die->sect_off));
e8d05480
JB
14146 return;
14147 }
14148
14149 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 14150 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 14151 <= PC_BOUNDS_INVALID)
e8d05480 14152 {
ae4d0c03
PM
14153 attr = dwarf2_attr (die, DW_AT_external, cu);
14154 if (!attr || !DW_UNSND (attr))
14155 complaint (&symfile_complaints,
3e43a32a 14156 _("cannot get low and high bounds "
9d8780f0
SM
14157 "for subprogram DIE at %s"),
14158 sect_offset_str (die->sect_off));
e8d05480
JB
14159 return;
14160 }
c906108c 14161
3e29f34a
MR
14162 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14163 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 14164
34eaf542
TT
14165 /* If we have any template arguments, then we must allocate a
14166 different sort of symbol. */
14167 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
14168 {
14169 if (child_die->tag == DW_TAG_template_type_param
14170 || child_die->tag == DW_TAG_template_value_param)
14171 {
e623cf5d 14172 templ_func = allocate_template_symbol (objfile);
cf724bc9 14173 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
14174 break;
14175 }
14176 }
14177
fe978cb0 14178 newobj = push_context (0, lowpc);
5e2db402
TT
14179 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
14180 (struct symbol *) templ_func);
4c2df51b 14181
4cecd739
DJ
14182 /* If there is a location expression for DW_AT_frame_base, record
14183 it. */
e142c38c 14184 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 14185 if (attr)
fe978cb0 14186 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 14187
63e43d3a
PMR
14188 /* If there is a location for the static link, record it. */
14189 newobj->static_link = NULL;
14190 attr = dwarf2_attr (die, DW_AT_static_link, cu);
14191 if (attr)
14192 {
224c3ddb
SM
14193 newobj->static_link
14194 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
14195 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
14196 }
14197
e142c38c 14198 cu->list_in_scope = &local_symbols;
c906108c 14199
639d11d3 14200 if (die->child != NULL)
c906108c 14201 {
639d11d3 14202 child_die = die->child;
c906108c
SS
14203 while (child_die && child_die->tag)
14204 {
34eaf542
TT
14205 if (child_die->tag == DW_TAG_template_type_param
14206 || child_die->tag == DW_TAG_template_value_param)
14207 {
14208 struct symbol *arg = new_symbol (child_die, NULL, cu);
14209
f1078f66 14210 if (arg != NULL)
2f4732b0 14211 template_args.push_back (arg);
34eaf542
TT
14212 }
14213 else
14214 process_die (child_die, cu);
c906108c
SS
14215 child_die = sibling_die (child_die);
14216 }
14217 }
14218
d389af10
JK
14219 inherit_abstract_dies (die, cu);
14220
4a811a97
UW
14221 /* If we have a DW_AT_specification, we might need to import using
14222 directives from the context of the specification DIE. See the
14223 comment in determine_prefix. */
14224 if (cu->language == language_cplus
14225 && dwarf2_attr (die, DW_AT_specification, cu))
14226 {
14227 struct dwarf2_cu *spec_cu = cu;
14228 struct die_info *spec_die = die_specification (die, &spec_cu);
14229
14230 while (spec_die)
14231 {
14232 child_die = spec_die->child;
14233 while (child_die && child_die->tag)
14234 {
14235 if (child_die->tag == DW_TAG_imported_module)
14236 process_die (child_die, spec_cu);
14237 child_die = sibling_die (child_die);
14238 }
14239
14240 /* In some cases, GCC generates specification DIEs that
14241 themselves contain DW_AT_specification attributes. */
14242 spec_die = die_specification (spec_die, &spec_cu);
14243 }
14244 }
14245
fe978cb0 14246 newobj = pop_context ();
c906108c 14247 /* Make a block for the local symbols within. */
fe978cb0 14248 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
63e43d3a 14249 newobj->static_link, lowpc, highpc);
801e3a5b 14250
df8a16a1 14251 /* For C++, set the block's scope. */
45280282
IB
14252 if ((cu->language == language_cplus
14253 || cu->language == language_fortran
c44af4eb
TT
14254 || cu->language == language_d
14255 || cu->language == language_rust)
4d4ec4e5 14256 && cu->processing_has_namespace_info)
195a3f6c
TT
14257 block_set_scope (block, determine_prefix (die, cu),
14258 &objfile->objfile_obstack);
df8a16a1 14259
801e3a5b
JB
14260 /* If we have address ranges, record them. */
14261 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 14262
fe978cb0 14263 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 14264
34eaf542 14265 /* Attach template arguments to function. */
2f4732b0 14266 if (!template_args.empty ())
34eaf542
TT
14267 {
14268 gdb_assert (templ_func != NULL);
14269
2f4732b0 14270 templ_func->n_template_arguments = template_args.size ();
34eaf542 14271 templ_func->template_arguments
8d749320
SM
14272 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
14273 templ_func->n_template_arguments);
34eaf542 14274 memcpy (templ_func->template_arguments,
2f4732b0 14275 template_args.data (),
34eaf542 14276 (templ_func->n_template_arguments * sizeof (struct symbol *)));
34eaf542
TT
14277 }
14278
208d8187
JB
14279 /* In C++, we can have functions nested inside functions (e.g., when
14280 a function declares a class that has methods). This means that
14281 when we finish processing a function scope, we may need to go
14282 back to building a containing block's symbol lists. */
fe978cb0 14283 local_symbols = newobj->locals;
22cee43f 14284 local_using_directives = newobj->local_using_directives;
208d8187 14285
921e78cf
JB
14286 /* If we've finished processing a top-level function, subsequent
14287 symbols go in the file symbol list. */
14288 if (outermost_context_p ())
e142c38c 14289 cu->list_in_scope = &file_symbols;
c906108c
SS
14290}
14291
14292/* Process all the DIES contained within a lexical block scope. Start
14293 a new scope, process the dies, and then close the scope. */
14294
14295static void
e7c27a73 14296read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14297{
518817b3 14298 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14299 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 14300 struct context_stack *newobj;
c906108c
SS
14301 CORE_ADDR lowpc, highpc;
14302 struct die_info *child_die;
e142c38c
DJ
14303 CORE_ADDR baseaddr;
14304
14305 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
14306
14307 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
14308 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
14309 as multiple lexical blocks? Handling children in a sane way would
6e70227d 14310 be nasty. Might be easier to properly extend generic blocks to
af34e669 14311 describe ranges. */
e385593e
JK
14312 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
14313 {
14314 case PC_BOUNDS_NOT_PRESENT:
14315 /* DW_TAG_lexical_block has no attributes, process its children as if
14316 there was no wrapping by that DW_TAG_lexical_block.
14317 GCC does no longer produces such DWARF since GCC r224161. */
14318 for (child_die = die->child;
14319 child_die != NULL && child_die->tag;
14320 child_die = sibling_die (child_die))
14321 process_die (child_die, cu);
14322 return;
14323 case PC_BOUNDS_INVALID:
14324 return;
14325 }
3e29f34a
MR
14326 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14327 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
14328
14329 push_context (0, lowpc);
639d11d3 14330 if (die->child != NULL)
c906108c 14331 {
639d11d3 14332 child_die = die->child;
c906108c
SS
14333 while (child_die && child_die->tag)
14334 {
e7c27a73 14335 process_die (child_die, cu);
c906108c
SS
14336 child_die = sibling_die (child_die);
14337 }
14338 }
3ea89b92 14339 inherit_abstract_dies (die, cu);
fe978cb0 14340 newobj = pop_context ();
c906108c 14341
22cee43f 14342 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 14343 {
801e3a5b 14344 struct block *block
63e43d3a 14345 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
fe978cb0 14346 newobj->start_addr, highpc);
801e3a5b
JB
14347
14348 /* Note that recording ranges after traversing children, as we
14349 do here, means that recording a parent's ranges entails
14350 walking across all its children's ranges as they appear in
14351 the address map, which is quadratic behavior.
14352
14353 It would be nicer to record the parent's ranges before
14354 traversing its children, simply overriding whatever you find
14355 there. But since we don't even decide whether to create a
14356 block until after we've traversed its children, that's hard
14357 to do. */
14358 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 14359 }
fe978cb0 14360 local_symbols = newobj->locals;
22cee43f 14361 local_using_directives = newobj->local_using_directives;
c906108c
SS
14362}
14363
216f72a1 14364/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
14365
14366static void
14367read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
14368{
518817b3 14369 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
14370 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14371 CORE_ADDR pc, baseaddr;
14372 struct attribute *attr;
14373 struct call_site *call_site, call_site_local;
14374 void **slot;
14375 int nparams;
14376 struct die_info *child_die;
14377
14378 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14379
216f72a1
JK
14380 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
14381 if (attr == NULL)
14382 {
14383 /* This was a pre-DWARF-5 GNU extension alias
14384 for DW_AT_call_return_pc. */
14385 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14386 }
96408a79
SA
14387 if (!attr)
14388 {
14389 complaint (&symfile_complaints,
216f72a1 14390 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
14391 "DIE %s [in module %s]"),
14392 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14393 return;
14394 }
31aa7e4e 14395 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 14396 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
14397
14398 if (cu->call_site_htab == NULL)
14399 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
14400 NULL, &objfile->objfile_obstack,
14401 hashtab_obstack_allocate, NULL);
14402 call_site_local.pc = pc;
14403 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
14404 if (*slot != NULL)
14405 {
14406 complaint (&symfile_complaints,
216f72a1 14407 _("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
14408 "DIE %s [in module %s]"),
14409 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 14410 objfile_name (objfile));
96408a79
SA
14411 return;
14412 }
14413
14414 /* Count parameters at the caller. */
14415
14416 nparams = 0;
14417 for (child_die = die->child; child_die && child_die->tag;
14418 child_die = sibling_die (child_die))
14419 {
216f72a1
JK
14420 if (child_die->tag != DW_TAG_call_site_parameter
14421 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14422 {
14423 complaint (&symfile_complaints,
216f72a1 14424 _("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
14425 "DW_TAG_call_site child DIE %s [in module %s]"),
14426 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 14427 objfile_name (objfile));
96408a79
SA
14428 continue;
14429 }
14430
14431 nparams++;
14432 }
14433
224c3ddb
SM
14434 call_site
14435 = ((struct call_site *)
14436 obstack_alloc (&objfile->objfile_obstack,
14437 sizeof (*call_site)
14438 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
14439 *slot = call_site;
14440 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14441 call_site->pc = pc;
14442
216f72a1
JK
14443 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14444 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
14445 {
14446 struct die_info *func_die;
14447
14448 /* Skip also over DW_TAG_inlined_subroutine. */
14449 for (func_die = die->parent;
14450 func_die && func_die->tag != DW_TAG_subprogram
14451 && func_die->tag != DW_TAG_subroutine_type;
14452 func_die = func_die->parent);
14453
216f72a1
JK
14454 /* DW_AT_call_all_calls is a superset
14455 of DW_AT_call_all_tail_calls. */
96408a79 14456 if (func_die
216f72a1 14457 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 14458 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 14459 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
14460 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14461 {
14462 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14463 not complete. But keep CALL_SITE for look ups via call_site_htab,
14464 both the initial caller containing the real return address PC and
14465 the final callee containing the current PC of a chain of tail
14466 calls do not need to have the tail call list complete. But any
14467 function candidate for a virtual tail call frame searched via
14468 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14469 determined unambiguously. */
14470 }
14471 else
14472 {
14473 struct type *func_type = NULL;
14474
14475 if (func_die)
14476 func_type = get_die_type (func_die, cu);
14477 if (func_type != NULL)
14478 {
14479 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14480
14481 /* Enlist this call site to the function. */
14482 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14483 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14484 }
14485 else
14486 complaint (&symfile_complaints,
216f72a1 14487 _("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
14488 "DIE %s [in module %s]"),
14489 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14490 }
14491 }
14492
216f72a1
JK
14493 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14494 if (attr == NULL)
14495 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14496 if (attr == NULL)
14497 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14498 if (attr == NULL)
216f72a1
JK
14499 {
14500 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14501 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14502 }
96408a79
SA
14503 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14504 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14505 /* Keep NULL DWARF_BLOCK. */;
14506 else if (attr_form_is_block (attr))
14507 {
14508 struct dwarf2_locexpr_baton *dlbaton;
14509
8d749320 14510 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
14511 dlbaton->data = DW_BLOCK (attr)->data;
14512 dlbaton->size = DW_BLOCK (attr)->size;
14513 dlbaton->per_cu = cu->per_cu;
14514
14515 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14516 }
7771576e 14517 else if (attr_form_is_ref (attr))
96408a79 14518 {
96408a79
SA
14519 struct dwarf2_cu *target_cu = cu;
14520 struct die_info *target_die;
14521
ac9ec31b 14522 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 14523 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
14524 if (die_is_declaration (target_die, target_cu))
14525 {
7d45c7c3 14526 const char *target_physname;
9112db09
JK
14527
14528 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14529 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14530 if (target_physname == NULL)
9112db09 14531 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
14532 if (target_physname == NULL)
14533 complaint (&symfile_complaints,
216f72a1 14534 _("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14535 "physname, for referencing DIE %s [in module %s]"),
14536 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14537 else
7d455152 14538 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14539 }
14540 else
14541 {
14542 CORE_ADDR lowpc;
14543
14544 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14545 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14546 <= PC_BOUNDS_INVALID)
96408a79 14547 complaint (&symfile_complaints,
216f72a1 14548 _("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14549 "low pc, for referencing DIE %s [in module %s]"),
14550 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14551 else
3e29f34a
MR
14552 {
14553 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14554 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14555 }
96408a79
SA
14556 }
14557 }
14558 else
14559 complaint (&symfile_complaints,
216f72a1 14560 _("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14561 "block nor reference, for DIE %s [in module %s]"),
14562 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14563
14564 call_site->per_cu = cu->per_cu;
14565
14566 for (child_die = die->child;
14567 child_die && child_die->tag;
14568 child_die = sibling_die (child_die))
14569 {
96408a79 14570 struct call_site_parameter *parameter;
1788b2d3 14571 struct attribute *loc, *origin;
96408a79 14572
216f72a1
JK
14573 if (child_die->tag != DW_TAG_call_site_parameter
14574 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14575 {
14576 /* Already printed the complaint above. */
14577 continue;
14578 }
14579
14580 gdb_assert (call_site->parameter_count < nparams);
14581 parameter = &call_site->parameter[call_site->parameter_count];
14582
1788b2d3
JK
14583 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14584 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14585 register is contained in DW_AT_call_value. */
96408a79 14586
24c5c679 14587 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14588 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14589 if (origin == NULL)
14590 {
14591 /* This was a pre-DWARF-5 GNU extension alias
14592 for DW_AT_call_parameter. */
14593 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14594 }
7771576e 14595 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14596 {
1788b2d3 14597 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14598
14599 sect_offset sect_off
14600 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14601 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14602 {
14603 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14604 binding can be done only inside one CU. Such referenced DIE
14605 therefore cannot be even moved to DW_TAG_partial_unit. */
14606 complaint (&symfile_complaints,
216f72a1 14607 _("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14608 "DW_TAG_call_site child DIE %s [in module %s]"),
14609 sect_offset_str (child_die->sect_off),
9c541725 14610 objfile_name (objfile));
d76b7dbc
JK
14611 continue;
14612 }
9c541725
PA
14613 parameter->u.param_cu_off
14614 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14615 }
14616 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
14617 {
14618 complaint (&symfile_complaints,
14619 _("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14620 "DW_TAG_call_site child DIE %s [in module %s]"),
14621 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14622 continue;
14623 }
24c5c679 14624 else
96408a79 14625 {
24c5c679
JK
14626 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14627 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14628 if (parameter->u.dwarf_reg != -1)
14629 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14630 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14631 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14632 &parameter->u.fb_offset))
14633 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14634 else
14635 {
14636 complaint (&symfile_complaints,
14637 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14638 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14639 "DW_TAG_call_site child DIE %s "
24c5c679 14640 "[in module %s]"),
9d8780f0 14641 sect_offset_str (child_die->sect_off),
9c541725 14642 objfile_name (objfile));
24c5c679
JK
14643 continue;
14644 }
96408a79
SA
14645 }
14646
216f72a1
JK
14647 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14648 if (attr == NULL)
14649 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14650 if (!attr_form_is_block (attr))
14651 {
14652 complaint (&symfile_complaints,
216f72a1 14653 _("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14654 "DW_TAG_call_site child DIE %s [in module %s]"),
14655 sect_offset_str (child_die->sect_off),
9c541725 14656 objfile_name (objfile));
96408a79
SA
14657 continue;
14658 }
14659 parameter->value = DW_BLOCK (attr)->data;
14660 parameter->value_size = DW_BLOCK (attr)->size;
14661
14662 /* Parameters are not pre-cleared by memset above. */
14663 parameter->data_value = NULL;
14664 parameter->data_value_size = 0;
14665 call_site->parameter_count++;
14666
216f72a1
JK
14667 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14668 if (attr == NULL)
14669 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
14670 if (attr)
14671 {
14672 if (!attr_form_is_block (attr))
14673 complaint (&symfile_complaints,
216f72a1 14674 _("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14675 "DW_TAG_call_site child DIE %s [in module %s]"),
14676 sect_offset_str (child_die->sect_off),
9c541725 14677 objfile_name (objfile));
96408a79
SA
14678 else
14679 {
14680 parameter->data_value = DW_BLOCK (attr)->data;
14681 parameter->data_value_size = DW_BLOCK (attr)->size;
14682 }
14683 }
14684 }
14685}
14686
71a3c369
TT
14687/* Helper function for read_variable. If DIE represents a virtual
14688 table, then return the type of the concrete object that is
14689 associated with the virtual table. Otherwise, return NULL. */
14690
14691static struct type *
14692rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14693{
14694 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14695 if (attr == NULL)
14696 return NULL;
14697
14698 /* Find the type DIE. */
14699 struct die_info *type_die = NULL;
14700 struct dwarf2_cu *type_cu = cu;
14701
14702 if (attr_form_is_ref (attr))
14703 type_die = follow_die_ref (die, attr, &type_cu);
14704 if (type_die == NULL)
14705 return NULL;
14706
14707 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14708 return NULL;
14709 return die_containing_type (type_die, type_cu);
14710}
14711
14712/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14713
14714static void
14715read_variable (struct die_info *die, struct dwarf2_cu *cu)
14716{
14717 struct rust_vtable_symbol *storage = NULL;
14718
14719 if (cu->language == language_rust)
14720 {
14721 struct type *containing_type = rust_containing_type (die, cu);
14722
14723 if (containing_type != NULL)
14724 {
518817b3 14725 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369
TT
14726
14727 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14728 struct rust_vtable_symbol);
14729 initialize_objfile_symbol (storage);
14730 storage->concrete_type = containing_type;
cf724bc9 14731 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14732 }
14733 }
14734
5e2db402 14735 new_symbol (die, NULL, cu, storage);
71a3c369
TT
14736}
14737
43988095
JK
14738/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14739 reading .debug_rnglists.
14740 Callback's type should be:
14741 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14742 Return true if the attributes are present and valid, otherwise,
14743 return false. */
14744
14745template <typename Callback>
14746static bool
14747dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14748 Callback &&callback)
14749{
ed2dc618 14750 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14751 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14752 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 14753 bfd *obfd = objfile->obfd;
43988095
JK
14754 /* Base address selection entry. */
14755 CORE_ADDR base;
14756 int found_base;
43988095 14757 const gdb_byte *buffer;
43988095
JK
14758 CORE_ADDR baseaddr;
14759 bool overflow = false;
14760
14761 found_base = cu->base_known;
14762 base = cu->base_address;
14763
14764 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14765 if (offset >= dwarf2_per_objfile->rnglists.size)
14766 {
14767 complaint (&symfile_complaints,
14768 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14769 offset);
14770 return false;
14771 }
14772 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14773
14774 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14775
14776 while (1)
14777 {
7814882a
JK
14778 /* Initialize it due to a false compiler warning. */
14779 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14780 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14781 + dwarf2_per_objfile->rnglists.size);
14782 unsigned int bytes_read;
14783
14784 if (buffer == buf_end)
14785 {
14786 overflow = true;
14787 break;
14788 }
14789 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14790 switch (rlet)
14791 {
14792 case DW_RLE_end_of_list:
14793 break;
14794 case DW_RLE_base_address:
14795 if (buffer + cu->header.addr_size > buf_end)
14796 {
14797 overflow = true;
14798 break;
14799 }
14800 base = read_address (obfd, buffer, cu, &bytes_read);
14801 found_base = 1;
14802 buffer += bytes_read;
14803 break;
14804 case DW_RLE_start_length:
14805 if (buffer + cu->header.addr_size > buf_end)
14806 {
14807 overflow = true;
14808 break;
14809 }
14810 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14811 buffer += bytes_read;
14812 range_end = (range_beginning
14813 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14814 buffer += bytes_read;
14815 if (buffer > buf_end)
14816 {
14817 overflow = true;
14818 break;
14819 }
14820 break;
14821 case DW_RLE_offset_pair:
14822 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14823 buffer += bytes_read;
14824 if (buffer > buf_end)
14825 {
14826 overflow = true;
14827 break;
14828 }
14829 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14830 buffer += bytes_read;
14831 if (buffer > buf_end)
14832 {
14833 overflow = true;
14834 break;
14835 }
14836 break;
14837 case DW_RLE_start_end:
14838 if (buffer + 2 * cu->header.addr_size > buf_end)
14839 {
14840 overflow = true;
14841 break;
14842 }
14843 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14844 buffer += bytes_read;
14845 range_end = read_address (obfd, buffer, cu, &bytes_read);
14846 buffer += bytes_read;
14847 break;
14848 default:
14849 complaint (&symfile_complaints,
14850 _("Invalid .debug_rnglists data (no base address)"));
14851 return false;
14852 }
14853 if (rlet == DW_RLE_end_of_list || overflow)
14854 break;
14855 if (rlet == DW_RLE_base_address)
14856 continue;
14857
14858 if (!found_base)
14859 {
14860 /* We have no valid base address for the ranges
14861 data. */
14862 complaint (&symfile_complaints,
14863 _("Invalid .debug_rnglists data (no base address)"));
14864 return false;
14865 }
14866
14867 if (range_beginning > range_end)
14868 {
14869 /* Inverted range entries are invalid. */
14870 complaint (&symfile_complaints,
14871 _("Invalid .debug_rnglists data (inverted range)"));
14872 return false;
14873 }
14874
14875 /* Empty range entries have no effect. */
14876 if (range_beginning == range_end)
14877 continue;
14878
14879 range_beginning += base;
14880 range_end += base;
14881
14882 /* A not-uncommon case of bad debug info.
14883 Don't pollute the addrmap with bad data. */
14884 if (range_beginning + baseaddr == 0
14885 && !dwarf2_per_objfile->has_section_at_zero)
14886 {
14887 complaint (&symfile_complaints,
14888 _(".debug_rnglists entry has start address of zero"
14889 " [in module %s]"), objfile_name (objfile));
14890 continue;
14891 }
14892
14893 callback (range_beginning, range_end);
14894 }
14895
14896 if (overflow)
14897 {
14898 complaint (&symfile_complaints,
14899 _("Offset %d is not terminated "
14900 "for DW_AT_ranges attribute"),
14901 offset);
14902 return false;
14903 }
14904
14905 return true;
14906}
14907
14908/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14909 Callback's type should be:
14910 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14911 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14912
43988095 14913template <typename Callback>
43039443 14914static int
5f46c5a5 14915dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14916 Callback &&callback)
43039443 14917{
ed2dc618 14918 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14919 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14920 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14921 struct comp_unit_head *cu_header = &cu->header;
14922 bfd *obfd = objfile->obfd;
14923 unsigned int addr_size = cu_header->addr_size;
14924 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14925 /* Base address selection entry. */
14926 CORE_ADDR base;
14927 int found_base;
14928 unsigned int dummy;
d521ce57 14929 const gdb_byte *buffer;
ff013f42 14930 CORE_ADDR baseaddr;
43039443 14931
43988095
JK
14932 if (cu_header->version >= 5)
14933 return dwarf2_rnglists_process (offset, cu, callback);
14934
d00adf39
DE
14935 found_base = cu->base_known;
14936 base = cu->base_address;
43039443 14937
be391dca 14938 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14939 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
14940 {
14941 complaint (&symfile_complaints,
14942 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14943 offset);
14944 return 0;
14945 }
dce234bc 14946 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14947
e7030f15 14948 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 14949
43039443
JK
14950 while (1)
14951 {
14952 CORE_ADDR range_beginning, range_end;
14953
14954 range_beginning = read_address (obfd, buffer, cu, &dummy);
14955 buffer += addr_size;
14956 range_end = read_address (obfd, buffer, cu, &dummy);
14957 buffer += addr_size;
14958 offset += 2 * addr_size;
14959
14960 /* An end of list marker is a pair of zero addresses. */
14961 if (range_beginning == 0 && range_end == 0)
14962 /* Found the end of list entry. */
14963 break;
14964
14965 /* Each base address selection entry is a pair of 2 values.
14966 The first is the largest possible address, the second is
14967 the base address. Check for a base address here. */
14968 if ((range_beginning & mask) == mask)
14969 {
28d2bfb9
AB
14970 /* If we found the largest possible address, then we already
14971 have the base address in range_end. */
14972 base = range_end;
43039443
JK
14973 found_base = 1;
14974 continue;
14975 }
14976
14977 if (!found_base)
14978 {
14979 /* We have no valid base address for the ranges
14980 data. */
14981 complaint (&symfile_complaints,
14982 _("Invalid .debug_ranges data (no base address)"));
14983 return 0;
14984 }
14985
9277c30c
UW
14986 if (range_beginning > range_end)
14987 {
14988 /* Inverted range entries are invalid. */
14989 complaint (&symfile_complaints,
14990 _("Invalid .debug_ranges data (inverted range)"));
14991 return 0;
14992 }
14993
14994 /* Empty range entries have no effect. */
14995 if (range_beginning == range_end)
14996 continue;
14997
43039443
JK
14998 range_beginning += base;
14999 range_end += base;
15000
01093045
DE
15001 /* A not-uncommon case of bad debug info.
15002 Don't pollute the addrmap with bad data. */
15003 if (range_beginning + baseaddr == 0
15004 && !dwarf2_per_objfile->has_section_at_zero)
15005 {
15006 complaint (&symfile_complaints,
15007 _(".debug_ranges entry has start address of zero"
4262abfb 15008 " [in module %s]"), objfile_name (objfile));
01093045
DE
15009 continue;
15010 }
15011
5f46c5a5
JK
15012 callback (range_beginning, range_end);
15013 }
15014
15015 return 1;
15016}
15017
15018/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
15019 Return 1 if the attributes are present and valid, otherwise, return 0.
15020 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
15021
15022static int
15023dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
15024 CORE_ADDR *high_return, struct dwarf2_cu *cu,
15025 struct partial_symtab *ranges_pst)
15026{
518817b3 15027 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5
JK
15028 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15029 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
15030 SECT_OFF_TEXT (objfile));
15031 int low_set = 0;
15032 CORE_ADDR low = 0;
15033 CORE_ADDR high = 0;
15034 int retval;
15035
15036 retval = dwarf2_ranges_process (offset, cu,
15037 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
15038 {
9277c30c 15039 if (ranges_pst != NULL)
3e29f34a
MR
15040 {
15041 CORE_ADDR lowpc;
15042 CORE_ADDR highpc;
15043
15044 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
15045 range_beginning + baseaddr);
15046 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
15047 range_end + baseaddr);
15048 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
15049 ranges_pst);
15050 }
ff013f42 15051
43039443
JK
15052 /* FIXME: This is recording everything as a low-high
15053 segment of consecutive addresses. We should have a
15054 data structure for discontiguous block ranges
15055 instead. */
15056 if (! low_set)
15057 {
15058 low = range_beginning;
15059 high = range_end;
15060 low_set = 1;
15061 }
15062 else
15063 {
15064 if (range_beginning < low)
15065 low = range_beginning;
15066 if (range_end > high)
15067 high = range_end;
15068 }
5f46c5a5
JK
15069 });
15070 if (!retval)
15071 return 0;
43039443
JK
15072
15073 if (! low_set)
15074 /* If the first entry is an end-of-list marker, the range
15075 describes an empty scope, i.e. no instructions. */
15076 return 0;
15077
15078 if (low_return)
15079 *low_return = low;
15080 if (high_return)
15081 *high_return = high;
15082 return 1;
15083}
15084
3a2b436a
JK
15085/* Get low and high pc attributes from a die. See enum pc_bounds_kind
15086 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 15087 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 15088
3a2b436a 15089static enum pc_bounds_kind
af34e669 15090dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
15091 CORE_ADDR *highpc, struct dwarf2_cu *cu,
15092 struct partial_symtab *pst)
c906108c 15093{
518817b3
SM
15094 struct dwarf2_per_objfile *dwarf2_per_objfile
15095 = cu->per_cu->dwarf2_per_objfile;
c906108c 15096 struct attribute *attr;
91da1414 15097 struct attribute *attr_high;
af34e669
DJ
15098 CORE_ADDR low = 0;
15099 CORE_ADDR high = 0;
e385593e 15100 enum pc_bounds_kind ret;
c906108c 15101
91da1414
MW
15102 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
15103 if (attr_high)
af34e669 15104 {
e142c38c 15105 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 15106 if (attr)
91da1414 15107 {
31aa7e4e
JB
15108 low = attr_value_as_address (attr);
15109 high = attr_value_as_address (attr_high);
15110 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
15111 high += low;
91da1414 15112 }
af34e669
DJ
15113 else
15114 /* Found high w/o low attribute. */
e385593e 15115 return PC_BOUNDS_INVALID;
af34e669
DJ
15116
15117 /* Found consecutive range of addresses. */
3a2b436a 15118 ret = PC_BOUNDS_HIGH_LOW;
af34e669 15119 }
c906108c 15120 else
af34e669 15121 {
e142c38c 15122 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
15123 if (attr != NULL)
15124 {
ab435259
DE
15125 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
15126 We take advantage of the fact that DW_AT_ranges does not appear
15127 in DW_TAG_compile_unit of DWO files. */
15128 int need_ranges_base = die->tag != DW_TAG_compile_unit;
15129 unsigned int ranges_offset = (DW_UNSND (attr)
15130 + (need_ranges_base
15131 ? cu->ranges_base
15132 : 0));
2e3cf129 15133
af34e669 15134 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 15135 .debug_ranges section. */
2e3cf129 15136 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 15137 return PC_BOUNDS_INVALID;
43039443 15138 /* Found discontinuous range of addresses. */
3a2b436a 15139 ret = PC_BOUNDS_RANGES;
af34e669 15140 }
e385593e
JK
15141 else
15142 return PC_BOUNDS_NOT_PRESENT;
af34e669 15143 }
c906108c 15144
48fbe735 15145 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 15146 if (high <= low)
e385593e 15147 return PC_BOUNDS_INVALID;
c906108c
SS
15148
15149 /* When using the GNU linker, .gnu.linkonce. sections are used to
15150 eliminate duplicate copies of functions and vtables and such.
15151 The linker will arbitrarily choose one and discard the others.
15152 The AT_*_pc values for such functions refer to local labels in
15153 these sections. If the section from that file was discarded, the
15154 labels are not in the output, so the relocs get a value of 0.
15155 If this is a discarded function, mark the pc bounds as invalid,
15156 so that GDB will ignore it. */
72dca2f5 15157 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 15158 return PC_BOUNDS_INVALID;
c906108c
SS
15159
15160 *lowpc = low;
96408a79
SA
15161 if (highpc)
15162 *highpc = high;
af34e669 15163 return ret;
c906108c
SS
15164}
15165
b084d499
JB
15166/* Assuming that DIE represents a subprogram DIE or a lexical block, get
15167 its low and high PC addresses. Do nothing if these addresses could not
15168 be determined. Otherwise, set LOWPC to the low address if it is smaller,
15169 and HIGHPC to the high address if greater than HIGHPC. */
15170
15171static void
15172dwarf2_get_subprogram_pc_bounds (struct die_info *die,
15173 CORE_ADDR *lowpc, CORE_ADDR *highpc,
15174 struct dwarf2_cu *cu)
15175{
15176 CORE_ADDR low, high;
15177 struct die_info *child = die->child;
15178
e385593e 15179 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 15180 {
325fac50
PA
15181 *lowpc = std::min (*lowpc, low);
15182 *highpc = std::max (*highpc, high);
b084d499
JB
15183 }
15184
15185 /* If the language does not allow nested subprograms (either inside
15186 subprograms or lexical blocks), we're done. */
15187 if (cu->language != language_ada)
15188 return;
6e70227d 15189
b084d499
JB
15190 /* Check all the children of the given DIE. If it contains nested
15191 subprograms, then check their pc bounds. Likewise, we need to
15192 check lexical blocks as well, as they may also contain subprogram
15193 definitions. */
15194 while (child && child->tag)
15195 {
15196 if (child->tag == DW_TAG_subprogram
15197 || child->tag == DW_TAG_lexical_block)
15198 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
15199 child = sibling_die (child);
15200 }
15201}
15202
fae299cd
DC
15203/* Get the low and high pc's represented by the scope DIE, and store
15204 them in *LOWPC and *HIGHPC. If the correct values can't be
15205 determined, set *LOWPC to -1 and *HIGHPC to 0. */
15206
15207static void
15208get_scope_pc_bounds (struct die_info *die,
15209 CORE_ADDR *lowpc, CORE_ADDR *highpc,
15210 struct dwarf2_cu *cu)
15211{
15212 CORE_ADDR best_low = (CORE_ADDR) -1;
15213 CORE_ADDR best_high = (CORE_ADDR) 0;
15214 CORE_ADDR current_low, current_high;
15215
3a2b436a 15216 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 15217 >= PC_BOUNDS_RANGES)
fae299cd
DC
15218 {
15219 best_low = current_low;
15220 best_high = current_high;
15221 }
15222 else
15223 {
15224 struct die_info *child = die->child;
15225
15226 while (child && child->tag)
15227 {
15228 switch (child->tag) {
15229 case DW_TAG_subprogram:
b084d499 15230 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
15231 break;
15232 case DW_TAG_namespace:
f55ee35c 15233 case DW_TAG_module:
fae299cd
DC
15234 /* FIXME: carlton/2004-01-16: Should we do this for
15235 DW_TAG_class_type/DW_TAG_structure_type, too? I think
15236 that current GCC's always emit the DIEs corresponding
15237 to definitions of methods of classes as children of a
15238 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
15239 the DIEs giving the declarations, which could be
15240 anywhere). But I don't see any reason why the
15241 standards says that they have to be there. */
15242 get_scope_pc_bounds (child, &current_low, &current_high, cu);
15243
15244 if (current_low != ((CORE_ADDR) -1))
15245 {
325fac50
PA
15246 best_low = std::min (best_low, current_low);
15247 best_high = std::max (best_high, current_high);
fae299cd
DC
15248 }
15249 break;
15250 default:
0963b4bd 15251 /* Ignore. */
fae299cd
DC
15252 break;
15253 }
15254
15255 child = sibling_die (child);
15256 }
15257 }
15258
15259 *lowpc = best_low;
15260 *highpc = best_high;
15261}
15262
801e3a5b
JB
15263/* Record the address ranges for BLOCK, offset by BASEADDR, as given
15264 in DIE. */
380bca97 15265
801e3a5b
JB
15266static void
15267dwarf2_record_block_ranges (struct die_info *die, struct block *block,
15268 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
15269{
518817b3 15270 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 15271 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 15272 struct attribute *attr;
91da1414 15273 struct attribute *attr_high;
801e3a5b 15274
91da1414
MW
15275 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
15276 if (attr_high)
801e3a5b 15277 {
801e3a5b
JB
15278 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
15279 if (attr)
15280 {
31aa7e4e
JB
15281 CORE_ADDR low = attr_value_as_address (attr);
15282 CORE_ADDR high = attr_value_as_address (attr_high);
15283
15284 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
15285 high += low;
9a619af0 15286
3e29f34a
MR
15287 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
15288 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
15289 record_block_range (block, low, high - 1);
801e3a5b
JB
15290 }
15291 }
15292
15293 attr = dwarf2_attr (die, DW_AT_ranges, cu);
15294 if (attr)
15295 {
ab435259
DE
15296 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
15297 We take advantage of the fact that DW_AT_ranges does not appear
15298 in DW_TAG_compile_unit of DWO files. */
15299 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
15300
15301 /* The value of the DW_AT_ranges attribute is the offset of the
15302 address range list in the .debug_ranges section. */
ab435259
DE
15303 unsigned long offset = (DW_UNSND (attr)
15304 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 15305 const gdb_byte *buffer;
801e3a5b
JB
15306
15307 /* For some target architectures, but not others, the
15308 read_address function sign-extends the addresses it returns.
15309 To recognize base address selection entries, we need a
15310 mask. */
15311 unsigned int addr_size = cu->header.addr_size;
15312 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
15313
15314 /* The base address, to which the next pair is relative. Note
15315 that this 'base' is a DWARF concept: most entries in a range
15316 list are relative, to reduce the number of relocs against the
15317 debugging information. This is separate from this function's
15318 'baseaddr' argument, which GDB uses to relocate debugging
15319 information from a shared library based on the address at
15320 which the library was loaded. */
d00adf39
DE
15321 CORE_ADDR base = cu->base_address;
15322 int base_known = cu->base_known;
801e3a5b 15323
5f46c5a5
JK
15324 dwarf2_ranges_process (offset, cu,
15325 [&] (CORE_ADDR start, CORE_ADDR end)
15326 {
58fdfd2c
JK
15327 start += baseaddr;
15328 end += baseaddr;
5f46c5a5
JK
15329 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
15330 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
15331 record_block_range (block, start, end - 1);
15332 });
801e3a5b
JB
15333 }
15334}
15335
685b1105
JK
15336/* Check whether the producer field indicates either of GCC < 4.6, or the
15337 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 15338
685b1105
JK
15339static void
15340check_producer (struct dwarf2_cu *cu)
60d5a603 15341{
38360086 15342 int major, minor;
60d5a603
JK
15343
15344 if (cu->producer == NULL)
15345 {
15346 /* For unknown compilers expect their behavior is DWARF version
15347 compliant.
15348
15349 GCC started to support .debug_types sections by -gdwarf-4 since
15350 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
15351 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
15352 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
15353 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 15354 }
b1ffba5a 15355 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 15356 {
38360086
MW
15357 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
15358 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 15359 }
5230b05a
WT
15360 else if (producer_is_icc (cu->producer, &major, &minor))
15361 cu->producer_is_icc_lt_14 = major < 14;
685b1105
JK
15362 else
15363 {
15364 /* For other non-GCC compilers, expect their behavior is DWARF version
15365 compliant. */
60d5a603
JK
15366 }
15367
ba919b58 15368 cu->checked_producer = 1;
685b1105 15369}
ba919b58 15370
685b1105
JK
15371/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
15372 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
15373 during 4.6.0 experimental. */
15374
15375static int
15376producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
15377{
15378 if (!cu->checked_producer)
15379 check_producer (cu);
15380
15381 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
15382}
15383
15384/* Return the default accessibility type if it is not overriden by
15385 DW_AT_accessibility. */
15386
15387static enum dwarf_access_attribute
15388dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
15389{
15390 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15391 {
15392 /* The default DWARF 2 accessibility for members is public, the default
15393 accessibility for inheritance is private. */
15394
15395 if (die->tag != DW_TAG_inheritance)
15396 return DW_ACCESS_public;
15397 else
15398 return DW_ACCESS_private;
15399 }
15400 else
15401 {
15402 /* DWARF 3+ defines the default accessibility a different way. The same
15403 rules apply now for DW_TAG_inheritance as for the members and it only
15404 depends on the container kind. */
15405
15406 if (die->parent->tag == DW_TAG_class_type)
15407 return DW_ACCESS_private;
15408 else
15409 return DW_ACCESS_public;
15410 }
15411}
15412
74ac6d43
TT
15413/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15414 offset. If the attribute was not found return 0, otherwise return
15415 1. If it was found but could not properly be handled, set *OFFSET
15416 to 0. */
15417
15418static int
15419handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15420 LONGEST *offset)
15421{
15422 struct attribute *attr;
15423
15424 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15425 if (attr != NULL)
15426 {
15427 *offset = 0;
15428
15429 /* Note that we do not check for a section offset first here.
15430 This is because DW_AT_data_member_location is new in DWARF 4,
15431 so if we see it, we can assume that a constant form is really
15432 a constant and not a section offset. */
15433 if (attr_form_is_constant (attr))
15434 *offset = dwarf2_get_attr_constant_value (attr, 0);
15435 else if (attr_form_is_section_offset (attr))
15436 dwarf2_complex_location_expr_complaint ();
15437 else if (attr_form_is_block (attr))
15438 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15439 else
15440 dwarf2_complex_location_expr_complaint ();
15441
15442 return 1;
15443 }
15444
15445 return 0;
15446}
15447
c906108c
SS
15448/* Add an aggregate field to the field list. */
15449
15450static void
107d2387 15451dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 15452 struct dwarf2_cu *cu)
6e70227d 15453{
518817b3 15454 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 15455 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
15456 struct nextfield *new_field;
15457 struct attribute *attr;
15458 struct field *fp;
15d034d0 15459 const char *fieldname = "";
c906108c 15460
7d0ccb61
DJ
15461 if (die->tag == DW_TAG_inheritance)
15462 {
be2daae6
TT
15463 fip->baseclasses.emplace_back ();
15464 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
15465 }
15466 else
15467 {
be2daae6
TT
15468 fip->fields.emplace_back ();
15469 new_field = &fip->fields.back ();
7d0ccb61 15470 }
be2daae6 15471
c906108c
SS
15472 fip->nfields++;
15473
e142c38c 15474 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
15475 if (attr)
15476 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
15477 else
15478 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
15479 if (new_field->accessibility != DW_ACCESS_public)
15480 fip->non_public_fields = 1;
60d5a603 15481
e142c38c 15482 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
15483 if (attr)
15484 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
15485 else
15486 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15487
15488 fp = &new_field->field;
a9a9bd0f 15489
e142c38c 15490 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15491 {
74ac6d43
TT
15492 LONGEST offset;
15493
a9a9bd0f 15494 /* Data member other than a C++ static data member. */
6e70227d 15495
c906108c 15496 /* Get type of field. */
e7c27a73 15497 fp->type = die_type (die, cu);
c906108c 15498
d6a843b5 15499 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15500
c906108c 15501 /* Get bit size of field (zero if none). */
e142c38c 15502 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
15503 if (attr)
15504 {
15505 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15506 }
15507 else
15508 {
15509 FIELD_BITSIZE (*fp) = 0;
15510 }
15511
15512 /* Get bit offset of field. */
74ac6d43
TT
15513 if (handle_data_member_location (die, cu, &offset))
15514 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 15515 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
15516 if (attr)
15517 {
5e2b427d 15518 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
15519 {
15520 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
15521 additional bit offset from the MSB of the containing
15522 anonymous object to the MSB of the field. We don't
15523 have to do anything special since we don't need to
15524 know the size of the anonymous object. */
f41f5e61 15525 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
15526 }
15527 else
15528 {
15529 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
15530 MSB of the anonymous object, subtract off the number of
15531 bits from the MSB of the field to the MSB of the
15532 object, and then subtract off the number of bits of
15533 the field itself. The result is the bit offset of
15534 the LSB of the field. */
c906108c
SS
15535 int anonymous_size;
15536 int bit_offset = DW_UNSND (attr);
15537
e142c38c 15538 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15539 if (attr)
15540 {
15541 /* The size of the anonymous object containing
15542 the bit field is explicit, so use the
15543 indicated size (in bytes). */
15544 anonymous_size = DW_UNSND (attr);
15545 }
15546 else
15547 {
15548 /* The size of the anonymous object containing
15549 the bit field must be inferred from the type
15550 attribute of the data member containing the
15551 bit field. */
15552 anonymous_size = TYPE_LENGTH (fp->type);
15553 }
f41f5e61
PA
15554 SET_FIELD_BITPOS (*fp,
15555 (FIELD_BITPOS (*fp)
15556 + anonymous_size * bits_per_byte
15557 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15558 }
15559 }
da5b30da
AA
15560 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15561 if (attr != NULL)
15562 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15563 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
15564
15565 /* Get name of field. */
39cbfefa
DJ
15566 fieldname = dwarf2_name (die, cu);
15567 if (fieldname == NULL)
15568 fieldname = "";
d8151005
DJ
15569
15570 /* The name is already allocated along with this objfile, so we don't
15571 need to duplicate it for the type. */
15572 fp->name = fieldname;
c906108c
SS
15573
15574 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15575 pointer or virtual base class pointer) to private. */
e142c38c 15576 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15577 {
d48cc9dd 15578 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
15579 new_field->accessibility = DW_ACCESS_private;
15580 fip->non_public_fields = 1;
15581 }
15582 }
a9a9bd0f 15583 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15584 {
a9a9bd0f
DC
15585 /* C++ static member. */
15586
15587 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15588 is a declaration, but all versions of G++ as of this writing
15589 (so through at least 3.2.1) incorrectly generate
15590 DW_TAG_variable tags. */
6e70227d 15591
ff355380 15592 const char *physname;
c906108c 15593
a9a9bd0f 15594 /* Get name of field. */
39cbfefa
DJ
15595 fieldname = dwarf2_name (die, cu);
15596 if (fieldname == NULL)
c906108c
SS
15597 return;
15598
254e6b9e 15599 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15600 if (attr
15601 /* Only create a symbol if this is an external value.
15602 new_symbol checks this and puts the value in the global symbol
15603 table, which we want. If it is not external, new_symbol
15604 will try to put the value in cu->list_in_scope which is wrong. */
15605 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15606 {
15607 /* A static const member, not much different than an enum as far as
15608 we're concerned, except that we can support more types. */
15609 new_symbol (die, NULL, cu);
15610 }
15611
2df3850c 15612 /* Get physical name. */
ff355380 15613 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15614
d8151005
DJ
15615 /* The name is already allocated along with this objfile, so we don't
15616 need to duplicate it for the type. */
15617 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15618 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15619 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15620 }
15621 else if (die->tag == DW_TAG_inheritance)
15622 {
74ac6d43 15623 LONGEST offset;
d4b96c9a 15624
74ac6d43
TT
15625 /* C++ base class field. */
15626 if (handle_data_member_location (die, cu, &offset))
15627 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15628 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15629 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c 15630 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
c906108c 15631 }
2ddeaf8a
TT
15632 else if (die->tag == DW_TAG_variant_part)
15633 {
15634 /* process_structure_scope will treat this DIE as a union. */
15635 process_structure_scope (die, cu);
15636
15637 /* The variant part is relative to the start of the enclosing
15638 structure. */
15639 SET_FIELD_BITPOS (*fp, 0);
15640 fp->type = get_die_type (die, cu);
15641 fp->artificial = 1;
15642 fp->name = "<<variant>>";
15643 }
15644 else
15645 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15646}
15647
883fd55a
KS
15648/* Can the type given by DIE define another type? */
15649
15650static bool
15651type_can_define_types (const struct die_info *die)
15652{
15653 switch (die->tag)
15654 {
15655 case DW_TAG_typedef:
15656 case DW_TAG_class_type:
15657 case DW_TAG_structure_type:
15658 case DW_TAG_union_type:
15659 case DW_TAG_enumeration_type:
15660 return true;
15661
15662 default:
15663 return false;
15664 }
15665}
15666
15667/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15668
15669static void
883fd55a
KS
15670dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15671 struct dwarf2_cu *cu)
6e70227d 15672{
be2daae6
TT
15673 struct decl_field fp;
15674 memset (&fp, 0, sizeof (fp));
98751a41 15675
883fd55a 15676 gdb_assert (type_can_define_types (die));
98751a41 15677
883fd55a 15678 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15679 fp.name = dwarf2_name (die, cu);
15680 fp.type = read_type_die (die, cu);
98751a41 15681
c191a687
KS
15682 /* Save accessibility. */
15683 enum dwarf_access_attribute accessibility;
15684 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15685 if (attr != NULL)
15686 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15687 else
15688 accessibility = dwarf2_default_access_attribute (die, cu);
15689 switch (accessibility)
15690 {
15691 case DW_ACCESS_public:
15692 /* The assumed value if neither private nor protected. */
15693 break;
15694 case DW_ACCESS_private:
be2daae6 15695 fp.is_private = 1;
c191a687
KS
15696 break;
15697 case DW_ACCESS_protected:
be2daae6 15698 fp.is_protected = 1;
c191a687
KS
15699 break;
15700 default:
37534686
KS
15701 complaint (&symfile_complaints,
15702 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15703 }
15704
883fd55a 15705 if (die->tag == DW_TAG_typedef)
be2daae6 15706 fip->typedef_field_list.push_back (fp);
883fd55a 15707 else
be2daae6 15708 fip->nested_types_list.push_back (fp);
98751a41
JK
15709}
15710
c906108c
SS
15711/* Create the vector of fields, and attach it to the type. */
15712
15713static void
fba45db2 15714dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15715 struct dwarf2_cu *cu)
c906108c
SS
15716{
15717 int nfields = fip->nfields;
15718
15719 /* Record the field count, allocate space for the array of fields,
15720 and create blank accessibility bitfields if necessary. */
15721 TYPE_NFIELDS (type) = nfields;
15722 TYPE_FIELDS (type) = (struct field *)
be2daae6 15723 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 15724
b4ba55a1 15725 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15726 {
15727 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15728
15729 TYPE_FIELD_PRIVATE_BITS (type) =
15730 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15731 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15732
15733 TYPE_FIELD_PROTECTED_BITS (type) =
15734 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15735 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15736
774b6a14
TT
15737 TYPE_FIELD_IGNORE_BITS (type) =
15738 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15739 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15740 }
15741
15742 /* If the type has baseclasses, allocate and clear a bit vector for
15743 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15744 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15745 {
be2daae6 15746 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15747 unsigned char *pointer;
c906108c
SS
15748
15749 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15750 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15751 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15752 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15753 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15754 }
15755
2ddeaf8a
TT
15756 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15757 {
15758 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15759
be2daae6 15760 for (int index = 0; index < nfields; ++index)
2ddeaf8a 15761 {
be2daae6
TT
15762 struct nextfield &field = fip->fields[index];
15763
15764 if (field.variant.is_discriminant)
2ddeaf8a 15765 di->discriminant_index = index;
be2daae6 15766 else if (field.variant.default_branch)
2ddeaf8a
TT
15767 di->default_index = index;
15768 else
be2daae6 15769 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
15770 }
15771 }
15772
be2daae6
TT
15773 /* Copy the saved-up fields into the field vector. */
15774 for (int i = 0; i < nfields; ++i)
c906108c 15775 {
be2daae6
TT
15776 struct nextfield &field
15777 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15778 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15779
be2daae6
TT
15780 TYPE_FIELD (type, i) = field.field;
15781 switch (field.accessibility)
c906108c 15782 {
c5aa993b 15783 case DW_ACCESS_private:
b4ba55a1 15784 if (cu->language != language_ada)
be2daae6 15785 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15786 break;
c906108c 15787
c5aa993b 15788 case DW_ACCESS_protected:
b4ba55a1 15789 if (cu->language != language_ada)
be2daae6 15790 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15791 break;
c906108c 15792
c5aa993b
JM
15793 case DW_ACCESS_public:
15794 break;
c906108c 15795
c5aa993b
JM
15796 default:
15797 /* Unknown accessibility. Complain and treat it as public. */
15798 {
e2e0b3e5 15799 complaint (&symfile_complaints, _("unsupported accessibility %d"),
be2daae6 15800 field.accessibility);
c5aa993b
JM
15801 }
15802 break;
c906108c 15803 }
be2daae6 15804 if (i < fip->baseclasses.size ())
c906108c 15805 {
be2daae6 15806 switch (field.virtuality)
c906108c 15807 {
c5aa993b
JM
15808 case DW_VIRTUALITY_virtual:
15809 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15810 if (cu->language == language_ada)
a73c6dcd 15811 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15812 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15813 break;
c906108c
SS
15814 }
15815 }
c906108c
SS
15816 }
15817}
15818
7d27a96d
TT
15819/* Return true if this member function is a constructor, false
15820 otherwise. */
15821
15822static int
15823dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15824{
15825 const char *fieldname;
fe978cb0 15826 const char *type_name;
7d27a96d
TT
15827 int len;
15828
15829 if (die->parent == NULL)
15830 return 0;
15831
15832 if (die->parent->tag != DW_TAG_structure_type
15833 && die->parent->tag != DW_TAG_union_type
15834 && die->parent->tag != DW_TAG_class_type)
15835 return 0;
15836
15837 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15838 type_name = dwarf2_name (die->parent, cu);
15839 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15840 return 0;
15841
15842 len = strlen (fieldname);
fe978cb0
PA
15843 return (strncmp (fieldname, type_name, len) == 0
15844 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15845}
15846
c906108c
SS
15847/* Add a member function to the proper fieldlist. */
15848
15849static void
107d2387 15850dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15851 struct type *type, struct dwarf2_cu *cu)
c906108c 15852{
518817b3 15853 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15854 struct attribute *attr;
c906108c 15855 int i;
be2daae6 15856 struct fnfieldlist *flp = nullptr;
c906108c 15857 struct fn_field *fnp;
15d034d0 15858 const char *fieldname;
f792889a 15859 struct type *this_type;
60d5a603 15860 enum dwarf_access_attribute accessibility;
c906108c 15861
b4ba55a1 15862 if (cu->language == language_ada)
a73c6dcd 15863 error (_("unexpected member function in Ada type"));
b4ba55a1 15864
2df3850c 15865 /* Get name of member function. */
39cbfefa
DJ
15866 fieldname = dwarf2_name (die, cu);
15867 if (fieldname == NULL)
2df3850c 15868 return;
c906108c 15869
c906108c 15870 /* Look up member function name in fieldlist. */
be2daae6 15871 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15872 {
27bfe10e 15873 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15874 {
15875 flp = &fip->fnfieldlists[i];
15876 break;
15877 }
c906108c
SS
15878 }
15879
be2daae6
TT
15880 /* Create a new fnfieldlist if necessary. */
15881 if (flp == nullptr)
c906108c 15882 {
be2daae6
TT
15883 fip->fnfieldlists.emplace_back ();
15884 flp = &fip->fnfieldlists.back ();
c906108c 15885 flp->name = fieldname;
be2daae6 15886 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15887 }
15888
be2daae6
TT
15889 /* Create a new member function field and add it to the vector of
15890 fnfieldlists. */
15891 flp->fnfields.emplace_back ();
15892 fnp = &flp->fnfields.back ();
3da10d80
KS
15893
15894 /* Delay processing of the physname until later. */
9c37b5ae 15895 if (cu->language == language_cplus)
be2daae6
TT
15896 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15897 die, cu);
3da10d80
KS
15898 else
15899 {
1d06ead6 15900 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15901 fnp->physname = physname ? physname : "";
15902 }
15903
c906108c 15904 fnp->type = alloc_type (objfile);
f792889a
DJ
15905 this_type = read_type_die (die, cu);
15906 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15907 {
f792889a 15908 int nparams = TYPE_NFIELDS (this_type);
c906108c 15909
f792889a 15910 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15911 of the method itself (TYPE_CODE_METHOD). */
15912 smash_to_method_type (fnp->type, type,
f792889a
DJ
15913 TYPE_TARGET_TYPE (this_type),
15914 TYPE_FIELDS (this_type),
15915 TYPE_NFIELDS (this_type),
15916 TYPE_VARARGS (this_type));
c906108c
SS
15917
15918 /* Handle static member functions.
c5aa993b 15919 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15920 member functions. G++ helps GDB by marking the first
15921 parameter for non-static member functions (which is the this
15922 pointer) as artificial. We obtain this information from
15923 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15924 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15925 fnp->voffset = VOFFSET_STATIC;
15926 }
15927 else
e2e0b3e5 15928 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 15929 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15930
15931 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15932 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15933 fnp->fcontext = die_containing_type (die, cu);
c906108c 15934
3e43a32a
MS
15935 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15936 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15937
15938 /* Get accessibility. */
e142c38c 15939 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 15940 if (attr)
aead7601 15941 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15942 else
15943 accessibility = dwarf2_default_access_attribute (die, cu);
15944 switch (accessibility)
c906108c 15945 {
60d5a603
JK
15946 case DW_ACCESS_private:
15947 fnp->is_private = 1;
15948 break;
15949 case DW_ACCESS_protected:
15950 fnp->is_protected = 1;
15951 break;
c906108c
SS
15952 }
15953
b02dede2 15954 /* Check for artificial methods. */
e142c38c 15955 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15956 if (attr && DW_UNSND (attr) != 0)
15957 fnp->is_artificial = 1;
15958
7d27a96d
TT
15959 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15960
0d564a31 15961 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15962 function. For older versions of GCC, this is an offset in the
15963 appropriate virtual table, as specified by DW_AT_containing_type.
15964 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15965 to the object address. */
15966
e142c38c 15967 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 15968 if (attr)
8e19ed76 15969 {
aec5aa8b 15970 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15971 {
aec5aa8b
TT
15972 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15973 {
15974 /* Old-style GCC. */
15975 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15976 }
15977 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15978 || (DW_BLOCK (attr)->size > 1
15979 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15980 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15981 {
aec5aa8b
TT
15982 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15983 if ((fnp->voffset % cu->header.addr_size) != 0)
15984 dwarf2_complex_location_expr_complaint ();
15985 else
15986 fnp->voffset /= cu->header.addr_size;
15987 fnp->voffset += 2;
15988 }
15989 else
15990 dwarf2_complex_location_expr_complaint ();
15991
15992 if (!fnp->fcontext)
7e993ebf
KS
15993 {
15994 /* If there is no `this' field and no DW_AT_containing_type,
15995 we cannot actually find a base class context for the
15996 vtable! */
15997 if (TYPE_NFIELDS (this_type) == 0
15998 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15999 {
16000 complaint (&symfile_complaints,
16001 _("cannot determine context for virtual member "
9d8780f0
SM
16002 "function \"%s\" (offset %s)"),
16003 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
16004 }
16005 else
16006 {
16007 fnp->fcontext
16008 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
16009 }
16010 }
aec5aa8b 16011 }
3690dd37 16012 else if (attr_form_is_section_offset (attr))
8e19ed76 16013 {
4d3c2250 16014 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
16015 }
16016 else
16017 {
4d3c2250
KB
16018 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
16019 fieldname);
8e19ed76 16020 }
0d564a31 16021 }
d48cc9dd
DJ
16022 else
16023 {
16024 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
16025 if (attr && DW_UNSND (attr))
16026 {
16027 /* GCC does this, as of 2008-08-25; PR debug/37237. */
16028 complaint (&symfile_complaints,
9d8780f0 16029 _("Member function \"%s\" (offset %s) is virtual "
3e43a32a 16030 "but the vtable offset is not specified"),
9d8780f0 16031 fieldname, sect_offset_str (die->sect_off));
9655fd1a 16032 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
16033 TYPE_CPLUS_DYNAMIC (type) = 1;
16034 }
16035 }
c906108c
SS
16036}
16037
16038/* Create the vector of member function fields, and attach it to the type. */
16039
16040static void
fba45db2 16041dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 16042 struct dwarf2_cu *cu)
c906108c 16043{
b4ba55a1 16044 if (cu->language == language_ada)
a73c6dcd 16045 error (_("unexpected member functions in Ada type"));
b4ba55a1 16046
c906108c
SS
16047 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16048 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
16049 TYPE_ALLOC (type,
16050 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 16051
be2daae6 16052 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 16053 {
be2daae6 16054 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 16055 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 16056
be2daae6
TT
16057 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
16058 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 16059 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
16060 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
16061
16062 for (int k = 0; k < nf.fnfields.size (); ++k)
16063 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
16064 }
16065
be2daae6 16066 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
16067}
16068
1168df01
JB
16069/* Returns non-zero if NAME is the name of a vtable member in CU's
16070 language, zero otherwise. */
16071static int
16072is_vtable_name (const char *name, struct dwarf2_cu *cu)
16073{
16074 static const char vptr[] = "_vptr";
16075
9c37b5ae
TT
16076 /* Look for the C++ form of the vtable. */
16077 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
16078 return 1;
16079
16080 return 0;
16081}
16082
c0dd20ea 16083/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
16084 functions, with the ABI-specified layout. If TYPE describes
16085 such a structure, smash it into a member function type.
61049d3b
DJ
16086
16087 GCC shouldn't do this; it should just output pointer to member DIEs.
16088 This is GCC PR debug/28767. */
c0dd20ea 16089
0b92b5bb
TT
16090static void
16091quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 16092{
09e2d7c7 16093 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
16094
16095 /* Check for a structure with no name and two children. */
0b92b5bb
TT
16096 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
16097 return;
c0dd20ea
DJ
16098
16099 /* Check for __pfn and __delta members. */
0b92b5bb
TT
16100 if (TYPE_FIELD_NAME (type, 0) == NULL
16101 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
16102 || TYPE_FIELD_NAME (type, 1) == NULL
16103 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
16104 return;
c0dd20ea
DJ
16105
16106 /* Find the type of the method. */
0b92b5bb 16107 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
16108 if (pfn_type == NULL
16109 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
16110 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 16111 return;
c0dd20ea
DJ
16112
16113 /* Look for the "this" argument. */
16114 pfn_type = TYPE_TARGET_TYPE (pfn_type);
16115 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 16116 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 16117 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 16118 return;
c0dd20ea 16119
09e2d7c7 16120 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 16121 new_type = alloc_type (objfile);
09e2d7c7 16122 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
16123 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
16124 TYPE_VARARGS (pfn_type));
0b92b5bb 16125 smash_to_methodptr_type (type, new_type);
c0dd20ea 16126}
1168df01 16127
685b1105 16128
c906108c 16129/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
16130 (definition) to create a type for the structure or union. Fill in
16131 the type's name and general properties; the members will not be
83655187
DE
16132 processed until process_structure_scope. A symbol table entry for
16133 the type will also not be done until process_structure_scope (assuming
16134 the type has a name).
c906108c 16135
c767944b
DJ
16136 NOTE: we need to call these functions regardless of whether or not the
16137 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 16138 structure or union. This gets the type entered into our set of
83655187 16139 user defined types. */
c906108c 16140
f792889a 16141static struct type *
134d01f1 16142read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16143{
518817b3 16144 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
16145 struct type *type;
16146 struct attribute *attr;
15d034d0 16147 const char *name;
c906108c 16148
348e048f
DE
16149 /* If the definition of this type lives in .debug_types, read that type.
16150 Don't follow DW_AT_specification though, that will take us back up
16151 the chain and we want to go down. */
45e58e77 16152 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
16153 if (attr)
16154 {
ac9ec31b 16155 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16156
ac9ec31b 16157 /* The type's CU may not be the same as CU.
02142a6c 16158 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16159 return set_die_type (die, type, cu);
16160 }
16161
c0dd20ea 16162 type = alloc_type (objfile);
c906108c 16163 INIT_CPLUS_SPECIFIC (type);
93311388 16164
39cbfefa
DJ
16165 name = dwarf2_name (die, cu);
16166 if (name != NULL)
c906108c 16167 {
987504bb 16168 if (cu->language == language_cplus
c44af4eb
TT
16169 || cu->language == language_d
16170 || cu->language == language_rust)
63d06c5c 16171 {
15d034d0 16172 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
16173
16174 /* dwarf2_full_name might have already finished building the DIE's
16175 type. If so, there is no need to continue. */
16176 if (get_die_type (die, cu) != NULL)
16177 return get_die_type (die, cu);
16178
16179 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
16180 if (die->tag == DW_TAG_structure_type
16181 || die->tag == DW_TAG_class_type)
16182 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
16183 }
16184 else
16185 {
d8151005
DJ
16186 /* The name is already allocated along with this objfile, so
16187 we don't need to duplicate it for the type. */
7d455152 16188 TYPE_TAG_NAME (type) = name;
94af9270
KS
16189 if (die->tag == DW_TAG_class_type)
16190 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 16191 }
c906108c
SS
16192 }
16193
16194 if (die->tag == DW_TAG_structure_type)
16195 {
16196 TYPE_CODE (type) = TYPE_CODE_STRUCT;
16197 }
16198 else if (die->tag == DW_TAG_union_type)
16199 {
16200 TYPE_CODE (type) = TYPE_CODE_UNION;
16201 }
2ddeaf8a
TT
16202 else if (die->tag == DW_TAG_variant_part)
16203 {
16204 TYPE_CODE (type) = TYPE_CODE_UNION;
16205 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
16206 }
c906108c
SS
16207 else
16208 {
4753d33b 16209 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
16210 }
16211
0cc2414c
TT
16212 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16213 TYPE_DECLARED_CLASS (type) = 1;
16214
e142c38c 16215 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16216 if (attr)
16217 {
155bfbd3
JB
16218 if (attr_form_is_constant (attr))
16219 TYPE_LENGTH (type) = DW_UNSND (attr);
16220 else
16221 {
16222 /* For the moment, dynamic type sizes are not supported
16223 by GDB's struct type. The actual size is determined
16224 on-demand when resolving the type of a given object,
16225 so set the type's length to zero for now. Otherwise,
16226 we record an expression as the length, and that expression
16227 could lead to a very large value, which could eventually
16228 lead to us trying to allocate that much memory when creating
16229 a value of that type. */
16230 TYPE_LENGTH (type) = 0;
16231 }
c906108c
SS
16232 }
16233 else
16234 {
16235 TYPE_LENGTH (type) = 0;
16236 }
16237
5230b05a 16238 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 16239 {
5230b05a
WT
16240 /* ICC<14 does not output the required DW_AT_declaration on
16241 incomplete types, but gives them a size of zero. */
422b1cb0 16242 TYPE_STUB (type) = 1;
685b1105
JK
16243 }
16244 else
16245 TYPE_STUB_SUPPORTED (type) = 1;
16246
dc718098 16247 if (die_is_declaration (die, cu))
876cecd0 16248 TYPE_STUB (type) = 1;
a6c727b2
DJ
16249 else if (attr == NULL && die->child == NULL
16250 && producer_is_realview (cu->producer))
16251 /* RealView does not output the required DW_AT_declaration
16252 on incomplete types. */
16253 TYPE_STUB (type) = 1;
dc718098 16254
c906108c
SS
16255 /* We need to add the type field to the die immediately so we don't
16256 infinitely recurse when dealing with pointers to the structure
0963b4bd 16257 type within the structure itself. */
1c379e20 16258 set_die_type (die, type, cu);
c906108c 16259
7e314c57
JK
16260 /* set_die_type should be already done. */
16261 set_descriptive_type (type, die, cu);
16262
c767944b
DJ
16263 return type;
16264}
16265
2ddeaf8a
TT
16266/* A helper for process_structure_scope that handles a single member
16267 DIE. */
16268
16269static void
16270handle_struct_member_die (struct die_info *child_die, struct type *type,
16271 struct field_info *fi,
16272 std::vector<struct symbol *> *template_args,
16273 struct dwarf2_cu *cu)
16274{
16275 if (child_die->tag == DW_TAG_member
16276 || child_die->tag == DW_TAG_variable
16277 || child_die->tag == DW_TAG_variant_part)
16278 {
16279 /* NOTE: carlton/2002-11-05: A C++ static data member
16280 should be a DW_TAG_member that is a declaration, but
16281 all versions of G++ as of this writing (so through at
16282 least 3.2.1) incorrectly generate DW_TAG_variable
16283 tags for them instead. */
16284 dwarf2_add_field (fi, child_die, cu);
16285 }
16286 else if (child_die->tag == DW_TAG_subprogram)
16287 {
16288 /* Rust doesn't have member functions in the C++ sense.
16289 However, it does emit ordinary functions as children
16290 of a struct DIE. */
16291 if (cu->language == language_rust)
16292 read_func_scope (child_die, cu);
16293 else
16294 {
16295 /* C++ member function. */
16296 dwarf2_add_member_fn (fi, child_die, type, cu);
16297 }
16298 }
16299 else if (child_die->tag == DW_TAG_inheritance)
16300 {
16301 /* C++ base class field. */
16302 dwarf2_add_field (fi, child_die, cu);
16303 }
16304 else if (type_can_define_types (child_die))
16305 dwarf2_add_type_defn (fi, child_die, cu);
16306 else if (child_die->tag == DW_TAG_template_type_param
16307 || child_die->tag == DW_TAG_template_value_param)
16308 {
16309 struct symbol *arg = new_symbol (child_die, NULL, cu);
16310
16311 if (arg != NULL)
16312 template_args->push_back (arg);
16313 }
16314 else if (child_die->tag == DW_TAG_variant)
16315 {
16316 /* In a variant we want to get the discriminant and also add a
16317 field for our sole member child. */
16318 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
16319
16320 for (struct die_info *variant_child = child_die->child;
16321 variant_child != NULL;
16322 variant_child = sibling_die (variant_child))
16323 {
16324 if (variant_child->tag == DW_TAG_member)
16325 {
16326 handle_struct_member_die (variant_child, type, fi,
16327 template_args, cu);
16328 /* Only handle the one. */
16329 break;
16330 }
16331 }
16332
16333 /* We don't handle this but we might as well report it if we see
16334 it. */
16335 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
16336 complaint (&symfile_complaints,
16337 _("DW_AT_discr_list is not supported yet"
16338 " - DIE at %s [in module %s]"),
16339 sect_offset_str (child_die->sect_off),
16340 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16341
16342 /* The first field was just added, so we can stash the
16343 discriminant there. */
be2daae6 16344 gdb_assert (!fi->fields.empty ());
2ddeaf8a 16345 if (discr == NULL)
be2daae6 16346 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 16347 else
be2daae6 16348 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
16349 }
16350}
16351
c767944b
DJ
16352/* Finish creating a structure or union type, including filling in
16353 its members and creating a symbol for it. */
16354
16355static void
16356process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16357{
518817b3 16358 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 16359 struct die_info *child_die;
c767944b
DJ
16360 struct type *type;
16361
16362 type = get_die_type (die, cu);
16363 if (type == NULL)
16364 type = read_structure_type (die, cu);
16365
2ddeaf8a
TT
16366 /* When reading a DW_TAG_variant_part, we need to notice when we
16367 read the discriminant member, so we can record it later in the
16368 discriminant_info. */
16369 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
16370 sect_offset discr_offset;
16371
16372 if (is_variant_part)
16373 {
16374 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16375 if (discr == NULL)
16376 {
16377 /* Maybe it's a univariant form, an extension we support.
16378 In this case arrange not to check the offset. */
16379 is_variant_part = false;
16380 }
16381 else if (attr_form_is_ref (discr))
16382 {
16383 struct dwarf2_cu *target_cu = cu;
16384 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16385
16386 discr_offset = target_die->sect_off;
16387 }
16388 else
16389 {
16390 complaint (&symfile_complaints,
16391 _("DW_AT_discr does not have DIE reference form"
16392 " - DIE at %s [in module %s]"),
16393 sect_offset_str (die->sect_off),
16394 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16395 is_variant_part = false;
16396 }
16397 }
16398
e142c38c 16399 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16400 {
16401 struct field_info fi;
2f4732b0 16402 std::vector<struct symbol *> template_args;
c906108c 16403
639d11d3 16404 child_die = die->child;
c906108c
SS
16405
16406 while (child_die && child_die->tag)
16407 {
2ddeaf8a 16408 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 16409
2ddeaf8a 16410 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 16411 fi.fields.back ().variant.is_discriminant = true;
34eaf542 16412
c906108c
SS
16413 child_die = sibling_die (child_die);
16414 }
16415
34eaf542 16416 /* Attach template arguments to type. */
2f4732b0 16417 if (!template_args.empty ())
34eaf542
TT
16418 {
16419 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16420 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16421 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16422 = XOBNEWVEC (&objfile->objfile_obstack,
16423 struct symbol *,
16424 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16425 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16426 template_args.data (),
34eaf542
TT
16427 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16428 * sizeof (struct symbol *)));
34eaf542
TT
16429 }
16430
c906108c
SS
16431 /* Attach fields and member functions to the type. */
16432 if (fi.nfields)
e7c27a73 16433 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16434 if (!fi.fnfieldlists.empty ())
c906108c 16435 {
e7c27a73 16436 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16437
c5aa993b 16438 /* Get the type which refers to the base class (possibly this
c906108c 16439 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16440 class from the DW_AT_containing_type attribute. This use of
16441 DW_AT_containing_type is a GNU extension. */
c906108c 16442
e142c38c 16443 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16444 {
e7c27a73 16445 struct type *t = die_containing_type (die, cu);
c906108c 16446
ae6ae975 16447 set_type_vptr_basetype (type, t);
c906108c
SS
16448 if (type == t)
16449 {
c906108c
SS
16450 int i;
16451
16452 /* Our own class provides vtbl ptr. */
16453 for (i = TYPE_NFIELDS (t) - 1;
16454 i >= TYPE_N_BASECLASSES (t);
16455 --i)
16456 {
0d5cff50 16457 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16458
1168df01 16459 if (is_vtable_name (fieldname, cu))
c906108c 16460 {
ae6ae975 16461 set_type_vptr_fieldno (type, i);
c906108c
SS
16462 break;
16463 }
16464 }
16465
16466 /* Complain if virtual function table field not found. */
16467 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 16468 complaint (&symfile_complaints,
3e43a32a
MS
16469 _("virtual function table pointer "
16470 "not found when defining class '%s'"),
4d3c2250
KB
16471 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
16472 "");
c906108c
SS
16473 }
16474 else
16475 {
ae6ae975 16476 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16477 }
16478 }
f6235d4c 16479 else if (cu->producer
61012eef 16480 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16481 {
16482 /* The IBM XLC compiler does not provide direct indication
16483 of the containing type, but the vtable pointer is
16484 always named __vfp. */
16485
16486 int i;
16487
16488 for (i = TYPE_NFIELDS (type) - 1;
16489 i >= TYPE_N_BASECLASSES (type);
16490 --i)
16491 {
16492 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16493 {
ae6ae975
DE
16494 set_type_vptr_fieldno (type, i);
16495 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16496 break;
16497 }
16498 }
16499 }
c906108c 16500 }
98751a41
JK
16501
16502 /* Copy fi.typedef_field_list linked list elements content into the
16503 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16504 if (!fi.typedef_field_list.empty ())
98751a41 16505 {
be2daae6 16506 int count = fi.typedef_field_list.size ();
98751a41 16507
a0d7a4ff 16508 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16509 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16510 = ((struct decl_field *)
be2daae6
TT
16511 TYPE_ALLOC (type,
16512 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16513 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16514
be2daae6
TT
16515 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16516 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16517 }
c767944b 16518
883fd55a
KS
16519 /* Copy fi.nested_types_list linked list elements content into the
16520 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16521 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16522 {
be2daae6 16523 int count = fi.nested_types_list.size ();
883fd55a
KS
16524
16525 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16526 TYPE_NESTED_TYPES_ARRAY (type)
16527 = ((struct decl_field *)
be2daae6
TT
16528 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16529 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16530
be2daae6
TT
16531 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16532 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16533 }
c906108c 16534 }
63d06c5c 16535
bb5ed363 16536 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16537 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16538 cu->rust_unions.push_back (type);
0b92b5bb 16539
90aeadfc
DC
16540 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16541 snapshots) has been known to create a die giving a declaration
16542 for a class that has, as a child, a die giving a definition for a
16543 nested class. So we have to process our children even if the
16544 current die is a declaration. Normally, of course, a declaration
16545 won't have any children at all. */
134d01f1 16546
ca040673
DE
16547 child_die = die->child;
16548
90aeadfc
DC
16549 while (child_die != NULL && child_die->tag)
16550 {
16551 if (child_die->tag == DW_TAG_member
16552 || child_die->tag == DW_TAG_variable
34eaf542
TT
16553 || child_die->tag == DW_TAG_inheritance
16554 || child_die->tag == DW_TAG_template_value_param
16555 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16556 {
90aeadfc 16557 /* Do nothing. */
134d01f1 16558 }
90aeadfc
DC
16559 else
16560 process_die (child_die, cu);
134d01f1 16561
90aeadfc 16562 child_die = sibling_die (child_die);
134d01f1
DJ
16563 }
16564
fa4028e9
JB
16565 /* Do not consider external references. According to the DWARF standard,
16566 these DIEs are identified by the fact that they have no byte_size
16567 attribute, and a declaration attribute. */
16568 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16569 || !die_is_declaration (die, cu))
c767944b 16570 new_symbol (die, type, cu);
134d01f1
DJ
16571}
16572
55426c9d
JB
16573/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16574 update TYPE using some information only available in DIE's children. */
16575
16576static void
16577update_enumeration_type_from_children (struct die_info *die,
16578 struct type *type,
16579 struct dwarf2_cu *cu)
16580{
60f7655a 16581 struct die_info *child_die;
55426c9d
JB
16582 int unsigned_enum = 1;
16583 int flag_enum = 1;
16584 ULONGEST mask = 0;
55426c9d 16585
8268c778 16586 auto_obstack obstack;
55426c9d 16587
60f7655a
DE
16588 for (child_die = die->child;
16589 child_die != NULL && child_die->tag;
16590 child_die = sibling_die (child_die))
55426c9d
JB
16591 {
16592 struct attribute *attr;
16593 LONGEST value;
16594 const gdb_byte *bytes;
16595 struct dwarf2_locexpr_baton *baton;
16596 const char *name;
60f7655a 16597
55426c9d
JB
16598 if (child_die->tag != DW_TAG_enumerator)
16599 continue;
16600
16601 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16602 if (attr == NULL)
16603 continue;
16604
16605 name = dwarf2_name (child_die, cu);
16606 if (name == NULL)
16607 name = "<anonymous enumerator>";
16608
16609 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16610 &value, &bytes, &baton);
16611 if (value < 0)
16612 {
16613 unsigned_enum = 0;
16614 flag_enum = 0;
16615 }
16616 else if ((mask & value) != 0)
16617 flag_enum = 0;
16618 else
16619 mask |= value;
16620
16621 /* If we already know that the enum type is neither unsigned, nor
16622 a flag type, no need to look at the rest of the enumerates. */
16623 if (!unsigned_enum && !flag_enum)
16624 break;
55426c9d
JB
16625 }
16626
16627 if (unsigned_enum)
16628 TYPE_UNSIGNED (type) = 1;
16629 if (flag_enum)
16630 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16631}
16632
134d01f1
DJ
16633/* Given a DW_AT_enumeration_type die, set its type. We do not
16634 complete the type's fields yet, or create any symbols. */
c906108c 16635
f792889a 16636static struct type *
134d01f1 16637read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16638{
518817b3 16639 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16640 struct type *type;
c906108c 16641 struct attribute *attr;
0114d602 16642 const char *name;
134d01f1 16643
348e048f
DE
16644 /* If the definition of this type lives in .debug_types, read that type.
16645 Don't follow DW_AT_specification though, that will take us back up
16646 the chain and we want to go down. */
45e58e77 16647 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
16648 if (attr)
16649 {
ac9ec31b 16650 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16651
ac9ec31b 16652 /* The type's CU may not be the same as CU.
02142a6c 16653 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16654 return set_die_type (die, type, cu);
16655 }
16656
c906108c
SS
16657 type = alloc_type (objfile);
16658
16659 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16660 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16661 if (name != NULL)
7d455152 16662 TYPE_TAG_NAME (type) = name;
c906108c 16663
0626fc76
TT
16664 attr = dwarf2_attr (die, DW_AT_type, cu);
16665 if (attr != NULL)
16666 {
16667 struct type *underlying_type = die_type (die, cu);
16668
16669 TYPE_TARGET_TYPE (type) = underlying_type;
16670 }
16671
e142c38c 16672 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16673 if (attr)
16674 {
16675 TYPE_LENGTH (type) = DW_UNSND (attr);
16676 }
16677 else
16678 {
16679 TYPE_LENGTH (type) = 0;
16680 }
16681
137033e9
JB
16682 /* The enumeration DIE can be incomplete. In Ada, any type can be
16683 declared as private in the package spec, and then defined only
16684 inside the package body. Such types are known as Taft Amendment
16685 Types. When another package uses such a type, an incomplete DIE
16686 may be generated by the compiler. */
02eb380e 16687 if (die_is_declaration (die, cu))
876cecd0 16688 TYPE_STUB (type) = 1;
02eb380e 16689
0626fc76
TT
16690 /* Finish the creation of this type by using the enum's children.
16691 We must call this even when the underlying type has been provided
16692 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16693 update_enumeration_type_from_children (die, type, cu);
16694
0626fc76
TT
16695 /* If this type has an underlying type that is not a stub, then we
16696 may use its attributes. We always use the "unsigned" attribute
16697 in this situation, because ordinarily we guess whether the type
16698 is unsigned -- but the guess can be wrong and the underlying type
16699 can tell us the reality. However, we defer to a local size
16700 attribute if one exists, because this lets the compiler override
16701 the underlying type if needed. */
16702 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16703 {
16704 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16705 if (TYPE_LENGTH (type) == 0)
16706 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16707 }
16708
3d567982
TT
16709 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16710
f792889a 16711 return set_die_type (die, type, cu);
134d01f1
DJ
16712}
16713
16714/* Given a pointer to a die which begins an enumeration, process all
16715 the dies that define the members of the enumeration, and create the
16716 symbol for the enumeration type.
16717
16718 NOTE: We reverse the order of the element list. */
16719
16720static void
16721process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16722{
f792889a 16723 struct type *this_type;
134d01f1 16724
f792889a
DJ
16725 this_type = get_die_type (die, cu);
16726 if (this_type == NULL)
16727 this_type = read_enumeration_type (die, cu);
9dc481d3 16728
639d11d3 16729 if (die->child != NULL)
c906108c 16730 {
9dc481d3
DE
16731 struct die_info *child_die;
16732 struct symbol *sym;
16733 struct field *fields = NULL;
16734 int num_fields = 0;
15d034d0 16735 const char *name;
9dc481d3 16736
639d11d3 16737 child_die = die->child;
c906108c
SS
16738 while (child_die && child_die->tag)
16739 {
16740 if (child_die->tag != DW_TAG_enumerator)
16741 {
e7c27a73 16742 process_die (child_die, cu);
c906108c
SS
16743 }
16744 else
16745 {
39cbfefa
DJ
16746 name = dwarf2_name (child_die, cu);
16747 if (name)
c906108c 16748 {
f792889a 16749 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
16750
16751 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16752 {
16753 fields = (struct field *)
16754 xrealloc (fields,
16755 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 16756 * sizeof (struct field));
c906108c
SS
16757 }
16758
3567439c 16759 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 16760 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 16761 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
16762 FIELD_BITSIZE (fields[num_fields]) = 0;
16763
16764 num_fields++;
16765 }
16766 }
16767
16768 child_die = sibling_die (child_die);
16769 }
16770
16771 if (num_fields)
16772 {
f792889a
DJ
16773 TYPE_NFIELDS (this_type) = num_fields;
16774 TYPE_FIELDS (this_type) = (struct field *)
16775 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16776 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 16777 sizeof (struct field) * num_fields);
b8c9b27d 16778 xfree (fields);
c906108c 16779 }
c906108c 16780 }
134d01f1 16781
6c83ed52
TT
16782 /* If we are reading an enum from a .debug_types unit, and the enum
16783 is a declaration, and the enum is not the signatured type in the
16784 unit, then we do not want to add a symbol for it. Adding a
16785 symbol would in some cases obscure the true definition of the
16786 enum, giving users an incomplete type when the definition is
16787 actually available. Note that we do not want to do this for all
16788 enums which are just declarations, because C++0x allows forward
16789 enum declarations. */
3019eac3 16790 if (cu->per_cu->is_debug_types
6c83ed52
TT
16791 && die_is_declaration (die, cu))
16792 {
52dc124a 16793 struct signatured_type *sig_type;
6c83ed52 16794
c0f78cd4 16795 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16796 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16797 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16798 return;
16799 }
16800
f792889a 16801 new_symbol (die, this_type, cu);
c906108c
SS
16802}
16803
16804/* Extract all information from a DW_TAG_array_type DIE and put it in
16805 the DIE's type field. For now, this only handles one dimensional
16806 arrays. */
16807
f792889a 16808static struct type *
e7c27a73 16809read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16810{
518817b3 16811 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16812 struct die_info *child_die;
7e314c57 16813 struct type *type;
c906108c 16814 struct type *element_type, *range_type, *index_type;
c906108c 16815 struct attribute *attr;
15d034d0 16816 const char *name;
a405673c 16817 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16818 unsigned int bit_stride = 0;
c906108c 16819
e7c27a73 16820 element_type = die_type (die, cu);
c906108c 16821
7e314c57
JK
16822 /* The die_type call above may have already set the type for this DIE. */
16823 type = get_die_type (die, cu);
16824 if (type)
16825 return type;
16826
dc53a7ad
JB
16827 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16828 if (attr != NULL)
a405673c
JB
16829 {
16830 int stride_ok;
16831
16832 byte_stride_prop
16833 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16834 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16835 if (!stride_ok)
16836 {
16837 complaint (&symfile_complaints,
16838 _("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16839 " - DIE at %s [in module %s]"),
16840 sect_offset_str (die->sect_off),
518817b3 16841 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16842 /* Ignore this attribute. We will likely not be able to print
16843 arrays of this type correctly, but there is little we can do
16844 to help if we cannot read the attribute's value. */
16845 byte_stride_prop = NULL;
16846 }
16847 }
dc53a7ad
JB
16848
16849 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16850 if (attr != NULL)
16851 bit_stride = DW_UNSND (attr);
16852
c906108c
SS
16853 /* Irix 6.2 native cc creates array types without children for
16854 arrays with unspecified length. */
639d11d3 16855 if (die->child == NULL)
c906108c 16856 {
46bf5051 16857 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16858 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16859 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16860 byte_stride_prop, bit_stride);
f792889a 16861 return set_die_type (die, type, cu);
c906108c
SS
16862 }
16863
791afaa2 16864 std::vector<struct type *> range_types;
639d11d3 16865 child_die = die->child;
c906108c
SS
16866 while (child_die && child_die->tag)
16867 {
16868 if (child_die->tag == DW_TAG_subrange_type)
16869 {
f792889a 16870 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16871
f792889a 16872 if (child_type != NULL)
a02abb62 16873 {
0963b4bd
MS
16874 /* The range type was succesfully read. Save it for the
16875 array type creation. */
791afaa2 16876 range_types.push_back (child_type);
a02abb62 16877 }
c906108c
SS
16878 }
16879 child_die = sibling_die (child_die);
16880 }
16881
16882 /* Dwarf2 dimensions are output from left to right, create the
16883 necessary array types in backwards order. */
7ca2d3a3 16884
c906108c 16885 type = element_type;
7ca2d3a3
DL
16886
16887 if (read_array_order (die, cu) == DW_ORD_col_major)
16888 {
16889 int i = 0;
9a619af0 16890
791afaa2 16891 while (i < range_types.size ())
dc53a7ad 16892 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16893 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16894 }
16895 else
16896 {
791afaa2 16897 size_t ndim = range_types.size ();
7ca2d3a3 16898 while (ndim-- > 0)
dc53a7ad 16899 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16900 byte_stride_prop, bit_stride);
7ca2d3a3 16901 }
c906108c 16902
f5f8a009
EZ
16903 /* Understand Dwarf2 support for vector types (like they occur on
16904 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16905 array type. This is not part of the Dwarf2/3 standard yet, but a
16906 custom vendor extension. The main difference between a regular
16907 array and the vector variant is that vectors are passed by value
16908 to functions. */
e142c38c 16909 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 16910 if (attr)
ea37ba09 16911 make_vector_type (type);
f5f8a009 16912
dbc98a8b
KW
16913 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16914 implementation may choose to implement triple vectors using this
16915 attribute. */
16916 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16917 if (attr)
16918 {
16919 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16920 TYPE_LENGTH (type) = DW_UNSND (attr);
16921 else
3e43a32a
MS
16922 complaint (&symfile_complaints,
16923 _("DW_AT_byte_size for array type smaller "
16924 "than the total size of elements"));
dbc98a8b
KW
16925 }
16926
39cbfefa
DJ
16927 name = dwarf2_name (die, cu);
16928 if (name)
16929 TYPE_NAME (type) = name;
6e70227d 16930
0963b4bd 16931 /* Install the type in the die. */
7e314c57
JK
16932 set_die_type (die, type, cu);
16933
16934 /* set_die_type should be already done. */
b4ba55a1
JB
16935 set_descriptive_type (type, die, cu);
16936
7e314c57 16937 return type;
c906108c
SS
16938}
16939
7ca2d3a3 16940static enum dwarf_array_dim_ordering
6e70227d 16941read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16942{
16943 struct attribute *attr;
16944
16945 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16946
aead7601
SM
16947 if (attr)
16948 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16949
0963b4bd
MS
16950 /* GNU F77 is a special case, as at 08/2004 array type info is the
16951 opposite order to the dwarf2 specification, but data is still
16952 laid out as per normal fortran.
7ca2d3a3 16953
0963b4bd
MS
16954 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16955 version checking. */
7ca2d3a3 16956
905e0470
PM
16957 if (cu->language == language_fortran
16958 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16959 {
16960 return DW_ORD_row_major;
16961 }
16962
6e70227d 16963 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16964 {
16965 case array_column_major:
16966 return DW_ORD_col_major;
16967 case array_row_major:
16968 default:
16969 return DW_ORD_row_major;
16970 };
16971}
16972
72019c9c 16973/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16974 the DIE's type field. */
72019c9c 16975
f792889a 16976static struct type *
72019c9c
GM
16977read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16978{
7e314c57
JK
16979 struct type *domain_type, *set_type;
16980 struct attribute *attr;
f792889a 16981
7e314c57
JK
16982 domain_type = die_type (die, cu);
16983
16984 /* The die_type call above may have already set the type for this DIE. */
16985 set_type = get_die_type (die, cu);
16986 if (set_type)
16987 return set_type;
16988
16989 set_type = create_set_type (NULL, domain_type);
16990
16991 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
16992 if (attr)
16993 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16994
f792889a 16995 return set_die_type (die, set_type, cu);
72019c9c 16996}
7ca2d3a3 16997
0971de02
TT
16998/* A helper for read_common_block that creates a locexpr baton.
16999 SYM is the symbol which we are marking as computed.
17000 COMMON_DIE is the DIE for the common block.
17001 COMMON_LOC is the location expression attribute for the common
17002 block itself.
17003 MEMBER_LOC is the location expression attribute for the particular
17004 member of the common block that we are processing.
17005 CU is the CU from which the above come. */
17006
17007static void
17008mark_common_block_symbol_computed (struct symbol *sym,
17009 struct die_info *common_die,
17010 struct attribute *common_loc,
17011 struct attribute *member_loc,
17012 struct dwarf2_cu *cu)
17013{
518817b3
SM
17014 struct dwarf2_per_objfile *dwarf2_per_objfile
17015 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
17016 struct objfile *objfile = dwarf2_per_objfile->objfile;
17017 struct dwarf2_locexpr_baton *baton;
17018 gdb_byte *ptr;
17019 unsigned int cu_off;
17020 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
17021 LONGEST offset = 0;
17022
17023 gdb_assert (common_loc && member_loc);
17024 gdb_assert (attr_form_is_block (common_loc));
17025 gdb_assert (attr_form_is_block (member_loc)
17026 || attr_form_is_constant (member_loc));
17027
8d749320 17028 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
17029 baton->per_cu = cu->per_cu;
17030 gdb_assert (baton->per_cu);
17031
17032 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17033
17034 if (attr_form_is_constant (member_loc))
17035 {
17036 offset = dwarf2_get_attr_constant_value (member_loc, 0);
17037 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17038 }
17039 else
17040 baton->size += DW_BLOCK (member_loc)->size;
17041
224c3ddb 17042 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
17043 baton->data = ptr;
17044
17045 *ptr++ = DW_OP_call4;
9c541725 17046 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
17047 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17048 ptr += 4;
17049
17050 if (attr_form_is_constant (member_loc))
17051 {
17052 *ptr++ = DW_OP_addr;
17053 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17054 ptr += cu->header.addr_size;
17055 }
17056 else
17057 {
17058 /* We have to copy the data here, because DW_OP_call4 will only
17059 use a DW_AT_location attribute. */
17060 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
17061 ptr += DW_BLOCK (member_loc)->size;
17062 }
17063
17064 *ptr++ = DW_OP_plus;
17065 gdb_assert (ptr - baton->data == baton->size);
17066
0971de02 17067 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 17068 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
17069}
17070
4357ac6c
TT
17071/* Create appropriate locally-scoped variables for all the
17072 DW_TAG_common_block entries. Also create a struct common_block
17073 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
17074 is used to sepate the common blocks name namespace from regular
17075 variable names. */
c906108c
SS
17076
17077static void
e7c27a73 17078read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17079{
0971de02
TT
17080 struct attribute *attr;
17081
17082 attr = dwarf2_attr (die, DW_AT_location, cu);
17083 if (attr)
17084 {
17085 /* Support the .debug_loc offsets. */
17086 if (attr_form_is_block (attr))
17087 {
17088 /* Ok. */
17089 }
17090 else if (attr_form_is_section_offset (attr))
17091 {
17092 dwarf2_complex_location_expr_complaint ();
17093 attr = NULL;
17094 }
17095 else
17096 {
17097 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17098 "common block member");
17099 attr = NULL;
17100 }
17101 }
17102
639d11d3 17103 if (die->child != NULL)
c906108c 17104 {
518817b3 17105 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
17106 struct die_info *child_die;
17107 size_t n_entries = 0, size;
17108 struct common_block *common_block;
17109 struct symbol *sym;
74ac6d43 17110
4357ac6c
TT
17111 for (child_die = die->child;
17112 child_die && child_die->tag;
17113 child_die = sibling_die (child_die))
17114 ++n_entries;
17115
17116 size = (sizeof (struct common_block)
17117 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
17118 common_block
17119 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17120 size);
4357ac6c
TT
17121 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17122 common_block->n_entries = 0;
17123
17124 for (child_die = die->child;
17125 child_die && child_die->tag;
17126 child_die = sibling_die (child_die))
17127 {
17128 /* Create the symbol in the DW_TAG_common_block block in the current
17129 symbol scope. */
e7c27a73 17130 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
17131 if (sym != NULL)
17132 {
17133 struct attribute *member_loc;
17134
17135 common_block->contents[common_block->n_entries++] = sym;
17136
17137 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17138 cu);
17139 if (member_loc)
17140 {
17141 /* GDB has handled this for a long time, but it is
17142 not specified by DWARF. It seems to have been
17143 emitted by gfortran at least as recently as:
17144 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17145 complaint (&symfile_complaints,
17146 _("Variable in common block has "
17147 "DW_AT_data_member_location "
9d8780f0
SM
17148 "- DIE at %s [in module %s]"),
17149 sect_offset_str (child_die->sect_off),
518817b3 17150 objfile_name (objfile));
0971de02
TT
17151
17152 if (attr_form_is_section_offset (member_loc))
17153 dwarf2_complex_location_expr_complaint ();
17154 else if (attr_form_is_constant (member_loc)
17155 || attr_form_is_block (member_loc))
17156 {
17157 if (attr)
17158 mark_common_block_symbol_computed (sym, die, attr,
17159 member_loc, cu);
17160 }
17161 else
17162 dwarf2_complex_location_expr_complaint ();
17163 }
17164 }
c906108c 17165 }
4357ac6c
TT
17166
17167 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17168 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
17169 }
17170}
17171
0114d602 17172/* Create a type for a C++ namespace. */
d9fa45fe 17173
0114d602
DJ
17174static struct type *
17175read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 17176{
518817b3 17177 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17178 const char *previous_prefix, *name;
9219021c 17179 int is_anonymous;
0114d602
DJ
17180 struct type *type;
17181
17182 /* For extensions, reuse the type of the original namespace. */
17183 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17184 {
17185 struct die_info *ext_die;
17186 struct dwarf2_cu *ext_cu = cu;
9a619af0 17187
0114d602
DJ
17188 ext_die = dwarf2_extension (die, &ext_cu);
17189 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
17190
17191 /* EXT_CU may not be the same as CU.
02142a6c 17192 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
17193 return set_die_type (die, type, cu);
17194 }
9219021c 17195
e142c38c 17196 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
17197
17198 /* Now build the name of the current namespace. */
17199
0114d602
DJ
17200 previous_prefix = determine_prefix (die, cu);
17201 if (previous_prefix[0] != '\0')
17202 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 17203 previous_prefix, name, 0, cu);
0114d602
DJ
17204
17205 /* Create the type. */
19f392bc 17206 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602
DJ
17207 TYPE_TAG_NAME (type) = TYPE_NAME (type);
17208
60531b24 17209 return set_die_type (die, type, cu);
0114d602
DJ
17210}
17211
22cee43f 17212/* Read a namespace scope. */
0114d602
DJ
17213
17214static void
17215read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17216{
518817b3 17217 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17218 int is_anonymous;
9219021c 17219
5c4e30ca
DC
17220 /* Add a symbol associated to this if we haven't seen the namespace
17221 before. Also, add a using directive if it's an anonymous
17222 namespace. */
9219021c 17223
f2f0e013 17224 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
17225 {
17226 struct type *type;
17227
0114d602 17228 type = read_type_die (die, cu);
e7c27a73 17229 new_symbol (die, type, cu);
5c4e30ca 17230
e8e80198 17231 namespace_name (die, &is_anonymous, cu);
5c4e30ca 17232 if (is_anonymous)
0114d602
DJ
17233 {
17234 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 17235
eb1e02fd 17236 std::vector<const char *> excludes;
22cee43f
PMR
17237 add_using_directive (using_directives (cu->language),
17238 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 17239 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 17240 }
5c4e30ca 17241 }
9219021c 17242
639d11d3 17243 if (die->child != NULL)
d9fa45fe 17244 {
639d11d3 17245 struct die_info *child_die = die->child;
6e70227d 17246
d9fa45fe
DC
17247 while (child_die && child_die->tag)
17248 {
e7c27a73 17249 process_die (child_die, cu);
d9fa45fe
DC
17250 child_die = sibling_die (child_die);
17251 }
17252 }
38d518c9
EZ
17253}
17254
f55ee35c
JK
17255/* Read a Fortran module as type. This DIE can be only a declaration used for
17256 imported module. Still we need that type as local Fortran "use ... only"
17257 declaration imports depend on the created type in determine_prefix. */
17258
17259static struct type *
17260read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17261{
518817b3 17262 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 17263 const char *module_name;
f55ee35c
JK
17264 struct type *type;
17265
17266 module_name = dwarf2_name (die, cu);
17267 if (!module_name)
3e43a32a 17268 complaint (&symfile_complaints,
9d8780f0
SM
17269 _("DW_TAG_module has no name, offset %s"),
17270 sect_offset_str (die->sect_off));
19f392bc 17271 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c
JK
17272
17273 /* determine_prefix uses TYPE_TAG_NAME. */
17274 TYPE_TAG_NAME (type) = TYPE_NAME (type);
17275
17276 return set_die_type (die, type, cu);
17277}
17278
5d7cb8df
JK
17279/* Read a Fortran module. */
17280
17281static void
17282read_module (struct die_info *die, struct dwarf2_cu *cu)
17283{
17284 struct die_info *child_die = die->child;
530e8392
KB
17285 struct type *type;
17286
17287 type = read_type_die (die, cu);
17288 new_symbol (die, type, cu);
5d7cb8df 17289
5d7cb8df
JK
17290 while (child_die && child_die->tag)
17291 {
17292 process_die (child_die, cu);
17293 child_die = sibling_die (child_die);
17294 }
17295}
17296
38d518c9
EZ
17297/* Return the name of the namespace represented by DIE. Set
17298 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17299 namespace. */
17300
17301static const char *
e142c38c 17302namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
17303{
17304 struct die_info *current_die;
17305 const char *name = NULL;
17306
17307 /* Loop through the extensions until we find a name. */
17308
17309 for (current_die = die;
17310 current_die != NULL;
f2f0e013 17311 current_die = dwarf2_extension (die, &cu))
38d518c9 17312 {
96553a0c
DE
17313 /* We don't use dwarf2_name here so that we can detect the absence
17314 of a name -> anonymous namespace. */
7d45c7c3 17315 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17316
38d518c9
EZ
17317 if (name != NULL)
17318 break;
17319 }
17320
17321 /* Is it an anonymous namespace? */
17322
17323 *is_anonymous = (name == NULL);
17324 if (*is_anonymous)
2b1dbab0 17325 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17326
17327 return name;
d9fa45fe
DC
17328}
17329
c906108c
SS
17330/* Extract all information from a DW_TAG_pointer_type DIE and add to
17331 the user defined type vector. */
17332
f792889a 17333static struct type *
e7c27a73 17334read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17335{
518817b3
SM
17336 struct gdbarch *gdbarch
17337 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 17338 struct comp_unit_head *cu_header = &cu->header;
c906108c 17339 struct type *type;
8b2dbe47
KB
17340 struct attribute *attr_byte_size;
17341 struct attribute *attr_address_class;
17342 int byte_size, addr_class;
7e314c57
JK
17343 struct type *target_type;
17344
17345 target_type = die_type (die, cu);
c906108c 17346
7e314c57
JK
17347 /* The die_type call above may have already set the type for this DIE. */
17348 type = get_die_type (die, cu);
17349 if (type)
17350 return type;
17351
17352 type = lookup_pointer_type (target_type);
8b2dbe47 17353
e142c38c 17354 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
17355 if (attr_byte_size)
17356 byte_size = DW_UNSND (attr_byte_size);
c906108c 17357 else
8b2dbe47
KB
17358 byte_size = cu_header->addr_size;
17359
e142c38c 17360 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
17361 if (attr_address_class)
17362 addr_class = DW_UNSND (attr_address_class);
17363 else
17364 addr_class = DW_ADDR_none;
17365
17366 /* If the pointer size or address class is different than the
17367 default, create a type variant marked as such and set the
17368 length accordingly. */
17369 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 17370 {
5e2b427d 17371 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
17372 {
17373 int type_flags;
17374
849957d9 17375 type_flags = gdbarch_address_class_type_flags
5e2b427d 17376 (gdbarch, byte_size, addr_class);
876cecd0
TT
17377 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17378 == 0);
8b2dbe47
KB
17379 type = make_type_with_address_space (type, type_flags);
17380 }
17381 else if (TYPE_LENGTH (type) != byte_size)
17382 {
3e43a32a
MS
17383 complaint (&symfile_complaints,
17384 _("invalid pointer size %d"), byte_size);
8b2dbe47 17385 }
6e70227d 17386 else
9a619af0
MS
17387 {
17388 /* Should we also complain about unhandled address classes? */
17389 }
c906108c 17390 }
8b2dbe47
KB
17391
17392 TYPE_LENGTH (type) = byte_size;
f792889a 17393 return set_die_type (die, type, cu);
c906108c
SS
17394}
17395
17396/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17397 the user defined type vector. */
17398
f792889a 17399static struct type *
e7c27a73 17400read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17401{
17402 struct type *type;
17403 struct type *to_type;
17404 struct type *domain;
17405
e7c27a73
DJ
17406 to_type = die_type (die, cu);
17407 domain = die_containing_type (die, cu);
0d5de010 17408
7e314c57
JK
17409 /* The calls above may have already set the type for this DIE. */
17410 type = get_die_type (die, cu);
17411 if (type)
17412 return type;
17413
0d5de010
DJ
17414 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17415 type = lookup_methodptr_type (to_type);
7078baeb
TT
17416 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17417 {
518817b3
SM
17418 struct type *new_type
17419 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
17420
17421 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17422 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17423 TYPE_VARARGS (to_type));
17424 type = lookup_methodptr_type (new_type);
17425 }
0d5de010
DJ
17426 else
17427 type = lookup_memberptr_type (to_type, domain);
c906108c 17428
f792889a 17429 return set_die_type (die, type, cu);
c906108c
SS
17430}
17431
4297a3f0 17432/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17433 the user defined type vector. */
17434
f792889a 17435static struct type *
4297a3f0
AV
17436read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17437 enum type_code refcode)
c906108c 17438{
e7c27a73 17439 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17440 struct type *type, *target_type;
c906108c
SS
17441 struct attribute *attr;
17442
4297a3f0
AV
17443 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17444
7e314c57
JK
17445 target_type = die_type (die, cu);
17446
17447 /* The die_type call above may have already set the type for this DIE. */
17448 type = get_die_type (die, cu);
17449 if (type)
17450 return type;
17451
4297a3f0 17452 type = lookup_reference_type (target_type, refcode);
e142c38c 17453 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17454 if (attr)
17455 {
17456 TYPE_LENGTH (type) = DW_UNSND (attr);
17457 }
17458 else
17459 {
107d2387 17460 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17461 }
f792889a 17462 return set_die_type (die, type, cu);
c906108c
SS
17463}
17464
cf363f18
MW
17465/* Add the given cv-qualifiers to the element type of the array. GCC
17466 outputs DWARF type qualifiers that apply to an array, not the
17467 element type. But GDB relies on the array element type to carry
17468 the cv-qualifiers. This mimics section 6.7.3 of the C99
17469 specification. */
17470
17471static struct type *
17472add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17473 struct type *base_type, int cnst, int voltl)
17474{
17475 struct type *el_type, *inner_array;
17476
17477 base_type = copy_type (base_type);
17478 inner_array = base_type;
17479
17480 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17481 {
17482 TYPE_TARGET_TYPE (inner_array) =
17483 copy_type (TYPE_TARGET_TYPE (inner_array));
17484 inner_array = TYPE_TARGET_TYPE (inner_array);
17485 }
17486
17487 el_type = TYPE_TARGET_TYPE (inner_array);
17488 cnst |= TYPE_CONST (el_type);
17489 voltl |= TYPE_VOLATILE (el_type);
17490 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17491
17492 return set_die_type (die, base_type, cu);
17493}
17494
f792889a 17495static struct type *
e7c27a73 17496read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17497{
f792889a 17498 struct type *base_type, *cv_type;
c906108c 17499
e7c27a73 17500 base_type = die_type (die, cu);
7e314c57
JK
17501
17502 /* The die_type call above may have already set the type for this DIE. */
17503 cv_type = get_die_type (die, cu);
17504 if (cv_type)
17505 return cv_type;
17506
2f608a3a
KW
17507 /* In case the const qualifier is applied to an array type, the element type
17508 is so qualified, not the array type (section 6.7.3 of C99). */
17509 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 17510 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17511
f792889a
DJ
17512 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17513 return set_die_type (die, cv_type, cu);
c906108c
SS
17514}
17515
f792889a 17516static struct type *
e7c27a73 17517read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17518{
f792889a 17519 struct type *base_type, *cv_type;
c906108c 17520
e7c27a73 17521 base_type = die_type (die, cu);
7e314c57
JK
17522
17523 /* The die_type call above may have already set the type for this DIE. */
17524 cv_type = get_die_type (die, cu);
17525 if (cv_type)
17526 return cv_type;
17527
cf363f18
MW
17528 /* In case the volatile qualifier is applied to an array type, the
17529 element type is so qualified, not the array type (section 6.7.3
17530 of C99). */
17531 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17532 return add_array_cv_type (die, cu, base_type, 0, 1);
17533
f792889a
DJ
17534 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17535 return set_die_type (die, cv_type, cu);
c906108c
SS
17536}
17537
06d66ee9
TT
17538/* Handle DW_TAG_restrict_type. */
17539
17540static struct type *
17541read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17542{
17543 struct type *base_type, *cv_type;
17544
17545 base_type = die_type (die, cu);
17546
17547 /* The die_type call above may have already set the type for this DIE. */
17548 cv_type = get_die_type (die, cu);
17549 if (cv_type)
17550 return cv_type;
17551
17552 cv_type = make_restrict_type (base_type);
17553 return set_die_type (die, cv_type, cu);
17554}
17555
a2c2acaf
MW
17556/* Handle DW_TAG_atomic_type. */
17557
17558static struct type *
17559read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17560{
17561 struct type *base_type, *cv_type;
17562
17563 base_type = die_type (die, cu);
17564
17565 /* The die_type call above may have already set the type for this DIE. */
17566 cv_type = get_die_type (die, cu);
17567 if (cv_type)
17568 return cv_type;
17569
17570 cv_type = make_atomic_type (base_type);
17571 return set_die_type (die, cv_type, cu);
17572}
17573
c906108c
SS
17574/* Extract all information from a DW_TAG_string_type DIE and add to
17575 the user defined type vector. It isn't really a user defined type,
17576 but it behaves like one, with other DIE's using an AT_user_def_type
17577 attribute to reference it. */
17578
f792889a 17579static struct type *
e7c27a73 17580read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17581{
518817b3 17582 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 17583 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17584 struct type *type, *range_type, *index_type, *char_type;
17585 struct attribute *attr;
17586 unsigned int length;
17587
e142c38c 17588 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
17589 if (attr)
17590 {
17591 length = DW_UNSND (attr);
17592 }
17593 else
17594 {
0963b4bd 17595 /* Check for the DW_AT_byte_size attribute. */
e142c38c 17596 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
17597 if (attr)
17598 {
17599 length = DW_UNSND (attr);
17600 }
17601 else
17602 {
17603 length = 1;
17604 }
c906108c 17605 }
6ccb9162 17606
46bf5051 17607 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17608 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
17609 char_type = language_string_char_type (cu->language_defn, gdbarch);
17610 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17611
f792889a 17612 return set_die_type (die, type, cu);
c906108c
SS
17613}
17614
4d804846
JB
17615/* Assuming that DIE corresponds to a function, returns nonzero
17616 if the function is prototyped. */
17617
17618static int
17619prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17620{
17621 struct attribute *attr;
17622
17623 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17624 if (attr && (DW_UNSND (attr) != 0))
17625 return 1;
17626
17627 /* The DWARF standard implies that the DW_AT_prototyped attribute
17628 is only meaninful for C, but the concept also extends to other
17629 languages that allow unprototyped functions (Eg: Objective C).
17630 For all other languages, assume that functions are always
17631 prototyped. */
17632 if (cu->language != language_c
17633 && cu->language != language_objc
17634 && cu->language != language_opencl)
17635 return 1;
17636
17637 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17638 prototyped and unprototyped functions; default to prototyped,
17639 since that is more common in modern code (and RealView warns
17640 about unprototyped functions). */
17641 if (producer_is_realview (cu->producer))
17642 return 1;
17643
17644 return 0;
17645}
17646
c906108c
SS
17647/* Handle DIES due to C code like:
17648
17649 struct foo
c5aa993b
JM
17650 {
17651 int (*funcp)(int a, long l);
17652 int b;
17653 };
c906108c 17654
0963b4bd 17655 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17656
f792889a 17657static struct type *
e7c27a73 17658read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17659{
518817b3 17660 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17661 struct type *type; /* Type that this function returns. */
17662 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17663 struct attribute *attr;
17664
e7c27a73 17665 type = die_type (die, cu);
7e314c57
JK
17666
17667 /* The die_type call above may have already set the type for this DIE. */
17668 ftype = get_die_type (die, cu);
17669 if (ftype)
17670 return ftype;
17671
0c8b41f1 17672 ftype = lookup_function_type (type);
c906108c 17673
4d804846 17674 if (prototyped_function_p (die, cu))
a6c727b2 17675 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17676
c055b101
CV
17677 /* Store the calling convention in the type if it's available in
17678 the subroutine die. Otherwise set the calling convention to
17679 the default value DW_CC_normal. */
17680 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
17681 if (attr)
17682 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17683 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17684 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17685 else
17686 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17687
743649fd
MW
17688 /* Record whether the function returns normally to its caller or not
17689 if the DWARF producer set that information. */
17690 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17691 if (attr && (DW_UNSND (attr) != 0))
17692 TYPE_NO_RETURN (ftype) = 1;
17693
76c10ea2
GM
17694 /* We need to add the subroutine type to the die immediately so
17695 we don't infinitely recurse when dealing with parameters
0963b4bd 17696 declared as the same subroutine type. */
76c10ea2 17697 set_die_type (die, ftype, cu);
6e70227d 17698
639d11d3 17699 if (die->child != NULL)
c906108c 17700 {
bb5ed363 17701 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17702 struct die_info *child_die;
8072405b 17703 int nparams, iparams;
c906108c
SS
17704
17705 /* Count the number of parameters.
17706 FIXME: GDB currently ignores vararg functions, but knows about
17707 vararg member functions. */
8072405b 17708 nparams = 0;
639d11d3 17709 child_die = die->child;
c906108c
SS
17710 while (child_die && child_die->tag)
17711 {
17712 if (child_die->tag == DW_TAG_formal_parameter)
17713 nparams++;
17714 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17715 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17716 child_die = sibling_die (child_die);
17717 }
17718
17719 /* Allocate storage for parameters and fill them in. */
17720 TYPE_NFIELDS (ftype) = nparams;
17721 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17722 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17723
8072405b
JK
17724 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17725 even if we error out during the parameters reading below. */
17726 for (iparams = 0; iparams < nparams; iparams++)
17727 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17728
17729 iparams = 0;
639d11d3 17730 child_die = die->child;
c906108c
SS
17731 while (child_die && child_die->tag)
17732 {
17733 if (child_die->tag == DW_TAG_formal_parameter)
17734 {
3ce3b1ba
PA
17735 struct type *arg_type;
17736
17737 /* DWARF version 2 has no clean way to discern C++
17738 static and non-static member functions. G++ helps
17739 GDB by marking the first parameter for non-static
17740 member functions (which is the this pointer) as
17741 artificial. We pass this information to
17742 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17743
17744 DWARF version 3 added DW_AT_object_pointer, which GCC
17745 4.5 does not yet generate. */
e142c38c 17746 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
17747 if (attr)
17748 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17749 else
9c37b5ae 17750 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17751 arg_type = die_type (child_die, cu);
17752
17753 /* RealView does not mark THIS as const, which the testsuite
17754 expects. GCC marks THIS as const in method definitions,
17755 but not in the class specifications (GCC PR 43053). */
17756 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17757 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17758 {
17759 int is_this = 0;
17760 struct dwarf2_cu *arg_cu = cu;
17761 const char *name = dwarf2_name (child_die, cu);
17762
17763 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17764 if (attr)
17765 {
17766 /* If the compiler emits this, use it. */
17767 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17768 is_this = 1;
17769 }
17770 else if (name && strcmp (name, "this") == 0)
17771 /* Function definitions will have the argument names. */
17772 is_this = 1;
17773 else if (name == NULL && iparams == 0)
17774 /* Declarations may not have the names, so like
17775 elsewhere in GDB, assume an artificial first
17776 argument is "this". */
17777 is_this = 1;
17778
17779 if (is_this)
17780 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17781 arg_type, 0);
17782 }
17783
17784 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17785 iparams++;
17786 }
17787 child_die = sibling_die (child_die);
17788 }
17789 }
17790
76c10ea2 17791 return ftype;
c906108c
SS
17792}
17793
f792889a 17794static struct type *
e7c27a73 17795read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17796{
518817b3 17797 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17798 const char *name = NULL;
3c8e0968 17799 struct type *this_type, *target_type;
c906108c 17800
94af9270 17801 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17802 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17803 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17804 set_die_type (die, this_type, cu);
3c8e0968
DE
17805 target_type = die_type (die, cu);
17806 if (target_type != this_type)
17807 TYPE_TARGET_TYPE (this_type) = target_type;
17808 else
17809 {
17810 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17811 spec and cause infinite loops in GDB. */
17812 complaint (&symfile_complaints,
17813 _("Self-referential DW_TAG_typedef "
9d8780f0
SM
17814 "- DIE at %s [in module %s]"),
17815 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17816 TYPE_TARGET_TYPE (this_type) = NULL;
17817 }
f792889a 17818 return this_type;
c906108c
SS
17819}
17820
9b790ce7
UW
17821/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17822 (which may be different from NAME) to the architecture back-end to allow
17823 it to guess the correct format if necessary. */
17824
17825static struct type *
17826dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17827 const char *name_hint)
17828{
17829 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17830 const struct floatformat **format;
17831 struct type *type;
17832
17833 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17834 if (format)
17835 type = init_float_type (objfile, bits, name, format);
17836 else
77b7c781 17837 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17838
17839 return type;
17840}
17841
c906108c
SS
17842/* Find a representation of a given base type and install
17843 it in the TYPE field of the die. */
17844
f792889a 17845static struct type *
e7c27a73 17846read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17847{
518817b3 17848 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17849 struct type *type;
17850 struct attribute *attr;
19f392bc 17851 int encoding = 0, bits = 0;
15d034d0 17852 const char *name;
c906108c 17853
e142c38c 17854 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
17855 if (attr)
17856 {
17857 encoding = DW_UNSND (attr);
17858 }
e142c38c 17859 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17860 if (attr)
17861 {
19f392bc 17862 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 17863 }
39cbfefa 17864 name = dwarf2_name (die, cu);
6ccb9162 17865 if (!name)
c906108c 17866 {
6ccb9162
UW
17867 complaint (&symfile_complaints,
17868 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 17869 }
6ccb9162
UW
17870
17871 switch (encoding)
c906108c 17872 {
6ccb9162
UW
17873 case DW_ATE_address:
17874 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17875 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17876 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17877 break;
17878 case DW_ATE_boolean:
19f392bc 17879 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17880 break;
17881 case DW_ATE_complex_float:
9b790ce7 17882 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 17883 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17884 break;
17885 case DW_ATE_decimal_float:
19f392bc 17886 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17887 break;
17888 case DW_ATE_float:
9b790ce7 17889 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
17890 break;
17891 case DW_ATE_signed:
19f392bc 17892 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
17893 break;
17894 case DW_ATE_unsigned:
3b2b8fea
TT
17895 if (cu->language == language_fortran
17896 && name
61012eef 17897 && startswith (name, "character("))
19f392bc
UW
17898 type = init_character_type (objfile, bits, 1, name);
17899 else
17900 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
17901 break;
17902 case DW_ATE_signed_char:
6e70227d 17903 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17904 || cu->language == language_pascal
17905 || cu->language == language_fortran)
19f392bc
UW
17906 type = init_character_type (objfile, bits, 0, name);
17907 else
17908 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
17909 break;
17910 case DW_ATE_unsigned_char:
868a0084 17911 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17912 || cu->language == language_pascal
c44af4eb
TT
17913 || cu->language == language_fortran
17914 || cu->language == language_rust)
19f392bc
UW
17915 type = init_character_type (objfile, bits, 1, name);
17916 else
17917 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 17918 break;
75079b2b 17919 case DW_ATE_UTF:
53e710ac
PA
17920 {
17921 gdbarch *arch = get_objfile_arch (objfile);
17922
17923 if (bits == 16)
17924 type = builtin_type (arch)->builtin_char16;
17925 else if (bits == 32)
17926 type = builtin_type (arch)->builtin_char32;
17927 else
17928 {
17929 complaint (&symfile_complaints,
17930 _("unsupported DW_ATE_UTF bit size: '%d'"),
17931 bits);
17932 type = init_integer_type (objfile, bits, 1, name);
17933 }
17934 return set_die_type (die, type, cu);
17935 }
75079b2b
TT
17936 break;
17937
6ccb9162
UW
17938 default:
17939 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17940 dwarf_type_encoding_name (encoding));
77b7c781 17941 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17942 break;
c906108c 17943 }
6ccb9162 17944
0114d602 17945 if (name && strcmp (name, "char") == 0)
876cecd0 17946 TYPE_NOSIGN (type) = 1;
0114d602 17947
f792889a 17948 return set_die_type (die, type, cu);
c906108c
SS
17949}
17950
80180f79
SA
17951/* Parse dwarf attribute if it's a block, reference or constant and put the
17952 resulting value of the attribute into struct bound_prop.
17953 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17954
17955static int
17956attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17957 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17958{
17959 struct dwarf2_property_baton *baton;
518817b3
SM
17960 struct obstack *obstack
17961 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79
SA
17962
17963 if (attr == NULL || prop == NULL)
17964 return 0;
17965
17966 if (attr_form_is_block (attr))
17967 {
8d749320 17968 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
17969 baton->referenced_type = NULL;
17970 baton->locexpr.per_cu = cu->per_cu;
17971 baton->locexpr.size = DW_BLOCK (attr)->size;
17972 baton->locexpr.data = DW_BLOCK (attr)->data;
17973 prop->data.baton = baton;
17974 prop->kind = PROP_LOCEXPR;
17975 gdb_assert (prop->data.baton != NULL);
17976 }
17977 else if (attr_form_is_ref (attr))
17978 {
17979 struct dwarf2_cu *target_cu = cu;
17980 struct die_info *target_die;
17981 struct attribute *target_attr;
17982
17983 target_die = follow_die_ref (die, attr, &target_cu);
17984 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17985 if (target_attr == NULL)
17986 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17987 target_cu);
80180f79
SA
17988 if (target_attr == NULL)
17989 return 0;
17990
df25ebbd 17991 switch (target_attr->name)
80180f79 17992 {
df25ebbd
JB
17993 case DW_AT_location:
17994 if (attr_form_is_section_offset (target_attr))
17995 {
8d749320 17996 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17997 baton->referenced_type = die_type (target_die, target_cu);
17998 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17999 prop->data.baton = baton;
18000 prop->kind = PROP_LOCLIST;
18001 gdb_assert (prop->data.baton != NULL);
18002 }
18003 else if (attr_form_is_block (target_attr))
18004 {
8d749320 18005 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
18006 baton->referenced_type = die_type (target_die, target_cu);
18007 baton->locexpr.per_cu = cu->per_cu;
18008 baton->locexpr.size = DW_BLOCK (target_attr)->size;
18009 baton->locexpr.data = DW_BLOCK (target_attr)->data;
18010 prop->data.baton = baton;
18011 prop->kind = PROP_LOCEXPR;
18012 gdb_assert (prop->data.baton != NULL);
18013 }
18014 else
18015 {
18016 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18017 "dynamic property");
18018 return 0;
18019 }
18020 break;
18021 case DW_AT_data_member_location:
18022 {
18023 LONGEST offset;
18024
18025 if (!handle_data_member_location (target_die, target_cu,
18026 &offset))
18027 return 0;
18028
8d749320 18029 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
18030 baton->referenced_type = read_type_die (target_die->parent,
18031 target_cu);
df25ebbd
JB
18032 baton->offset_info.offset = offset;
18033 baton->offset_info.type = die_type (target_die, target_cu);
18034 prop->data.baton = baton;
18035 prop->kind = PROP_ADDR_OFFSET;
18036 break;
18037 }
80180f79
SA
18038 }
18039 }
18040 else if (attr_form_is_constant (attr))
18041 {
18042 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
18043 prop->kind = PROP_CONST;
18044 }
18045 else
18046 {
18047 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18048 dwarf2_name (die, cu));
18049 return 0;
18050 }
18051
18052 return 1;
18053}
18054
a02abb62
JB
18055/* Read the given DW_AT_subrange DIE. */
18056
f792889a 18057static struct type *
a02abb62
JB
18058read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18059{
4c9ad8c2 18060 struct type *base_type, *orig_base_type;
a02abb62
JB
18061 struct type *range_type;
18062 struct attribute *attr;
729efb13 18063 struct dynamic_prop low, high;
4fae6e18 18064 int low_default_is_valid;
c451ebe5 18065 int high_bound_is_count = 0;
15d034d0 18066 const char *name;
43bbcdc2 18067 LONGEST negative_mask;
e77813c8 18068
4c9ad8c2
TT
18069 orig_base_type = die_type (die, cu);
18070 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18071 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18072 creating the range type, but we use the result of check_typedef
18073 when examining properties of the type. */
18074 base_type = check_typedef (orig_base_type);
a02abb62 18075
7e314c57
JK
18076 /* The die_type call above may have already set the type for this DIE. */
18077 range_type = get_die_type (die, cu);
18078 if (range_type)
18079 return range_type;
18080
729efb13
SA
18081 low.kind = PROP_CONST;
18082 high.kind = PROP_CONST;
18083 high.data.const_val = 0;
18084
4fae6e18
JK
18085 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18086 omitting DW_AT_lower_bound. */
18087 switch (cu->language)
6e70227d 18088 {
4fae6e18
JK
18089 case language_c:
18090 case language_cplus:
729efb13 18091 low.data.const_val = 0;
4fae6e18
JK
18092 low_default_is_valid = 1;
18093 break;
18094 case language_fortran:
729efb13 18095 low.data.const_val = 1;
4fae6e18
JK
18096 low_default_is_valid = 1;
18097 break;
18098 case language_d:
4fae6e18 18099 case language_objc:
c44af4eb 18100 case language_rust:
729efb13 18101 low.data.const_val = 0;
4fae6e18
JK
18102 low_default_is_valid = (cu->header.version >= 4);
18103 break;
18104 case language_ada:
18105 case language_m2:
18106 case language_pascal:
729efb13 18107 low.data.const_val = 1;
4fae6e18
JK
18108 low_default_is_valid = (cu->header.version >= 4);
18109 break;
18110 default:
729efb13 18111 low.data.const_val = 0;
4fae6e18
JK
18112 low_default_is_valid = 0;
18113 break;
a02abb62
JB
18114 }
18115
e142c38c 18116 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 18117 if (attr)
11c1ba78 18118 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
18119 else if (!low_default_is_valid)
18120 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
9d8780f0
SM
18121 "- DIE at %s [in module %s]"),
18122 sect_offset_str (die->sect_off),
518817b3 18123 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 18124
e142c38c 18125 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 18126 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
18127 {
18128 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 18129 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 18130 {
c451ebe5
SA
18131 /* If bounds are constant do the final calculation here. */
18132 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
18133 high.data.const_val = low.data.const_val + high.data.const_val - 1;
18134 else
18135 high_bound_is_count = 1;
c2ff108b 18136 }
e77813c8
PM
18137 }
18138
18139 /* Dwarf-2 specifications explicitly allows to create subrange types
18140 without specifying a base type.
18141 In that case, the base type must be set to the type of
18142 the lower bound, upper bound or count, in that order, if any of these
18143 three attributes references an object that has a type.
18144 If no base type is found, the Dwarf-2 specifications say that
18145 a signed integer type of size equal to the size of an address should
18146 be used.
18147 For the following C code: `extern char gdb_int [];'
18148 GCC produces an empty range DIE.
18149 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 18150 high bound or count are not yet handled by this code. */
e77813c8
PM
18151 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
18152 {
518817b3 18153 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e77813c8
PM
18154 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18155 int addr_size = gdbarch_addr_bit (gdbarch) /8;
18156 struct type *int_type = objfile_type (objfile)->builtin_int;
18157
18158 /* Test "int", "long int", and "long long int" objfile types,
18159 and select the first one having a size above or equal to the
18160 architecture address size. */
18161 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
18162 base_type = int_type;
18163 else
18164 {
18165 int_type = objfile_type (objfile)->builtin_long;
18166 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
18167 base_type = int_type;
18168 else
18169 {
18170 int_type = objfile_type (objfile)->builtin_long_long;
18171 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
18172 base_type = int_type;
18173 }
18174 }
18175 }
a02abb62 18176
dbb9c2b1
JB
18177 /* Normally, the DWARF producers are expected to use a signed
18178 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18179 But this is unfortunately not always the case, as witnessed
18180 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18181 is used instead. To work around that ambiguity, we treat
18182 the bounds as signed, and thus sign-extend their values, when
18183 the base type is signed. */
6e70227d 18184 negative_mask =
66c6502d 18185 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
18186 if (low.kind == PROP_CONST
18187 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18188 low.data.const_val |= negative_mask;
18189 if (high.kind == PROP_CONST
18190 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18191 high.data.const_val |= negative_mask;
43bbcdc2 18192
729efb13 18193 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 18194
c451ebe5
SA
18195 if (high_bound_is_count)
18196 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18197
c2ff108b
JK
18198 /* Ada expects an empty array on no boundary attributes. */
18199 if (attr == NULL && cu->language != language_ada)
729efb13 18200 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 18201
39cbfefa
DJ
18202 name = dwarf2_name (die, cu);
18203 if (name)
18204 TYPE_NAME (range_type) = name;
6e70227d 18205
e142c38c 18206 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
18207 if (attr)
18208 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18209
7e314c57
JK
18210 set_die_type (die, range_type, cu);
18211
18212 /* set_die_type should be already done. */
b4ba55a1
JB
18213 set_descriptive_type (range_type, die, cu);
18214
7e314c57 18215 return range_type;
a02abb62 18216}
6e70227d 18217
f792889a 18218static struct type *
81a17f79
JB
18219read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18220{
18221 struct type *type;
81a17f79 18222
518817b3
SM
18223 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18224 NULL);
0114d602 18225 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 18226
74a2f8ff
JB
18227 /* In Ada, an unspecified type is typically used when the description
18228 of the type is defered to a different unit. When encountering
18229 such a type, we treat it as a stub, and try to resolve it later on,
18230 when needed. */
18231 if (cu->language == language_ada)
18232 TYPE_STUB (type) = 1;
18233
f792889a 18234 return set_die_type (die, type, cu);
81a17f79 18235}
a02abb62 18236
639d11d3
DC
18237/* Read a single die and all its descendents. Set the die's sibling
18238 field to NULL; set other fields in the die correctly, and set all
18239 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18240 location of the info_ptr after reading all of those dies. PARENT
18241 is the parent of the die in question. */
18242
18243static struct die_info *
dee91e82 18244read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18245 const gdb_byte *info_ptr,
18246 const gdb_byte **new_info_ptr,
dee91e82 18247 struct die_info *parent)
639d11d3
DC
18248{
18249 struct die_info *die;
d521ce57 18250 const gdb_byte *cur_ptr;
639d11d3
DC
18251 int has_children;
18252
bf6af496 18253 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
18254 if (die == NULL)
18255 {
18256 *new_info_ptr = cur_ptr;
18257 return NULL;
18258 }
93311388 18259 store_in_ref_table (die, reader->cu);
639d11d3
DC
18260
18261 if (has_children)
bf6af496 18262 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18263 else
18264 {
18265 die->child = NULL;
18266 *new_info_ptr = cur_ptr;
18267 }
18268
18269 die->sibling = NULL;
18270 die->parent = parent;
18271 return die;
18272}
18273
18274/* Read a die, all of its descendents, and all of its siblings; set
18275 all of the fields of all of the dies correctly. Arguments are as
18276 in read_die_and_children. */
18277
18278static struct die_info *
bf6af496 18279read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18280 const gdb_byte *info_ptr,
18281 const gdb_byte **new_info_ptr,
bf6af496 18282 struct die_info *parent)
639d11d3
DC
18283{
18284 struct die_info *first_die, *last_sibling;
d521ce57 18285 const gdb_byte *cur_ptr;
639d11d3 18286
c906108c 18287 cur_ptr = info_ptr;
639d11d3
DC
18288 first_die = last_sibling = NULL;
18289
18290 while (1)
c906108c 18291 {
639d11d3 18292 struct die_info *die
dee91e82 18293 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18294
1d325ec1 18295 if (die == NULL)
c906108c 18296 {
639d11d3
DC
18297 *new_info_ptr = cur_ptr;
18298 return first_die;
c906108c 18299 }
1d325ec1
DJ
18300
18301 if (!first_die)
18302 first_die = die;
c906108c 18303 else
1d325ec1
DJ
18304 last_sibling->sibling = die;
18305
18306 last_sibling = die;
c906108c 18307 }
c906108c
SS
18308}
18309
bf6af496
DE
18310/* Read a die, all of its descendents, and all of its siblings; set
18311 all of the fields of all of the dies correctly. Arguments are as
18312 in read_die_and_children.
18313 This the main entry point for reading a DIE and all its children. */
18314
18315static struct die_info *
18316read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18317 const gdb_byte *info_ptr,
18318 const gdb_byte **new_info_ptr,
bf6af496
DE
18319 struct die_info *parent)
18320{
18321 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18322 new_info_ptr, parent);
18323
b4f54984 18324 if (dwarf_die_debug)
bf6af496
DE
18325 {
18326 fprintf_unfiltered (gdb_stdlog,
18327 "Read die from %s@0x%x of %s:\n",
a32a8923 18328 get_section_name (reader->die_section),
bf6af496
DE
18329 (unsigned) (info_ptr - reader->die_section->buffer),
18330 bfd_get_filename (reader->abfd));
b4f54984 18331 dump_die (die, dwarf_die_debug);
bf6af496
DE
18332 }
18333
18334 return die;
18335}
18336
3019eac3
DE
18337/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18338 attributes.
18339 The caller is responsible for filling in the extra attributes
18340 and updating (*DIEP)->num_attrs.
18341 Set DIEP to point to a newly allocated die with its information,
18342 except for its child, sibling, and parent fields.
18343 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 18344
d521ce57 18345static const gdb_byte *
3019eac3 18346read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18347 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 18348 int *has_children, int num_extra_attrs)
93311388 18349{
b64f50a1 18350 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18351 struct abbrev_info *abbrev;
18352 struct die_info *die;
18353 struct dwarf2_cu *cu = reader->cu;
18354 bfd *abfd = reader->abfd;
18355
9c541725 18356 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18357 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18358 info_ptr += bytes_read;
18359 if (!abbrev_number)
18360 {
18361 *diep = NULL;
18362 *has_children = 0;
18363 return info_ptr;
18364 }
18365
685af9cd 18366 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18367 if (!abbrev)
348e048f
DE
18368 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18369 abbrev_number,
18370 bfd_get_filename (abfd));
18371
3019eac3 18372 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18373 die->sect_off = sect_off;
93311388
DE
18374 die->tag = abbrev->tag;
18375 die->abbrev = abbrev_number;
18376
3019eac3
DE
18377 /* Make the result usable.
18378 The caller needs to update num_attrs after adding the extra
18379 attributes. */
93311388
DE
18380 die->num_attrs = abbrev->num_attrs;
18381
18382 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
18383 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18384 info_ptr);
93311388
DE
18385
18386 *diep = die;
18387 *has_children = abbrev->has_children;
18388 return info_ptr;
18389}
18390
3019eac3
DE
18391/* Read a die and all its attributes.
18392 Set DIEP to point to a newly allocated die with its information,
18393 except for its child, sibling, and parent fields.
18394 Set HAS_CHILDREN to tell whether the die has children or not. */
18395
d521ce57 18396static const gdb_byte *
3019eac3 18397read_full_die (const struct die_reader_specs *reader,
d521ce57 18398 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
18399 int *has_children)
18400{
d521ce57 18401 const gdb_byte *result;
bf6af496
DE
18402
18403 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18404
b4f54984 18405 if (dwarf_die_debug)
bf6af496
DE
18406 {
18407 fprintf_unfiltered (gdb_stdlog,
18408 "Read die from %s@0x%x of %s:\n",
a32a8923 18409 get_section_name (reader->die_section),
bf6af496
DE
18410 (unsigned) (info_ptr - reader->die_section->buffer),
18411 bfd_get_filename (reader->abfd));
b4f54984 18412 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18413 }
18414
18415 return result;
3019eac3 18416}
433df2d4
DE
18417\f
18418/* Abbreviation tables.
3019eac3 18419
433df2d4 18420 In DWARF version 2, the description of the debugging information is
c906108c
SS
18421 stored in a separate .debug_abbrev section. Before we read any
18422 dies from a section we read in all abbreviations and install them
433df2d4
DE
18423 in a hash table. */
18424
18425/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18426
685af9cd
TT
18427struct abbrev_info *
18428abbrev_table::alloc_abbrev ()
433df2d4
DE
18429{
18430 struct abbrev_info *abbrev;
18431
685af9cd 18432 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
433df2d4 18433 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 18434
433df2d4
DE
18435 return abbrev;
18436}
18437
18438/* Add an abbreviation to the table. */
c906108c 18439
685af9cd
TT
18440void
18441abbrev_table::add_abbrev (unsigned int abbrev_number,
18442 struct abbrev_info *abbrev)
433df2d4
DE
18443{
18444 unsigned int hash_number;
18445
18446 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768
YQ
18447 abbrev->next = m_abbrevs[hash_number];
18448 m_abbrevs[hash_number] = abbrev;
433df2d4 18449}
dee91e82 18450
433df2d4
DE
18451/* Look up an abbrev in the table.
18452 Returns NULL if the abbrev is not found. */
18453
685af9cd
TT
18454struct abbrev_info *
18455abbrev_table::lookup_abbrev (unsigned int abbrev_number)
c906108c 18456{
433df2d4
DE
18457 unsigned int hash_number;
18458 struct abbrev_info *abbrev;
18459
18460 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768 18461 abbrev = m_abbrevs[hash_number];
433df2d4
DE
18462
18463 while (abbrev)
18464 {
18465 if (abbrev->number == abbrev_number)
18466 return abbrev;
18467 abbrev = abbrev->next;
18468 }
18469 return NULL;
18470}
18471
18472/* Read in an abbrev table. */
18473
685af9cd 18474static abbrev_table_up
ed2dc618
SM
18475abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18476 struct dwarf2_section_info *section,
9c541725 18477 sect_offset sect_off)
433df2d4
DE
18478{
18479 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 18480 bfd *abfd = get_section_bfd_owner (section);
d521ce57 18481 const gdb_byte *abbrev_ptr;
c906108c
SS
18482 struct abbrev_info *cur_abbrev;
18483 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 18484 unsigned int abbrev_form;
f3dd6933
DJ
18485 struct attr_abbrev *cur_attrs;
18486 unsigned int allocated_attrs;
c906108c 18487
685af9cd 18488 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
c906108c 18489
433df2d4 18490 dwarf2_read_section (objfile, section);
9c541725 18491 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
18492 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18493 abbrev_ptr += bytes_read;
18494
f3dd6933 18495 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 18496 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 18497
0963b4bd 18498 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
18499 while (abbrev_number)
18500 {
685af9cd 18501 cur_abbrev = abbrev_table->alloc_abbrev ();
c906108c
SS
18502
18503 /* read in abbrev header */
18504 cur_abbrev->number = abbrev_number;
aead7601
SM
18505 cur_abbrev->tag
18506 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
18507 abbrev_ptr += bytes_read;
18508 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18509 abbrev_ptr += 1;
18510
18511 /* now read in declarations */
22d2f3ab 18512 for (;;)
c906108c 18513 {
43988095
JK
18514 LONGEST implicit_const;
18515
22d2f3ab
JK
18516 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18517 abbrev_ptr += bytes_read;
18518 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18519 abbrev_ptr += bytes_read;
43988095
JK
18520 if (abbrev_form == DW_FORM_implicit_const)
18521 {
18522 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18523 &bytes_read);
18524 abbrev_ptr += bytes_read;
18525 }
18526 else
18527 {
18528 /* Initialize it due to a false compiler warning. */
18529 implicit_const = -1;
18530 }
22d2f3ab
JK
18531
18532 if (abbrev_name == 0)
18533 break;
18534
f3dd6933 18535 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 18536 {
f3dd6933
DJ
18537 allocated_attrs += ATTR_ALLOC_CHUNK;
18538 cur_attrs
224c3ddb 18539 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 18540 }
ae038cb0 18541
aead7601
SM
18542 cur_attrs[cur_abbrev->num_attrs].name
18543 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 18544 cur_attrs[cur_abbrev->num_attrs].form
aead7601 18545 = (enum dwarf_form) abbrev_form;
43988095 18546 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 18547 ++cur_abbrev->num_attrs;
c906108c
SS
18548 }
18549
8d749320
SM
18550 cur_abbrev->attrs =
18551 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18552 cur_abbrev->num_attrs);
f3dd6933
DJ
18553 memcpy (cur_abbrev->attrs, cur_attrs,
18554 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18555
685af9cd 18556 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
c906108c
SS
18557
18558 /* Get next abbreviation.
18559 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18560 always properly terminated with an abbrev number of 0.
18561 Exit loop if we encounter an abbreviation which we have
18562 already read (which means we are about to read the abbreviations
18563 for the next compile unit) or if the end of the abbreviation
18564 table is reached. */
433df2d4 18565 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18566 break;
18567 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18568 abbrev_ptr += bytes_read;
685af9cd 18569 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
c906108c
SS
18570 break;
18571 }
f3dd6933
DJ
18572
18573 xfree (cur_attrs);
433df2d4 18574 return abbrev_table;
c906108c
SS
18575}
18576
72bf9492
DJ
18577/* Returns nonzero if TAG represents a type that we might generate a partial
18578 symbol for. */
18579
18580static int
18581is_type_tag_for_partial (int tag)
18582{
18583 switch (tag)
18584 {
18585#if 0
18586 /* Some types that would be reasonable to generate partial symbols for,
18587 that we don't at present. */
18588 case DW_TAG_array_type:
18589 case DW_TAG_file_type:
18590 case DW_TAG_ptr_to_member_type:
18591 case DW_TAG_set_type:
18592 case DW_TAG_string_type:
18593 case DW_TAG_subroutine_type:
18594#endif
18595 case DW_TAG_base_type:
18596 case DW_TAG_class_type:
680b30c7 18597 case DW_TAG_interface_type:
72bf9492
DJ
18598 case DW_TAG_enumeration_type:
18599 case DW_TAG_structure_type:
18600 case DW_TAG_subrange_type:
18601 case DW_TAG_typedef:
18602 case DW_TAG_union_type:
18603 return 1;
18604 default:
18605 return 0;
18606 }
18607}
18608
18609/* Load all DIEs that are interesting for partial symbols into memory. */
18610
18611static struct partial_die_info *
dee91e82 18612load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18613 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18614{
dee91e82 18615 struct dwarf2_cu *cu = reader->cu;
518817b3 18616 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18617 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18618 unsigned int bytes_read;
5afb4e99 18619 unsigned int load_all = 0;
72bf9492
DJ
18620 int nesting_level = 1;
18621
18622 parent_die = NULL;
18623 last_die = NULL;
18624
7adf1e79
DE
18625 gdb_assert (cu->per_cu != NULL);
18626 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18627 load_all = 1;
18628
72bf9492
DJ
18629 cu->partial_dies
18630 = htab_create_alloc_ex (cu->header.length / 12,
18631 partial_die_hash,
18632 partial_die_eq,
18633 NULL,
18634 &cu->comp_unit_obstack,
18635 hashtab_obstack_allocate,
18636 dummy_obstack_deallocate);
18637
72bf9492
DJ
18638 while (1)
18639 {
685af9cd 18640 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18641
18642 /* A NULL abbrev means the end of a series of children. */
18643 if (abbrev == NULL)
18644 {
18645 if (--nesting_level == 0)
cd9983dd
YQ
18646 return first_die;
18647
72bf9492
DJ
18648 info_ptr += bytes_read;
18649 last_die = parent_die;
18650 parent_die = parent_die->die_parent;
18651 continue;
18652 }
18653
98bfdba5
PA
18654 /* Check for template arguments. We never save these; if
18655 they're seen, we just mark the parent, and go on our way. */
18656 if (parent_die != NULL
18657 && cu->language == language_cplus
18658 && (abbrev->tag == DW_TAG_template_type_param
18659 || abbrev->tag == DW_TAG_template_value_param))
18660 {
18661 parent_die->has_template_arguments = 1;
18662
18663 if (!load_all)
18664 {
18665 /* We don't need a partial DIE for the template argument. */
dee91e82 18666 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18667 continue;
18668 }
18669 }
18670
0d99eb77 18671 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18672 Skip their other children. */
18673 if (!load_all
18674 && cu->language == language_cplus
18675 && parent_die != NULL
18676 && parent_die->tag == DW_TAG_subprogram)
18677 {
dee91e82 18678 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18679 continue;
18680 }
18681
5afb4e99
DJ
18682 /* Check whether this DIE is interesting enough to save. Normally
18683 we would not be interested in members here, but there may be
18684 later variables referencing them via DW_AT_specification (for
18685 static members). */
18686 if (!load_all
18687 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18688 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18689 && abbrev->tag != DW_TAG_enumerator
18690 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18691 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18692 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18693 && abbrev->tag != DW_TAG_variable
5afb4e99 18694 && abbrev->tag != DW_TAG_namespace
f55ee35c 18695 && abbrev->tag != DW_TAG_module
95554aad 18696 && abbrev->tag != DW_TAG_member
74921315
KS
18697 && abbrev->tag != DW_TAG_imported_unit
18698 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18699 {
18700 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18701 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18702 continue;
18703 }
18704
6f06d47b
YQ
18705 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18706 abbrev);
cd9983dd 18707
48fbe735 18708 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18709
18710 /* This two-pass algorithm for processing partial symbols has a
18711 high cost in cache pressure. Thus, handle some simple cases
18712 here which cover the majority of C partial symbols. DIEs
18713 which neither have specification tags in them, nor could have
18714 specification tags elsewhere pointing at them, can simply be
18715 processed and discarded.
18716
18717 This segment is also optional; scan_partial_symbols and
18718 add_partial_symbol will handle these DIEs if we chain
18719 them in normally. When compilers which do not emit large
18720 quantities of duplicate debug information are more common,
18721 this code can probably be removed. */
18722
18723 /* Any complete simple types at the top level (pretty much all
18724 of them, for a language without namespaces), can be processed
18725 directly. */
18726 if (parent_die == NULL
cd9983dd
YQ
18727 && pdi.has_specification == 0
18728 && pdi.is_declaration == 0
18729 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18730 || pdi.tag == DW_TAG_base_type
18731 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18732 {
cd9983dd
YQ
18733 if (building_psymtab && pdi.name != NULL)
18734 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
72bf9492 18735 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 18736 &objfile->static_psymbols,
1762568f 18737 0, cu->language, objfile);
cd9983dd 18738 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18739 continue;
18740 }
18741
d8228535
JK
18742 /* The exception for DW_TAG_typedef with has_children above is
18743 a workaround of GCC PR debug/47510. In the case of this complaint
18744 type_name_no_tag_or_error will error on such types later.
18745
18746 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18747 it could not find the child DIEs referenced later, this is checked
18748 above. In correct DWARF DW_TAG_typedef should have no children. */
18749
cd9983dd 18750 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
d8228535
JK
18751 complaint (&symfile_complaints,
18752 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18753 "- DIE at %s [in module %s]"),
cd9983dd 18754 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18755
72bf9492
DJ
18756 /* If we're at the second level, and we're an enumerator, and
18757 our parent has no specification (meaning possibly lives in a
18758 namespace elsewhere), then we can add the partial symbol now
18759 instead of queueing it. */
cd9983dd 18760 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18761 && parent_die != NULL
18762 && parent_die->die_parent == NULL
18763 && parent_die->tag == DW_TAG_enumeration_type
18764 && parent_die->has_specification == 0)
18765 {
cd9983dd 18766 if (pdi.name == NULL)
3e43a32a
MS
18767 complaint (&symfile_complaints,
18768 _("malformed enumerator DIE ignored"));
72bf9492 18769 else if (building_psymtab)
cd9983dd 18770 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
72bf9492 18771 VAR_DOMAIN, LOC_CONST,
9c37b5ae 18772 cu->language == language_cplus
bb5ed363
DE
18773 ? &objfile->global_psymbols
18774 : &objfile->static_psymbols,
1762568f 18775 0, cu->language, objfile);
72bf9492 18776
cd9983dd 18777 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18778 continue;
18779 }
18780
cd9983dd 18781 struct partial_die_info *part_die
6f06d47b 18782 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18783
72bf9492
DJ
18784 /* We'll save this DIE so link it in. */
18785 part_die->die_parent = parent_die;
18786 part_die->die_sibling = NULL;
18787 part_die->die_child = NULL;
18788
18789 if (last_die && last_die == parent_die)
18790 last_die->die_child = part_die;
18791 else if (last_die)
18792 last_die->die_sibling = part_die;
18793
18794 last_die = part_die;
18795
18796 if (first_die == NULL)
18797 first_die = part_die;
18798
18799 /* Maybe add the DIE to the hash table. Not all DIEs that we
18800 find interesting need to be in the hash table, because we
18801 also have the parent/sibling/child chains; only those that we
18802 might refer to by offset later during partial symbol reading.
18803
18804 For now this means things that might have be the target of a
18805 DW_AT_specification, DW_AT_abstract_origin, or
18806 DW_AT_extension. DW_AT_extension will refer only to
18807 namespaces; DW_AT_abstract_origin refers to functions (and
18808 many things under the function DIE, but we do not recurse
18809 into function DIEs during partial symbol reading) and
18810 possibly variables as well; DW_AT_specification refers to
18811 declarations. Declarations ought to have the DW_AT_declaration
18812 flag. It happens that GCC forgets to put it in sometimes, but
18813 only for functions, not for types.
18814
18815 Adding more things than necessary to the hash table is harmless
18816 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18817 wasted time in find_partial_die, when we reread the compilation
18818 unit with load_all_dies set. */
72bf9492 18819
5afb4e99 18820 if (load_all
72929c62 18821 || abbrev->tag == DW_TAG_constant
5afb4e99 18822 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18823 || abbrev->tag == DW_TAG_variable
18824 || abbrev->tag == DW_TAG_namespace
18825 || part_die->is_declaration)
18826 {
18827 void **slot;
18828
18829 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18830 to_underlying (part_die->sect_off),
18831 INSERT);
72bf9492
DJ
18832 *slot = part_die;
18833 }
18834
72bf9492 18835 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18836 we have no reason to follow the children of structures; for other
98bfdba5
PA
18837 languages we have to, so that we can get at method physnames
18838 to infer fully qualified class names, for DW_AT_specification,
18839 and for C++ template arguments. For C++, we also look one level
18840 inside functions to find template arguments (if the name of the
18841 function does not already contain the template arguments).
bc30ff58
JB
18842
18843 For Ada, we need to scan the children of subprograms and lexical
18844 blocks as well because Ada allows the definition of nested
18845 entities that could be interesting for the debugger, such as
18846 nested subprograms for instance. */
72bf9492 18847 if (last_die->has_children
5afb4e99
DJ
18848 && (load_all
18849 || last_die->tag == DW_TAG_namespace
f55ee35c 18850 || last_die->tag == DW_TAG_module
72bf9492 18851 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18852 || (cu->language == language_cplus
18853 && last_die->tag == DW_TAG_subprogram
18854 && (last_die->name == NULL
18855 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18856 || (cu->language != language_c
18857 && (last_die->tag == DW_TAG_class_type
680b30c7 18858 || last_die->tag == DW_TAG_interface_type
72bf9492 18859 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
18860 || last_die->tag == DW_TAG_union_type))
18861 || (cu->language == language_ada
18862 && (last_die->tag == DW_TAG_subprogram
18863 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18864 {
18865 nesting_level++;
18866 parent_die = last_die;
18867 continue;
18868 }
18869
18870 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18871 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18872
18873 /* Back to the top, do it again. */
18874 }
18875}
18876
6f06d47b
YQ
18877partial_die_info::partial_die_info (sect_offset sect_off_,
18878 struct abbrev_info *abbrev)
18879 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18880{
18881}
18882
35cc7ed7
YQ
18883/* Read a minimal amount of information into the minimal die structure.
18884 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18885
48fbe735
YQ
18886const gdb_byte *
18887partial_die_info::read (const struct die_reader_specs *reader,
18888 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18889{
dee91e82 18890 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18891 struct dwarf2_per_objfile *dwarf2_per_objfile
18892 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18893 unsigned int i;
c5aa993b 18894 int has_low_pc_attr = 0;
c906108c 18895 int has_high_pc_attr = 0;
91da1414 18896 int high_pc_relative = 0;
c906108c 18897
fd0a254f 18898 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18899 {
48fbe735
YQ
18900 struct attribute attr;
18901
fd0a254f 18902 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
c906108c
SS
18903
18904 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18905 partial symbol table. */
c906108c
SS
18906 switch (attr.name)
18907 {
18908 case DW_AT_name:
48fbe735 18909 switch (tag)
71c25dea
TT
18910 {
18911 case DW_TAG_compile_unit:
95554aad 18912 case DW_TAG_partial_unit:
348e048f 18913 case DW_TAG_type_unit:
71c25dea
TT
18914 /* Compilation units have a DW_AT_name that is a filename, not
18915 a source language identifier. */
18916 case DW_TAG_enumeration_type:
18917 case DW_TAG_enumerator:
18918 /* These tags always have simple identifiers already; no need
18919 to canonicalize them. */
48fbe735 18920 name = DW_STRING (&attr);
71c25dea
TT
18921 break;
18922 default:
48fbe735
YQ
18923 {
18924 struct objfile *objfile = dwarf2_per_objfile->objfile;
18925
18926 name
18927 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18928 &objfile->per_bfd->storage_obstack);
18929 }
71c25dea
TT
18930 break;
18931 }
c906108c 18932 break;
31ef98ae 18933 case DW_AT_linkage_name:
c906108c 18934 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18935 /* Note that both forms of linkage name might appear. We
18936 assume they will be the same, and we only store the last
18937 one we see. */
94af9270 18938 if (cu->language == language_ada)
48fbe735
YQ
18939 name = DW_STRING (&attr);
18940 linkage_name = DW_STRING (&attr);
c906108c
SS
18941 break;
18942 case DW_AT_low_pc:
18943 has_low_pc_attr = 1;
48fbe735 18944 lowpc = attr_value_as_address (&attr);
c906108c
SS
18945 break;
18946 case DW_AT_high_pc:
18947 has_high_pc_attr = 1;
48fbe735 18948 highpc = attr_value_as_address (&attr);
31aa7e4e
JB
18949 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18950 high_pc_relative = 1;
c906108c
SS
18951 break;
18952 case DW_AT_location:
0963b4bd 18953 /* Support the .debug_loc offsets. */
8e19ed76
PS
18954 if (attr_form_is_block (&attr))
18955 {
48fbe735 18956 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18957 }
3690dd37 18958 else if (attr_form_is_section_offset (&attr))
8e19ed76 18959 {
4d3c2250 18960 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18961 }
18962 else
18963 {
4d3c2250
KB
18964 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18965 "partial symbol information");
8e19ed76 18966 }
c906108c 18967 break;
c906108c 18968 case DW_AT_external:
48fbe735 18969 is_external = DW_UNSND (&attr);
c906108c
SS
18970 break;
18971 case DW_AT_declaration:
48fbe735 18972 is_declaration = DW_UNSND (&attr);
c906108c
SS
18973 break;
18974 case DW_AT_type:
48fbe735 18975 has_type = 1;
c906108c
SS
18976 break;
18977 case DW_AT_abstract_origin:
18978 case DW_AT_specification:
72bf9492 18979 case DW_AT_extension:
48fbe735
YQ
18980 has_specification = 1;
18981 spec_offset = dwarf2_get_ref_die_offset (&attr);
18982 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18983 || cu->per_cu->is_dwz);
c906108c
SS
18984 break;
18985 case DW_AT_sibling:
18986 /* Ignore absolute siblings, they might point outside of
18987 the current compile unit. */
18988 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
18989 complaint (&symfile_complaints,
18990 _("ignoring absolute DW_AT_sibling"));
c906108c 18991 else
b9502d3f 18992 {
48fbe735 18993 const gdb_byte *buffer = reader->buffer;
9c541725
PA
18994 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18995 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18996
18997 if (sibling_ptr < info_ptr)
18998 complaint (&symfile_complaints,
18999 _("DW_AT_sibling points backwards"));
22869d73
KS
19000 else if (sibling_ptr > reader->buffer_end)
19001 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 19002 else
48fbe735 19003 sibling = sibling_ptr;
b9502d3f 19004 }
c906108c 19005 break;
fa4028e9 19006 case DW_AT_byte_size:
48fbe735 19007 has_byte_size = 1;
fa4028e9 19008 break;
ff908ebf 19009 case DW_AT_const_value:
48fbe735 19010 has_const_value = 1;
ff908ebf 19011 break;
68511cec
CES
19012 case DW_AT_calling_convention:
19013 /* DWARF doesn't provide a way to identify a program's source-level
19014 entry point. DW_AT_calling_convention attributes are only meant
19015 to describe functions' calling conventions.
19016
19017 However, because it's a necessary piece of information in
0c1b455e
TT
19018 Fortran, and before DWARF 4 DW_CC_program was the only
19019 piece of debugging information whose definition refers to
19020 a 'main program' at all, several compilers marked Fortran
19021 main programs with DW_CC_program --- even when those
19022 functions use the standard calling conventions.
19023
19024 Although DWARF now specifies a way to provide this
19025 information, we support this practice for backward
19026 compatibility. */
68511cec 19027 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 19028 && cu->language == language_fortran)
48fbe735 19029 main_subprogram = 1;
68511cec 19030 break;
481860b3
GB
19031 case DW_AT_inline:
19032 if (DW_UNSND (&attr) == DW_INL_inlined
19033 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 19034 may_be_inlined = 1;
481860b3 19035 break;
95554aad
TT
19036
19037 case DW_AT_import:
48fbe735 19038 if (tag == DW_TAG_imported_unit)
36586728 19039 {
48fbe735
YQ
19040 d.sect_off = dwarf2_get_ref_die_offset (&attr);
19041 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19042 || cu->per_cu->is_dwz);
19043 }
95554aad
TT
19044 break;
19045
0c1b455e 19046 case DW_AT_main_subprogram:
48fbe735 19047 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
19048 break;
19049
c906108c
SS
19050 default:
19051 break;
19052 }
19053 }
19054
91da1414 19055 if (high_pc_relative)
48fbe735 19056 highpc += lowpc;
91da1414 19057
9373cf26
JK
19058 if (has_low_pc_attr && has_high_pc_attr)
19059 {
19060 /* When using the GNU linker, .gnu.linkonce. sections are used to
19061 eliminate duplicate copies of functions and vtables and such.
19062 The linker will arbitrarily choose one and discard the others.
19063 The AT_*_pc values for such functions refer to local labels in
19064 these sections. If the section from that file was discarded, the
19065 labels are not in the output, so the relocs get a value of 0.
19066 If this is a discarded function, mark the pc bounds as invalid,
19067 so that GDB will ignore it. */
48fbe735 19068 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 19069 {
48fbe735 19070 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 19071 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
19072
19073 complaint (&symfile_complaints,
19074 _("DW_AT_low_pc %s is zero "
9d8780f0 19075 "for DIE at %s [in module %s]"),
48fbe735
YQ
19076 paddress (gdbarch, lowpc),
19077 sect_offset_str (sect_off),
9d8780f0 19078 objfile_name (objfile));
9373cf26
JK
19079 }
19080 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19081 else if (lowpc >= highpc)
9373cf26 19082 {
48fbe735 19083 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 19084 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
19085
19086 complaint (&symfile_complaints,
19087 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19088 "for DIE at %s [in module %s]"),
48fbe735
YQ
19089 paddress (gdbarch, lowpc),
19090 paddress (gdbarch, highpc),
19091 sect_offset_str (sect_off),
9c541725 19092 objfile_name (objfile));
9373cf26
JK
19093 }
19094 else
48fbe735 19095 has_pc_info = 1;
9373cf26 19096 }
85cbf3d3 19097
c906108c
SS
19098 return info_ptr;
19099}
19100
72bf9492
DJ
19101/* Find a cached partial DIE at OFFSET in CU. */
19102
d590ff25
YQ
19103struct partial_die_info *
19104dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19105{
19106 struct partial_die_info *lookup_die = NULL;
6f06d47b 19107 struct partial_die_info part_die (sect_off);
72bf9492 19108
9a3c8263 19109 lookup_die = ((struct partial_die_info *)
d590ff25 19110 htab_find_with_hash (partial_dies, &part_die,
9c541725 19111 to_underlying (sect_off)));
72bf9492 19112
72bf9492
DJ
19113 return lookup_die;
19114}
19115
348e048f
DE
19116/* Find a partial DIE at OFFSET, which may or may not be in CU,
19117 except in the case of .debug_types DIEs which do not reference
19118 outside their CU (they do however referencing other types via
55f1336d 19119 DW_FORM_ref_sig8). */
72bf9492
DJ
19120
19121static struct partial_die_info *
9c541725 19122find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19123{
518817b3
SM
19124 struct dwarf2_per_objfile *dwarf2_per_objfile
19125 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19126 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
19127 struct dwarf2_per_cu_data *per_cu = NULL;
19128 struct partial_die_info *pd = NULL;
72bf9492 19129
36586728 19130 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 19131 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 19132 {
d590ff25 19133 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
19134 if (pd != NULL)
19135 return pd;
0d99eb77
DE
19136 /* We missed recording what we needed.
19137 Load all dies and try again. */
19138 per_cu = cu->per_cu;
5afb4e99 19139 }
0d99eb77
DE
19140 else
19141 {
19142 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 19143 if (cu->per_cu->is_debug_types)
0d99eb77 19144 {
9d8780f0
SM
19145 error (_("Dwarf Error: Type Unit at offset %s contains"
19146 " external reference to offset %s [in module %s].\n"),
19147 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19148 bfd_get_filename (objfile->obfd));
19149 }
9c541725 19150 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 19151 dwarf2_per_objfile);
72bf9492 19152
0d99eb77
DE
19153 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19154 load_partial_comp_unit (per_cu);
ae038cb0 19155
0d99eb77 19156 per_cu->cu->last_used = 0;
d590ff25 19157 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 19158 }
5afb4e99 19159
dee91e82
DE
19160 /* If we didn't find it, and not all dies have been loaded,
19161 load them all and try again. */
19162
5afb4e99
DJ
19163 if (pd == NULL && per_cu->load_all_dies == 0)
19164 {
5afb4e99 19165 per_cu->load_all_dies = 1;
fd820528
DE
19166
19167 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19168 THIS_CU->cu may already be in use. So we can't just free it and
19169 replace its DIEs with the ones we read in. Instead, we leave those
19170 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19171 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19172 set. */
dee91e82 19173 load_partial_comp_unit (per_cu);
5afb4e99 19174
d590ff25 19175 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
19176 }
19177
19178 if (pd == NULL)
19179 internal_error (__FILE__, __LINE__,
9d8780f0 19180 _("could not find partial DIE %s "
3e43a32a 19181 "in cache [from module %s]\n"),
9d8780f0 19182 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
5afb4e99 19183 return pd;
72bf9492
DJ
19184}
19185
abc72ce4
DE
19186/* See if we can figure out if the class lives in a namespace. We do
19187 this by looking for a member function; its demangled name will
19188 contain namespace info, if there is any. */
19189
19190static void
19191guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19192 struct dwarf2_cu *cu)
19193{
19194 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19195 what template types look like, because the demangler
19196 frequently doesn't give the same name as the debug info. We
19197 could fix this by only using the demangled name to get the
19198 prefix (but see comment in read_structure_type). */
19199
19200 struct partial_die_info *real_pdi;
19201 struct partial_die_info *child_pdi;
19202
19203 /* If this DIE (this DIE's specification, if any) has a parent, then
19204 we should not do this. We'll prepend the parent's fully qualified
19205 name when we create the partial symbol. */
19206
19207 real_pdi = struct_pdi;
19208 while (real_pdi->has_specification)
36586728
TT
19209 real_pdi = find_partial_die (real_pdi->spec_offset,
19210 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
19211
19212 if (real_pdi->die_parent != NULL)
19213 return;
19214
19215 for (child_pdi = struct_pdi->die_child;
19216 child_pdi != NULL;
19217 child_pdi = child_pdi->die_sibling)
19218 {
19219 if (child_pdi->tag == DW_TAG_subprogram
19220 && child_pdi->linkage_name != NULL)
19221 {
19222 char *actual_class_name
19223 = language_class_name_from_physname (cu->language_defn,
19224 child_pdi->linkage_name);
19225 if (actual_class_name != NULL)
19226 {
518817b3 19227 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 19228 struct_pdi->name
224c3ddb 19229 = ((const char *)
e3b94546 19230 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb
SM
19231 actual_class_name,
19232 strlen (actual_class_name)));
abc72ce4
DE
19233 xfree (actual_class_name);
19234 }
19235 break;
19236 }
19237 }
19238}
19239
52356b79
YQ
19240void
19241partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19242{
abc72ce4
DE
19243 /* Once we've fixed up a die, there's no point in doing so again.
19244 This also avoids a memory leak if we were to call
19245 guess_partial_die_structure_name multiple times. */
52356b79 19246 if (fixup_called)
abc72ce4
DE
19247 return;
19248
72bf9492
DJ
19249 /* If we found a reference attribute and the DIE has no name, try
19250 to find a name in the referred to DIE. */
19251
52356b79 19252 if (name == NULL && has_specification)
72bf9492
DJ
19253 {
19254 struct partial_die_info *spec_die;
72bf9492 19255
52356b79 19256 spec_die = find_partial_die (spec_offset, spec_is_dwz, cu);
72bf9492 19257
52356b79 19258 spec_die->fixup (cu);
72bf9492
DJ
19259
19260 if (spec_die->name)
19261 {
52356b79 19262 name = spec_die->name;
72bf9492
DJ
19263
19264 /* Copy DW_AT_external attribute if it is set. */
19265 if (spec_die->is_external)
52356b79 19266 is_external = spec_die->is_external;
72bf9492
DJ
19267 }
19268 }
19269
19270 /* Set default names for some unnamed DIEs. */
72bf9492 19271
52356b79
YQ
19272 if (name == NULL && tag == DW_TAG_namespace)
19273 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 19274
abc72ce4
DE
19275 /* If there is no parent die to provide a namespace, and there are
19276 children, see if we can determine the namespace from their linkage
122d1940 19277 name. */
abc72ce4 19278 if (cu->language == language_cplus
518817b3
SM
19279 && !VEC_empty (dwarf2_section_info_def,
19280 cu->per_cu->dwarf2_per_objfile->types)
52356b79
YQ
19281 && die_parent == NULL
19282 && has_children
19283 && (tag == DW_TAG_class_type
19284 || tag == DW_TAG_structure_type
19285 || tag == DW_TAG_union_type))
19286 guess_partial_die_structure_name (this, cu);
abc72ce4 19287
53832f31
TT
19288 /* GCC might emit a nameless struct or union that has a linkage
19289 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
19290 if (name == NULL
19291 && (tag == DW_TAG_class_type
19292 || tag == DW_TAG_interface_type
19293 || tag == DW_TAG_structure_type
19294 || tag == DW_TAG_union_type)
19295 && linkage_name != NULL)
53832f31
TT
19296 {
19297 char *demangled;
19298
52356b79 19299 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
53832f31
TT
19300 if (demangled)
19301 {
96408a79
SA
19302 const char *base;
19303
19304 /* Strip any leading namespaces/classes, keep only the base name.
19305 DW_AT_name for named DIEs does not contain the prefixes. */
19306 base = strrchr (demangled, ':');
19307 if (base && base > demangled && base[-1] == ':')
19308 base++;
19309 else
19310 base = demangled;
19311
518817b3 19312 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
52356b79 19313 name
224c3ddb 19314 = ((const char *)
e3b94546 19315 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb 19316 base, strlen (base)));
53832f31
TT
19317 xfree (demangled);
19318 }
19319 }
19320
52356b79 19321 fixup_called = 1;
72bf9492
DJ
19322}
19323
a8329558 19324/* Read an attribute value described by an attribute form. */
c906108c 19325
d521ce57 19326static const gdb_byte *
dee91e82
DE
19327read_attribute_value (const struct die_reader_specs *reader,
19328 struct attribute *attr, unsigned form,
43988095 19329 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 19330{
dee91e82 19331 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19332 struct dwarf2_per_objfile *dwarf2_per_objfile
19333 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19334 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 19335 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 19336 bfd *abfd = reader->abfd;
e7c27a73 19337 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19338 unsigned int bytes_read;
19339 struct dwarf_block *blk;
19340
aead7601 19341 attr->form = (enum dwarf_form) form;
a8329558 19342 switch (form)
c906108c 19343 {
c906108c 19344 case DW_FORM_ref_addr:
ae411497 19345 if (cu->header.version == 2)
4568ecf9 19346 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 19347 else
4568ecf9
DE
19348 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19349 &cu->header, &bytes_read);
ae411497
TT
19350 info_ptr += bytes_read;
19351 break;
36586728
TT
19352 case DW_FORM_GNU_ref_alt:
19353 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19354 info_ptr += bytes_read;
19355 break;
ae411497 19356 case DW_FORM_addr:
e7c27a73 19357 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 19358 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 19359 info_ptr += bytes_read;
c906108c
SS
19360 break;
19361 case DW_FORM_block2:
7b5a2f43 19362 blk = dwarf_alloc_block (cu);
c906108c
SS
19363 blk->size = read_2_bytes (abfd, info_ptr);
19364 info_ptr += 2;
19365 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19366 info_ptr += blk->size;
19367 DW_BLOCK (attr) = blk;
19368 break;
19369 case DW_FORM_block4:
7b5a2f43 19370 blk = dwarf_alloc_block (cu);
c906108c
SS
19371 blk->size = read_4_bytes (abfd, info_ptr);
19372 info_ptr += 4;
19373 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19374 info_ptr += blk->size;
19375 DW_BLOCK (attr) = blk;
19376 break;
19377 case DW_FORM_data2:
19378 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19379 info_ptr += 2;
19380 break;
19381 case DW_FORM_data4:
19382 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19383 info_ptr += 4;
19384 break;
19385 case DW_FORM_data8:
19386 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19387 info_ptr += 8;
19388 break;
0224619f
JK
19389 case DW_FORM_data16:
19390 blk = dwarf_alloc_block (cu);
19391 blk->size = 16;
19392 blk->data = read_n_bytes (abfd, info_ptr, 16);
19393 info_ptr += 16;
19394 DW_BLOCK (attr) = blk;
19395 break;
2dc7f7b3
TT
19396 case DW_FORM_sec_offset:
19397 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19398 info_ptr += bytes_read;
19399 break;
c906108c 19400 case DW_FORM_string:
9b1c24c8 19401 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19402 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19403 info_ptr += bytes_read;
19404 break;
4bdf3d34 19405 case DW_FORM_strp:
36586728
TT
19406 if (!cu->per_cu->is_dwz)
19407 {
ed2dc618
SM
19408 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19409 abfd, info_ptr, cu_header,
36586728
TT
19410 &bytes_read);
19411 DW_STRING_IS_CANONICAL (attr) = 0;
19412 info_ptr += bytes_read;
19413 break;
19414 }
19415 /* FALLTHROUGH */
43988095
JK
19416 case DW_FORM_line_strp:
19417 if (!cu->per_cu->is_dwz)
19418 {
ed2dc618
SM
19419 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19420 abfd, info_ptr,
43988095
JK
19421 cu_header, &bytes_read);
19422 DW_STRING_IS_CANONICAL (attr) = 0;
19423 info_ptr += bytes_read;
19424 break;
19425 }
19426 /* FALLTHROUGH */
36586728
TT
19427 case DW_FORM_GNU_strp_alt:
19428 {
ed2dc618 19429 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19430 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19431 &bytes_read);
19432
ed2dc618
SM
19433 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19434 dwz, str_offset);
36586728
TT
19435 DW_STRING_IS_CANONICAL (attr) = 0;
19436 info_ptr += bytes_read;
19437 }
4bdf3d34 19438 break;
2dc7f7b3 19439 case DW_FORM_exprloc:
c906108c 19440 case DW_FORM_block:
7b5a2f43 19441 blk = dwarf_alloc_block (cu);
c906108c
SS
19442 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19443 info_ptr += bytes_read;
19444 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19445 info_ptr += blk->size;
19446 DW_BLOCK (attr) = blk;
19447 break;
19448 case DW_FORM_block1:
7b5a2f43 19449 blk = dwarf_alloc_block (cu);
c906108c
SS
19450 blk->size = read_1_byte (abfd, info_ptr);
19451 info_ptr += 1;
19452 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19453 info_ptr += blk->size;
19454 DW_BLOCK (attr) = blk;
19455 break;
19456 case DW_FORM_data1:
19457 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19458 info_ptr += 1;
19459 break;
19460 case DW_FORM_flag:
19461 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19462 info_ptr += 1;
19463 break;
2dc7f7b3
TT
19464 case DW_FORM_flag_present:
19465 DW_UNSND (attr) = 1;
19466 break;
c906108c
SS
19467 case DW_FORM_sdata:
19468 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19469 info_ptr += bytes_read;
19470 break;
19471 case DW_FORM_udata:
19472 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19473 info_ptr += bytes_read;
19474 break;
19475 case DW_FORM_ref1:
9c541725 19476 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19477 + read_1_byte (abfd, info_ptr));
c906108c
SS
19478 info_ptr += 1;
19479 break;
19480 case DW_FORM_ref2:
9c541725 19481 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19482 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19483 info_ptr += 2;
19484 break;
19485 case DW_FORM_ref4:
9c541725 19486 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19487 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19488 info_ptr += 4;
19489 break;
613e1657 19490 case DW_FORM_ref8:
9c541725 19491 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19492 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19493 info_ptr += 8;
19494 break;
55f1336d 19495 case DW_FORM_ref_sig8:
ac9ec31b 19496 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19497 info_ptr += 8;
19498 break;
c906108c 19499 case DW_FORM_ref_udata:
9c541725 19500 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19501 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19502 info_ptr += bytes_read;
19503 break;
c906108c 19504 case DW_FORM_indirect:
a8329558
KW
19505 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19506 info_ptr += bytes_read;
43988095
JK
19507 if (form == DW_FORM_implicit_const)
19508 {
19509 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19510 info_ptr += bytes_read;
19511 }
19512 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19513 info_ptr);
19514 break;
19515 case DW_FORM_implicit_const:
19516 DW_SND (attr) = implicit_const;
a8329558 19517 break;
3019eac3
DE
19518 case DW_FORM_GNU_addr_index:
19519 if (reader->dwo_file == NULL)
19520 {
19521 /* For now flag a hard error.
19522 Later we can turn this into a complaint. */
19523 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19524 dwarf_form_name (form),
19525 bfd_get_filename (abfd));
19526 }
19527 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19528 info_ptr += bytes_read;
19529 break;
19530 case DW_FORM_GNU_str_index:
19531 if (reader->dwo_file == NULL)
19532 {
19533 /* For now flag a hard error.
19534 Later we can turn this into a complaint if warranted. */
19535 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19536 dwarf_form_name (form),
19537 bfd_get_filename (abfd));
19538 }
19539 {
19540 ULONGEST str_index =
19541 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19542
342587c4 19543 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
19544 DW_STRING_IS_CANONICAL (attr) = 0;
19545 info_ptr += bytes_read;
19546 }
19547 break;
c906108c 19548 default:
8a3fe4f8 19549 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19550 dwarf_form_name (form),
19551 bfd_get_filename (abfd));
c906108c 19552 }
28e94949 19553
36586728 19554 /* Super hack. */
7771576e 19555 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19556 attr->form = DW_FORM_GNU_ref_alt;
19557
28e94949
JB
19558 /* We have seen instances where the compiler tried to emit a byte
19559 size attribute of -1 which ended up being encoded as an unsigned
19560 0xffffffff. Although 0xffffffff is technically a valid size value,
19561 an object of this size seems pretty unlikely so we can relatively
19562 safely treat these cases as if the size attribute was invalid and
19563 treat them as zero by default. */
19564 if (attr->name == DW_AT_byte_size
19565 && form == DW_FORM_data4
19566 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19567 {
19568 complaint
19569 (&symfile_complaints,
43bbcdc2
PH
19570 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19571 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19572 DW_UNSND (attr) = 0;
19573 }
28e94949 19574
c906108c
SS
19575 return info_ptr;
19576}
19577
a8329558
KW
19578/* Read an attribute described by an abbreviated attribute. */
19579
d521ce57 19580static const gdb_byte *
dee91e82
DE
19581read_attribute (const struct die_reader_specs *reader,
19582 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 19583 const gdb_byte *info_ptr)
a8329558
KW
19584{
19585 attr->name = abbrev->name;
43988095
JK
19586 return read_attribute_value (reader, attr, abbrev->form,
19587 abbrev->implicit_const, info_ptr);
a8329558
KW
19588}
19589
0963b4bd 19590/* Read dwarf information from a buffer. */
c906108c
SS
19591
19592static unsigned int
a1855c1d 19593read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19594{
fe1b8b76 19595 return bfd_get_8 (abfd, buf);
c906108c
SS
19596}
19597
19598static int
a1855c1d 19599read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19600{
fe1b8b76 19601 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19602}
19603
19604static unsigned int
a1855c1d 19605read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19606{
fe1b8b76 19607 return bfd_get_16 (abfd, buf);
c906108c
SS
19608}
19609
21ae7a4d 19610static int
a1855c1d 19611read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19612{
19613 return bfd_get_signed_16 (abfd, buf);
19614}
19615
c906108c 19616static unsigned int
a1855c1d 19617read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19618{
fe1b8b76 19619 return bfd_get_32 (abfd, buf);
c906108c
SS
19620}
19621
21ae7a4d 19622static int
a1855c1d 19623read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19624{
19625 return bfd_get_signed_32 (abfd, buf);
19626}
19627
93311388 19628static ULONGEST
a1855c1d 19629read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19630{
fe1b8b76 19631 return bfd_get_64 (abfd, buf);
c906108c
SS
19632}
19633
19634static CORE_ADDR
d521ce57 19635read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19636 unsigned int *bytes_read)
c906108c 19637{
e7c27a73 19638 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19639 CORE_ADDR retval = 0;
19640
107d2387 19641 if (cu_header->signed_addr_p)
c906108c 19642 {
107d2387
AC
19643 switch (cu_header->addr_size)
19644 {
19645 case 2:
fe1b8b76 19646 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19647 break;
19648 case 4:
fe1b8b76 19649 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19650 break;
19651 case 8:
fe1b8b76 19652 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19653 break;
19654 default:
8e65ff28 19655 internal_error (__FILE__, __LINE__,
e2e0b3e5 19656 _("read_address: bad switch, signed [in module %s]"),
659b0389 19657 bfd_get_filename (abfd));
107d2387
AC
19658 }
19659 }
19660 else
19661 {
19662 switch (cu_header->addr_size)
19663 {
19664 case 2:
fe1b8b76 19665 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19666 break;
19667 case 4:
fe1b8b76 19668 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19669 break;
19670 case 8:
fe1b8b76 19671 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19672 break;
19673 default:
8e65ff28 19674 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19675 _("read_address: bad switch, "
19676 "unsigned [in module %s]"),
659b0389 19677 bfd_get_filename (abfd));
107d2387 19678 }
c906108c 19679 }
64367e0a 19680
107d2387
AC
19681 *bytes_read = cu_header->addr_size;
19682 return retval;
c906108c
SS
19683}
19684
f7ef9339 19685/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19686 specification allows the initial length to take up either 4 bytes
19687 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19688 bytes describe the length and all offsets will be 8 bytes in length
19689 instead of 4.
19690
f7ef9339
KB
19691 An older, non-standard 64-bit format is also handled by this
19692 function. The older format in question stores the initial length
19693 as an 8-byte quantity without an escape value. Lengths greater
19694 than 2^32 aren't very common which means that the initial 4 bytes
19695 is almost always zero. Since a length value of zero doesn't make
19696 sense for the 32-bit format, this initial zero can be considered to
19697 be an escape value which indicates the presence of the older 64-bit
19698 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19699 greater than 4GB. If it becomes necessary to handle lengths
19700 somewhat larger than 4GB, we could allow other small values (such
19701 as the non-sensical values of 1, 2, and 3) to also be used as
19702 escape values indicating the presence of the old format.
f7ef9339 19703
917c78fc
MK
19704 The value returned via bytes_read should be used to increment the
19705 relevant pointer after calling read_initial_length().
c764a876 19706
613e1657
KB
19707 [ Note: read_initial_length() and read_offset() are based on the
19708 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19709 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19710 from:
19711
f7ef9339 19712 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19713
613e1657
KB
19714 This document is only a draft and is subject to change. (So beware.)
19715
f7ef9339 19716 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19717 determined empirically by examining 64-bit ELF files produced by
19718 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19719
19720 - Kevin, July 16, 2002
613e1657
KB
19721 ] */
19722
19723static LONGEST
d521ce57 19724read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19725{
fe1b8b76 19726 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19727
dd373385 19728 if (length == 0xffffffff)
613e1657 19729 {
fe1b8b76 19730 length = bfd_get_64 (abfd, buf + 4);
613e1657 19731 *bytes_read = 12;
613e1657 19732 }
dd373385 19733 else if (length == 0)
f7ef9339 19734 {
dd373385 19735 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19736 length = bfd_get_64 (abfd, buf);
f7ef9339 19737 *bytes_read = 8;
f7ef9339 19738 }
613e1657
KB
19739 else
19740 {
19741 *bytes_read = 4;
613e1657
KB
19742 }
19743
c764a876
DE
19744 return length;
19745}
dd373385 19746
c764a876
DE
19747/* Cover function for read_initial_length.
19748 Returns the length of the object at BUF, and stores the size of the
19749 initial length in *BYTES_READ and stores the size that offsets will be in
19750 *OFFSET_SIZE.
19751 If the initial length size is not equivalent to that specified in
19752 CU_HEADER then issue a complaint.
19753 This is useful when reading non-comp-unit headers. */
dd373385 19754
c764a876 19755static LONGEST
d521ce57 19756read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19757 const struct comp_unit_head *cu_header,
19758 unsigned int *bytes_read,
19759 unsigned int *offset_size)
19760{
19761 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19762
19763 gdb_assert (cu_header->initial_length_size == 4
19764 || cu_header->initial_length_size == 8
19765 || cu_header->initial_length_size == 12);
19766
19767 if (cu_header->initial_length_size != *bytes_read)
19768 complaint (&symfile_complaints,
19769 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19770
c764a876 19771 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19772 return length;
613e1657
KB
19773}
19774
19775/* Read an offset from the data stream. The size of the offset is
917c78fc 19776 given by cu_header->offset_size. */
613e1657
KB
19777
19778static LONGEST
d521ce57
TT
19779read_offset (bfd *abfd, const gdb_byte *buf,
19780 const struct comp_unit_head *cu_header,
891d2f0b 19781 unsigned int *bytes_read)
c764a876
DE
19782{
19783 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19784
c764a876
DE
19785 *bytes_read = cu_header->offset_size;
19786 return offset;
19787}
19788
19789/* Read an offset from the data stream. */
19790
19791static LONGEST
d521ce57 19792read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19793{
19794 LONGEST retval = 0;
19795
c764a876 19796 switch (offset_size)
613e1657
KB
19797 {
19798 case 4:
fe1b8b76 19799 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19800 break;
19801 case 8:
fe1b8b76 19802 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19803 break;
19804 default:
8e65ff28 19805 internal_error (__FILE__, __LINE__,
c764a876 19806 _("read_offset_1: bad switch [in module %s]"),
659b0389 19807 bfd_get_filename (abfd));
613e1657
KB
19808 }
19809
917c78fc 19810 return retval;
613e1657
KB
19811}
19812
d521ce57
TT
19813static const gdb_byte *
19814read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19815{
19816 /* If the size of a host char is 8 bits, we can return a pointer
19817 to the buffer, otherwise we have to copy the data to a buffer
19818 allocated on the temporary obstack. */
4bdf3d34 19819 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19820 return buf;
c906108c
SS
19821}
19822
d521ce57
TT
19823static const char *
19824read_direct_string (bfd *abfd, const gdb_byte *buf,
19825 unsigned int *bytes_read_ptr)
c906108c
SS
19826{
19827 /* If the size of a host char is 8 bits, we can return a pointer
19828 to the string, otherwise we have to copy the string to a buffer
19829 allocated on the temporary obstack. */
4bdf3d34 19830 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19831 if (*buf == '\0')
19832 {
19833 *bytes_read_ptr = 1;
19834 return NULL;
19835 }
d521ce57
TT
19836 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19837 return (const char *) buf;
4bdf3d34
JJ
19838}
19839
43988095
JK
19840/* Return pointer to string at section SECT offset STR_OFFSET with error
19841 reporting strings FORM_NAME and SECT_NAME. */
19842
d521ce57 19843static const char *
ed2dc618
SM
19844read_indirect_string_at_offset_from (struct objfile *objfile,
19845 bfd *abfd, LONGEST str_offset,
43988095
JK
19846 struct dwarf2_section_info *sect,
19847 const char *form_name,
19848 const char *sect_name)
19849{
ed2dc618 19850 dwarf2_read_section (objfile, sect);
43988095
JK
19851 if (sect->buffer == NULL)
19852 error (_("%s used without %s section [in module %s]"),
19853 form_name, sect_name, bfd_get_filename (abfd));
19854 if (str_offset >= sect->size)
19855 error (_("%s pointing outside of %s section [in module %s]"),
19856 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19857 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19858 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19859 return NULL;
43988095
JK
19860 return (const char *) (sect->buffer + str_offset);
19861}
19862
19863/* Return pointer to string at .debug_str offset STR_OFFSET. */
19864
19865static const char *
ed2dc618
SM
19866read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19867 bfd *abfd, LONGEST str_offset)
43988095 19868{
ed2dc618
SM
19869 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19870 abfd, str_offset,
43988095
JK
19871 &dwarf2_per_objfile->str,
19872 "DW_FORM_strp", ".debug_str");
19873}
19874
19875/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19876
19877static const char *
ed2dc618
SM
19878read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19879 bfd *abfd, LONGEST str_offset)
43988095 19880{
ed2dc618
SM
19881 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19882 abfd, str_offset,
43988095
JK
19883 &dwarf2_per_objfile->line_str,
19884 "DW_FORM_line_strp",
19885 ".debug_line_str");
c906108c
SS
19886}
19887
36586728
TT
19888/* Read a string at offset STR_OFFSET in the .debug_str section from
19889 the .dwz file DWZ. Throw an error if the offset is too large. If
19890 the string consists of a single NUL byte, return NULL; otherwise
19891 return a pointer to the string. */
19892
d521ce57 19893static const char *
ed2dc618
SM
19894read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19895 LONGEST str_offset)
36586728 19896{
ed2dc618 19897 dwarf2_read_section (objfile, &dwz->str);
36586728
TT
19898
19899 if (dwz->str.buffer == NULL)
19900 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19901 "section [in module %s]"),
19902 bfd_get_filename (dwz->dwz_bfd));
19903 if (str_offset >= dwz->str.size)
19904 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19905 ".debug_str section [in module %s]"),
19906 bfd_get_filename (dwz->dwz_bfd));
19907 gdb_assert (HOST_CHAR_BIT == 8);
19908 if (dwz->str.buffer[str_offset] == '\0')
19909 return NULL;
d521ce57 19910 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
19911}
19912
43988095
JK
19913/* Return pointer to string at .debug_str offset as read from BUF.
19914 BUF is assumed to be in a compilation unit described by CU_HEADER.
19915 Return *BYTES_READ_PTR count of bytes read from BUF. */
19916
d521ce57 19917static const char *
ed2dc618
SM
19918read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19919 const gdb_byte *buf,
cf2c3c16
TT
19920 const struct comp_unit_head *cu_header,
19921 unsigned int *bytes_read_ptr)
19922{
19923 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19924
ed2dc618 19925 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
19926}
19927
43988095
JK
19928/* Return pointer to string at .debug_line_str offset as read from BUF.
19929 BUF is assumed to be in a compilation unit described by CU_HEADER.
19930 Return *BYTES_READ_PTR count of bytes read from BUF. */
19931
19932static const char *
ed2dc618
SM
19933read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19934 bfd *abfd, const gdb_byte *buf,
43988095
JK
19935 const struct comp_unit_head *cu_header,
19936 unsigned int *bytes_read_ptr)
19937{
19938 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19939
ed2dc618
SM
19940 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19941 str_offset);
43988095
JK
19942}
19943
19944ULONGEST
d521ce57 19945read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 19946 unsigned int *bytes_read_ptr)
c906108c 19947{
12df843f 19948 ULONGEST result;
ce5d95e1 19949 unsigned int num_read;
870f88f7 19950 int shift;
c906108c
SS
19951 unsigned char byte;
19952
19953 result = 0;
19954 shift = 0;
19955 num_read = 0;
c906108c
SS
19956 while (1)
19957 {
fe1b8b76 19958 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19959 buf++;
19960 num_read++;
12df843f 19961 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19962 if ((byte & 128) == 0)
19963 {
19964 break;
19965 }
19966 shift += 7;
19967 }
19968 *bytes_read_ptr = num_read;
19969 return result;
19970}
19971
12df843f 19972static LONGEST
d521ce57
TT
19973read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19974 unsigned int *bytes_read_ptr)
c906108c 19975{
12df843f 19976 LONGEST result;
870f88f7 19977 int shift, num_read;
c906108c
SS
19978 unsigned char byte;
19979
19980 result = 0;
19981 shift = 0;
c906108c 19982 num_read = 0;
c906108c
SS
19983 while (1)
19984 {
fe1b8b76 19985 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19986 buf++;
19987 num_read++;
12df843f 19988 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
19989 shift += 7;
19990 if ((byte & 128) == 0)
19991 {
19992 break;
19993 }
19994 }
77e0b926 19995 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 19996 result |= -(((LONGEST) 1) << shift);
c906108c
SS
19997 *bytes_read_ptr = num_read;
19998 return result;
19999}
20000
3019eac3
DE
20001/* Given index ADDR_INDEX in .debug_addr, fetch the value.
20002 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
20003 ADDR_SIZE is the size of addresses from the CU header. */
20004
20005static CORE_ADDR
ed2dc618
SM
20006read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
20007 unsigned int addr_index, ULONGEST addr_base, int addr_size)
3019eac3
DE
20008{
20009 struct objfile *objfile = dwarf2_per_objfile->objfile;
20010 bfd *abfd = objfile->obfd;
20011 const gdb_byte *info_ptr;
20012
20013 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
20014 if (dwarf2_per_objfile->addr.buffer == NULL)
20015 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20016 objfile_name (objfile));
3019eac3
DE
20017 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
20018 error (_("DW_FORM_addr_index pointing outside of "
20019 ".debug_addr section [in module %s]"),
4262abfb 20020 objfile_name (objfile));
3019eac3
DE
20021 info_ptr = (dwarf2_per_objfile->addr.buffer
20022 + addr_base + addr_index * addr_size);
20023 if (addr_size == 4)
20024 return bfd_get_32 (abfd, info_ptr);
20025 else
20026 return bfd_get_64 (abfd, info_ptr);
20027}
20028
20029/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20030
20031static CORE_ADDR
20032read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20033{
518817b3
SM
20034 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
20035 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20036}
20037
20038/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20039
20040static CORE_ADDR
d521ce57 20041read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20042 unsigned int *bytes_read)
20043{
518817b3 20044 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
20045 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20046
20047 return read_addr_index (cu, addr_index);
20048}
20049
20050/* Data structure to pass results from dwarf2_read_addr_index_reader
20051 back to dwarf2_read_addr_index. */
20052
20053struct dwarf2_read_addr_index_data
20054{
20055 ULONGEST addr_base;
20056 int addr_size;
20057};
20058
20059/* die_reader_func for dwarf2_read_addr_index. */
20060
20061static void
20062dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 20063 const gdb_byte *info_ptr,
3019eac3
DE
20064 struct die_info *comp_unit_die,
20065 int has_children,
20066 void *data)
20067{
20068 struct dwarf2_cu *cu = reader->cu;
20069 struct dwarf2_read_addr_index_data *aidata =
20070 (struct dwarf2_read_addr_index_data *) data;
20071
20072 aidata->addr_base = cu->addr_base;
20073 aidata->addr_size = cu->header.addr_size;
20074}
20075
20076/* Given an index in .debug_addr, fetch the value.
20077 NOTE: This can be called during dwarf expression evaluation,
20078 long after the debug information has been read, and thus per_cu->cu
20079 may no longer exist. */
20080
20081CORE_ADDR
20082dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
20083 unsigned int addr_index)
20084{
ed2dc618
SM
20085 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
20086 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
20087 struct dwarf2_cu *cu = per_cu->cu;
20088 ULONGEST addr_base;
20089 int addr_size;
20090
3019eac3
DE
20091 /* We need addr_base and addr_size.
20092 If we don't have PER_CU->cu, we have to get it.
20093 Nasty, but the alternative is storing the needed info in PER_CU,
20094 which at this point doesn't seem justified: it's not clear how frequently
20095 it would get used and it would increase the size of every PER_CU.
20096 Entry points like dwarf2_per_cu_addr_size do a similar thing
20097 so we're not in uncharted territory here.
20098 Alas we need to be a bit more complicated as addr_base is contained
20099 in the DIE.
20100
20101 We don't need to read the entire CU(/TU).
20102 We just need the header and top level die.
a1b64ce1 20103
3019eac3 20104 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20105 For now we skip this optimization. */
3019eac3
DE
20106
20107 if (cu != NULL)
20108 {
20109 addr_base = cu->addr_base;
20110 addr_size = cu->header.addr_size;
20111 }
20112 else
20113 {
20114 struct dwarf2_read_addr_index_data aidata;
20115
a1b64ce1
DE
20116 /* Note: We can't use init_cutu_and_read_dies_simple here,
20117 we need addr_base. */
20118 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
20119 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
20120 addr_base = aidata.addr_base;
20121 addr_size = aidata.addr_size;
20122 }
20123
ed2dc618
SM
20124 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20125 addr_size);
3019eac3
DE
20126}
20127
57d63ce2
DE
20128/* Given a DW_FORM_GNU_str_index, fetch the string.
20129 This is only used by the Fission support. */
3019eac3 20130
d521ce57 20131static const char *
342587c4 20132read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3 20133{
ed2dc618 20134 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
20135 struct dwarf2_per_objfile *dwarf2_per_objfile
20136 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20137 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 20138 const char *objf_name = objfile_name (objfile);
3019eac3 20139 bfd *abfd = objfile->obfd;
73869dc2
DE
20140 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
20141 struct dwarf2_section_info *str_offsets_section =
20142 &reader->dwo_file->sections.str_offsets;
d521ce57 20143 const gdb_byte *info_ptr;
3019eac3 20144 ULONGEST str_offset;
57d63ce2 20145 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 20146
73869dc2
DE
20147 dwarf2_read_section (objfile, str_section);
20148 dwarf2_read_section (objfile, str_offsets_section);
20149 if (str_section->buffer == NULL)
57d63ce2 20150 error (_("%s used without .debug_str.dwo section"
9d8780f0
SM
20151 " in CU at offset %s [in module %s]"),
20152 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20153 if (str_offsets_section->buffer == NULL)
57d63ce2 20154 error (_("%s used without .debug_str_offsets.dwo section"
9d8780f0
SM
20155 " in CU at offset %s [in module %s]"),
20156 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20157 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 20158 error (_("%s pointing outside of .debug_str_offsets.dwo"
9d8780f0
SM
20159 " section in CU at offset %s [in module %s]"),
20160 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20161 info_ptr = (str_offsets_section->buffer
3019eac3
DE
20162 + str_index * cu->header.offset_size);
20163 if (cu->header.offset_size == 4)
20164 str_offset = bfd_get_32 (abfd, info_ptr);
20165 else
20166 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20167 if (str_offset >= str_section->size)
57d63ce2 20168 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20169 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20170 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20171 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20172}
20173
3019eac3
DE
20174/* Return the length of an LEB128 number in BUF. */
20175
20176static int
20177leb128_size (const gdb_byte *buf)
20178{
20179 const gdb_byte *begin = buf;
20180 gdb_byte byte;
20181
20182 while (1)
20183 {
20184 byte = *buf++;
20185 if ((byte & 128) == 0)
20186 return buf - begin;
20187 }
20188}
20189
c906108c 20190static void
e142c38c 20191set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20192{
20193 switch (lang)
20194 {
20195 case DW_LANG_C89:
76bee0cc 20196 case DW_LANG_C99:
0cfd832f 20197 case DW_LANG_C11:
c906108c 20198 case DW_LANG_C:
d1be3247 20199 case DW_LANG_UPC:
e142c38c 20200 cu->language = language_c;
c906108c 20201 break;
9c37b5ae 20202 case DW_LANG_Java:
c906108c 20203 case DW_LANG_C_plus_plus:
0cfd832f
MW
20204 case DW_LANG_C_plus_plus_11:
20205 case DW_LANG_C_plus_plus_14:
e142c38c 20206 cu->language = language_cplus;
c906108c 20207 break;
6aecb9c2
JB
20208 case DW_LANG_D:
20209 cu->language = language_d;
20210 break;
c906108c
SS
20211 case DW_LANG_Fortran77:
20212 case DW_LANG_Fortran90:
b21b22e0 20213 case DW_LANG_Fortran95:
f7de9aab
MW
20214 case DW_LANG_Fortran03:
20215 case DW_LANG_Fortran08:
e142c38c 20216 cu->language = language_fortran;
c906108c 20217 break;
a766d390
DE
20218 case DW_LANG_Go:
20219 cu->language = language_go;
20220 break;
c906108c 20221 case DW_LANG_Mips_Assembler:
e142c38c 20222 cu->language = language_asm;
c906108c
SS
20223 break;
20224 case DW_LANG_Ada83:
8aaf0b47 20225 case DW_LANG_Ada95:
bc5f45f8
JB
20226 cu->language = language_ada;
20227 break;
72019c9c
GM
20228 case DW_LANG_Modula2:
20229 cu->language = language_m2;
20230 break;
fe8e67fd
PM
20231 case DW_LANG_Pascal83:
20232 cu->language = language_pascal;
20233 break;
22566fbd
DJ
20234 case DW_LANG_ObjC:
20235 cu->language = language_objc;
20236 break;
c44af4eb
TT
20237 case DW_LANG_Rust:
20238 case DW_LANG_Rust_old:
20239 cu->language = language_rust;
20240 break;
c906108c
SS
20241 case DW_LANG_Cobol74:
20242 case DW_LANG_Cobol85:
c906108c 20243 default:
e142c38c 20244 cu->language = language_minimal;
c906108c
SS
20245 break;
20246 }
e142c38c 20247 cu->language_defn = language_def (cu->language);
c906108c
SS
20248}
20249
20250/* Return the named attribute or NULL if not there. */
20251
20252static struct attribute *
e142c38c 20253dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20254{
a48e046c 20255 for (;;)
c906108c 20256 {
a48e046c
TT
20257 unsigned int i;
20258 struct attribute *spec = NULL;
20259
20260 for (i = 0; i < die->num_attrs; ++i)
20261 {
20262 if (die->attrs[i].name == name)
20263 return &die->attrs[i];
20264 if (die->attrs[i].name == DW_AT_specification
20265 || die->attrs[i].name == DW_AT_abstract_origin)
20266 spec = &die->attrs[i];
20267 }
20268
20269 if (!spec)
20270 break;
c906108c 20271
f2f0e013 20272 die = follow_die_ref (die, spec, &cu);
f2f0e013 20273 }
c5aa993b 20274
c906108c
SS
20275 return NULL;
20276}
20277
348e048f
DE
20278/* Return the named attribute or NULL if not there,
20279 but do not follow DW_AT_specification, etc.
20280 This is for use in contexts where we're reading .debug_types dies.
20281 Following DW_AT_specification, DW_AT_abstract_origin will take us
20282 back up the chain, and we want to go down. */
20283
20284static struct attribute *
45e58e77 20285dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
20286{
20287 unsigned int i;
20288
20289 for (i = 0; i < die->num_attrs; ++i)
20290 if (die->attrs[i].name == name)
20291 return &die->attrs[i];
20292
20293 return NULL;
20294}
20295
7d45c7c3
KB
20296/* Return the string associated with a string-typed attribute, or NULL if it
20297 is either not found or is of an incorrect type. */
20298
20299static const char *
20300dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20301{
20302 struct attribute *attr;
20303 const char *str = NULL;
20304
20305 attr = dwarf2_attr (die, name, cu);
20306
20307 if (attr != NULL)
20308 {
43988095 20309 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438
L
20310 || attr->form == DW_FORM_string
20311 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 20312 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
20313 str = DW_STRING (attr);
20314 else
20315 complaint (&symfile_complaints,
20316 _("string type expected for attribute %s for "
9d8780f0
SM
20317 "DIE at %s in module %s"),
20318 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 20319 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
20320 }
20321
20322 return str;
20323}
20324
05cf31d1
JB
20325/* Return non-zero iff the attribute NAME is defined for the given DIE,
20326 and holds a non-zero value. This function should only be used for
2dc7f7b3 20327 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20328
20329static int
20330dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20331{
20332 struct attribute *attr = dwarf2_attr (die, name, cu);
20333
20334 return (attr && DW_UNSND (attr));
20335}
20336
3ca72b44 20337static int
e142c38c 20338die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20339{
05cf31d1
JB
20340 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20341 which value is non-zero. However, we have to be careful with
20342 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20343 (via dwarf2_flag_true_p) follows this attribute. So we may
20344 end up accidently finding a declaration attribute that belongs
20345 to a different DIE referenced by the specification attribute,
20346 even though the given DIE does not have a declaration attribute. */
20347 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20348 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20349}
20350
63d06c5c 20351/* Return the die giving the specification for DIE, if there is
f2f0e013 20352 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20353 containing the return value on output. If there is no
20354 specification, but there is an abstract origin, that is
20355 returned. */
63d06c5c
DC
20356
20357static struct die_info *
f2f0e013 20358die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20359{
f2f0e013
DJ
20360 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20361 *spec_cu);
63d06c5c 20362
edb3359d
DJ
20363 if (spec_attr == NULL)
20364 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20365
63d06c5c
DC
20366 if (spec_attr == NULL)
20367 return NULL;
20368 else
f2f0e013 20369 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20370}
c906108c 20371
527f3840
JK
20372/* Stub for free_line_header to match void * callback types. */
20373
20374static void
20375free_line_header_voidp (void *arg)
20376{
9a3c8263 20377 struct line_header *lh = (struct line_header *) arg;
527f3840 20378
fff8551c 20379 delete lh;
527f3840
JK
20380}
20381
fff8551c
PA
20382void
20383line_header::add_include_dir (const char *include_dir)
c906108c 20384{
27e0867f 20385 if (dwarf_line_debug >= 2)
fff8551c
PA
20386 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20387 include_dirs.size () + 1, include_dir);
27e0867f 20388
fff8551c 20389 include_dirs.push_back (include_dir);
debd256d 20390}
6e70227d 20391
fff8551c
PA
20392void
20393line_header::add_file_name (const char *name,
ecfb656c 20394 dir_index d_index,
fff8551c
PA
20395 unsigned int mod_time,
20396 unsigned int length)
debd256d 20397{
27e0867f
DE
20398 if (dwarf_line_debug >= 2)
20399 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 20400 (unsigned) file_names.size () + 1, name);
27e0867f 20401
ecfb656c 20402 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 20403}
6e70227d 20404
83769d0b 20405/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20406
20407static struct dwarf2_section_info *
20408get_debug_line_section (struct dwarf2_cu *cu)
20409{
20410 struct dwarf2_section_info *section;
518817b3
SM
20411 struct dwarf2_per_objfile *dwarf2_per_objfile
20412 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
20413
20414 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20415 DWO file. */
20416 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20417 section = &cu->dwo_unit->dwo_file->sections.line;
20418 else if (cu->per_cu->is_dwz)
20419 {
ed2dc618 20420 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
20421
20422 section = &dwz->line;
20423 }
20424 else
20425 section = &dwarf2_per_objfile->line;
20426
20427 return section;
20428}
20429
43988095
JK
20430/* Read directory or file name entry format, starting with byte of
20431 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20432 entries count and the entries themselves in the described entry
20433 format. */
20434
20435static void
ed2dc618
SM
20436read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20437 bfd *abfd, const gdb_byte **bufp,
43988095
JK
20438 struct line_header *lh,
20439 const struct comp_unit_head *cu_header,
20440 void (*callback) (struct line_header *lh,
20441 const char *name,
ecfb656c 20442 dir_index d_index,
43988095
JK
20443 unsigned int mod_time,
20444 unsigned int length))
20445{
20446 gdb_byte format_count, formati;
20447 ULONGEST data_count, datai;
20448 const gdb_byte *buf = *bufp;
20449 const gdb_byte *format_header_data;
43988095
JK
20450 unsigned int bytes_read;
20451
20452 format_count = read_1_byte (abfd, buf);
20453 buf += 1;
20454 format_header_data = buf;
20455 for (formati = 0; formati < format_count; formati++)
20456 {
20457 read_unsigned_leb128 (abfd, buf, &bytes_read);
20458 buf += bytes_read;
20459 read_unsigned_leb128 (abfd, buf, &bytes_read);
20460 buf += bytes_read;
20461 }
20462
20463 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20464 buf += bytes_read;
20465 for (datai = 0; datai < data_count; datai++)
20466 {
20467 const gdb_byte *format = format_header_data;
20468 struct file_entry fe;
20469
43988095
JK
20470 for (formati = 0; formati < format_count; formati++)
20471 {
ecfb656c 20472 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20473 format += bytes_read;
43988095 20474
ecfb656c 20475 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20476 format += bytes_read;
ecfb656c
PA
20477
20478 gdb::optional<const char *> string;
20479 gdb::optional<unsigned int> uint;
20480
43988095
JK
20481 switch (form)
20482 {
20483 case DW_FORM_string:
ecfb656c 20484 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
20485 buf += bytes_read;
20486 break;
20487
20488 case DW_FORM_line_strp:
ed2dc618
SM
20489 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20490 abfd, buf,
ecfb656c
PA
20491 cu_header,
20492 &bytes_read));
43988095
JK
20493 buf += bytes_read;
20494 break;
20495
20496 case DW_FORM_data1:
ecfb656c 20497 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
20498 buf += 1;
20499 break;
20500
20501 case DW_FORM_data2:
ecfb656c 20502 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
20503 buf += 2;
20504 break;
20505
20506 case DW_FORM_data4:
ecfb656c 20507 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
20508 buf += 4;
20509 break;
20510
20511 case DW_FORM_data8:
ecfb656c 20512 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
20513 buf += 8;
20514 break;
20515
20516 case DW_FORM_udata:
ecfb656c 20517 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
20518 buf += bytes_read;
20519 break;
20520
20521 case DW_FORM_block:
20522 /* It is valid only for DW_LNCT_timestamp which is ignored by
20523 current GDB. */
20524 break;
20525 }
ecfb656c
PA
20526
20527 switch (content_type)
20528 {
20529 case DW_LNCT_path:
20530 if (string.has_value ())
20531 fe.name = *string;
20532 break;
20533 case DW_LNCT_directory_index:
20534 if (uint.has_value ())
20535 fe.d_index = (dir_index) *uint;
20536 break;
20537 case DW_LNCT_timestamp:
20538 if (uint.has_value ())
20539 fe.mod_time = *uint;
20540 break;
20541 case DW_LNCT_size:
20542 if (uint.has_value ())
20543 fe.length = *uint;
20544 break;
20545 case DW_LNCT_MD5:
20546 break;
20547 default:
20548 complaint (&symfile_complaints,
20549 _("Unknown format content type %s"),
20550 pulongest (content_type));
20551 }
43988095
JK
20552 }
20553
ecfb656c 20554 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20555 }
20556
20557 *bufp = buf;
20558}
20559
debd256d 20560/* Read the statement program header starting at OFFSET in
3019eac3 20561 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20562 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20563 Returns NULL if there is a problem reading the header, e.g., if it
20564 has a version we don't understand.
debd256d
JB
20565
20566 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20567 the returned object point into the dwarf line section buffer,
20568 and must not be freed. */
ae2de4f8 20569
fff8551c 20570static line_header_up
9c541725 20571dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20572{
d521ce57 20573 const gdb_byte *line_ptr;
c764a876 20574 unsigned int bytes_read, offset_size;
debd256d 20575 int i;
d521ce57 20576 const char *cur_dir, *cur_file;
3019eac3
DE
20577 struct dwarf2_section_info *section;
20578 bfd *abfd;
518817b3
SM
20579 struct dwarf2_per_objfile *dwarf2_per_objfile
20580 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20581
36586728 20582 section = get_debug_line_section (cu);
3019eac3
DE
20583 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20584 if (section->buffer == NULL)
debd256d 20585 {
3019eac3
DE
20586 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20587 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20588 else
20589 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
20590 return 0;
20591 }
20592
fceca515
DE
20593 /* We can't do this until we know the section is non-empty.
20594 Only then do we know we have such a section. */
a32a8923 20595 abfd = get_section_bfd_owner (section);
fceca515 20596
a738430d
MK
20597 /* Make sure that at least there's room for the total_length field.
20598 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20599 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20600 {
4d3c2250 20601 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20602 return 0;
20603 }
20604
fff8551c 20605 line_header_up lh (new line_header ());
debd256d 20606
9c541725 20607 lh->sect_off = sect_off;
527f3840
JK
20608 lh->offset_in_dwz = cu->per_cu->is_dwz;
20609
9c541725 20610 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20611
a738430d 20612 /* Read in the header. */
6e70227d 20613 lh->total_length =
c764a876
DE
20614 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20615 &bytes_read, &offset_size);
debd256d 20616 line_ptr += bytes_read;
3019eac3 20617 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20618 {
4d3c2250 20619 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20620 return 0;
20621 }
20622 lh->statement_program_end = line_ptr + lh->total_length;
20623 lh->version = read_2_bytes (abfd, line_ptr);
20624 line_ptr += 2;
43988095 20625 if (lh->version > 5)
cd366ee8
DE
20626 {
20627 /* This is a version we don't understand. The format could have
20628 changed in ways we don't handle properly so just punt. */
20629 complaint (&symfile_complaints,
20630 _("unsupported version in .debug_line section"));
20631 return NULL;
20632 }
43988095
JK
20633 if (lh->version >= 5)
20634 {
20635 gdb_byte segment_selector_size;
20636
20637 /* Skip address size. */
20638 read_1_byte (abfd, line_ptr);
20639 line_ptr += 1;
20640
20641 segment_selector_size = read_1_byte (abfd, line_ptr);
20642 line_ptr += 1;
20643 if (segment_selector_size != 0)
20644 {
20645 complaint (&symfile_complaints,
20646 _("unsupported segment selector size %u "
20647 "in .debug_line section"),
20648 segment_selector_size);
20649 return NULL;
20650 }
20651 }
c764a876
DE
20652 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20653 line_ptr += offset_size;
debd256d
JB
20654 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20655 line_ptr += 1;
2dc7f7b3
TT
20656 if (lh->version >= 4)
20657 {
20658 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20659 line_ptr += 1;
20660 }
20661 else
20662 lh->maximum_ops_per_instruction = 1;
20663
20664 if (lh->maximum_ops_per_instruction == 0)
20665 {
20666 lh->maximum_ops_per_instruction = 1;
20667 complaint (&symfile_complaints,
3e43a32a
MS
20668 _("invalid maximum_ops_per_instruction "
20669 "in `.debug_line' section"));
2dc7f7b3
TT
20670 }
20671
debd256d
JB
20672 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20673 line_ptr += 1;
20674 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20675 line_ptr += 1;
20676 lh->line_range = read_1_byte (abfd, line_ptr);
20677 line_ptr += 1;
20678 lh->opcode_base = read_1_byte (abfd, line_ptr);
20679 line_ptr += 1;
fff8551c 20680 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20681
20682 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20683 for (i = 1; i < lh->opcode_base; ++i)
20684 {
20685 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20686 line_ptr += 1;
20687 }
20688
43988095 20689 if (lh->version >= 5)
debd256d 20690 {
43988095 20691 /* Read directory table. */
ed2dc618
SM
20692 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20693 &cu->header,
fff8551c 20694 [] (struct line_header *lh, const char *name,
ecfb656c 20695 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20696 unsigned int length)
20697 {
20698 lh->add_include_dir (name);
20699 });
debd256d 20700
43988095 20701 /* Read file name table. */
ed2dc618
SM
20702 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20703 &cu->header,
fff8551c 20704 [] (struct line_header *lh, const char *name,
ecfb656c 20705 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20706 unsigned int length)
20707 {
ecfb656c 20708 lh->add_file_name (name, d_index, mod_time, length);
fff8551c 20709 });
43988095
JK
20710 }
20711 else
debd256d 20712 {
43988095
JK
20713 /* Read directory table. */
20714 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20715 {
20716 line_ptr += bytes_read;
fff8551c 20717 lh->add_include_dir (cur_dir);
43988095 20718 }
debd256d
JB
20719 line_ptr += bytes_read;
20720
43988095
JK
20721 /* Read file name table. */
20722 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20723 {
ecfb656c
PA
20724 unsigned int mod_time, length;
20725 dir_index d_index;
43988095
JK
20726
20727 line_ptr += bytes_read;
ecfb656c 20728 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20729 line_ptr += bytes_read;
20730 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20731 line_ptr += bytes_read;
20732 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20733 line_ptr += bytes_read;
20734
ecfb656c 20735 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20736 }
20737 line_ptr += bytes_read;
debd256d 20738 }
6e70227d 20739 lh->statement_program_start = line_ptr;
debd256d 20740
3019eac3 20741 if (line_ptr > (section->buffer + section->size))
4d3c2250 20742 complaint (&symfile_complaints,
3e43a32a
MS
20743 _("line number info header doesn't "
20744 "fit in `.debug_line' section"));
debd256d 20745
debd256d
JB
20746 return lh;
20747}
c906108c 20748
c6da4cef
DE
20749/* Subroutine of dwarf_decode_lines to simplify it.
20750 Return the file name of the psymtab for included file FILE_INDEX
20751 in line header LH of PST.
20752 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20753 If space for the result is malloc'd, *NAME_HOLDER will be set.
20754 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20755
d521ce57 20756static const char *
c6da4cef
DE
20757psymtab_include_file_name (const struct line_header *lh, int file_index,
20758 const struct partial_symtab *pst,
c89b44cd
TT
20759 const char *comp_dir,
20760 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20761{
8c43009f 20762 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
20763 const char *include_name = fe.name;
20764 const char *include_name_to_compare = include_name;
72b9f47f 20765 const char *pst_filename;
c6da4cef
DE
20766 int file_is_pst;
20767
8c43009f 20768 const char *dir_name = fe.include_dir (lh);
c6da4cef 20769
c89b44cd 20770 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20771 if (!IS_ABSOLUTE_PATH (include_name)
20772 && (dir_name != NULL || comp_dir != NULL))
20773 {
20774 /* Avoid creating a duplicate psymtab for PST.
20775 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20776 Before we do the comparison, however, we need to account
20777 for DIR_NAME and COMP_DIR.
20778 First prepend dir_name (if non-NULL). If we still don't
20779 have an absolute path prepend comp_dir (if non-NULL).
20780 However, the directory we record in the include-file's
20781 psymtab does not contain COMP_DIR (to match the
20782 corresponding symtab(s)).
20783
20784 Example:
20785
20786 bash$ cd /tmp
20787 bash$ gcc -g ./hello.c
20788 include_name = "hello.c"
20789 dir_name = "."
20790 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20791 DW_AT_name = "./hello.c"
20792
20793 */
c6da4cef
DE
20794
20795 if (dir_name != NULL)
20796 {
c89b44cd
TT
20797 name_holder->reset (concat (dir_name, SLASH_STRING,
20798 include_name, (char *) NULL));
20799 include_name = name_holder->get ();
c6da4cef 20800 include_name_to_compare = include_name;
c6da4cef
DE
20801 }
20802 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20803 {
c89b44cd
TT
20804 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20805 include_name, (char *) NULL));
20806 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20807 }
20808 }
20809
20810 pst_filename = pst->filename;
c89b44cd 20811 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20812 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20813 {
c89b44cd
TT
20814 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20815 pst_filename, (char *) NULL));
20816 pst_filename = copied_name.get ();
c6da4cef
DE
20817 }
20818
1e3fad37 20819 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20820
c6da4cef
DE
20821 if (file_is_pst)
20822 return NULL;
20823 return include_name;
20824}
20825
d9b3de22
DE
20826/* State machine to track the state of the line number program. */
20827
6f77053d 20828class lnp_state_machine
d9b3de22 20829{
6f77053d
PA
20830public:
20831 /* Initialize a machine state for the start of a line number
20832 program. */
20833 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20834
8c43009f
PA
20835 file_entry *current_file ()
20836 {
20837 /* lh->file_names is 0-based, but the file name numbers in the
20838 statement program are 1-based. */
6f77053d
PA
20839 return m_line_header->file_name_at (m_file);
20840 }
20841
20842 /* Record the line in the state machine. END_SEQUENCE is true if
20843 we're processing the end of a sequence. */
20844 void record_line (bool end_sequence);
20845
20846 /* Check address and if invalid nop-out the rest of the lines in this
20847 sequence. */
20848 void check_line_address (struct dwarf2_cu *cu,
20849 const gdb_byte *line_ptr,
20850 CORE_ADDR lowpc, CORE_ADDR address);
20851
20852 void handle_set_discriminator (unsigned int discriminator)
20853 {
20854 m_discriminator = discriminator;
20855 m_line_has_non_zero_discriminator |= discriminator != 0;
20856 }
20857
20858 /* Handle DW_LNE_set_address. */
20859 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20860 {
20861 m_op_index = 0;
20862 address += baseaddr;
20863 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20864 }
20865
20866 /* Handle DW_LNS_advance_pc. */
20867 void handle_advance_pc (CORE_ADDR adjust);
20868
20869 /* Handle a special opcode. */
20870 void handle_special_opcode (unsigned char op_code);
20871
20872 /* Handle DW_LNS_advance_line. */
20873 void handle_advance_line (int line_delta)
20874 {
20875 advance_line (line_delta);
20876 }
20877
20878 /* Handle DW_LNS_set_file. */
20879 void handle_set_file (file_name_index file);
20880
20881 /* Handle DW_LNS_negate_stmt. */
20882 void handle_negate_stmt ()
20883 {
20884 m_is_stmt = !m_is_stmt;
20885 }
20886
20887 /* Handle DW_LNS_const_add_pc. */
20888 void handle_const_add_pc ();
20889
20890 /* Handle DW_LNS_fixed_advance_pc. */
20891 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20892 {
20893 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20894 m_op_index = 0;
20895 }
20896
20897 /* Handle DW_LNS_copy. */
20898 void handle_copy ()
20899 {
20900 record_line (false);
20901 m_discriminator = 0;
20902 }
20903
20904 /* Handle DW_LNE_end_sequence. */
20905 void handle_end_sequence ()
20906 {
20907 m_record_line_callback = ::record_line;
20908 }
20909
20910private:
20911 /* Advance the line by LINE_DELTA. */
20912 void advance_line (int line_delta)
20913 {
20914 m_line += line_delta;
20915
20916 if (line_delta != 0)
20917 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20918 }
20919
6f77053d
PA
20920 gdbarch *m_gdbarch;
20921
20922 /* True if we're recording lines.
20923 Otherwise we're building partial symtabs and are just interested in
20924 finding include files mentioned by the line number program. */
20925 bool m_record_lines_p;
20926
8c43009f 20927 /* The line number header. */
6f77053d 20928 line_header *m_line_header;
8c43009f 20929
6f77053d
PA
20930 /* These are part of the standard DWARF line number state machine,
20931 and initialized according to the DWARF spec. */
d9b3de22 20932
6f77053d 20933 unsigned char m_op_index = 0;
8c43009f 20934 /* The line table index (1-based) of the current file. */
6f77053d
PA
20935 file_name_index m_file = (file_name_index) 1;
20936 unsigned int m_line = 1;
20937
20938 /* These are initialized in the constructor. */
20939
20940 CORE_ADDR m_address;
20941 bool m_is_stmt;
20942 unsigned int m_discriminator;
d9b3de22
DE
20943
20944 /* Additional bits of state we need to track. */
20945
20946 /* The last file that we called dwarf2_start_subfile for.
20947 This is only used for TLLs. */
6f77053d 20948 unsigned int m_last_file = 0;
d9b3de22 20949 /* The last file a line number was recorded for. */
6f77053d 20950 struct subfile *m_last_subfile = NULL;
d9b3de22
DE
20951
20952 /* The function to call to record a line. */
6f77053d 20953 record_line_ftype *m_record_line_callback = NULL;
d9b3de22
DE
20954
20955 /* The last line number that was recorded, used to coalesce
20956 consecutive entries for the same line. This can happen, for
20957 example, when discriminators are present. PR 17276. */
6f77053d
PA
20958 unsigned int m_last_line = 0;
20959 bool m_line_has_non_zero_discriminator = false;
8c43009f 20960};
d9b3de22 20961
6f77053d
PA
20962void
20963lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20964{
20965 CORE_ADDR addr_adj = (((m_op_index + adjust)
20966 / m_line_header->maximum_ops_per_instruction)
20967 * m_line_header->minimum_instruction_length);
20968 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20969 m_op_index = ((m_op_index + adjust)
20970 % m_line_header->maximum_ops_per_instruction);
20971}
d9b3de22 20972
6f77053d
PA
20973void
20974lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20975{
6f77053d
PA
20976 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20977 CORE_ADDR addr_adj = (((m_op_index
20978 + (adj_opcode / m_line_header->line_range))
20979 / m_line_header->maximum_ops_per_instruction)
20980 * m_line_header->minimum_instruction_length);
20981 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20982 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20983 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20984
6f77053d
PA
20985 int line_delta = (m_line_header->line_base
20986 + (adj_opcode % m_line_header->line_range));
20987 advance_line (line_delta);
20988 record_line (false);
20989 m_discriminator = 0;
20990}
d9b3de22 20991
6f77053d
PA
20992void
20993lnp_state_machine::handle_set_file (file_name_index file)
20994{
20995 m_file = file;
20996
20997 const file_entry *fe = current_file ();
20998 if (fe == NULL)
20999 dwarf2_debug_line_missing_file_complaint ();
21000 else if (m_record_lines_p)
21001 {
21002 const char *dir = fe->include_dir (m_line_header);
21003
21004 m_last_subfile = current_subfile;
21005 m_line_has_non_zero_discriminator = m_discriminator != 0;
21006 dwarf2_start_subfile (fe->name, dir);
21007 }
21008}
21009
21010void
21011lnp_state_machine::handle_const_add_pc ()
21012{
21013 CORE_ADDR adjust
21014 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21015
21016 CORE_ADDR addr_adj
21017 = (((m_op_index + adjust)
21018 / m_line_header->maximum_ops_per_instruction)
21019 * m_line_header->minimum_instruction_length);
21020
21021 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21022 m_op_index = ((m_op_index + adjust)
21023 % m_line_header->maximum_ops_per_instruction);
21024}
d9b3de22 21025
c91513d8
PP
21026/* Ignore this record_line request. */
21027
21028static void
21029noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
21030{
21031 return;
21032}
21033
a05a36a5
DE
21034/* Return non-zero if we should add LINE to the line number table.
21035 LINE is the line to add, LAST_LINE is the last line that was added,
21036 LAST_SUBFILE is the subfile for LAST_LINE.
21037 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21038 had a non-zero discriminator.
21039
21040 We have to be careful in the presence of discriminators.
21041 E.g., for this line:
21042
21043 for (i = 0; i < 100000; i++);
21044
21045 clang can emit four line number entries for that one line,
21046 each with a different discriminator.
21047 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21048
21049 However, we want gdb to coalesce all four entries into one.
21050 Otherwise the user could stepi into the middle of the line and
21051 gdb would get confused about whether the pc really was in the
21052 middle of the line.
21053
21054 Things are further complicated by the fact that two consecutive
21055 line number entries for the same line is a heuristic used by gcc
21056 to denote the end of the prologue. So we can't just discard duplicate
21057 entries, we have to be selective about it. The heuristic we use is
21058 that we only collapse consecutive entries for the same line if at least
21059 one of those entries has a non-zero discriminator. PR 17276.
21060
21061 Note: Addresses in the line number state machine can never go backwards
21062 within one sequence, thus this coalescing is ok. */
21063
21064static int
21065dwarf_record_line_p (unsigned int line, unsigned int last_line,
21066 int line_has_non_zero_discriminator,
21067 struct subfile *last_subfile)
21068{
21069 if (current_subfile != last_subfile)
21070 return 1;
21071 if (line != last_line)
21072 return 1;
21073 /* Same line for the same file that we've seen already.
21074 As a last check, for pr 17276, only record the line if the line
21075 has never had a non-zero discriminator. */
21076 if (!line_has_non_zero_discriminator)
21077 return 1;
21078 return 0;
21079}
21080
252a6764
DE
21081/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
21082 in the line table of subfile SUBFILE. */
21083
21084static void
d9b3de22
DE
21085dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21086 unsigned int line, CORE_ADDR address,
21087 record_line_ftype p_record_line)
252a6764
DE
21088{
21089 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21090
27e0867f
DE
21091 if (dwarf_line_debug)
21092 {
21093 fprintf_unfiltered (gdb_stdlog,
21094 "Recording line %u, file %s, address %s\n",
21095 line, lbasename (subfile->name),
21096 paddress (gdbarch, address));
21097 }
21098
d5962de5 21099 (*p_record_line) (subfile, line, addr);
252a6764
DE
21100}
21101
21102/* Subroutine of dwarf_decode_lines_1 to simplify it.
21103 Mark the end of a set of line number records.
d9b3de22 21104 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
21105 If SUBFILE is NULL the request is ignored. */
21106
21107static void
21108dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21109 CORE_ADDR address, record_line_ftype p_record_line)
21110{
27e0867f
DE
21111 if (subfile == NULL)
21112 return;
21113
21114 if (dwarf_line_debug)
21115 {
21116 fprintf_unfiltered (gdb_stdlog,
21117 "Finishing current line, file %s, address %s\n",
21118 lbasename (subfile->name),
21119 paddress (gdbarch, address));
21120 }
21121
d9b3de22
DE
21122 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
21123}
21124
6f77053d
PA
21125void
21126lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21127{
d9b3de22
DE
21128 if (dwarf_line_debug)
21129 {
21130 fprintf_unfiltered (gdb_stdlog,
21131 "Processing actual line %u: file %u,"
21132 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
21133 m_line, to_underlying (m_file),
21134 paddress (m_gdbarch, m_address),
21135 m_is_stmt, m_discriminator);
d9b3de22
DE
21136 }
21137
6f77053d 21138 file_entry *fe = current_file ();
8c43009f
PA
21139
21140 if (fe == NULL)
d9b3de22
DE
21141 dwarf2_debug_line_missing_file_complaint ();
21142 /* For now we ignore lines not starting on an instruction boundary.
21143 But not when processing end_sequence for compatibility with the
21144 previous version of the code. */
6f77053d 21145 else if (m_op_index == 0 || end_sequence)
d9b3de22 21146 {
8c43009f 21147 fe->included_p = 1;
6f77053d 21148 if (m_record_lines_p && m_is_stmt)
d9b3de22 21149 {
6f77053d 21150 if (m_last_subfile != current_subfile || end_sequence)
d9b3de22 21151 {
6f77053d
PA
21152 dwarf_finish_line (m_gdbarch, m_last_subfile,
21153 m_address, m_record_line_callback);
d9b3de22
DE
21154 }
21155
21156 if (!end_sequence)
21157 {
6f77053d
PA
21158 if (dwarf_record_line_p (m_line, m_last_line,
21159 m_line_has_non_zero_discriminator,
21160 m_last_subfile))
d9b3de22 21161 {
6f77053d
PA
21162 dwarf_record_line_1 (m_gdbarch, current_subfile,
21163 m_line, m_address,
21164 m_record_line_callback);
d9b3de22 21165 }
6f77053d
PA
21166 m_last_subfile = current_subfile;
21167 m_last_line = m_line;
d9b3de22
DE
21168 }
21169 }
21170 }
21171}
21172
6f77053d
PA
21173lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
21174 bool record_lines_p)
d9b3de22 21175{
6f77053d
PA
21176 m_gdbarch = arch;
21177 m_record_lines_p = record_lines_p;
21178 m_line_header = lh;
d9b3de22 21179
6f77053d 21180 m_record_line_callback = ::record_line;
d9b3de22 21181
d9b3de22
DE
21182 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21183 was a line entry for it so that the backend has a chance to adjust it
21184 and also record it in case it needs it. This is currently used by MIPS
21185 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21186 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21187 m_is_stmt = lh->default_is_stmt;
21188 m_discriminator = 0;
252a6764
DE
21189}
21190
6f77053d
PA
21191void
21192lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21193 const gdb_byte *line_ptr,
21194 CORE_ADDR lowpc, CORE_ADDR address)
924c2928
DE
21195{
21196 /* If address < lowpc then it's not a usable value, it's outside the
21197 pc range of the CU. However, we restrict the test to only address
21198 values of zero to preserve GDB's previous behaviour which is to
21199 handle the specific case of a function being GC'd by the linker. */
21200
21201 if (address == 0 && address < lowpc)
21202 {
21203 /* This line table is for a function which has been
21204 GCd by the linker. Ignore it. PR gdb/12528 */
21205
518817b3 21206 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
21207 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21208
21209 complaint (&symfile_complaints,
21210 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21211 line_offset, objfile_name (objfile));
6f77053d
PA
21212 m_record_line_callback = noop_record_line;
21213 /* Note: record_line_callback is left as noop_record_line until
21214 we see DW_LNE_end_sequence. */
924c2928
DE
21215 }
21216}
21217
f3f5162e 21218/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21219 Process the line number information in LH.
21220 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21221 program in order to set included_p for every referenced header. */
debd256d 21222
c906108c 21223static void
43f3e411
DE
21224dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21225 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21226{
d521ce57
TT
21227 const gdb_byte *line_ptr, *extended_end;
21228 const gdb_byte *line_end;
a8c50c1f 21229 unsigned int bytes_read, extended_len;
699ca60a 21230 unsigned char op_code, extended_op;
e142c38c 21231 CORE_ADDR baseaddr;
518817b3 21232 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21233 bfd *abfd = objfile->obfd;
fbf65064 21234 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
21235 /* True if we're recording line info (as opposed to building partial
21236 symtabs and just interested in finding include files mentioned by
21237 the line number program). */
21238 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
21239
21240 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21241
debd256d
JB
21242 line_ptr = lh->statement_program_start;
21243 line_end = lh->statement_program_end;
c906108c
SS
21244
21245 /* Read the statement sequences until there's nothing left. */
21246 while (line_ptr < line_end)
21247 {
6f77053d
PA
21248 /* The DWARF line number program state machine. Reset the state
21249 machine at the start of each sequence. */
21250 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
21251 bool end_sequence = false;
d9b3de22 21252
8c43009f 21253 if (record_lines_p)
c906108c 21254 {
8c43009f
PA
21255 /* Start a subfile for the current file of the state
21256 machine. */
21257 const file_entry *fe = state_machine.current_file ();
21258
21259 if (fe != NULL)
21260 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
c906108c
SS
21261 }
21262
a738430d 21263 /* Decode the table. */
d9b3de22 21264 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21265 {
21266 op_code = read_1_byte (abfd, line_ptr);
21267 line_ptr += 1;
9aa1fe7e 21268
debd256d 21269 if (op_code >= lh->opcode_base)
6e70227d 21270 {
8e07a239 21271 /* Special opcode. */
6f77053d 21272 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21273 }
21274 else switch (op_code)
c906108c
SS
21275 {
21276 case DW_LNS_extended_op:
3e43a32a
MS
21277 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21278 &bytes_read);
473b7be6 21279 line_ptr += bytes_read;
a8c50c1f 21280 extended_end = line_ptr + extended_len;
c906108c
SS
21281 extended_op = read_1_byte (abfd, line_ptr);
21282 line_ptr += 1;
21283 switch (extended_op)
21284 {
21285 case DW_LNE_end_sequence:
6f77053d
PA
21286 state_machine.handle_end_sequence ();
21287 end_sequence = true;
c906108c
SS
21288 break;
21289 case DW_LNE_set_address:
d9b3de22
DE
21290 {
21291 CORE_ADDR address
21292 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 21293 line_ptr += bytes_read;
6f77053d
PA
21294
21295 state_machine.check_line_address (cu, line_ptr,
21296 lowpc, address);
21297 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21298 }
c906108c
SS
21299 break;
21300 case DW_LNE_define_file:
debd256d 21301 {
d521ce57 21302 const char *cur_file;
ecfb656c
PA
21303 unsigned int mod_time, length;
21304 dir_index dindex;
6e70227d 21305
3e43a32a
MS
21306 cur_file = read_direct_string (abfd, line_ptr,
21307 &bytes_read);
debd256d 21308 line_ptr += bytes_read;
ecfb656c 21309 dindex = (dir_index)
debd256d
JB
21310 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21311 line_ptr += bytes_read;
21312 mod_time =
21313 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21314 line_ptr += bytes_read;
21315 length =
21316 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21317 line_ptr += bytes_read;
ecfb656c 21318 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 21319 }
c906108c 21320 break;
d0c6ba3d 21321 case DW_LNE_set_discriminator:
6f77053d
PA
21322 {
21323 /* The discriminator is not interesting to the
21324 debugger; just ignore it. We still need to
21325 check its value though:
21326 if there are consecutive entries for the same
21327 (non-prologue) line we want to coalesce them.
21328 PR 17276. */
21329 unsigned int discr
21330 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21331 line_ptr += bytes_read;
21332
21333 state_machine.handle_set_discriminator (discr);
21334 }
d0c6ba3d 21335 break;
c906108c 21336 default:
4d3c2250 21337 complaint (&symfile_complaints,
e2e0b3e5 21338 _("mangled .debug_line section"));
debd256d 21339 return;
c906108c 21340 }
a8c50c1f
DJ
21341 /* Make sure that we parsed the extended op correctly. If e.g.
21342 we expected a different address size than the producer used,
21343 we may have read the wrong number of bytes. */
21344 if (line_ptr != extended_end)
21345 {
21346 complaint (&symfile_complaints,
21347 _("mangled .debug_line section"));
21348 return;
21349 }
c906108c
SS
21350 break;
21351 case DW_LNS_copy:
6f77053d 21352 state_machine.handle_copy ();
c906108c
SS
21353 break;
21354 case DW_LNS_advance_pc:
2dc7f7b3
TT
21355 {
21356 CORE_ADDR adjust
21357 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21358 line_ptr += bytes_read;
6f77053d
PA
21359
21360 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21361 }
c906108c
SS
21362 break;
21363 case DW_LNS_advance_line:
a05a36a5
DE
21364 {
21365 int line_delta
21366 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21367 line_ptr += bytes_read;
6f77053d
PA
21368
21369 state_machine.handle_advance_line (line_delta);
a05a36a5 21370 }
c906108c
SS
21371 break;
21372 case DW_LNS_set_file:
d9b3de22 21373 {
6f77053d 21374 file_name_index file
ecfb656c
PA
21375 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21376 &bytes_read);
d9b3de22 21377 line_ptr += bytes_read;
8c43009f 21378
6f77053d 21379 state_machine.handle_set_file (file);
d9b3de22 21380 }
c906108c
SS
21381 break;
21382 case DW_LNS_set_column:
0ad93d4f 21383 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21384 line_ptr += bytes_read;
21385 break;
21386 case DW_LNS_negate_stmt:
6f77053d 21387 state_machine.handle_negate_stmt ();
c906108c
SS
21388 break;
21389 case DW_LNS_set_basic_block:
c906108c 21390 break;
c2c6d25f
JM
21391 /* Add to the address register of the state machine the
21392 address increment value corresponding to special opcode
a738430d
MK
21393 255. I.e., this value is scaled by the minimum
21394 instruction length since special opcode 255 would have
b021a221 21395 scaled the increment. */
c906108c 21396 case DW_LNS_const_add_pc:
6f77053d 21397 state_machine.handle_const_add_pc ();
c906108c
SS
21398 break;
21399 case DW_LNS_fixed_advance_pc:
3e29f34a 21400 {
6f77053d 21401 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21402 line_ptr += 2;
6f77053d
PA
21403
21404 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21405 }
c906108c 21406 break;
9aa1fe7e 21407 default:
a738430d
MK
21408 {
21409 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21410 int i;
a738430d 21411
debd256d 21412 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21413 {
21414 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21415 line_ptr += bytes_read;
21416 }
21417 }
c906108c
SS
21418 }
21419 }
d9b3de22
DE
21420
21421 if (!end_sequence)
21422 dwarf2_debug_line_missing_end_sequence_complaint ();
21423
21424 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21425 in which case we still finish recording the last line). */
6f77053d 21426 state_machine.record_line (true);
c906108c 21427 }
f3f5162e
DE
21428}
21429
21430/* Decode the Line Number Program (LNP) for the given line_header
21431 structure and CU. The actual information extracted and the type
21432 of structures created from the LNP depends on the value of PST.
21433
21434 1. If PST is NULL, then this procedure uses the data from the program
21435 to create all necessary symbol tables, and their linetables.
21436
21437 2. If PST is not NULL, this procedure reads the program to determine
21438 the list of files included by the unit represented by PST, and
21439 builds all the associated partial symbol tables.
21440
21441 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21442 It is used for relative paths in the line table.
21443 NOTE: When processing partial symtabs (pst != NULL),
21444 comp_dir == pst->dirname.
21445
21446 NOTE: It is important that psymtabs have the same file name (via strcmp)
21447 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21448 symtab we don't use it in the name of the psymtabs we create.
21449 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21450 A good testcase for this is mb-inline.exp.
21451
527f3840
JK
21452 LOWPC is the lowest address in CU (or 0 if not known).
21453
21454 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21455 for its PC<->lines mapping information. Otherwise only the filename
21456 table is read in. */
f3f5162e
DE
21457
21458static void
21459dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 21460 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 21461 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21462{
518817b3 21463 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21464 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21465
527f3840
JK
21466 if (decode_mapping)
21467 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21468
21469 if (decode_for_pst_p)
21470 {
21471 int file_index;
21472
21473 /* Now that we're done scanning the Line Header Program, we can
21474 create the psymtab of each included file. */
fff8551c 21475 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
21476 if (lh->file_names[file_index].included_p == 1)
21477 {
c89b44cd 21478 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21479 const char *include_name =
c89b44cd
TT
21480 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21481 &name_holder);
c6da4cef 21482 if (include_name != NULL)
aaa75496
JB
21483 dwarf2_create_include_psymtab (include_name, pst, objfile);
21484 }
21485 }
cb1df416
DJ
21486 else
21487 {
21488 /* Make sure a symtab is created for every file, even files
21489 which contain only variables (i.e. no code with associated
21490 line numbers). */
43f3e411 21491 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 21492 int i;
cb1df416 21493
fff8551c 21494 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 21495 {
8c43009f 21496 file_entry &fe = lh->file_names[i];
9a619af0 21497
8c43009f 21498 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
cb1df416 21499
cb1df416 21500 if (current_subfile->symtab == NULL)
43f3e411
DE
21501 {
21502 current_subfile->symtab
21503 = allocate_symtab (cust, current_subfile->name);
21504 }
8c43009f 21505 fe.symtab = current_subfile->symtab;
cb1df416
DJ
21506 }
21507 }
c906108c
SS
21508}
21509
21510/* Start a subfile for DWARF. FILENAME is the name of the file and
21511 DIRNAME the name of the source directory which contains FILENAME
4d663531 21512 or NULL if not known.
c906108c
SS
21513 This routine tries to keep line numbers from identical absolute and
21514 relative file names in a common subfile.
21515
21516 Using the `list' example from the GDB testsuite, which resides in
21517 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21518 of /srcdir/list0.c yields the following debugging information for list0.c:
21519
c5aa993b 21520 DW_AT_name: /srcdir/list0.c
4d663531 21521 DW_AT_comp_dir: /compdir
357e46e7 21522 files.files[0].name: list0.h
c5aa993b 21523 files.files[0].dir: /srcdir
357e46e7 21524 files.files[1].name: list0.c
c5aa993b 21525 files.files[1].dir: /srcdir
c906108c
SS
21526
21527 The line number information for list0.c has to end up in a single
4f1520fb
FR
21528 subfile, so that `break /srcdir/list0.c:1' works as expected.
21529 start_subfile will ensure that this happens provided that we pass the
21530 concatenation of files.files[1].dir and files.files[1].name as the
21531 subfile's name. */
c906108c
SS
21532
21533static void
4d663531 21534dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 21535{
d521ce57 21536 char *copy = NULL;
4f1520fb 21537
4d663531 21538 /* In order not to lose the line information directory,
4f1520fb
FR
21539 we concatenate it to the filename when it makes sense.
21540 Note that the Dwarf3 standard says (speaking of filenames in line
21541 information): ``The directory index is ignored for file names
21542 that represent full path names''. Thus ignoring dirname in the
21543 `else' branch below isn't an issue. */
c906108c 21544
d5166ae1 21545 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
21546 {
21547 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21548 filename = copy;
21549 }
c906108c 21550
4d663531 21551 start_subfile (filename);
4f1520fb 21552
d521ce57
TT
21553 if (copy != NULL)
21554 xfree (copy);
c906108c
SS
21555}
21556
f4dc4d17
DE
21557/* Start a symtab for DWARF.
21558 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21559
43f3e411 21560static struct compunit_symtab *
f4dc4d17 21561dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 21562 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 21563{
43f3e411 21564 struct compunit_symtab *cust
518817b3
SM
21565 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21566 low_pc, cu->language);
43f3e411 21567
f4dc4d17
DE
21568 record_debugformat ("DWARF 2");
21569 record_producer (cu->producer);
21570
21571 /* We assume that we're processing GCC output. */
21572 processing_gcc_compilation = 2;
21573
4d4ec4e5 21574 cu->processing_has_namespace_info = 0;
43f3e411
DE
21575
21576 return cust;
f4dc4d17
DE
21577}
21578
4c2df51b
DJ
21579static void
21580var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21581 struct dwarf2_cu *cu)
4c2df51b 21582{
518817b3 21583 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21584 struct comp_unit_head *cu_header = &cu->header;
21585
4c2df51b
DJ
21586 /* NOTE drow/2003-01-30: There used to be a comment and some special
21587 code here to turn a symbol with DW_AT_external and a
21588 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21589 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21590 with some versions of binutils) where shared libraries could have
21591 relocations against symbols in their debug information - the
21592 minimal symbol would have the right address, but the debug info
21593 would not. It's no longer necessary, because we will explicitly
21594 apply relocations when we read in the debug information now. */
21595
21596 /* A DW_AT_location attribute with no contents indicates that a
21597 variable has been optimized away. */
21598 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21599 {
f1e6e072 21600 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21601 return;
21602 }
21603
21604 /* Handle one degenerate form of location expression specially, to
21605 preserve GDB's previous behavior when section offsets are
3019eac3
DE
21606 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21607 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21608
21609 if (attr_form_is_block (attr)
3019eac3
DE
21610 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21611 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21612 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21613 && (DW_BLOCK (attr)->size
21614 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21615 {
891d2f0b 21616 unsigned int dummy;
4c2df51b 21617
3019eac3
DE
21618 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21619 SYMBOL_VALUE_ADDRESS (sym) =
21620 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21621 else
21622 SYMBOL_VALUE_ADDRESS (sym) =
21623 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 21624 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
21625 fixup_symbol_section (sym, objfile);
21626 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21627 SYMBOL_SECTION (sym));
4c2df51b
DJ
21628 return;
21629 }
21630
21631 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21632 expression evaluator, and use LOC_COMPUTED only when necessary
21633 (i.e. when the value of a register or memory location is
21634 referenced, or a thread-local block, etc.). Then again, it might
21635 not be worthwhile. I'm assuming that it isn't unless performance
21636 or memory numbers show me otherwise. */
21637
f1e6e072 21638 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21639
f1e6e072 21640 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 21641 cu->has_loclist = 1;
4c2df51b
DJ
21642}
21643
c906108c
SS
21644/* Given a pointer to a DWARF information entry, figure out if we need
21645 to make a symbol table entry for it, and if so, create a new entry
21646 and return a pointer to it.
21647 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21648 used the passed type.
21649 If SPACE is not NULL, use it to hold the new symbol. If it is
21650 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21651
21652static struct symbol *
5e2db402
TT
21653new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21654 struct symbol *space)
c906108c 21655{
518817b3
SM
21656 struct dwarf2_per_objfile *dwarf2_per_objfile
21657 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21658 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21659 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21660 struct symbol *sym = NULL;
15d034d0 21661 const char *name;
c906108c
SS
21662 struct attribute *attr = NULL;
21663 struct attribute *attr2 = NULL;
e142c38c 21664 CORE_ADDR baseaddr;
e37fd15a
SW
21665 struct pending **list_to_add = NULL;
21666
edb3359d 21667 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
21668
21669 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21670
94af9270 21671 name = dwarf2_name (die, cu);
c906108c
SS
21672 if (name)
21673 {
94af9270 21674 const char *linkagename;
34eaf542 21675 int suppress_add = 0;
94af9270 21676
34eaf542
TT
21677 if (space)
21678 sym = space;
21679 else
e623cf5d 21680 sym = allocate_symbol (objfile);
c906108c 21681 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21682
21683 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 21684 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
21685 linkagename = dwarf2_physname (name, die, cu);
21686 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 21687
f55ee35c
JK
21688 /* Fortran does not have mangling standard and the mangling does differ
21689 between gfortran, iFort etc. */
21690 if (cu->language == language_fortran
b250c185 21691 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 21692 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 21693 dwarf2_full_name (name, die, cu),
29df156d 21694 NULL);
f55ee35c 21695
c906108c 21696 /* Default assumptions.
c5aa993b 21697 Use the passed type or decode it from the die. */
176620f1 21698 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21699 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21700 if (type != NULL)
21701 SYMBOL_TYPE (sym) = type;
21702 else
e7c27a73 21703 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21704 attr = dwarf2_attr (die,
21705 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21706 cu);
c906108c
SS
21707 if (attr)
21708 {
21709 SYMBOL_LINE (sym) = DW_UNSND (attr);
21710 }
cb1df416 21711
edb3359d
DJ
21712 attr = dwarf2_attr (die,
21713 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21714 cu);
cb1df416
DJ
21715 if (attr)
21716 {
ecfb656c 21717 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21718 struct file_entry *fe;
9a619af0 21719
ecfb656c
PA
21720 if (cu->line_header != NULL)
21721 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21722 else
21723 fe = NULL;
21724
21725 if (fe == NULL)
cb1df416
DJ
21726 complaint (&symfile_complaints,
21727 _("file index out of range"));
8c43009f
PA
21728 else
21729 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21730 }
21731
c906108c
SS
21732 switch (die->tag)
21733 {
21734 case DW_TAG_label:
e142c38c 21735 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 21736 if (attr)
3e29f34a
MR
21737 {
21738 CORE_ADDR addr;
21739
21740 addr = attr_value_as_address (attr);
21741 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21742 SYMBOL_VALUE_ADDRESS (sym) = addr;
21743 }
0f5238ed
TT
21744 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21745 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21746 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 21747 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21748 break;
21749 case DW_TAG_subprogram:
21750 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21751 finish_block. */
f1e6e072 21752 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21753 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
21754 if ((attr2 && (DW_UNSND (attr2) != 0))
21755 || cu->language == language_ada)
c906108c 21756 {
2cfa0c8d
JB
21757 /* Subprograms marked external are stored as a global symbol.
21758 Ada subprograms, whether marked external or not, are always
21759 stored as a global symbol, because we want to be able to
21760 access them globally. For instance, we want to be able
21761 to break on a nested subprogram without having to
21762 specify the context. */
e37fd15a 21763 list_to_add = &global_symbols;
c906108c
SS
21764 }
21765 else
21766 {
e37fd15a 21767 list_to_add = cu->list_in_scope;
c906108c
SS
21768 }
21769 break;
edb3359d
DJ
21770 case DW_TAG_inlined_subroutine:
21771 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21772 finish_block. */
f1e6e072 21773 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21774 SYMBOL_INLINED (sym) = 1;
481860b3 21775 list_to_add = cu->list_in_scope;
edb3359d 21776 break;
34eaf542
TT
21777 case DW_TAG_template_value_param:
21778 suppress_add = 1;
21779 /* Fall through. */
72929c62 21780 case DW_TAG_constant:
c906108c 21781 case DW_TAG_variable:
254e6b9e 21782 case DW_TAG_member:
0963b4bd
MS
21783 /* Compilation with minimal debug info may result in
21784 variables with missing type entries. Change the
21785 misleading `void' type to something sensible. */
c906108c 21786 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21787 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21788
e142c38c 21789 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21790 /* In the case of DW_TAG_member, we should only be called for
21791 static const members. */
21792 if (die->tag == DW_TAG_member)
21793 {
3863f96c
DE
21794 /* dwarf2_add_field uses die_is_declaration,
21795 so we do the same. */
254e6b9e
DE
21796 gdb_assert (die_is_declaration (die, cu));
21797 gdb_assert (attr);
21798 }
c906108c
SS
21799 if (attr)
21800 {
e7c27a73 21801 dwarf2_const_value (attr, sym, cu);
e142c38c 21802 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21803 if (!suppress_add)
34eaf542
TT
21804 {
21805 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 21806 list_to_add = &global_symbols;
34eaf542 21807 else
e37fd15a 21808 list_to_add = cu->list_in_scope;
34eaf542 21809 }
c906108c
SS
21810 break;
21811 }
e142c38c 21812 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21813 if (attr)
21814 {
e7c27a73 21815 var_decode_location (attr, sym, cu);
e142c38c 21816 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21817
21818 /* Fortran explicitly imports any global symbols to the local
21819 scope by DW_TAG_common_block. */
21820 if (cu->language == language_fortran && die->parent
21821 && die->parent->tag == DW_TAG_common_block)
21822 attr2 = NULL;
21823
caac4577
JG
21824 if (SYMBOL_CLASS (sym) == LOC_STATIC
21825 && SYMBOL_VALUE_ADDRESS (sym) == 0
21826 && !dwarf2_per_objfile->has_section_at_zero)
21827 {
21828 /* When a static variable is eliminated by the linker,
21829 the corresponding debug information is not stripped
21830 out, but the variable address is set to null;
21831 do not add such variables into symbol table. */
21832 }
21833 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21834 {
f55ee35c
JK
21835 /* Workaround gfortran PR debug/40040 - it uses
21836 DW_AT_location for variables in -fPIC libraries which may
21837 get overriden by other libraries/executable and get
21838 a different address. Resolve it by the minimal symbol
21839 which may come from inferior's executable using copy
21840 relocation. Make this workaround only for gfortran as for
21841 other compilers GDB cannot guess the minimal symbol
21842 Fortran mangling kind. */
21843 if (cu->language == language_fortran && die->parent
21844 && die->parent->tag == DW_TAG_module
21845 && cu->producer
28586665 21846 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 21847 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 21848
1c809c68
TT
21849 /* A variable with DW_AT_external is never static,
21850 but it may be block-scoped. */
21851 list_to_add = (cu->list_in_scope == &file_symbols
21852 ? &global_symbols : cu->list_in_scope);
1c809c68 21853 }
c906108c 21854 else
e37fd15a 21855 list_to_add = cu->list_in_scope;
c906108c
SS
21856 }
21857 else
21858 {
21859 /* We do not know the address of this symbol.
c5aa993b
JM
21860 If it is an external symbol and we have type information
21861 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21862 The address of the variable will then be determined from
21863 the minimal symbol table whenever the variable is
21864 referenced. */
e142c38c 21865 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21866
21867 /* Fortran explicitly imports any global symbols to the local
21868 scope by DW_TAG_common_block. */
21869 if (cu->language == language_fortran && die->parent
21870 && die->parent->tag == DW_TAG_common_block)
21871 {
21872 /* SYMBOL_CLASS doesn't matter here because
21873 read_common_block is going to reset it. */
21874 if (!suppress_add)
21875 list_to_add = cu->list_in_scope;
21876 }
21877 else if (attr2 && (DW_UNSND (attr2) != 0)
21878 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21879 {
0fe7935b
DJ
21880 /* A variable with DW_AT_external is never static, but it
21881 may be block-scoped. */
21882 list_to_add = (cu->list_in_scope == &file_symbols
21883 ? &global_symbols : cu->list_in_scope);
21884
f1e6e072 21885 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21886 }
442ddf59
JK
21887 else if (!die_is_declaration (die, cu))
21888 {
21889 /* Use the default LOC_OPTIMIZED_OUT class. */
21890 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21891 if (!suppress_add)
21892 list_to_add = cu->list_in_scope;
442ddf59 21893 }
c906108c
SS
21894 }
21895 break;
21896 case DW_TAG_formal_parameter:
edb3359d
DJ
21897 /* If we are inside a function, mark this as an argument. If
21898 not, we might be looking at an argument to an inlined function
21899 when we do not have enough information to show inlined frames;
21900 pretend it's a local variable in that case so that the user can
21901 still see it. */
21902 if (context_stack_depth > 0
21903 && context_stack[context_stack_depth - 1].name != NULL)
21904 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 21905 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21906 if (attr)
21907 {
e7c27a73 21908 var_decode_location (attr, sym, cu);
c906108c 21909 }
e142c38c 21910 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21911 if (attr)
21912 {
e7c27a73 21913 dwarf2_const_value (attr, sym, cu);
c906108c 21914 }
f346a30d 21915
e37fd15a 21916 list_to_add = cu->list_in_scope;
c906108c
SS
21917 break;
21918 case DW_TAG_unspecified_parameters:
21919 /* From varargs functions; gdb doesn't seem to have any
21920 interest in this information, so just ignore it for now.
21921 (FIXME?) */
21922 break;
34eaf542
TT
21923 case DW_TAG_template_type_param:
21924 suppress_add = 1;
21925 /* Fall through. */
c906108c 21926 case DW_TAG_class_type:
680b30c7 21927 case DW_TAG_interface_type:
c906108c
SS
21928 case DW_TAG_structure_type:
21929 case DW_TAG_union_type:
72019c9c 21930 case DW_TAG_set_type:
c906108c 21931 case DW_TAG_enumeration_type:
f1e6e072 21932 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21933 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21934
63d06c5c 21935 {
9c37b5ae 21936 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21937 really ever be static objects: otherwise, if you try
21938 to, say, break of a class's method and you're in a file
21939 which doesn't mention that class, it won't work unless
21940 the check for all static symbols in lookup_symbol_aux
21941 saves you. See the OtherFileClass tests in
21942 gdb.c++/namespace.exp. */
21943
e37fd15a 21944 if (!suppress_add)
34eaf542 21945 {
34eaf542 21946 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 21947 && cu->language == language_cplus
34eaf542 21948 ? &global_symbols : cu->list_in_scope);
63d06c5c 21949
64382290 21950 /* The semantics of C++ state that "struct foo {
9c37b5ae 21951 ... }" also defines a typedef for "foo". */
64382290 21952 if (cu->language == language_cplus
45280282 21953 || cu->language == language_ada
c44af4eb
TT
21954 || cu->language == language_d
21955 || cu->language == language_rust)
64382290
TT
21956 {
21957 /* The symbol's name is already allocated along
21958 with this objfile, so we don't need to
21959 duplicate it for the type. */
21960 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21961 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21962 }
63d06c5c
DC
21963 }
21964 }
c906108c
SS
21965 break;
21966 case DW_TAG_typedef:
f1e6e072 21967 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21968 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21969 list_to_add = cu->list_in_scope;
63d06c5c 21970 break;
c906108c 21971 case DW_TAG_base_type:
a02abb62 21972 case DW_TAG_subrange_type:
f1e6e072 21973 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21974 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21975 list_to_add = cu->list_in_scope;
c906108c
SS
21976 break;
21977 case DW_TAG_enumerator:
e142c38c 21978 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21979 if (attr)
21980 {
e7c27a73 21981 dwarf2_const_value (attr, sym, cu);
c906108c 21982 }
63d06c5c
DC
21983 {
21984 /* NOTE: carlton/2003-11-10: See comment above in the
21985 DW_TAG_class_type, etc. block. */
21986
e142c38c 21987 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 21988 && cu->language == language_cplus
e142c38c 21989 ? &global_symbols : cu->list_in_scope);
63d06c5c 21990 }
c906108c 21991 break;
74921315 21992 case DW_TAG_imported_declaration:
5c4e30ca 21993 case DW_TAG_namespace:
f1e6e072 21994 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 21995 list_to_add = &global_symbols;
5c4e30ca 21996 break;
530e8392
KB
21997 case DW_TAG_module:
21998 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21999 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22000 list_to_add = &global_symbols;
22001 break;
4357ac6c 22002 case DW_TAG_common_block:
f1e6e072 22003 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
22004 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22005 add_symbol_to_list (sym, cu->list_in_scope);
22006 break;
c906108c
SS
22007 default:
22008 /* Not a tag we recognize. Hopefully we aren't processing
22009 trash data, but since we must specifically ignore things
22010 we don't recognize, there is nothing else we should do at
0963b4bd 22011 this point. */
e2e0b3e5 22012 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 22013 dwarf_tag_name (die->tag));
c906108c
SS
22014 break;
22015 }
df8a16a1 22016
e37fd15a
SW
22017 if (suppress_add)
22018 {
22019 sym->hash_next = objfile->template_symbols;
22020 objfile->template_symbols = sym;
22021 list_to_add = NULL;
22022 }
22023
22024 if (list_to_add != NULL)
22025 add_symbol_to_list (sym, list_to_add);
22026
df8a16a1
DJ
22027 /* For the benefit of old versions of GCC, check for anonymous
22028 namespaces based on the demangled name. */
4d4ec4e5 22029 if (!cu->processing_has_namespace_info
94af9270 22030 && cu->language == language_cplus)
a10964d1 22031 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
22032 }
22033 return (sym);
22034}
22035
98bfdba5
PA
22036/* Given an attr with a DW_FORM_dataN value in host byte order,
22037 zero-extend it as appropriate for the symbol's type. The DWARF
22038 standard (v4) is not entirely clear about the meaning of using
22039 DW_FORM_dataN for a constant with a signed type, where the type is
22040 wider than the data. The conclusion of a discussion on the DWARF
22041 list was that this is unspecified. We choose to always zero-extend
22042 because that is the interpretation long in use by GCC. */
c906108c 22043
98bfdba5 22044static gdb_byte *
ff39bb5e 22045dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 22046 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 22047{
518817b3 22048 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
22049 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22050 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
22051 LONGEST l = DW_UNSND (attr);
22052
22053 if (bits < sizeof (*value) * 8)
22054 {
22055 l &= ((LONGEST) 1 << bits) - 1;
22056 *value = l;
22057 }
22058 else if (bits == sizeof (*value) * 8)
22059 *value = l;
22060 else
22061 {
224c3ddb 22062 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
22063 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22064 return bytes;
22065 }
22066
22067 return NULL;
22068}
22069
22070/* Read a constant value from an attribute. Either set *VALUE, or if
22071 the value does not fit in *VALUE, set *BYTES - either already
22072 allocated on the objfile obstack, or newly allocated on OBSTACK,
22073 or, set *BATON, if we translated the constant to a location
22074 expression. */
22075
22076static void
ff39bb5e 22077dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
22078 const char *name, struct obstack *obstack,
22079 struct dwarf2_cu *cu,
d521ce57 22080 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
22081 struct dwarf2_locexpr_baton **baton)
22082{
518817b3 22083 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 22084 struct comp_unit_head *cu_header = &cu->header;
c906108c 22085 struct dwarf_block *blk;
98bfdba5
PA
22086 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22087 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22088
22089 *value = 0;
22090 *bytes = NULL;
22091 *baton = NULL;
c906108c
SS
22092
22093 switch (attr->form)
22094 {
22095 case DW_FORM_addr:
3019eac3 22096 case DW_FORM_GNU_addr_index:
ac56253d 22097 {
ac56253d
TT
22098 gdb_byte *data;
22099
98bfdba5
PA
22100 if (TYPE_LENGTH (type) != cu_header->addr_size)
22101 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22102 cu_header->addr_size,
98bfdba5 22103 TYPE_LENGTH (type));
ac56253d
TT
22104 /* Symbols of this form are reasonably rare, so we just
22105 piggyback on the existing location code rather than writing
22106 a new implementation of symbol_computed_ops. */
8d749320 22107 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
22108 (*baton)->per_cu = cu->per_cu;
22109 gdb_assert ((*baton)->per_cu);
ac56253d 22110
98bfdba5 22111 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22112 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22113 (*baton)->data = data;
ac56253d
TT
22114
22115 data[0] = DW_OP_addr;
22116 store_unsigned_integer (&data[1], cu_header->addr_size,
22117 byte_order, DW_ADDR (attr));
22118 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22119 }
c906108c 22120 break;
4ac36638 22121 case DW_FORM_string:
93b5768b 22122 case DW_FORM_strp:
3019eac3 22123 case DW_FORM_GNU_str_index:
36586728 22124 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
22125 /* DW_STRING is already allocated on the objfile obstack, point
22126 directly to it. */
d521ce57 22127 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 22128 break;
c906108c
SS
22129 case DW_FORM_block1:
22130 case DW_FORM_block2:
22131 case DW_FORM_block4:
22132 case DW_FORM_block:
2dc7f7b3 22133 case DW_FORM_exprloc:
0224619f 22134 case DW_FORM_data16:
c906108c 22135 blk = DW_BLOCK (attr);
98bfdba5
PA
22136 if (TYPE_LENGTH (type) != blk->size)
22137 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22138 TYPE_LENGTH (type));
22139 *bytes = blk->data;
c906108c 22140 break;
2df3850c
JM
22141
22142 /* The DW_AT_const_value attributes are supposed to carry the
22143 symbol's value "represented as it would be on the target
22144 architecture." By the time we get here, it's already been
22145 converted to host endianness, so we just need to sign- or
22146 zero-extend it as appropriate. */
22147 case DW_FORM_data1:
3aef2284 22148 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22149 break;
c906108c 22150 case DW_FORM_data2:
3aef2284 22151 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22152 break;
c906108c 22153 case DW_FORM_data4:
3aef2284 22154 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22155 break;
c906108c 22156 case DW_FORM_data8:
3aef2284 22157 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22158 break;
22159
c906108c 22160 case DW_FORM_sdata:
663c44ac 22161 case DW_FORM_implicit_const:
98bfdba5 22162 *value = DW_SND (attr);
2df3850c
JM
22163 break;
22164
c906108c 22165 case DW_FORM_udata:
98bfdba5 22166 *value = DW_UNSND (attr);
c906108c 22167 break;
2df3850c 22168
c906108c 22169 default:
4d3c2250 22170 complaint (&symfile_complaints,
e2e0b3e5 22171 _("unsupported const value attribute form: '%s'"),
4d3c2250 22172 dwarf_form_name (attr->form));
98bfdba5 22173 *value = 0;
c906108c
SS
22174 break;
22175 }
22176}
22177
2df3850c 22178
98bfdba5
PA
22179/* Copy constant value from an attribute to a symbol. */
22180
2df3850c 22181static void
ff39bb5e 22182dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22183 struct dwarf2_cu *cu)
2df3850c 22184{
518817b3 22185 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 22186 LONGEST value;
d521ce57 22187 const gdb_byte *bytes;
98bfdba5 22188 struct dwarf2_locexpr_baton *baton;
2df3850c 22189
98bfdba5
PA
22190 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22191 SYMBOL_PRINT_NAME (sym),
22192 &objfile->objfile_obstack, cu,
22193 &value, &bytes, &baton);
2df3850c 22194
98bfdba5
PA
22195 if (baton != NULL)
22196 {
98bfdba5 22197 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22198 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22199 }
22200 else if (bytes != NULL)
22201 {
22202 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22203 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22204 }
22205 else
22206 {
22207 SYMBOL_VALUE (sym) = value;
f1e6e072 22208 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22209 }
2df3850c
JM
22210}
22211
c906108c
SS
22212/* Return the type of the die in question using its DW_AT_type attribute. */
22213
22214static struct type *
e7c27a73 22215die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22216{
c906108c 22217 struct attribute *type_attr;
c906108c 22218
e142c38c 22219 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22220 if (!type_attr)
22221 {
518817b3 22222 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22223 /* A missing DW_AT_type represents a void type. */
518817b3 22224 return objfile_type (objfile)->builtin_void;
c906108c 22225 }
348e048f 22226
673bfd45 22227 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22228}
22229
b4ba55a1
JB
22230/* True iff CU's producer generates GNAT Ada auxiliary information
22231 that allows to find parallel types through that information instead
22232 of having to do expensive parallel lookups by type name. */
22233
22234static int
22235need_gnat_info (struct dwarf2_cu *cu)
22236{
de4cb04a
JB
22237 /* Assume that the Ada compiler was GNAT, which always produces
22238 the auxiliary information. */
22239 return (cu->language == language_ada);
b4ba55a1
JB
22240}
22241
b4ba55a1
JB
22242/* Return the auxiliary type of the die in question using its
22243 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22244 attribute is not present. */
22245
22246static struct type *
22247die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22248{
b4ba55a1 22249 struct attribute *type_attr;
b4ba55a1
JB
22250
22251 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22252 if (!type_attr)
22253 return NULL;
22254
673bfd45 22255 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22256}
22257
22258/* If DIE has a descriptive_type attribute, then set the TYPE's
22259 descriptive type accordingly. */
22260
22261static void
22262set_descriptive_type (struct type *type, struct die_info *die,
22263 struct dwarf2_cu *cu)
22264{
22265 struct type *descriptive_type = die_descriptive_type (die, cu);
22266
22267 if (descriptive_type)
22268 {
22269 ALLOCATE_GNAT_AUX_TYPE (type);
22270 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22271 }
22272}
22273
c906108c
SS
22274/* Return the containing type of the die in question using its
22275 DW_AT_containing_type attribute. */
22276
22277static struct type *
e7c27a73 22278die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22279{
c906108c 22280 struct attribute *type_attr;
518817b3 22281 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22282
e142c38c 22283 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22284 if (!type_attr)
22285 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22286 "[in module %s]"), objfile_name (objfile));
33ac96f0 22287
673bfd45 22288 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22289}
22290
ac9ec31b
DE
22291/* Return an error marker type to use for the ill formed type in DIE/CU. */
22292
22293static struct type *
22294build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22295{
518817b3
SM
22296 struct dwarf2_per_objfile *dwarf2_per_objfile
22297 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
22298 struct objfile *objfile = dwarf2_per_objfile->objfile;
22299 char *message, *saved;
22300
9d8780f0 22301 message = xstrprintf (_("<unknown type in %s, CU %s, DIE %s>"),
4262abfb 22302 objfile_name (objfile),
9d8780f0
SM
22303 sect_offset_str (cu->header.sect_off),
22304 sect_offset_str (die->sect_off));
224c3ddb
SM
22305 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
22306 message, strlen (message));
ac9ec31b
DE
22307 xfree (message);
22308
19f392bc 22309 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22310}
22311
673bfd45 22312/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22313 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22314 DW_AT_containing_type.
673bfd45
DE
22315 If there is no type substitute an error marker. */
22316
c906108c 22317static struct type *
ff39bb5e 22318lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22319 struct dwarf2_cu *cu)
c906108c 22320{
518817b3
SM
22321 struct dwarf2_per_objfile *dwarf2_per_objfile
22322 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22323 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
22324 struct type *this_type;
22325
ac9ec31b
DE
22326 gdb_assert (attr->name == DW_AT_type
22327 || attr->name == DW_AT_GNAT_descriptive_type
22328 || attr->name == DW_AT_containing_type);
22329
673bfd45
DE
22330 /* First see if we have it cached. */
22331
36586728
TT
22332 if (attr->form == DW_FORM_GNU_ref_alt)
22333 {
22334 struct dwarf2_per_cu_data *per_cu;
9c541725 22335 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 22336
ed2dc618
SM
22337 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22338 dwarf2_per_objfile);
9c541725 22339 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 22340 }
7771576e 22341 else if (attr_form_is_ref (attr))
673bfd45 22342 {
9c541725 22343 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 22344
9c541725 22345 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 22346 }
55f1336d 22347 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22348 {
ac9ec31b 22349 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 22350
ac9ec31b 22351 return get_signatured_type (die, signature, cu);
673bfd45
DE
22352 }
22353 else
22354 {
ac9ec31b
DE
22355 complaint (&symfile_complaints,
22356 _("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22357 " at %s [in module %s]"),
22358 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22359 objfile_name (objfile));
ac9ec31b 22360 return build_error_marker_type (cu, die);
673bfd45
DE
22361 }
22362
22363 /* If not cached we need to read it in. */
22364
22365 if (this_type == NULL)
22366 {
ac9ec31b 22367 struct die_info *type_die = NULL;
673bfd45
DE
22368 struct dwarf2_cu *type_cu = cu;
22369
7771576e 22370 if (attr_form_is_ref (attr))
ac9ec31b
DE
22371 type_die = follow_die_ref (die, attr, &type_cu);
22372 if (type_die == NULL)
22373 return build_error_marker_type (cu, die);
22374 /* If we find the type now, it's probably because the type came
3019eac3
DE
22375 from an inter-CU reference and the type's CU got expanded before
22376 ours. */
ac9ec31b 22377 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22378 }
22379
22380 /* If we still don't have a type use an error marker. */
22381
22382 if (this_type == NULL)
ac9ec31b 22383 return build_error_marker_type (cu, die);
673bfd45 22384
f792889a 22385 return this_type;
c906108c
SS
22386}
22387
673bfd45
DE
22388/* Return the type in DIE, CU.
22389 Returns NULL for invalid types.
22390
02142a6c 22391 This first does a lookup in die_type_hash,
673bfd45
DE
22392 and only reads the die in if necessary.
22393
22394 NOTE: This can be called when reading in partial or full symbols. */
22395
f792889a 22396static struct type *
e7c27a73 22397read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22398{
f792889a
DJ
22399 struct type *this_type;
22400
22401 this_type = get_die_type (die, cu);
22402 if (this_type)
22403 return this_type;
22404
673bfd45
DE
22405 return read_type_die_1 (die, cu);
22406}
22407
22408/* Read the type in DIE, CU.
22409 Returns NULL for invalid types. */
22410
22411static struct type *
22412read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22413{
22414 struct type *this_type = NULL;
22415
c906108c
SS
22416 switch (die->tag)
22417 {
22418 case DW_TAG_class_type:
680b30c7 22419 case DW_TAG_interface_type:
c906108c
SS
22420 case DW_TAG_structure_type:
22421 case DW_TAG_union_type:
f792889a 22422 this_type = read_structure_type (die, cu);
c906108c
SS
22423 break;
22424 case DW_TAG_enumeration_type:
f792889a 22425 this_type = read_enumeration_type (die, cu);
c906108c
SS
22426 break;
22427 case DW_TAG_subprogram:
22428 case DW_TAG_subroutine_type:
edb3359d 22429 case DW_TAG_inlined_subroutine:
f792889a 22430 this_type = read_subroutine_type (die, cu);
c906108c
SS
22431 break;
22432 case DW_TAG_array_type:
f792889a 22433 this_type = read_array_type (die, cu);
c906108c 22434 break;
72019c9c 22435 case DW_TAG_set_type:
f792889a 22436 this_type = read_set_type (die, cu);
72019c9c 22437 break;
c906108c 22438 case DW_TAG_pointer_type:
f792889a 22439 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22440 break;
22441 case DW_TAG_ptr_to_member_type:
f792889a 22442 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22443 break;
22444 case DW_TAG_reference_type:
4297a3f0
AV
22445 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22446 break;
22447 case DW_TAG_rvalue_reference_type:
22448 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22449 break;
22450 case DW_TAG_const_type:
f792889a 22451 this_type = read_tag_const_type (die, cu);
c906108c
SS
22452 break;
22453 case DW_TAG_volatile_type:
f792889a 22454 this_type = read_tag_volatile_type (die, cu);
c906108c 22455 break;
06d66ee9
TT
22456 case DW_TAG_restrict_type:
22457 this_type = read_tag_restrict_type (die, cu);
22458 break;
c906108c 22459 case DW_TAG_string_type:
f792889a 22460 this_type = read_tag_string_type (die, cu);
c906108c
SS
22461 break;
22462 case DW_TAG_typedef:
f792889a 22463 this_type = read_typedef (die, cu);
c906108c 22464 break;
a02abb62 22465 case DW_TAG_subrange_type:
f792889a 22466 this_type = read_subrange_type (die, cu);
a02abb62 22467 break;
c906108c 22468 case DW_TAG_base_type:
f792889a 22469 this_type = read_base_type (die, cu);
c906108c 22470 break;
81a17f79 22471 case DW_TAG_unspecified_type:
f792889a 22472 this_type = read_unspecified_type (die, cu);
81a17f79 22473 break;
0114d602
DJ
22474 case DW_TAG_namespace:
22475 this_type = read_namespace_type (die, cu);
22476 break;
f55ee35c
JK
22477 case DW_TAG_module:
22478 this_type = read_module_type (die, cu);
22479 break;
a2c2acaf
MW
22480 case DW_TAG_atomic_type:
22481 this_type = read_tag_atomic_type (die, cu);
22482 break;
c906108c 22483 default:
3e43a32a
MS
22484 complaint (&symfile_complaints,
22485 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 22486 dwarf_tag_name (die->tag));
c906108c
SS
22487 break;
22488 }
63d06c5c 22489
f792889a 22490 return this_type;
63d06c5c
DC
22491}
22492
abc72ce4
DE
22493/* See if we can figure out if the class lives in a namespace. We do
22494 this by looking for a member function; its demangled name will
22495 contain namespace info, if there is any.
22496 Return the computed name or NULL.
22497 Space for the result is allocated on the objfile's obstack.
22498 This is the full-die version of guess_partial_die_structure_name.
22499 In this case we know DIE has no useful parent. */
22500
22501static char *
22502guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22503{
22504 struct die_info *spec_die;
22505 struct dwarf2_cu *spec_cu;
22506 struct die_info *child;
518817b3 22507 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
22508
22509 spec_cu = cu;
22510 spec_die = die_specification (die, &spec_cu);
22511 if (spec_die != NULL)
22512 {
22513 die = spec_die;
22514 cu = spec_cu;
22515 }
22516
22517 for (child = die->child;
22518 child != NULL;
22519 child = child->sibling)
22520 {
22521 if (child->tag == DW_TAG_subprogram)
22522 {
73b9be8b 22523 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22524
7d45c7c3 22525 if (linkage_name != NULL)
abc72ce4
DE
22526 {
22527 char *actual_name
22528 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 22529 linkage_name);
abc72ce4
DE
22530 char *name = NULL;
22531
22532 if (actual_name != NULL)
22533 {
15d034d0 22534 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22535
22536 if (die_name != NULL
22537 && strcmp (die_name, actual_name) != 0)
22538 {
22539 /* Strip off the class name from the full name.
22540 We want the prefix. */
22541 int die_name_len = strlen (die_name);
22542 int actual_name_len = strlen (actual_name);
22543
22544 /* Test for '::' as a sanity check. */
22545 if (actual_name_len > die_name_len + 2
3e43a32a
MS
22546 && actual_name[actual_name_len
22547 - die_name_len - 1] == ':')
224c3ddb 22548 name = (char *) obstack_copy0 (
e3b94546 22549 &objfile->per_bfd->storage_obstack,
224c3ddb 22550 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
22551 }
22552 }
22553 xfree (actual_name);
22554 return name;
22555 }
22556 }
22557 }
22558
22559 return NULL;
22560}
22561
96408a79
SA
22562/* GCC might emit a nameless typedef that has a linkage name. Determine the
22563 prefix part in such case. See
22564 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22565
a121b7c1 22566static const char *
96408a79
SA
22567anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22568{
22569 struct attribute *attr;
e6a959d6 22570 const char *base;
96408a79
SA
22571
22572 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22573 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22574 return NULL;
22575
7d45c7c3 22576 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22577 return NULL;
22578
73b9be8b 22579 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22580 if (attr == NULL || DW_STRING (attr) == NULL)
22581 return NULL;
22582
22583 /* dwarf2_name had to be already called. */
22584 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22585
22586 /* Strip the base name, keep any leading namespaces/classes. */
22587 base = strrchr (DW_STRING (attr), ':');
22588 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22589 return "";
22590
518817b3 22591 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e3b94546 22592 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb
SM
22593 DW_STRING (attr),
22594 &base[-1] - DW_STRING (attr));
96408a79
SA
22595}
22596
fdde2d81 22597/* Return the name of the namespace/class that DIE is defined within,
0114d602 22598 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22599
0114d602
DJ
22600 For example, if we're within the method foo() in the following
22601 code:
22602
22603 namespace N {
22604 class C {
22605 void foo () {
22606 }
22607 };
22608 }
22609
22610 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22611
0d5cff50 22612static const char *
e142c38c 22613determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22614{
518817b3
SM
22615 struct dwarf2_per_objfile *dwarf2_per_objfile
22616 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22617 struct die_info *parent, *spec_die;
22618 struct dwarf2_cu *spec_cu;
22619 struct type *parent_type;
a121b7c1 22620 const char *retval;
63d06c5c 22621
9c37b5ae 22622 if (cu->language != language_cplus
c44af4eb
TT
22623 && cu->language != language_fortran && cu->language != language_d
22624 && cu->language != language_rust)
0114d602
DJ
22625 return "";
22626
96408a79
SA
22627 retval = anonymous_struct_prefix (die, cu);
22628 if (retval)
22629 return retval;
22630
0114d602
DJ
22631 /* We have to be careful in the presence of DW_AT_specification.
22632 For example, with GCC 3.4, given the code
22633
22634 namespace N {
22635 void foo() {
22636 // Definition of N::foo.
22637 }
22638 }
22639
22640 then we'll have a tree of DIEs like this:
22641
22642 1: DW_TAG_compile_unit
22643 2: DW_TAG_namespace // N
22644 3: DW_TAG_subprogram // declaration of N::foo
22645 4: DW_TAG_subprogram // definition of N::foo
22646 DW_AT_specification // refers to die #3
22647
22648 Thus, when processing die #4, we have to pretend that we're in
22649 the context of its DW_AT_specification, namely the contex of die
22650 #3. */
22651 spec_cu = cu;
22652 spec_die = die_specification (die, &spec_cu);
22653 if (spec_die == NULL)
22654 parent = die->parent;
22655 else
63d06c5c 22656 {
0114d602
DJ
22657 parent = spec_die->parent;
22658 cu = spec_cu;
63d06c5c 22659 }
0114d602
DJ
22660
22661 if (parent == NULL)
22662 return "";
98bfdba5
PA
22663 else if (parent->building_fullname)
22664 {
22665 const char *name;
22666 const char *parent_name;
22667
22668 /* It has been seen on RealView 2.2 built binaries,
22669 DW_TAG_template_type_param types actually _defined_ as
22670 children of the parent class:
22671
22672 enum E {};
22673 template class <class Enum> Class{};
22674 Class<enum E> class_e;
22675
22676 1: DW_TAG_class_type (Class)
22677 2: DW_TAG_enumeration_type (E)
22678 3: DW_TAG_enumerator (enum1:0)
22679 3: DW_TAG_enumerator (enum2:1)
22680 ...
22681 2: DW_TAG_template_type_param
22682 DW_AT_type DW_FORM_ref_udata (E)
22683
22684 Besides being broken debug info, it can put GDB into an
22685 infinite loop. Consider:
22686
22687 When we're building the full name for Class<E>, we'll start
22688 at Class, and go look over its template type parameters,
22689 finding E. We'll then try to build the full name of E, and
22690 reach here. We're now trying to build the full name of E,
22691 and look over the parent DIE for containing scope. In the
22692 broken case, if we followed the parent DIE of E, we'd again
22693 find Class, and once again go look at its template type
22694 arguments, etc., etc. Simply don't consider such parent die
22695 as source-level parent of this die (it can't be, the language
22696 doesn't allow it), and break the loop here. */
22697 name = dwarf2_name (die, cu);
22698 parent_name = dwarf2_name (parent, cu);
22699 complaint (&symfile_complaints,
22700 _("template param type '%s' defined within parent '%s'"),
22701 name ? name : "<unknown>",
22702 parent_name ? parent_name : "<unknown>");
22703 return "";
22704 }
63d06c5c 22705 else
0114d602
DJ
22706 switch (parent->tag)
22707 {
63d06c5c 22708 case DW_TAG_namespace:
0114d602 22709 parent_type = read_type_die (parent, cu);
acebe513
UW
22710 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22711 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22712 Work around this problem here. */
22713 if (cu->language == language_cplus
22714 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22715 return "";
0114d602
DJ
22716 /* We give a name to even anonymous namespaces. */
22717 return TYPE_TAG_NAME (parent_type);
63d06c5c 22718 case DW_TAG_class_type:
680b30c7 22719 case DW_TAG_interface_type:
63d06c5c 22720 case DW_TAG_structure_type:
0114d602 22721 case DW_TAG_union_type:
f55ee35c 22722 case DW_TAG_module:
0114d602
DJ
22723 parent_type = read_type_die (parent, cu);
22724 if (TYPE_TAG_NAME (parent_type) != NULL)
22725 return TYPE_TAG_NAME (parent_type);
22726 else
22727 /* An anonymous structure is only allowed non-static data
22728 members; no typedefs, no member functions, et cetera.
22729 So it does not need a prefix. */
22730 return "";
abc72ce4 22731 case DW_TAG_compile_unit:
95554aad 22732 case DW_TAG_partial_unit:
abc72ce4
DE
22733 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22734 if (cu->language == language_cplus
8b70b953 22735 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
22736 && die->child != NULL
22737 && (die->tag == DW_TAG_class_type
22738 || die->tag == DW_TAG_structure_type
22739 || die->tag == DW_TAG_union_type))
22740 {
22741 char *name = guess_full_die_structure_name (die, cu);
22742 if (name != NULL)
22743 return name;
22744 }
22745 return "";
3d567982
TT
22746 case DW_TAG_enumeration_type:
22747 parent_type = read_type_die (parent, cu);
22748 if (TYPE_DECLARED_CLASS (parent_type))
22749 {
22750 if (TYPE_TAG_NAME (parent_type) != NULL)
22751 return TYPE_TAG_NAME (parent_type);
22752 return "";
22753 }
22754 /* Fall through. */
63d06c5c 22755 default:
8176b9b8 22756 return determine_prefix (parent, cu);
63d06c5c 22757 }
63d06c5c
DC
22758}
22759
3e43a32a
MS
22760/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22761 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22762 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22763 an obconcat, otherwise allocate storage for the result. The CU argument is
22764 used to determine the language and hence, the appropriate separator. */
987504bb 22765
f55ee35c 22766#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22767
22768static char *
f55ee35c
JK
22769typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22770 int physname, struct dwarf2_cu *cu)
63d06c5c 22771{
f55ee35c 22772 const char *lead = "";
5c315b68 22773 const char *sep;
63d06c5c 22774
3e43a32a
MS
22775 if (suffix == NULL || suffix[0] == '\0'
22776 || prefix == NULL || prefix[0] == '\0')
987504bb 22777 sep = "";
45280282
IB
22778 else if (cu->language == language_d)
22779 {
22780 /* For D, the 'main' function could be defined in any module, but it
22781 should never be prefixed. */
22782 if (strcmp (suffix, "D main") == 0)
22783 {
22784 prefix = "";
22785 sep = "";
22786 }
22787 else
22788 sep = ".";
22789 }
f55ee35c
JK
22790 else if (cu->language == language_fortran && physname)
22791 {
22792 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22793 DW_AT_MIPS_linkage_name is preferred and used instead. */
22794
22795 lead = "__";
22796 sep = "_MOD_";
22797 }
987504bb
JJ
22798 else
22799 sep = "::";
63d06c5c 22800
6dd47d34
DE
22801 if (prefix == NULL)
22802 prefix = "";
22803 if (suffix == NULL)
22804 suffix = "";
22805
987504bb
JJ
22806 if (obs == NULL)
22807 {
3e43a32a 22808 char *retval
224c3ddb
SM
22809 = ((char *)
22810 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22811
f55ee35c
JK
22812 strcpy (retval, lead);
22813 strcat (retval, prefix);
6dd47d34
DE
22814 strcat (retval, sep);
22815 strcat (retval, suffix);
63d06c5c
DC
22816 return retval;
22817 }
987504bb
JJ
22818 else
22819 {
22820 /* We have an obstack. */
f55ee35c 22821 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22822 }
63d06c5c
DC
22823}
22824
c906108c
SS
22825/* Return sibling of die, NULL if no sibling. */
22826
f9aca02d 22827static struct die_info *
fba45db2 22828sibling_die (struct die_info *die)
c906108c 22829{
639d11d3 22830 return die->sibling;
c906108c
SS
22831}
22832
71c25dea
TT
22833/* Get name of a die, return NULL if not found. */
22834
15d034d0
TT
22835static const char *
22836dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
22837 struct obstack *obstack)
22838{
22839 if (name && cu->language == language_cplus)
22840 {
2f408ecb 22841 std::string canon_name = cp_canonicalize_string (name);
71c25dea 22842
2f408ecb 22843 if (!canon_name.empty ())
71c25dea 22844 {
2f408ecb
PA
22845 if (canon_name != name)
22846 name = (const char *) obstack_copy0 (obstack,
22847 canon_name.c_str (),
22848 canon_name.length ());
71c25dea
TT
22849 }
22850 }
22851
22852 return name;
c906108c
SS
22853}
22854
96553a0c
DE
22855/* Get name of a die, return NULL if not found.
22856 Anonymous namespaces are converted to their magic string. */
9219021c 22857
15d034d0 22858static const char *
e142c38c 22859dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22860{
22861 struct attribute *attr;
518817b3 22862 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 22863
e142c38c 22864 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22865 if ((!attr || !DW_STRING (attr))
96553a0c 22866 && die->tag != DW_TAG_namespace
53832f31
TT
22867 && die->tag != DW_TAG_class_type
22868 && die->tag != DW_TAG_interface_type
22869 && die->tag != DW_TAG_structure_type
22870 && die->tag != DW_TAG_union_type)
71c25dea
TT
22871 return NULL;
22872
22873 switch (die->tag)
22874 {
22875 case DW_TAG_compile_unit:
95554aad 22876 case DW_TAG_partial_unit:
71c25dea
TT
22877 /* Compilation units have a DW_AT_name that is a filename, not
22878 a source language identifier. */
22879 case DW_TAG_enumeration_type:
22880 case DW_TAG_enumerator:
22881 /* These tags always have simple identifiers already; no need
22882 to canonicalize them. */
22883 return DW_STRING (attr);
907af001 22884
96553a0c
DE
22885 case DW_TAG_namespace:
22886 if (attr != NULL && DW_STRING (attr) != NULL)
22887 return DW_STRING (attr);
22888 return CP_ANONYMOUS_NAMESPACE_STR;
22889
907af001
UW
22890 case DW_TAG_class_type:
22891 case DW_TAG_interface_type:
22892 case DW_TAG_structure_type:
22893 case DW_TAG_union_type:
22894 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22895 structures or unions. These were of the form "._%d" in GCC 4.1,
22896 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22897 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22898 if (attr && DW_STRING (attr)
61012eef
GB
22899 && (startswith (DW_STRING (attr), "._")
22900 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22901 return NULL;
53832f31
TT
22902
22903 /* GCC might emit a nameless typedef that has a linkage name. See
22904 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22905 if (!attr || DW_STRING (attr) == NULL)
22906 {
df5c6c50 22907 char *demangled = NULL;
53832f31 22908
73b9be8b 22909 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22910 if (attr == NULL || DW_STRING (attr) == NULL)
22911 return NULL;
22912
df5c6c50
JK
22913 /* Avoid demangling DW_STRING (attr) the second time on a second
22914 call for the same DIE. */
22915 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 22916 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
22917
22918 if (demangled)
22919 {
e6a959d6 22920 const char *base;
96408a79 22921
53832f31 22922 /* FIXME: we already did this for the partial symbol... */
34a68019 22923 DW_STRING (attr)
224c3ddb 22924 = ((const char *)
e3b94546 22925 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb 22926 demangled, strlen (demangled)));
53832f31
TT
22927 DW_STRING_IS_CANONICAL (attr) = 1;
22928 xfree (demangled);
96408a79
SA
22929
22930 /* Strip any leading namespaces/classes, keep only the base name.
22931 DW_AT_name for named DIEs does not contain the prefixes. */
22932 base = strrchr (DW_STRING (attr), ':');
22933 if (base && base > DW_STRING (attr) && base[-1] == ':')
22934 return &base[1];
22935 else
22936 return DW_STRING (attr);
53832f31
TT
22937 }
22938 }
907af001
UW
22939 break;
22940
71c25dea 22941 default:
907af001
UW
22942 break;
22943 }
22944
22945 if (!DW_STRING_IS_CANONICAL (attr))
22946 {
22947 DW_STRING (attr)
22948 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 22949 &objfile->per_bfd->storage_obstack);
907af001 22950 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22951 }
907af001 22952 return DW_STRING (attr);
9219021c
DC
22953}
22954
22955/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22956 is none. *EXT_CU is the CU containing DIE on input, and the CU
22957 containing the return value on output. */
9219021c
DC
22958
22959static struct die_info *
f2f0e013 22960dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22961{
22962 struct attribute *attr;
9219021c 22963
f2f0e013 22964 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22965 if (attr == NULL)
22966 return NULL;
22967
f2f0e013 22968 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22969}
22970
c906108c
SS
22971/* Convert a DIE tag into its string name. */
22972
f39c6ffd 22973static const char *
aa1ee363 22974dwarf_tag_name (unsigned tag)
c906108c 22975{
f39c6ffd
TT
22976 const char *name = get_DW_TAG_name (tag);
22977
22978 if (name == NULL)
22979 return "DW_TAG_<unknown>";
22980
22981 return name;
c906108c
SS
22982}
22983
22984/* Convert a DWARF attribute code into its string name. */
22985
f39c6ffd 22986static const char *
aa1ee363 22987dwarf_attr_name (unsigned attr)
c906108c 22988{
f39c6ffd
TT
22989 const char *name;
22990
c764a876 22991#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
22992 if (attr == DW_AT_MIPS_fde)
22993 return "DW_AT_MIPS_fde";
22994#else
22995 if (attr == DW_AT_HP_block_index)
22996 return "DW_AT_HP_block_index";
c764a876 22997#endif
f39c6ffd
TT
22998
22999 name = get_DW_AT_name (attr);
23000
23001 if (name == NULL)
23002 return "DW_AT_<unknown>";
23003
23004 return name;
c906108c
SS
23005}
23006
23007/* Convert a DWARF value form code into its string name. */
23008
f39c6ffd 23009static const char *
aa1ee363 23010dwarf_form_name (unsigned form)
c906108c 23011{
f39c6ffd
TT
23012 const char *name = get_DW_FORM_name (form);
23013
23014 if (name == NULL)
23015 return "DW_FORM_<unknown>";
23016
23017 return name;
c906108c
SS
23018}
23019
a121b7c1 23020static const char *
fba45db2 23021dwarf_bool_name (unsigned mybool)
c906108c
SS
23022{
23023 if (mybool)
23024 return "TRUE";
23025 else
23026 return "FALSE";
23027}
23028
23029/* Convert a DWARF type code into its string name. */
23030
f39c6ffd 23031static const char *
aa1ee363 23032dwarf_type_encoding_name (unsigned enc)
c906108c 23033{
f39c6ffd 23034 const char *name = get_DW_ATE_name (enc);
c906108c 23035
f39c6ffd
TT
23036 if (name == NULL)
23037 return "DW_ATE_<unknown>";
c906108c 23038
f39c6ffd 23039 return name;
c906108c 23040}
c906108c 23041
f9aca02d 23042static void
d97bc12b 23043dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
23044{
23045 unsigned int i;
23046
d97bc12b 23047 print_spaces (indent, f);
9d8780f0 23048 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 23049 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 23050 sect_offset_str (die->sect_off));
d97bc12b
DE
23051
23052 if (die->parent != NULL)
23053 {
23054 print_spaces (indent, f);
9d8780f0
SM
23055 fprintf_unfiltered (f, " parent at offset: %s\n",
23056 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
23057 }
23058
23059 print_spaces (indent, f);
23060 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 23061 dwarf_bool_name (die->child != NULL));
c906108c 23062
d97bc12b
DE
23063 print_spaces (indent, f);
23064 fprintf_unfiltered (f, " attributes:\n");
23065
c906108c
SS
23066 for (i = 0; i < die->num_attrs; ++i)
23067 {
d97bc12b
DE
23068 print_spaces (indent, f);
23069 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
23070 dwarf_attr_name (die->attrs[i].name),
23071 dwarf_form_name (die->attrs[i].form));
d97bc12b 23072
c906108c
SS
23073 switch (die->attrs[i].form)
23074 {
c906108c 23075 case DW_FORM_addr:
3019eac3 23076 case DW_FORM_GNU_addr_index:
d97bc12b 23077 fprintf_unfiltered (f, "address: ");
5af949e3 23078 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
23079 break;
23080 case DW_FORM_block2:
23081 case DW_FORM_block4:
23082 case DW_FORM_block:
23083 case DW_FORM_block1:
56eb65bd
SP
23084 fprintf_unfiltered (f, "block: size %s",
23085 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 23086 break;
2dc7f7b3 23087 case DW_FORM_exprloc:
56eb65bd
SP
23088 fprintf_unfiltered (f, "expression: size %s",
23089 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 23090 break;
0224619f
JK
23091 case DW_FORM_data16:
23092 fprintf_unfiltered (f, "constant of 16 bytes");
23093 break;
4568ecf9
DE
23094 case DW_FORM_ref_addr:
23095 fprintf_unfiltered (f, "ref address: ");
23096 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23097 break;
36586728
TT
23098 case DW_FORM_GNU_ref_alt:
23099 fprintf_unfiltered (f, "alt ref address: ");
23100 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23101 break;
10b3939b
DJ
23102 case DW_FORM_ref1:
23103 case DW_FORM_ref2:
23104 case DW_FORM_ref4:
4568ecf9
DE
23105 case DW_FORM_ref8:
23106 case DW_FORM_ref_udata:
d97bc12b 23107 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 23108 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 23109 break;
c906108c
SS
23110 case DW_FORM_data1:
23111 case DW_FORM_data2:
23112 case DW_FORM_data4:
ce5d95e1 23113 case DW_FORM_data8:
c906108c
SS
23114 case DW_FORM_udata:
23115 case DW_FORM_sdata:
43bbcdc2
PH
23116 fprintf_unfiltered (f, "constant: %s",
23117 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 23118 break;
2dc7f7b3
TT
23119 case DW_FORM_sec_offset:
23120 fprintf_unfiltered (f, "section offset: %s",
23121 pulongest (DW_UNSND (&die->attrs[i])));
23122 break;
55f1336d 23123 case DW_FORM_ref_sig8:
ac9ec31b
DE
23124 fprintf_unfiltered (f, "signature: %s",
23125 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 23126 break;
c906108c 23127 case DW_FORM_string:
4bdf3d34 23128 case DW_FORM_strp:
43988095 23129 case DW_FORM_line_strp:
3019eac3 23130 case DW_FORM_GNU_str_index:
36586728 23131 case DW_FORM_GNU_strp_alt:
8285870a 23132 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 23133 DW_STRING (&die->attrs[i])
8285870a
JK
23134 ? DW_STRING (&die->attrs[i]) : "",
23135 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
23136 break;
23137 case DW_FORM_flag:
23138 if (DW_UNSND (&die->attrs[i]))
d97bc12b 23139 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23140 else
d97bc12b 23141 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23142 break;
2dc7f7b3
TT
23143 case DW_FORM_flag_present:
23144 fprintf_unfiltered (f, "flag: TRUE");
23145 break;
a8329558 23146 case DW_FORM_indirect:
0963b4bd
MS
23147 /* The reader will have reduced the indirect form to
23148 the "base form" so this form should not occur. */
3e43a32a
MS
23149 fprintf_unfiltered (f,
23150 "unexpected attribute form: DW_FORM_indirect");
a8329558 23151 break;
663c44ac
JK
23152 case DW_FORM_implicit_const:
23153 fprintf_unfiltered (f, "constant: %s",
23154 plongest (DW_SND (&die->attrs[i])));
23155 break;
c906108c 23156 default:
d97bc12b 23157 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23158 die->attrs[i].form);
d97bc12b 23159 break;
c906108c 23160 }
d97bc12b 23161 fprintf_unfiltered (f, "\n");
c906108c
SS
23162 }
23163}
23164
f9aca02d 23165static void
d97bc12b 23166dump_die_for_error (struct die_info *die)
c906108c 23167{
d97bc12b
DE
23168 dump_die_shallow (gdb_stderr, 0, die);
23169}
23170
23171static void
23172dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23173{
23174 int indent = level * 4;
23175
23176 gdb_assert (die != NULL);
23177
23178 if (level >= max_level)
23179 return;
23180
23181 dump_die_shallow (f, indent, die);
23182
23183 if (die->child != NULL)
c906108c 23184 {
d97bc12b
DE
23185 print_spaces (indent, f);
23186 fprintf_unfiltered (f, " Children:");
23187 if (level + 1 < max_level)
23188 {
23189 fprintf_unfiltered (f, "\n");
23190 dump_die_1 (f, level + 1, max_level, die->child);
23191 }
23192 else
23193 {
3e43a32a
MS
23194 fprintf_unfiltered (f,
23195 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23196 }
23197 }
23198
23199 if (die->sibling != NULL && level > 0)
23200 {
23201 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23202 }
23203}
23204
d97bc12b
DE
23205/* This is called from the pdie macro in gdbinit.in.
23206 It's not static so gcc will keep a copy callable from gdb. */
23207
23208void
23209dump_die (struct die_info *die, int max_level)
23210{
23211 dump_die_1 (gdb_stdlog, 0, max_level, die);
23212}
23213
f9aca02d 23214static void
51545339 23215store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23216{
51545339 23217 void **slot;
c906108c 23218
9c541725
PA
23219 slot = htab_find_slot_with_hash (cu->die_hash, die,
23220 to_underlying (die->sect_off),
b64f50a1 23221 INSERT);
51545339
DJ
23222
23223 *slot = die;
c906108c
SS
23224}
23225
b64f50a1
JK
23226/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23227 required kind. */
23228
23229static sect_offset
ff39bb5e 23230dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 23231{
7771576e 23232 if (attr_form_is_ref (attr))
9c541725 23233 return (sect_offset) DW_UNSND (attr);
93311388
DE
23234
23235 complaint (&symfile_complaints,
23236 _("unsupported die ref attribute form: '%s'"),
23237 dwarf_form_name (attr->form));
9c541725 23238 return {};
c906108c
SS
23239}
23240
43bbcdc2
PH
23241/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23242 * the value held by the attribute is not constant. */
a02abb62 23243
43bbcdc2 23244static LONGEST
ff39bb5e 23245dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 23246{
663c44ac 23247 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
23248 return DW_SND (attr);
23249 else if (attr->form == DW_FORM_udata
23250 || attr->form == DW_FORM_data1
23251 || attr->form == DW_FORM_data2
23252 || attr->form == DW_FORM_data4
23253 || attr->form == DW_FORM_data8)
23254 return DW_UNSND (attr);
23255 else
23256 {
0224619f 23257 /* For DW_FORM_data16 see attr_form_is_constant. */
3e43a32a
MS
23258 complaint (&symfile_complaints,
23259 _("Attribute value is not a constant (%s)"),
a02abb62
JB
23260 dwarf_form_name (attr->form));
23261 return default_value;
23262 }
23263}
23264
348e048f
DE
23265/* Follow reference or signature attribute ATTR of SRC_DIE.
23266 On entry *REF_CU is the CU of SRC_DIE.
23267 On exit *REF_CU is the CU of the result. */
23268
23269static struct die_info *
ff39bb5e 23270follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23271 struct dwarf2_cu **ref_cu)
23272{
23273 struct die_info *die;
23274
7771576e 23275 if (attr_form_is_ref (attr))
348e048f 23276 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23277 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23278 die = follow_die_sig (src_die, attr, ref_cu);
23279 else
23280 {
23281 dump_die_for_error (src_die);
23282 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 23283 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
23284 }
23285
23286 return die;
03dd20cc
DJ
23287}
23288
5c631832 23289/* Follow reference OFFSET.
673bfd45
DE
23290 On entry *REF_CU is the CU of the source die referencing OFFSET.
23291 On exit *REF_CU is the CU of the result.
23292 Returns NULL if OFFSET is invalid. */
f504f079 23293
f9aca02d 23294static struct die_info *
9c541725 23295follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23296 struct dwarf2_cu **ref_cu)
c906108c 23297{
10b3939b 23298 struct die_info temp_die;
f2f0e013 23299 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
23300 struct dwarf2_per_objfile *dwarf2_per_objfile
23301 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23302 struct objfile *objfile = dwarf2_per_objfile->objfile;
10b3939b 23303
348e048f
DE
23304 gdb_assert (cu->per_cu != NULL);
23305
98bfdba5
PA
23306 target_cu = cu;
23307
3019eac3 23308 if (cu->per_cu->is_debug_types)
348e048f
DE
23309 {
23310 /* .debug_types CUs cannot reference anything outside their CU.
23311 If they need to, they have to reference a signatured type via
55f1336d 23312 DW_FORM_ref_sig8. */
9c541725 23313 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 23314 return NULL;
348e048f 23315 }
36586728 23316 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 23317 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
23318 {
23319 struct dwarf2_per_cu_data *per_cu;
9a619af0 23320
9c541725 23321 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 23322 dwarf2_per_objfile);
03dd20cc
DJ
23323
23324 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
23325 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23326 load_full_comp_unit (per_cu, cu->language);
03dd20cc 23327
10b3939b
DJ
23328 target_cu = per_cu->cu;
23329 }
98bfdba5
PA
23330 else if (cu->dies == NULL)
23331 {
23332 /* We're loading full DIEs during partial symbol reading. */
23333 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 23334 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 23335 }
c906108c 23336
f2f0e013 23337 *ref_cu = target_cu;
9c541725 23338 temp_die.sect_off = sect_off;
9a3c8263 23339 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23340 &temp_die,
23341 to_underlying (sect_off));
5c631832 23342}
10b3939b 23343
5c631832
JK
23344/* Follow reference attribute ATTR of SRC_DIE.
23345 On entry *REF_CU is the CU of SRC_DIE.
23346 On exit *REF_CU is the CU of the result. */
23347
23348static struct die_info *
ff39bb5e 23349follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23350 struct dwarf2_cu **ref_cu)
23351{
9c541725 23352 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
23353 struct dwarf2_cu *cu = *ref_cu;
23354 struct die_info *die;
23355
9c541725 23356 die = follow_die_offset (sect_off,
36586728
TT
23357 (attr->form == DW_FORM_GNU_ref_alt
23358 || cu->per_cu->is_dwz),
23359 ref_cu);
5c631832 23360 if (!die)
9d8780f0
SM
23361 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23362 "at %s [in module %s]"),
23363 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 23364 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 23365
5c631832
JK
23366 return die;
23367}
23368
9c541725 23369/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 23370 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
23371 dwarf2_locexpr_baton->data has lifetime of
23372 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
23373
23374struct dwarf2_locexpr_baton
9c541725 23375dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
23376 struct dwarf2_per_cu_data *per_cu,
23377 CORE_ADDR (*get_frame_pc) (void *baton),
23378 void *baton)
5c631832 23379{
918dd910 23380 struct dwarf2_cu *cu;
5c631832
JK
23381 struct die_info *die;
23382 struct attribute *attr;
23383 struct dwarf2_locexpr_baton retval;
e3b94546 23384 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ed2dc618
SM
23385 struct dwarf2_per_objfile *dwarf2_per_objfile
23386 = get_dwarf2_per_objfile (objfile);
8cf6f0b1 23387
918dd910
JK
23388 if (per_cu->cu == NULL)
23389 load_cu (per_cu);
23390 cu = per_cu->cu;
cc12ce38
DE
23391 if (cu == NULL)
23392 {
23393 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23394 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23395 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23396 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23397 }
918dd910 23398
9c541725 23399 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23400 if (!die)
9d8780f0
SM
23401 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23402 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23403
23404 attr = dwarf2_attr (die, DW_AT_location, cu);
23405 if (!attr)
23406 {
e103e986
JK
23407 /* DWARF: "If there is no such attribute, then there is no effect.".
23408 DATA is ignored if SIZE is 0. */
5c631832 23409
e103e986 23410 retval.data = NULL;
5c631832
JK
23411 retval.size = 0;
23412 }
8cf6f0b1
TT
23413 else if (attr_form_is_section_offset (attr))
23414 {
23415 struct dwarf2_loclist_baton loclist_baton;
23416 CORE_ADDR pc = (*get_frame_pc) (baton);
23417 size_t size;
23418
23419 fill_in_loclist_baton (cu, &loclist_baton, attr);
23420
23421 retval.data = dwarf2_find_location_expression (&loclist_baton,
23422 &size, pc);
23423 retval.size = size;
23424 }
5c631832
JK
23425 else
23426 {
23427 if (!attr_form_is_block (attr))
9d8780f0 23428 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23429 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23430 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23431
23432 retval.data = DW_BLOCK (attr)->data;
23433 retval.size = DW_BLOCK (attr)->size;
23434 }
23435 retval.per_cu = cu->per_cu;
918dd910 23436
ed2dc618 23437 age_cached_comp_units (dwarf2_per_objfile);
918dd910 23438
5c631832 23439 return retval;
348e048f
DE
23440}
23441
8b9737bf
TT
23442/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23443 offset. */
23444
23445struct dwarf2_locexpr_baton
23446dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23447 struct dwarf2_per_cu_data *per_cu,
23448 CORE_ADDR (*get_frame_pc) (void *baton),
23449 void *baton)
23450{
9c541725 23451 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23452
9c541725 23453 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
23454}
23455
b6807d98
TT
23456/* Write a constant of a given type as target-ordered bytes into
23457 OBSTACK. */
23458
23459static const gdb_byte *
23460write_constant_as_bytes (struct obstack *obstack,
23461 enum bfd_endian byte_order,
23462 struct type *type,
23463 ULONGEST value,
23464 LONGEST *len)
23465{
23466 gdb_byte *result;
23467
23468 *len = TYPE_LENGTH (type);
224c3ddb 23469 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23470 store_unsigned_integer (result, *len, byte_order, value);
23471
23472 return result;
23473}
23474
23475/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23476 pointer to the constant bytes and set LEN to the length of the
23477 data. If memory is needed, allocate it on OBSTACK. If the DIE
23478 does not have a DW_AT_const_value, return NULL. */
23479
23480const gdb_byte *
9c541725 23481dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
23482 struct dwarf2_per_cu_data *per_cu,
23483 struct obstack *obstack,
23484 LONGEST *len)
23485{
23486 struct dwarf2_cu *cu;
23487 struct die_info *die;
23488 struct attribute *attr;
23489 const gdb_byte *result = NULL;
23490 struct type *type;
23491 LONGEST value;
23492 enum bfd_endian byte_order;
e3b94546 23493 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 23494
b6807d98
TT
23495 if (per_cu->cu == NULL)
23496 load_cu (per_cu);
23497 cu = per_cu->cu;
cc12ce38
DE
23498 if (cu == NULL)
23499 {
23500 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23501 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23502 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23503 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23504 }
b6807d98 23505
9c541725 23506 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23507 if (!die)
9d8780f0
SM
23508 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23509 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23510
23511 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23512 if (attr == NULL)
23513 return NULL;
23514
e3b94546 23515 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23516 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23517
23518 switch (attr->form)
23519 {
23520 case DW_FORM_addr:
23521 case DW_FORM_GNU_addr_index:
23522 {
23523 gdb_byte *tem;
23524
23525 *len = cu->header.addr_size;
224c3ddb 23526 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23527 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23528 result = tem;
23529 }
23530 break;
23531 case DW_FORM_string:
23532 case DW_FORM_strp:
23533 case DW_FORM_GNU_str_index:
23534 case DW_FORM_GNU_strp_alt:
23535 /* DW_STRING is already allocated on the objfile obstack, point
23536 directly to it. */
23537 result = (const gdb_byte *) DW_STRING (attr);
23538 *len = strlen (DW_STRING (attr));
23539 break;
23540 case DW_FORM_block1:
23541 case DW_FORM_block2:
23542 case DW_FORM_block4:
23543 case DW_FORM_block:
23544 case DW_FORM_exprloc:
0224619f 23545 case DW_FORM_data16:
b6807d98
TT
23546 result = DW_BLOCK (attr)->data;
23547 *len = DW_BLOCK (attr)->size;
23548 break;
23549
23550 /* The DW_AT_const_value attributes are supposed to carry the
23551 symbol's value "represented as it would be on the target
23552 architecture." By the time we get here, it's already been
23553 converted to host endianness, so we just need to sign- or
23554 zero-extend it as appropriate. */
23555 case DW_FORM_data1:
23556 type = die_type (die, cu);
23557 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23558 if (result == NULL)
23559 result = write_constant_as_bytes (obstack, byte_order,
23560 type, value, len);
23561 break;
23562 case DW_FORM_data2:
23563 type = die_type (die, cu);
23564 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23565 if (result == NULL)
23566 result = write_constant_as_bytes (obstack, byte_order,
23567 type, value, len);
23568 break;
23569 case DW_FORM_data4:
23570 type = die_type (die, cu);
23571 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23572 if (result == NULL)
23573 result = write_constant_as_bytes (obstack, byte_order,
23574 type, value, len);
23575 break;
23576 case DW_FORM_data8:
23577 type = die_type (die, cu);
23578 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23579 if (result == NULL)
23580 result = write_constant_as_bytes (obstack, byte_order,
23581 type, value, len);
23582 break;
23583
23584 case DW_FORM_sdata:
663c44ac 23585 case DW_FORM_implicit_const:
b6807d98
TT
23586 type = die_type (die, cu);
23587 result = write_constant_as_bytes (obstack, byte_order,
23588 type, DW_SND (attr), len);
23589 break;
23590
23591 case DW_FORM_udata:
23592 type = die_type (die, cu);
23593 result = write_constant_as_bytes (obstack, byte_order,
23594 type, DW_UNSND (attr), len);
23595 break;
23596
23597 default:
23598 complaint (&symfile_complaints,
23599 _("unsupported const value attribute form: '%s'"),
23600 dwarf_form_name (attr->form));
23601 break;
23602 }
23603
23604 return result;
23605}
23606
7942e96e
AA
23607/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23608 valid type for this die is found. */
23609
23610struct type *
9c541725 23611dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23612 struct dwarf2_per_cu_data *per_cu)
23613{
23614 struct dwarf2_cu *cu;
23615 struct die_info *die;
23616
7942e96e
AA
23617 if (per_cu->cu == NULL)
23618 load_cu (per_cu);
23619 cu = per_cu->cu;
23620 if (!cu)
23621 return NULL;
23622
9c541725 23623 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23624 if (!die)
23625 return NULL;
23626
23627 return die_type (die, cu);
23628}
23629
8a9b8146
TT
23630/* Return the type of the DIE at DIE_OFFSET in the CU named by
23631 PER_CU. */
23632
23633struct type *
b64f50a1 23634dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23635 struct dwarf2_per_cu_data *per_cu)
23636{
9c541725 23637 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23638 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23639}
23640
ac9ec31b 23641/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23642 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23643 On exit *REF_CU is the CU of the result.
23644 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23645
23646static struct die_info *
ac9ec31b
DE
23647follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23648 struct dwarf2_cu **ref_cu)
348e048f 23649{
348e048f 23650 struct die_info temp_die;
348e048f
DE
23651 struct dwarf2_cu *sig_cu;
23652 struct die_info *die;
23653
ac9ec31b
DE
23654 /* While it might be nice to assert sig_type->type == NULL here,
23655 we can get here for DW_AT_imported_declaration where we need
23656 the DIE not the type. */
348e048f
DE
23657
23658 /* If necessary, add it to the queue and load its DIEs. */
23659
95554aad 23660 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23661 read_signatured_type (sig_type);
348e048f 23662
348e048f 23663 sig_cu = sig_type->per_cu.cu;
69d751e3 23664 gdb_assert (sig_cu != NULL);
9c541725
PA
23665 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23666 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23667 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23668 to_underlying (temp_die.sect_off));
348e048f
DE
23669 if (die)
23670 {
ed2dc618 23671 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23672 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23673
796a7ff8
DE
23674 /* For .gdb_index version 7 keep track of included TUs.
23675 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23676 if (dwarf2_per_objfile->index_table != NULL
23677 && dwarf2_per_objfile->index_table->version <= 7)
23678 {
23679 VEC_safe_push (dwarf2_per_cu_ptr,
23680 (*ref_cu)->per_cu->imported_symtabs,
23681 sig_cu->per_cu);
23682 }
23683
348e048f
DE
23684 *ref_cu = sig_cu;
23685 return die;
23686 }
23687
ac9ec31b
DE
23688 return NULL;
23689}
23690
23691/* Follow signatured type referenced by ATTR in SRC_DIE.
23692 On entry *REF_CU is the CU of SRC_DIE.
23693 On exit *REF_CU is the CU of the result.
23694 The result is the DIE of the type.
23695 If the referenced type cannot be found an error is thrown. */
23696
23697static struct die_info *
ff39bb5e 23698follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23699 struct dwarf2_cu **ref_cu)
23700{
23701 ULONGEST signature = DW_SIGNATURE (attr);
23702 struct signatured_type *sig_type;
23703 struct die_info *die;
23704
23705 gdb_assert (attr->form == DW_FORM_ref_sig8);
23706
a2ce51a0 23707 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23708 /* sig_type will be NULL if the signatured type is missing from
23709 the debug info. */
23710 if (sig_type == NULL)
23711 {
23712 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23713 " from DIE at %s [in module %s]"),
23714 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23715 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23716 }
23717
23718 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23719 if (die == NULL)
23720 {
23721 dump_die_for_error (src_die);
23722 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23723 " from DIE at %s [in module %s]"),
23724 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23725 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23726 }
23727
23728 return die;
23729}
23730
23731/* Get the type specified by SIGNATURE referenced in DIE/CU,
23732 reading in and processing the type unit if necessary. */
23733
23734static struct type *
23735get_signatured_type (struct die_info *die, ULONGEST signature,
23736 struct dwarf2_cu *cu)
23737{
518817b3
SM
23738 struct dwarf2_per_objfile *dwarf2_per_objfile
23739 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23740 struct signatured_type *sig_type;
23741 struct dwarf2_cu *type_cu;
23742 struct die_info *type_die;
23743 struct type *type;
23744
a2ce51a0 23745 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23746 /* sig_type will be NULL if the signatured type is missing from
23747 the debug info. */
23748 if (sig_type == NULL)
23749 {
23750 complaint (&symfile_complaints,
23751 _("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23752 " from DIE at %s [in module %s]"),
23753 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23754 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23755 return build_error_marker_type (cu, die);
23756 }
23757
23758 /* If we already know the type we're done. */
23759 if (sig_type->type != NULL)
23760 return sig_type->type;
23761
23762 type_cu = cu;
23763 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23764 if (type_die != NULL)
23765 {
23766 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23767 is created. This is important, for example, because for c++ classes
23768 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23769 type = read_type_die (type_die, type_cu);
23770 if (type == NULL)
23771 {
23772 complaint (&symfile_complaints,
23773 _("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23774 " referenced from DIE at %s [in module %s]"),
23775 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23776 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23777 type = build_error_marker_type (cu, die);
23778 }
23779 }
23780 else
23781 {
23782 complaint (&symfile_complaints,
23783 _("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23784 " from DIE at %s [in module %s]"),
23785 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23786 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23787 type = build_error_marker_type (cu, die);
23788 }
23789 sig_type->type = type;
23790
23791 return type;
23792}
23793
23794/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23795 reading in and processing the type unit if necessary. */
23796
23797static struct type *
ff39bb5e 23798get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23799 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23800{
23801 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 23802 if (attr_form_is_ref (attr))
ac9ec31b
DE
23803 {
23804 struct dwarf2_cu *type_cu = cu;
23805 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23806
23807 return read_type_die (type_die, type_cu);
23808 }
23809 else if (attr->form == DW_FORM_ref_sig8)
23810 {
23811 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23812 }
23813 else
23814 {
518817b3
SM
23815 struct dwarf2_per_objfile *dwarf2_per_objfile
23816 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23817
ac9ec31b
DE
23818 complaint (&symfile_complaints,
23819 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23820 " at %s [in module %s]"),
23821 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 23822 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23823 return build_error_marker_type (cu, die);
23824 }
348e048f
DE
23825}
23826
e5fe5e75 23827/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23828
23829static void
e5fe5e75 23830load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 23831{
52dc124a 23832 struct signatured_type *sig_type;
348e048f 23833
f4dc4d17
DE
23834 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23835 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23836
6721b2ec
DE
23837 /* We have the per_cu, but we need the signatured_type.
23838 Fortunately this is an easy translation. */
23839 gdb_assert (per_cu->is_debug_types);
23840 sig_type = (struct signatured_type *) per_cu;
348e048f 23841
6721b2ec 23842 gdb_assert (per_cu->cu == NULL);
348e048f 23843
52dc124a 23844 read_signatured_type (sig_type);
348e048f 23845
6721b2ec 23846 gdb_assert (per_cu->cu != NULL);
348e048f
DE
23847}
23848
dee91e82
DE
23849/* die_reader_func for read_signatured_type.
23850 This is identical to load_full_comp_unit_reader,
23851 but is kept separate for now. */
348e048f
DE
23852
23853static void
dee91e82 23854read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 23855 const gdb_byte *info_ptr,
dee91e82
DE
23856 struct die_info *comp_unit_die,
23857 int has_children,
23858 void *data)
348e048f 23859{
dee91e82 23860 struct dwarf2_cu *cu = reader->cu;
348e048f 23861
dee91e82
DE
23862 gdb_assert (cu->die_hash == NULL);
23863 cu->die_hash =
23864 htab_create_alloc_ex (cu->header.length / 12,
23865 die_hash,
23866 die_eq,
23867 NULL,
23868 &cu->comp_unit_obstack,
23869 hashtab_obstack_allocate,
23870 dummy_obstack_deallocate);
348e048f 23871
dee91e82
DE
23872 if (has_children)
23873 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23874 &info_ptr, comp_unit_die);
23875 cu->dies = comp_unit_die;
23876 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
23877
23878 /* We try not to read any attributes in this function, because not
9cdd5dbd 23879 all CUs needed for references have been loaded yet, and symbol
348e048f 23880 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
23881 or we won't be able to build types correctly.
23882 Similarly, if we do not read the producer, we can not apply
23883 producer-specific interpretation. */
95554aad 23884 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 23885}
348e048f 23886
3019eac3
DE
23887/* Read in a signatured type and build its CU and DIEs.
23888 If the type is a stub for the real type in a DWO file,
23889 read in the real type from the DWO file as well. */
dee91e82
DE
23890
23891static void
23892read_signatured_type (struct signatured_type *sig_type)
23893{
23894 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 23895
3019eac3 23896 gdb_assert (per_cu->is_debug_types);
dee91e82 23897 gdb_assert (per_cu->cu == NULL);
348e048f 23898
f4dc4d17
DE
23899 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
23900 read_signatured_type_reader, NULL);
7ee85ab1 23901 sig_type->per_cu.tu_read = 1;
c906108c
SS
23902}
23903
c906108c
SS
23904/* Decode simple location descriptions.
23905 Given a pointer to a dwarf block that defines a location, compute
23906 the location and return the value.
23907
4cecd739
DJ
23908 NOTE drow/2003-11-18: This function is called in two situations
23909 now: for the address of static or global variables (partial symbols
23910 only) and for offsets into structures which are expected to be
23911 (more or less) constant. The partial symbol case should go away,
23912 and only the constant case should remain. That will let this
23913 function complain more accurately. A few special modes are allowed
23914 without complaint for global variables (for instance, global
23915 register values and thread-local values).
c906108c
SS
23916
23917 A location description containing no operations indicates that the
4cecd739 23918 object is optimized out. The return value is 0 for that case.
6b992462
DJ
23919 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23920 callers will only want a very basic result and this can become a
21ae7a4d
JK
23921 complaint.
23922
23923 Note that stack[0] is unused except as a default error return. */
c906108c
SS
23924
23925static CORE_ADDR
e7c27a73 23926decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 23927{
518817b3 23928 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
23929 size_t i;
23930 size_t size = blk->size;
d521ce57 23931 const gdb_byte *data = blk->data;
21ae7a4d
JK
23932 CORE_ADDR stack[64];
23933 int stacki;
23934 unsigned int bytes_read, unsnd;
23935 gdb_byte op;
c906108c 23936
21ae7a4d
JK
23937 i = 0;
23938 stacki = 0;
23939 stack[stacki] = 0;
23940 stack[++stacki] = 0;
23941
23942 while (i < size)
23943 {
23944 op = data[i++];
23945 switch (op)
23946 {
23947 case DW_OP_lit0:
23948 case DW_OP_lit1:
23949 case DW_OP_lit2:
23950 case DW_OP_lit3:
23951 case DW_OP_lit4:
23952 case DW_OP_lit5:
23953 case DW_OP_lit6:
23954 case DW_OP_lit7:
23955 case DW_OP_lit8:
23956 case DW_OP_lit9:
23957 case DW_OP_lit10:
23958 case DW_OP_lit11:
23959 case DW_OP_lit12:
23960 case DW_OP_lit13:
23961 case DW_OP_lit14:
23962 case DW_OP_lit15:
23963 case DW_OP_lit16:
23964 case DW_OP_lit17:
23965 case DW_OP_lit18:
23966 case DW_OP_lit19:
23967 case DW_OP_lit20:
23968 case DW_OP_lit21:
23969 case DW_OP_lit22:
23970 case DW_OP_lit23:
23971 case DW_OP_lit24:
23972 case DW_OP_lit25:
23973 case DW_OP_lit26:
23974 case DW_OP_lit27:
23975 case DW_OP_lit28:
23976 case DW_OP_lit29:
23977 case DW_OP_lit30:
23978 case DW_OP_lit31:
23979 stack[++stacki] = op - DW_OP_lit0;
23980 break;
f1bea926 23981
21ae7a4d
JK
23982 case DW_OP_reg0:
23983 case DW_OP_reg1:
23984 case DW_OP_reg2:
23985 case DW_OP_reg3:
23986 case DW_OP_reg4:
23987 case DW_OP_reg5:
23988 case DW_OP_reg6:
23989 case DW_OP_reg7:
23990 case DW_OP_reg8:
23991 case DW_OP_reg9:
23992 case DW_OP_reg10:
23993 case DW_OP_reg11:
23994 case DW_OP_reg12:
23995 case DW_OP_reg13:
23996 case DW_OP_reg14:
23997 case DW_OP_reg15:
23998 case DW_OP_reg16:
23999 case DW_OP_reg17:
24000 case DW_OP_reg18:
24001 case DW_OP_reg19:
24002 case DW_OP_reg20:
24003 case DW_OP_reg21:
24004 case DW_OP_reg22:
24005 case DW_OP_reg23:
24006 case DW_OP_reg24:
24007 case DW_OP_reg25:
24008 case DW_OP_reg26:
24009 case DW_OP_reg27:
24010 case DW_OP_reg28:
24011 case DW_OP_reg29:
24012 case DW_OP_reg30:
24013 case DW_OP_reg31:
24014 stack[++stacki] = op - DW_OP_reg0;
24015 if (i < size)
24016 dwarf2_complex_location_expr_complaint ();
24017 break;
c906108c 24018
21ae7a4d
JK
24019 case DW_OP_regx:
24020 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24021 i += bytes_read;
24022 stack[++stacki] = unsnd;
24023 if (i < size)
24024 dwarf2_complex_location_expr_complaint ();
24025 break;
c906108c 24026
21ae7a4d
JK
24027 case DW_OP_addr:
24028 stack[++stacki] = read_address (objfile->obfd, &data[i],
24029 cu, &bytes_read);
24030 i += bytes_read;
24031 break;
d53d4ac5 24032
21ae7a4d
JK
24033 case DW_OP_const1u:
24034 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24035 i += 1;
24036 break;
24037
24038 case DW_OP_const1s:
24039 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24040 i += 1;
24041 break;
24042
24043 case DW_OP_const2u:
24044 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24045 i += 2;
24046 break;
24047
24048 case DW_OP_const2s:
24049 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24050 i += 2;
24051 break;
d53d4ac5 24052
21ae7a4d
JK
24053 case DW_OP_const4u:
24054 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24055 i += 4;
24056 break;
24057
24058 case DW_OP_const4s:
24059 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24060 i += 4;
24061 break;
24062
585861ea
JK
24063 case DW_OP_const8u:
24064 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24065 i += 8;
24066 break;
24067
21ae7a4d
JK
24068 case DW_OP_constu:
24069 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24070 &bytes_read);
24071 i += bytes_read;
24072 break;
24073
24074 case DW_OP_consts:
24075 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24076 i += bytes_read;
24077 break;
24078
24079 case DW_OP_dup:
24080 stack[stacki + 1] = stack[stacki];
24081 stacki++;
24082 break;
24083
24084 case DW_OP_plus:
24085 stack[stacki - 1] += stack[stacki];
24086 stacki--;
24087 break;
24088
24089 case DW_OP_plus_uconst:
24090 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24091 &bytes_read);
24092 i += bytes_read;
24093 break;
24094
24095 case DW_OP_minus:
24096 stack[stacki - 1] -= stack[stacki];
24097 stacki--;
24098 break;
24099
24100 case DW_OP_deref:
24101 /* If we're not the last op, then we definitely can't encode
24102 this using GDB's address_class enum. This is valid for partial
24103 global symbols, although the variable's address will be bogus
24104 in the psymtab. */
24105 if (i < size)
24106 dwarf2_complex_location_expr_complaint ();
24107 break;
24108
24109 case DW_OP_GNU_push_tls_address:
4aa4e28b 24110 case DW_OP_form_tls_address:
21ae7a4d
JK
24111 /* The top of the stack has the offset from the beginning
24112 of the thread control block at which the variable is located. */
24113 /* Nothing should follow this operator, so the top of stack would
24114 be returned. */
24115 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24116 address will be bogus in the psymtab. Make it always at least
24117 non-zero to not look as a variable garbage collected by linker
24118 which have DW_OP_addr 0. */
21ae7a4d
JK
24119 if (i < size)
24120 dwarf2_complex_location_expr_complaint ();
585861ea 24121 stack[stacki]++;
21ae7a4d
JK
24122 break;
24123
24124 case DW_OP_GNU_uninit:
24125 break;
24126
3019eac3 24127 case DW_OP_GNU_addr_index:
49f6c839 24128 case DW_OP_GNU_const_index:
3019eac3
DE
24129 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24130 &bytes_read);
24131 i += bytes_read;
24132 break;
24133
21ae7a4d
JK
24134 default:
24135 {
f39c6ffd 24136 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
24137
24138 if (name)
24139 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
24140 name);
24141 else
24142 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
24143 op);
24144 }
24145
24146 return (stack[stacki]);
d53d4ac5 24147 }
3c6e0cb3 24148
21ae7a4d
JK
24149 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24150 outside of the allocated space. Also enforce minimum>0. */
24151 if (stacki >= ARRAY_SIZE (stack) - 1)
24152 {
24153 complaint (&symfile_complaints,
24154 _("location description stack overflow"));
24155 return 0;
24156 }
24157
24158 if (stacki <= 0)
24159 {
24160 complaint (&symfile_complaints,
24161 _("location description stack underflow"));
24162 return 0;
24163 }
24164 }
24165 return (stack[stacki]);
c906108c
SS
24166}
24167
24168/* memory allocation interface */
24169
c906108c 24170static struct dwarf_block *
7b5a2f43 24171dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24172{
8d749320 24173 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24174}
24175
c906108c 24176static struct die_info *
b60c80d6 24177dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24178{
24179 struct die_info *die;
b60c80d6
DJ
24180 size_t size = sizeof (struct die_info);
24181
24182 if (num_attrs > 1)
24183 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24184
b60c80d6 24185 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24186 memset (die, 0, sizeof (struct die_info));
24187 return (die);
24188}
2e276125
JB
24189
24190\f
24191/* Macro support. */
24192
233d95b5
JK
24193/* Return file name relative to the compilation directory of file number I in
24194 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 24195 responsible for freeing it. */
233d95b5 24196
2e276125 24197static char *
233d95b5 24198file_file_name (int file, struct line_header *lh)
2e276125 24199{
6a83a1e6
EZ
24200 /* Is the file number a valid index into the line header's file name
24201 table? Remember that file numbers start with one, not zero. */
fff8551c 24202 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 24203 {
8c43009f 24204 const file_entry &fe = lh->file_names[file - 1];
6e70227d 24205
8c43009f
PA
24206 if (!IS_ABSOLUTE_PATH (fe.name))
24207 {
24208 const char *dir = fe.include_dir (lh);
24209 if (dir != NULL)
24210 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
24211 }
24212 return xstrdup (fe.name);
6a83a1e6 24213 }
2e276125
JB
24214 else
24215 {
6a83a1e6
EZ
24216 /* The compiler produced a bogus file number. We can at least
24217 record the macro definitions made in the file, even if we
24218 won't be able to find the file by name. */
24219 char fake_name[80];
9a619af0 24220
8c042590
PM
24221 xsnprintf (fake_name, sizeof (fake_name),
24222 "<bad macro file number %d>", file);
2e276125 24223
6e70227d 24224 complaint (&symfile_complaints,
6a83a1e6
EZ
24225 _("bad file number in macro information (%d)"),
24226 file);
2e276125 24227
6a83a1e6 24228 return xstrdup (fake_name);
2e276125
JB
24229 }
24230}
24231
233d95b5
JK
24232/* Return the full name of file number I in *LH's file name table.
24233 Use COMP_DIR as the name of the current directory of the
24234 compilation. The result is allocated using xmalloc; the caller is
24235 responsible for freeing it. */
24236static char *
24237file_full_name (int file, struct line_header *lh, const char *comp_dir)
24238{
24239 /* Is the file number a valid index into the line header's file name
24240 table? Remember that file numbers start with one, not zero. */
fff8551c 24241 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
24242 {
24243 char *relative = file_file_name (file, lh);
24244
24245 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24246 return relative;
b36cec19
PA
24247 return reconcat (relative, comp_dir, SLASH_STRING,
24248 relative, (char *) NULL);
233d95b5
JK
24249 }
24250 else
24251 return file_file_name (file, lh);
24252}
24253
2e276125
JB
24254
24255static struct macro_source_file *
24256macro_start_file (int file, int line,
24257 struct macro_source_file *current_file,
43f3e411 24258 struct line_header *lh)
2e276125 24259{
233d95b5
JK
24260 /* File name relative to the compilation directory of this source file. */
24261 char *file_name = file_file_name (file, lh);
2e276125 24262
2e276125 24263 if (! current_file)
abc9d0dc 24264 {
fc474241
DE
24265 /* Note: We don't create a macro table for this compilation unit
24266 at all until we actually get a filename. */
43f3e411 24267 struct macro_table *macro_table = get_macro_table ();
fc474241 24268
abc9d0dc
TT
24269 /* If we have no current file, then this must be the start_file
24270 directive for the compilation unit's main source file. */
fc474241
DE
24271 current_file = macro_set_main (macro_table, file_name);
24272 macro_define_special (macro_table);
abc9d0dc 24273 }
2e276125 24274 else
233d95b5 24275 current_file = macro_include (current_file, line, file_name);
2e276125 24276
233d95b5 24277 xfree (file_name);
6e70227d 24278
2e276125
JB
24279 return current_file;
24280}
24281
2e276125
JB
24282static const char *
24283consume_improper_spaces (const char *p, const char *body)
24284{
24285 if (*p == ' ')
24286 {
4d3c2250 24287 complaint (&symfile_complaints,
3e43a32a
MS
24288 _("macro definition contains spaces "
24289 "in formal argument list:\n`%s'"),
4d3c2250 24290 body);
2e276125
JB
24291
24292 while (*p == ' ')
24293 p++;
24294 }
24295
24296 return p;
24297}
24298
24299
24300static void
24301parse_macro_definition (struct macro_source_file *file, int line,
24302 const char *body)
24303{
24304 const char *p;
24305
24306 /* The body string takes one of two forms. For object-like macro
24307 definitions, it should be:
24308
24309 <macro name> " " <definition>
24310
24311 For function-like macro definitions, it should be:
24312
24313 <macro name> "() " <definition>
24314 or
24315 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24316
24317 Spaces may appear only where explicitly indicated, and in the
24318 <definition>.
24319
24320 The Dwarf 2 spec says that an object-like macro's name is always
24321 followed by a space, but versions of GCC around March 2002 omit
6e70227d 24322 the space when the macro's definition is the empty string.
2e276125
JB
24323
24324 The Dwarf 2 spec says that there should be no spaces between the
24325 formal arguments in a function-like macro's formal argument list,
24326 but versions of GCC around March 2002 include spaces after the
24327 commas. */
24328
24329
24330 /* Find the extent of the macro name. The macro name is terminated
24331 by either a space or null character (for an object-like macro) or
24332 an opening paren (for a function-like macro). */
24333 for (p = body; *p; p++)
24334 if (*p == ' ' || *p == '(')
24335 break;
24336
24337 if (*p == ' ' || *p == '\0')
24338 {
24339 /* It's an object-like macro. */
24340 int name_len = p - body;
3f8a7804 24341 char *name = savestring (body, name_len);
2e276125
JB
24342 const char *replacement;
24343
24344 if (*p == ' ')
24345 replacement = body + name_len + 1;
24346 else
24347 {
4d3c2250 24348 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24349 replacement = body + name_len;
24350 }
6e70227d 24351
2e276125
JB
24352 macro_define_object (file, line, name, replacement);
24353
24354 xfree (name);
24355 }
24356 else if (*p == '(')
24357 {
24358 /* It's a function-like macro. */
3f8a7804 24359 char *name = savestring (body, p - body);
2e276125
JB
24360 int argc = 0;
24361 int argv_size = 1;
8d749320 24362 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
24363
24364 p++;
24365
24366 p = consume_improper_spaces (p, body);
24367
24368 /* Parse the formal argument list. */
24369 while (*p && *p != ')')
24370 {
24371 /* Find the extent of the current argument name. */
24372 const char *arg_start = p;
24373
24374 while (*p && *p != ',' && *p != ')' && *p != ' ')
24375 p++;
24376
24377 if (! *p || p == arg_start)
4d3c2250 24378 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24379 else
24380 {
24381 /* Make sure argv has room for the new argument. */
24382 if (argc >= argv_size)
24383 {
24384 argv_size *= 2;
224c3ddb 24385 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
24386 }
24387
3f8a7804 24388 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
24389 }
24390
24391 p = consume_improper_spaces (p, body);
24392
24393 /* Consume the comma, if present. */
24394 if (*p == ',')
24395 {
24396 p++;
24397
24398 p = consume_improper_spaces (p, body);
24399 }
24400 }
24401
24402 if (*p == ')')
24403 {
24404 p++;
24405
24406 if (*p == ' ')
24407 /* Perfectly formed definition, no complaints. */
24408 macro_define_function (file, line, name,
6e70227d 24409 argc, (const char **) argv,
2e276125
JB
24410 p + 1);
24411 else if (*p == '\0')
24412 {
24413 /* Complain, but do define it. */
4d3c2250 24414 dwarf2_macro_malformed_definition_complaint (body);
2e276125 24415 macro_define_function (file, line, name,
6e70227d 24416 argc, (const char **) argv,
2e276125
JB
24417 p);
24418 }
24419 else
24420 /* Just complain. */
4d3c2250 24421 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24422 }
24423 else
24424 /* Just complain. */
4d3c2250 24425 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24426
24427 xfree (name);
24428 {
24429 int i;
24430
24431 for (i = 0; i < argc; i++)
24432 xfree (argv[i]);
24433 }
24434 xfree (argv);
24435 }
24436 else
4d3c2250 24437 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24438}
24439
cf2c3c16
TT
24440/* Skip some bytes from BYTES according to the form given in FORM.
24441 Returns the new pointer. */
2e276125 24442
d521ce57
TT
24443static const gdb_byte *
24444skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
24445 enum dwarf_form form,
24446 unsigned int offset_size,
24447 struct dwarf2_section_info *section)
2e276125 24448{
cf2c3c16 24449 unsigned int bytes_read;
2e276125 24450
cf2c3c16 24451 switch (form)
2e276125 24452 {
cf2c3c16
TT
24453 case DW_FORM_data1:
24454 case DW_FORM_flag:
24455 ++bytes;
24456 break;
24457
24458 case DW_FORM_data2:
24459 bytes += 2;
24460 break;
24461
24462 case DW_FORM_data4:
24463 bytes += 4;
24464 break;
24465
24466 case DW_FORM_data8:
24467 bytes += 8;
24468 break;
24469
0224619f
JK
24470 case DW_FORM_data16:
24471 bytes += 16;
24472 break;
24473
cf2c3c16
TT
24474 case DW_FORM_string:
24475 read_direct_string (abfd, bytes, &bytes_read);
24476 bytes += bytes_read;
24477 break;
24478
24479 case DW_FORM_sec_offset:
24480 case DW_FORM_strp:
36586728 24481 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
24482 bytes += offset_size;
24483 break;
24484
24485 case DW_FORM_block:
24486 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24487 bytes += bytes_read;
24488 break;
24489
24490 case DW_FORM_block1:
24491 bytes += 1 + read_1_byte (abfd, bytes);
24492 break;
24493 case DW_FORM_block2:
24494 bytes += 2 + read_2_bytes (abfd, bytes);
24495 break;
24496 case DW_FORM_block4:
24497 bytes += 4 + read_4_bytes (abfd, bytes);
24498 break;
24499
24500 case DW_FORM_sdata:
24501 case DW_FORM_udata:
3019eac3
DE
24502 case DW_FORM_GNU_addr_index:
24503 case DW_FORM_GNU_str_index:
d521ce57 24504 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
24505 if (bytes == NULL)
24506 {
24507 dwarf2_section_buffer_overflow_complaint (section);
24508 return NULL;
24509 }
cf2c3c16
TT
24510 break;
24511
663c44ac
JK
24512 case DW_FORM_implicit_const:
24513 break;
24514
cf2c3c16
TT
24515 default:
24516 {
cf2c3c16
TT
24517 complaint (&symfile_complaints,
24518 _("invalid form 0x%x in `%s'"),
a32a8923 24519 form, get_section_name (section));
cf2c3c16
TT
24520 return NULL;
24521 }
2e276125
JB
24522 }
24523
cf2c3c16
TT
24524 return bytes;
24525}
757a13d0 24526
cf2c3c16
TT
24527/* A helper for dwarf_decode_macros that handles skipping an unknown
24528 opcode. Returns an updated pointer to the macro data buffer; or,
24529 on error, issues a complaint and returns NULL. */
757a13d0 24530
d521ce57 24531static const gdb_byte *
cf2c3c16 24532skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24533 const gdb_byte **opcode_definitions,
24534 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24535 bfd *abfd,
24536 unsigned int offset_size,
24537 struct dwarf2_section_info *section)
24538{
24539 unsigned int bytes_read, i;
24540 unsigned long arg;
d521ce57 24541 const gdb_byte *defn;
2e276125 24542
cf2c3c16 24543 if (opcode_definitions[opcode] == NULL)
2e276125 24544 {
cf2c3c16
TT
24545 complaint (&symfile_complaints,
24546 _("unrecognized DW_MACFINO opcode 0x%x"),
24547 opcode);
24548 return NULL;
24549 }
2e276125 24550
cf2c3c16
TT
24551 defn = opcode_definitions[opcode];
24552 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24553 defn += bytes_read;
2e276125 24554
cf2c3c16
TT
24555 for (i = 0; i < arg; ++i)
24556 {
aead7601
SM
24557 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24558 (enum dwarf_form) defn[i], offset_size,
f664829e 24559 section);
cf2c3c16
TT
24560 if (mac_ptr == NULL)
24561 {
24562 /* skip_form_bytes already issued the complaint. */
24563 return NULL;
24564 }
24565 }
757a13d0 24566
cf2c3c16
TT
24567 return mac_ptr;
24568}
757a13d0 24569
cf2c3c16
TT
24570/* A helper function which parses the header of a macro section.
24571 If the macro section is the extended (for now called "GNU") type,
24572 then this updates *OFFSET_SIZE. Returns a pointer to just after
24573 the header, or issues a complaint and returns NULL on error. */
757a13d0 24574
d521ce57
TT
24575static const gdb_byte *
24576dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24577 bfd *abfd,
d521ce57 24578 const gdb_byte *mac_ptr,
cf2c3c16
TT
24579 unsigned int *offset_size,
24580 int section_is_gnu)
24581{
24582 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24583
cf2c3c16
TT
24584 if (section_is_gnu)
24585 {
24586 unsigned int version, flags;
757a13d0 24587
cf2c3c16 24588 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24589 if (version != 4 && version != 5)
cf2c3c16
TT
24590 {
24591 complaint (&symfile_complaints,
24592 _("unrecognized version `%d' in .debug_macro section"),
24593 version);
24594 return NULL;
24595 }
24596 mac_ptr += 2;
757a13d0 24597
cf2c3c16
TT
24598 flags = read_1_byte (abfd, mac_ptr);
24599 ++mac_ptr;
24600 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24601
cf2c3c16
TT
24602 if ((flags & 2) != 0)
24603 /* We don't need the line table offset. */
24604 mac_ptr += *offset_size;
757a13d0 24605
cf2c3c16
TT
24606 /* Vendor opcode descriptions. */
24607 if ((flags & 4) != 0)
24608 {
24609 unsigned int i, count;
757a13d0 24610
cf2c3c16
TT
24611 count = read_1_byte (abfd, mac_ptr);
24612 ++mac_ptr;
24613 for (i = 0; i < count; ++i)
24614 {
24615 unsigned int opcode, bytes_read;
24616 unsigned long arg;
24617
24618 opcode = read_1_byte (abfd, mac_ptr);
24619 ++mac_ptr;
24620 opcode_definitions[opcode] = mac_ptr;
24621 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24622 mac_ptr += bytes_read;
24623 mac_ptr += arg;
24624 }
757a13d0 24625 }
cf2c3c16 24626 }
757a13d0 24627
cf2c3c16
TT
24628 return mac_ptr;
24629}
757a13d0 24630
cf2c3c16 24631/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24632 including DW_MACRO_import. */
cf2c3c16
TT
24633
24634static void
ed2dc618
SM
24635dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24636 bfd *abfd,
d521ce57 24637 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24638 struct macro_source_file *current_file,
43f3e411 24639 struct line_header *lh,
cf2c3c16 24640 struct dwarf2_section_info *section,
36586728 24641 int section_is_gnu, int section_is_dwz,
cf2c3c16 24642 unsigned int offset_size,
8fc3fc34 24643 htab_t include_hash)
cf2c3c16 24644{
4d663531 24645 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24646 enum dwarf_macro_record_type macinfo_type;
24647 int at_commandline;
d521ce57 24648 const gdb_byte *opcode_definitions[256];
757a13d0 24649
cf2c3c16
TT
24650 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24651 &offset_size, section_is_gnu);
24652 if (mac_ptr == NULL)
24653 {
24654 /* We already issued a complaint. */
24655 return;
24656 }
757a13d0
JK
24657
24658 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24659 GDB is still reading the definitions from command line. First
24660 DW_MACINFO_start_file will need to be ignored as it was already executed
24661 to create CURRENT_FILE for the main source holding also the command line
24662 definitions. On first met DW_MACINFO_start_file this flag is reset to
24663 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24664
24665 at_commandline = 1;
24666
24667 do
24668 {
24669 /* Do we at least have room for a macinfo type byte? */
24670 if (mac_ptr >= mac_end)
24671 {
f664829e 24672 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24673 break;
24674 }
24675
aead7601 24676 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24677 mac_ptr++;
24678
cf2c3c16
TT
24679 /* Note that we rely on the fact that the corresponding GNU and
24680 DWARF constants are the same. */
132448f8
SM
24681 DIAGNOSTIC_PUSH
24682 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24683 switch (macinfo_type)
24684 {
24685 /* A zero macinfo type indicates the end of the macro
24686 information. */
24687 case 0:
24688 break;
2e276125 24689
0af92d60
JK
24690 case DW_MACRO_define:
24691 case DW_MACRO_undef:
24692 case DW_MACRO_define_strp:
24693 case DW_MACRO_undef_strp:
24694 case DW_MACRO_define_sup:
24695 case DW_MACRO_undef_sup:
2e276125 24696 {
891d2f0b 24697 unsigned int bytes_read;
2e276125 24698 int line;
d521ce57 24699 const char *body;
cf2c3c16 24700 int is_define;
2e276125 24701
cf2c3c16
TT
24702 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24703 mac_ptr += bytes_read;
24704
0af92d60
JK
24705 if (macinfo_type == DW_MACRO_define
24706 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24707 {
24708 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24709 mac_ptr += bytes_read;
24710 }
24711 else
24712 {
24713 LONGEST str_offset;
24714
24715 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24716 mac_ptr += offset_size;
2e276125 24717
0af92d60
JK
24718 if (macinfo_type == DW_MACRO_define_sup
24719 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24720 || section_is_dwz)
36586728 24721 {
ed2dc618
SM
24722 struct dwz_file *dwz
24723 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24724
ed2dc618
SM
24725 body = read_indirect_string_from_dwz (objfile,
24726 dwz, str_offset);
36586728
TT
24727 }
24728 else
ed2dc618
SM
24729 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24730 abfd, str_offset);
cf2c3c16
TT
24731 }
24732
0af92d60
JK
24733 is_define = (macinfo_type == DW_MACRO_define
24734 || macinfo_type == DW_MACRO_define_strp
24735 || macinfo_type == DW_MACRO_define_sup);
2e276125 24736 if (! current_file)
757a13d0
JK
24737 {
24738 /* DWARF violation as no main source is present. */
24739 complaint (&symfile_complaints,
24740 _("debug info with no main source gives macro %s "
24741 "on line %d: %s"),
cf2c3c16
TT
24742 is_define ? _("definition") : _("undefinition"),
24743 line, body);
757a13d0
JK
24744 break;
24745 }
3e43a32a
MS
24746 if ((line == 0 && !at_commandline)
24747 || (line != 0 && at_commandline))
4d3c2250 24748 complaint (&symfile_complaints,
757a13d0
JK
24749 _("debug info gives %s macro %s with %s line %d: %s"),
24750 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24751 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24752 line == 0 ? _("zero") : _("non-zero"), line, body);
24753
cf2c3c16 24754 if (is_define)
757a13d0 24755 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24756 else
24757 {
0af92d60
JK
24758 gdb_assert (macinfo_type == DW_MACRO_undef
24759 || macinfo_type == DW_MACRO_undef_strp
24760 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24761 macro_undef (current_file, line, body);
24762 }
2e276125
JB
24763 }
24764 break;
24765
0af92d60 24766 case DW_MACRO_start_file:
2e276125 24767 {
891d2f0b 24768 unsigned int bytes_read;
2e276125
JB
24769 int line, file;
24770
24771 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24772 mac_ptr += bytes_read;
24773 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24774 mac_ptr += bytes_read;
24775
3e43a32a
MS
24776 if ((line == 0 && !at_commandline)
24777 || (line != 0 && at_commandline))
757a13d0
JK
24778 complaint (&symfile_complaints,
24779 _("debug info gives source %d included "
24780 "from %s at %s line %d"),
24781 file, at_commandline ? _("command-line") : _("file"),
24782 line == 0 ? _("zero") : _("non-zero"), line);
24783
24784 if (at_commandline)
24785 {
0af92d60 24786 /* This DW_MACRO_start_file was executed in the
cf2c3c16 24787 pass one. */
757a13d0
JK
24788 at_commandline = 0;
24789 }
24790 else
43f3e411 24791 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
24792 }
24793 break;
24794
0af92d60 24795 case DW_MACRO_end_file:
2e276125 24796 if (! current_file)
4d3c2250 24797 complaint (&symfile_complaints,
3e43a32a
MS
24798 _("macro debug info has an unmatched "
24799 "`close_file' directive"));
2e276125
JB
24800 else
24801 {
24802 current_file = current_file->included_by;
24803 if (! current_file)
24804 {
cf2c3c16 24805 enum dwarf_macro_record_type next_type;
2e276125
JB
24806
24807 /* GCC circa March 2002 doesn't produce the zero
24808 type byte marking the end of the compilation
24809 unit. Complain if it's not there, but exit no
24810 matter what. */
24811
24812 /* Do we at least have room for a macinfo type byte? */
24813 if (mac_ptr >= mac_end)
24814 {
f664829e 24815 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
24816 return;
24817 }
24818
24819 /* We don't increment mac_ptr here, so this is just
24820 a look-ahead. */
aead7601
SM
24821 next_type
24822 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24823 mac_ptr);
2e276125 24824 if (next_type != 0)
4d3c2250 24825 complaint (&symfile_complaints,
3e43a32a
MS
24826 _("no terminating 0-type entry for "
24827 "macros in `.debug_macinfo' section"));
2e276125
JB
24828
24829 return;
24830 }
24831 }
24832 break;
24833
0af92d60
JK
24834 case DW_MACRO_import:
24835 case DW_MACRO_import_sup:
cf2c3c16
TT
24836 {
24837 LONGEST offset;
8fc3fc34 24838 void **slot;
a036ba48
TT
24839 bfd *include_bfd = abfd;
24840 struct dwarf2_section_info *include_section = section;
d521ce57 24841 const gdb_byte *include_mac_end = mac_end;
a036ba48 24842 int is_dwz = section_is_dwz;
d521ce57 24843 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
24844
24845 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24846 mac_ptr += offset_size;
24847
0af92d60 24848 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 24849 {
ed2dc618 24850 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 24851
4d663531 24852 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 24853
a036ba48 24854 include_section = &dwz->macro;
a32a8923 24855 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
24856 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24857 is_dwz = 1;
24858 }
24859
24860 new_mac_ptr = include_section->buffer + offset;
24861 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24862
8fc3fc34
TT
24863 if (*slot != NULL)
24864 {
24865 /* This has actually happened; see
24866 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24867 complaint (&symfile_complaints,
0af92d60 24868 _("recursive DW_MACRO_import in "
8fc3fc34
TT
24869 ".debug_macro section"));
24870 }
24871 else
24872 {
d521ce57 24873 *slot = (void *) new_mac_ptr;
36586728 24874
ed2dc618
SM
24875 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24876 include_bfd, new_mac_ptr,
43f3e411 24877 include_mac_end, current_file, lh,
36586728 24878 section, section_is_gnu, is_dwz,
4d663531 24879 offset_size, include_hash);
8fc3fc34 24880
d521ce57 24881 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 24882 }
cf2c3c16
TT
24883 }
24884 break;
24885
2e276125 24886 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
24887 if (!section_is_gnu)
24888 {
24889 unsigned int bytes_read;
2e276125 24890
ac298888
TT
24891 /* This reads the constant, but since we don't recognize
24892 any vendor extensions, we ignore it. */
24893 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
24894 mac_ptr += bytes_read;
24895 read_direct_string (abfd, mac_ptr, &bytes_read);
24896 mac_ptr += bytes_read;
2e276125 24897
cf2c3c16
TT
24898 /* We don't recognize any vendor extensions. */
24899 break;
24900 }
24901 /* FALLTHROUGH */
24902
24903 default:
24904 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24905 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24906 section);
24907 if (mac_ptr == NULL)
24908 return;
24909 break;
2e276125 24910 }
132448f8 24911 DIAGNOSTIC_POP
757a13d0 24912 } while (macinfo_type != 0);
2e276125 24913}
8e19ed76 24914
cf2c3c16 24915static void
09262596 24916dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 24917 int section_is_gnu)
cf2c3c16 24918{
518817b3
SM
24919 struct dwarf2_per_objfile *dwarf2_per_objfile
24920 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24921 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
24922 struct line_header *lh = cu->line_header;
24923 bfd *abfd;
d521ce57 24924 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
24925 struct macro_source_file *current_file = 0;
24926 enum dwarf_macro_record_type macinfo_type;
24927 unsigned int offset_size = cu->header.offset_size;
d521ce57 24928 const gdb_byte *opcode_definitions[256];
8fc3fc34 24929 void **slot;
09262596
DE
24930 struct dwarf2_section_info *section;
24931 const char *section_name;
24932
24933 if (cu->dwo_unit != NULL)
24934 {
24935 if (section_is_gnu)
24936 {
24937 section = &cu->dwo_unit->dwo_file->sections.macro;
24938 section_name = ".debug_macro.dwo";
24939 }
24940 else
24941 {
24942 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24943 section_name = ".debug_macinfo.dwo";
24944 }
24945 }
24946 else
24947 {
24948 if (section_is_gnu)
24949 {
24950 section = &dwarf2_per_objfile->macro;
24951 section_name = ".debug_macro";
24952 }
24953 else
24954 {
24955 section = &dwarf2_per_objfile->macinfo;
24956 section_name = ".debug_macinfo";
24957 }
24958 }
cf2c3c16 24959
bb5ed363 24960 dwarf2_read_section (objfile, section);
cf2c3c16
TT
24961 if (section->buffer == NULL)
24962 {
fceca515 24963 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
24964 return;
24965 }
a32a8923 24966 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
24967
24968 /* First pass: Find the name of the base filename.
24969 This filename is needed in order to process all macros whose definition
24970 (or undefinition) comes from the command line. These macros are defined
24971 before the first DW_MACINFO_start_file entry, and yet still need to be
24972 associated to the base file.
24973
24974 To determine the base file name, we scan the macro definitions until we
24975 reach the first DW_MACINFO_start_file entry. We then initialize
24976 CURRENT_FILE accordingly so that any macro definition found before the
24977 first DW_MACINFO_start_file can still be associated to the base file. */
24978
24979 mac_ptr = section->buffer + offset;
24980 mac_end = section->buffer + section->size;
24981
24982 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24983 &offset_size, section_is_gnu);
24984 if (mac_ptr == NULL)
24985 {
24986 /* We already issued a complaint. */
24987 return;
24988 }
24989
24990 do
24991 {
24992 /* Do we at least have room for a macinfo type byte? */
24993 if (mac_ptr >= mac_end)
24994 {
24995 /* Complaint is printed during the second pass as GDB will probably
24996 stop the first pass earlier upon finding
24997 DW_MACINFO_start_file. */
24998 break;
24999 }
25000
aead7601 25001 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
25002 mac_ptr++;
25003
25004 /* Note that we rely on the fact that the corresponding GNU and
25005 DWARF constants are the same. */
132448f8
SM
25006 DIAGNOSTIC_PUSH
25007 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
25008 switch (macinfo_type)
25009 {
25010 /* A zero macinfo type indicates the end of the macro
25011 information. */
25012 case 0:
25013 break;
25014
0af92d60
JK
25015 case DW_MACRO_define:
25016 case DW_MACRO_undef:
cf2c3c16
TT
25017 /* Only skip the data by MAC_PTR. */
25018 {
25019 unsigned int bytes_read;
25020
25021 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25022 mac_ptr += bytes_read;
25023 read_direct_string (abfd, mac_ptr, &bytes_read);
25024 mac_ptr += bytes_read;
25025 }
25026 break;
25027
0af92d60 25028 case DW_MACRO_start_file:
cf2c3c16
TT
25029 {
25030 unsigned int bytes_read;
25031 int line, file;
25032
25033 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25034 mac_ptr += bytes_read;
25035 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25036 mac_ptr += bytes_read;
25037
43f3e411 25038 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
25039 }
25040 break;
25041
0af92d60 25042 case DW_MACRO_end_file:
cf2c3c16
TT
25043 /* No data to skip by MAC_PTR. */
25044 break;
25045
0af92d60
JK
25046 case DW_MACRO_define_strp:
25047 case DW_MACRO_undef_strp:
25048 case DW_MACRO_define_sup:
25049 case DW_MACRO_undef_sup:
cf2c3c16
TT
25050 {
25051 unsigned int bytes_read;
25052
25053 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25054 mac_ptr += bytes_read;
25055 mac_ptr += offset_size;
25056 }
25057 break;
25058
0af92d60
JK
25059 case DW_MACRO_import:
25060 case DW_MACRO_import_sup:
cf2c3c16 25061 /* Note that, according to the spec, a transparent include
0af92d60 25062 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
25063 skip this opcode. */
25064 mac_ptr += offset_size;
25065 break;
25066
25067 case DW_MACINFO_vendor_ext:
25068 /* Only skip the data by MAC_PTR. */
25069 if (!section_is_gnu)
25070 {
25071 unsigned int bytes_read;
25072
25073 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25074 mac_ptr += bytes_read;
25075 read_direct_string (abfd, mac_ptr, &bytes_read);
25076 mac_ptr += bytes_read;
25077 }
25078 /* FALLTHROUGH */
25079
25080 default:
25081 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 25082 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
25083 section);
25084 if (mac_ptr == NULL)
25085 return;
25086 break;
25087 }
132448f8 25088 DIAGNOSTIC_POP
cf2c3c16
TT
25089 } while (macinfo_type != 0 && current_file == NULL);
25090
25091 /* Second pass: Process all entries.
25092
25093 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25094 command-line macro definitions/undefinitions. This flag is unset when we
25095 reach the first DW_MACINFO_start_file entry. */
25096
fc4007c9
TT
25097 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25098 htab_eq_pointer,
25099 NULL, xcalloc, xfree));
8fc3fc34 25100 mac_ptr = section->buffer + offset;
fc4007c9 25101 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 25102 *slot = (void *) mac_ptr;
ed2dc618
SM
25103 dwarf_decode_macro_bytes (dwarf2_per_objfile,
25104 abfd, mac_ptr, mac_end,
43f3e411 25105 current_file, lh, section,
fc4007c9
TT
25106 section_is_gnu, 0, offset_size,
25107 include_hash.get ());
cf2c3c16
TT
25108}
25109
8e19ed76 25110/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 25111 if so return true else false. */
380bca97 25112
8e19ed76 25113static int
6e5a29e1 25114attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
25115{
25116 return (attr == NULL ? 0 :
25117 attr->form == DW_FORM_block1
25118 || attr->form == DW_FORM_block2
25119 || attr->form == DW_FORM_block4
2dc7f7b3
TT
25120 || attr->form == DW_FORM_block
25121 || attr->form == DW_FORM_exprloc);
8e19ed76 25122}
4c2df51b 25123
c6a0999f
JB
25124/* Return non-zero if ATTR's value is a section offset --- classes
25125 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25126 You may use DW_UNSND (attr) to retrieve such offsets.
25127
25128 Section 7.5.4, "Attribute Encodings", explains that no attribute
25129 may have a value that belongs to more than one of these classes; it
25130 would be ambiguous if we did, because we use the same forms for all
25131 of them. */
380bca97 25132
3690dd37 25133static int
6e5a29e1 25134attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
25135{
25136 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
25137 || attr->form == DW_FORM_data8
25138 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
25139}
25140
3690dd37
JB
25141/* Return non-zero if ATTR's value falls in the 'constant' class, or
25142 zero otherwise. When this function returns true, you can apply
25143 dwarf2_get_attr_constant_value to it.
25144
25145 However, note that for some attributes you must check
25146 attr_form_is_section_offset before using this test. DW_FORM_data4
25147 and DW_FORM_data8 are members of both the constant class, and of
25148 the classes that contain offsets into other debug sections
25149 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25150 that, if an attribute's can be either a constant or one of the
25151 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
25152 taken as section offsets, not constants.
25153
25154 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25155 cannot handle that. */
380bca97 25156
3690dd37 25157static int
6e5a29e1 25158attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
25159{
25160 switch (attr->form)
25161 {
25162 case DW_FORM_sdata:
25163 case DW_FORM_udata:
25164 case DW_FORM_data1:
25165 case DW_FORM_data2:
25166 case DW_FORM_data4:
25167 case DW_FORM_data8:
663c44ac 25168 case DW_FORM_implicit_const:
3690dd37
JB
25169 return 1;
25170 default:
25171 return 0;
25172 }
25173}
25174
7771576e
SA
25175
25176/* DW_ADDR is always stored already as sect_offset; despite for the forms
25177 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25178
25179static int
6e5a29e1 25180attr_form_is_ref (const struct attribute *attr)
7771576e
SA
25181{
25182 switch (attr->form)
25183 {
25184 case DW_FORM_ref_addr:
25185 case DW_FORM_ref1:
25186 case DW_FORM_ref2:
25187 case DW_FORM_ref4:
25188 case DW_FORM_ref8:
25189 case DW_FORM_ref_udata:
25190 case DW_FORM_GNU_ref_alt:
25191 return 1;
25192 default:
25193 return 0;
25194 }
25195}
25196
3019eac3
DE
25197/* Return the .debug_loc section to use for CU.
25198 For DWO files use .debug_loc.dwo. */
25199
25200static struct dwarf2_section_info *
25201cu_debug_loc_section (struct dwarf2_cu *cu)
25202{
518817b3
SM
25203 struct dwarf2_per_objfile *dwarf2_per_objfile
25204 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 25205
3019eac3 25206 if (cu->dwo_unit)
43988095
JK
25207 {
25208 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25209
25210 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25211 }
25212 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25213 : &dwarf2_per_objfile->loc);
3019eac3
DE
25214}
25215
8cf6f0b1
TT
25216/* A helper function that fills in a dwarf2_loclist_baton. */
25217
25218static void
25219fill_in_loclist_baton (struct dwarf2_cu *cu,
25220 struct dwarf2_loclist_baton *baton,
ff39bb5e 25221 const struct attribute *attr)
8cf6f0b1 25222{
518817b3
SM
25223 struct dwarf2_per_objfile *dwarf2_per_objfile
25224 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
25225 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25226
25227 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
25228
25229 baton->per_cu = cu->per_cu;
25230 gdb_assert (baton->per_cu);
25231 /* We don't know how long the location list is, but make sure we
25232 don't run off the edge of the section. */
3019eac3
DE
25233 baton->size = section->size - DW_UNSND (attr);
25234 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 25235 baton->base_address = cu->base_address;
f664829e 25236 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
25237}
25238
4c2df51b 25239static void
ff39bb5e 25240dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 25241 struct dwarf2_cu *cu, int is_block)
4c2df51b 25242{
518817b3
SM
25243 struct dwarf2_per_objfile *dwarf2_per_objfile
25244 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 25245 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 25246 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 25247
3690dd37 25248 if (attr_form_is_section_offset (attr)
3019eac3 25249 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
25250 the section. If so, fall through to the complaint in the
25251 other branch. */
3019eac3 25252 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 25253 {
0d53c4c4 25254 struct dwarf2_loclist_baton *baton;
4c2df51b 25255
8d749320 25256 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 25257
8cf6f0b1 25258 fill_in_loclist_baton (cu, baton, attr);
be391dca 25259
d00adf39 25260 if (cu->base_known == 0)
0d53c4c4 25261 complaint (&symfile_complaints,
3e43a32a
MS
25262 _("Location list used without "
25263 "specifying the CU base address."));
4c2df51b 25264
f1e6e072
TT
25265 SYMBOL_ACLASS_INDEX (sym) = (is_block
25266 ? dwarf2_loclist_block_index
25267 : dwarf2_loclist_index);
0d53c4c4
DJ
25268 SYMBOL_LOCATION_BATON (sym) = baton;
25269 }
25270 else
25271 {
25272 struct dwarf2_locexpr_baton *baton;
25273
8d749320 25274 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
25275 baton->per_cu = cu->per_cu;
25276 gdb_assert (baton->per_cu);
0d53c4c4
DJ
25277
25278 if (attr_form_is_block (attr))
25279 {
25280 /* Note that we're just copying the block's data pointer
25281 here, not the actual data. We're still pointing into the
6502dd73
DJ
25282 info_buffer for SYM's objfile; right now we never release
25283 that buffer, but when we do clean up properly this may
25284 need to change. */
0d53c4c4
DJ
25285 baton->size = DW_BLOCK (attr)->size;
25286 baton->data = DW_BLOCK (attr)->data;
25287 }
25288 else
25289 {
25290 dwarf2_invalid_attrib_class_complaint ("location description",
25291 SYMBOL_NATURAL_NAME (sym));
25292 baton->size = 0;
0d53c4c4 25293 }
6e70227d 25294
f1e6e072
TT
25295 SYMBOL_ACLASS_INDEX (sym) = (is_block
25296 ? dwarf2_locexpr_block_index
25297 : dwarf2_locexpr_index);
0d53c4c4
DJ
25298 SYMBOL_LOCATION_BATON (sym) = baton;
25299 }
4c2df51b 25300}
6502dd73 25301
9aa1f1e3
TT
25302/* Return the OBJFILE associated with the compilation unit CU. If CU
25303 came from a separate debuginfo file, then the master objfile is
25304 returned. */
ae0d2f24
UW
25305
25306struct objfile *
25307dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25308{
e3b94546 25309 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
25310
25311 /* Return the master objfile, so that we can report and look up the
25312 correct file containing this variable. */
25313 if (objfile->separate_debug_objfile_backlink)
25314 objfile = objfile->separate_debug_objfile_backlink;
25315
25316 return objfile;
25317}
25318
96408a79
SA
25319/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25320 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25321 CU_HEADERP first. */
25322
25323static const struct comp_unit_head *
25324per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25325 struct dwarf2_per_cu_data *per_cu)
25326{
d521ce57 25327 const gdb_byte *info_ptr;
96408a79
SA
25328
25329 if (per_cu->cu)
25330 return &per_cu->cu->header;
25331
9c541725 25332 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
25333
25334 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
25335 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25336 rcuh_kind::COMPILE);
96408a79
SA
25337
25338 return cu_headerp;
25339}
25340
ae0d2f24
UW
25341/* Return the address size given in the compilation unit header for CU. */
25342
98714339 25343int
ae0d2f24
UW
25344dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25345{
96408a79
SA
25346 struct comp_unit_head cu_header_local;
25347 const struct comp_unit_head *cu_headerp;
c471e790 25348
96408a79
SA
25349 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25350
25351 return cu_headerp->addr_size;
ae0d2f24
UW
25352}
25353
9eae7c52
TT
25354/* Return the offset size given in the compilation unit header for CU. */
25355
25356int
25357dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25358{
96408a79
SA
25359 struct comp_unit_head cu_header_local;
25360 const struct comp_unit_head *cu_headerp;
9c6c53f7 25361
96408a79
SA
25362 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25363
25364 return cu_headerp->offset_size;
25365}
25366
25367/* See its dwarf2loc.h declaration. */
25368
25369int
25370dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25371{
25372 struct comp_unit_head cu_header_local;
25373 const struct comp_unit_head *cu_headerp;
25374
25375 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25376
25377 if (cu_headerp->version == 2)
25378 return cu_headerp->addr_size;
25379 else
25380 return cu_headerp->offset_size;
181cebd4
JK
25381}
25382
9aa1f1e3
TT
25383/* Return the text offset of the CU. The returned offset comes from
25384 this CU's objfile. If this objfile came from a separate debuginfo
25385 file, then the offset may be different from the corresponding
25386 offset in the parent objfile. */
25387
25388CORE_ADDR
25389dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25390{
e3b94546 25391 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9aa1f1e3
TT
25392
25393 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25394}
25395
43988095
JK
25396/* Return DWARF version number of PER_CU. */
25397
25398short
25399dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25400{
25401 return per_cu->dwarf_version;
25402}
25403
348e048f
DE
25404/* Locate the .debug_info compilation unit from CU's objfile which contains
25405 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
25406
25407static struct dwarf2_per_cu_data *
9c541725 25408dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 25409 unsigned int offset_in_dwz,
ed2dc618 25410 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25411{
25412 struct dwarf2_per_cu_data *this_cu;
25413 int low, high;
36586728 25414 const sect_offset *cu_off;
ae038cb0 25415
ae038cb0
DJ
25416 low = 0;
25417 high = dwarf2_per_objfile->n_comp_units - 1;
25418 while (high > low)
25419 {
36586728 25420 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 25421 int mid = low + (high - low) / 2;
9a619af0 25422
36586728 25423 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
9c541725 25424 cu_off = &mid_cu->sect_off;
36586728 25425 if (mid_cu->is_dwz > offset_in_dwz
9c541725 25426 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
ae038cb0
DJ
25427 high = mid;
25428 else
25429 low = mid + 1;
25430 }
25431 gdb_assert (low == high);
36586728 25432 this_cu = dwarf2_per_objfile->all_comp_units[low];
9c541725
PA
25433 cu_off = &this_cu->sect_off;
25434 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
ae038cb0 25435 {
36586728 25436 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 25437 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
25438 "offset %s [in module %s]"),
25439 sect_offset_str (sect_off),
ed2dc618 25440 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 25441
9c541725
PA
25442 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25443 <= sect_off);
ae038cb0
DJ
25444 return dwarf2_per_objfile->all_comp_units[low-1];
25445 }
25446 else
25447 {
25448 this_cu = dwarf2_per_objfile->all_comp_units[low];
25449 if (low == dwarf2_per_objfile->n_comp_units - 1
9c541725 25450 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 25451 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 25452 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
25453 return this_cu;
25454 }
25455}
25456
23745b47 25457/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 25458
fcd3b13d
SM
25459dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25460 : per_cu (per_cu_),
25461 mark (0),
25462 has_loclist (0),
25463 checked_producer (0),
25464 producer_is_gxx_lt_4_6 (0),
25465 producer_is_gcc_lt_4_3 (0),
25466 producer_is_icc_lt_14 (0),
25467 processing_has_namespace_info (0)
93311388 25468{
fcd3b13d
SM
25469 per_cu->cu = this;
25470}
25471
25472/* Destroy a dwarf2_cu. */
25473
25474dwarf2_cu::~dwarf2_cu ()
25475{
25476 per_cu->cu = NULL;
9816fde3
JK
25477}
25478
25479/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25480
25481static void
95554aad
TT
25482prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25483 enum language pretend_language)
9816fde3
JK
25484{
25485 struct attribute *attr;
25486
25487 /* Set the language we're debugging. */
25488 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25489 if (attr)
25490 set_cu_language (DW_UNSND (attr), cu);
25491 else
9cded63f 25492 {
95554aad 25493 cu->language = pretend_language;
9cded63f
TT
25494 cu->language_defn = language_def (cu->language);
25495 }
dee91e82 25496
7d45c7c3 25497 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25498}
25499
ae038cb0
DJ
25500/* Free all cached compilation units. */
25501
25502static void
25503free_cached_comp_units (void *data)
25504{
ed2dc618
SM
25505 struct dwarf2_per_objfile *dwarf2_per_objfile
25506 = (struct dwarf2_per_objfile *) data;
25507
330cdd98 25508 dwarf2_per_objfile->free_cached_comp_units ();
ae038cb0
DJ
25509}
25510
25511/* Increase the age counter on each cached compilation unit, and free
25512 any that are too old. */
25513
25514static void
ed2dc618 25515age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25516{
25517 struct dwarf2_per_cu_data *per_cu, **last_chain;
25518
25519 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25520 per_cu = dwarf2_per_objfile->read_in_chain;
25521 while (per_cu != NULL)
25522 {
25523 per_cu->cu->last_used ++;
b4f54984 25524 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
25525 dwarf2_mark (per_cu->cu);
25526 per_cu = per_cu->cu->read_in_chain;
25527 }
25528
25529 per_cu = dwarf2_per_objfile->read_in_chain;
25530 last_chain = &dwarf2_per_objfile->read_in_chain;
25531 while (per_cu != NULL)
25532 {
25533 struct dwarf2_per_cu_data *next_cu;
25534
25535 next_cu = per_cu->cu->read_in_chain;
25536
25537 if (!per_cu->cu->mark)
25538 {
fcd3b13d 25539 delete per_cu->cu;
ae038cb0
DJ
25540 *last_chain = next_cu;
25541 }
25542 else
25543 last_chain = &per_cu->cu->read_in_chain;
25544
25545 per_cu = next_cu;
25546 }
25547}
25548
25549/* Remove a single compilation unit from the cache. */
25550
25551static void
dee91e82 25552free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25553{
25554 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
25555 struct dwarf2_per_objfile *dwarf2_per_objfile
25556 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
25557
25558 per_cu = dwarf2_per_objfile->read_in_chain;
25559 last_chain = &dwarf2_per_objfile->read_in_chain;
25560 while (per_cu != NULL)
25561 {
25562 struct dwarf2_per_cu_data *next_cu;
25563
25564 next_cu = per_cu->cu->read_in_chain;
25565
dee91e82 25566 if (per_cu == target_per_cu)
ae038cb0 25567 {
fcd3b13d 25568 delete per_cu->cu;
dee91e82 25569 per_cu->cu = NULL;
ae038cb0
DJ
25570 *last_chain = next_cu;
25571 break;
25572 }
25573 else
25574 last_chain = &per_cu->cu->read_in_chain;
25575
25576 per_cu = next_cu;
25577 }
25578}
25579
fe3e1990
DJ
25580/* Release all extra memory associated with OBJFILE. */
25581
25582void
25583dwarf2_free_objfile (struct objfile *objfile)
25584{
ed2dc618
SM
25585 struct dwarf2_per_objfile *dwarf2_per_objfile
25586 = get_dwarf2_per_objfile (objfile);
fe3e1990 25587
fd90ace4 25588 delete dwarf2_per_objfile;
fe3e1990
DJ
25589}
25590
dee91e82
DE
25591/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25592 We store these in a hash table separate from the DIEs, and preserve them
25593 when the DIEs are flushed out of cache.
25594
25595 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25596 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25597 or the type may come from a DWO file. Furthermore, while it's more logical
25598 to use per_cu->section+offset, with Fission the section with the data is in
25599 the DWO file but we don't know that section at the point we need it.
25600 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25601 because we can enter the lookup routine, get_die_type_at_offset, from
25602 outside this file, and thus won't necessarily have PER_CU->cu.
25603 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25604
dee91e82 25605struct dwarf2_per_cu_offset_and_type
1c379e20 25606{
dee91e82 25607 const struct dwarf2_per_cu_data *per_cu;
9c541725 25608 sect_offset sect_off;
1c379e20
DJ
25609 struct type *type;
25610};
25611
dee91e82 25612/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25613
25614static hashval_t
dee91e82 25615per_cu_offset_and_type_hash (const void *item)
1c379e20 25616{
9a3c8263
SM
25617 const struct dwarf2_per_cu_offset_and_type *ofs
25618 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25619
9c541725 25620 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25621}
25622
dee91e82 25623/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25624
25625static int
dee91e82 25626per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25627{
9a3c8263
SM
25628 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25629 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25630 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25631 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25632
dee91e82 25633 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25634 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25635}
25636
25637/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25638 table if necessary. For convenience, return TYPE.
25639
25640 The DIEs reading must have careful ordering to:
25641 * Not cause infite loops trying to read in DIEs as a prerequisite for
25642 reading current DIE.
25643 * Not trying to dereference contents of still incompletely read in types
25644 while reading in other DIEs.
25645 * Enable referencing still incompletely read in types just by a pointer to
25646 the type without accessing its fields.
25647
25648 Therefore caller should follow these rules:
25649 * Try to fetch any prerequisite types we may need to build this DIE type
25650 before building the type and calling set_die_type.
e71ec853 25651 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25652 possible before fetching more types to complete the current type.
25653 * Make the type as complete as possible before fetching more types. */
1c379e20 25654
f792889a 25655static struct type *
1c379e20
DJ
25656set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25657{
518817b3
SM
25658 struct dwarf2_per_objfile *dwarf2_per_objfile
25659 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25660 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25661 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25662 struct attribute *attr;
25663 struct dynamic_prop prop;
1c379e20 25664
b4ba55a1
JB
25665 /* For Ada types, make sure that the gnat-specific data is always
25666 initialized (if not already set). There are a few types where
25667 we should not be doing so, because the type-specific area is
25668 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25669 where the type-specific area is used to store the floatformat).
25670 But this is not a problem, because the gnat-specific information
25671 is actually not needed for these types. */
25672 if (need_gnat_info (cu)
25673 && TYPE_CODE (type) != TYPE_CODE_FUNC
25674 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25675 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25676 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25677 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25678 && !HAVE_GNAT_AUX_INFO (type))
25679 INIT_GNAT_SPECIFIC (type);
25680
3f2f83dd
KB
25681 /* Read DW_AT_allocated and set in type. */
25682 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25683 if (attr_form_is_block (attr))
25684 {
25685 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25686 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25687 }
25688 else if (attr != NULL)
25689 {
25690 complaint (&symfile_complaints,
9d8780f0 25691 _("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 25692 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25693 sect_offset_str (die->sect_off));
3f2f83dd
KB
25694 }
25695
25696 /* Read DW_AT_associated and set in type. */
25697 attr = dwarf2_attr (die, DW_AT_associated, cu);
25698 if (attr_form_is_block (attr))
25699 {
25700 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25701 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25702 }
25703 else if (attr != NULL)
25704 {
25705 complaint (&symfile_complaints,
9d8780f0 25706 _("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 25707 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25708 sect_offset_str (die->sect_off));
3f2f83dd
KB
25709 }
25710
3cdcd0ce
JB
25711 /* Read DW_AT_data_location and set in type. */
25712 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25713 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25714 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25715
dee91e82 25716 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25717 {
dee91e82
DE
25718 dwarf2_per_objfile->die_type_hash =
25719 htab_create_alloc_ex (127,
25720 per_cu_offset_and_type_hash,
25721 per_cu_offset_and_type_eq,
25722 NULL,
25723 &objfile->objfile_obstack,
25724 hashtab_obstack_allocate,
25725 dummy_obstack_deallocate);
f792889a 25726 }
1c379e20 25727
dee91e82 25728 ofs.per_cu = cu->per_cu;
9c541725 25729 ofs.sect_off = die->sect_off;
1c379e20 25730 ofs.type = type;
dee91e82
DE
25731 slot = (struct dwarf2_per_cu_offset_and_type **)
25732 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
25733 if (*slot)
25734 complaint (&symfile_complaints,
9d8780f0
SM
25735 _("A problem internal to GDB: DIE %s has type already set"),
25736 sect_offset_str (die->sect_off));
8d749320
SM
25737 *slot = XOBNEW (&objfile->objfile_obstack,
25738 struct dwarf2_per_cu_offset_and_type);
1c379e20 25739 **slot = ofs;
f792889a 25740 return type;
1c379e20
DJ
25741}
25742
9c541725 25743/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25744 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25745
25746static struct type *
9c541725 25747get_die_type_at_offset (sect_offset sect_off,
673bfd45 25748 struct dwarf2_per_cu_data *per_cu)
1c379e20 25749{
dee91e82 25750 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 25751 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 25752
dee91e82 25753 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25754 return NULL;
1c379e20 25755
dee91e82 25756 ofs.per_cu = per_cu;
9c541725 25757 ofs.sect_off = sect_off;
9a3c8263
SM
25758 slot = ((struct dwarf2_per_cu_offset_and_type *)
25759 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25760 if (slot)
25761 return slot->type;
25762 else
25763 return NULL;
25764}
25765
02142a6c 25766/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25767 or return NULL if DIE does not have a saved type. */
25768
25769static struct type *
25770get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25771{
9c541725 25772 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
25773}
25774
10b3939b
DJ
25775/* Add a dependence relationship from CU to REF_PER_CU. */
25776
25777static void
25778dwarf2_add_dependence (struct dwarf2_cu *cu,
25779 struct dwarf2_per_cu_data *ref_per_cu)
25780{
25781 void **slot;
25782
25783 if (cu->dependencies == NULL)
25784 cu->dependencies
25785 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25786 NULL, &cu->comp_unit_obstack,
25787 hashtab_obstack_allocate,
25788 dummy_obstack_deallocate);
25789
25790 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25791 if (*slot == NULL)
25792 *slot = ref_per_cu;
25793}
1c379e20 25794
f504f079
DE
25795/* Subroutine of dwarf2_mark to pass to htab_traverse.
25796 Set the mark field in every compilation unit in the
ae038cb0
DJ
25797 cache that we must keep because we are keeping CU. */
25798
10b3939b
DJ
25799static int
25800dwarf2_mark_helper (void **slot, void *data)
25801{
25802 struct dwarf2_per_cu_data *per_cu;
25803
25804 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
25805
25806 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25807 reading of the chain. As such dependencies remain valid it is not much
25808 useful to track and undo them during QUIT cleanups. */
25809 if (per_cu->cu == NULL)
25810 return 1;
25811
10b3939b
DJ
25812 if (per_cu->cu->mark)
25813 return 1;
25814 per_cu->cu->mark = 1;
25815
25816 if (per_cu->cu->dependencies != NULL)
25817 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25818
25819 return 1;
25820}
25821
f504f079
DE
25822/* Set the mark field in CU and in every other compilation unit in the
25823 cache that we must keep because we are keeping CU. */
25824
ae038cb0
DJ
25825static void
25826dwarf2_mark (struct dwarf2_cu *cu)
25827{
25828 if (cu->mark)
25829 return;
25830 cu->mark = 1;
10b3939b
DJ
25831 if (cu->dependencies != NULL)
25832 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
25833}
25834
25835static void
25836dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25837{
25838 while (per_cu)
25839 {
25840 per_cu->cu->mark = 0;
25841 per_cu = per_cu->cu->read_in_chain;
25842 }
72bf9492
DJ
25843}
25844
72bf9492
DJ
25845/* Trivial hash function for partial_die_info: the hash value of a DIE
25846 is its offset in .debug_info for this objfile. */
25847
25848static hashval_t
25849partial_die_hash (const void *item)
25850{
9a3c8263
SM
25851 const struct partial_die_info *part_die
25852 = (const struct partial_die_info *) item;
9a619af0 25853
9c541725 25854 return to_underlying (part_die->sect_off);
72bf9492
DJ
25855}
25856
25857/* Trivial comparison function for partial_die_info structures: two DIEs
25858 are equal if they have the same offset. */
25859
25860static int
25861partial_die_eq (const void *item_lhs, const void *item_rhs)
25862{
9a3c8263
SM
25863 const struct partial_die_info *part_die_lhs
25864 = (const struct partial_die_info *) item_lhs;
25865 const struct partial_die_info *part_die_rhs
25866 = (const struct partial_die_info *) item_rhs;
9a619af0 25867
9c541725 25868 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25869}
25870
b4f54984
DE
25871static struct cmd_list_element *set_dwarf_cmdlist;
25872static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
25873
25874static void
981a3fb3 25875set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 25876{
b4f54984 25877 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 25878 gdb_stdout);
ae038cb0
DJ
25879}
25880
25881static void
981a3fb3 25882show_dwarf_cmd (const char *args, int from_tty)
6e70227d 25883{
b4f54984 25884 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
25885}
25886
ae2de4f8 25887/* The "save gdb-index" command. */
9291a0cd 25888
437afbb8
JK
25889/* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
25890 error checking. */
25891
25892static void
25893file_write (FILE *file, const void *data, size_t size)
25894{
25895 if (fwrite (data, 1, size, file) != size)
25896 error (_("couldn't data write to file"));
25897}
25898
25899/* Write the contents of VEC to FILE, with error checking. */
25900
25901template<typename Elem, typename Alloc>
25902static void
25903file_write (FILE *file, const std::vector<Elem, Alloc> &vec)
25904{
25905 file_write (file, vec.data (), vec.size () * sizeof (vec[0]));
25906}
25907
bc8f2430
JK
25908/* In-memory buffer to prepare data to be written later to a file. */
25909class data_buf
9291a0cd 25910{
bc8f2430 25911public:
bc8f2430
JK
25912 /* Copy DATA to the end of the buffer. */
25913 template<typename T>
25914 void append_data (const T &data)
25915 {
25916 std::copy (reinterpret_cast<const gdb_byte *> (&data),
25917 reinterpret_cast<const gdb_byte *> (&data + 1),
c2f134ac 25918 grow (sizeof (data)));
bc8f2430 25919 }
b89be57b 25920
c2f134ac
PA
25921 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
25922 terminating zero is appended too. */
bc8f2430
JK
25923 void append_cstr0 (const char *cstr)
25924 {
25925 const size_t size = strlen (cstr) + 1;
c2f134ac
PA
25926 std::copy (cstr, cstr + size, grow (size));
25927 }
25928
437afbb8
JK
25929 /* Store INPUT as ULEB128 to the end of buffer. */
25930 void append_unsigned_leb128 (ULONGEST input)
25931 {
25932 for (;;)
25933 {
25934 gdb_byte output = input & 0x7f;
25935 input >>= 7;
25936 if (input)
25937 output |= 0x80;
25938 append_data (output);
25939 if (input == 0)
25940 break;
25941 }
25942 }
25943
c2f134ac
PA
25944 /* Accept a host-format integer in VAL and append it to the buffer
25945 as a target-format integer which is LEN bytes long. */
25946 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
25947 {
25948 ::store_unsigned_integer (grow (len), len, byte_order, val);
bc8f2430 25949 }
9291a0cd 25950
bc8f2430
JK
25951 /* Return the size of the buffer. */
25952 size_t size () const
25953 {
25954 return m_vec.size ();
25955 }
25956
437afbb8
JK
25957 /* Return true iff the buffer is empty. */
25958 bool empty () const
25959 {
25960 return m_vec.empty ();
25961 }
25962
bc8f2430
JK
25963 /* Write the buffer to FILE. */
25964 void file_write (FILE *file) const
25965 {
437afbb8 25966 ::file_write (file, m_vec);
bc8f2430
JK
25967 }
25968
25969private:
c2f134ac
PA
25970 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
25971 the start of the new block. */
25972 gdb_byte *grow (size_t size)
25973 {
25974 m_vec.resize (m_vec.size () + size);
25975 return &*m_vec.end () - size;
25976 }
25977
d5722aa2 25978 gdb::byte_vector m_vec;
bc8f2430 25979};
9291a0cd
TT
25980
25981/* An entry in the symbol table. */
25982struct symtab_index_entry
25983{
25984 /* The name of the symbol. */
25985 const char *name;
25986 /* The offset of the name in the constant pool. */
25987 offset_type index_offset;
25988 /* A sorted vector of the indices of all the CUs that hold an object
25989 of this name. */
bc8f2430 25990 std::vector<offset_type> cu_indices;
9291a0cd
TT
25991};
25992
25993/* The symbol table. This is a power-of-2-sized hash table. */
25994struct mapped_symtab
25995{
bc8f2430
JK
25996 mapped_symtab ()
25997 {
25998 data.resize (1024);
25999 }
b89be57b 26000
bc8f2430 26001 offset_type n_elements = 0;
4b76cda9 26002 std::vector<symtab_index_entry> data;
bc8f2430 26003};
9291a0cd 26004
bc8f2430 26005/* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
559a7a62
JK
26006 the slot.
26007
26008 Function is used only during write_hash_table so no index format backward
26009 compatibility is needed. */
b89be57b 26010
4b76cda9 26011static symtab_index_entry &
9291a0cd
TT
26012find_slot (struct mapped_symtab *symtab, const char *name)
26013{
559a7a62 26014 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd 26015
bc8f2430
JK
26016 index = hash & (symtab->data.size () - 1);
26017 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
9291a0cd
TT
26018
26019 for (;;)
26020 {
4b76cda9
PA
26021 if (symtab->data[index].name == NULL
26022 || strcmp (name, symtab->data[index].name) == 0)
bc8f2430
JK
26023 return symtab->data[index];
26024 index = (index + step) & (symtab->data.size () - 1);
9291a0cd
TT
26025 }
26026}
26027
26028/* Expand SYMTAB's hash table. */
b89be57b 26029
9291a0cd
TT
26030static void
26031hash_expand (struct mapped_symtab *symtab)
26032{
bc8f2430 26033 auto old_entries = std::move (symtab->data);
9291a0cd 26034
bc8f2430
JK
26035 symtab->data.clear ();
26036 symtab->data.resize (old_entries.size () * 2);
9291a0cd 26037
bc8f2430 26038 for (auto &it : old_entries)
4b76cda9 26039 if (it.name != NULL)
bc8f2430 26040 {
4b76cda9 26041 auto &ref = find_slot (symtab, it.name);
bc8f2430
JK
26042 ref = std::move (it);
26043 }
9291a0cd
TT
26044}
26045
156942c7
DE
26046/* Add an entry to SYMTAB. NAME is the name of the symbol.
26047 CU_INDEX is the index of the CU in which the symbol appears.
26048 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 26049
9291a0cd
TT
26050static void
26051add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 26052 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
26053 offset_type cu_index)
26054{
156942c7 26055 offset_type cu_index_and_attrs;
9291a0cd
TT
26056
26057 ++symtab->n_elements;
bc8f2430 26058 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
9291a0cd
TT
26059 hash_expand (symtab);
26060
4b76cda9
PA
26061 symtab_index_entry &slot = find_slot (symtab, name);
26062 if (slot.name == NULL)
9291a0cd 26063 {
4b76cda9 26064 slot.name = name;
156942c7 26065 /* index_offset is set later. */
9291a0cd 26066 }
156942c7
DE
26067
26068 cu_index_and_attrs = 0;
26069 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
26070 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
26071 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
26072
26073 /* We don't want to record an index value twice as we want to avoid the
26074 duplication.
26075 We process all global symbols and then all static symbols
26076 (which would allow us to avoid the duplication by only having to check
26077 the last entry pushed), but a symbol could have multiple kinds in one CU.
26078 To keep things simple we don't worry about the duplication here and
26079 sort and uniqufy the list after we've processed all symbols. */
4b76cda9 26080 slot.cu_indices.push_back (cu_index_and_attrs);
156942c7
DE
26081}
26082
26083/* Sort and remove duplicates of all symbols' cu_indices lists. */
26084
26085static void
26086uniquify_cu_indices (struct mapped_symtab *symtab)
26087{
4b76cda9 26088 for (auto &entry : symtab->data)
156942c7 26089 {
4b76cda9 26090 if (entry.name != NULL && !entry.cu_indices.empty ())
156942c7 26091 {
4b76cda9 26092 auto &cu_indices = entry.cu_indices;
6fd931f2
PA
26093 std::sort (cu_indices.begin (), cu_indices.end ());
26094 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
26095 cu_indices.erase (from, cu_indices.end ());
156942c7
DE
26096 }
26097 }
9291a0cd
TT
26098}
26099
bc8f2430
JK
26100/* A form of 'const char *' suitable for container keys. Only the
26101 pointer is stored. The strings themselves are compared, not the
26102 pointers. */
26103class c_str_view
9291a0cd 26104{
bc8f2430
JK
26105public:
26106 c_str_view (const char *cstr)
26107 : m_cstr (cstr)
26108 {}
9291a0cd 26109
bc8f2430
JK
26110 bool operator== (const c_str_view &other) const
26111 {
26112 return strcmp (m_cstr, other.m_cstr) == 0;
26113 }
9291a0cd 26114
437afbb8
JK
26115 /* Return the underlying C string. Note, the returned string is
26116 only a reference with lifetime of this object. */
26117 const char *c_str () const
26118 {
26119 return m_cstr;
26120 }
26121
bc8f2430
JK
26122private:
26123 friend class c_str_view_hasher;
26124 const char *const m_cstr;
26125};
9291a0cd 26126
bc8f2430
JK
26127/* A std::unordered_map::hasher for c_str_view that uses the right
26128 hash function for strings in a mapped index. */
26129class c_str_view_hasher
26130{
26131public:
26132 size_t operator () (const c_str_view &x) const
26133 {
26134 return mapped_index_string_hash (INT_MAX, x.m_cstr);
26135 }
26136};
b89be57b 26137
bc8f2430
JK
26138/* A std::unordered_map::hasher for std::vector<>. */
26139template<typename T>
26140class vector_hasher
9291a0cd 26141{
bc8f2430
JK
26142public:
26143 size_t operator () (const std::vector<T> &key) const
26144 {
26145 return iterative_hash (key.data (),
26146 sizeof (key.front ()) * key.size (), 0);
26147 }
26148};
9291a0cd 26149
bc8f2430
JK
26150/* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
26151 constant pool entries going into the data buffer CPOOL. */
3876f04e 26152
bc8f2430
JK
26153static void
26154write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
26155{
26156 {
26157 /* Elements are sorted vectors of the indices of all the CUs that
26158 hold an object of this name. */
26159 std::unordered_map<std::vector<offset_type>, offset_type,
26160 vector_hasher<offset_type>>
26161 symbol_hash_table;
26162
26163 /* We add all the index vectors to the constant pool first, to
26164 ensure alignment is ok. */
4b76cda9 26165 for (symtab_index_entry &entry : symtab->data)
bc8f2430 26166 {
4b76cda9 26167 if (entry.name == NULL)
bc8f2430 26168 continue;
4b76cda9 26169 gdb_assert (entry.index_offset == 0);
70a1152b
PA
26170
26171 /* Finding before inserting is faster than always trying to
26172 insert, because inserting always allocates a node, does the
26173 lookup, and then destroys the new node if another node
26174 already had the same key. C++17 try_emplace will avoid
26175 this. */
26176 const auto found
4b76cda9 26177 = symbol_hash_table.find (entry.cu_indices);
70a1152b
PA
26178 if (found != symbol_hash_table.end ())
26179 {
4b76cda9 26180 entry.index_offset = found->second;
70a1152b
PA
26181 continue;
26182 }
26183
4b76cda9
PA
26184 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
26185 entry.index_offset = cpool.size ();
26186 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
26187 for (const auto index : entry.cu_indices)
26188 cpool.append_data (MAYBE_SWAP (index));
bc8f2430
JK
26189 }
26190 }
9291a0cd
TT
26191
26192 /* Now write out the hash table. */
bc8f2430 26193 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
4b76cda9 26194 for (const auto &entry : symtab->data)
9291a0cd
TT
26195 {
26196 offset_type str_off, vec_off;
26197
4b76cda9 26198 if (entry.name != NULL)
9291a0cd 26199 {
4b76cda9 26200 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
bc8f2430 26201 if (insertpair.second)
4b76cda9 26202 cpool.append_cstr0 (entry.name);
bc8f2430 26203 str_off = insertpair.first->second;
4b76cda9 26204 vec_off = entry.index_offset;
9291a0cd
TT
26205 }
26206 else
26207 {
26208 /* While 0 is a valid constant pool index, it is not valid
26209 to have 0 for both offsets. */
26210 str_off = 0;
26211 vec_off = 0;
26212 }
26213
bc8f2430
JK
26214 output.append_data (MAYBE_SWAP (str_off));
26215 output.append_data (MAYBE_SWAP (vec_off));
9291a0cd 26216 }
9291a0cd
TT
26217}
26218
bc8f2430 26219typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
0a5429f6
DE
26220
26221/* Helper struct for building the address table. */
26222struct addrmap_index_data
26223{
bc8f2430
JK
26224 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
26225 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
26226 {}
26227
0a5429f6 26228 struct objfile *objfile;
bc8f2430
JK
26229 data_buf &addr_vec;
26230 psym_index_map &cu_index_htab;
0a5429f6
DE
26231
26232 /* Non-zero if the previous_* fields are valid.
26233 We can't write an entry until we see the next entry (since it is only then
26234 that we know the end of the entry). */
26235 int previous_valid;
26236 /* Index of the CU in the table of all CUs in the index file. */
26237 unsigned int previous_cu_index;
0963b4bd 26238 /* Start address of the CU. */
0a5429f6
DE
26239 CORE_ADDR previous_cu_start;
26240};
26241
bc8f2430 26242/* Write an address entry to ADDR_VEC. */
b89be57b 26243
9291a0cd 26244static void
bc8f2430 26245add_address_entry (struct objfile *objfile, data_buf &addr_vec,
0a5429f6 26246 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 26247{
9291a0cd
TT
26248 CORE_ADDR baseaddr;
26249
26250 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
26251
c2f134ac
PA
26252 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
26253 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
bc8f2430 26254 addr_vec.append_data (MAYBE_SWAP (cu_index));
0a5429f6
DE
26255}
26256
26257/* Worker function for traversing an addrmap to build the address table. */
26258
26259static int
26260add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
26261{
9a3c8263
SM
26262 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
26263 struct partial_symtab *pst = (struct partial_symtab *) obj;
0a5429f6
DE
26264
26265 if (data->previous_valid)
bc8f2430 26266 add_address_entry (data->objfile, data->addr_vec,
0a5429f6
DE
26267 data->previous_cu_start, start_addr,
26268 data->previous_cu_index);
26269
26270 data->previous_cu_start = start_addr;
26271 if (pst != NULL)
26272 {
bc8f2430
JK
26273 const auto it = data->cu_index_htab.find (pst);
26274 gdb_assert (it != data->cu_index_htab.cend ());
26275 data->previous_cu_index = it->second;
0a5429f6
DE
26276 data->previous_valid = 1;
26277 }
26278 else
bc8f2430 26279 data->previous_valid = 0;
0a5429f6
DE
26280
26281 return 0;
26282}
26283
bc8f2430 26284/* Write OBJFILE's address map to ADDR_VEC.
0a5429f6
DE
26285 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
26286 in the index file. */
26287
26288static void
bc8f2430
JK
26289write_address_map (struct objfile *objfile, data_buf &addr_vec,
26290 psym_index_map &cu_index_htab)
0a5429f6 26291{
bc8f2430 26292 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
0a5429f6
DE
26293
26294 /* When writing the address table, we have to cope with the fact that
26295 the addrmap iterator only provides the start of a region; we have to
26296 wait until the next invocation to get the start of the next region. */
26297
26298 addrmap_index_data.objfile = objfile;
0a5429f6
DE
26299 addrmap_index_data.previous_valid = 0;
26300
26301 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
26302 &addrmap_index_data);
26303
26304 /* It's highly unlikely the last entry (end address = 0xff...ff)
26305 is valid, but we should still handle it.
26306 The end address is recorded as the start of the next region, but that
26307 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
26308 anyway. */
26309 if (addrmap_index_data.previous_valid)
bc8f2430 26310 add_address_entry (objfile, addr_vec,
0a5429f6
DE
26311 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
26312 addrmap_index_data.previous_cu_index);
9291a0cd
TT
26313}
26314
156942c7
DE
26315/* Return the symbol kind of PSYM. */
26316
26317static gdb_index_symbol_kind
26318symbol_kind (struct partial_symbol *psym)
26319{
26320 domain_enum domain = PSYMBOL_DOMAIN (psym);
26321 enum address_class aclass = PSYMBOL_CLASS (psym);
26322
26323 switch (domain)
26324 {
26325 case VAR_DOMAIN:
26326 switch (aclass)
26327 {
26328 case LOC_BLOCK:
26329 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
26330 case LOC_TYPEDEF:
26331 return GDB_INDEX_SYMBOL_KIND_TYPE;
26332 case LOC_COMPUTED:
26333 case LOC_CONST_BYTES:
26334 case LOC_OPTIMIZED_OUT:
26335 case LOC_STATIC:
26336 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
26337 case LOC_CONST:
26338 /* Note: It's currently impossible to recognize psyms as enum values
26339 short of reading the type info. For now punt. */
26340 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
26341 default:
26342 /* There are other LOC_FOO values that one might want to classify
26343 as variables, but dwarf2read.c doesn't currently use them. */
26344 return GDB_INDEX_SYMBOL_KIND_OTHER;
26345 }
26346 case STRUCT_DOMAIN:
26347 return GDB_INDEX_SYMBOL_KIND_TYPE;
26348 default:
26349 return GDB_INDEX_SYMBOL_KIND_OTHER;
26350 }
26351}
26352
9291a0cd 26353/* Add a list of partial symbols to SYMTAB. */
b89be57b 26354
9291a0cd
TT
26355static void
26356write_psymbols (struct mapped_symtab *symtab,
bc8f2430 26357 std::unordered_set<partial_symbol *> &psyms_seen,
9291a0cd
TT
26358 struct partial_symbol **psymp,
26359 int count,
987d643c
TT
26360 offset_type cu_index,
26361 int is_static)
9291a0cd
TT
26362{
26363 for (; count-- > 0; ++psymp)
26364 {
156942c7 26365 struct partial_symbol *psym = *psymp;
987d643c 26366
156942c7 26367 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 26368 error (_("Ada is not currently supported by the index"));
987d643c 26369
987d643c 26370 /* Only add a given psymbol once. */
bc8f2430 26371 if (psyms_seen.insert (psym).second)
987d643c 26372 {
156942c7
DE
26373 gdb_index_symbol_kind kind = symbol_kind (psym);
26374
156942c7
DE
26375 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
26376 is_static, kind, cu_index);
987d643c 26377 }
9291a0cd
TT
26378 }
26379}
26380
1fd400ff
TT
26381/* A helper struct used when iterating over debug_types. */
26382struct signatured_type_index_data
26383{
bc8f2430
JK
26384 signatured_type_index_data (data_buf &types_list_,
26385 std::unordered_set<partial_symbol *> &psyms_seen_)
26386 : types_list (types_list_), psyms_seen (psyms_seen_)
26387 {}
26388
1fd400ff
TT
26389 struct objfile *objfile;
26390 struct mapped_symtab *symtab;
bc8f2430
JK
26391 data_buf &types_list;
26392 std::unordered_set<partial_symbol *> &psyms_seen;
1fd400ff
TT
26393 int cu_index;
26394};
26395
26396/* A helper function that writes a single signatured_type to an
26397 obstack. */
b89be57b 26398
1fd400ff
TT
26399static int
26400write_one_signatured_type (void **slot, void *d)
26401{
9a3c8263
SM
26402 struct signatured_type_index_data *info
26403 = (struct signatured_type_index_data *) d;
1fd400ff 26404 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 26405 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
26406
26407 write_psymbols (info->symtab,
987d643c 26408 info->psyms_seen,
af5bf4ad 26409 &info->objfile->global_psymbols[psymtab->globals_offset],
987d643c
TT
26410 psymtab->n_global_syms, info->cu_index,
26411 0);
1fd400ff 26412 write_psymbols (info->symtab,
987d643c 26413 info->psyms_seen,
af5bf4ad 26414 &info->objfile->static_psymbols[psymtab->statics_offset],
987d643c
TT
26415 psymtab->n_static_syms, info->cu_index,
26416 1);
1fd400ff 26417
c2f134ac
PA
26418 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26419 to_underlying (entry->per_cu.sect_off));
26420 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26421 to_underlying (entry->type_offset_in_tu));
26422 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
1fd400ff
TT
26423
26424 ++info->cu_index;
26425
26426 return 1;
26427}
26428
e8f8bcb3
PA
26429/* Recurse into all "included" dependencies and count their symbols as
26430 if they appeared in this psymtab. */
26431
26432static void
26433recursively_count_psymbols (struct partial_symtab *psymtab,
26434 size_t &psyms_seen)
26435{
26436 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26437 if (psymtab->dependencies[i]->user != NULL)
26438 recursively_count_psymbols (psymtab->dependencies[i],
26439 psyms_seen);
26440
26441 psyms_seen += psymtab->n_global_syms;
26442 psyms_seen += psymtab->n_static_syms;
26443}
26444
95554aad
TT
26445/* Recurse into all "included" dependencies and write their symbols as
26446 if they appeared in this psymtab. */
26447
26448static void
26449recursively_write_psymbols (struct objfile *objfile,
26450 struct partial_symtab *psymtab,
26451 struct mapped_symtab *symtab,
bc8f2430 26452 std::unordered_set<partial_symbol *> &psyms_seen,
95554aad
TT
26453 offset_type cu_index)
26454{
26455 int i;
26456
26457 for (i = 0; i < psymtab->number_of_dependencies; ++i)
26458 if (psymtab->dependencies[i]->user != NULL)
26459 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26460 symtab, psyms_seen, cu_index);
26461
26462 write_psymbols (symtab,
26463 psyms_seen,
af5bf4ad 26464 &objfile->global_psymbols[psymtab->globals_offset],
95554aad
TT
26465 psymtab->n_global_syms, cu_index,
26466 0);
26467 write_psymbols (symtab,
26468 psyms_seen,
af5bf4ad 26469 &objfile->static_psymbols[psymtab->statics_offset],
95554aad
TT
26470 psymtab->n_static_syms, cu_index,
26471 1);
26472}
26473
437afbb8
JK
26474/* DWARF-5 .debug_names builder. */
26475class debug_names
26476{
26477public:
ed2dc618
SM
26478 debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile, bool is_dwarf64,
26479 bfd_endian dwarf5_byte_order)
437afbb8
JK
26480 : m_dwarf5_byte_order (dwarf5_byte_order),
26481 m_dwarf32 (dwarf5_byte_order),
26482 m_dwarf64 (dwarf5_byte_order),
26483 m_dwarf (is_dwarf64
26484 ? static_cast<dwarf &> (m_dwarf64)
26485 : static_cast<dwarf &> (m_dwarf32)),
26486 m_name_table_string_offs (m_dwarf.name_table_string_offs),
ed2dc618
SM
26487 m_name_table_entry_offs (m_dwarf.name_table_entry_offs),
26488 m_debugstrlookup (dwarf2_per_objfile)
437afbb8 26489 {}
9291a0cd 26490
8af5c486
JK
26491 int dwarf5_offset_size () const
26492 {
26493 const bool dwarf5_is_dwarf64 = &m_dwarf == &m_dwarf64;
26494 return dwarf5_is_dwarf64 ? 8 : 4;
26495 }
26496
26497 /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit? */
26498 enum class unit_kind { cu, tu };
26499
437afbb8 26500 /* Insert one symbol. */
8af5c486
JK
26501 void insert (const partial_symbol *psym, int cu_index, bool is_static,
26502 unit_kind kind)
437afbb8
JK
26503 {
26504 const int dwarf_tag = psymbol_tag (psym);
26505 if (dwarf_tag == 0)
26506 return;
26507 const char *const name = SYMBOL_SEARCH_NAME (psym);
26508 const auto insertpair
26509 = m_name_to_value_set.emplace (c_str_view (name),
26510 std::set<symbol_value> ());
26511 std::set<symbol_value> &value_set = insertpair.first->second;
8af5c486 26512 value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static, kind));
437afbb8 26513 }
9291a0cd 26514
437afbb8
JK
26515 /* Build all the tables. All symbols must be already inserted.
26516 This function does not call file_write, caller has to do it
26517 afterwards. */
26518 void build ()
26519 {
26520 /* Verify the build method has not be called twice. */
26521 gdb_assert (m_abbrev_table.empty ());
26522 const size_t name_count = m_name_to_value_set.size ();
26523 m_bucket_table.resize
26524 (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3))));
26525 m_hash_table.reserve (name_count);
26526 m_name_table_string_offs.reserve (name_count);
26527 m_name_table_entry_offs.reserve (name_count);
26528
26529 /* Map each hash of symbol to its name and value. */
26530 struct hash_it_pair
26531 {
26532 uint32_t hash;
26533 decltype (m_name_to_value_set)::const_iterator it;
26534 };
26535 std::vector<std::forward_list<hash_it_pair>> bucket_hash;
26536 bucket_hash.resize (m_bucket_table.size ());
26537 for (decltype (m_name_to_value_set)::const_iterator it
26538 = m_name_to_value_set.cbegin ();
26539 it != m_name_to_value_set.cend ();
26540 ++it)
26541 {
26542 const char *const name = it->first.c_str ();
26543 const uint32_t hash = dwarf5_djb_hash (name);
26544 hash_it_pair hashitpair;
26545 hashitpair.hash = hash;
26546 hashitpair.it = it;
26547 auto &slot = bucket_hash[hash % bucket_hash.size()];
26548 slot.push_front (std::move (hashitpair));
26549 }
26550 for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix)
26551 {
26552 const std::forward_list<hash_it_pair> &hashitlist
26553 = bucket_hash[bucket_ix];
26554 if (hashitlist.empty ())
26555 continue;
26556 uint32_t &bucket_slot = m_bucket_table[bucket_ix];
26557 /* The hashes array is indexed starting at 1. */
26558 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot),
26559 sizeof (bucket_slot), m_dwarf5_byte_order,
26560 m_hash_table.size () + 1);
26561 for (const hash_it_pair &hashitpair : hashitlist)
26562 {
26563 m_hash_table.push_back (0);
26564 store_unsigned_integer (reinterpret_cast<gdb_byte *>
26565 (&m_hash_table.back ()),
26566 sizeof (m_hash_table.back ()),
26567 m_dwarf5_byte_order, hashitpair.hash);
26568 const c_str_view &name = hashitpair.it->first;
26569 const std::set<symbol_value> &value_set = hashitpair.it->second;
26570 m_name_table_string_offs.push_back_reorder
26571 (m_debugstrlookup.lookup (name.c_str ()));
26572 m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
26573 gdb_assert (!value_set.empty ());
26574 for (const symbol_value &value : value_set)
26575 {
26576 int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag,
8af5c486
JK
26577 value.is_static,
26578 value.kind)];
437afbb8
JK
26579 if (idx == 0)
26580 {
26581 idx = m_idx_next++;
26582 m_abbrev_table.append_unsigned_leb128 (idx);
26583 m_abbrev_table.append_unsigned_leb128 (value.dwarf_tag);
8af5c486
JK
26584 m_abbrev_table.append_unsigned_leb128
26585 (value.kind == unit_kind::cu ? DW_IDX_compile_unit
26586 : DW_IDX_type_unit);
437afbb8
JK
26587 m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
26588 m_abbrev_table.append_unsigned_leb128 (value.is_static
26589 ? DW_IDX_GNU_internal
26590 : DW_IDX_GNU_external);
26591 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
26592
26593 /* Terminate attributes list. */
26594 m_abbrev_table.append_unsigned_leb128 (0);
26595 m_abbrev_table.append_unsigned_leb128 (0);
26596 }
9291a0cd 26597
437afbb8
JK
26598 m_entry_pool.append_unsigned_leb128 (idx);
26599 m_entry_pool.append_unsigned_leb128 (value.cu_index);
26600 }
9291a0cd 26601
437afbb8
JK
26602 /* Terminate the list of CUs. */
26603 m_entry_pool.append_unsigned_leb128 (0);
26604 }
26605 }
26606 gdb_assert (m_hash_table.size () == name_count);
987d643c 26607
437afbb8
JK
26608 /* Terminate tags list. */
26609 m_abbrev_table.append_unsigned_leb128 (0);
26610 }
0a5429f6 26611
437afbb8
JK
26612 /* Return .debug_names bucket count. This must be called only after
26613 calling the build method. */
26614 uint32_t bucket_count () const
26615 {
26616 /* Verify the build method has been already called. */
26617 gdb_assert (!m_abbrev_table.empty ());
26618 const uint32_t retval = m_bucket_table.size ();
26619
26620 /* Check for overflow. */
26621 gdb_assert (retval == m_bucket_table.size ());
26622 return retval;
26623 }
26624
26625 /* Return .debug_names names count. This must be called only after
26626 calling the build method. */
26627 uint32_t name_count () const
26628 {
26629 /* Verify the build method has been already called. */
26630 gdb_assert (!m_abbrev_table.empty ());
26631 const uint32_t retval = m_hash_table.size ();
26632
26633 /* Check for overflow. */
26634 gdb_assert (retval == m_hash_table.size ());
26635 return retval;
26636 }
26637
26638 /* Return number of bytes of .debug_names abbreviation table. This
26639 must be called only after calling the build method. */
26640 uint32_t abbrev_table_bytes () const
26641 {
26642 gdb_assert (!m_abbrev_table.empty ());
26643 return m_abbrev_table.size ();
26644 }
26645
26646 /* Recurse into all "included" dependencies and store their symbols
26647 as if they appeared in this psymtab. */
26648 void recursively_write_psymbols
26649 (struct objfile *objfile,
26650 struct partial_symtab *psymtab,
26651 std::unordered_set<partial_symbol *> &psyms_seen,
26652 int cu_index)
26653 {
26654 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26655 if (psymtab->dependencies[i]->user != NULL)
26656 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26657 psyms_seen, cu_index);
26658
26659 write_psymbols (psyms_seen,
26660 &objfile->global_psymbols[psymtab->globals_offset],
8af5c486 26661 psymtab->n_global_syms, cu_index, false, unit_kind::cu);
437afbb8
JK
26662 write_psymbols (psyms_seen,
26663 &objfile->static_psymbols[psymtab->statics_offset],
8af5c486 26664 psymtab->n_static_syms, cu_index, true, unit_kind::cu);
437afbb8
JK
26665 }
26666
26667 /* Return number of bytes the .debug_names section will have. This
26668 must be called only after calling the build method. */
26669 size_t bytes () const
26670 {
26671 /* Verify the build method has been already called. */
26672 gdb_assert (!m_abbrev_table.empty ());
26673 size_t expected_bytes = 0;
26674 expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]);
26675 expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]);
26676 expected_bytes += m_name_table_string_offs.bytes ();
26677 expected_bytes += m_name_table_entry_offs.bytes ();
26678 expected_bytes += m_abbrev_table.size ();
26679 expected_bytes += m_entry_pool.size ();
26680 return expected_bytes;
26681 }
26682
26683 /* Write .debug_names to FILE_NAMES and .debug_str addition to
26684 FILE_STR. This must be called only after calling the build
26685 method. */
26686 void file_write (FILE *file_names, FILE *file_str) const
26687 {
26688 /* Verify the build method has been already called. */
26689 gdb_assert (!m_abbrev_table.empty ());
26690 ::file_write (file_names, m_bucket_table);
26691 ::file_write (file_names, m_hash_table);
26692 m_name_table_string_offs.file_write (file_names);
26693 m_name_table_entry_offs.file_write (file_names);
26694 m_abbrev_table.file_write (file_names);
26695 m_entry_pool.file_write (file_names);
26696 m_debugstrlookup.file_write (file_str);
26697 }
26698
8af5c486
JK
26699 /* A helper user data for write_one_signatured_type. */
26700 class write_one_signatured_type_data
26701 {
26702 public:
26703 write_one_signatured_type_data (debug_names &nametable_,
26704 signatured_type_index_data &&info_)
26705 : nametable (nametable_), info (std::move (info_))
26706 {}
26707 debug_names &nametable;
26708 struct signatured_type_index_data info;
26709 };
26710
26711 /* A helper function to pass write_one_signatured_type to
26712 htab_traverse_noresize. */
26713 static int
26714 write_one_signatured_type (void **slot, void *d)
26715 {
26716 write_one_signatured_type_data *data = (write_one_signatured_type_data *) d;
26717 struct signatured_type_index_data *info = &data->info;
26718 struct signatured_type *entry = (struct signatured_type *) *slot;
26719
26720 data->nametable.write_one_signatured_type (entry, info);
26721
26722 return 1;
26723 }
26724
437afbb8
JK
26725private:
26726
26727 /* Storage for symbol names mapping them to their .debug_str section
26728 offsets. */
26729 class debug_str_lookup
26730 {
26731 public:
26732
26733 /* Object costructor to be called for current DWARF2_PER_OBJFILE.
26734 All .debug_str section strings are automatically stored. */
ed2dc618
SM
26735 debug_str_lookup (struct dwarf2_per_objfile *dwarf2_per_objfile)
26736 : m_abfd (dwarf2_per_objfile->objfile->obfd),
26737 m_dwarf2_per_objfile (dwarf2_per_objfile)
437afbb8
JK
26738 {
26739 dwarf2_read_section (dwarf2_per_objfile->objfile,
26740 &dwarf2_per_objfile->str);
26741 if (dwarf2_per_objfile->str.buffer == NULL)
26742 return;
26743 for (const gdb_byte *data = dwarf2_per_objfile->str.buffer;
26744 data < (dwarf2_per_objfile->str.buffer
26745 + dwarf2_per_objfile->str.size);)
26746 {
26747 const char *const s = reinterpret_cast<const char *> (data);
26748 const auto insertpair
26749 = m_str_table.emplace (c_str_view (s),
26750 data - dwarf2_per_objfile->str.buffer);
26751 if (!insertpair.second)
26752 complaint (&symfile_complaints,
26753 _("Duplicate string \"%s\" in "
26754 ".debug_str section [in module %s]"),
26755 s, bfd_get_filename (m_abfd));
26756 data += strlen (s) + 1;
26757 }
26758 }
26759
26760 /* Return offset of symbol name S in the .debug_str section. Add
26761 such symbol to the section's end if it does not exist there
26762 yet. */
26763 size_t lookup (const char *s)
26764 {
26765 const auto it = m_str_table.find (c_str_view (s));
26766 if (it != m_str_table.end ())
26767 return it->second;
ed2dc618 26768 const size_t offset = (m_dwarf2_per_objfile->str.size
437afbb8
JK
26769 + m_str_add_buf.size ());
26770 m_str_table.emplace (c_str_view (s), offset);
26771 m_str_add_buf.append_cstr0 (s);
26772 return offset;
26773 }
26774
26775 /* Append the end of the .debug_str section to FILE. */
26776 void file_write (FILE *file) const
26777 {
26778 m_str_add_buf.file_write (file);
26779 }
26780
26781 private:
26782 std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
26783 bfd *const m_abfd;
ed2dc618 26784 struct dwarf2_per_objfile *m_dwarf2_per_objfile;
437afbb8
JK
26785
26786 /* Data to add at the end of .debug_str for new needed symbol names. */
26787 data_buf m_str_add_buf;
26788 };
26789
26790 /* Container to map used DWARF tags to their .debug_names abbreviation
26791 tags. */
26792 class index_key
26793 {
26794 public:
8af5c486
JK
26795 index_key (int dwarf_tag_, bool is_static_, unit_kind kind_)
26796 : dwarf_tag (dwarf_tag_), is_static (is_static_), kind (kind_)
437afbb8
JK
26797 {
26798 }
26799
26800 bool
26801 operator== (const index_key &other) const
26802 {
8af5c486
JK
26803 return (dwarf_tag == other.dwarf_tag && is_static == other.is_static
26804 && kind == other.kind);
437afbb8
JK
26805 }
26806
26807 const int dwarf_tag;
26808 const bool is_static;
8af5c486 26809 const unit_kind kind;
437afbb8
JK
26810 };
26811
26812 /* Provide std::unordered_map::hasher for index_key. */
26813 class index_key_hasher
26814 {
26815 public:
26816 size_t
26817 operator () (const index_key &key) const
26818 {
26819 return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static;
26820 }
26821 };
26822
26823 /* Parameters of one symbol entry. */
26824 class symbol_value
26825 {
26826 public:
26827 const int dwarf_tag, cu_index;
26828 const bool is_static;
8af5c486 26829 const unit_kind kind;
437afbb8 26830
8af5c486
JK
26831 symbol_value (int dwarf_tag_, int cu_index_, bool is_static_,
26832 unit_kind kind_)
26833 : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_),
26834 kind (kind_)
437afbb8
JK
26835 {}
26836
26837 bool
26838 operator< (const symbol_value &other) const
26839 {
26840#define X(n) \
26841 do \
26842 { \
26843 if (n < other.n) \
26844 return true; \
26845 if (n > other.n) \
26846 return false; \
26847 } \
26848 while (0)
26849 X (dwarf_tag);
26850 X (is_static);
8af5c486 26851 X (kind);
437afbb8
JK
26852 X (cu_index);
26853#undef X
26854 return false;
26855 }
26856 };
26857
26858 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
26859 output. */
26860 class offset_vec
26861 {
26862 protected:
26863 const bfd_endian dwarf5_byte_order;
26864 public:
26865 explicit offset_vec (bfd_endian dwarf5_byte_order_)
26866 : dwarf5_byte_order (dwarf5_byte_order_)
26867 {}
26868
26869 /* Call std::vector::reserve for NELEM elements. */
26870 virtual void reserve (size_t nelem) = 0;
26871
26872 /* Call std::vector::push_back with store_unsigned_integer byte
26873 reordering for ELEM. */
26874 virtual void push_back_reorder (size_t elem) = 0;
26875
26876 /* Return expected output size in bytes. */
26877 virtual size_t bytes () const = 0;
26878
26879 /* Write name table to FILE. */
26880 virtual void file_write (FILE *file) const = 0;
26881 };
26882
26883 /* Template to unify DWARF-32 and DWARF-64 output. */
26884 template<typename OffsetSize>
26885 class offset_vec_tmpl : public offset_vec
26886 {
26887 public:
26888 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
26889 : offset_vec (dwarf5_byte_order_)
26890 {}
26891
26892 /* Implement offset_vec::reserve. */
26893 void reserve (size_t nelem) override
26894 {
26895 m_vec.reserve (nelem);
26896 }
26897
26898 /* Implement offset_vec::push_back_reorder. */
26899 void push_back_reorder (size_t elem) override
26900 {
26901 m_vec.push_back (elem);
26902 /* Check for overflow. */
26903 gdb_assert (m_vec.back () == elem);
26904 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
26905 sizeof (m_vec.back ()), dwarf5_byte_order, elem);
26906 }
26907
26908 /* Implement offset_vec::bytes. */
26909 size_t bytes () const override
26910 {
26911 return m_vec.size () * sizeof (m_vec[0]);
26912 }
26913
26914 /* Implement offset_vec::file_write. */
26915 void file_write (FILE *file) const override
26916 {
26917 ::file_write (file, m_vec);
26918 }
26919
26920 private:
26921 std::vector<OffsetSize> m_vec;
26922 };
26923
26924 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
26925 respecting name table width. */
26926 class dwarf
26927 {
26928 public:
26929 offset_vec &name_table_string_offs, &name_table_entry_offs;
26930
26931 dwarf (offset_vec &name_table_string_offs_,
26932 offset_vec &name_table_entry_offs_)
26933 : name_table_string_offs (name_table_string_offs_),
26934 name_table_entry_offs (name_table_entry_offs_)
26935 {
26936 }
26937 };
e8f8bcb3 26938
437afbb8
JK
26939 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
26940 respecting name table width. */
26941 template<typename OffsetSize>
26942 class dwarf_tmpl : public dwarf
26943 {
26944 public:
26945 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
26946 : dwarf (m_name_table_string_offs, m_name_table_entry_offs),
26947 m_name_table_string_offs (dwarf5_byte_order_),
26948 m_name_table_entry_offs (dwarf5_byte_order_)
26949 {}
26950
26951 private:
26952 offset_vec_tmpl<OffsetSize> m_name_table_string_offs;
26953 offset_vec_tmpl<OffsetSize> m_name_table_entry_offs;
26954 };
26955
26956 /* Try to reconstruct original DWARF tag for given partial_symbol.
26957 This function is not DWARF-5 compliant but it is sufficient for
26958 GDB as a DWARF-5 index consumer. */
26959 static int psymbol_tag (const struct partial_symbol *psym)
26960 {
26961 domain_enum domain = PSYMBOL_DOMAIN (psym);
26962 enum address_class aclass = PSYMBOL_CLASS (psym);
26963
26964 switch (domain)
26965 {
26966 case VAR_DOMAIN:
26967 switch (aclass)
26968 {
26969 case LOC_BLOCK:
26970 return DW_TAG_subprogram;
26971 case LOC_TYPEDEF:
26972 return DW_TAG_typedef;
26973 case LOC_COMPUTED:
26974 case LOC_CONST_BYTES:
26975 case LOC_OPTIMIZED_OUT:
26976 case LOC_STATIC:
26977 return DW_TAG_variable;
26978 case LOC_CONST:
26979 /* Note: It's currently impossible to recognize psyms as enum values
26980 short of reading the type info. For now punt. */
26981 return DW_TAG_variable;
26982 default:
26983 /* There are other LOC_FOO values that one might want to classify
26984 as variables, but dwarf2read.c doesn't currently use them. */
26985 return DW_TAG_variable;
26986 }
26987 case STRUCT_DOMAIN:
26988 return DW_TAG_structure_type;
26989 default:
26990 return 0;
26991 }
26992 }
26993
26994 /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */
26995 void write_psymbols (std::unordered_set<partial_symbol *> &psyms_seen,
26996 struct partial_symbol **psymp, int count, int cu_index,
8af5c486 26997 bool is_static, unit_kind kind)
437afbb8
JK
26998 {
26999 for (; count-- > 0; ++psymp)
27000 {
27001 struct partial_symbol *psym = *psymp;
27002
27003 if (SYMBOL_LANGUAGE (psym) == language_ada)
27004 error (_("Ada is not currently supported by the index"));
27005
27006 /* Only add a given psymbol once. */
27007 if (psyms_seen.insert (psym).second)
8af5c486 27008 insert (psym, cu_index, is_static, kind);
437afbb8
JK
27009 }
27010 }
27011
8af5c486
JK
27012 /* A helper function that writes a single signatured_type
27013 to a debug_names. */
27014 void
27015 write_one_signatured_type (struct signatured_type *entry,
27016 struct signatured_type_index_data *info)
27017 {
27018 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
27019
27020 write_psymbols (info->psyms_seen,
27021 &info->objfile->global_psymbols[psymtab->globals_offset],
27022 psymtab->n_global_syms, info->cu_index, false,
27023 unit_kind::tu);
27024 write_psymbols (info->psyms_seen,
27025 &info->objfile->static_psymbols[psymtab->statics_offset],
27026 psymtab->n_static_syms, info->cu_index, true,
27027 unit_kind::tu);
27028
27029 info->types_list.append_uint (dwarf5_offset_size (), m_dwarf5_byte_order,
27030 to_underlying (entry->per_cu.sect_off));
27031
27032 ++info->cu_index;
27033 }
27034
437afbb8
JK
27035 /* Store value of each symbol. */
27036 std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher>
27037 m_name_to_value_set;
27038
27039 /* Tables of DWARF-5 .debug_names. They are in object file byte
27040 order. */
27041 std::vector<uint32_t> m_bucket_table;
27042 std::vector<uint32_t> m_hash_table;
27043
27044 const bfd_endian m_dwarf5_byte_order;
27045 dwarf_tmpl<uint32_t> m_dwarf32;
27046 dwarf_tmpl<uint64_t> m_dwarf64;
27047 dwarf &m_dwarf;
27048 offset_vec &m_name_table_string_offs, &m_name_table_entry_offs;
27049 debug_str_lookup m_debugstrlookup;
27050
27051 /* Map each used .debug_names abbreviation tag parameter to its
27052 index value. */
27053 std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
27054
27055 /* Next unused .debug_names abbreviation tag for
27056 m_indexkey_to_idx. */
27057 int m_idx_next = 1;
27058
27059 /* .debug_names abbreviation table. */
27060 data_buf m_abbrev_table;
27061
27062 /* .debug_names entry pool. */
27063 data_buf m_entry_pool;
27064};
27065
27066/* Return iff any of the needed offsets does not fit into 32-bit
27067 .debug_names section. */
27068
27069static bool
ed2dc618 27070check_dwarf64_offsets (struct dwarf2_per_objfile *dwarf2_per_objfile)
437afbb8
JK
27071{
27072 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27073 {
27074 const dwarf2_per_cu_data &per_cu = *dwarf2_per_objfile->all_comp_units[i];
27075
27076 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
27077 return true;
27078 }
27079 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
27080 {
27081 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
27082 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
27083
27084 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
27085 return true;
27086 }
27087 return false;
27088}
27089
27090/* The psyms_seen set is potentially going to be largish (~40k
27091 elements when indexing a -g3 build of GDB itself). Estimate the
27092 number of elements in order to avoid too many rehashes, which
27093 require rebuilding buckets and thus many trips to
27094 malloc/free. */
27095
27096static size_t
ed2dc618 27097psyms_seen_size (struct dwarf2_per_objfile *dwarf2_per_objfile)
437afbb8 27098{
e8f8bcb3
PA
27099 size_t psyms_count = 0;
27100 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27101 {
27102 struct dwarf2_per_cu_data *per_cu
27103 = dwarf2_per_objfile->all_comp_units[i];
27104 struct partial_symtab *psymtab = per_cu->v.psymtab;
27105
27106 if (psymtab != NULL && psymtab->user == NULL)
27107 recursively_count_psymbols (psymtab, psyms_count);
27108 }
27109 /* Generating an index for gdb itself shows a ratio of
27110 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
437afbb8
JK
27111 return psyms_count / 4;
27112}
27113
27114/* Write new .gdb_index section for OBJFILE into OUT_FILE.
27115 Return how many bytes were expected to be written into OUT_FILE. */
27116
27117static size_t
ed2dc618 27118write_gdbindex (struct dwarf2_per_objfile *dwarf2_per_objfile, FILE *out_file)
437afbb8 27119{
ed2dc618 27120 struct objfile *objfile = dwarf2_per_objfile->objfile;
437afbb8
JK
27121 mapped_symtab symtab;
27122 data_buf cu_list;
27123
27124 /* While we're scanning CU's create a table that maps a psymtab pointer
27125 (which is what addrmap records) to its index (which is what is recorded
27126 in the index file). This will later be needed to write the address
27127 table. */
27128 psym_index_map cu_index_htab;
27129 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
27130
27131 /* The CU list is already sorted, so we don't need to do additional
27132 work here. Also, the debug_types entries do not appear in
27133 all_comp_units, but only in their own hash table. */
27134
ed2dc618
SM
27135 std::unordered_set<partial_symbol *> psyms_seen
27136 (psyms_seen_size (dwarf2_per_objfile));
bc8f2430 27137 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd 27138 {
3e43a32a
MS
27139 struct dwarf2_per_cu_data *per_cu
27140 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 27141 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 27142
92fac807
JK
27143 /* CU of a shared file from 'dwz -m' may be unused by this main file.
27144 It may be referenced from a local scope but in such case it does not
27145 need to be present in .gdb_index. */
27146 if (psymtab == NULL)
27147 continue;
27148
95554aad 27149 if (psymtab->user == NULL)
bc8f2430
JK
27150 recursively_write_psymbols (objfile, psymtab, &symtab,
27151 psyms_seen, i);
9291a0cd 27152
bc8f2430
JK
27153 const auto insertpair = cu_index_htab.emplace (psymtab, i);
27154 gdb_assert (insertpair.second);
9291a0cd 27155
c2f134ac
PA
27156 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
27157 to_underlying (per_cu->sect_off));
27158 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
27159 }
27160
0a5429f6 27161 /* Dump the address map. */
bc8f2430
JK
27162 data_buf addr_vec;
27163 write_address_map (objfile, addr_vec, cu_index_htab);
0a5429f6 27164
1fd400ff 27165 /* Write out the .debug_type entries, if any. */
bc8f2430 27166 data_buf types_cu_list;
1fd400ff
TT
27167 if (dwarf2_per_objfile->signatured_types)
27168 {
bc8f2430
JK
27169 signatured_type_index_data sig_data (types_cu_list,
27170 psyms_seen);
1fd400ff
TT
27171
27172 sig_data.objfile = objfile;
bc8f2430 27173 sig_data.symtab = &symtab;
1fd400ff
TT
27174 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
27175 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
27176 write_one_signatured_type, &sig_data);
27177 }
27178
156942c7
DE
27179 /* Now that we've processed all symbols we can shrink their cu_indices
27180 lists. */
bc8f2430 27181 uniquify_cu_indices (&symtab);
156942c7 27182
bc8f2430
JK
27183 data_buf symtab_vec, constant_pool;
27184 write_hash_table (&symtab, symtab_vec, constant_pool);
9291a0cd 27185
bc8f2430
JK
27186 data_buf contents;
27187 const offset_type size_of_contents = 6 * sizeof (offset_type);
27188 offset_type total_len = size_of_contents;
9291a0cd
TT
27189
27190 /* The version number. */
bc8f2430 27191 contents.append_data (MAYBE_SWAP (8));
9291a0cd
TT
27192
27193 /* The offset of the CU list from the start of the file. */
bc8f2430
JK
27194 contents.append_data (MAYBE_SWAP (total_len));
27195 total_len += cu_list.size ();
9291a0cd 27196
1fd400ff 27197 /* The offset of the types CU list from the start of the file. */
bc8f2430
JK
27198 contents.append_data (MAYBE_SWAP (total_len));
27199 total_len += types_cu_list.size ();
1fd400ff 27200
9291a0cd 27201 /* The offset of the address table from the start of the file. */
bc8f2430
JK
27202 contents.append_data (MAYBE_SWAP (total_len));
27203 total_len += addr_vec.size ();
9291a0cd
TT
27204
27205 /* The offset of the symbol table from the start of the file. */
bc8f2430
JK
27206 contents.append_data (MAYBE_SWAP (total_len));
27207 total_len += symtab_vec.size ();
9291a0cd
TT
27208
27209 /* The offset of the constant pool from the start of the file. */
bc8f2430
JK
27210 contents.append_data (MAYBE_SWAP (total_len));
27211 total_len += constant_pool.size ();
9291a0cd 27212
bc8f2430 27213 gdb_assert (contents.size () == size_of_contents);
9291a0cd 27214
bc8f2430
JK
27215 contents.file_write (out_file);
27216 cu_list.file_write (out_file);
27217 types_cu_list.file_write (out_file);
27218 addr_vec.file_write (out_file);
27219 symtab_vec.file_write (out_file);
27220 constant_pool.file_write (out_file);
9291a0cd 27221
437afbb8
JK
27222 return total_len;
27223}
27224
27225/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
27226static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
27227
27228/* Write a new .debug_names section for OBJFILE into OUT_FILE, write
27229 needed addition to .debug_str section to OUT_FILE_STR. Return how
27230 many bytes were expected to be written into OUT_FILE. */
27231
27232static size_t
ed2dc618
SM
27233write_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
27234 FILE *out_file, FILE *out_file_str)
437afbb8 27235{
ed2dc618
SM
27236 const bool dwarf5_is_dwarf64 = check_dwarf64_offsets (dwarf2_per_objfile);
27237 struct objfile *objfile = dwarf2_per_objfile->objfile;
437afbb8
JK
27238 const enum bfd_endian dwarf5_byte_order
27239 = gdbarch_byte_order (get_objfile_arch (objfile));
27240
27241 /* The CU list is already sorted, so we don't need to do additional
27242 work here. Also, the debug_types entries do not appear in
27243 all_comp_units, but only in their own hash table. */
27244 data_buf cu_list;
ed2dc618
SM
27245 debug_names nametable (dwarf2_per_objfile, dwarf5_is_dwarf64,
27246 dwarf5_byte_order);
27247 std::unordered_set<partial_symbol *>
27248 psyms_seen (psyms_seen_size (dwarf2_per_objfile));
437afbb8
JK
27249 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27250 {
27251 const dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
27252 partial_symtab *psymtab = per_cu->v.psymtab;
27253
27254 /* CU of a shared file from 'dwz -m' may be unused by this main
27255 file. It may be referenced from a local scope but in such
27256 case it does not need to be present in .debug_names. */
27257 if (psymtab == NULL)
27258 continue;
27259
27260 if (psymtab->user == NULL)
27261 nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i);
27262
8af5c486 27263 cu_list.append_uint (nametable.dwarf5_offset_size (), dwarf5_byte_order,
437afbb8
JK
27264 to_underlying (per_cu->sect_off));
27265 }
437afbb8 27266
8af5c486 27267 /* Write out the .debug_type entries, if any. */
437afbb8 27268 data_buf types_cu_list;
8af5c486 27269 if (dwarf2_per_objfile->signatured_types)
437afbb8 27270 {
8af5c486
JK
27271 debug_names::write_one_signatured_type_data sig_data (nametable,
27272 signatured_type_index_data (types_cu_list, psyms_seen));
437afbb8 27273
8af5c486
JK
27274 sig_data.info.objfile = objfile;
27275 /* It is used only for gdb_index. */
27276 sig_data.info.symtab = nullptr;
27277 sig_data.info.cu_index = 0;
27278 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
27279 debug_names::write_one_signatured_type,
27280 &sig_data);
437afbb8
JK
27281 }
27282
8af5c486
JK
27283 nametable.build ();
27284
27285 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
27286
437afbb8
JK
27287 const offset_type bytes_of_header
27288 = ((dwarf5_is_dwarf64 ? 12 : 4)
27289 + 2 + 2 + 7 * 4
27290 + sizeof (dwarf5_gdb_augmentation));
27291 size_t expected_bytes = 0;
27292 expected_bytes += bytes_of_header;
27293 expected_bytes += cu_list.size ();
27294 expected_bytes += types_cu_list.size ();
27295 expected_bytes += nametable.bytes ();
27296 data_buf header;
27297
27298 if (!dwarf5_is_dwarf64)
27299 {
27300 const uint64_t size64 = expected_bytes - 4;
27301 gdb_assert (size64 < 0xfffffff0);
27302 header.append_uint (4, dwarf5_byte_order, size64);
27303 }
27304 else
27305 {
27306 header.append_uint (4, dwarf5_byte_order, 0xffffffff);
27307 header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
27308 }
27309
27310 /* The version number. */
27311 header.append_uint (2, dwarf5_byte_order, 5);
27312
27313 /* Padding. */
27314 header.append_uint (2, dwarf5_byte_order, 0);
27315
27316 /* comp_unit_count - The number of CUs in the CU list. */
27317 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_comp_units);
27318
27319 /* local_type_unit_count - The number of TUs in the local TU
27320 list. */
27321 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_type_units);
27322
27323 /* foreign_type_unit_count - The number of TUs in the foreign TU
27324 list. */
27325 header.append_uint (4, dwarf5_byte_order, 0);
27326
27327 /* bucket_count - The number of hash buckets in the hash lookup
27328 table. */
27329 header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ());
27330
27331 /* name_count - The number of unique names in the index. */
27332 header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
27333
27334 /* abbrev_table_size - The size in bytes of the abbreviations
27335 table. */
27336 header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
27337
27338 /* augmentation_string_size - The size in bytes of the augmentation
27339 string. This value is rounded up to a multiple of 4. */
27340 static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, "");
27341 header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation));
27342 header.append_data (dwarf5_gdb_augmentation);
27343
27344 gdb_assert (header.size () == bytes_of_header);
27345
27346 header.file_write (out_file);
27347 cu_list.file_write (out_file);
27348 types_cu_list.file_write (out_file);
27349 nametable.file_write (out_file, out_file_str);
27350
27351 return expected_bytes;
27352}
27353
27354/* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
27355 position is at the end of the file. */
27356
27357static void
27358assert_file_size (FILE *file, const char *filename, size_t expected_size)
27359{
27360 const auto file_size = ftell (file);
27361 if (file_size == -1)
27362 error (_("Can't get `%s' size"), filename);
27363 gdb_assert (file_size == expected_size);
27364}
27365
437afbb8
JK
27366/* Create an index file for OBJFILE in the directory DIR. */
27367
27368static void
ed2dc618
SM
27369write_psymtabs_to_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
27370 const char *dir,
437afbb8
JK
27371 dw_index_kind index_kind)
27372{
ed2dc618
SM
27373 struct objfile *objfile = dwarf2_per_objfile->objfile;
27374
437afbb8
JK
27375 if (dwarf2_per_objfile->using_index)
27376 error (_("Cannot use an index to create the index"));
27377
27378 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
27379 error (_("Cannot make an index when the file has multiple .debug_types sections"));
27380
27381 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
27382 return;
27383
27384 struct stat st;
27385 if (stat (objfile_name (objfile), &st) < 0)
27386 perror_with_name (objfile_name (objfile));
27387
27388 std::string filename (std::string (dir) + SLASH_STRING
27389 + lbasename (objfile_name (objfile))
27390 + (index_kind == dw_index_kind::DEBUG_NAMES
27391 ? INDEX5_SUFFIX : INDEX4_SUFFIX));
27392
27393 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
27394 if (!out_file)
27395 error (_("Can't open `%s' for writing"), filename.c_str ());
27396
27397 /* Order matters here; we want FILE to be closed before FILENAME is
27398 unlinked, because on MS-Windows one cannot delete a file that is
27399 still open. (Don't call anything here that might throw until
27400 file_closer is created.) */
27401 gdb::unlinker unlink_file (filename.c_str ());
27402 gdb_file_up close_out_file (out_file);
27403
27404 if (index_kind == dw_index_kind::DEBUG_NAMES)
27405 {
27406 std::string filename_str (std::string (dir) + SLASH_STRING
27407 + lbasename (objfile_name (objfile))
27408 + DEBUG_STR_SUFFIX);
27409 FILE *out_file_str
27410 = gdb_fopen_cloexec (filename_str.c_str (), "wb").release ();
27411 if (!out_file_str)
27412 error (_("Can't open `%s' for writing"), filename_str.c_str ());
27413 gdb::unlinker unlink_file_str (filename_str.c_str ());
27414 gdb_file_up close_out_file_str (out_file_str);
27415
27416 const size_t total_len
ed2dc618 27417 = write_debug_names (dwarf2_per_objfile, out_file, out_file_str);
437afbb8
JK
27418 assert_file_size (out_file, filename.c_str (), total_len);
27419
27420 /* We want to keep the file .debug_str file too. */
27421 unlink_file_str.keep ();
27422 }
27423 else
27424 {
27425 const size_t total_len
ed2dc618 27426 = write_gdbindex (dwarf2_per_objfile, out_file);
437afbb8
JK
27427 assert_file_size (out_file, filename.c_str (), total_len);
27428 }
27429
bef155c3
TT
27430 /* We want to keep the file. */
27431 unlink_file.keep ();
9291a0cd
TT
27432}
27433
90476074
TT
27434/* Implementation of the `save gdb-index' command.
27435
437afbb8
JK
27436 Note that the .gdb_index file format used by this command is
27437 documented in the GDB manual. Any changes here must be documented
27438 there. */
11570e71 27439
9291a0cd 27440static void
8384c356 27441save_gdb_index_command (const char *arg, int from_tty)
9291a0cd
TT
27442{
27443 struct objfile *objfile;
437afbb8
JK
27444 const char dwarf5space[] = "-dwarf-5 ";
27445 dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
27446
27447 if (!arg)
27448 arg = "";
27449
27450 arg = skip_spaces (arg);
27451 if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
27452 {
27453 index_kind = dw_index_kind::DEBUG_NAMES;
27454 arg += strlen (dwarf5space);
27455 arg = skip_spaces (arg);
27456 }
9291a0cd 27457
437afbb8
JK
27458 if (!*arg)
27459 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
9291a0cd
TT
27460
27461 ALL_OBJFILES (objfile)
27462 {
27463 struct stat st;
27464
27465 /* If the objfile does not correspond to an actual file, skip it. */
4262abfb 27466 if (stat (objfile_name (objfile), &st) < 0)
9291a0cd
TT
27467 continue;
27468
ed2dc618
SM
27469 struct dwarf2_per_objfile *dwarf2_per_objfile
27470 = get_dwarf2_per_objfile (objfile);
9291a0cd 27471
ed2dc618
SM
27472 if (dwarf2_per_objfile != NULL)
27473 {
492d29ea 27474 TRY
9291a0cd 27475 {
ed2dc618 27476 write_psymtabs_to_index (dwarf2_per_objfile, arg, index_kind);
9291a0cd 27477 }
492d29ea
PA
27478 CATCH (except, RETURN_MASK_ERROR)
27479 {
27480 exception_fprintf (gdb_stderr, except,
27481 _("Error while writing index for `%s': "),
27482 objfile_name (objfile));
27483 }
27484 END_CATCH
9291a0cd 27485 }
ed2dc618 27486
9291a0cd 27487 }
dce234bc
PP
27488}
27489
9291a0cd
TT
27490\f
27491
b4f54984 27492int dwarf_always_disassemble;
9eae7c52
TT
27493
27494static void
b4f54984
DE
27495show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
27496 struct cmd_list_element *c, const char *value)
9eae7c52 27497{
3e43a32a
MS
27498 fprintf_filtered (file,
27499 _("Whether to always disassemble "
27500 "DWARF expressions is %s.\n"),
9eae7c52
TT
27501 value);
27502}
27503
900e11f9
JK
27504static void
27505show_check_physname (struct ui_file *file, int from_tty,
27506 struct cmd_list_element *c, const char *value)
27507{
27508 fprintf_filtered (file,
27509 _("Whether to check \"physname\" is %s.\n"),
27510 value);
27511}
27512
6502dd73
DJ
27513void
27514_initialize_dwarf2_read (void)
27515{
96d19272
JK
27516 struct cmd_list_element *c;
27517
fc8e7e75 27518 dwarf2_objfile_data_key = register_objfile_data ();
ae038cb0 27519
b4f54984
DE
27520 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
27521Set DWARF specific variables.\n\
27522Configure DWARF variables such as the cache size"),
27523 &set_dwarf_cmdlist, "maintenance set dwarf ",
ae038cb0
DJ
27524 0/*allow-unknown*/, &maintenance_set_cmdlist);
27525
b4f54984
DE
27526 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
27527Show DWARF specific variables\n\
27528Show DWARF variables such as the cache size"),
27529 &show_dwarf_cmdlist, "maintenance show dwarf ",
ae038cb0
DJ
27530 0/*allow-unknown*/, &maintenance_show_cmdlist);
27531
27532 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
b4f54984
DE
27533 &dwarf_max_cache_age, _("\
27534Set the upper bound on the age of cached DWARF compilation units."), _("\
27535Show the upper bound on the age of cached DWARF compilation units."), _("\
7915a72c
AC
27536A higher limit means that cached compilation units will be stored\n\
27537in memory longer, and more total memory will be used. Zero disables\n\
27538caching, which can slow down startup."),
2c5b56ce 27539 NULL,
b4f54984
DE
27540 show_dwarf_max_cache_age,
27541 &set_dwarf_cmdlist,
27542 &show_dwarf_cmdlist);
d97bc12b 27543
9eae7c52 27544 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
b4f54984 27545 &dwarf_always_disassemble, _("\
9eae7c52
TT
27546Set whether `info address' always disassembles DWARF expressions."), _("\
27547Show whether `info address' always disassembles DWARF expressions."), _("\
27548When enabled, DWARF expressions are always printed in an assembly-like\n\
27549syntax. When disabled, expressions will be printed in a more\n\
27550conversational style, when possible."),
27551 NULL,
b4f54984
DE
27552 show_dwarf_always_disassemble,
27553 &set_dwarf_cmdlist,
27554 &show_dwarf_cmdlist);
27555
27556 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
27557Set debugging of the DWARF reader."), _("\
27558Show debugging of the DWARF reader."), _("\
27559When enabled (non-zero), debugging messages are printed during DWARF\n\
73be47f5
DE
27560reading and symtab expansion. A value of 1 (one) provides basic\n\
27561information. A value greater than 1 provides more verbose information."),
45cfd468
DE
27562 NULL,
27563 NULL,
27564 &setdebuglist, &showdebuglist);
27565
b4f54984
DE
27566 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
27567Set debugging of the DWARF DIE reader."), _("\
27568Show debugging of the DWARF DIE reader."), _("\
d97bc12b
DE
27569When enabled (non-zero), DIEs are dumped after they are read in.\n\
27570The value is the maximum depth to print."),
ccce17b0
YQ
27571 NULL,
27572 NULL,
27573 &setdebuglist, &showdebuglist);
9291a0cd 27574
27e0867f
DE
27575 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
27576Set debugging of the dwarf line reader."), _("\
27577Show debugging of the dwarf line reader."), _("\
27578When enabled (non-zero), line number entries are dumped as they are read in.\n\
27579A value of 1 (one) provides basic information.\n\
27580A value greater than 1 provides more verbose information."),
27581 NULL,
27582 NULL,
27583 &setdebuglist, &showdebuglist);
27584
900e11f9
JK
27585 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
27586Set cross-checking of \"physname\" code against demangler."), _("\
27587Show cross-checking of \"physname\" code against demangler."), _("\
27588When enabled, GDB's internal \"physname\" code is checked against\n\
27589the demangler."),
27590 NULL, show_check_physname,
27591 &setdebuglist, &showdebuglist);
27592
e615022a
DE
27593 add_setshow_boolean_cmd ("use-deprecated-index-sections",
27594 no_class, &use_deprecated_index_sections, _("\
27595Set whether to use deprecated gdb_index sections."), _("\
27596Show whether to use deprecated gdb_index sections."), _("\
27597When enabled, deprecated .gdb_index sections are used anyway.\n\
27598Normally they are ignored either because of a missing feature or\n\
27599performance issue.\n\
27600Warning: This option must be enabled before gdb reads the file."),
27601 NULL,
27602 NULL,
27603 &setlist, &showlist);
27604
96d19272 27605 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 27606 _("\
fc1a9d6e 27607Save a gdb-index file.\n\
437afbb8
JK
27608Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
27609\n\
27610No options create one file with .gdb-index extension for pre-DWARF-5\n\
27611compatible .gdb_index section. With -dwarf-5 creates two files with\n\
27612extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
96d19272
JK
27613 &save_cmdlist);
27614 set_cmd_completer (c, filename_completer);
f1e6e072
TT
27615
27616 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
27617 &dwarf2_locexpr_funcs);
27618 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
27619 &dwarf2_loclist_funcs);
27620
27621 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
27622 &dwarf2_block_frame_base_locexpr_funcs);
27623 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
27624 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
27625
27626#if GDB_SELF_TEST
27627 selftests::register_test ("dw2_expand_symtabs_matching",
27628 selftests::dw2_expand_symtabs_matching::run_test);
27629#endif
6502dd73 27630}
This page took 4.48171 seconds and 4 git commands to generate.