Create new common/pathstuff.[ch]
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
e2882c85 3 Copyright (C) 1994-2018 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
80626a55 33#include "elf-bfd.h"
c906108c
SS
34#include "symtab.h"
35#include "gdbtypes.h"
c906108c 36#include "objfiles.h"
fa8f86ff 37#include "dwarf2.h"
c906108c
SS
38#include "buildsym.h"
39#include "demangle.h"
50f182aa 40#include "gdb-demangle.h"
c906108c 41#include "expression.h"
d5166ae1 42#include "filenames.h" /* for DOSish file names */
2e276125 43#include "macrotab.h"
c906108c
SS
44#include "language.h"
45#include "complaints.h"
357e46e7 46#include "bcache.h"
4c2df51b
DJ
47#include "dwarf2expr.h"
48#include "dwarf2loc.h"
9219021c 49#include "cp-support.h"
72bf9492 50#include "hashtab.h"
ae038cb0
DJ
51#include "command.h"
52#include "gdbcmd.h"
edb3359d 53#include "block.h"
ff013f42 54#include "addrmap.h"
94af9270 55#include "typeprint.h"
ccefe4c4 56#include "psympriv.h"
53ce3c39 57#include <sys/stat.h>
96d19272 58#include "completer.h"
34eaf542 59#include "vec.h"
98bfdba5 60#include "c-lang.h"
a766d390 61#include "go-lang.h"
98bfdba5 62#include "valprint.h"
3019eac3 63#include "gdbcore.h" /* for gnutarget */
156942c7 64#include "gdb/gdb-index.h"
60d5a603 65#include <ctype.h>
cbb099e8 66#include "gdb_bfd.h"
4357ac6c 67#include "f-lang.h"
05cba821 68#include "source.h"
614c279d 69#include "filestuff.h"
dc294be5 70#include "build-id.h"
22cee43f 71#include "namespace.h"
bef155c3 72#include "common/gdb_unlinker.h"
14bc53a8 73#include "common/function-view.h"
ecfb656c
PA
74#include "common/gdb_optional.h"
75#include "common/underlying.h"
d5722aa2 76#include "common/byte-vector.h"
927aa2e7 77#include "common/hash_enum.h"
bbf2f4df 78#include "filename-seen-cache.h"
b32b108a 79#include "producer.h"
c906108c 80#include <fcntl.h>
c906108c 81#include <sys/types.h>
325fac50 82#include <algorithm>
bc8f2430
JK
83#include <unordered_set>
84#include <unordered_map>
c62446b1 85#include "selftest.h"
437afbb8
JK
86#include <cmath>
87#include <set>
88#include <forward_list>
c9317f21 89#include "rust-lang.h"
b4987c95 90#include "common/pathstuff.h"
437afbb8 91
73be47f5
DE
92/* When == 1, print basic high level tracing messages.
93 When > 1, be more verbose.
b4f54984
DE
94 This is in contrast to the low level DIE reading of dwarf_die_debug. */
95static unsigned int dwarf_read_debug = 0;
45cfd468 96
d97bc12b 97/* When non-zero, dump DIEs after they are read in. */
b4f54984 98static unsigned int dwarf_die_debug = 0;
d97bc12b 99
27e0867f
DE
100/* When non-zero, dump line number entries as they are read in. */
101static unsigned int dwarf_line_debug = 0;
102
900e11f9
JK
103/* When non-zero, cross-check physname against demangler. */
104static int check_physname = 0;
105
481860b3 106/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 107static int use_deprecated_index_sections = 0;
481860b3 108
6502dd73
DJ
109static const struct objfile_data *dwarf2_objfile_data_key;
110
f1e6e072
TT
111/* The "aclass" indices for various kinds of computed DWARF symbols. */
112
113static int dwarf2_locexpr_index;
114static int dwarf2_loclist_index;
115static int dwarf2_locexpr_block_index;
116static int dwarf2_loclist_block_index;
117
73869dc2
DE
118/* A descriptor for dwarf sections.
119
120 S.ASECTION, SIZE are typically initialized when the objfile is first
121 scanned. BUFFER, READIN are filled in later when the section is read.
122 If the section contained compressed data then SIZE is updated to record
123 the uncompressed size of the section.
124
125 DWP file format V2 introduces a wrinkle that is easiest to handle by
126 creating the concept of virtual sections contained within a real section.
127 In DWP V2 the sections of the input DWO files are concatenated together
128 into one section, but section offsets are kept relative to the original
129 input section.
130 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
131 the real section this "virtual" section is contained in, and BUFFER,SIZE
132 describe the virtual section. */
133
dce234bc
PP
134struct dwarf2_section_info
135{
73869dc2
DE
136 union
137 {
e5aa3347 138 /* If this is a real section, the bfd section. */
049412e3 139 asection *section;
73869dc2 140 /* If this is a virtual section, pointer to the containing ("real")
e5aa3347 141 section. */
73869dc2
DE
142 struct dwarf2_section_info *containing_section;
143 } s;
19ac8c2e 144 /* Pointer to section data, only valid if readin. */
d521ce57 145 const gdb_byte *buffer;
73869dc2 146 /* The size of the section, real or virtual. */
dce234bc 147 bfd_size_type size;
73869dc2
DE
148 /* If this is a virtual section, the offset in the real section.
149 Only valid if is_virtual. */
150 bfd_size_type virtual_offset;
be391dca 151 /* True if we have tried to read this section. */
73869dc2
DE
152 char readin;
153 /* True if this is a virtual section, False otherwise.
049412e3 154 This specifies which of s.section and s.containing_section to use. */
73869dc2 155 char is_virtual;
dce234bc
PP
156};
157
8b70b953
TT
158typedef struct dwarf2_section_info dwarf2_section_info_def;
159DEF_VEC_O (dwarf2_section_info_def);
160
9291a0cd
TT
161/* All offsets in the index are of this type. It must be
162 architecture-independent. */
163typedef uint32_t offset_type;
164
165DEF_VEC_I (offset_type);
166
156942c7
DE
167/* Ensure only legit values are used. */
168#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
169 do { \
170 gdb_assert ((unsigned int) (value) <= 1); \
171 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
172 } while (0)
173
174/* Ensure only legit values are used. */
175#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
176 do { \
177 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
178 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
179 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
180 } while (0)
181
182/* Ensure we don't use more than the alloted nuber of bits for the CU. */
183#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
184 do { \
185 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
186 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
187 } while (0)
188
3f563c84
PA
189#if WORDS_BIGENDIAN
190
191/* Convert VALUE between big- and little-endian. */
192
193static offset_type
194byte_swap (offset_type value)
195{
196 offset_type result;
197
198 result = (value & 0xff) << 24;
199 result |= (value & 0xff00) << 8;
200 result |= (value & 0xff0000) >> 8;
201 result |= (value & 0xff000000) >> 24;
202 return result;
203}
204
205#define MAYBE_SWAP(V) byte_swap (V)
206
207#else
208#define MAYBE_SWAP(V) static_cast<offset_type> (V)
209#endif /* WORDS_BIGENDIAN */
210
211/* An index into a (C++) symbol name component in a symbol name as
212 recorded in the mapped_index's symbol table. For each C++ symbol
213 in the symbol table, we record one entry for the start of each
214 component in the symbol in a table of name components, and then
215 sort the table, in order to be able to binary search symbol names,
216 ignoring leading namespaces, both completion and regular look up.
217 For example, for symbol "A::B::C", we'll have an entry that points
218 to "A::B::C", another that points to "B::C", and another for "C".
219 Note that function symbols in GDB index have no parameter
220 information, just the function/method names. You can convert a
221 name_component to a "const char *" using the
222 'mapped_index::symbol_name_at(offset_type)' method. */
223
224struct name_component
225{
226 /* Offset in the symbol name where the component starts. Stored as
227 a (32-bit) offset instead of a pointer to save memory and improve
228 locality on 64-bit architectures. */
229 offset_type name_offset;
230
231 /* The symbol's index in the symbol and constant pool tables of a
232 mapped_index. */
233 offset_type idx;
234};
235
44ed8f3e
PA
236/* Base class containing bits shared by both .gdb_index and
237 .debug_name indexes. */
238
239struct mapped_index_base
240{
241 /* The name_component table (a sorted vector). See name_component's
242 description above. */
243 std::vector<name_component> name_components;
244
245 /* How NAME_COMPONENTS is sorted. */
246 enum case_sensitivity name_components_casing;
247
248 /* Return the number of names in the symbol table. */
249 virtual size_t symbol_name_count () const = 0;
250
251 /* Get the name of the symbol at IDX in the symbol table. */
252 virtual const char *symbol_name_at (offset_type idx) const = 0;
253
254 /* Return whether the name at IDX in the symbol table should be
255 ignored. */
256 virtual bool symbol_name_slot_invalid (offset_type idx) const
257 {
258 return false;
259 }
260
261 /* Build the symbol name component sorted vector, if we haven't
262 yet. */
263 void build_name_components ();
264
265 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
266 possible matches for LN_NO_PARAMS in the name component
267 vector. */
268 std::pair<std::vector<name_component>::const_iterator,
269 std::vector<name_component>::const_iterator>
270 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
271
272 /* Prevent deleting/destroying via a base class pointer. */
273protected:
274 ~mapped_index_base() = default;
275};
276
9291a0cd
TT
277/* A description of the mapped index. The file format is described in
278 a comment by the code that writes the index. */
fc898b42 279struct mapped_index final : public mapped_index_base
9291a0cd 280{
f00a2de2
PA
281 /* A slot/bucket in the symbol table hash. */
282 struct symbol_table_slot
283 {
284 const offset_type name;
285 const offset_type vec;
286 };
287
559a7a62
JK
288 /* Index data format version. */
289 int version;
290
9291a0cd
TT
291 /* The total length of the buffer. */
292 off_t total_size;
b11b1f88 293
f00a2de2
PA
294 /* The address table data. */
295 gdb::array_view<const gdb_byte> address_table;
b11b1f88 296
3876f04e 297 /* The symbol table, implemented as a hash table. */
f00a2de2 298 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 299
9291a0cd
TT
300 /* A pointer to the constant pool. */
301 const char *constant_pool;
3f563c84 302
44ed8f3e
PA
303 bool symbol_name_slot_invalid (offset_type idx) const override
304 {
305 const auto &bucket = this->symbol_table[idx];
306 return bucket.name == 0 && bucket.vec;
307 }
5c58de74 308
3f563c84
PA
309 /* Convenience method to get at the name of the symbol at IDX in the
310 symbol table. */
44ed8f3e 311 const char *symbol_name_at (offset_type idx) const override
f00a2de2 312 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 313
44ed8f3e
PA
314 size_t symbol_name_count () const override
315 { return this->symbol_table.size (); }
9291a0cd
TT
316};
317
927aa2e7
JK
318/* A description of the mapped .debug_names.
319 Uninitialized map has CU_COUNT 0. */
fc898b42 320struct mapped_debug_names final : public mapped_index_base
927aa2e7 321{
ed2dc618
SM
322 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
323 : dwarf2_per_objfile (dwarf2_per_objfile_)
324 {}
325
326 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
327 bfd_endian dwarf5_byte_order;
328 bool dwarf5_is_dwarf64;
329 bool augmentation_is_gdb;
330 uint8_t offset_size;
331 uint32_t cu_count = 0;
332 uint32_t tu_count, bucket_count, name_count;
333 const gdb_byte *cu_table_reordered, *tu_table_reordered;
334 const uint32_t *bucket_table_reordered, *hash_table_reordered;
335 const gdb_byte *name_table_string_offs_reordered;
336 const gdb_byte *name_table_entry_offs_reordered;
337 const gdb_byte *entry_pool;
338
339 struct index_val
340 {
341 ULONGEST dwarf_tag;
342 struct attr
343 {
344 /* Attribute name DW_IDX_*. */
345 ULONGEST dw_idx;
346
347 /* Attribute form DW_FORM_*. */
348 ULONGEST form;
349
350 /* Value if FORM is DW_FORM_implicit_const. */
351 LONGEST implicit_const;
352 };
353 std::vector<attr> attr_vec;
354 };
355
356 std::unordered_map<ULONGEST, index_val> abbrev_map;
357
358 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
359
360 /* Implementation of the mapped_index_base virtual interface, for
361 the name_components cache. */
362
363 const char *symbol_name_at (offset_type idx) const override
364 { return namei_to_name (idx); }
365
366 size_t symbol_name_count () const override
367 { return this->name_count; }
927aa2e7
JK
368};
369
95554aad
TT
370typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
371DEF_VEC_P (dwarf2_per_cu_ptr);
372
52059ffd
TT
373struct tu_stats
374{
375 int nr_uniq_abbrev_tables;
376 int nr_symtabs;
377 int nr_symtab_sharers;
378 int nr_stmt_less_type_units;
379 int nr_all_type_units_reallocs;
380};
381
9cdd5dbd
DE
382/* Collection of data recorded per objfile.
383 This hangs off of dwarf2_objfile_data_key. */
384
fd90ace4 385struct dwarf2_per_objfile : public allocate_on_obstack
6502dd73 386{
330cdd98
PA
387 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
388 dwarf2 section names, or is NULL if the standard ELF names are
389 used. */
390 dwarf2_per_objfile (struct objfile *objfile,
391 const dwarf2_debug_sections *names);
ae038cb0 392
330cdd98
PA
393 ~dwarf2_per_objfile ();
394
d6541620 395 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
330cdd98
PA
396
397 /* Free all cached compilation units. */
398 void free_cached_comp_units ();
399private:
400 /* This function is mapped across the sections and remembers the
401 offset and size of each of the debugging sections we are
402 interested in. */
403 void locate_sections (bfd *abfd, asection *sectp,
404 const dwarf2_debug_sections &names);
405
406public:
407 dwarf2_section_info info {};
408 dwarf2_section_info abbrev {};
409 dwarf2_section_info line {};
410 dwarf2_section_info loc {};
411 dwarf2_section_info loclists {};
412 dwarf2_section_info macinfo {};
413 dwarf2_section_info macro {};
414 dwarf2_section_info str {};
415 dwarf2_section_info line_str {};
416 dwarf2_section_info ranges {};
417 dwarf2_section_info rnglists {};
418 dwarf2_section_info addr {};
419 dwarf2_section_info frame {};
420 dwarf2_section_info eh_frame {};
421 dwarf2_section_info gdb_index {};
927aa2e7
JK
422 dwarf2_section_info debug_names {};
423 dwarf2_section_info debug_aranges {};
330cdd98
PA
424
425 VEC (dwarf2_section_info_def) *types = NULL;
8b70b953 426
be391dca 427 /* Back link. */
330cdd98 428 struct objfile *objfile = NULL;
be391dca 429
d467dd73 430 /* Table of all the compilation units. This is used to locate
10b3939b 431 the target compilation unit of a particular reference. */
330cdd98 432 struct dwarf2_per_cu_data **all_comp_units = NULL;
ae038cb0
DJ
433
434 /* The number of compilation units in ALL_COMP_UNITS. */
330cdd98 435 int n_comp_units = 0;
ae038cb0 436
1fd400ff 437 /* The number of .debug_types-related CUs. */
330cdd98 438 int n_type_units = 0;
1fd400ff 439
6aa5f3a6
DE
440 /* The number of elements allocated in all_type_units.
441 If there are skeleton-less TUs, we add them to all_type_units lazily. */
330cdd98 442 int n_allocated_type_units = 0;
6aa5f3a6 443
a2ce51a0
DE
444 /* The .debug_types-related CUs (TUs).
445 This is stored in malloc space because we may realloc it. */
330cdd98 446 struct signatured_type **all_type_units = NULL;
1fd400ff 447
f4dc4d17
DE
448 /* Table of struct type_unit_group objects.
449 The hash key is the DW_AT_stmt_list value. */
330cdd98 450 htab_t type_unit_groups {};
72dca2f5 451
348e048f
DE
452 /* A table mapping .debug_types signatures to its signatured_type entry.
453 This is NULL if the .debug_types section hasn't been read in yet. */
330cdd98 454 htab_t signatured_types {};
348e048f 455
f4dc4d17
DE
456 /* Type unit statistics, to see how well the scaling improvements
457 are doing. */
330cdd98 458 struct tu_stats tu_stats {};
f4dc4d17
DE
459
460 /* A chain of compilation units that are currently read in, so that
461 they can be freed later. */
330cdd98 462 dwarf2_per_cu_data *read_in_chain = NULL;
f4dc4d17 463
3019eac3
DE
464 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
465 This is NULL if the table hasn't been allocated yet. */
330cdd98 466 htab_t dwo_files {};
3019eac3 467
330cdd98
PA
468 /* True if we've checked for whether there is a DWP file. */
469 bool dwp_checked = false;
80626a55
DE
470
471 /* The DWP file if there is one, or NULL. */
330cdd98 472 struct dwp_file *dwp_file = NULL;
80626a55 473
36586728
TT
474 /* The shared '.dwz' file, if one exists. This is used when the
475 original data was compressed using 'dwz -m'. */
330cdd98 476 struct dwz_file *dwz_file = NULL;
36586728 477
330cdd98 478 /* A flag indicating whether this objfile has a section loaded at a
72dca2f5 479 VMA of 0. */
330cdd98 480 bool has_section_at_zero = false;
9291a0cd 481
ae2de4f8
DE
482 /* True if we are using the mapped index,
483 or we are faking it for OBJF_READNOW's sake. */
330cdd98 484 bool using_index = false;
9291a0cd 485
ae2de4f8 486 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
330cdd98 487 mapped_index *index_table = NULL;
98bfdba5 488
927aa2e7
JK
489 /* The mapped index, or NULL if .debug_names is missing or not being used. */
490 std::unique_ptr<mapped_debug_names> debug_names_table;
491
7b9f3c50 492 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
493 TUs typically share line table entries with a CU, so we maintain a
494 separate table of all line table entries to support the sharing.
495 Note that while there can be way more TUs than CUs, we've already
496 sorted all the TUs into "type unit groups", grouped by their
497 DW_AT_stmt_list value. Therefore the only sharing done here is with a
498 CU and its associated TU group if there is one. */
330cdd98 499 htab_t quick_file_names_table {};
7b9f3c50 500
98bfdba5
PA
501 /* Set during partial symbol reading, to prevent queueing of full
502 symbols. */
330cdd98 503 bool reading_partial_symbols = false;
673bfd45 504
dee91e82 505 /* Table mapping type DIEs to their struct type *.
673bfd45 506 This is NULL if not allocated yet.
02142a6c 507 The mapping is done via (CU/TU + DIE offset) -> type. */
330cdd98 508 htab_t die_type_hash {};
95554aad
TT
509
510 /* The CUs we recently read. */
330cdd98 511 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
527f3840
JK
512
513 /* Table containing line_header indexed by offset and offset_in_dwz. */
330cdd98 514 htab_t line_header_hash {};
bbf2f4df
PA
515
516 /* Table containing all filenames. This is an optional because the
517 table is lazily constructed on first access. */
518 gdb::optional<filename_seen_cache> filenames_cache;
6502dd73
DJ
519};
520
ed2dc618
SM
521/* Get the dwarf2_per_objfile associated to OBJFILE. */
522
523struct dwarf2_per_objfile *
524get_dwarf2_per_objfile (struct objfile *objfile)
525{
526 return ((struct dwarf2_per_objfile *)
527 objfile_data (objfile, dwarf2_objfile_data_key));
528}
529
530/* Set the dwarf2_per_objfile associated to OBJFILE. */
531
532void
533set_dwarf2_per_objfile (struct objfile *objfile,
534 struct dwarf2_per_objfile *dwarf2_per_objfile)
535{
536 gdb_assert (get_dwarf2_per_objfile (objfile) == NULL);
537 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
538}
c906108c 539
251d32d9 540/* Default names of the debugging sections. */
c906108c 541
233a11ab
CS
542/* Note that if the debugging section has been compressed, it might
543 have a name like .zdebug_info. */
544
9cdd5dbd
DE
545static const struct dwarf2_debug_sections dwarf2_elf_names =
546{
251d32d9
TG
547 { ".debug_info", ".zdebug_info" },
548 { ".debug_abbrev", ".zdebug_abbrev" },
549 { ".debug_line", ".zdebug_line" },
550 { ".debug_loc", ".zdebug_loc" },
43988095 551 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 552 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 553 { ".debug_macro", ".zdebug_macro" },
251d32d9 554 { ".debug_str", ".zdebug_str" },
43988095 555 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 556 { ".debug_ranges", ".zdebug_ranges" },
43988095 557 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 558 { ".debug_types", ".zdebug_types" },
3019eac3 559 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
560 { ".debug_frame", ".zdebug_frame" },
561 { ".eh_frame", NULL },
24d3216f 562 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
563 { ".debug_names", ".zdebug_names" },
564 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 565 23
251d32d9 566};
c906108c 567
80626a55 568/* List of DWO/DWP sections. */
3019eac3 569
80626a55 570static const struct dwop_section_names
3019eac3
DE
571{
572 struct dwarf2_section_names abbrev_dwo;
573 struct dwarf2_section_names info_dwo;
574 struct dwarf2_section_names line_dwo;
575 struct dwarf2_section_names loc_dwo;
43988095 576 struct dwarf2_section_names loclists_dwo;
09262596
DE
577 struct dwarf2_section_names macinfo_dwo;
578 struct dwarf2_section_names macro_dwo;
3019eac3
DE
579 struct dwarf2_section_names str_dwo;
580 struct dwarf2_section_names str_offsets_dwo;
581 struct dwarf2_section_names types_dwo;
80626a55
DE
582 struct dwarf2_section_names cu_index;
583 struct dwarf2_section_names tu_index;
3019eac3 584}
80626a55 585dwop_section_names =
3019eac3
DE
586{
587 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
588 { ".debug_info.dwo", ".zdebug_info.dwo" },
589 { ".debug_line.dwo", ".zdebug_line.dwo" },
590 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 591 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
592 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
593 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
594 { ".debug_str.dwo", ".zdebug_str.dwo" },
595 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
596 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
597 { ".debug_cu_index", ".zdebug_cu_index" },
598 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
599};
600
c906108c
SS
601/* local data types */
602
107d2387
AC
603/* The data in a compilation unit header, after target2host
604 translation, looks like this. */
c906108c 605struct comp_unit_head
a738430d 606{
c764a876 607 unsigned int length;
a738430d 608 short version;
a738430d
MK
609 unsigned char addr_size;
610 unsigned char signed_addr_p;
9c541725 611 sect_offset abbrev_sect_off;
57349743 612
a738430d
MK
613 /* Size of file offsets; either 4 or 8. */
614 unsigned int offset_size;
57349743 615
a738430d
MK
616 /* Size of the length field; either 4 or 12. */
617 unsigned int initial_length_size;
57349743 618
43988095
JK
619 enum dwarf_unit_type unit_type;
620
a738430d
MK
621 /* Offset to the first byte of this compilation unit header in the
622 .debug_info section, for resolving relative reference dies. */
9c541725 623 sect_offset sect_off;
57349743 624
d00adf39
DE
625 /* Offset to first die in this cu from the start of the cu.
626 This will be the first byte following the compilation unit header. */
9c541725 627 cu_offset first_die_cu_offset;
43988095
JK
628
629 /* 64-bit signature of this type unit - it is valid only for
630 UNIT_TYPE DW_UT_type. */
631 ULONGEST signature;
632
633 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 634 cu_offset type_cu_offset_in_tu;
a738430d 635};
c906108c 636
3da10d80
KS
637/* Type used for delaying computation of method physnames.
638 See comments for compute_delayed_physnames. */
639struct delayed_method_info
640{
641 /* The type to which the method is attached, i.e., its parent class. */
642 struct type *type;
643
644 /* The index of the method in the type's function fieldlists. */
645 int fnfield_index;
646
647 /* The index of the method in the fieldlist. */
648 int index;
649
650 /* The name of the DIE. */
651 const char *name;
652
653 /* The DIE associated with this method. */
654 struct die_info *die;
655};
656
e7c27a73
DJ
657/* Internal state when decoding a particular compilation unit. */
658struct dwarf2_cu
659{
fcd3b13d
SM
660 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
661 ~dwarf2_cu ();
662
663 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
664
d00adf39 665 /* The header of the compilation unit. */
fcd3b13d 666 struct comp_unit_head header {};
e142c38c 667
d00adf39 668 /* Base address of this compilation unit. */
fcd3b13d 669 CORE_ADDR base_address = 0;
d00adf39
DE
670
671 /* Non-zero if base_address has been set. */
fcd3b13d 672 int base_known = 0;
d00adf39 673
e142c38c 674 /* The language we are debugging. */
fcd3b13d
SM
675 enum language language = language_unknown;
676 const struct language_defn *language_defn = nullptr;
e142c38c 677
fcd3b13d 678 const char *producer = nullptr;
b0f35d58 679
e142c38c
DJ
680 /* The generic symbol table building routines have separate lists for
681 file scope symbols and all all other scopes (local scopes). So
682 we need to select the right one to pass to add_symbol_to_list().
683 We do it by keeping a pointer to the correct list in list_in_scope.
684
685 FIXME: The original dwarf code just treated the file scope as the
686 first local scope, and all other local scopes as nested local
687 scopes, and worked fine. Check to see if we really need to
688 distinguish these in buildsym.c. */
fcd3b13d 689 struct pending **list_in_scope = nullptr;
e142c38c 690
b64f50a1
JK
691 /* Hash table holding all the loaded partial DIEs
692 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 693 htab_t partial_dies = nullptr;
72bf9492
DJ
694
695 /* Storage for things with the same lifetime as this read-in compilation
696 unit, including partial DIEs. */
fcd3b13d 697 auto_obstack comp_unit_obstack;
72bf9492 698
ae038cb0
DJ
699 /* When multiple dwarf2_cu structures are living in memory, this field
700 chains them all together, so that they can be released efficiently.
701 We will probably also want a generation counter so that most-recently-used
702 compilation units are cached... */
fcd3b13d 703 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 704
69d751e3 705 /* Backlink to our per_cu entry. */
ae038cb0
DJ
706 struct dwarf2_per_cu_data *per_cu;
707
708 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 709 int last_used = 0;
ae038cb0 710
b64f50a1
JK
711 /* A hash table of DIE cu_offset for following references with
712 die_info->offset.sect_off as hash. */
fcd3b13d 713 htab_t die_hash = nullptr;
10b3939b
DJ
714
715 /* Full DIEs if read in. */
fcd3b13d 716 struct die_info *dies = nullptr;
10b3939b
DJ
717
718 /* A set of pointers to dwarf2_per_cu_data objects for compilation
719 units referenced by this one. Only set during full symbol processing;
720 partial symbol tables do not have dependencies. */
fcd3b13d 721 htab_t dependencies = nullptr;
10b3939b 722
cb1df416 723 /* Header data from the line table, during full symbol processing. */
fcd3b13d 724 struct line_header *line_header = nullptr;
4c8aa72d
PA
725 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
726 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
727 this is the DW_TAG_compile_unit die for this CU. We'll hold on
728 to the line header as long as this DIE is being processed. See
729 process_die_scope. */
fcd3b13d 730 die_info *line_header_die_owner = nullptr;
cb1df416 731
3da10d80
KS
732 /* A list of methods which need to have physnames computed
733 after all type information has been read. */
c89b44cd 734 std::vector<delayed_method_info> method_list;
3da10d80 735
96408a79 736 /* To be copied to symtab->call_site_htab. */
fcd3b13d 737 htab_t call_site_htab = nullptr;
96408a79 738
034e5797
DE
739 /* Non-NULL if this CU came from a DWO file.
740 There is an invariant here that is important to remember:
741 Except for attributes copied from the top level DIE in the "main"
742 (or "stub") file in preparation for reading the DWO file
743 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
744 Either there isn't a DWO file (in which case this is NULL and the point
745 is moot), or there is and either we're not going to read it (in which
746 case this is NULL) or there is and we are reading it (in which case this
747 is non-NULL). */
fcd3b13d 748 struct dwo_unit *dwo_unit = nullptr;
3019eac3
DE
749
750 /* The DW_AT_addr_base attribute if present, zero otherwise
751 (zero is a valid value though).
1dbab08b 752 Note this value comes from the Fission stub CU/TU's DIE. */
fcd3b13d 753 ULONGEST addr_base = 0;
3019eac3 754
2e3cf129
DE
755 /* The DW_AT_ranges_base attribute if present, zero otherwise
756 (zero is a valid value though).
1dbab08b 757 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 758 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
759 be used without needing to know whether DWO files are in use or not.
760 N.B. This does not apply to DW_AT_ranges appearing in
761 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
762 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
763 DW_AT_ranges_base *would* have to be applied, and we'd have to care
764 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 765 ULONGEST ranges_base = 0;
2e3cf129 766
c9317f21
TT
767 /* When reading debug info generated by older versions of rustc, we
768 have to rewrite some union types to be struct types with a
769 variant part. This rewriting must be done after the CU is fully
770 read in, because otherwise at the point of rewriting some struct
771 type might not have been fully processed. So, we keep a list of
772 all such types here and process them after expansion. */
773 std::vector<struct type *> rust_unions;
774
ae038cb0
DJ
775 /* Mark used when releasing cached dies. */
776 unsigned int mark : 1;
777
8be455d7
JK
778 /* This CU references .debug_loc. See the symtab->locations_valid field.
779 This test is imperfect as there may exist optimized debug code not using
780 any location list and still facing inlining issues if handled as
781 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 782 unsigned int has_loclist : 1;
ba919b58 783
1b80a9fa
JK
784 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
785 if all the producer_is_* fields are valid. This information is cached
786 because profiling CU expansion showed excessive time spent in
787 producer_is_gxx_lt_4_6. */
ba919b58
TT
788 unsigned int checked_producer : 1;
789 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 790 unsigned int producer_is_gcc_lt_4_3 : 1;
5230b05a 791 unsigned int producer_is_icc_lt_14 : 1;
4d4ec4e5
TT
792
793 /* When set, the file that we're processing is known to have
794 debugging info for C++ namespaces. GCC 3.3.x did not produce
795 this information, but later versions do. */
796
797 unsigned int processing_has_namespace_info : 1;
d590ff25
YQ
798
799 struct partial_die_info *find_partial_die (sect_offset sect_off);
e7c27a73
DJ
800};
801
10b3939b
DJ
802/* Persistent data held for a compilation unit, even when not
803 processing it. We put a pointer to this structure in the
28dee7f5 804 read_symtab_private field of the psymtab. */
10b3939b 805
ae038cb0
DJ
806struct dwarf2_per_cu_data
807{
36586728 808 /* The start offset and length of this compilation unit.
45452591 809 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
810 initial_length_size.
811 If the DIE refers to a DWO file, this is always of the original die,
812 not the DWO file. */
9c541725 813 sect_offset sect_off;
36586728 814 unsigned int length;
ae038cb0 815
43988095
JK
816 /* DWARF standard version this data has been read from (such as 4 or 5). */
817 short dwarf_version;
818
ae038cb0
DJ
819 /* Flag indicating this compilation unit will be read in before
820 any of the current compilation units are processed. */
c764a876 821 unsigned int queued : 1;
ae038cb0 822
0d99eb77
DE
823 /* This flag will be set when reading partial DIEs if we need to load
824 absolutely all DIEs for this compilation unit, instead of just the ones
825 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
826 hash table and don't find it. */
827 unsigned int load_all_dies : 1;
828
0186c6a7
DE
829 /* Non-zero if this CU is from .debug_types.
830 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
831 this is non-zero. */
3019eac3
DE
832 unsigned int is_debug_types : 1;
833
36586728
TT
834 /* Non-zero if this CU is from the .dwz file. */
835 unsigned int is_dwz : 1;
836
a2ce51a0
DE
837 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
838 This flag is only valid if is_debug_types is true.
839 We can't read a CU directly from a DWO file: There are required
840 attributes in the stub. */
841 unsigned int reading_dwo_directly : 1;
842
7ee85ab1
DE
843 /* Non-zero if the TU has been read.
844 This is used to assist the "Stay in DWO Optimization" for Fission:
845 When reading a DWO, it's faster to read TUs from the DWO instead of
846 fetching them from random other DWOs (due to comdat folding).
847 If the TU has already been read, the optimization is unnecessary
848 (and unwise - we don't want to change where gdb thinks the TU lives
849 "midflight").
850 This flag is only valid if is_debug_types is true. */
851 unsigned int tu_read : 1;
852
3019eac3
DE
853 /* The section this CU/TU lives in.
854 If the DIE refers to a DWO file, this is always the original die,
855 not the DWO file. */
8a0459fd 856 struct dwarf2_section_info *section;
348e048f 857
17ea53c3 858 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
cc12ce38
DE
859 of the CU cache it gets reset to NULL again. This is left as NULL for
860 dummy CUs (a CU header, but nothing else). */
ae038cb0 861 struct dwarf2_cu *cu;
1c379e20 862
e3b94546
SM
863 /* The corresponding dwarf2_per_objfile. */
864 struct dwarf2_per_objfile *dwarf2_per_objfile;
9291a0cd 865
fffbe6a8
YQ
866 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
867 is active. Otherwise, the 'psymtab' field is active. */
9291a0cd
TT
868 union
869 {
870 /* The partial symbol table associated with this compilation unit,
95554aad 871 or NULL for unread partial units. */
9291a0cd
TT
872 struct partial_symtab *psymtab;
873
874 /* Data needed by the "quick" functions. */
875 struct dwarf2_per_cu_quick_data *quick;
876 } v;
95554aad 877
796a7ff8
DE
878 /* The CUs we import using DW_TAG_imported_unit. This is filled in
879 while reading psymtabs, used to compute the psymtab dependencies,
880 and then cleared. Then it is filled in again while reading full
881 symbols, and only deleted when the objfile is destroyed.
882
883 This is also used to work around a difference between the way gold
884 generates .gdb_index version <=7 and the way gdb does. Arguably this
885 is a gold bug. For symbols coming from TUs, gold records in the index
886 the CU that includes the TU instead of the TU itself. This breaks
887 dw2_lookup_symbol: It assumes that if the index says symbol X lives
888 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
889 will find X. Alas TUs live in their own symtab, so after expanding CU Y
890 we need to look in TU Z to find X. Fortunately, this is akin to
891 DW_TAG_imported_unit, so we just use the same mechanism: For
892 .gdb_index version <=7 this also records the TUs that the CU referred
893 to. Concurrently with this change gdb was modified to emit version 8
69d751e3
DE
894 indices so we only pay a price for gold generated indices.
895 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
796a7ff8 896 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
897};
898
348e048f
DE
899/* Entry in the signatured_types hash table. */
900
901struct signatured_type
902{
42e7ad6c 903 /* The "per_cu" object of this type.
ac9ec31b 904 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
905 N.B.: This is the first member so that it's easy to convert pointers
906 between them. */
907 struct dwarf2_per_cu_data per_cu;
908
3019eac3 909 /* The type's signature. */
348e048f
DE
910 ULONGEST signature;
911
3019eac3 912 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
913 If this TU is a DWO stub and the definition lives in a DWO file
914 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
915 cu_offset type_offset_in_tu;
916
917 /* Offset in the section of the type's DIE.
918 If the definition lives in a DWO file, this is the offset in the
919 .debug_types.dwo section.
920 The value is zero until the actual value is known.
921 Zero is otherwise not a valid section offset. */
922 sect_offset type_offset_in_section;
0186c6a7
DE
923
924 /* Type units are grouped by their DW_AT_stmt_list entry so that they
925 can share them. This points to the containing symtab. */
926 struct type_unit_group *type_unit_group;
ac9ec31b
DE
927
928 /* The type.
929 The first time we encounter this type we fully read it in and install it
930 in the symbol tables. Subsequent times we only need the type. */
931 struct type *type;
a2ce51a0
DE
932
933 /* Containing DWO unit.
934 This field is valid iff per_cu.reading_dwo_directly. */
935 struct dwo_unit *dwo_unit;
348e048f
DE
936};
937
0186c6a7
DE
938typedef struct signatured_type *sig_type_ptr;
939DEF_VEC_P (sig_type_ptr);
940
094b34ac
DE
941/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
942 This includes type_unit_group and quick_file_names. */
943
944struct stmt_list_hash
945{
946 /* The DWO unit this table is from or NULL if there is none. */
947 struct dwo_unit *dwo_unit;
948
949 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 950 sect_offset line_sect_off;
094b34ac
DE
951};
952
f4dc4d17
DE
953/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
954 an object of this type. */
955
956struct type_unit_group
957{
0186c6a7 958 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
959 To simplify things we create an artificial CU that "includes" all the
960 type units using this stmt_list so that the rest of the code still has
961 a "per_cu" handle on the symtab.
962 This PER_CU is recognized by having no section. */
8a0459fd 963#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
964 struct dwarf2_per_cu_data per_cu;
965
0186c6a7
DE
966 /* The TUs that share this DW_AT_stmt_list entry.
967 This is added to while parsing type units to build partial symtabs,
968 and is deleted afterwards and not used again. */
969 VEC (sig_type_ptr) *tus;
f4dc4d17 970
43f3e411 971 /* The compunit symtab.
094b34ac 972 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
973 so we create an essentially anonymous symtab as the compunit symtab. */
974 struct compunit_symtab *compunit_symtab;
f4dc4d17 975
094b34ac
DE
976 /* The data used to construct the hash key. */
977 struct stmt_list_hash hash;
f4dc4d17
DE
978
979 /* The number of symtabs from the line header.
980 The value here must match line_header.num_file_names. */
981 unsigned int num_symtabs;
982
983 /* The symbol tables for this TU (obtained from the files listed in
984 DW_AT_stmt_list).
985 WARNING: The order of entries here must match the order of entries
986 in the line header. After the first TU using this type_unit_group, the
987 line header for the subsequent TUs is recreated from this. This is done
988 because we need to use the same symtabs for each TU using the same
989 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
990 there's no guarantee the line header doesn't have duplicate entries. */
991 struct symtab **symtabs;
992};
993
73869dc2 994/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
995
996struct dwo_sections
997{
998 struct dwarf2_section_info abbrev;
3019eac3
DE
999 struct dwarf2_section_info line;
1000 struct dwarf2_section_info loc;
43988095 1001 struct dwarf2_section_info loclists;
09262596
DE
1002 struct dwarf2_section_info macinfo;
1003 struct dwarf2_section_info macro;
3019eac3
DE
1004 struct dwarf2_section_info str;
1005 struct dwarf2_section_info str_offsets;
80626a55
DE
1006 /* In the case of a virtual DWO file, these two are unused. */
1007 struct dwarf2_section_info info;
3019eac3
DE
1008 VEC (dwarf2_section_info_def) *types;
1009};
1010
c88ee1f0 1011/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
1012
1013struct dwo_unit
1014{
1015 /* Backlink to the containing struct dwo_file. */
1016 struct dwo_file *dwo_file;
1017
1018 /* The "id" that distinguishes this CU/TU.
1019 .debug_info calls this "dwo_id", .debug_types calls this "signature".
1020 Since signatures came first, we stick with it for consistency. */
1021 ULONGEST signature;
1022
1023 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 1024 struct dwarf2_section_info *section;
3019eac3 1025
9c541725
PA
1026 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
1027 sect_offset sect_off;
3019eac3
DE
1028 unsigned int length;
1029
1030 /* For types, offset in the type's DIE of the type defined by this TU. */
1031 cu_offset type_offset_in_tu;
1032};
1033
73869dc2
DE
1034/* include/dwarf2.h defines the DWP section codes.
1035 It defines a max value but it doesn't define a min value, which we
1036 use for error checking, so provide one. */
1037
1038enum dwp_v2_section_ids
1039{
1040 DW_SECT_MIN = 1
1041};
1042
80626a55 1043/* Data for one DWO file.
57d63ce2
DE
1044
1045 This includes virtual DWO files (a virtual DWO file is a DWO file as it
1046 appears in a DWP file). DWP files don't really have DWO files per se -
1047 comdat folding of types "loses" the DWO file they came from, and from
1048 a high level view DWP files appear to contain a mass of random types.
1049 However, to maintain consistency with the non-DWP case we pretend DWP
1050 files contain virtual DWO files, and we assign each TU with one virtual
1051 DWO file (generally based on the line and abbrev section offsets -
1052 a heuristic that seems to work in practice). */
3019eac3
DE
1053
1054struct dwo_file
1055{
0ac5b59e 1056 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
1057 For virtual DWO files the name is constructed from the section offsets
1058 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
1059 from related CU+TUs. */
0ac5b59e
DE
1060 const char *dwo_name;
1061
1062 /* The DW_AT_comp_dir attribute. */
1063 const char *comp_dir;
3019eac3 1064
80626a55
DE
1065 /* The bfd, when the file is open. Otherwise this is NULL.
1066 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
1067 bfd *dbfd;
3019eac3 1068
73869dc2
DE
1069 /* The sections that make up this DWO file.
1070 Remember that for virtual DWO files in DWP V2, these are virtual
1071 sections (for lack of a better name). */
3019eac3
DE
1072 struct dwo_sections sections;
1073
33c5cd75
DB
1074 /* The CUs in the file.
1075 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
1076 an extension to handle LLVM's Link Time Optimization output (where
1077 multiple source files may be compiled into a single object/dwo pair). */
1078 htab_t cus;
3019eac3
DE
1079
1080 /* Table of TUs in the file.
1081 Each element is a struct dwo_unit. */
1082 htab_t tus;
1083};
1084
80626a55
DE
1085/* These sections are what may appear in a DWP file. */
1086
1087struct dwp_sections
1088{
73869dc2 1089 /* These are used by both DWP version 1 and 2. */
80626a55
DE
1090 struct dwarf2_section_info str;
1091 struct dwarf2_section_info cu_index;
1092 struct dwarf2_section_info tu_index;
73869dc2
DE
1093
1094 /* These are only used by DWP version 2 files.
1095 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
1096 sections are referenced by section number, and are not recorded here.
1097 In DWP version 2 there is at most one copy of all these sections, each
1098 section being (effectively) comprised of the concatenation of all of the
1099 individual sections that exist in the version 1 format.
1100 To keep the code simple we treat each of these concatenated pieces as a
1101 section itself (a virtual section?). */
1102 struct dwarf2_section_info abbrev;
1103 struct dwarf2_section_info info;
1104 struct dwarf2_section_info line;
1105 struct dwarf2_section_info loc;
1106 struct dwarf2_section_info macinfo;
1107 struct dwarf2_section_info macro;
1108 struct dwarf2_section_info str_offsets;
1109 struct dwarf2_section_info types;
80626a55
DE
1110};
1111
73869dc2
DE
1112/* These sections are what may appear in a virtual DWO file in DWP version 1.
1113 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 1114
73869dc2 1115struct virtual_v1_dwo_sections
80626a55
DE
1116{
1117 struct dwarf2_section_info abbrev;
1118 struct dwarf2_section_info line;
1119 struct dwarf2_section_info loc;
1120 struct dwarf2_section_info macinfo;
1121 struct dwarf2_section_info macro;
1122 struct dwarf2_section_info str_offsets;
1123 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 1124 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
1125 struct dwarf2_section_info info_or_types;
1126};
1127
73869dc2
DE
1128/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
1129 In version 2, the sections of the DWO files are concatenated together
1130 and stored in one section of that name. Thus each ELF section contains
1131 several "virtual" sections. */
1132
1133struct virtual_v2_dwo_sections
1134{
1135 bfd_size_type abbrev_offset;
1136 bfd_size_type abbrev_size;
1137
1138 bfd_size_type line_offset;
1139 bfd_size_type line_size;
1140
1141 bfd_size_type loc_offset;
1142 bfd_size_type loc_size;
1143
1144 bfd_size_type macinfo_offset;
1145 bfd_size_type macinfo_size;
1146
1147 bfd_size_type macro_offset;
1148 bfd_size_type macro_size;
1149
1150 bfd_size_type str_offsets_offset;
1151 bfd_size_type str_offsets_size;
1152
1153 /* Each DWP hash table entry records one CU or one TU.
1154 That is recorded here, and copied to dwo_unit.section. */
1155 bfd_size_type info_or_types_offset;
1156 bfd_size_type info_or_types_size;
1157};
1158
80626a55
DE
1159/* Contents of DWP hash tables. */
1160
1161struct dwp_hash_table
1162{
73869dc2 1163 uint32_t version, nr_columns;
80626a55 1164 uint32_t nr_units, nr_slots;
73869dc2
DE
1165 const gdb_byte *hash_table, *unit_table;
1166 union
1167 {
1168 struct
1169 {
1170 const gdb_byte *indices;
1171 } v1;
1172 struct
1173 {
1174 /* This is indexed by column number and gives the id of the section
1175 in that column. */
1176#define MAX_NR_V2_DWO_SECTIONS \
1177 (1 /* .debug_info or .debug_types */ \
1178 + 1 /* .debug_abbrev */ \
1179 + 1 /* .debug_line */ \
1180 + 1 /* .debug_loc */ \
1181 + 1 /* .debug_str_offsets */ \
1182 + 1 /* .debug_macro or .debug_macinfo */)
1183 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1184 const gdb_byte *offsets;
1185 const gdb_byte *sizes;
1186 } v2;
1187 } section_pool;
80626a55
DE
1188};
1189
1190/* Data for one DWP file. */
1191
1192struct dwp_file
1193{
1194 /* Name of the file. */
1195 const char *name;
1196
73869dc2
DE
1197 /* File format version. */
1198 int version;
1199
93417882 1200 /* The bfd. */
80626a55
DE
1201 bfd *dbfd;
1202
1203 /* Section info for this file. */
1204 struct dwp_sections sections;
1205
57d63ce2 1206 /* Table of CUs in the file. */
80626a55
DE
1207 const struct dwp_hash_table *cus;
1208
1209 /* Table of TUs in the file. */
1210 const struct dwp_hash_table *tus;
1211
19ac8c2e
DE
1212 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1213 htab_t loaded_cus;
1214 htab_t loaded_tus;
80626a55 1215
73869dc2
DE
1216 /* Table to map ELF section numbers to their sections.
1217 This is only needed for the DWP V1 file format. */
80626a55
DE
1218 unsigned int num_sections;
1219 asection **elf_sections;
1220};
1221
36586728
TT
1222/* This represents a '.dwz' file. */
1223
1224struct dwz_file
1225{
1226 /* A dwz file can only contain a few sections. */
1227 struct dwarf2_section_info abbrev;
1228 struct dwarf2_section_info info;
1229 struct dwarf2_section_info str;
1230 struct dwarf2_section_info line;
1231 struct dwarf2_section_info macro;
2ec9a5e0 1232 struct dwarf2_section_info gdb_index;
927aa2e7 1233 struct dwarf2_section_info debug_names;
36586728
TT
1234
1235 /* The dwz's BFD. */
1236 bfd *dwz_bfd;
1237};
1238
0963b4bd
MS
1239/* Struct used to pass misc. parameters to read_die_and_children, et
1240 al. which are used for both .debug_info and .debug_types dies.
1241 All parameters here are unchanging for the life of the call. This
dee91e82 1242 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
1243
1244struct die_reader_specs
1245{
a32a8923 1246 /* The bfd of die_section. */
93311388
DE
1247 bfd* abfd;
1248
1249 /* The CU of the DIE we are parsing. */
1250 struct dwarf2_cu *cu;
1251
80626a55 1252 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
1253 struct dwo_file *dwo_file;
1254
dee91e82 1255 /* The section the die comes from.
3019eac3 1256 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
1257 struct dwarf2_section_info *die_section;
1258
1259 /* die_section->buffer. */
d521ce57 1260 const gdb_byte *buffer;
f664829e
DE
1261
1262 /* The end of the buffer. */
1263 const gdb_byte *buffer_end;
a2ce51a0
DE
1264
1265 /* The value of the DW_AT_comp_dir attribute. */
1266 const char *comp_dir;
685af9cd
TT
1267
1268 /* The abbreviation table to use when reading the DIEs. */
1269 struct abbrev_table *abbrev_table;
93311388
DE
1270};
1271
fd820528 1272/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 1273typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 1274 const gdb_byte *info_ptr,
dee91e82
DE
1275 struct die_info *comp_unit_die,
1276 int has_children,
1277 void *data);
1278
ecfb656c
PA
1279/* A 1-based directory index. This is a strong typedef to prevent
1280 accidentally using a directory index as a 0-based index into an
1281 array/vector. */
1282enum class dir_index : unsigned int {};
1283
1284/* Likewise, a 1-based file name index. */
1285enum class file_name_index : unsigned int {};
1286
52059ffd
TT
1287struct file_entry
1288{
fff8551c
PA
1289 file_entry () = default;
1290
ecfb656c 1291 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
1292 unsigned int mod_time_, unsigned int length_)
1293 : name (name_),
ecfb656c 1294 d_index (d_index_),
fff8551c
PA
1295 mod_time (mod_time_),
1296 length (length_)
1297 {}
1298
ecfb656c
PA
1299 /* Return the include directory at D_INDEX stored in LH. Returns
1300 NULL if D_INDEX is out of bounds. */
8c43009f
PA
1301 const char *include_dir (const line_header *lh) const;
1302
fff8551c
PA
1303 /* The file name. Note this is an observing pointer. The memory is
1304 owned by debug_line_buffer. */
1305 const char *name {};
1306
8c43009f 1307 /* The directory index (1-based). */
ecfb656c 1308 dir_index d_index {};
fff8551c
PA
1309
1310 unsigned int mod_time {};
1311
1312 unsigned int length {};
1313
1314 /* True if referenced by the Line Number Program. */
1315 bool included_p {};
1316
83769d0b 1317 /* The associated symbol table, if any. */
fff8551c 1318 struct symtab *symtab {};
52059ffd
TT
1319};
1320
debd256d
JB
1321/* The line number information for a compilation unit (found in the
1322 .debug_line section) begins with a "statement program header",
1323 which contains the following information. */
1324struct line_header
1325{
fff8551c
PA
1326 line_header ()
1327 : offset_in_dwz {}
1328 {}
1329
1330 /* Add an entry to the include directory table. */
1331 void add_include_dir (const char *include_dir);
1332
1333 /* Add an entry to the file name table. */
ecfb656c 1334 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
1335 unsigned int mod_time, unsigned int length);
1336
ecfb656c 1337 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
8c43009f 1338 is out of bounds. */
ecfb656c 1339 const char *include_dir_at (dir_index index) const
8c43009f 1340 {
ecfb656c
PA
1341 /* Convert directory index number (1-based) to vector index
1342 (0-based). */
1343 size_t vec_index = to_underlying (index) - 1;
1344
1345 if (vec_index >= include_dirs.size ())
8c43009f 1346 return NULL;
ecfb656c 1347 return include_dirs[vec_index];
8c43009f
PA
1348 }
1349
ecfb656c 1350 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
8c43009f 1351 is out of bounds. */
ecfb656c 1352 file_entry *file_name_at (file_name_index index)
8c43009f 1353 {
ecfb656c
PA
1354 /* Convert file name index number (1-based) to vector index
1355 (0-based). */
1356 size_t vec_index = to_underlying (index) - 1;
1357
1358 if (vec_index >= file_names.size ())
fff8551c 1359 return NULL;
ecfb656c 1360 return &file_names[vec_index];
fff8551c
PA
1361 }
1362
1363 /* Const version of the above. */
1364 const file_entry *file_name_at (unsigned int index) const
1365 {
1366 if (index >= file_names.size ())
8c43009f
PA
1367 return NULL;
1368 return &file_names[index];
1369 }
1370
527f3840 1371 /* Offset of line number information in .debug_line section. */
9c541725 1372 sect_offset sect_off {};
527f3840
JK
1373
1374 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1375 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1376
1377 unsigned int total_length {};
1378 unsigned short version {};
1379 unsigned int header_length {};
1380 unsigned char minimum_instruction_length {};
1381 unsigned char maximum_ops_per_instruction {};
1382 unsigned char default_is_stmt {};
1383 int line_base {};
1384 unsigned char line_range {};
1385 unsigned char opcode_base {};
debd256d
JB
1386
1387 /* standard_opcode_lengths[i] is the number of operands for the
1388 standard opcode whose value is i. This means that
1389 standard_opcode_lengths[0] is unused, and the last meaningful
1390 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1391 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1392
fff8551c
PA
1393 /* The include_directories table. Note these are observing
1394 pointers. The memory is owned by debug_line_buffer. */
1395 std::vector<const char *> include_dirs;
debd256d 1396
fff8551c
PA
1397 /* The file_names table. */
1398 std::vector<file_entry> file_names;
debd256d
JB
1399
1400 /* The start and end of the statement program following this
6502dd73 1401 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1402 const gdb_byte *statement_program_start {}, *statement_program_end {};
debd256d 1403};
c906108c 1404
fff8551c
PA
1405typedef std::unique_ptr<line_header> line_header_up;
1406
8c43009f
PA
1407const char *
1408file_entry::include_dir (const line_header *lh) const
1409{
ecfb656c 1410 return lh->include_dir_at (d_index);
8c43009f
PA
1411}
1412
c906108c 1413/* When we construct a partial symbol table entry we only
0963b4bd 1414 need this much information. */
6f06d47b 1415struct partial_die_info : public allocate_on_obstack
c906108c 1416 {
6f06d47b
YQ
1417 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1418
1419 /* Disable assign but still keep copy ctor, which is needed
1420 load_partial_dies. */
1421 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1422
52356b79
YQ
1423 /* Adjust the partial die before generating a symbol for it. This
1424 function may set the is_external flag or change the DIE's
1425 name. */
1426 void fixup (struct dwarf2_cu *cu);
1427
48fbe735
YQ
1428 /* Read a minimal amount of information into the minimal die
1429 structure. */
1430 const gdb_byte *read (const struct die_reader_specs *reader,
1431 const struct abbrev_info &abbrev,
1432 const gdb_byte *info_ptr);
1433
72bf9492 1434 /* Offset of this DIE. */
6f06d47b 1435 const sect_offset sect_off;
72bf9492
DJ
1436
1437 /* DWARF-2 tag for this DIE. */
6f06d47b 1438 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1439
72bf9492 1440 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1441 const unsigned int has_children : 1;
1442
72bf9492
DJ
1443 unsigned int is_external : 1;
1444 unsigned int is_declaration : 1;
1445 unsigned int has_type : 1;
1446 unsigned int has_specification : 1;
1447 unsigned int has_pc_info : 1;
481860b3 1448 unsigned int may_be_inlined : 1;
72bf9492 1449
0c1b455e
TT
1450 /* This DIE has been marked DW_AT_main_subprogram. */
1451 unsigned int main_subprogram : 1;
1452
72bf9492
DJ
1453 /* Flag set if the SCOPE field of this structure has been
1454 computed. */
1455 unsigned int scope_set : 1;
1456
fa4028e9
JB
1457 /* Flag set if the DIE has a byte_size attribute. */
1458 unsigned int has_byte_size : 1;
1459
ff908ebf
AW
1460 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1461 unsigned int has_const_value : 1;
1462
98bfdba5
PA
1463 /* Flag set if any of the DIE's children are template arguments. */
1464 unsigned int has_template_arguments : 1;
1465
52356b79 1466 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1467 unsigned int fixup_called : 1;
1468
36586728
TT
1469 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1470 unsigned int is_dwz : 1;
1471
1472 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1473 unsigned int spec_is_dwz : 1;
1474
72bf9492 1475 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1476 sometimes a default name for unnamed DIEs. */
6f06d47b 1477 const char *name = nullptr;
72bf9492 1478
abc72ce4 1479 /* The linkage name, if present. */
6f06d47b 1480 const char *linkage_name = nullptr;
abc72ce4 1481
72bf9492
DJ
1482 /* The scope to prepend to our children. This is generally
1483 allocated on the comp_unit_obstack, so will disappear
1484 when this compilation unit leaves the cache. */
6f06d47b 1485 const char *scope = nullptr;
72bf9492 1486
95554aad
TT
1487 /* Some data associated with the partial DIE. The tag determines
1488 which field is live. */
1489 union
1490 {
1491 /* The location description associated with this DIE, if any. */
1492 struct dwarf_block *locdesc;
1493 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1494 sect_offset sect_off;
6f06d47b 1495 } d {};
72bf9492
DJ
1496
1497 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1498 CORE_ADDR lowpc = 0;
1499 CORE_ADDR highpc = 0;
72bf9492 1500
93311388 1501 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1502 DW_AT_sibling, if any. */
48fbe735
YQ
1503 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1504 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1505 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1506
1507 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1508 DW_AT_specification (or DW_AT_abstract_origin or
1509 DW_AT_extension). */
6f06d47b 1510 sect_offset spec_offset {};
72bf9492
DJ
1511
1512 /* Pointers to this DIE's parent, first child, and next sibling,
1513 if any. */
6f06d47b
YQ
1514 struct partial_die_info *die_parent = nullptr;
1515 struct partial_die_info *die_child = nullptr;
1516 struct partial_die_info *die_sibling = nullptr;
1517
1518 friend struct partial_die_info *
1519 dwarf2_cu::find_partial_die (sect_offset sect_off);
1520
1521 private:
1522 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1523 partial_die_info (sect_offset sect_off)
1524 : partial_die_info (sect_off, DW_TAG_padding, 0)
1525 {
1526 }
1527
1528 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1529 int has_children_)
1530 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1531 {
1532 is_external = 0;
1533 is_declaration = 0;
1534 has_type = 0;
1535 has_specification = 0;
1536 has_pc_info = 0;
1537 may_be_inlined = 0;
1538 main_subprogram = 0;
1539 scope_set = 0;
1540 has_byte_size = 0;
1541 has_const_value = 0;
1542 has_template_arguments = 0;
1543 fixup_called = 0;
1544 is_dwz = 0;
1545 spec_is_dwz = 0;
1546 }
c906108c
SS
1547 };
1548
0963b4bd 1549/* This data structure holds the information of an abbrev. */
c906108c
SS
1550struct abbrev_info
1551 {
1552 unsigned int number; /* number identifying abbrev */
1553 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1554 unsigned short has_children; /* boolean */
1555 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1556 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1557 struct abbrev_info *next; /* next in chain */
1558 };
1559
1560struct attr_abbrev
1561 {
9d25dd43
DE
1562 ENUM_BITFIELD(dwarf_attribute) name : 16;
1563 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1564
1565 /* It is valid only if FORM is DW_FORM_implicit_const. */
1566 LONGEST implicit_const;
c906108c
SS
1567 };
1568
433df2d4
DE
1569/* Size of abbrev_table.abbrev_hash_table. */
1570#define ABBREV_HASH_SIZE 121
1571
1572/* Top level data structure to contain an abbreviation table. */
1573
1574struct abbrev_table
1575{
685af9cd
TT
1576 explicit abbrev_table (sect_offset off)
1577 : sect_off (off)
1578 {
4a17f768 1579 m_abbrevs =
685af9cd 1580 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
4a17f768 1581 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
685af9cd
TT
1582 }
1583
1584 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1585
1586 /* Allocate space for a struct abbrev_info object in
1587 ABBREV_TABLE. */
1588 struct abbrev_info *alloc_abbrev ();
1589
1590 /* Add an abbreviation to the table. */
1591 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1592
1593 /* Look up an abbrev in the table.
1594 Returns NULL if the abbrev is not found. */
1595
1596 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1597
1598
f4dc4d17
DE
1599 /* Where the abbrev table came from.
1600 This is used as a sanity check when the table is used. */
685af9cd 1601 const sect_offset sect_off;
433df2d4
DE
1602
1603 /* Storage for the abbrev table. */
685af9cd 1604 auto_obstack abbrev_obstack;
433df2d4 1605
4a17f768
YQ
1606private:
1607
433df2d4
DE
1608 /* Hash table of abbrevs.
1609 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1610 It could be statically allocated, but the previous code didn't so we
1611 don't either. */
4a17f768 1612 struct abbrev_info **m_abbrevs;
433df2d4
DE
1613};
1614
685af9cd
TT
1615typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1616
0963b4bd 1617/* Attributes have a name and a value. */
b60c80d6
DJ
1618struct attribute
1619 {
9d25dd43 1620 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1621 ENUM_BITFIELD(dwarf_form) form : 15;
1622
1623 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1624 field should be in u.str (existing only for DW_STRING) but it is kept
1625 here for better struct attribute alignment. */
1626 unsigned int string_is_canonical : 1;
1627
b60c80d6
DJ
1628 union
1629 {
15d034d0 1630 const char *str;
b60c80d6 1631 struct dwarf_block *blk;
43bbcdc2
PH
1632 ULONGEST unsnd;
1633 LONGEST snd;
b60c80d6 1634 CORE_ADDR addr;
ac9ec31b 1635 ULONGEST signature;
b60c80d6
DJ
1636 }
1637 u;
1638 };
1639
0963b4bd 1640/* This data structure holds a complete die structure. */
c906108c
SS
1641struct die_info
1642 {
76815b17
DE
1643 /* DWARF-2 tag for this DIE. */
1644 ENUM_BITFIELD(dwarf_tag) tag : 16;
1645
1646 /* Number of attributes */
98bfdba5
PA
1647 unsigned char num_attrs;
1648
1649 /* True if we're presently building the full type name for the
1650 type derived from this DIE. */
1651 unsigned char building_fullname : 1;
76815b17 1652
adde2bff
DE
1653 /* True if this die is in process. PR 16581. */
1654 unsigned char in_process : 1;
1655
76815b17
DE
1656 /* Abbrev number */
1657 unsigned int abbrev;
1658
93311388 1659 /* Offset in .debug_info or .debug_types section. */
9c541725 1660 sect_offset sect_off;
78ba4af6
JB
1661
1662 /* The dies in a compilation unit form an n-ary tree. PARENT
1663 points to this die's parent; CHILD points to the first child of
1664 this node; and all the children of a given node are chained
4950bc1c 1665 together via their SIBLING fields. */
639d11d3
DC
1666 struct die_info *child; /* Its first child, if any. */
1667 struct die_info *sibling; /* Its next sibling, if any. */
1668 struct die_info *parent; /* Its parent, if any. */
c906108c 1669
b60c80d6
DJ
1670 /* An array of attributes, with NUM_ATTRS elements. There may be
1671 zero, but it's not common and zero-sized arrays are not
1672 sufficiently portable C. */
1673 struct attribute attrs[1];
c906108c
SS
1674 };
1675
0963b4bd 1676/* Get at parts of an attribute structure. */
c906108c
SS
1677
1678#define DW_STRING(attr) ((attr)->u.str)
8285870a 1679#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1680#define DW_UNSND(attr) ((attr)->u.unsnd)
1681#define DW_BLOCK(attr) ((attr)->u.blk)
1682#define DW_SND(attr) ((attr)->u.snd)
1683#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1684#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1685
0963b4bd 1686/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1687struct dwarf_block
1688 {
56eb65bd 1689 size_t size;
1d6edc3c
JK
1690
1691 /* Valid only if SIZE is not zero. */
d521ce57 1692 const gdb_byte *data;
c906108c
SS
1693 };
1694
c906108c
SS
1695#ifndef ATTR_ALLOC_CHUNK
1696#define ATTR_ALLOC_CHUNK 4
1697#endif
1698
c906108c
SS
1699/* Allocate fields for structs, unions and enums in this size. */
1700#ifndef DW_FIELD_ALLOC_CHUNK
1701#define DW_FIELD_ALLOC_CHUNK 4
1702#endif
1703
c906108c
SS
1704/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1705 but this would require a corresponding change in unpack_field_as_long
1706 and friends. */
1707static int bits_per_byte = 8;
1708
2ddeaf8a
TT
1709/* When reading a variant or variant part, we track a bit more
1710 information about the field, and store it in an object of this
1711 type. */
1712
1713struct variant_field
1714{
1715 /* If we see a DW_TAG_variant, then this will be the discriminant
1716 value. */
1717 ULONGEST discriminant_value;
1718 /* If we see a DW_TAG_variant, then this will be set if this is the
1719 default branch. */
1720 bool default_branch;
1721 /* While reading a DW_TAG_variant_part, this will be set if this
1722 field is the discriminant. */
1723 bool is_discriminant;
1724};
1725
52059ffd
TT
1726struct nextfield
1727{
1728 struct nextfield *next;
1729 int accessibility;
1730 int virtuality;
2ddeaf8a
TT
1731 /* Extra information to describe a variant or variant part. */
1732 struct variant_field variant;
52059ffd
TT
1733 struct field field;
1734};
1735
1736struct nextfnfield
1737{
1738 struct nextfnfield *next;
1739 struct fn_field fnfield;
1740};
1741
1742struct fnfieldlist
1743{
1744 const char *name;
1745 int length;
1746 struct nextfnfield *head;
1747};
1748
883fd55a 1749struct decl_field_list
52059ffd 1750{
883fd55a
KS
1751 struct decl_field field;
1752 struct decl_field_list *next;
52059ffd
TT
1753};
1754
c906108c
SS
1755/* The routines that read and process dies for a C struct or C++ class
1756 pass lists of data member fields and lists of member function fields
1757 in an instance of a field_info structure, as defined below. */
1758struct field_info
c5aa993b 1759 {
0963b4bd 1760 /* List of data member and baseclasses fields. */
52059ffd 1761 struct nextfield *fields, *baseclasses;
c906108c 1762
7d0ccb61 1763 /* Number of fields (including baseclasses). */
c5aa993b 1764 int nfields;
c906108c 1765
c5aa993b
JM
1766 /* Number of baseclasses. */
1767 int nbaseclasses;
c906108c 1768
c5aa993b
JM
1769 /* Set if the accesibility of one of the fields is not public. */
1770 int non_public_fields;
c906108c 1771
c5aa993b
JM
1772 /* Member function fieldlist array, contains name of possibly overloaded
1773 member function, number of overloaded member functions and a pointer
1774 to the head of the member function field chain. */
52059ffd 1775 struct fnfieldlist *fnfieldlists;
c906108c 1776
c5aa993b
JM
1777 /* Number of entries in the fnfieldlists array. */
1778 int nfnfields;
98751a41
JK
1779
1780 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1781 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
883fd55a 1782 struct decl_field_list *typedef_field_list;
98751a41 1783 unsigned typedef_field_list_count;
883fd55a
KS
1784
1785 /* Nested types defined by this class and the number of elements in this
1786 list. */
1787 struct decl_field_list *nested_types_list;
1788 unsigned nested_types_list_count;
c5aa993b 1789 };
c906108c 1790
10b3939b
DJ
1791/* One item on the queue of compilation units to read in full symbols
1792 for. */
1793struct dwarf2_queue_item
1794{
1795 struct dwarf2_per_cu_data *per_cu;
95554aad 1796 enum language pretend_language;
10b3939b
DJ
1797 struct dwarf2_queue_item *next;
1798};
1799
1800/* The current queue. */
1801static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1802
ae038cb0
DJ
1803/* Loaded secondary compilation units are kept in memory until they
1804 have not been referenced for the processing of this many
1805 compilation units. Set this to zero to disable caching. Cache
1806 sizes of up to at least twenty will improve startup time for
1807 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1808static int dwarf_max_cache_age = 5;
920d2a44 1809static void
b4f54984
DE
1810show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1811 struct cmd_list_element *c, const char *value)
920d2a44 1812{
3e43a32a 1813 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1814 "DWARF compilation units is %s.\n"),
920d2a44
AC
1815 value);
1816}
4390d890 1817\f
c906108c
SS
1818/* local function prototypes */
1819
a32a8923
DE
1820static const char *get_section_name (const struct dwarf2_section_info *);
1821
1822static const char *get_section_file_name (const struct dwarf2_section_info *);
1823
918dd910
JK
1824static void dwarf2_find_base_address (struct die_info *die,
1825 struct dwarf2_cu *cu);
1826
0018ea6f
DE
1827static struct partial_symtab *create_partial_symtab
1828 (struct dwarf2_per_cu_data *per_cu, const char *name);
1829
f1902523
JK
1830static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1831 const gdb_byte *info_ptr,
1832 struct die_info *type_unit_die,
1833 int has_children, void *data);
1834
ed2dc618
SM
1835static void dwarf2_build_psymtabs_hard
1836 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1837
72bf9492
DJ
1838static void scan_partial_symbols (struct partial_die_info *,
1839 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1840 int, struct dwarf2_cu *);
c906108c 1841
72bf9492
DJ
1842static void add_partial_symbol (struct partial_die_info *,
1843 struct dwarf2_cu *);
63d06c5c 1844
72bf9492
DJ
1845static void add_partial_namespace (struct partial_die_info *pdi,
1846 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1847 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1848
5d7cb8df 1849static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1850 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1851 struct dwarf2_cu *cu);
1852
72bf9492
DJ
1853static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1854 struct dwarf2_cu *cu);
91c24f0a 1855
bc30ff58
JB
1856static void add_partial_subprogram (struct partial_die_info *pdi,
1857 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1858 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1859
257e7a09
YQ
1860static void dwarf2_read_symtab (struct partial_symtab *,
1861 struct objfile *);
c906108c 1862
a14ed312 1863static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1864
685af9cd 1865static abbrev_table_up abbrev_table_read_table
ed2dc618
SM
1866 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1867 sect_offset);
433df2d4 1868
d521ce57 1869static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1870
dee91e82 1871static struct partial_die_info *load_partial_dies
d521ce57 1872 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1873
36586728 1874static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1875 struct dwarf2_cu *);
72bf9492 1876
d521ce57
TT
1877static const gdb_byte *read_attribute (const struct die_reader_specs *,
1878 struct attribute *, struct attr_abbrev *,
1879 const gdb_byte *);
a8329558 1880
a1855c1d 1881static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1882
a1855c1d 1883static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1884
a1855c1d 1885static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1886
a1855c1d 1887static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1888
a1855c1d 1889static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1890
d521ce57 1891static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1892 unsigned int *);
c906108c 1893
d521ce57 1894static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1895
1896static LONGEST read_checked_initial_length_and_offset
d521ce57 1897 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1898 unsigned int *, unsigned int *);
613e1657 1899
d521ce57
TT
1900static LONGEST read_offset (bfd *, const gdb_byte *,
1901 const struct comp_unit_head *,
c764a876
DE
1902 unsigned int *);
1903
d521ce57 1904static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1905
ed2dc618
SM
1906static sect_offset read_abbrev_offset
1907 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1908 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1909
d521ce57 1910static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1911
d521ce57 1912static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1913
ed2dc618
SM
1914static const char *read_indirect_string
1915 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1916 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1917
ed2dc618
SM
1918static const char *read_indirect_line_string
1919 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1920 const struct comp_unit_head *, unsigned int *);
36586728 1921
ed2dc618
SM
1922static const char *read_indirect_string_at_offset
1923 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1924 LONGEST str_offset);
927aa2e7 1925
ed2dc618
SM
1926static const char *read_indirect_string_from_dwz
1927 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1928
d521ce57 1929static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1930
d521ce57
TT
1931static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1932 const gdb_byte *,
3019eac3
DE
1933 unsigned int *);
1934
d521ce57 1935static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1936 ULONGEST str_index);
3019eac3 1937
e142c38c 1938static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1939
e142c38c
DJ
1940static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1941 struct dwarf2_cu *);
c906108c 1942
348e048f 1943static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1944 unsigned int);
348e048f 1945
7d45c7c3
KB
1946static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1947 struct dwarf2_cu *cu);
1948
05cf31d1
JB
1949static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1950 struct dwarf2_cu *cu);
1951
e142c38c 1952static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1953
e142c38c 1954static struct die_info *die_specification (struct die_info *die,
f2f0e013 1955 struct dwarf2_cu **);
63d06c5c 1956
9c541725 1957static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1958 struct dwarf2_cu *cu);
debd256d 1959
f3f5162e 1960static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1961 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1962 CORE_ADDR, int decode_mapping);
c906108c 1963
4d663531 1964static void dwarf2_start_subfile (const char *, const char *);
c906108c 1965
43f3e411
DE
1966static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1967 const char *, const char *,
1968 CORE_ADDR);
f4dc4d17 1969
a14ed312 1970static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1971 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1972
ff39bb5e 1973static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1974 struct dwarf2_cu *);
c906108c 1975
ff39bb5e 1976static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1977 struct type *type,
1978 const char *name,
1979 struct obstack *obstack,
12df843f 1980 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1981 const gdb_byte **bytes,
98bfdba5 1982 struct dwarf2_locexpr_baton **baton);
2df3850c 1983
e7c27a73 1984static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1985
b4ba55a1
JB
1986static int need_gnat_info (struct dwarf2_cu *);
1987
3e43a32a
MS
1988static struct type *die_descriptive_type (struct die_info *,
1989 struct dwarf2_cu *);
b4ba55a1
JB
1990
1991static void set_descriptive_type (struct type *, struct die_info *,
1992 struct dwarf2_cu *);
1993
e7c27a73
DJ
1994static struct type *die_containing_type (struct die_info *,
1995 struct dwarf2_cu *);
c906108c 1996
ff39bb5e 1997static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1998 struct dwarf2_cu *);
c906108c 1999
f792889a 2000static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 2001
673bfd45
DE
2002static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
2003
0d5cff50 2004static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 2005
6e70227d 2006static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
2007 const char *suffix, int physname,
2008 struct dwarf2_cu *cu);
63d06c5c 2009
e7c27a73 2010static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 2011
348e048f
DE
2012static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
2013
e7c27a73 2014static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 2015
e7c27a73 2016static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 2017
96408a79
SA
2018static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
2019
71a3c369
TT
2020static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
2021
ff013f42
JK
2022static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
2023 struct dwarf2_cu *, struct partial_symtab *);
2024
3a2b436a 2025/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 2026 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
2027enum pc_bounds_kind
2028{
e385593e 2029 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
2030 PC_BOUNDS_NOT_PRESENT,
2031
e385593e
JK
2032 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
2033 were present but they do not form a valid range of PC addresses. */
2034 PC_BOUNDS_INVALID,
2035
3a2b436a
JK
2036 /* Discontiguous range was found - that is DW_AT_ranges was found. */
2037 PC_BOUNDS_RANGES,
2038
2039 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
2040 PC_BOUNDS_HIGH_LOW,
2041};
2042
2043static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
2044 CORE_ADDR *, CORE_ADDR *,
2045 struct dwarf2_cu *,
2046 struct partial_symtab *);
c906108c 2047
fae299cd
DC
2048static void get_scope_pc_bounds (struct die_info *,
2049 CORE_ADDR *, CORE_ADDR *,
2050 struct dwarf2_cu *);
2051
801e3a5b
JB
2052static void dwarf2_record_block_ranges (struct die_info *, struct block *,
2053 CORE_ADDR, struct dwarf2_cu *);
2054
a14ed312 2055static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 2056 struct dwarf2_cu *);
c906108c 2057
a14ed312 2058static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 2059 struct type *, struct dwarf2_cu *);
c906108c 2060
a14ed312 2061static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 2062 struct die_info *, struct type *,
e7c27a73 2063 struct dwarf2_cu *);
c906108c 2064
a14ed312 2065static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
2066 struct type *,
2067 struct dwarf2_cu *);
c906108c 2068
134d01f1 2069static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 2070
e7c27a73 2071static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 2072
e7c27a73 2073static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 2074
5d7cb8df
JK
2075static void read_module (struct die_info *die, struct dwarf2_cu *cu);
2076
22cee43f
PMR
2077static struct using_direct **using_directives (enum language);
2078
27aa8d6a
SW
2079static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
2080
74921315
KS
2081static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
2082
f55ee35c
JK
2083static struct type *read_module_type (struct die_info *die,
2084 struct dwarf2_cu *cu);
2085
38d518c9 2086static const char *namespace_name (struct die_info *die,
e142c38c 2087 int *is_anonymous, struct dwarf2_cu *);
38d518c9 2088
134d01f1 2089static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 2090
e7c27a73 2091static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 2092
6e70227d 2093static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
2094 struct dwarf2_cu *);
2095
bf6af496 2096static struct die_info *read_die_and_siblings_1
d521ce57 2097 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 2098 struct die_info *);
639d11d3 2099
dee91e82 2100static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
2101 const gdb_byte *info_ptr,
2102 const gdb_byte **new_info_ptr,
639d11d3
DC
2103 struct die_info *parent);
2104
d521ce57
TT
2105static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
2106 struct die_info **, const gdb_byte *,
2107 int *, int);
3019eac3 2108
d521ce57
TT
2109static const gdb_byte *read_full_die (const struct die_reader_specs *,
2110 struct die_info **, const gdb_byte *,
2111 int *);
93311388 2112
e7c27a73 2113static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 2114
15d034d0
TT
2115static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
2116 struct obstack *);
71c25dea 2117
15d034d0 2118static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 2119
15d034d0 2120static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
2121 struct die_info *die,
2122 struct dwarf2_cu *cu);
2123
ca69b9e6
DE
2124static const char *dwarf2_physname (const char *name, struct die_info *die,
2125 struct dwarf2_cu *cu);
2126
e142c38c 2127static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 2128 struct dwarf2_cu **);
9219021c 2129
f39c6ffd 2130static const char *dwarf_tag_name (unsigned int);
c906108c 2131
f39c6ffd 2132static const char *dwarf_attr_name (unsigned int);
c906108c 2133
f39c6ffd 2134static const char *dwarf_form_name (unsigned int);
c906108c 2135
a121b7c1 2136static const char *dwarf_bool_name (unsigned int);
c906108c 2137
f39c6ffd 2138static const char *dwarf_type_encoding_name (unsigned int);
c906108c 2139
f9aca02d 2140static struct die_info *sibling_die (struct die_info *);
c906108c 2141
d97bc12b
DE
2142static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
2143
2144static void dump_die_for_error (struct die_info *);
2145
2146static void dump_die_1 (struct ui_file *, int level, int max_level,
2147 struct die_info *);
c906108c 2148
d97bc12b 2149/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 2150
51545339 2151static void store_in_ref_table (struct die_info *,
10b3939b 2152 struct dwarf2_cu *);
c906108c 2153
ff39bb5e 2154static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 2155
ff39bb5e 2156static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 2157
348e048f 2158static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 2159 const struct attribute *,
348e048f
DE
2160 struct dwarf2_cu **);
2161
10b3939b 2162static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 2163 const struct attribute *,
f2f0e013 2164 struct dwarf2_cu **);
c906108c 2165
348e048f 2166static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 2167 const struct attribute *,
348e048f
DE
2168 struct dwarf2_cu **);
2169
ac9ec31b
DE
2170static struct type *get_signatured_type (struct die_info *, ULONGEST,
2171 struct dwarf2_cu *);
2172
2173static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 2174 const struct attribute *,
ac9ec31b
DE
2175 struct dwarf2_cu *);
2176
e5fe5e75 2177static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 2178
52dc124a 2179static void read_signatured_type (struct signatured_type *);
348e048f 2180
63e43d3a
PMR
2181static int attr_to_dynamic_prop (const struct attribute *attr,
2182 struct die_info *die, struct dwarf2_cu *cu,
2183 struct dynamic_prop *prop);
2184
c906108c
SS
2185/* memory allocation interface */
2186
7b5a2f43 2187static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 2188
b60c80d6 2189static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 2190
43f3e411 2191static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 2192
6e5a29e1 2193static int attr_form_is_block (const struct attribute *);
8e19ed76 2194
6e5a29e1 2195static int attr_form_is_section_offset (const struct attribute *);
3690dd37 2196
6e5a29e1 2197static int attr_form_is_constant (const struct attribute *);
3690dd37 2198
6e5a29e1 2199static int attr_form_is_ref (const struct attribute *);
7771576e 2200
8cf6f0b1
TT
2201static void fill_in_loclist_baton (struct dwarf2_cu *cu,
2202 struct dwarf2_loclist_baton *baton,
ff39bb5e 2203 const struct attribute *attr);
8cf6f0b1 2204
ff39bb5e 2205static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 2206 struct symbol *sym,
f1e6e072
TT
2207 struct dwarf2_cu *cu,
2208 int is_block);
4c2df51b 2209
d521ce57
TT
2210static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
2211 const gdb_byte *info_ptr,
2212 struct abbrev_info *abbrev);
4bb7a0a7 2213
72bf9492
DJ
2214static hashval_t partial_die_hash (const void *item);
2215
2216static int partial_die_eq (const void *item_lhs, const void *item_rhs);
2217
ae038cb0 2218static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
2219 (sect_offset sect_off, unsigned int offset_in_dwz,
2220 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 2221
9816fde3 2222static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
2223 struct die_info *comp_unit_die,
2224 enum language pretend_language);
93311388 2225
ae038cb0
DJ
2226static void free_cached_comp_units (void *);
2227
ed2dc618 2228static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 2229
dee91e82 2230static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 2231
f792889a
DJ
2232static struct type *set_die_type (struct die_info *, struct type *,
2233 struct dwarf2_cu *);
1c379e20 2234
ed2dc618 2235static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 2236
ed2dc618 2237static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 2238
95554aad
TT
2239static void load_full_comp_unit (struct dwarf2_per_cu_data *,
2240 enum language);
10b3939b 2241
95554aad
TT
2242static void process_full_comp_unit (struct dwarf2_per_cu_data *,
2243 enum language);
10b3939b 2244
f4dc4d17
DE
2245static void process_full_type_unit (struct dwarf2_per_cu_data *,
2246 enum language);
2247
10b3939b
DJ
2248static void dwarf2_add_dependence (struct dwarf2_cu *,
2249 struct dwarf2_per_cu_data *);
2250
ae038cb0
DJ
2251static void dwarf2_mark (struct dwarf2_cu *);
2252
2253static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
2254
b64f50a1 2255static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 2256 struct dwarf2_per_cu_data *);
673bfd45 2257
f792889a 2258static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 2259
95554aad
TT
2260static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
2261 enum language pretend_language);
2262
ed2dc618 2263static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 2264
b303c6f6
AB
2265/* Class, the destructor of which frees all allocated queue entries. This
2266 will only have work to do if an error was thrown while processing the
2267 dwarf. If no error was thrown then the queue entries should have all
2268 been processed, and freed, as we went along. */
2269
2270class dwarf2_queue_guard
2271{
2272public:
2273 dwarf2_queue_guard () = default;
2274
2275 /* Free any entries remaining on the queue. There should only be
2276 entries left if we hit an error while processing the dwarf. */
2277 ~dwarf2_queue_guard ()
2278 {
2279 struct dwarf2_queue_item *item, *last;
2280
2281 item = dwarf2_queue;
2282 while (item)
2283 {
2284 /* Anything still marked queued is likely to be in an
2285 inconsistent state, so discard it. */
2286 if (item->per_cu->queued)
2287 {
2288 if (item->per_cu->cu != NULL)
2289 free_one_cached_comp_unit (item->per_cu);
2290 item->per_cu->queued = 0;
2291 }
2292
2293 last = item;
2294 item = item->next;
2295 xfree (last);
2296 }
2297
2298 dwarf2_queue = dwarf2_queue_tail = NULL;
2299 }
2300};
2301
d721ba37
PA
2302/* The return type of find_file_and_directory. Note, the enclosed
2303 string pointers are only valid while this object is valid. */
2304
2305struct file_and_directory
2306{
2307 /* The filename. This is never NULL. */
2308 const char *name;
2309
2310 /* The compilation directory. NULL if not known. If we needed to
2311 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2312 points directly to the DW_AT_comp_dir string attribute owned by
2313 the obstack that owns the DIE. */
2314 const char *comp_dir;
2315
2316 /* If we needed to build a new string for comp_dir, this is what
2317 owns the storage. */
2318 std::string comp_dir_storage;
2319};
2320
2321static file_and_directory find_file_and_directory (struct die_info *die,
2322 struct dwarf2_cu *cu);
9291a0cd
TT
2323
2324static char *file_full_name (int file, struct line_header *lh,
2325 const char *comp_dir);
2326
43988095
JK
2327/* Expected enum dwarf_unit_type for read_comp_unit_head. */
2328enum class rcuh_kind { COMPILE, TYPE };
2329
d521ce57 2330static const gdb_byte *read_and_check_comp_unit_head
ed2dc618
SM
2331 (struct dwarf2_per_objfile* dwarf2_per_objfile,
2332 struct comp_unit_head *header,
36586728 2333 struct dwarf2_section_info *section,
d521ce57 2334 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 2335 rcuh_kind section_kind);
36586728 2336
fd820528 2337static void init_cutu_and_read_dies
f4dc4d17
DE
2338 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2339 int use_existing_cu, int keep,
3019eac3
DE
2340 die_reader_func_ftype *die_reader_func, void *data);
2341
dee91e82
DE
2342static void init_cutu_and_read_dies_simple
2343 (struct dwarf2_per_cu_data *this_cu,
2344 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 2345
673bfd45 2346static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 2347
3019eac3
DE
2348static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2349
57d63ce2 2350static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
2351 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2352 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 2353 ULONGEST signature, int is_debug_types);
a2ce51a0 2354
ed2dc618
SM
2355static struct dwp_file *get_dwp_file
2356 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 2357
3019eac3 2358static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 2359 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
2360
2361static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 2362 (struct signatured_type *, const char *, const char *);
3019eac3 2363
89e63ee4
DE
2364static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2365
3019eac3
DE
2366static void free_dwo_file_cleanup (void *);
2367
ed2dc618
SM
2368struct free_dwo_file_cleanup_data
2369{
2370 struct dwo_file *dwo_file;
2371 struct dwarf2_per_objfile *dwarf2_per_objfile;
2372};
2373
2374static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 2375
1b80a9fa 2376static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2377
2378static void free_line_header_voidp (void *arg);
4390d890
DE
2379\f
2380/* Various complaints about symbol reading that don't abort the process. */
2381
2382static void
2383dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2384{
2385 complaint (&symfile_complaints,
2386 _("statement list doesn't fit in .debug_line section"));
2387}
2388
2389static void
2390dwarf2_debug_line_missing_file_complaint (void)
2391{
2392 complaint (&symfile_complaints,
2393 _(".debug_line section has line data without a file"));
2394}
2395
2396static void
2397dwarf2_debug_line_missing_end_sequence_complaint (void)
2398{
2399 complaint (&symfile_complaints,
2400 _(".debug_line section has line "
2401 "program sequence without an end"));
2402}
2403
2404static void
2405dwarf2_complex_location_expr_complaint (void)
2406{
2407 complaint (&symfile_complaints, _("location expression too complex"));
2408}
2409
2410static void
2411dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2412 int arg3)
2413{
2414 complaint (&symfile_complaints,
2415 _("const value length mismatch for '%s', got %d, expected %d"),
2416 arg1, arg2, arg3);
2417}
2418
2419static void
2420dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2421{
2422 complaint (&symfile_complaints,
2423 _("debug info runs off end of %s section"
2424 " [in module %s]"),
a32a8923
DE
2425 get_section_name (section),
2426 get_section_file_name (section));
4390d890 2427}
1b80a9fa 2428
4390d890
DE
2429static void
2430dwarf2_macro_malformed_definition_complaint (const char *arg1)
2431{
2432 complaint (&symfile_complaints,
2433 _("macro debug info contains a "
2434 "malformed macro definition:\n`%s'"),
2435 arg1);
2436}
2437
2438static void
2439dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2440{
2441 complaint (&symfile_complaints,
2442 _("invalid attribute class or form for '%s' in '%s'"),
2443 arg1, arg2);
2444}
527f3840
JK
2445
2446/* Hash function for line_header_hash. */
2447
2448static hashval_t
2449line_header_hash (const struct line_header *ofs)
2450{
9c541725 2451 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2452}
2453
2454/* Hash function for htab_create_alloc_ex for line_header_hash. */
2455
2456static hashval_t
2457line_header_hash_voidp (const void *item)
2458{
9a3c8263 2459 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2460
2461 return line_header_hash (ofs);
2462}
2463
2464/* Equality function for line_header_hash. */
2465
2466static int
2467line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2468{
9a3c8263
SM
2469 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2470 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2471
9c541725 2472 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2473 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2474}
2475
4390d890 2476\f
9291a0cd 2477
31aa7e4e
JB
2478/* Read the given attribute value as an address, taking the attribute's
2479 form into account. */
2480
2481static CORE_ADDR
2482attr_value_as_address (struct attribute *attr)
2483{
2484 CORE_ADDR addr;
2485
2486 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2487 {
2488 /* Aside from a few clearly defined exceptions, attributes that
2489 contain an address must always be in DW_FORM_addr form.
2490 Unfortunately, some compilers happen to be violating this
2491 requirement by encoding addresses using other forms, such
2492 as DW_FORM_data4 for example. For those broken compilers,
2493 we try to do our best, without any guarantee of success,
2494 to interpret the address correctly. It would also be nice
2495 to generate a complaint, but that would require us to maintain
2496 a list of legitimate cases where a non-address form is allowed,
2497 as well as update callers to pass in at least the CU's DWARF
2498 version. This is more overhead than what we're willing to
2499 expand for a pretty rare case. */
2500 addr = DW_UNSND (attr);
2501 }
2502 else
2503 addr = DW_ADDR (attr);
2504
2505 return addr;
2506}
2507
9291a0cd 2508/* The suffix for an index file. */
437afbb8
JK
2509#define INDEX4_SUFFIX ".gdb-index"
2510#define INDEX5_SUFFIX ".debug_names"
2511#define DEBUG_STR_SUFFIX ".debug_str"
9291a0cd 2512
330cdd98
PA
2513/* See declaration. */
2514
2515dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2516 const dwarf2_debug_sections *names)
2517 : objfile (objfile_)
2518{
2519 if (names == NULL)
2520 names = &dwarf2_elf_names;
2521
2522 bfd *obfd = objfile->obfd;
2523
2524 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2525 locate_sections (obfd, sec, *names);
2526}
2527
fc8e7e75
SM
2528static void free_dwo_files (htab_t dwo_files, struct objfile *objfile);
2529
330cdd98
PA
2530dwarf2_per_objfile::~dwarf2_per_objfile ()
2531{
2532 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2533 free_cached_comp_units ();
2534
2535 if (quick_file_names_table)
2536 htab_delete (quick_file_names_table);
2537
2538 if (line_header_hash)
2539 htab_delete (line_header_hash);
2540
fc8e7e75
SM
2541 for (int ix = 0; ix < n_comp_units; ++ix)
2542 VEC_free (dwarf2_per_cu_ptr, all_comp_units[ix]->imported_symtabs);
2543
2544 for (int ix = 0; ix < n_type_units; ++ix)
2545 VEC_free (dwarf2_per_cu_ptr,
2546 all_type_units[ix]->per_cu.imported_symtabs);
2547 xfree (all_type_units);
2548
2549 VEC_free (dwarf2_section_info_def, types);
2550
2551 if (dwo_files != NULL)
2552 free_dwo_files (dwo_files, objfile);
2553 if (dwp_file != NULL)
2554 gdb_bfd_unref (dwp_file->dbfd);
2555
2556 if (dwz_file != NULL && dwz_file->dwz_bfd)
2557 gdb_bfd_unref (dwz_file->dwz_bfd);
2558
2559 if (index_table != NULL)
2560 index_table->~mapped_index ();
2561
330cdd98
PA
2562 /* Everything else should be on the objfile obstack. */
2563}
2564
2565/* See declaration. */
2566
2567void
2568dwarf2_per_objfile::free_cached_comp_units ()
2569{
2570 dwarf2_per_cu_data *per_cu = read_in_chain;
2571 dwarf2_per_cu_data **last_chain = &read_in_chain;
2572 while (per_cu != NULL)
2573 {
2574 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2575
fcd3b13d 2576 delete per_cu->cu;
330cdd98
PA
2577 *last_chain = next_cu;
2578 per_cu = next_cu;
2579 }
2580}
2581
c906108c 2582/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2583 information and return true if we have enough to do something.
2584 NAMES points to the dwarf2 section names, or is NULL if the standard
2585 ELF names are used. */
c906108c
SS
2586
2587int
251d32d9
TG
2588dwarf2_has_info (struct objfile *objfile,
2589 const struct dwarf2_debug_sections *names)
c906108c 2590{
97cbe998
SDJ
2591 if (objfile->flags & OBJF_READNEVER)
2592 return 0;
2593
ed2dc618
SM
2594 struct dwarf2_per_objfile *dwarf2_per_objfile
2595 = get_dwarf2_per_objfile (objfile);
2596
2597 if (dwarf2_per_objfile == NULL)
be391dca
TT
2598 {
2599 /* Initialize per-objfile state. */
fd90ace4
YQ
2600 dwarf2_per_objfile
2601 = new (&objfile->objfile_obstack) struct dwarf2_per_objfile (objfile,
2602 names);
ed2dc618 2603 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
be391dca 2604 }
73869dc2 2605 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2606 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2607 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2608 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2609}
2610
2611/* Return the containing section of virtual section SECTION. */
2612
2613static struct dwarf2_section_info *
2614get_containing_section (const struct dwarf2_section_info *section)
2615{
2616 gdb_assert (section->is_virtual);
2617 return section->s.containing_section;
c906108c
SS
2618}
2619
a32a8923
DE
2620/* Return the bfd owner of SECTION. */
2621
2622static struct bfd *
2623get_section_bfd_owner (const struct dwarf2_section_info *section)
2624{
73869dc2
DE
2625 if (section->is_virtual)
2626 {
2627 section = get_containing_section (section);
2628 gdb_assert (!section->is_virtual);
2629 }
049412e3 2630 return section->s.section->owner;
a32a8923
DE
2631}
2632
2633/* Return the bfd section of SECTION.
2634 Returns NULL if the section is not present. */
2635
2636static asection *
2637get_section_bfd_section (const struct dwarf2_section_info *section)
2638{
73869dc2
DE
2639 if (section->is_virtual)
2640 {
2641 section = get_containing_section (section);
2642 gdb_assert (!section->is_virtual);
2643 }
049412e3 2644 return section->s.section;
a32a8923
DE
2645}
2646
2647/* Return the name of SECTION. */
2648
2649static const char *
2650get_section_name (const struct dwarf2_section_info *section)
2651{
2652 asection *sectp = get_section_bfd_section (section);
2653
2654 gdb_assert (sectp != NULL);
2655 return bfd_section_name (get_section_bfd_owner (section), sectp);
2656}
2657
2658/* Return the name of the file SECTION is in. */
2659
2660static const char *
2661get_section_file_name (const struct dwarf2_section_info *section)
2662{
2663 bfd *abfd = get_section_bfd_owner (section);
2664
2665 return bfd_get_filename (abfd);
2666}
2667
2668/* Return the id of SECTION.
2669 Returns 0 if SECTION doesn't exist. */
2670
2671static int
2672get_section_id (const struct dwarf2_section_info *section)
2673{
2674 asection *sectp = get_section_bfd_section (section);
2675
2676 if (sectp == NULL)
2677 return 0;
2678 return sectp->id;
2679}
2680
2681/* Return the flags of SECTION.
73869dc2 2682 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2683
2684static int
2685get_section_flags (const struct dwarf2_section_info *section)
2686{
2687 asection *sectp = get_section_bfd_section (section);
2688
2689 gdb_assert (sectp != NULL);
2690 return bfd_get_section_flags (sectp->owner, sectp);
2691}
2692
251d32d9
TG
2693/* When loading sections, we look either for uncompressed section or for
2694 compressed section names. */
233a11ab
CS
2695
2696static int
251d32d9
TG
2697section_is_p (const char *section_name,
2698 const struct dwarf2_section_names *names)
233a11ab 2699{
251d32d9
TG
2700 if (names->normal != NULL
2701 && strcmp (section_name, names->normal) == 0)
2702 return 1;
2703 if (names->compressed != NULL
2704 && strcmp (section_name, names->compressed) == 0)
2705 return 1;
2706 return 0;
233a11ab
CS
2707}
2708
330cdd98 2709/* See declaration. */
c906108c 2710
330cdd98
PA
2711void
2712dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2713 const dwarf2_debug_sections &names)
c906108c 2714{
dc7650b8 2715 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9 2716
dc7650b8
JK
2717 if ((aflag & SEC_HAS_CONTENTS) == 0)
2718 {
2719 }
330cdd98 2720 else if (section_is_p (sectp->name, &names.info))
c906108c 2721 {
330cdd98
PA
2722 this->info.s.section = sectp;
2723 this->info.size = bfd_get_section_size (sectp);
c906108c 2724 }
330cdd98 2725 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2726 {
330cdd98
PA
2727 this->abbrev.s.section = sectp;
2728 this->abbrev.size = bfd_get_section_size (sectp);
c906108c 2729 }
330cdd98 2730 else if (section_is_p (sectp->name, &names.line))
c906108c 2731 {
330cdd98
PA
2732 this->line.s.section = sectp;
2733 this->line.size = bfd_get_section_size (sectp);
c906108c 2734 }
330cdd98 2735 else if (section_is_p (sectp->name, &names.loc))
c906108c 2736 {
330cdd98
PA
2737 this->loc.s.section = sectp;
2738 this->loc.size = bfd_get_section_size (sectp);
c906108c 2739 }
330cdd98 2740 else if (section_is_p (sectp->name, &names.loclists))
43988095 2741 {
330cdd98
PA
2742 this->loclists.s.section = sectp;
2743 this->loclists.size = bfd_get_section_size (sectp);
43988095 2744 }
330cdd98 2745 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2746 {
330cdd98
PA
2747 this->macinfo.s.section = sectp;
2748 this->macinfo.size = bfd_get_section_size (sectp);
c906108c 2749 }
330cdd98 2750 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2751 {
330cdd98
PA
2752 this->macro.s.section = sectp;
2753 this->macro.size = bfd_get_section_size (sectp);
cf2c3c16 2754 }
330cdd98 2755 else if (section_is_p (sectp->name, &names.str))
c906108c 2756 {
330cdd98
PA
2757 this->str.s.section = sectp;
2758 this->str.size = bfd_get_section_size (sectp);
c906108c 2759 }
330cdd98 2760 else if (section_is_p (sectp->name, &names.line_str))
43988095 2761 {
330cdd98
PA
2762 this->line_str.s.section = sectp;
2763 this->line_str.size = bfd_get_section_size (sectp);
43988095 2764 }
330cdd98 2765 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2766 {
330cdd98
PA
2767 this->addr.s.section = sectp;
2768 this->addr.size = bfd_get_section_size (sectp);
3019eac3 2769 }
330cdd98 2770 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2771 {
330cdd98
PA
2772 this->frame.s.section = sectp;
2773 this->frame.size = bfd_get_section_size (sectp);
b6af0555 2774 }
330cdd98 2775 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2776 {
330cdd98
PA
2777 this->eh_frame.s.section = sectp;
2778 this->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2779 }
330cdd98 2780 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2781 {
330cdd98
PA
2782 this->ranges.s.section = sectp;
2783 this->ranges.size = bfd_get_section_size (sectp);
af34e669 2784 }
330cdd98 2785 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2786 {
330cdd98
PA
2787 this->rnglists.s.section = sectp;
2788 this->rnglists.size = bfd_get_section_size (sectp);
43988095 2789 }
330cdd98 2790 else if (section_is_p (sectp->name, &names.types))
348e048f 2791 {
8b70b953
TT
2792 struct dwarf2_section_info type_section;
2793
2794 memset (&type_section, 0, sizeof (type_section));
049412e3 2795 type_section.s.section = sectp;
8b70b953
TT
2796 type_section.size = bfd_get_section_size (sectp);
2797
330cdd98 2798 VEC_safe_push (dwarf2_section_info_def, this->types,
8b70b953 2799 &type_section);
348e048f 2800 }
330cdd98 2801 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2802 {
330cdd98
PA
2803 this->gdb_index.s.section = sectp;
2804 this->gdb_index.size = bfd_get_section_size (sectp);
9291a0cd 2805 }
927aa2e7
JK
2806 else if (section_is_p (sectp->name, &names.debug_names))
2807 {
2808 this->debug_names.s.section = sectp;
2809 this->debug_names.size = bfd_get_section_size (sectp);
2810 }
2811 else if (section_is_p (sectp->name, &names.debug_aranges))
2812 {
2813 this->debug_aranges.s.section = sectp;
2814 this->debug_aranges.size = bfd_get_section_size (sectp);
2815 }
dce234bc 2816
b4e1fd61 2817 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5 2818 && bfd_section_vma (abfd, sectp) == 0)
330cdd98 2819 this->has_section_at_zero = true;
c906108c
SS
2820}
2821
fceca515
DE
2822/* A helper function that decides whether a section is empty,
2823 or not present. */
9e0ac564
TT
2824
2825static int
19ac8c2e 2826dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2827{
73869dc2
DE
2828 if (section->is_virtual)
2829 return section->size == 0;
049412e3 2830 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2831}
2832
3019eac3
DE
2833/* Read the contents of the section INFO.
2834 OBJFILE is the main object file, but not necessarily the file where
a32a8923
DE
2835 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2836 of the DWO file.
dce234bc 2837 If the section is compressed, uncompress it before returning. */
c906108c 2838
dce234bc
PP
2839static void
2840dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 2841{
a32a8923 2842 asection *sectp;
3019eac3 2843 bfd *abfd;
dce234bc 2844 gdb_byte *buf, *retbuf;
c906108c 2845
be391dca
TT
2846 if (info->readin)
2847 return;
dce234bc 2848 info->buffer = NULL;
be391dca 2849 info->readin = 1;
188dd5d6 2850
9e0ac564 2851 if (dwarf2_section_empty_p (info))
dce234bc 2852 return;
c906108c 2853
a32a8923 2854 sectp = get_section_bfd_section (info);
3019eac3 2855
73869dc2
DE
2856 /* If this is a virtual section we need to read in the real one first. */
2857 if (info->is_virtual)
2858 {
2859 struct dwarf2_section_info *containing_section =
2860 get_containing_section (info);
2861
2862 gdb_assert (sectp != NULL);
2863 if ((sectp->flags & SEC_RELOC) != 0)
2864 {
2865 error (_("Dwarf Error: DWP format V2 with relocations is not"
2866 " supported in section %s [in module %s]"),
2867 get_section_name (info), get_section_file_name (info));
2868 }
2869 dwarf2_read_section (objfile, containing_section);
2870 /* Other code should have already caught virtual sections that don't
2871 fit. */
2872 gdb_assert (info->virtual_offset + info->size
2873 <= containing_section->size);
2874 /* If the real section is empty or there was a problem reading the
2875 section we shouldn't get here. */
2876 gdb_assert (containing_section->buffer != NULL);
2877 info->buffer = containing_section->buffer + info->virtual_offset;
2878 return;
2879 }
2880
4bf44c1c
TT
2881 /* If the section has relocations, we must read it ourselves.
2882 Otherwise we attach it to the BFD. */
2883 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2884 {
d521ce57 2885 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2886 return;
dce234bc 2887 }
dce234bc 2888
224c3ddb 2889 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2890 info->buffer = buf;
dce234bc
PP
2891
2892 /* When debugging .o files, we may need to apply relocations; see
2893 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2894 We never compress sections in .o files, so we only need to
2895 try this when the section is not compressed. */
ac8035ab 2896 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2897 if (retbuf != NULL)
2898 {
2899 info->buffer = retbuf;
2900 return;
2901 }
2902
a32a8923
DE
2903 abfd = get_section_bfd_owner (info);
2904 gdb_assert (abfd != NULL);
2905
dce234bc
PP
2906 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2907 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2908 {
2909 error (_("Dwarf Error: Can't read DWARF data"
2910 " in section %s [in module %s]"),
2911 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2912 }
dce234bc
PP
2913}
2914
9e0ac564
TT
2915/* A helper function that returns the size of a section in a safe way.
2916 If you are positive that the section has been read before using the
2917 size, then it is safe to refer to the dwarf2_section_info object's
2918 "size" field directly. In other cases, you must call this
2919 function, because for compressed sections the size field is not set
2920 correctly until the section has been read. */
2921
2922static bfd_size_type
2923dwarf2_section_size (struct objfile *objfile,
2924 struct dwarf2_section_info *info)
2925{
2926 if (!info->readin)
2927 dwarf2_read_section (objfile, info);
2928 return info->size;
2929}
2930
dce234bc 2931/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2932 SECTION_NAME. */
af34e669 2933
dce234bc 2934void
3017a003
TG
2935dwarf2_get_section_info (struct objfile *objfile,
2936 enum dwarf2_section_enum sect,
d521ce57 2937 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2938 bfd_size_type *sizep)
2939{
2940 struct dwarf2_per_objfile *data
9a3c8263
SM
2941 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2942 dwarf2_objfile_data_key);
dce234bc 2943 struct dwarf2_section_info *info;
a3b2a86b
TT
2944
2945 /* We may see an objfile without any DWARF, in which case we just
2946 return nothing. */
2947 if (data == NULL)
2948 {
2949 *sectp = NULL;
2950 *bufp = NULL;
2951 *sizep = 0;
2952 return;
2953 }
3017a003
TG
2954 switch (sect)
2955 {
2956 case DWARF2_DEBUG_FRAME:
2957 info = &data->frame;
2958 break;
2959 case DWARF2_EH_FRAME:
2960 info = &data->eh_frame;
2961 break;
2962 default:
2963 gdb_assert_not_reached ("unexpected section");
2964 }
dce234bc 2965
9e0ac564 2966 dwarf2_read_section (objfile, info);
dce234bc 2967
a32a8923 2968 *sectp = get_section_bfd_section (info);
dce234bc
PP
2969 *bufp = info->buffer;
2970 *sizep = info->size;
2971}
2972
36586728
TT
2973/* A helper function to find the sections for a .dwz file. */
2974
2975static void
2976locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2977{
9a3c8263 2978 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2979
2980 /* Note that we only support the standard ELF names, because .dwz
2981 is ELF-only (at the time of writing). */
2982 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2983 {
049412e3 2984 dwz_file->abbrev.s.section = sectp;
36586728
TT
2985 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2986 }
2987 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2988 {
049412e3 2989 dwz_file->info.s.section = sectp;
36586728
TT
2990 dwz_file->info.size = bfd_get_section_size (sectp);
2991 }
2992 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2993 {
049412e3 2994 dwz_file->str.s.section = sectp;
36586728
TT
2995 dwz_file->str.size = bfd_get_section_size (sectp);
2996 }
2997 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2998 {
049412e3 2999 dwz_file->line.s.section = sectp;
36586728
TT
3000 dwz_file->line.size = bfd_get_section_size (sectp);
3001 }
3002 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
3003 {
049412e3 3004 dwz_file->macro.s.section = sectp;
36586728
TT
3005 dwz_file->macro.size = bfd_get_section_size (sectp);
3006 }
2ec9a5e0
TT
3007 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
3008 {
049412e3 3009 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
3010 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
3011 }
927aa2e7
JK
3012 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
3013 {
3014 dwz_file->debug_names.s.section = sectp;
3015 dwz_file->debug_names.size = bfd_get_section_size (sectp);
3016 }
36586728
TT
3017}
3018
4db1a1dc
TT
3019/* Open the separate '.dwz' debug file, if needed. Return NULL if
3020 there is no .gnu_debugaltlink section in the file. Error if there
3021 is such a section but the file cannot be found. */
36586728
TT
3022
3023static struct dwz_file *
ed2dc618 3024dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 3025{
36586728
TT
3026 const char *filename;
3027 struct dwz_file *result;
acd13123 3028 bfd_size_type buildid_len_arg;
dc294be5
TT
3029 size_t buildid_len;
3030 bfd_byte *buildid;
36586728
TT
3031
3032 if (dwarf2_per_objfile->dwz_file != NULL)
3033 return dwarf2_per_objfile->dwz_file;
3034
4db1a1dc 3035 bfd_set_error (bfd_error_no_error);
791afaa2
TT
3036 gdb::unique_xmalloc_ptr<char> data
3037 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
3038 &buildid_len_arg, &buildid));
4db1a1dc
TT
3039 if (data == NULL)
3040 {
3041 if (bfd_get_error () == bfd_error_no_error)
3042 return NULL;
3043 error (_("could not read '.gnu_debugaltlink' section: %s"),
3044 bfd_errmsg (bfd_get_error ()));
3045 }
791afaa2
TT
3046
3047 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 3048
acd13123
TT
3049 buildid_len = (size_t) buildid_len_arg;
3050
791afaa2 3051 filename = data.get ();
d721ba37
PA
3052
3053 std::string abs_storage;
36586728
TT
3054 if (!IS_ABSOLUTE_PATH (filename))
3055 {
14278e1f
TT
3056 gdb::unique_xmalloc_ptr<char> abs
3057 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 3058
14278e1f 3059 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 3060 filename = abs_storage.c_str ();
36586728
TT
3061 }
3062
dc294be5
TT
3063 /* First try the file name given in the section. If that doesn't
3064 work, try to use the build-id instead. */
192b62ce 3065 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 3066 if (dwz_bfd != NULL)
36586728 3067 {
192b62ce
TT
3068 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
3069 dwz_bfd.release ();
36586728
TT
3070 }
3071
dc294be5
TT
3072 if (dwz_bfd == NULL)
3073 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
3074
3075 if (dwz_bfd == NULL)
3076 error (_("could not find '.gnu_debugaltlink' file for %s"),
3077 objfile_name (dwarf2_per_objfile->objfile));
3078
36586728
TT
3079 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
3080 struct dwz_file);
192b62ce 3081 result->dwz_bfd = dwz_bfd.release ();
36586728 3082
192b62ce 3083 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
36586728 3084
192b62ce 3085 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
8d2cc612 3086 dwarf2_per_objfile->dwz_file = result;
36586728
TT
3087 return result;
3088}
9291a0cd 3089\f
7b9f3c50
DE
3090/* DWARF quick_symbols_functions support. */
3091
3092/* TUs can share .debug_line entries, and there can be a lot more TUs than
3093 unique line tables, so we maintain a separate table of all .debug_line
3094 derived entries to support the sharing.
3095 All the quick functions need is the list of file names. We discard the
3096 line_header when we're done and don't need to record it here. */
3097struct quick_file_names
3098{
094b34ac
DE
3099 /* The data used to construct the hash key. */
3100 struct stmt_list_hash hash;
7b9f3c50
DE
3101
3102 /* The number of entries in file_names, real_names. */
3103 unsigned int num_file_names;
3104
3105 /* The file names from the line table, after being run through
3106 file_full_name. */
3107 const char **file_names;
3108
3109 /* The file names from the line table after being run through
3110 gdb_realpath. These are computed lazily. */
3111 const char **real_names;
3112};
3113
3114/* When using the index (and thus not using psymtabs), each CU has an
3115 object of this type. This is used to hold information needed by
3116 the various "quick" methods. */
3117struct dwarf2_per_cu_quick_data
3118{
3119 /* The file table. This can be NULL if there was no file table
3120 or it's currently not read in.
3121 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
3122 struct quick_file_names *file_names;
3123
3124 /* The corresponding symbol table. This is NULL if symbols for this
3125 CU have not yet been read. */
43f3e411 3126 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
3127
3128 /* A temporary mark bit used when iterating over all CUs in
3129 expand_symtabs_matching. */
3130 unsigned int mark : 1;
3131
3132 /* True if we've tried to read the file table and found there isn't one.
3133 There will be no point in trying to read it again next time. */
3134 unsigned int no_file_data : 1;
3135};
3136
094b34ac
DE
3137/* Utility hash function for a stmt_list_hash. */
3138
3139static hashval_t
3140hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
3141{
3142 hashval_t v = 0;
3143
3144 if (stmt_list_hash->dwo_unit != NULL)
3145 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 3146 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
3147 return v;
3148}
3149
3150/* Utility equality function for a stmt_list_hash. */
3151
3152static int
3153eq_stmt_list_entry (const struct stmt_list_hash *lhs,
3154 const struct stmt_list_hash *rhs)
3155{
3156 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
3157 return 0;
3158 if (lhs->dwo_unit != NULL
3159 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
3160 return 0;
3161
9c541725 3162 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
3163}
3164
7b9f3c50
DE
3165/* Hash function for a quick_file_names. */
3166
3167static hashval_t
3168hash_file_name_entry (const void *e)
3169{
9a3c8263
SM
3170 const struct quick_file_names *file_data
3171 = (const struct quick_file_names *) e;
7b9f3c50 3172
094b34ac 3173 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
3174}
3175
3176/* Equality function for a quick_file_names. */
3177
3178static int
3179eq_file_name_entry (const void *a, const void *b)
3180{
9a3c8263
SM
3181 const struct quick_file_names *ea = (const struct quick_file_names *) a;
3182 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 3183
094b34ac 3184 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
3185}
3186
3187/* Delete function for a quick_file_names. */
3188
3189static void
3190delete_file_name_entry (void *e)
3191{
9a3c8263 3192 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
3193 int i;
3194
3195 for (i = 0; i < file_data->num_file_names; ++i)
3196 {
3197 xfree ((void*) file_data->file_names[i]);
3198 if (file_data->real_names)
3199 xfree ((void*) file_data->real_names[i]);
3200 }
3201
3202 /* The space for the struct itself lives on objfile_obstack,
3203 so we don't free it here. */
3204}
3205
3206/* Create a quick_file_names hash table. */
3207
3208static htab_t
3209create_quick_file_names_table (unsigned int nr_initial_entries)
3210{
3211 return htab_create_alloc (nr_initial_entries,
3212 hash_file_name_entry, eq_file_name_entry,
3213 delete_file_name_entry, xcalloc, xfree);
3214}
9291a0cd 3215
918dd910
JK
3216/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
3217 have to be created afterwards. You should call age_cached_comp_units after
3218 processing PER_CU->CU. dw2_setup must have been already called. */
3219
3220static void
3221load_cu (struct dwarf2_per_cu_data *per_cu)
3222{
3019eac3 3223 if (per_cu->is_debug_types)
e5fe5e75 3224 load_full_type_unit (per_cu);
918dd910 3225 else
95554aad 3226 load_full_comp_unit (per_cu, language_minimal);
918dd910 3227
cc12ce38
DE
3228 if (per_cu->cu == NULL)
3229 return; /* Dummy CU. */
2dc860c0
DE
3230
3231 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
3232}
3233
a0f42c21 3234/* Read in the symbols for PER_CU. */
2fdf6df6 3235
9291a0cd 3236static void
a0f42c21 3237dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 3238{
ed2dc618 3239 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 3240
f4dc4d17
DE
3241 /* Skip type_unit_groups, reading the type units they contain
3242 is handled elsewhere. */
3243 if (IS_TYPE_UNIT_GROUP (per_cu))
3244 return;
3245
b303c6f6
AB
3246 /* The destructor of dwarf2_queue_guard frees any entries left on
3247 the queue. After this point we're guaranteed to leave this function
3248 with the dwarf queue empty. */
3249 dwarf2_queue_guard q_guard;
9291a0cd 3250
95554aad 3251 if (dwarf2_per_objfile->using_index
43f3e411 3252 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
3253 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
3254 {
3255 queue_comp_unit (per_cu, language_minimal);
3256 load_cu (per_cu);
89e63ee4
DE
3257
3258 /* If we just loaded a CU from a DWO, and we're working with an index
3259 that may badly handle TUs, load all the TUs in that DWO as well.
3260 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
3261 if (!per_cu->is_debug_types
cc12ce38 3262 && per_cu->cu != NULL
89e63ee4
DE
3263 && per_cu->cu->dwo_unit != NULL
3264 && dwarf2_per_objfile->index_table != NULL
3265 && dwarf2_per_objfile->index_table->version <= 7
3266 /* DWP files aren't supported yet. */
ed2dc618 3267 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 3268 queue_and_load_all_dwo_tus (per_cu);
95554aad 3269 }
9291a0cd 3270
ed2dc618 3271 process_queue (dwarf2_per_objfile);
9291a0cd
TT
3272
3273 /* Age the cache, releasing compilation units that have not
3274 been used recently. */
ed2dc618 3275 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
3276}
3277
3278/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
3279 the objfile from which this CU came. Returns the resulting symbol
3280 table. */
2fdf6df6 3281
43f3e411 3282static struct compunit_symtab *
a0f42c21 3283dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 3284{
ed2dc618
SM
3285 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3286
95554aad 3287 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 3288 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 3289 {
ed2dc618
SM
3290 struct cleanup *back_to = make_cleanup (free_cached_comp_units,
3291 dwarf2_per_objfile);
c83dd867 3292 scoped_restore decrementer = increment_reading_symtab ();
a0f42c21 3293 dw2_do_instantiate_symtab (per_cu);
ed2dc618 3294 process_cu_includes (dwarf2_per_objfile);
9291a0cd
TT
3295 do_cleanups (back_to);
3296 }
f194fefb 3297
43f3e411 3298 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
3299}
3300
8832e7e3 3301/* Return the CU/TU given its index.
f4dc4d17
DE
3302
3303 This is intended for loops like:
3304
3305 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3306 + dwarf2_per_objfile->n_type_units); ++i)
3307 {
8832e7e3 3308 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
f4dc4d17
DE
3309
3310 ...;
3311 }
3312*/
2fdf6df6 3313
1fd400ff 3314static struct dwarf2_per_cu_data *
ed2dc618
SM
3315dw2_get_cutu (struct dwarf2_per_objfile *dwarf2_per_objfile,
3316 int index)
1fd400ff
TT
3317{
3318 if (index >= dwarf2_per_objfile->n_comp_units)
3319 {
f4dc4d17 3320 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
3321 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3322 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
3323 }
3324
3325 return dwarf2_per_objfile->all_comp_units[index];
3326}
3327
8832e7e3
DE
3328/* Return the CU given its index.
3329 This differs from dw2_get_cutu in that it's for when you know INDEX
3330 refers to a CU. */
f4dc4d17
DE
3331
3332static struct dwarf2_per_cu_data *
ed2dc618 3333dw2_get_cu (struct dwarf2_per_objfile *dwarf2_per_objfile, int index)
f4dc4d17 3334{
8832e7e3 3335 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
f4dc4d17 3336
1fd400ff
TT
3337 return dwarf2_per_objfile->all_comp_units[index];
3338}
3339
4b514bc8
JK
3340/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
3341 objfile_obstack, and constructed with the specified field
3342 values. */
3343
3344static dwarf2_per_cu_data *
ed2dc618 3345create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
3346 struct dwarf2_section_info *section,
3347 int is_dwz,
3348 sect_offset sect_off, ULONGEST length)
3349{
ed2dc618 3350 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
3351 dwarf2_per_cu_data *the_cu
3352 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3353 struct dwarf2_per_cu_data);
3354 the_cu->sect_off = sect_off;
3355 the_cu->length = length;
e3b94546 3356 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
3357 the_cu->section = section;
3358 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3359 struct dwarf2_per_cu_quick_data);
3360 the_cu->is_dwz = is_dwz;
3361 return the_cu;
3362}
3363
2ec9a5e0
TT
3364/* A helper for create_cus_from_index that handles a given list of
3365 CUs. */
2fdf6df6 3366
74a0d9f6 3367static void
2ec9a5e0
TT
3368create_cus_from_index_list (struct objfile *objfile,
3369 const gdb_byte *cu_list, offset_type n_elements,
3370 struct dwarf2_section_info *section,
3371 int is_dwz,
3372 int base_offset)
9291a0cd
TT
3373{
3374 offset_type i;
ed2dc618
SM
3375 struct dwarf2_per_objfile *dwarf2_per_objfile
3376 = get_dwarf2_per_objfile (objfile);
9291a0cd 3377
2ec9a5e0 3378 for (i = 0; i < n_elements; i += 2)
9291a0cd 3379 {
74a0d9f6 3380 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3381
3382 sect_offset sect_off
3383 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3384 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
3385 cu_list += 2 * 8;
3386
4b514bc8 3387 dwarf2_per_objfile->all_comp_units[base_offset + i / 2]
ed2dc618
SM
3388 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3389 sect_off, length);
9291a0cd 3390 }
9291a0cd
TT
3391}
3392
2ec9a5e0 3393/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3394 the CU objects for this objfile. */
2ec9a5e0 3395
74a0d9f6 3396static void
2ec9a5e0
TT
3397create_cus_from_index (struct objfile *objfile,
3398 const gdb_byte *cu_list, offset_type cu_list_elements,
3399 const gdb_byte *dwz_list, offset_type dwz_elements)
3400{
3401 struct dwz_file *dwz;
ed2dc618
SM
3402 struct dwarf2_per_objfile *dwarf2_per_objfile
3403 = get_dwarf2_per_objfile (objfile);
2ec9a5e0
TT
3404
3405 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
8d749320
SM
3406 dwarf2_per_objfile->all_comp_units =
3407 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3408 dwarf2_per_objfile->n_comp_units);
2ec9a5e0 3409
74a0d9f6
JK
3410 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3411 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
3412
3413 if (dwz_elements == 0)
74a0d9f6 3414 return;
2ec9a5e0 3415
ed2dc618 3416 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
74a0d9f6
JK
3417 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3418 cu_list_elements / 2);
2ec9a5e0
TT
3419}
3420
1fd400ff 3421/* Create the signatured type hash table from the index. */
673bfd45 3422
74a0d9f6 3423static void
673bfd45 3424create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 3425 struct dwarf2_section_info *section,
673bfd45
DE
3426 const gdb_byte *bytes,
3427 offset_type elements)
1fd400ff
TT
3428{
3429 offset_type i;
673bfd45 3430 htab_t sig_types_hash;
ed2dc618
SM
3431 struct dwarf2_per_objfile *dwarf2_per_objfile
3432 = get_dwarf2_per_objfile (objfile);
1fd400ff 3433
6aa5f3a6
DE
3434 dwarf2_per_objfile->n_type_units
3435 = dwarf2_per_objfile->n_allocated_type_units
3436 = elements / 3;
8d749320
SM
3437 dwarf2_per_objfile->all_type_units =
3438 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
1fd400ff 3439
673bfd45 3440 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
3441
3442 for (i = 0; i < elements; i += 3)
3443 {
52dc124a 3444 struct signatured_type *sig_type;
9c541725 3445 ULONGEST signature;
1fd400ff 3446 void **slot;
9c541725 3447 cu_offset type_offset_in_tu;
1fd400ff 3448
74a0d9f6 3449 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3450 sect_offset sect_off
3451 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3452 type_offset_in_tu
3453 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3454 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3455 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3456 bytes += 3 * 8;
3457
52dc124a 3458 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3459 struct signatured_type);
52dc124a 3460 sig_type->signature = signature;
9c541725 3461 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3462 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3463 sig_type->per_cu.section = section;
9c541725 3464 sig_type->per_cu.sect_off = sect_off;
e3b94546 3465 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 3466 sig_type->per_cu.v.quick
1fd400ff
TT
3467 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3468 struct dwarf2_per_cu_quick_data);
3469
52dc124a
DE
3470 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3471 *slot = sig_type;
1fd400ff 3472
b4dd5633 3473 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
3474 }
3475
673bfd45 3476 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3477}
3478
927aa2e7
JK
3479/* Create the signatured type hash table from .debug_names. */
3480
3481static void
3482create_signatured_type_table_from_debug_names
ed2dc618 3483 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3484 const mapped_debug_names &map,
3485 struct dwarf2_section_info *section,
3486 struct dwarf2_section_info *abbrev_section)
3487{
ed2dc618
SM
3488 struct objfile *objfile = dwarf2_per_objfile->objfile;
3489
927aa2e7
JK
3490 dwarf2_read_section (objfile, section);
3491 dwarf2_read_section (objfile, abbrev_section);
3492
3493 dwarf2_per_objfile->n_type_units
3494 = dwarf2_per_objfile->n_allocated_type_units
3495 = map.tu_count;
3496 dwarf2_per_objfile->all_type_units
3497 = XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3498
3499 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3500
3501 for (uint32_t i = 0; i < map.tu_count; ++i)
3502 {
3503 struct signatured_type *sig_type;
3504 ULONGEST signature;
3505 void **slot;
3506 cu_offset type_offset_in_tu;
3507
3508 sect_offset sect_off
3509 = (sect_offset) (extract_unsigned_integer
3510 (map.tu_table_reordered + i * map.offset_size,
3511 map.offset_size,
3512 map.dwarf5_byte_order));
3513
3514 comp_unit_head cu_header;
ed2dc618
SM
3515 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3516 abbrev_section,
927aa2e7
JK
3517 section->buffer + to_underlying (sect_off),
3518 rcuh_kind::TYPE);
3519
3520 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3521 struct signatured_type);
3522 sig_type->signature = cu_header.signature;
3523 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3524 sig_type->per_cu.is_debug_types = 1;
3525 sig_type->per_cu.section = section;
3526 sig_type->per_cu.sect_off = sect_off;
e3b94546 3527 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
3528 sig_type->per_cu.v.quick
3529 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3530 struct dwarf2_per_cu_quick_data);
3531
3532 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3533 *slot = sig_type;
3534
3535 dwarf2_per_objfile->all_type_units[i] = sig_type;
3536 }
3537
3538 dwarf2_per_objfile->signatured_types = sig_types_hash;
3539}
3540
9291a0cd
TT
3541/* Read the address map data from the mapped index, and use it to
3542 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3543
9291a0cd 3544static void
ed2dc618
SM
3545create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3546 struct mapped_index *index)
9291a0cd 3547{
ed2dc618 3548 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 3549 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3550 const gdb_byte *iter, *end;
9291a0cd 3551 struct addrmap *mutable_map;
9291a0cd
TT
3552 CORE_ADDR baseaddr;
3553
8268c778
PA
3554 auto_obstack temp_obstack;
3555
9291a0cd
TT
3556 mutable_map = addrmap_create_mutable (&temp_obstack);
3557
f00a2de2
PA
3558 iter = index->address_table.data ();
3559 end = iter + index->address_table.size ();
9291a0cd
TT
3560
3561 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3562
3563 while (iter < end)
3564 {
3565 ULONGEST hi, lo, cu_index;
3566 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3567 iter += 8;
3568 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3569 iter += 8;
3570 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3571 iter += 4;
f652bce2 3572
24a55014 3573 if (lo > hi)
f652bce2 3574 {
24a55014
DE
3575 complaint (&symfile_complaints,
3576 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3577 hex_string (lo), hex_string (hi));
24a55014 3578 continue;
f652bce2 3579 }
24a55014
DE
3580
3581 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
3582 {
3583 complaint (&symfile_complaints,
3584 _(".gdb_index address table has invalid CU number %u"),
3585 (unsigned) cu_index);
24a55014 3586 continue;
f652bce2 3587 }
24a55014 3588
3e29f34a
MR
3589 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3590 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
ed2dc618
SM
3591 addrmap_set_empty (mutable_map, lo, hi - 1,
3592 dw2_get_cutu (dwarf2_per_objfile, cu_index));
9291a0cd
TT
3593 }
3594
3595 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3596 &objfile->objfile_obstack);
9291a0cd
TT
3597}
3598
927aa2e7
JK
3599/* Read the address map data from DWARF-5 .debug_aranges, and use it to
3600 populate the objfile's psymtabs_addrmap. */
3601
3602static void
ed2dc618 3603create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3604 struct dwarf2_section_info *section)
3605{
ed2dc618 3606 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
3607 bfd *abfd = objfile->obfd;
3608 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3609 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3610 SECT_OFF_TEXT (objfile));
3611
3612 auto_obstack temp_obstack;
3613 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3614
3615 std::unordered_map<sect_offset,
3616 dwarf2_per_cu_data *,
3617 gdb::hash_enum<sect_offset>>
3618 debug_info_offset_to_per_cu;
3619 for (int cui = 0; cui < dwarf2_per_objfile->n_comp_units; ++cui)
3620 {
ed2dc618 3621 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, cui);
927aa2e7
JK
3622 const auto insertpair
3623 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3624 if (!insertpair.second)
3625 {
3626 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
3627 "debug_info_offset %s, ignoring .debug_aranges."),
3628 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
3629 return;
3630 }
3631 }
3632
3633 dwarf2_read_section (objfile, section);
3634
3635 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3636
3637 const gdb_byte *addr = section->buffer;
3638
3639 while (addr < section->buffer + section->size)
3640 {
3641 const gdb_byte *const entry_addr = addr;
3642 unsigned int bytes_read;
3643
3644 const LONGEST entry_length = read_initial_length (abfd, addr,
3645 &bytes_read);
3646 addr += bytes_read;
3647
3648 const gdb_byte *const entry_end = addr + entry_length;
3649 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3650 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3651 if (addr + entry_length > section->buffer + section->size)
3652 {
3653 warning (_("Section .debug_aranges in %s entry at offset %zu "
3654 "length %s exceeds section length %s, "
3655 "ignoring .debug_aranges."),
3656 objfile_name (objfile), entry_addr - section->buffer,
3657 plongest (bytes_read + entry_length),
3658 pulongest (section->size));
3659 return;
3660 }
3661
3662 /* The version number. */
3663 const uint16_t version = read_2_bytes (abfd, addr);
3664 addr += 2;
3665 if (version != 2)
3666 {
3667 warning (_("Section .debug_aranges in %s entry at offset %zu "
3668 "has unsupported version %d, ignoring .debug_aranges."),
3669 objfile_name (objfile), entry_addr - section->buffer,
3670 version);
3671 return;
3672 }
3673
3674 const uint64_t debug_info_offset
3675 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3676 addr += offset_size;
3677 const auto per_cu_it
3678 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3679 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3680 {
3681 warning (_("Section .debug_aranges in %s entry at offset %zu "
3682 "debug_info_offset %s does not exists, "
3683 "ignoring .debug_aranges."),
3684 objfile_name (objfile), entry_addr - section->buffer,
3685 pulongest (debug_info_offset));
3686 return;
3687 }
3688 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3689
3690 const uint8_t address_size = *addr++;
3691 if (address_size < 1 || address_size > 8)
3692 {
3693 warning (_("Section .debug_aranges in %s entry at offset %zu "
3694 "address_size %u is invalid, ignoring .debug_aranges."),
3695 objfile_name (objfile), entry_addr - section->buffer,
3696 address_size);
3697 return;
3698 }
3699
3700 const uint8_t segment_selector_size = *addr++;
3701 if (segment_selector_size != 0)
3702 {
3703 warning (_("Section .debug_aranges in %s entry at offset %zu "
3704 "segment_selector_size %u is not supported, "
3705 "ignoring .debug_aranges."),
3706 objfile_name (objfile), entry_addr - section->buffer,
3707 segment_selector_size);
3708 return;
3709 }
3710
3711 /* Must pad to an alignment boundary that is twice the address
3712 size. It is undocumented by the DWARF standard but GCC does
3713 use it. */
3714 for (size_t padding = ((-(addr - section->buffer))
3715 & (2 * address_size - 1));
3716 padding > 0; padding--)
3717 if (*addr++ != 0)
3718 {
3719 warning (_("Section .debug_aranges in %s entry at offset %zu "
3720 "padding is not zero, ignoring .debug_aranges."),
3721 objfile_name (objfile), entry_addr - section->buffer);
3722 return;
3723 }
3724
3725 for (;;)
3726 {
3727 if (addr + 2 * address_size > entry_end)
3728 {
3729 warning (_("Section .debug_aranges in %s entry at offset %zu "
3730 "address list is not properly terminated, "
3731 "ignoring .debug_aranges."),
3732 objfile_name (objfile), entry_addr - section->buffer);
3733 return;
3734 }
3735 ULONGEST start = extract_unsigned_integer (addr, address_size,
3736 dwarf5_byte_order);
3737 addr += address_size;
3738 ULONGEST length = extract_unsigned_integer (addr, address_size,
3739 dwarf5_byte_order);
3740 addr += address_size;
3741 if (start == 0 && length == 0)
3742 break;
3743 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3744 {
3745 /* Symbol was eliminated due to a COMDAT group. */
3746 continue;
3747 }
3748 ULONGEST end = start + length;
3749 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3750 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3751 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3752 }
3753 }
3754
3755 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3756 &objfile->objfile_obstack);
3757}
3758
59d7bcaf
JK
3759/* The hash function for strings in the mapped index. This is the same as
3760 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3761 implementation. This is necessary because the hash function is tied to the
3762 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
3763 SYMBOL_HASH_NEXT.
3764
3765 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 3766
9291a0cd 3767static hashval_t
559a7a62 3768mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
3769{
3770 const unsigned char *str = (const unsigned char *) p;
3771 hashval_t r = 0;
3772 unsigned char c;
3773
3774 while ((c = *str++) != 0)
559a7a62
JK
3775 {
3776 if (index_version >= 5)
3777 c = tolower (c);
3778 r = r * 67 + c - 113;
3779 }
9291a0cd
TT
3780
3781 return r;
3782}
3783
3784/* Find a slot in the mapped index INDEX for the object named NAME.
3785 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3786 constant pool and return true. If NAME cannot be found, return
3787 false. */
2fdf6df6 3788
109483d9 3789static bool
9291a0cd
TT
3790find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3791 offset_type **vec_out)
3792{
0cf03b49 3793 offset_type hash;
9291a0cd 3794 offset_type slot, step;
559a7a62 3795 int (*cmp) (const char *, const char *);
9291a0cd 3796
791afaa2 3797 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3798 if (current_language->la_language == language_cplus
45280282
IB
3799 || current_language->la_language == language_fortran
3800 || current_language->la_language == language_d)
0cf03b49
JK
3801 {
3802 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3803 not contain any. */
a8719064 3804
72998fb3 3805 if (strchr (name, '(') != NULL)
0cf03b49 3806 {
109483d9 3807 without_params = cp_remove_params (name);
0cf03b49 3808
72998fb3 3809 if (without_params != NULL)
791afaa2 3810 name = without_params.get ();
0cf03b49
JK
3811 }
3812 }
3813
559a7a62 3814 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3815 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3816 simulate our NAME being searched is also lowercased. */
3817 hash = mapped_index_string_hash ((index->version == 4
3818 && case_sensitivity == case_sensitive_off
3819 ? 5 : index->version),
3820 name);
3821
f00a2de2
PA
3822 slot = hash & (index->symbol_table.size () - 1);
3823 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3824 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3825
3826 for (;;)
3827 {
9291a0cd 3828 const char *str;
f00a2de2
PA
3829
3830 const auto &bucket = index->symbol_table[slot];
3831 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3832 return false;
9291a0cd 3833
f00a2de2 3834 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3835 if (!cmp (name, str))
9291a0cd
TT
3836 {
3837 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3838 + MAYBE_SWAP (bucket.vec));
109483d9 3839 return true;
9291a0cd
TT
3840 }
3841
f00a2de2 3842 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3843 }
3844}
3845
2ec9a5e0
TT
3846/* A helper function that reads the .gdb_index from SECTION and fills
3847 in MAP. FILENAME is the name of the file containing the section;
3848 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3849 ok to use deprecated sections.
3850
3851 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3852 out parameters that are filled in with information about the CU and
3853 TU lists in the section.
3854
3855 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3856
9291a0cd 3857static int
2ec9a5e0
TT
3858read_index_from_section (struct objfile *objfile,
3859 const char *filename,
3860 int deprecated_ok,
3861 struct dwarf2_section_info *section,
3862 struct mapped_index *map,
3863 const gdb_byte **cu_list,
3864 offset_type *cu_list_elements,
3865 const gdb_byte **types_list,
3866 offset_type *types_list_elements)
9291a0cd 3867{
948f8e3d 3868 const gdb_byte *addr;
2ec9a5e0 3869 offset_type version;
b3b272e1 3870 offset_type *metadata;
1fd400ff 3871 int i;
9291a0cd 3872
2ec9a5e0 3873 if (dwarf2_section_empty_p (section))
9291a0cd 3874 return 0;
82430852
JK
3875
3876 /* Older elfutils strip versions could keep the section in the main
3877 executable while splitting it for the separate debug info file. */
a32a8923 3878 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3879 return 0;
3880
2ec9a5e0 3881 dwarf2_read_section (objfile, section);
9291a0cd 3882
2ec9a5e0 3883 addr = section->buffer;
9291a0cd 3884 /* Version check. */
1fd400ff 3885 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3886 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3887 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3888 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3889 indices. */
831adc1f 3890 if (version < 4)
481860b3
GB
3891 {
3892 static int warning_printed = 0;
3893 if (!warning_printed)
3894 {
3895 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3896 filename);
481860b3
GB
3897 warning_printed = 1;
3898 }
3899 return 0;
3900 }
3901 /* Index version 4 uses a different hash function than index version
3902 5 and later.
3903
3904 Versions earlier than 6 did not emit psymbols for inlined
3905 functions. Using these files will cause GDB not to be able to
3906 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3907 indices unless the user has done
3908 "set use-deprecated-index-sections on". */
2ec9a5e0 3909 if (version < 6 && !deprecated_ok)
481860b3
GB
3910 {
3911 static int warning_printed = 0;
3912 if (!warning_printed)
3913 {
e615022a
DE
3914 warning (_("\
3915Skipping deprecated .gdb_index section in %s.\n\
3916Do \"set use-deprecated-index-sections on\" before the file is read\n\
3917to use the section anyway."),
2ec9a5e0 3918 filename);
481860b3
GB
3919 warning_printed = 1;
3920 }
3921 return 0;
3922 }
796a7ff8 3923 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3924 of the TU (for symbols coming from TUs),
3925 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3926 Plus gold-generated indices can have duplicate entries for global symbols,
3927 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3928 These are just performance bugs, and we can't distinguish gdb-generated
3929 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3930
481860b3 3931 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3932 longer backward compatible. */
796a7ff8 3933 if (version > 8)
594e8718 3934 return 0;
9291a0cd 3935
559a7a62 3936 map->version = version;
2ec9a5e0 3937 map->total_size = section->size;
9291a0cd
TT
3938
3939 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3940
3941 i = 0;
2ec9a5e0
TT
3942 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3943 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3944 / 8);
1fd400ff
TT
3945 ++i;
3946
2ec9a5e0
TT
3947 *types_list = addr + MAYBE_SWAP (metadata[i]);
3948 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3949 - MAYBE_SWAP (metadata[i]))
3950 / 8);
987d643c 3951 ++i;
1fd400ff 3952
f00a2de2
PA
3953 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3954 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3955 map->address_table
3956 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3957 ++i;
3958
f00a2de2
PA
3959 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3960 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3961 map->symbol_table
3962 = gdb::array_view<mapped_index::symbol_table_slot>
3963 ((mapped_index::symbol_table_slot *) symbol_table,
3964 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3965
f00a2de2 3966 ++i;
f9d83a0b 3967 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3968
2ec9a5e0
TT
3969 return 1;
3970}
3971
927aa2e7 3972/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3973 elements of all the CUs and return 1. Otherwise, return 0. */
3974
3975static int
3976dwarf2_read_index (struct objfile *objfile)
3977{
3978 struct mapped_index local_map, *map;
3979 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3980 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3981 struct dwz_file *dwz;
ed2dc618
SM
3982 struct dwarf2_per_objfile *dwarf2_per_objfile
3983 = get_dwarf2_per_objfile (objfile);
2ec9a5e0 3984
4262abfb 3985 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0
TT
3986 use_deprecated_index_sections,
3987 &dwarf2_per_objfile->gdb_index, &local_map,
3988 &cu_list, &cu_list_elements,
3989 &types_list, &types_list_elements))
3990 return 0;
3991
0fefef59 3992 /* Don't use the index if it's empty. */
f00a2de2 3993 if (local_map.symbol_table.empty ())
0fefef59
DE
3994 return 0;
3995
2ec9a5e0
TT
3996 /* If there is a .dwz file, read it so we can get its CU list as
3997 well. */
ed2dc618 3998 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3999 if (dwz != NULL)
2ec9a5e0 4000 {
2ec9a5e0
TT
4001 struct mapped_index dwz_map;
4002 const gdb_byte *dwz_types_ignore;
4003 offset_type dwz_types_elements_ignore;
4004
4005 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
4006 1,
4007 &dwz->gdb_index, &dwz_map,
4008 &dwz_list, &dwz_list_elements,
4009 &dwz_types_ignore,
4010 &dwz_types_elements_ignore))
4011 {
4012 warning (_("could not read '.gdb_index' section from %s; skipping"),
4013 bfd_get_filename (dwz->dwz_bfd));
4014 return 0;
4015 }
4016 }
4017
74a0d9f6
JK
4018 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
4019 dwz_list_elements);
1fd400ff 4020
8b70b953
TT
4021 if (types_list_elements)
4022 {
4023 struct dwarf2_section_info *section;
4024
4025 /* We can only handle a single .debug_types when we have an
4026 index. */
4027 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
4028 return 0;
4029
4030 section = VEC_index (dwarf2_section_info_def,
4031 dwarf2_per_objfile->types, 0);
4032
74a0d9f6
JK
4033 create_signatured_type_table_from_index (objfile, section, types_list,
4034 types_list_elements);
8b70b953 4035 }
9291a0cd 4036
ed2dc618 4037 create_addrmap_from_index (dwarf2_per_objfile, &local_map);
2ec9a5e0 4038
8d749320 4039 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3f563c84 4040 map = new (map) mapped_index ();
2ec9a5e0 4041 *map = local_map;
9291a0cd
TT
4042
4043 dwarf2_per_objfile->index_table = map;
4044 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
4045 dwarf2_per_objfile->quick_file_names_table =
4046 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
4047
4048 return 1;
4049}
4050
dee91e82 4051/* die_reader_func for dw2_get_file_names. */
2fdf6df6 4052
dee91e82
DE
4053static void
4054dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 4055 const gdb_byte *info_ptr,
dee91e82
DE
4056 struct die_info *comp_unit_die,
4057 int has_children,
4058 void *data)
9291a0cd 4059{
dee91e82 4060 struct dwarf2_cu *cu = reader->cu;
ed2dc618 4061 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
4062 struct dwarf2_per_objfile *dwarf2_per_objfile
4063 = cu->per_cu->dwarf2_per_objfile;
dee91e82 4064 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 4065 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 4066 struct attribute *attr;
dee91e82 4067 int i;
7b9f3c50
DE
4068 void **slot;
4069 struct quick_file_names *qfn;
9291a0cd 4070
0186c6a7
DE
4071 gdb_assert (! this_cu->is_debug_types);
4072
07261596
TT
4073 /* Our callers never want to match partial units -- instead they
4074 will match the enclosing full CU. */
4075 if (comp_unit_die->tag == DW_TAG_partial_unit)
4076 {
4077 this_cu->v.quick->no_file_data = 1;
4078 return;
4079 }
4080
0186c6a7 4081 lh_cu = this_cu;
7b9f3c50 4082 slot = NULL;
dee91e82 4083
fff8551c 4084 line_header_up lh;
9c541725 4085 sect_offset line_offset {};
fff8551c 4086
dee91e82 4087 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
4088 if (attr)
4089 {
7b9f3c50
DE
4090 struct quick_file_names find_entry;
4091
9c541725 4092 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
4093
4094 /* We may have already read in this line header (TU line header sharing).
4095 If we have we're done. */
094b34ac 4096 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 4097 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
4098 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
4099 &find_entry, INSERT);
4100 if (*slot != NULL)
4101 {
9a3c8263 4102 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 4103 return;
7b9f3c50
DE
4104 }
4105
3019eac3 4106 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
4107 }
4108 if (lh == NULL)
4109 {
094b34ac 4110 lh_cu->v.quick->no_file_data = 1;
dee91e82 4111 return;
9291a0cd
TT
4112 }
4113
8d749320 4114 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 4115 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 4116 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
4117 gdb_assert (slot != NULL);
4118 *slot = qfn;
9291a0cd 4119
d721ba37 4120 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 4121
fff8551c 4122 qfn->num_file_names = lh->file_names.size ();
8d749320 4123 qfn->file_names =
fff8551c
PA
4124 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
4125 for (i = 0; i < lh->file_names.size (); ++i)
4126 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 4127 qfn->real_names = NULL;
9291a0cd 4128
094b34ac 4129 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
4130}
4131
4132/* A helper for the "quick" functions which attempts to read the line
4133 table for THIS_CU. */
4134
4135static struct quick_file_names *
e4a48d9d 4136dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 4137{
0186c6a7
DE
4138 /* This should never be called for TUs. */
4139 gdb_assert (! this_cu->is_debug_types);
4140 /* Nor type unit groups. */
4141 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 4142
dee91e82
DE
4143 if (this_cu->v.quick->file_names != NULL)
4144 return this_cu->v.quick->file_names;
4145 /* If we know there is no line data, no point in looking again. */
4146 if (this_cu->v.quick->no_file_data)
4147 return NULL;
4148
0186c6a7 4149 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
4150
4151 if (this_cu->v.quick->no_file_data)
4152 return NULL;
4153 return this_cu->v.quick->file_names;
9291a0cd
TT
4154}
4155
4156/* A helper for the "quick" functions which computes and caches the
7b9f3c50 4157 real path for a given file name from the line table. */
2fdf6df6 4158
9291a0cd 4159static const char *
7b9f3c50
DE
4160dw2_get_real_path (struct objfile *objfile,
4161 struct quick_file_names *qfn, int index)
9291a0cd 4162{
7b9f3c50
DE
4163 if (qfn->real_names == NULL)
4164 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 4165 qfn->num_file_names, const char *);
9291a0cd 4166
7b9f3c50 4167 if (qfn->real_names[index] == NULL)
14278e1f 4168 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 4169
7b9f3c50 4170 return qfn->real_names[index];
9291a0cd
TT
4171}
4172
4173static struct symtab *
4174dw2_find_last_source_symtab (struct objfile *objfile)
4175{
ed2dc618
SM
4176 struct dwarf2_per_objfile *dwarf2_per_objfile
4177 = get_dwarf2_per_objfile (objfile);
4178 int index = dwarf2_per_objfile->n_comp_units - 1;
4179 dwarf2_per_cu_data *dwarf_cu = dw2_get_cutu (dwarf2_per_objfile, index);
4180 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu);
ae2de4f8 4181
43f3e411
DE
4182 if (cust == NULL)
4183 return NULL;
ed2dc618 4184
43f3e411 4185 return compunit_primary_filetab (cust);
9291a0cd
TT
4186}
4187
7b9f3c50
DE
4188/* Traversal function for dw2_forget_cached_source_info. */
4189
4190static int
4191dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 4192{
7b9f3c50 4193 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 4194
7b9f3c50 4195 if (file_data->real_names)
9291a0cd 4196 {
7b9f3c50 4197 int i;
9291a0cd 4198
7b9f3c50 4199 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 4200 {
7b9f3c50
DE
4201 xfree ((void*) file_data->real_names[i]);
4202 file_data->real_names[i] = NULL;
9291a0cd
TT
4203 }
4204 }
7b9f3c50
DE
4205
4206 return 1;
4207}
4208
4209static void
4210dw2_forget_cached_source_info (struct objfile *objfile)
4211{
ed2dc618
SM
4212 struct dwarf2_per_objfile *dwarf2_per_objfile
4213 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
4214
4215 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
4216 dw2_free_cached_file_names, NULL);
9291a0cd
TT
4217}
4218
f8eba3c6
TT
4219/* Helper function for dw2_map_symtabs_matching_filename that expands
4220 the symtabs and calls the iterator. */
4221
4222static int
4223dw2_map_expand_apply (struct objfile *objfile,
4224 struct dwarf2_per_cu_data *per_cu,
f5b95b50 4225 const char *name, const char *real_path,
14bc53a8 4226 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 4227{
43f3e411 4228 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
4229
4230 /* Don't visit already-expanded CUs. */
43f3e411 4231 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
4232 return 0;
4233
4234 /* This may expand more than one symtab, and we want to iterate over
4235 all of them. */
a0f42c21 4236 dw2_instantiate_symtab (per_cu);
f8eba3c6 4237
14bc53a8
PA
4238 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
4239 last_made, callback);
f8eba3c6
TT
4240}
4241
4242/* Implementation of the map_symtabs_matching_filename method. */
4243
14bc53a8
PA
4244static bool
4245dw2_map_symtabs_matching_filename
4246 (struct objfile *objfile, const char *name, const char *real_path,
4247 gdb::function_view<bool (symtab *)> callback)
9291a0cd
TT
4248{
4249 int i;
c011a4f4 4250 const char *name_basename = lbasename (name);
ed2dc618
SM
4251 struct dwarf2_per_objfile *dwarf2_per_objfile
4252 = get_dwarf2_per_objfile (objfile);
ae2de4f8 4253
848e3e78
DE
4254 /* The rule is CUs specify all the files, including those used by
4255 any TU, so there's no need to scan TUs here. */
f4dc4d17 4256
ed2dc618 4257 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4258 {
4259 int j;
ed2dc618 4260 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
7b9f3c50 4261 struct quick_file_names *file_data;
9291a0cd 4262
3d7bb9d9 4263 /* We only need to look at symtabs not already expanded. */
43f3e411 4264 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4265 continue;
4266
e4a48d9d 4267 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4268 if (file_data == NULL)
9291a0cd
TT
4269 continue;
4270
7b9f3c50 4271 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4272 {
7b9f3c50 4273 const char *this_name = file_data->file_names[j];
da235a7c 4274 const char *this_real_name;
9291a0cd 4275
af529f8f 4276 if (compare_filenames_for_search (this_name, name))
9291a0cd 4277 {
f5b95b50 4278 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
4279 callback))
4280 return true;
288e77a7 4281 continue;
4aac40c8 4282 }
9291a0cd 4283
c011a4f4
DE
4284 /* Before we invoke realpath, which can get expensive when many
4285 files are involved, do a quick comparison of the basenames. */
4286 if (! basenames_may_differ
4287 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
4288 continue;
4289
da235a7c
JK
4290 this_real_name = dw2_get_real_path (objfile, file_data, j);
4291 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 4292 {
da235a7c 4293 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
4294 callback))
4295 return true;
288e77a7 4296 continue;
da235a7c 4297 }
9291a0cd 4298
da235a7c
JK
4299 if (real_path != NULL)
4300 {
af529f8f
JK
4301 gdb_assert (IS_ABSOLUTE_PATH (real_path));
4302 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 4303 if (this_real_name != NULL
af529f8f 4304 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 4305 {
f5b95b50 4306 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
4307 callback))
4308 return true;
288e77a7 4309 continue;
9291a0cd
TT
4310 }
4311 }
4312 }
4313 }
4314
14bc53a8 4315 return false;
9291a0cd
TT
4316}
4317
da51c347
DE
4318/* Struct used to manage iterating over all CUs looking for a symbol. */
4319
4320struct dw2_symtab_iterator
9291a0cd 4321{
ed2dc618
SM
4322 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
4323 struct dwarf2_per_objfile *dwarf2_per_objfile;
da51c347
DE
4324 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
4325 int want_specific_block;
4326 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
4327 Unused if !WANT_SPECIFIC_BLOCK. */
4328 int block_index;
4329 /* The kind of symbol we're looking for. */
4330 domain_enum domain;
4331 /* The list of CUs from the index entry of the symbol,
4332 or NULL if not found. */
4333 offset_type *vec;
4334 /* The next element in VEC to look at. */
4335 int next;
4336 /* The number of elements in VEC, or zero if there is no match. */
4337 int length;
8943b874
DE
4338 /* Have we seen a global version of the symbol?
4339 If so we can ignore all further global instances.
4340 This is to work around gold/15646, inefficient gold-generated
4341 indices. */
4342 int global_seen;
da51c347 4343};
9291a0cd 4344
da51c347
DE
4345/* Initialize the index symtab iterator ITER.
4346 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
4347 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 4348
9291a0cd 4349static void
da51c347 4350dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 4351 struct dwarf2_per_objfile *dwarf2_per_objfile,
da51c347
DE
4352 int want_specific_block,
4353 int block_index,
4354 domain_enum domain,
4355 const char *name)
4356{
ed2dc618 4357 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
4358 iter->want_specific_block = want_specific_block;
4359 iter->block_index = block_index;
4360 iter->domain = domain;
4361 iter->next = 0;
8943b874 4362 iter->global_seen = 0;
da51c347 4363
ed2dc618
SM
4364 mapped_index *index = dwarf2_per_objfile->index_table;
4365
4366 /* index is NULL if OBJF_READNOW. */
4367 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
4368 iter->length = MAYBE_SWAP (*iter->vec);
4369 else
4370 {
4371 iter->vec = NULL;
4372 iter->length = 0;
4373 }
4374}
4375
4376/* Return the next matching CU or NULL if there are no more. */
4377
4378static struct dwarf2_per_cu_data *
4379dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
4380{
ed2dc618
SM
4381 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
4382
da51c347
DE
4383 for ( ; iter->next < iter->length; ++iter->next)
4384 {
4385 offset_type cu_index_and_attrs =
4386 MAYBE_SWAP (iter->vec[iter->next + 1]);
4387 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 4388 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
4389 int want_static = iter->block_index != GLOBAL_BLOCK;
4390 /* This value is only valid for index versions >= 7. */
4391 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4392 gdb_index_symbol_kind symbol_kind =
4393 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4394 /* Only check the symbol attributes if they're present.
4395 Indices prior to version 7 don't record them,
4396 and indices >= 7 may elide them for certain symbols
4397 (gold does this). */
4398 int attrs_valid =
ed2dc618 4399 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
4400 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4401
3190f0c6
DE
4402 /* Don't crash on bad data. */
4403 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4404 + dwarf2_per_objfile->n_type_units))
4405 {
4406 complaint (&symfile_complaints,
4407 _(".gdb_index entry has bad CU index"
4262abfb
JK
4408 " [in module %s]"),
4409 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
4410 continue;
4411 }
4412
ed2dc618 4413 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
3190f0c6 4414
da51c347 4415 /* Skip if already read in. */
43f3e411 4416 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
4417 continue;
4418
8943b874
DE
4419 /* Check static vs global. */
4420 if (attrs_valid)
4421 {
4422 if (iter->want_specific_block
4423 && want_static != is_static)
4424 continue;
4425 /* Work around gold/15646. */
4426 if (!is_static && iter->global_seen)
4427 continue;
4428 if (!is_static)
4429 iter->global_seen = 1;
4430 }
da51c347
DE
4431
4432 /* Only check the symbol's kind if it has one. */
4433 if (attrs_valid)
4434 {
4435 switch (iter->domain)
4436 {
4437 case VAR_DOMAIN:
4438 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4439 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4440 /* Some types are also in VAR_DOMAIN. */
4441 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4442 continue;
4443 break;
4444 case STRUCT_DOMAIN:
4445 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4446 continue;
4447 break;
4448 case LABEL_DOMAIN:
4449 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4450 continue;
4451 break;
4452 default:
4453 break;
4454 }
4455 }
4456
4457 ++iter->next;
4458 return per_cu;
4459 }
4460
4461 return NULL;
4462}
4463
43f3e411 4464static struct compunit_symtab *
da51c347
DE
4465dw2_lookup_symbol (struct objfile *objfile, int block_index,
4466 const char *name, domain_enum domain)
9291a0cd 4467{
43f3e411 4468 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
4469 struct dwarf2_per_objfile *dwarf2_per_objfile
4470 = get_dwarf2_per_objfile (objfile);
9291a0cd 4471
b5ec771e
PA
4472 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4473
ed2dc618
SM
4474 struct dw2_symtab_iterator iter;
4475 struct dwarf2_per_cu_data *per_cu;
da51c347 4476
ed2dc618 4477 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
9291a0cd 4478
ed2dc618
SM
4479 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4480 {
4481 struct symbol *sym, *with_opaque = NULL;
4482 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
4483 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4484 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4485
ed2dc618
SM
4486 sym = block_find_symbol (block, name, domain,
4487 block_find_non_opaque_type_preferred,
4488 &with_opaque);
b2e2f908 4489
ed2dc618
SM
4490 /* Some caution must be observed with overloaded functions
4491 and methods, since the index will not contain any overload
4492 information (but NAME might contain it). */
da51c347 4493
ed2dc618
SM
4494 if (sym != NULL
4495 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4496 return stab;
4497 if (with_opaque != NULL
4498 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4499 stab_best = stab;
da51c347 4500
ed2dc618 4501 /* Keep looking through other CUs. */
9291a0cd 4502 }
9291a0cd 4503
da51c347 4504 return stab_best;
9291a0cd
TT
4505}
4506
4507static void
4508dw2_print_stats (struct objfile *objfile)
4509{
ed2dc618
SM
4510 struct dwarf2_per_objfile *dwarf2_per_objfile
4511 = get_dwarf2_per_objfile (objfile);
4512 int total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
4513 int count = 0;
9291a0cd 4514
ed2dc618 4515 for (int i = 0; i < total; ++i)
9291a0cd 4516 {
ed2dc618 4517 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
9291a0cd 4518
43f3e411 4519 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4520 ++count;
4521 }
e4a48d9d 4522 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4523 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4524}
4525
779bd270
DE
4526/* This dumps minimal information about the index.
4527 It is called via "mt print objfiles".
4528 One use is to verify .gdb_index has been loaded by the
4529 gdb.dwarf2/gdb-index.exp testcase. */
4530
9291a0cd
TT
4531static void
4532dw2_dump (struct objfile *objfile)
4533{
ed2dc618
SM
4534 struct dwarf2_per_objfile *dwarf2_per_objfile
4535 = get_dwarf2_per_objfile (objfile);
4536
779bd270
DE
4537 gdb_assert (dwarf2_per_objfile->using_index);
4538 printf_filtered (".gdb_index:");
4539 if (dwarf2_per_objfile->index_table != NULL)
4540 {
4541 printf_filtered (" version %d\n",
4542 dwarf2_per_objfile->index_table->version);
4543 }
4544 else
4545 printf_filtered (" faked for \"readnow\"\n");
4546 printf_filtered ("\n");
9291a0cd
TT
4547}
4548
4549static void
3189cb12
DE
4550dw2_relocate (struct objfile *objfile,
4551 const struct section_offsets *new_offsets,
4552 const struct section_offsets *delta)
9291a0cd
TT
4553{
4554 /* There's nothing to relocate here. */
4555}
4556
4557static void
4558dw2_expand_symtabs_for_function (struct objfile *objfile,
4559 const char *func_name)
4560{
ed2dc618
SM
4561 struct dwarf2_per_objfile *dwarf2_per_objfile
4562 = get_dwarf2_per_objfile (objfile);
da51c347 4563
ed2dc618
SM
4564 struct dw2_symtab_iterator iter;
4565 struct dwarf2_per_cu_data *per_cu;
da51c347 4566
ed2dc618
SM
4567 /* Note: It doesn't matter what we pass for block_index here. */
4568 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4569 func_name);
da51c347 4570
ed2dc618
SM
4571 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4572 dw2_instantiate_symtab (per_cu);
da51c347 4573
9291a0cd
TT
4574}
4575
4576static void
4577dw2_expand_all_symtabs (struct objfile *objfile)
4578{
ed2dc618
SM
4579 struct dwarf2_per_objfile *dwarf2_per_objfile
4580 = get_dwarf2_per_objfile (objfile);
4581 int total_units = (dwarf2_per_objfile->n_comp_units
4582 + dwarf2_per_objfile->n_type_units);
9291a0cd 4583
ed2dc618 4584 for (int i = 0; i < total_units; ++i)
9291a0cd 4585 {
ed2dc618
SM
4586 struct dwarf2_per_cu_data *per_cu
4587 = dw2_get_cutu (dwarf2_per_objfile, i);
9291a0cd 4588
a0f42c21 4589 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4590 }
4591}
4592
4593static void
652a8996
JK
4594dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4595 const char *fullname)
9291a0cd 4596{
ed2dc618
SM
4597 struct dwarf2_per_objfile *dwarf2_per_objfile
4598 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
4599
4600 /* We don't need to consider type units here.
4601 This is only called for examining code, e.g. expand_line_sal.
4602 There can be an order of magnitude (or more) more type units
4603 than comp units, and we avoid them if we can. */
4604
ed2dc618 4605 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4606 {
4607 int j;
ed2dc618 4608 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
7b9f3c50 4609 struct quick_file_names *file_data;
9291a0cd 4610
3d7bb9d9 4611 /* We only need to look at symtabs not already expanded. */
43f3e411 4612 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4613 continue;
4614
e4a48d9d 4615 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4616 if (file_data == NULL)
9291a0cd
TT
4617 continue;
4618
7b9f3c50 4619 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4620 {
652a8996
JK
4621 const char *this_fullname = file_data->file_names[j];
4622
4623 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4624 {
a0f42c21 4625 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4626 break;
4627 }
4628 }
4629 }
4630}
4631
9291a0cd 4632static void
ade7ed9e 4633dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 4634 const char * name, domain_enum domain,
ade7ed9e 4635 int global,
40658b94
PH
4636 int (*callback) (struct block *,
4637 struct symbol *, void *),
b5ec771e 4638 void *data, symbol_name_match_type match,
2edb89d3 4639 symbol_compare_ftype *ordered_compare)
9291a0cd 4640{
40658b94 4641 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4642 current language is Ada for a non-Ada objfile using GNU index. As Ada
4643 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4644}
4645
b5ec771e
PA
4646/* Symbol name matcher for .gdb_index names.
4647
4648 Symbol names in .gdb_index have a few particularities:
4649
4650 - There's no indication of which is the language of each symbol.
4651
4652 Since each language has its own symbol name matching algorithm,
4653 and we don't know which language is the right one, we must match
3f563c84
PA
4654 each symbol against all languages. This would be a potential
4655 performance problem if it were not mitigated by the
4656 mapped_index::name_components lookup table, which significantly
4657 reduces the number of times we need to call into this matcher,
4658 making it a non-issue.
b5ec771e
PA
4659
4660 - Symbol names in the index have no overload (parameter)
4661 information. I.e., in C++, "foo(int)" and "foo(long)" both
4662 appear as "foo" in the index, for example.
4663
4664 This means that the lookup names passed to the symbol name
4665 matcher functions must have no parameter information either
4666 because (e.g.) symbol search name "foo" does not match
4667 lookup-name "foo(int)" [while swapping search name for lookup
4668 name would match].
4669*/
4670class gdb_index_symbol_name_matcher
4671{
4672public:
4673 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4674 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4675
4676 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4677 Returns true if any matcher matches. */
4678 bool matches (const char *symbol_name);
4679
4680private:
4681 /* A reference to the lookup name we're matching against. */
4682 const lookup_name_info &m_lookup_name;
4683
4684 /* A vector holding all the different symbol name matchers, for all
4685 languages. */
4686 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4687};
4688
4689gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4690 (const lookup_name_info &lookup_name)
4691 : m_lookup_name (lookup_name)
4692{
4693 /* Prepare the vector of comparison functions upfront, to avoid
4694 doing the same work for each symbol. Care is taken to avoid
4695 matching with the same matcher more than once if/when multiple
4696 languages use the same matcher function. */
4697 auto &matchers = m_symbol_name_matcher_funcs;
4698 matchers.reserve (nr_languages);
4699
4700 matchers.push_back (default_symbol_name_matcher);
4701
4702 for (int i = 0; i < nr_languages; i++)
4703 {
4704 const language_defn *lang = language_def ((enum language) i);
c63d3e8d 4705 symbol_name_matcher_ftype *name_matcher
618daa93 4706 = get_symbol_name_matcher (lang, m_lookup_name);
c63d3e8d
PA
4707
4708 /* Don't insert the same comparison routine more than once.
4709 Note that we do this linear walk instead of a seemingly
4710 cheaper sorted insert, or use a std::set or something like
4711 that, because relative order of function addresses is not
4712 stable. This is not a problem in practice because the number
4713 of supported languages is low, and the cost here is tiny
4714 compared to the number of searches we'll do afterwards using
4715 this object. */
4716 if (name_matcher != default_symbol_name_matcher
4717 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4718 == matchers.end ()))
4719 matchers.push_back (name_matcher);
b5ec771e
PA
4720 }
4721}
4722
4723bool
4724gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4725{
4726 for (auto matches_name : m_symbol_name_matcher_funcs)
4727 if (matches_name (symbol_name, m_lookup_name, NULL))
4728 return true;
4729
4730 return false;
4731}
4732
e1ef7d7a
PA
4733/* Starting from a search name, return the string that finds the upper
4734 bound of all strings that start with SEARCH_NAME in a sorted name
4735 list. Returns the empty string to indicate that the upper bound is
4736 the end of the list. */
4737
4738static std::string
4739make_sort_after_prefix_name (const char *search_name)
4740{
4741 /* When looking to complete "func", we find the upper bound of all
4742 symbols that start with "func" by looking for where we'd insert
4743 the closest string that would follow "func" in lexicographical
4744 order. Usually, that's "func"-with-last-character-incremented,
4745 i.e. "fund". Mind non-ASCII characters, though. Usually those
4746 will be UTF-8 multi-byte sequences, but we can't be certain.
4747 Especially mind the 0xff character, which is a valid character in
4748 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4749 rule out compilers allowing it in identifiers. Note that
4750 conveniently, strcmp/strcasecmp are specified to compare
4751 characters interpreted as unsigned char. So what we do is treat
4752 the whole string as a base 256 number composed of a sequence of
4753 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4754 to 0, and carries 1 to the following more-significant position.
4755 If the very first character in SEARCH_NAME ends up incremented
4756 and carries/overflows, then the upper bound is the end of the
4757 list. The string after the empty string is also the empty
4758 string.
4759
4760 Some examples of this operation:
4761
4762 SEARCH_NAME => "+1" RESULT
4763
4764 "abc" => "abd"
4765 "ab\xff" => "ac"
4766 "\xff" "a" "\xff" => "\xff" "b"
4767 "\xff" => ""
4768 "\xff\xff" => ""
4769 "" => ""
4770
4771 Then, with these symbols for example:
4772
4773 func
4774 func1
4775 fund
4776
4777 completing "func" looks for symbols between "func" and
4778 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4779 which finds "func" and "func1", but not "fund".
4780
4781 And with:
4782
4783 funcÿ (Latin1 'ÿ' [0xff])
4784 funcÿ1
4785 fund
4786
4787 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4788 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4789
4790 And with:
4791
4792 ÿÿ (Latin1 'ÿ' [0xff])
4793 ÿÿ1
4794
4795 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4796 the end of the list.
4797 */
4798 std::string after = search_name;
4799 while (!after.empty () && (unsigned char) after.back () == 0xff)
4800 after.pop_back ();
4801 if (!after.empty ())
4802 after.back () = (unsigned char) after.back () + 1;
4803 return after;
4804}
4805
5c58de74 4806/* See declaration. */
61d96d7e 4807
5c58de74
PA
4808std::pair<std::vector<name_component>::const_iterator,
4809 std::vector<name_component>::const_iterator>
44ed8f3e 4810mapped_index_base::find_name_components_bounds
5c58de74 4811 (const lookup_name_info &lookup_name_without_params) const
3f563c84 4812{
5c58de74
PA
4813 auto *name_cmp
4814 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84
PA
4815
4816 const char *cplus
c62446b1 4817 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
9291a0cd 4818
3f563c84
PA
4819 /* Comparison function object for lower_bound that matches against a
4820 given symbol name. */
4821 auto lookup_compare_lower = [&] (const name_component &elem,
4822 const char *name)
4823 {
5c58de74 4824 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4825 const char *elem_name = elem_qualified + elem.name_offset;
4826 return name_cmp (elem_name, name) < 0;
4827 };
4828
4829 /* Comparison function object for upper_bound that matches against a
4830 given symbol name. */
4831 auto lookup_compare_upper = [&] (const char *name,
4832 const name_component &elem)
4833 {
5c58de74 4834 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4835 const char *elem_name = elem_qualified + elem.name_offset;
4836 return name_cmp (name, elem_name) < 0;
4837 };
4838
5c58de74
PA
4839 auto begin = this->name_components.begin ();
4840 auto end = this->name_components.end ();
3f563c84
PA
4841
4842 /* Find the lower bound. */
4843 auto lower = [&] ()
4844 {
5c58de74 4845 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
3f563c84
PA
4846 return begin;
4847 else
4848 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4849 } ();
4850
4851 /* Find the upper bound. */
4852 auto upper = [&] ()
4853 {
5c58de74 4854 if (lookup_name_without_params.completion_mode ())
3f563c84 4855 {
e1ef7d7a
PA
4856 /* In completion mode, we want UPPER to point past all
4857 symbols names that have the same prefix. I.e., with
4858 these symbols, and completing "func":
4859
4860 function << lower bound
4861 function1
4862 other_function << upper bound
4863
4864 We find the upper bound by looking for the insertion
4865 point of "func"-with-last-character-incremented,
4866 i.e. "fund". */
4867 std::string after = make_sort_after_prefix_name (cplus);
4868 if (after.empty ())
3f563c84 4869 return end;
e6b2f5ef
PA
4870 return std::lower_bound (lower, end, after.c_str (),
4871 lookup_compare_lower);
3f563c84
PA
4872 }
4873 else
4874 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4875 } ();
4876
5c58de74
PA
4877 return {lower, upper};
4878}
4879
4880/* See declaration. */
4881
4882void
44ed8f3e 4883mapped_index_base::build_name_components ()
5c58de74
PA
4884{
4885 if (!this->name_components.empty ())
4886 return;
4887
4888 this->name_components_casing = case_sensitivity;
4889 auto *name_cmp
4890 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4891
4892 /* The code below only knows how to break apart components of C++
4893 symbol names (and other languages that use '::' as
4894 namespace/module separator). If we add support for wild matching
4895 to some language that uses some other operator (E.g., Ada, Go and
4896 D use '.'), then we'll need to try splitting the symbol name
4897 according to that language too. Note that Ada does support wild
4898 matching, but doesn't currently support .gdb_index. */
44ed8f3e
PA
4899 auto count = this->symbol_name_count ();
4900 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4901 {
44ed8f3e 4902 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4903 continue;
4904
4905 const char *name = this->symbol_name_at (idx);
4906
4907 /* Add each name component to the name component table. */
4908 unsigned int previous_len = 0;
4909 for (unsigned int current_len = cp_find_first_component (name);
4910 name[current_len] != '\0';
4911 current_len += cp_find_first_component (name + current_len))
4912 {
4913 gdb_assert (name[current_len] == ':');
4914 this->name_components.push_back ({previous_len, idx});
4915 /* Skip the '::'. */
4916 current_len += 2;
4917 previous_len = current_len;
4918 }
4919 this->name_components.push_back ({previous_len, idx});
4920 }
4921
4922 /* Sort name_components elements by name. */
4923 auto name_comp_compare = [&] (const name_component &left,
4924 const name_component &right)
4925 {
4926 const char *left_qualified = this->symbol_name_at (left.idx);
4927 const char *right_qualified = this->symbol_name_at (right.idx);
4928
4929 const char *left_name = left_qualified + left.name_offset;
4930 const char *right_name = right_qualified + right.name_offset;
4931
4932 return name_cmp (left_name, right_name) < 0;
4933 };
4934
4935 std::sort (this->name_components.begin (),
4936 this->name_components.end (),
4937 name_comp_compare);
4938}
4939
4940/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4941 mapped_index_base instead of the containing objfile. This is split
4942 to a separate function in order to be able to unit test the
4943 name_components matching using a mock mapped_index_base. For each
5c58de74 4944 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4945 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4946
4947static void
4948dw2_expand_symtabs_matching_symbol
44ed8f3e 4949 (mapped_index_base &index,
5c58de74
PA
4950 const lookup_name_info &lookup_name_in,
4951 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4952 enum search_domain kind,
4953 gdb::function_view<void (offset_type)> match_callback)
4954{
4955 lookup_name_info lookup_name_without_params
4956 = lookup_name_in.make_ignore_params ();
4957 gdb_index_symbol_name_matcher lookup_name_matcher
4958 (lookup_name_without_params);
4959
4960 /* Build the symbol name component sorted vector, if we haven't
4961 yet. */
4962 index.build_name_components ();
4963
4964 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4965
3f563c84
PA
4966 /* Now for each symbol name in range, check to see if we have a name
4967 match, and if so, call the MATCH_CALLBACK callback. */
4968
4969 /* The same symbol may appear more than once in the range though.
4970 E.g., if we're looking for symbols that complete "w", and we have
4971 a symbol named "w1::w2", we'll find the two name components for
4972 that same symbol in the range. To be sure we only call the
4973 callback once per symbol, we first collect the symbol name
4974 indexes that matched in a temporary vector and ignore
4975 duplicates. */
4976 std::vector<offset_type> matches;
5c58de74 4977 matches.reserve (std::distance (bounds.first, bounds.second));
3f563c84 4978
5c58de74 4979 for (; bounds.first != bounds.second; ++bounds.first)
3f563c84 4980 {
5c58de74 4981 const char *qualified = index.symbol_name_at (bounds.first->idx);
3f563c84
PA
4982
4983 if (!lookup_name_matcher.matches (qualified)
4984 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
9291a0cd
TT
4985 continue;
4986
5c58de74 4987 matches.push_back (bounds.first->idx);
3f563c84
PA
4988 }
4989
4990 std::sort (matches.begin (), matches.end ());
4991
4992 /* Finally call the callback, once per match. */
4993 ULONGEST prev = -1;
4994 for (offset_type idx : matches)
4995 {
4996 if (prev != idx)
4997 {
4998 match_callback (idx);
4999 prev = idx;
5000 }
5001 }
5002
5003 /* Above we use a type wider than idx's for 'prev', since 0 and
5004 (offset_type)-1 are both possible values. */
5005 static_assert (sizeof (prev) > sizeof (offset_type), "");
5006}
5007
c62446b1
PA
5008#if GDB_SELF_TEST
5009
5010namespace selftests { namespace dw2_expand_symtabs_matching {
5011
a3c5fafd
PA
5012/* A mock .gdb_index/.debug_names-like name index table, enough to
5013 exercise dw2_expand_symtabs_matching_symbol, which works with the
5014 mapped_index_base interface. Builds an index from the symbol list
5015 passed as parameter to the constructor. */
5016class mock_mapped_index : public mapped_index_base
c62446b1
PA
5017{
5018public:
a3c5fafd
PA
5019 mock_mapped_index (gdb::array_view<const char *> symbols)
5020 : m_symbol_table (symbols)
c62446b1
PA
5021 {}
5022
a3c5fafd 5023 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 5024
a3c5fafd
PA
5025 /* Return the number of names in the symbol table. */
5026 virtual size_t symbol_name_count () const
c62446b1 5027 {
a3c5fafd 5028 return m_symbol_table.size ();
c62446b1
PA
5029 }
5030
a3c5fafd
PA
5031 /* Get the name of the symbol at IDX in the symbol table. */
5032 virtual const char *symbol_name_at (offset_type idx) const
5033 {
5034 return m_symbol_table[idx];
5035 }
c62446b1 5036
a3c5fafd
PA
5037private:
5038 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
5039};
5040
5041/* Convenience function that converts a NULL pointer to a "<null>"
5042 string, to pass to print routines. */
5043
5044static const char *
5045string_or_null (const char *str)
5046{
5047 return str != NULL ? str : "<null>";
5048}
5049
5050/* Check if a lookup_name_info built from
5051 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
5052 index. EXPECTED_LIST is the list of expected matches, in expected
5053 matching order. If no match expected, then an empty list is
5054 specified. Returns true on success. On failure prints a warning
5055 indicating the file:line that failed, and returns false. */
5056
5057static bool
5058check_match (const char *file, int line,
5059 mock_mapped_index &mock_index,
5060 const char *name, symbol_name_match_type match_type,
5061 bool completion_mode,
5062 std::initializer_list<const char *> expected_list)
5063{
5064 lookup_name_info lookup_name (name, match_type, completion_mode);
5065
5066 bool matched = true;
5067
5068 auto mismatch = [&] (const char *expected_str,
5069 const char *got)
5070 {
5071 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
5072 "expected=\"%s\", got=\"%s\"\n"),
5073 file, line,
5074 (match_type == symbol_name_match_type::FULL
5075 ? "FULL" : "WILD"),
5076 name, string_or_null (expected_str), string_or_null (got));
5077 matched = false;
5078 };
5079
5080 auto expected_it = expected_list.begin ();
5081 auto expected_end = expected_list.end ();
5082
a3c5fafd 5083 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
5084 NULL, ALL_DOMAIN,
5085 [&] (offset_type idx)
5086 {
a3c5fafd 5087 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
5088 const char *expected_str
5089 = expected_it == expected_end ? NULL : *expected_it++;
5090
5091 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
5092 mismatch (expected_str, matched_name);
5093 });
5094
5095 const char *expected_str
5096 = expected_it == expected_end ? NULL : *expected_it++;
5097 if (expected_str != NULL)
5098 mismatch (expected_str, NULL);
5099
5100 return matched;
5101}
5102
5103/* The symbols added to the mock mapped_index for testing (in
5104 canonical form). */
5105static const char *test_symbols[] = {
5106 "function",
5107 "std::bar",
5108 "std::zfunction",
5109 "std::zfunction2",
5110 "w1::w2",
5111 "ns::foo<char*>",
5112 "ns::foo<int>",
5113 "ns::foo<long>",
a20714ff
PA
5114 "ns2::tmpl<int>::foo2",
5115 "(anonymous namespace)::A::B::C",
c62446b1 5116
e1ef7d7a
PA
5117 /* These are used to check that the increment-last-char in the
5118 matching algorithm for completion doesn't match "t1_fund" when
5119 completing "t1_func". */
5120 "t1_func",
5121 "t1_func1",
5122 "t1_fund",
5123 "t1_fund1",
5124
5125 /* A UTF-8 name with multi-byte sequences to make sure that
5126 cp-name-parser understands this as a single identifier ("função"
5127 is "function" in PT). */
5128 u8"u8função",
5129
5130 /* \377 (0xff) is Latin1 'ÿ'. */
5131 "yfunc\377",
5132
5133 /* \377 (0xff) is Latin1 'ÿ'. */
5134 "\377",
5135 "\377\377123",
5136
c62446b1
PA
5137 /* A name with all sorts of complications. Starts with "z" to make
5138 it easier for the completion tests below. */
5139#define Z_SYM_NAME \
5140 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
5141 "::tuple<(anonymous namespace)::ui*, " \
5142 "std::default_delete<(anonymous namespace)::ui>, void>"
5143
5144 Z_SYM_NAME
5145};
5146
a3c5fafd
PA
5147/* Returns true if the mapped_index_base::find_name_component_bounds
5148 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
5149 in completion mode. */
5c58de74
PA
5150
5151static bool
a3c5fafd 5152check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
5153 const char *search_name,
5154 gdb::array_view<const char *> expected_syms)
5155{
5156 lookup_name_info lookup_name (search_name,
5157 symbol_name_match_type::FULL, true);
5158
5159 auto bounds = index.find_name_components_bounds (lookup_name);
5160
5161 size_t distance = std::distance (bounds.first, bounds.second);
5162 if (distance != expected_syms.size ())
5163 return false;
5164
5165 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
5166 {
5167 auto nc_elem = bounds.first + exp_elem;
5168 const char *qualified = index.symbol_name_at (nc_elem->idx);
5169 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
5170 return false;
5171 }
5172
5173 return true;
5174}
5175
5176/* Test the lower-level mapped_index::find_name_component_bounds
5177 method. */
5178
c62446b1 5179static void
5c58de74
PA
5180test_mapped_index_find_name_component_bounds ()
5181{
5182 mock_mapped_index mock_index (test_symbols);
5183
a3c5fafd 5184 mock_index.build_name_components ();
5c58de74
PA
5185
5186 /* Test the lower-level mapped_index::find_name_component_bounds
5187 method in completion mode. */
5188 {
5189 static const char *expected_syms[] = {
5190 "t1_func",
5191 "t1_func1",
5c58de74
PA
5192 };
5193
a3c5fafd 5194 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
5195 "t1_func", expected_syms));
5196 }
5197
5198 /* Check that the increment-last-char in the name matching algorithm
5199 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
5200 {
5201 static const char *expected_syms1[] = {
5202 "\377",
5203 "\377\377123",
5204 };
a3c5fafd 5205 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
5206 "\377", expected_syms1));
5207
5208 static const char *expected_syms2[] = {
5209 "\377\377123",
5210 };
a3c5fafd 5211 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
5212 "\377\377", expected_syms2));
5213 }
5214}
5215
5216/* Test dw2_expand_symtabs_matching_symbol. */
5217
5218static void
5219test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
5220{
5221 mock_mapped_index mock_index (test_symbols);
5222
5223 /* We let all tests run until the end even if some fails, for debug
5224 convenience. */
5225 bool any_mismatch = false;
5226
5227 /* Create the expected symbols list (an initializer_list). Needed
5228 because lists have commas, and we need to pass them to CHECK,
5229 which is a macro. */
5230#define EXPECT(...) { __VA_ARGS__ }
5231
5232 /* Wrapper for check_match that passes down the current
5233 __FILE__/__LINE__. */
5234#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
5235 any_mismatch |= !check_match (__FILE__, __LINE__, \
5236 mock_index, \
5237 NAME, MATCH_TYPE, COMPLETION_MODE, \
5238 EXPECTED_LIST)
5239
5240 /* Identity checks. */
5241 for (const char *sym : test_symbols)
5242 {
5243 /* Should be able to match all existing symbols. */
5244 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
5245 EXPECT (sym));
5246
5247 /* Should be able to match all existing symbols with
5248 parameters. */
5249 std::string with_params = std::string (sym) + "(int)";
5250 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5251 EXPECT (sym));
5252
5253 /* Should be able to match all existing symbols with
5254 parameters and qualifiers. */
5255 with_params = std::string (sym) + " ( int ) const";
5256 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5257 EXPECT (sym));
5258
5259 /* This should really find sym, but cp-name-parser.y doesn't
5260 know about lvalue/rvalue qualifiers yet. */
5261 with_params = std::string (sym) + " ( int ) &&";
5262 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5263 {});
5264 }
5265
e1ef7d7a
PA
5266 /* Check that the name matching algorithm for completion doesn't get
5267 confused with Latin1 'ÿ' / 0xff. */
5268 {
5269 static const char str[] = "\377";
5270 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5271 EXPECT ("\377", "\377\377123"));
5272 }
5273
5274 /* Check that the increment-last-char in the matching algorithm for
5275 completion doesn't match "t1_fund" when completing "t1_func". */
5276 {
5277 static const char str[] = "t1_func";
5278 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5279 EXPECT ("t1_func", "t1_func1"));
5280 }
5281
c62446b1
PA
5282 /* Check that completion mode works at each prefix of the expected
5283 symbol name. */
5284 {
5285 static const char str[] = "function(int)";
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 ("function"));
5294 }
5295 }
5296
5297 /* While "w" is a prefix of both components, the match function
5298 should still only be called once. */
5299 {
5300 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
5301 EXPECT ("w1::w2"));
a20714ff
PA
5302 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
5303 EXPECT ("w1::w2"));
c62446b1
PA
5304 }
5305
5306 /* Same, with a "complicated" symbol. */
5307 {
5308 static const char str[] = Z_SYM_NAME;
5309 size_t len = strlen (str);
5310 std::string lookup;
5311
5312 for (size_t i = 1; i < len; i++)
5313 {
5314 lookup.assign (str, i);
5315 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5316 EXPECT (Z_SYM_NAME));
5317 }
5318 }
5319
5320 /* In FULL mode, an incomplete symbol doesn't match. */
5321 {
5322 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
5323 {});
5324 }
5325
5326 /* A complete symbol with parameters matches any overload, since the
5327 index has no overload info. */
5328 {
5329 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
5330 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
5331 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
5332 EXPECT ("std::zfunction", "std::zfunction2"));
5333 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
5334 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
5335 }
5336
5337 /* Check that whitespace is ignored appropriately. A symbol with a
5338 template argument list. */
5339 {
5340 static const char expected[] = "ns::foo<int>";
5341 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
5342 EXPECT (expected));
a20714ff
PA
5343 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
5344 EXPECT (expected));
c62446b1
PA
5345 }
5346
5347 /* Check that whitespace is ignored appropriately. A symbol with a
5348 template argument list that includes a pointer. */
5349 {
5350 static const char expected[] = "ns::foo<char*>";
5351 /* Try both completion and non-completion modes. */
5352 static const bool completion_mode[2] = {false, true};
5353 for (size_t i = 0; i < 2; i++)
5354 {
5355 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
5356 completion_mode[i], EXPECT (expected));
a20714ff
PA
5357 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
5358 completion_mode[i], EXPECT (expected));
c62446b1
PA
5359
5360 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
5361 completion_mode[i], EXPECT (expected));
a20714ff
PA
5362 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
5363 completion_mode[i], EXPECT (expected));
c62446b1
PA
5364 }
5365 }
5366
5367 {
5368 /* Check method qualifiers are ignored. */
5369 static const char expected[] = "ns::foo<char*>";
5370 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
5371 symbol_name_match_type::FULL, true, EXPECT (expected));
5372 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
5373 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
5374 CHECK_MATCH ("foo < char * > ( int ) const",
5375 symbol_name_match_type::WILD, true, EXPECT (expected));
5376 CHECK_MATCH ("foo < char * > ( int ) &&",
5377 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
5378 }
5379
5380 /* Test lookup names that don't match anything. */
5381 {
a20714ff
PA
5382 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
5383 {});
5384
c62446b1
PA
5385 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
5386 {});
5387 }
5388
a20714ff
PA
5389 /* Some wild matching tests, exercising "(anonymous namespace)",
5390 which should not be confused with a parameter list. */
5391 {
5392 static const char *syms[] = {
5393 "A::B::C",
5394 "B::C",
5395 "C",
5396 "A :: B :: C ( int )",
5397 "B :: C ( int )",
5398 "C ( int )",
5399 };
5400
5401 for (const char *s : syms)
5402 {
5403 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
5404 EXPECT ("(anonymous namespace)::A::B::C"));
5405 }
5406 }
5407
5408 {
5409 static const char expected[] = "ns2::tmpl<int>::foo2";
5410 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
5411 EXPECT (expected));
5412 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5413 EXPECT (expected));
5414 }
5415
c62446b1
PA
5416 SELF_CHECK (!any_mismatch);
5417
5418#undef EXPECT
5419#undef CHECK_MATCH
5420}
5421
5c58de74
PA
5422static void
5423run_test ()
5424{
5425 test_mapped_index_find_name_component_bounds ();
5426 test_dw2_expand_symtabs_matching_symbol ();
5427}
5428
c62446b1
PA
5429}} // namespace selftests::dw2_expand_symtabs_matching
5430
5431#endif /* GDB_SELF_TEST */
5432
4b514bc8
JK
5433/* If FILE_MATCHER is NULL or if PER_CU has
5434 dwarf2_per_cu_quick_data::MARK set (see
5435 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5436 EXPANSION_NOTIFY on it. */
5437
5438static void
5439dw2_expand_symtabs_matching_one
5440 (struct dwarf2_per_cu_data *per_cu,
5441 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5442 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5443{
5444 if (file_matcher == NULL || per_cu->v.quick->mark)
5445 {
5446 bool symtab_was_null
5447 = (per_cu->v.quick->compunit_symtab == NULL);
5448
5449 dw2_instantiate_symtab (per_cu);
5450
5451 if (expansion_notify != NULL
5452 && symtab_was_null
5453 && per_cu->v.quick->compunit_symtab != NULL)
5454 expansion_notify (per_cu->v.quick->compunit_symtab);
5455 }
5456}
5457
3f563c84
PA
5458/* Helper for dw2_expand_matching symtabs. Called on each symbol
5459 matched, to expand corresponding CUs that were marked. IDX is the
5460 index of the symbol name that matched. */
5461
5462static void
5463dw2_expand_marked_cus
ed2dc618 5464 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
5465 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5466 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5467 search_domain kind)
5468{
3f563c84
PA
5469 offset_type *vec, vec_len, vec_idx;
5470 bool global_seen = false;
ed2dc618 5471 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 5472
61920122 5473 vec = (offset_type *) (index.constant_pool
f00a2de2 5474 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5475 vec_len = MAYBE_SWAP (vec[0]);
5476 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5477 {
5478 struct dwarf2_per_cu_data *per_cu;
5479 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5480 /* This value is only valid for index versions >= 7. */
5481 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5482 gdb_index_symbol_kind symbol_kind =
5483 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5484 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5485 /* Only check the symbol attributes if they're present.
5486 Indices prior to version 7 don't record them,
5487 and indices >= 7 may elide them for certain symbols
5488 (gold does this). */
5489 int attrs_valid =
5490 (index.version >= 7
5491 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5492
5493 /* Work around gold/15646. */
5494 if (attrs_valid)
9291a0cd 5495 {
61920122
PA
5496 if (!is_static && global_seen)
5497 continue;
5498 if (!is_static)
5499 global_seen = true;
5500 }
3190f0c6 5501
61920122
PA
5502 /* Only check the symbol's kind if it has one. */
5503 if (attrs_valid)
5504 {
5505 switch (kind)
8943b874 5506 {
61920122
PA
5507 case VARIABLES_DOMAIN:
5508 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5509 continue;
5510 break;
5511 case FUNCTIONS_DOMAIN:
5512 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5513 continue;
61920122
PA
5514 break;
5515 case TYPES_DOMAIN:
5516 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5517 continue;
5518 break;
5519 default:
5520 break;
8943b874 5521 }
61920122 5522 }
8943b874 5523
61920122
PA
5524 /* Don't crash on bad data. */
5525 if (cu_index >= (dwarf2_per_objfile->n_comp_units
5526 + dwarf2_per_objfile->n_type_units))
5527 {
5528 complaint (&symfile_complaints,
5529 _(".gdb_index entry has bad CU index"
ed2dc618
SM
5530 " [in module %s]"),
5531 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
5532 continue;
5533 }
5534
ed2dc618 5535 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
4b514bc8
JK
5536 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5537 expansion_notify);
61920122
PA
5538 }
5539}
5540
4b514bc8
JK
5541/* If FILE_MATCHER is non-NULL, set all the
5542 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5543 that match FILE_MATCHER. */
5544
61920122 5545static void
4b514bc8 5546dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
5547 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5548 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5549{
4b514bc8 5550 if (file_matcher == NULL)
61920122
PA
5551 return;
5552
4b514bc8
JK
5553 objfile *const objfile = dwarf2_per_objfile->objfile;
5554
5555 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5556 htab_eq_pointer,
5557 NULL, xcalloc, xfree));
5558 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5559 htab_eq_pointer,
5560 NULL, xcalloc, xfree));
61920122 5561
4b514bc8
JK
5562 /* The rule is CUs specify all the files, including those used by
5563 any TU, so there's no need to scan TUs here. */
61920122 5564
927aa2e7
JK
5565 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5566 {
5567 int j;
ed2dc618 5568 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
927aa2e7
JK
5569 struct quick_file_names *file_data;
5570 void **slot;
5571
5572 QUIT;
5573
5574 per_cu->v.quick->mark = 0;
5575
5576 /* We only need to look at symtabs not already expanded. */
5577 if (per_cu->v.quick->compunit_symtab)
5578 continue;
5579
5580 file_data = dw2_get_file_names (per_cu);
5581 if (file_data == NULL)
5582 continue;
5583
5584 if (htab_find (visited_not_found.get (), file_data) != NULL)
5585 continue;
5586 else if (htab_find (visited_found.get (), file_data) != NULL)
5587 {
5588 per_cu->v.quick->mark = 1;
5589 continue;
5590 }
5591
5592 for (j = 0; j < file_data->num_file_names; ++j)
5593 {
5594 const char *this_real_name;
5595
5596 if (file_matcher (file_data->file_names[j], false))
5597 {
5598 per_cu->v.quick->mark = 1;
5599 break;
5600 }
5601
5602 /* Before we invoke realpath, which can get expensive when many
5603 files are involved, do a quick comparison of the basenames. */
5604 if (!basenames_may_differ
5605 && !file_matcher (lbasename (file_data->file_names[j]),
5606 true))
5607 continue;
5608
5609 this_real_name = dw2_get_real_path (objfile, file_data, j);
5610 if (file_matcher (this_real_name, false))
5611 {
5612 per_cu->v.quick->mark = 1;
5613 break;
5614 }
5615 }
5616
5617 slot = htab_find_slot (per_cu->v.quick->mark
5618 ? visited_found.get ()
5619 : visited_not_found.get (),
5620 file_data, INSERT);
5621 *slot = file_data;
5622 }
5623}
5624
5625static void
5626dw2_expand_symtabs_matching
5627 (struct objfile *objfile,
5628 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5629 const lookup_name_info &lookup_name,
5630 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5631 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5632 enum search_domain kind)
5633{
ed2dc618
SM
5634 struct dwarf2_per_objfile *dwarf2_per_objfile
5635 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5636
5637 /* index_table is NULL if OBJF_READNOW. */
5638 if (!dwarf2_per_objfile->index_table)
5639 return;
5640
ed2dc618 5641 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
5642
5643 mapped_index &index = *dwarf2_per_objfile->index_table;
5644
5645 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5646 symbol_matcher,
5647 kind, [&] (offset_type idx)
5648 {
ed2dc618 5649 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7
JK
5650 expansion_notify, kind);
5651 });
5652}
5653
5654/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5655 symtab. */
5656
5657static struct compunit_symtab *
5658recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5659 CORE_ADDR pc)
5660{
5661 int i;
5662
5663 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5664 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5665 return cust;
5666
5667 if (cust->includes == NULL)
5668 return NULL;
5669
5670 for (i = 0; cust->includes[i]; ++i)
5671 {
5672 struct compunit_symtab *s = cust->includes[i];
5673
5674 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5675 if (s != NULL)
5676 return s;
5677 }
5678
5679 return NULL;
5680}
5681
5682static struct compunit_symtab *
5683dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5684 struct bound_minimal_symbol msymbol,
5685 CORE_ADDR pc,
5686 struct obj_section *section,
5687 int warn_if_readin)
5688{
5689 struct dwarf2_per_cu_data *data;
5690 struct compunit_symtab *result;
5691
927aa2e7
JK
5692 if (!objfile->psymtabs_addrmap)
5693 return NULL;
5694
5695 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5696 pc);
5697 if (!data)
5698 return NULL;
5699
5700 if (warn_if_readin && data->v.quick->compunit_symtab)
5701 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5702 paddress (get_objfile_arch (objfile), pc));
5703
5704 result
5705 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5706 pc);
5707 gdb_assert (result != NULL);
5708 return result;
5709}
5710
5711static void
5712dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5713 void *data, int need_fullname)
5714{
ed2dc618
SM
5715 struct dwarf2_per_objfile *dwarf2_per_objfile
5716 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5717
5718 if (!dwarf2_per_objfile->filenames_cache)
5719 {
5720 dwarf2_per_objfile->filenames_cache.emplace ();
5721
5722 htab_up visited (htab_create_alloc (10,
5723 htab_hash_pointer, htab_eq_pointer,
5724 NULL, xcalloc, xfree));
5725
5726 /* The rule is CUs specify all the files, including those used
5727 by any TU, so there's no need to scan TUs here. We can
5728 ignore file names coming from already-expanded CUs. */
5729
5730 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5731 {
ed2dc618 5732 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
927aa2e7
JK
5733
5734 if (per_cu->v.quick->compunit_symtab)
5735 {
5736 void **slot = htab_find_slot (visited.get (),
5737 per_cu->v.quick->file_names,
5738 INSERT);
5739
5740 *slot = per_cu->v.quick->file_names;
5741 }
5742 }
5743
5744 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5745 {
ed2dc618 5746 dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
927aa2e7
JK
5747 struct quick_file_names *file_data;
5748 void **slot;
5749
5750 /* We only need to look at symtabs not already expanded. */
5751 if (per_cu->v.quick->compunit_symtab)
5752 continue;
5753
5754 file_data = dw2_get_file_names (per_cu);
5755 if (file_data == NULL)
5756 continue;
5757
5758 slot = htab_find_slot (visited.get (), file_data, INSERT);
5759 if (*slot)
5760 {
5761 /* Already visited. */
5762 continue;
5763 }
5764 *slot = file_data;
5765
5766 for (int j = 0; j < file_data->num_file_names; ++j)
5767 {
5768 const char *filename = file_data->file_names[j];
5769 dwarf2_per_objfile->filenames_cache->seen (filename);
5770 }
5771 }
5772 }
5773
5774 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5775 {
5776 gdb::unique_xmalloc_ptr<char> this_real_name;
5777
5778 if (need_fullname)
5779 this_real_name = gdb_realpath (filename);
5780 (*fun) (filename, this_real_name.get (), data);
5781 });
5782}
5783
5784static int
5785dw2_has_symbols (struct objfile *objfile)
5786{
5787 return 1;
5788}
5789
5790const struct quick_symbol_functions dwarf2_gdb_index_functions =
5791{
5792 dw2_has_symbols,
5793 dw2_find_last_source_symtab,
5794 dw2_forget_cached_source_info,
5795 dw2_map_symtabs_matching_filename,
5796 dw2_lookup_symbol,
5797 dw2_print_stats,
5798 dw2_dump,
5799 dw2_relocate,
5800 dw2_expand_symtabs_for_function,
5801 dw2_expand_all_symtabs,
5802 dw2_expand_symtabs_with_fullname,
5803 dw2_map_matching_symbols,
5804 dw2_expand_symtabs_matching,
5805 dw2_find_pc_sect_compunit_symtab,
5806 NULL,
5807 dw2_map_symbol_filenames
5808};
5809
5810/* DWARF-5 debug_names reader. */
5811
5812/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5813static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5814
5815/* A helper function that reads the .debug_names section in SECTION
5816 and fills in MAP. FILENAME is the name of the file containing the
5817 section; it is used for error reporting.
5818
5819 Returns true if all went well, false otherwise. */
5820
5821static bool
5822read_debug_names_from_section (struct objfile *objfile,
5823 const char *filename,
5824 struct dwarf2_section_info *section,
5825 mapped_debug_names &map)
5826{
5827 if (dwarf2_section_empty_p (section))
5828 return false;
5829
5830 /* Older elfutils strip versions could keep the section in the main
5831 executable while splitting it for the separate debug info file. */
5832 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5833 return false;
5834
5835 dwarf2_read_section (objfile, section);
5836
5837 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5838
5839 const gdb_byte *addr = section->buffer;
5840
5841 bfd *const abfd = get_section_bfd_owner (section);
5842
5843 unsigned int bytes_read;
5844 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5845 addr += bytes_read;
5846
5847 map.dwarf5_is_dwarf64 = bytes_read != 4;
5848 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5849 if (bytes_read + length != section->size)
5850 {
5851 /* There may be multiple per-CU indices. */
5852 warning (_("Section .debug_names in %s length %s does not match "
5853 "section length %s, ignoring .debug_names."),
5854 filename, plongest (bytes_read + length),
5855 pulongest (section->size));
5856 return false;
5857 }
5858
5859 /* The version number. */
5860 uint16_t version = read_2_bytes (abfd, addr);
5861 addr += 2;
5862 if (version != 5)
5863 {
5864 warning (_("Section .debug_names in %s has unsupported version %d, "
5865 "ignoring .debug_names."),
5866 filename, version);
5867 return false;
5868 }
5869
5870 /* Padding. */
5871 uint16_t padding = read_2_bytes (abfd, addr);
5872 addr += 2;
5873 if (padding != 0)
5874 {
5875 warning (_("Section .debug_names in %s has unsupported padding %d, "
5876 "ignoring .debug_names."),
5877 filename, padding);
5878 return false;
5879 }
5880
5881 /* comp_unit_count - The number of CUs in the CU list. */
5882 map.cu_count = read_4_bytes (abfd, addr);
5883 addr += 4;
5884
5885 /* local_type_unit_count - The number of TUs in the local TU
5886 list. */
5887 map.tu_count = read_4_bytes (abfd, addr);
5888 addr += 4;
5889
5890 /* foreign_type_unit_count - The number of TUs in the foreign TU
5891 list. */
5892 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5893 addr += 4;
5894 if (foreign_tu_count != 0)
5895 {
5896 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5897 "ignoring .debug_names."),
5898 filename, static_cast<unsigned long> (foreign_tu_count));
5899 return false;
5900 }
5901
5902 /* bucket_count - The number of hash buckets in the hash lookup
5903 table. */
5904 map.bucket_count = read_4_bytes (abfd, addr);
5905 addr += 4;
5906
5907 /* name_count - The number of unique names in the index. */
5908 map.name_count = read_4_bytes (abfd, addr);
5909 addr += 4;
5910
5911 /* abbrev_table_size - The size in bytes of the abbreviations
5912 table. */
5913 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5914 addr += 4;
5915
5916 /* augmentation_string_size - The size in bytes of the augmentation
5917 string. This value is rounded up to a multiple of 4. */
5918 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5919 addr += 4;
5920 map.augmentation_is_gdb = ((augmentation_string_size
5921 == sizeof (dwarf5_augmentation))
5922 && memcmp (addr, dwarf5_augmentation,
5923 sizeof (dwarf5_augmentation)) == 0);
5924 augmentation_string_size += (-augmentation_string_size) & 3;
5925 addr += augmentation_string_size;
5926
5927 /* List of CUs */
5928 map.cu_table_reordered = addr;
5929 addr += map.cu_count * map.offset_size;
5930
5931 /* List of Local TUs */
5932 map.tu_table_reordered = addr;
5933 addr += map.tu_count * map.offset_size;
5934
5935 /* Hash Lookup Table */
5936 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5937 addr += map.bucket_count * 4;
5938 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5939 addr += map.name_count * 4;
5940
5941 /* Name Table */
5942 map.name_table_string_offs_reordered = addr;
5943 addr += map.name_count * map.offset_size;
5944 map.name_table_entry_offs_reordered = addr;
5945 addr += map.name_count * map.offset_size;
5946
5947 const gdb_byte *abbrev_table_start = addr;
5948 for (;;)
5949 {
5950 unsigned int bytes_read;
5951 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5952 addr += bytes_read;
5953 if (index_num == 0)
5954 break;
5955
5956 const auto insertpair
5957 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5958 if (!insertpair.second)
5959 {
5960 warning (_("Section .debug_names in %s has duplicate index %s, "
5961 "ignoring .debug_names."),
5962 filename, pulongest (index_num));
5963 return false;
5964 }
5965 mapped_debug_names::index_val &indexval = insertpair.first->second;
5966 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5967 addr += bytes_read;
5968
5969 for (;;)
5970 {
5971 mapped_debug_names::index_val::attr attr;
5972 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5973 addr += bytes_read;
5974 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5975 addr += bytes_read;
5976 if (attr.form == DW_FORM_implicit_const)
5977 {
5978 attr.implicit_const = read_signed_leb128 (abfd, addr,
5979 &bytes_read);
5980 addr += bytes_read;
5981 }
5982 if (attr.dw_idx == 0 && attr.form == 0)
5983 break;
5984 indexval.attr_vec.push_back (std::move (attr));
5985 }
5986 }
5987 if (addr != abbrev_table_start + abbrev_table_size)
5988 {
5989 warning (_("Section .debug_names in %s has abbreviation_table "
5990 "of size %zu vs. written as %u, ignoring .debug_names."),
5991 filename, addr - abbrev_table_start, abbrev_table_size);
5992 return false;
5993 }
5994 map.entry_pool = addr;
5995
5996 return true;
5997}
5998
5999/* A helper for create_cus_from_debug_names that handles the MAP's CU
6000 list. */
6001
6002static void
ed2dc618 6003create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
6004 const mapped_debug_names &map,
6005 dwarf2_section_info &section,
6006 bool is_dwz, int base_offset)
6007{
6008 sect_offset sect_off_prev;
6009 for (uint32_t i = 0; i <= map.cu_count; ++i)
6010 {
6011 sect_offset sect_off_next;
6012 if (i < map.cu_count)
6013 {
6014 sect_off_next
6015 = (sect_offset) (extract_unsigned_integer
6016 (map.cu_table_reordered + i * map.offset_size,
6017 map.offset_size,
6018 map.dwarf5_byte_order));
6019 }
6020 else
6021 sect_off_next = (sect_offset) section.size;
6022 if (i >= 1)
6023 {
6024 const ULONGEST length = sect_off_next - sect_off_prev;
6025 dwarf2_per_objfile->all_comp_units[base_offset + (i - 1)]
ed2dc618 6026 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7
JK
6027 sect_off_prev, length);
6028 }
6029 sect_off_prev = sect_off_next;
6030 }
6031}
6032
6033/* Read the CU list from the mapped index, and use it to create all
ed2dc618 6034 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
6035
6036static void
ed2dc618 6037create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
6038 const mapped_debug_names &map,
6039 const mapped_debug_names &dwz_map)
6040{
ed2dc618 6041 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
6042
6043 dwarf2_per_objfile->n_comp_units = map.cu_count + dwz_map.cu_count;
6044 dwarf2_per_objfile->all_comp_units
6045 = XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
6046 dwarf2_per_objfile->n_comp_units);
6047
ed2dc618
SM
6048 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
6049 dwarf2_per_objfile->info,
927aa2e7
JK
6050 false /* is_dwz */,
6051 0 /* base_offset */);
6052
6053 if (dwz_map.cu_count == 0)
6054 return;
6055
ed2dc618
SM
6056 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
6057 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
927aa2e7
JK
6058 true /* is_dwz */,
6059 map.cu_count /* base_offset */);
6060}
6061
6062/* Read .debug_names. If everything went ok, initialize the "quick"
6063 elements of all the CUs and return true. Otherwise, return false. */
6064
6065static bool
ed2dc618 6066dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 6067{
ed2dc618
SM
6068 mapped_debug_names local_map (dwarf2_per_objfile);
6069 mapped_debug_names dwz_map (dwarf2_per_objfile);
6070 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
6071
6072 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
6073 &dwarf2_per_objfile->debug_names,
6074 local_map))
6075 return false;
6076
6077 /* Don't use the index if it's empty. */
6078 if (local_map.name_count == 0)
6079 return false;
6080
6081 /* If there is a .dwz file, read it so we can get its CU list as
6082 well. */
ed2dc618 6083 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
6084 if (dwz != NULL)
6085 {
6086 if (!read_debug_names_from_section (objfile,
6087 bfd_get_filename (dwz->dwz_bfd),
6088 &dwz->debug_names, dwz_map))
6089 {
6090 warning (_("could not read '.debug_names' section from %s; skipping"),
6091 bfd_get_filename (dwz->dwz_bfd));
6092 return false;
6093 }
6094 }
6095
ed2dc618 6096 create_cus_from_debug_names (dwarf2_per_objfile, local_map, dwz_map);
927aa2e7
JK
6097
6098 if (local_map.tu_count != 0)
6099 {
6100 /* We can only handle a single .debug_types when we have an
6101 index. */
6102 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
6103 return false;
6104
6105 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
6106 dwarf2_per_objfile->types, 0);
6107
6108 create_signatured_type_table_from_debug_names
ed2dc618 6109 (dwarf2_per_objfile, local_map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
6110 }
6111
ed2dc618
SM
6112 create_addrmap_from_aranges (dwarf2_per_objfile,
6113 &dwarf2_per_objfile->debug_aranges);
927aa2e7 6114
ed2dc618
SM
6115 dwarf2_per_objfile->debug_names_table.reset
6116 (new mapped_debug_names (dwarf2_per_objfile));
927aa2e7
JK
6117 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
6118 dwarf2_per_objfile->using_index = 1;
6119 dwarf2_per_objfile->quick_file_names_table =
6120 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6121
6122 return true;
6123}
6124
6125/* Symbol name hashing function as specified by DWARF-5. */
6126
6127static uint32_t
6128dwarf5_djb_hash (const char *str_)
6129{
6130 const unsigned char *str = (const unsigned char *) str_;
6131
6132 /* Note: tolower here ignores UTF-8, which isn't fully compliant.
6133 See http://dwarfstd.org/ShowIssue.php?issue=161027.1. */
6134
6135 uint32_t hash = 5381;
6136 while (int c = *str++)
6137 hash = hash * 33 + tolower (c);
6138 return hash;
6139}
6140
6141/* Type used to manage iterating over all CUs looking for a symbol for
6142 .debug_names. */
6143
6144class dw2_debug_names_iterator
6145{
6146public:
6147 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
6148 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
6149 dw2_debug_names_iterator (const mapped_debug_names &map,
6150 bool want_specific_block,
6151 block_enum block_index, domain_enum domain,
6152 const char *name)
6153 : m_map (map), m_want_specific_block (want_specific_block),
6154 m_block_index (block_index), m_domain (domain),
6155 m_addr (find_vec_in_debug_names (map, name))
6156 {}
6157
6158 dw2_debug_names_iterator (const mapped_debug_names &map,
6159 search_domain search, uint32_t namei)
6160 : m_map (map),
6161 m_search (search),
6162 m_addr (find_vec_in_debug_names (map, namei))
6163 {}
6164
6165 /* Return the next matching CU or NULL if there are no more. */
6166 dwarf2_per_cu_data *next ();
6167
6168private:
6169 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6170 const char *name);
6171 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6172 uint32_t namei);
6173
6174 /* The internalized form of .debug_names. */
6175 const mapped_debug_names &m_map;
6176
6177 /* If true, only look for symbols that match BLOCK_INDEX. */
6178 const bool m_want_specific_block = false;
6179
6180 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
6181 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
6182 value. */
6183 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
6184
6185 /* The kind of symbol we're looking for. */
6186 const domain_enum m_domain = UNDEF_DOMAIN;
6187 const search_domain m_search = ALL_DOMAIN;
6188
6189 /* The list of CUs from the index entry of the symbol, or NULL if
6190 not found. */
6191 const gdb_byte *m_addr;
6192};
6193
6194const char *
6195mapped_debug_names::namei_to_name (uint32_t namei) const
6196{
6197 const ULONGEST namei_string_offs
6198 = extract_unsigned_integer ((name_table_string_offs_reordered
6199 + namei * offset_size),
6200 offset_size,
6201 dwarf5_byte_order);
6202 return read_indirect_string_at_offset
ed2dc618 6203 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
6204}
6205
6206/* Find a slot in .debug_names for the object named NAME. If NAME is
6207 found, return pointer to its pool data. If NAME cannot be found,
6208 return NULL. */
6209
6210const gdb_byte *
6211dw2_debug_names_iterator::find_vec_in_debug_names
6212 (const mapped_debug_names &map, const char *name)
6213{
6214 int (*cmp) (const char *, const char *);
6215
6216 if (current_language->la_language == language_cplus
6217 || current_language->la_language == language_fortran
6218 || current_language->la_language == language_d)
6219 {
6220 /* NAME is already canonical. Drop any qualifiers as
6221 .debug_names does not contain any. */
6222
6223 if (strchr (name, '(') != NULL)
6224 {
6225 gdb::unique_xmalloc_ptr<char> without_params
6226 = cp_remove_params (name);
6227
6228 if (without_params != NULL)
6229 {
6230 name = without_params.get();
6231 }
6232 }
6233 }
6234
6235 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
6236
6237 const uint32_t full_hash = dwarf5_djb_hash (name);
6238 uint32_t namei
6239 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6240 (map.bucket_table_reordered
6241 + (full_hash % map.bucket_count)), 4,
6242 map.dwarf5_byte_order);
6243 if (namei == 0)
6244 return NULL;
6245 --namei;
6246 if (namei >= map.name_count)
6247 {
6248 complaint (&symfile_complaints,
6249 _("Wrong .debug_names with name index %u but name_count=%u "
6250 "[in module %s]"),
6251 namei, map.name_count,
ed2dc618 6252 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
6253 return NULL;
6254 }
6255
6256 for (;;)
6257 {
6258 const uint32_t namei_full_hash
6259 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6260 (map.hash_table_reordered + namei), 4,
6261 map.dwarf5_byte_order);
6262 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
6263 return NULL;
6264
6265 if (full_hash == namei_full_hash)
6266 {
6267 const char *const namei_string = map.namei_to_name (namei);
6268
6269#if 0 /* An expensive sanity check. */
6270 if (namei_full_hash != dwarf5_djb_hash (namei_string))
6271 {
6272 complaint (&symfile_complaints,
6273 _("Wrong .debug_names hash for string at index %u "
6274 "[in module %s]"),
6275 namei, objfile_name (dwarf2_per_objfile->objfile));
6276 return NULL;
6277 }
6278#endif
6279
6280 if (cmp (namei_string, name) == 0)
6281 {
6282 const ULONGEST namei_entry_offs
6283 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6284 + namei * map.offset_size),
6285 map.offset_size, map.dwarf5_byte_order);
6286 return map.entry_pool + namei_entry_offs;
6287 }
6288 }
6289
6290 ++namei;
6291 if (namei >= map.name_count)
6292 return NULL;
6293 }
6294}
6295
6296const gdb_byte *
6297dw2_debug_names_iterator::find_vec_in_debug_names
6298 (const mapped_debug_names &map, uint32_t namei)
6299{
6300 if (namei >= map.name_count)
6301 {
6302 complaint (&symfile_complaints,
6303 _("Wrong .debug_names with name index %u but name_count=%u "
6304 "[in module %s]"),
6305 namei, map.name_count,
ed2dc618 6306 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
6307 return NULL;
6308 }
6309
6310 const ULONGEST namei_entry_offs
6311 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6312 + namei * map.offset_size),
6313 map.offset_size, map.dwarf5_byte_order);
6314 return map.entry_pool + namei_entry_offs;
6315}
6316
6317/* See dw2_debug_names_iterator. */
6318
6319dwarf2_per_cu_data *
6320dw2_debug_names_iterator::next ()
6321{
6322 if (m_addr == NULL)
6323 return NULL;
6324
ed2dc618
SM
6325 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
6326 struct objfile *objfile = dwarf2_per_objfile->objfile;
6327 bfd *const abfd = objfile->obfd;
927aa2e7
JK
6328
6329 again:
6330
6331 unsigned int bytes_read;
6332 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6333 m_addr += bytes_read;
6334 if (abbrev == 0)
6335 return NULL;
6336
6337 const auto indexval_it = m_map.abbrev_map.find (abbrev);
6338 if (indexval_it == m_map.abbrev_map.cend ())
6339 {
6340 complaint (&symfile_complaints,
6341 _("Wrong .debug_names undefined abbrev code %s "
6342 "[in module %s]"),
ed2dc618 6343 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
6344 return NULL;
6345 }
6346 const mapped_debug_names::index_val &indexval = indexval_it->second;
6347 bool have_is_static = false;
6348 bool is_static;
6349 dwarf2_per_cu_data *per_cu = NULL;
6350 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
6351 {
6352 ULONGEST ull;
6353 switch (attr.form)
6354 {
6355 case DW_FORM_implicit_const:
6356 ull = attr.implicit_const;
6357 break;
6358 case DW_FORM_flag_present:
6359 ull = 1;
6360 break;
6361 case DW_FORM_udata:
6362 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6363 m_addr += bytes_read;
6364 break;
6365 default:
6366 complaint (&symfile_complaints,
6367 _("Unsupported .debug_names form %s [in module %s]"),
6368 dwarf_form_name (attr.form),
ed2dc618 6369 objfile_name (objfile));
927aa2e7
JK
6370 return NULL;
6371 }
6372 switch (attr.dw_idx)
6373 {
6374 case DW_IDX_compile_unit:
6375 /* Don't crash on bad data. */
8af5c486 6376 if (ull >= dwarf2_per_objfile->n_comp_units)
927aa2e7
JK
6377 {
6378 complaint (&symfile_complaints,
6379 _(".debug_names entry has bad CU index %s"
6380 " [in module %s]"),
6381 pulongest (ull),
6382 objfile_name (dwarf2_per_objfile->objfile));
6383 continue;
6384 }
ed2dc618 6385 per_cu = dw2_get_cutu (dwarf2_per_objfile, ull);
927aa2e7 6386 break;
8af5c486
JK
6387 case DW_IDX_type_unit:
6388 /* Don't crash on bad data. */
6389 if (ull >= dwarf2_per_objfile->n_type_units)
6390 {
6391 complaint (&symfile_complaints,
6392 _(".debug_names entry has bad TU index %s"
6393 " [in module %s]"),
6394 pulongest (ull),
6395 objfile_name (dwarf2_per_objfile->objfile));
6396 continue;
6397 }
ed2dc618
SM
6398 per_cu = dw2_get_cutu (dwarf2_per_objfile,
6399 dwarf2_per_objfile->n_comp_units + ull);
8af5c486 6400 break;
927aa2e7
JK
6401 case DW_IDX_GNU_internal:
6402 if (!m_map.augmentation_is_gdb)
6403 break;
6404 have_is_static = true;
6405 is_static = true;
6406 break;
6407 case DW_IDX_GNU_external:
6408 if (!m_map.augmentation_is_gdb)
6409 break;
6410 have_is_static = true;
6411 is_static = false;
6412 break;
6413 }
6414 }
6415
6416 /* Skip if already read in. */
6417 if (per_cu->v.quick->compunit_symtab)
6418 goto again;
6419
6420 /* Check static vs global. */
6421 if (have_is_static)
6422 {
6423 const bool want_static = m_block_index != GLOBAL_BLOCK;
6424 if (m_want_specific_block && want_static != is_static)
6425 goto again;
6426 }
6427
6428 /* Match dw2_symtab_iter_next, symbol_kind
6429 and debug_names::psymbol_tag. */
6430 switch (m_domain)
6431 {
6432 case VAR_DOMAIN:
6433 switch (indexval.dwarf_tag)
6434 {
6435 case DW_TAG_variable:
6436 case DW_TAG_subprogram:
6437 /* Some types are also in VAR_DOMAIN. */
6438 case DW_TAG_typedef:
6439 case DW_TAG_structure_type:
6440 break;
6441 default:
6442 goto again;
6443 }
6444 break;
6445 case STRUCT_DOMAIN:
6446 switch (indexval.dwarf_tag)
6447 {
6448 case DW_TAG_typedef:
6449 case DW_TAG_structure_type:
6450 break;
6451 default:
6452 goto again;
6453 }
6454 break;
6455 case LABEL_DOMAIN:
6456 switch (indexval.dwarf_tag)
6457 {
6458 case 0:
6459 case DW_TAG_variable:
6460 break;
6461 default:
6462 goto again;
6463 }
6464 break;
6465 default:
6466 break;
6467 }
6468
6469 /* Match dw2_expand_symtabs_matching, symbol_kind and
6470 debug_names::psymbol_tag. */
6471 switch (m_search)
4b514bc8 6472 {
927aa2e7
JK
6473 case VARIABLES_DOMAIN:
6474 switch (indexval.dwarf_tag)
4b514bc8 6475 {
927aa2e7
JK
6476 case DW_TAG_variable:
6477 break;
6478 default:
6479 goto again;
4b514bc8 6480 }
927aa2e7
JK
6481 break;
6482 case FUNCTIONS_DOMAIN:
6483 switch (indexval.dwarf_tag)
4b514bc8 6484 {
927aa2e7
JK
6485 case DW_TAG_subprogram:
6486 break;
6487 default:
6488 goto again;
4b514bc8 6489 }
927aa2e7
JK
6490 break;
6491 case TYPES_DOMAIN:
6492 switch (indexval.dwarf_tag)
6493 {
6494 case DW_TAG_typedef:
6495 case DW_TAG_structure_type:
6496 break;
6497 default:
6498 goto again;
6499 }
6500 break;
6501 default:
6502 break;
4b514bc8 6503 }
927aa2e7
JK
6504
6505 return per_cu;
4b514bc8 6506}
61920122 6507
927aa2e7
JK
6508static struct compunit_symtab *
6509dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6510 const char *name, domain_enum domain)
4b514bc8 6511{
927aa2e7 6512 const block_enum block_index = static_cast<block_enum> (block_index_int);
ed2dc618
SM
6513 struct dwarf2_per_objfile *dwarf2_per_objfile
6514 = get_dwarf2_per_objfile (objfile);
61920122 6515
927aa2e7
JK
6516 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6517 if (!mapp)
61920122 6518 {
927aa2e7
JK
6519 /* index is NULL if OBJF_READNOW. */
6520 return NULL;
6521 }
6522 const auto &map = *mapp;
9291a0cd 6523
927aa2e7
JK
6524 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6525 block_index, domain, name);
9703b513 6526
927aa2e7
JK
6527 struct compunit_symtab *stab_best = NULL;
6528 struct dwarf2_per_cu_data *per_cu;
6529 while ((per_cu = iter.next ()) != NULL)
6530 {
6531 struct symbol *sym, *with_opaque = NULL;
6532 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6533 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6534 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6535
927aa2e7
JK
6536 sym = block_find_symbol (block, name, domain,
6537 block_find_non_opaque_type_preferred,
6538 &with_opaque);
9703b513 6539
927aa2e7
JK
6540 /* Some caution must be observed with overloaded functions and
6541 methods, since the index will not contain any overload
6542 information (but NAME might contain it). */
a3ec0bb1 6543
927aa2e7
JK
6544 if (sym != NULL
6545 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6546 return stab;
6547 if (with_opaque != NULL
6548 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6549 stab_best = stab;
9703b513 6550
927aa2e7 6551 /* Keep looking through other CUs. */
9703b513
TT
6552 }
6553
927aa2e7 6554 return stab_best;
9703b513
TT
6555}
6556
927aa2e7
JK
6557/* This dumps minimal information about .debug_names. It is called
6558 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6559 uses this to verify that .debug_names has been loaded. */
9291a0cd 6560
927aa2e7
JK
6561static void
6562dw2_debug_names_dump (struct objfile *objfile)
6563{
ed2dc618
SM
6564 struct dwarf2_per_objfile *dwarf2_per_objfile
6565 = get_dwarf2_per_objfile (objfile);
6566
927aa2e7
JK
6567 gdb_assert (dwarf2_per_objfile->using_index);
6568 printf_filtered (".debug_names:");
6569 if (dwarf2_per_objfile->debug_names_table)
6570 printf_filtered (" exists\n");
6571 else
6572 printf_filtered (" faked for \"readnow\"\n");
6573 printf_filtered ("\n");
9291a0cd
TT
6574}
6575
9291a0cd 6576static void
927aa2e7
JK
6577dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6578 const char *func_name)
9291a0cd 6579{
ed2dc618
SM
6580 struct dwarf2_per_objfile *dwarf2_per_objfile
6581 = get_dwarf2_per_objfile (objfile);
ae2de4f8 6582
927aa2e7
JK
6583 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6584 if (dwarf2_per_objfile->debug_names_table)
24c79950 6585 {
927aa2e7 6586 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6587
927aa2e7
JK
6588 /* Note: It doesn't matter what we pass for block_index here. */
6589 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6590 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
24c79950 6591
927aa2e7
JK
6592 struct dwarf2_per_cu_data *per_cu;
6593 while ((per_cu = iter.next ()) != NULL)
6594 dw2_instantiate_symtab (per_cu);
6595 }
6596}
24c79950 6597
927aa2e7
JK
6598static void
6599dw2_debug_names_expand_symtabs_matching
6600 (struct objfile *objfile,
6601 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6602 const lookup_name_info &lookup_name,
6603 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6604 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6605 enum search_domain kind)
6606{
ed2dc618
SM
6607 struct dwarf2_per_objfile *dwarf2_per_objfile
6608 = get_dwarf2_per_objfile (objfile);
9291a0cd 6609
927aa2e7
JK
6610 /* debug_names_table is NULL if OBJF_READNOW. */
6611 if (!dwarf2_per_objfile->debug_names_table)
6612 return;
9291a0cd 6613
ed2dc618 6614 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 6615
44ed8f3e 6616 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6617
44ed8f3e
PA
6618 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6619 symbol_matcher,
6620 kind, [&] (offset_type namei)
927aa2e7 6621 {
927aa2e7
JK
6622 /* The name was matched, now expand corresponding CUs that were
6623 marked. */
6624 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6625
927aa2e7
JK
6626 struct dwarf2_per_cu_data *per_cu;
6627 while ((per_cu = iter.next ()) != NULL)
6628 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6629 expansion_notify);
44ed8f3e 6630 });
9291a0cd
TT
6631}
6632
927aa2e7 6633const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6634{
6635 dw2_has_symbols,
6636 dw2_find_last_source_symtab,
6637 dw2_forget_cached_source_info,
f8eba3c6 6638 dw2_map_symtabs_matching_filename,
927aa2e7 6639 dw2_debug_names_lookup_symbol,
9291a0cd 6640 dw2_print_stats,
927aa2e7 6641 dw2_debug_names_dump,
9291a0cd 6642 dw2_relocate,
927aa2e7 6643 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6644 dw2_expand_all_symtabs,
652a8996 6645 dw2_expand_symtabs_with_fullname,
40658b94 6646 dw2_map_matching_symbols,
927aa2e7 6647 dw2_debug_names_expand_symtabs_matching,
43f3e411 6648 dw2_find_pc_sect_compunit_symtab,
71a3c369 6649 NULL,
9291a0cd
TT
6650 dw2_map_symbol_filenames
6651};
6652
3c0aa29a 6653/* See symfile.h. */
9291a0cd 6654
3c0aa29a
PA
6655bool
6656dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6657{
ed2dc618
SM
6658 struct dwarf2_per_objfile *dwarf2_per_objfile
6659 = get_dwarf2_per_objfile (objfile);
6660
9291a0cd
TT
6661 /* If we're about to read full symbols, don't bother with the
6662 indices. In this case we also don't care if some other debug
6663 format is making psymtabs, because they are all about to be
6664 expanded anyway. */
6665 if ((objfile->flags & OBJF_READNOW))
6666 {
6667 int i;
6668
6669 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
6670 create_all_comp_units (dwarf2_per_objfile);
6671 create_all_type_units (dwarf2_per_objfile);
7b9f3c50
DE
6672 dwarf2_per_objfile->quick_file_names_table =
6673 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 6674
1fd400ff 6675 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 6676 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 6677 {
ed2dc618 6678 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
9291a0cd 6679
e254ef6a
DE
6680 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6681 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6682 }
6683
6684 /* Return 1 so that gdb sees the "quick" functions. However,
6685 these functions will be no-ops because we will have expanded
6686 all symtabs. */
3c0aa29a
PA
6687 *index_kind = dw_index_kind::GDB_INDEX;
6688 return true;
9291a0cd
TT
6689 }
6690
ed2dc618 6691 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6692 {
6693 *index_kind = dw_index_kind::DEBUG_NAMES;
6694 return true;
6695 }
927aa2e7 6696
9291a0cd 6697 if (dwarf2_read_index (objfile))
3c0aa29a
PA
6698 {
6699 *index_kind = dw_index_kind::GDB_INDEX;
6700 return true;
6701 }
9291a0cd 6702
3c0aa29a 6703 return false;
9291a0cd
TT
6704}
6705
6706\f
6707
dce234bc
PP
6708/* Build a partial symbol table. */
6709
6710void
f29dff0a 6711dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6712{
ed2dc618
SM
6713 struct dwarf2_per_objfile *dwarf2_per_objfile
6714 = get_dwarf2_per_objfile (objfile);
c9bf0622 6715
af5bf4ad
SM
6716 if (objfile->global_psymbols.capacity () == 0
6717 && objfile->static_psymbols.capacity () == 0)
6718 init_psymbol_list (objfile, 1024);
c906108c 6719
492d29ea 6720 TRY
c9bf0622
TT
6721 {
6722 /* This isn't really ideal: all the data we allocate on the
6723 objfile's obstack is still uselessly kept around. However,
6724 freeing it seems unsafe. */
906768f9 6725 psymtab_discarder psymtabs (objfile);
ed2dc618 6726 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6727 psymtabs.keep ();
c9bf0622 6728 }
492d29ea
PA
6729 CATCH (except, RETURN_MASK_ERROR)
6730 {
6731 exception_print (gdb_stderr, except);
6732 }
6733 END_CATCH
c906108c 6734}
c906108c 6735
1ce1cefd
DE
6736/* Return the total length of the CU described by HEADER. */
6737
6738static unsigned int
6739get_cu_length (const struct comp_unit_head *header)
6740{
6741 return header->initial_length_size + header->length;
6742}
6743
9c541725 6744/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6745
9c541725
PA
6746static inline bool
6747offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6748{
9c541725
PA
6749 sect_offset bottom = cu_header->sect_off;
6750 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6751
9c541725 6752 return sect_off >= bottom && sect_off < top;
45452591
DE
6753}
6754
3b80fe9b
DE
6755/* Find the base address of the compilation unit for range lists and
6756 location lists. It will normally be specified by DW_AT_low_pc.
6757 In DWARF-3 draft 4, the base address could be overridden by
6758 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6759 compilation units with discontinuous ranges. */
6760
6761static void
6762dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6763{
6764 struct attribute *attr;
6765
6766 cu->base_known = 0;
6767 cu->base_address = 0;
6768
6769 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6770 if (attr)
6771 {
31aa7e4e 6772 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6773 cu->base_known = 1;
6774 }
6775 else
6776 {
6777 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6778 if (attr)
6779 {
31aa7e4e 6780 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6781 cu->base_known = 1;
6782 }
6783 }
6784}
6785
93311388 6786/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6787 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6788 NOTE: This leaves members offset, first_die_offset to be filled in
6789 by the caller. */
107d2387 6790
d521ce57 6791static const gdb_byte *
107d2387 6792read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6793 const gdb_byte *info_ptr,
6794 struct dwarf2_section_info *section,
6795 rcuh_kind section_kind)
107d2387
AC
6796{
6797 int signed_addr;
891d2f0b 6798 unsigned int bytes_read;
43988095
JK
6799 const char *filename = get_section_file_name (section);
6800 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6801
6802 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6803 cu_header->initial_length_size = bytes_read;
6804 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6805 info_ptr += bytes_read;
107d2387
AC
6806 cu_header->version = read_2_bytes (abfd, info_ptr);
6807 info_ptr += 2;
43988095
JK
6808 if (cu_header->version < 5)
6809 switch (section_kind)
6810 {
6811 case rcuh_kind::COMPILE:
6812 cu_header->unit_type = DW_UT_compile;
6813 break;
6814 case rcuh_kind::TYPE:
6815 cu_header->unit_type = DW_UT_type;
6816 break;
6817 default:
6818 internal_error (__FILE__, __LINE__,
6819 _("read_comp_unit_head: invalid section_kind"));
6820 }
6821 else
6822 {
6823 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6824 (read_1_byte (abfd, info_ptr));
6825 info_ptr += 1;
6826 switch (cu_header->unit_type)
6827 {
6828 case DW_UT_compile:
6829 if (section_kind != rcuh_kind::COMPILE)
6830 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6831 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6832 filename);
6833 break;
6834 case DW_UT_type:
6835 section_kind = rcuh_kind::TYPE;
6836 break;
6837 default:
6838 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6839 "(is %d, should be %d or %d) [in module %s]"),
6840 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6841 }
6842
6843 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6844 info_ptr += 1;
6845 }
9c541725
PA
6846 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6847 cu_header,
6848 &bytes_read);
613e1657 6849 info_ptr += bytes_read;
43988095
JK
6850 if (cu_header->version < 5)
6851 {
6852 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6853 info_ptr += 1;
6854 }
107d2387
AC
6855 signed_addr = bfd_get_sign_extend_vma (abfd);
6856 if (signed_addr < 0)
8e65ff28 6857 internal_error (__FILE__, __LINE__,
e2e0b3e5 6858 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6859 cu_header->signed_addr_p = signed_addr;
c764a876 6860
43988095
JK
6861 if (section_kind == rcuh_kind::TYPE)
6862 {
6863 LONGEST type_offset;
6864
6865 cu_header->signature = read_8_bytes (abfd, info_ptr);
6866 info_ptr += 8;
6867
6868 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6869 info_ptr += bytes_read;
9c541725
PA
6870 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6871 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6872 error (_("Dwarf Error: Too big type_offset in compilation unit "
6873 "header (is %s) [in module %s]"), plongest (type_offset),
6874 filename);
6875 }
6876
107d2387
AC
6877 return info_ptr;
6878}
6879
36586728
TT
6880/* Helper function that returns the proper abbrev section for
6881 THIS_CU. */
6882
6883static struct dwarf2_section_info *
6884get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6885{
6886 struct dwarf2_section_info *abbrev;
ed2dc618 6887 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6888
6889 if (this_cu->is_dwz)
ed2dc618 6890 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6891 else
6892 abbrev = &dwarf2_per_objfile->abbrev;
6893
6894 return abbrev;
6895}
6896
9ff913ba
DE
6897/* Subroutine of read_and_check_comp_unit_head and
6898 read_and_check_type_unit_head to simplify them.
6899 Perform various error checking on the header. */
6900
6901static void
ed2dc618
SM
6902error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6903 struct comp_unit_head *header,
4bdcc0c1
DE
6904 struct dwarf2_section_info *section,
6905 struct dwarf2_section_info *abbrev_section)
9ff913ba 6906{
a32a8923 6907 const char *filename = get_section_file_name (section);
9ff913ba 6908
43988095 6909 if (header->version < 2 || header->version > 5)
9ff913ba 6910 error (_("Dwarf Error: wrong version in compilation unit header "
43988095 6911 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
9ff913ba
DE
6912 filename);
6913
9c541725 6914 if (to_underlying (header->abbrev_sect_off)
36586728 6915 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9d8780f0
SM
6916 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6917 "(offset %s + 6) [in module %s]"),
6918 sect_offset_str (header->abbrev_sect_off),
6919 sect_offset_str (header->sect_off),
9ff913ba
DE
6920 filename);
6921
9c541725 6922 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6923 avoid potential 32-bit overflow. */
9c541725 6924 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6925 > section->size)
9c541725 6926 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
9d8780f0
SM
6927 "(offset %s + 0) [in module %s]"),
6928 header->length, sect_offset_str (header->sect_off),
9ff913ba
DE
6929 filename);
6930}
6931
6932/* Read in a CU/TU header and perform some basic error checking.
6933 The contents of the header are stored in HEADER.
6934 The result is a pointer to the start of the first DIE. */
adabb602 6935
d521ce57 6936static const gdb_byte *
ed2dc618
SM
6937read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6938 struct comp_unit_head *header,
9ff913ba 6939 struct dwarf2_section_info *section,
4bdcc0c1 6940 struct dwarf2_section_info *abbrev_section,
d521ce57 6941 const gdb_byte *info_ptr,
43988095 6942 rcuh_kind section_kind)
72bf9492 6943{
d521ce57 6944 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6945
9c541725 6946 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6947
43988095 6948 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6949
9c541725 6950 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6951
ed2dc618
SM
6952 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6953 abbrev_section);
9ff913ba
DE
6954
6955 return info_ptr;
348e048f
DE
6956}
6957
f4dc4d17
DE
6958/* Fetch the abbreviation table offset from a comp or type unit header. */
6959
6960static sect_offset
ed2dc618
SM
6961read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6962 struct dwarf2_section_info *section,
9c541725 6963 sect_offset sect_off)
f4dc4d17 6964{
a32a8923 6965 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6966 const gdb_byte *info_ptr;
ac298888 6967 unsigned int initial_length_size, offset_size;
43988095 6968 uint16_t version;
f4dc4d17
DE
6969
6970 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6971 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6972 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6973 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6974 info_ptr += initial_length_size;
6975
6976 version = read_2_bytes (abfd, info_ptr);
6977 info_ptr += 2;
6978 if (version >= 5)
6979 {
6980 /* Skip unit type and address size. */
6981 info_ptr += 2;
6982 }
6983
9c541725 6984 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6985}
6986
aaa75496
JB
6987/* Allocate a new partial symtab for file named NAME and mark this new
6988 partial symtab as being an include of PST. */
6989
6990static void
d521ce57 6991dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
6992 struct objfile *objfile)
6993{
6994 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6995
fbd9ab74
JK
6996 if (!IS_ABSOLUTE_PATH (subpst->filename))
6997 {
6998 /* It shares objfile->objfile_obstack. */
6999 subpst->dirname = pst->dirname;
7000 }
7001
aaa75496
JB
7002 subpst->textlow = 0;
7003 subpst->texthigh = 0;
7004
8d749320
SM
7005 subpst->dependencies
7006 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
7007 subpst->dependencies[0] = pst;
7008 subpst->number_of_dependencies = 1;
7009
7010 subpst->globals_offset = 0;
7011 subpst->n_global_syms = 0;
7012 subpst->statics_offset = 0;
7013 subpst->n_static_syms = 0;
43f3e411 7014 subpst->compunit_symtab = NULL;
aaa75496
JB
7015 subpst->read_symtab = pst->read_symtab;
7016 subpst->readin = 0;
7017
7018 /* No private part is necessary for include psymtabs. This property
7019 can be used to differentiate between such include psymtabs and
10b3939b 7020 the regular ones. */
58a9656e 7021 subpst->read_symtab_private = NULL;
aaa75496
JB
7022}
7023
7024/* Read the Line Number Program data and extract the list of files
7025 included by the source file represented by PST. Build an include
d85a05f0 7026 partial symtab for each of these included files. */
aaa75496
JB
7027
7028static void
7029dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
7030 struct die_info *die,
7031 struct partial_symtab *pst)
aaa75496 7032{
fff8551c 7033 line_header_up lh;
d85a05f0 7034 struct attribute *attr;
aaa75496 7035
d85a05f0
DJ
7036 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7037 if (attr)
9c541725 7038 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
7039 if (lh == NULL)
7040 return; /* No linetable, so no includes. */
7041
c6da4cef 7042 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
fff8551c 7043 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
7044}
7045
348e048f 7046static hashval_t
52dc124a 7047hash_signatured_type (const void *item)
348e048f 7048{
9a3c8263
SM
7049 const struct signatured_type *sig_type
7050 = (const struct signatured_type *) item;
9a619af0 7051
348e048f 7052 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 7053 return sig_type->signature;
348e048f
DE
7054}
7055
7056static int
52dc124a 7057eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 7058{
9a3c8263
SM
7059 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
7060 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 7061
348e048f
DE
7062 return lhs->signature == rhs->signature;
7063}
7064
1fd400ff
TT
7065/* Allocate a hash table for signatured types. */
7066
7067static htab_t
673bfd45 7068allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
7069{
7070 return htab_create_alloc_ex (41,
52dc124a
DE
7071 hash_signatured_type,
7072 eq_signatured_type,
1fd400ff
TT
7073 NULL,
7074 &objfile->objfile_obstack,
7075 hashtab_obstack_allocate,
7076 dummy_obstack_deallocate);
7077}
7078
d467dd73 7079/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
7080
7081static int
d467dd73 7082add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 7083{
9a3c8263
SM
7084 struct signatured_type *sigt = (struct signatured_type *) *slot;
7085 struct signatured_type ***datap = (struct signatured_type ***) datum;
1fd400ff 7086
b4dd5633 7087 **datap = sigt;
1fd400ff
TT
7088 ++*datap;
7089
7090 return 1;
7091}
7092
78d4d2c5 7093/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
7094 and fill them into TYPES_HTAB. It will process only type units,
7095 therefore DW_UT_type. */
c88ee1f0 7096
78d4d2c5 7097static void
ed2dc618
SM
7098create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
7099 struct dwo_file *dwo_file,
43988095
JK
7100 dwarf2_section_info *section, htab_t &types_htab,
7101 rcuh_kind section_kind)
348e048f 7102{
3019eac3 7103 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 7104 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
7105 bfd *abfd;
7106 const gdb_byte *info_ptr, *end_ptr;
348e048f 7107
4bdcc0c1
DE
7108 abbrev_section = (dwo_file != NULL
7109 ? &dwo_file->sections.abbrev
7110 : &dwarf2_per_objfile->abbrev);
7111
b4f54984 7112 if (dwarf_read_debug)
43988095
JK
7113 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
7114 get_section_name (section),
a32a8923 7115 get_section_file_name (abbrev_section));
09406207 7116
78d4d2c5
JK
7117 dwarf2_read_section (objfile, section);
7118 info_ptr = section->buffer;
348e048f 7119
78d4d2c5
JK
7120 if (info_ptr == NULL)
7121 return;
348e048f 7122
78d4d2c5
JK
7123 /* We can't set abfd until now because the section may be empty or
7124 not present, in which case the bfd is unknown. */
7125 abfd = get_section_bfd_owner (section);
348e048f 7126
78d4d2c5
JK
7127 /* We don't use init_cutu_and_read_dies_simple, or some such, here
7128 because we don't need to read any dies: the signature is in the
7129 header. */
3019eac3 7130
78d4d2c5
JK
7131 end_ptr = info_ptr + section->size;
7132 while (info_ptr < end_ptr)
7133 {
78d4d2c5
JK
7134 struct signatured_type *sig_type;
7135 struct dwo_unit *dwo_tu;
7136 void **slot;
7137 const gdb_byte *ptr = info_ptr;
7138 struct comp_unit_head header;
7139 unsigned int length;
8b70b953 7140
9c541725 7141 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 7142
a49dd8dd
JK
7143 /* Initialize it due to a false compiler warning. */
7144 header.signature = -1;
9c541725 7145 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 7146
78d4d2c5
JK
7147 /* We need to read the type's signature in order to build the hash
7148 table, but we don't need anything else just yet. */
348e048f 7149
ed2dc618 7150 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 7151 abbrev_section, ptr, section_kind);
348e048f 7152
78d4d2c5 7153 length = get_cu_length (&header);
6caca83c 7154
78d4d2c5
JK
7155 /* Skip dummy type units. */
7156 if (ptr >= info_ptr + length
43988095
JK
7157 || peek_abbrev_code (abfd, ptr) == 0
7158 || header.unit_type != DW_UT_type)
78d4d2c5
JK
7159 {
7160 info_ptr += length;
7161 continue;
7162 }
dee91e82 7163
78d4d2c5
JK
7164 if (types_htab == NULL)
7165 {
7166 if (dwo_file)
7167 types_htab = allocate_dwo_unit_table (objfile);
7168 else
7169 types_htab = allocate_signatured_type_table (objfile);
7170 }
8b70b953 7171
78d4d2c5
JK
7172 if (dwo_file)
7173 {
7174 sig_type = NULL;
7175 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7176 struct dwo_unit);
7177 dwo_tu->dwo_file = dwo_file;
43988095 7178 dwo_tu->signature = header.signature;
9c541725 7179 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 7180 dwo_tu->section = section;
9c541725 7181 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
7182 dwo_tu->length = length;
7183 }
7184 else
7185 {
7186 /* N.B.: type_offset is not usable if this type uses a DWO file.
7187 The real type_offset is in the DWO file. */
7188 dwo_tu = NULL;
7189 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7190 struct signatured_type);
43988095 7191 sig_type->signature = header.signature;
9c541725 7192 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 7193 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
7194 sig_type->per_cu.is_debug_types = 1;
7195 sig_type->per_cu.section = section;
9c541725 7196 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
7197 sig_type->per_cu.length = length;
7198 }
7199
7200 slot = htab_find_slot (types_htab,
7201 dwo_file ? (void*) dwo_tu : (void *) sig_type,
7202 INSERT);
7203 gdb_assert (slot != NULL);
7204 if (*slot != NULL)
7205 {
9c541725 7206 sect_offset dup_sect_off;
0349ea22 7207
3019eac3
DE
7208 if (dwo_file)
7209 {
78d4d2c5
JK
7210 const struct dwo_unit *dup_tu
7211 = (const struct dwo_unit *) *slot;
7212
9c541725 7213 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
7214 }
7215 else
7216 {
78d4d2c5
JK
7217 const struct signatured_type *dup_tu
7218 = (const struct signatured_type *) *slot;
7219
9c541725 7220 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 7221 }
8b70b953 7222
78d4d2c5 7223 complaint (&symfile_complaints,
9d8780f0
SM
7224 _("debug type entry at offset %s is duplicate to"
7225 " the entry at offset %s, signature %s"),
7226 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 7227 hex_string (header.signature));
78d4d2c5
JK
7228 }
7229 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 7230
78d4d2c5 7231 if (dwarf_read_debug > 1)
9d8780f0
SM
7232 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
7233 sect_offset_str (sect_off),
43988095 7234 hex_string (header.signature));
3019eac3 7235
78d4d2c5
JK
7236 info_ptr += length;
7237 }
7238}
3019eac3 7239
78d4d2c5
JK
7240/* Create the hash table of all entries in the .debug_types
7241 (or .debug_types.dwo) section(s).
7242 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
7243 otherwise it is NULL.
b3c8eb43 7244
78d4d2c5 7245 The result is a pointer to the hash table or NULL if there are no types.
348e048f 7246
78d4d2c5 7247 Note: This function processes DWO files only, not DWP files. */
348e048f 7248
78d4d2c5 7249static void
ed2dc618
SM
7250create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
7251 struct dwo_file *dwo_file,
78d4d2c5
JK
7252 VEC (dwarf2_section_info_def) *types,
7253 htab_t &types_htab)
7254{
7255 int ix;
7256 struct dwarf2_section_info *section;
7257
7258 if (VEC_empty (dwarf2_section_info_def, types))
7259 return;
348e048f 7260
78d4d2c5
JK
7261 for (ix = 0;
7262 VEC_iterate (dwarf2_section_info_def, types, ix, section);
7263 ++ix)
ed2dc618
SM
7264 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
7265 types_htab, rcuh_kind::TYPE);
3019eac3
DE
7266}
7267
7268/* Create the hash table of all entries in the .debug_types section,
7269 and initialize all_type_units.
7270 The result is zero if there is an error (e.g. missing .debug_types section),
7271 otherwise non-zero. */
7272
7273static int
ed2dc618 7274create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 7275{
78d4d2c5 7276 htab_t types_htab = NULL;
b4dd5633 7277 struct signatured_type **iter;
3019eac3 7278
ed2dc618
SM
7279 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
7280 &dwarf2_per_objfile->info, types_htab,
43988095 7281 rcuh_kind::COMPILE);
ed2dc618
SM
7282 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
7283 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
7284 if (types_htab == NULL)
7285 {
7286 dwarf2_per_objfile->signatured_types = NULL;
7287 return 0;
7288 }
7289
348e048f
DE
7290 dwarf2_per_objfile->signatured_types = types_htab;
7291
6aa5f3a6
DE
7292 dwarf2_per_objfile->n_type_units
7293 = dwarf2_per_objfile->n_allocated_type_units
7294 = htab_elements (types_htab);
8d749320
SM
7295 dwarf2_per_objfile->all_type_units =
7296 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
d467dd73
DE
7297 iter = &dwarf2_per_objfile->all_type_units[0];
7298 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
7299 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
7300 == dwarf2_per_objfile->n_type_units);
1fd400ff 7301
348e048f
DE
7302 return 1;
7303}
7304
6aa5f3a6
DE
7305/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
7306 If SLOT is non-NULL, it is the entry to use in the hash table.
7307 Otherwise we find one. */
7308
7309static struct signatured_type *
ed2dc618
SM
7310add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
7311 void **slot)
6aa5f3a6
DE
7312{
7313 struct objfile *objfile = dwarf2_per_objfile->objfile;
7314 int n_type_units = dwarf2_per_objfile->n_type_units;
7315 struct signatured_type *sig_type;
7316
7317 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
7318 ++n_type_units;
7319 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
7320 {
7321 if (dwarf2_per_objfile->n_allocated_type_units == 0)
7322 dwarf2_per_objfile->n_allocated_type_units = 1;
7323 dwarf2_per_objfile->n_allocated_type_units *= 2;
7324 dwarf2_per_objfile->all_type_units
224c3ddb
SM
7325 = XRESIZEVEC (struct signatured_type *,
7326 dwarf2_per_objfile->all_type_units,
7327 dwarf2_per_objfile->n_allocated_type_units);
6aa5f3a6
DE
7328 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
7329 }
7330 dwarf2_per_objfile->n_type_units = n_type_units;
7331
7332 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7333 struct signatured_type);
7334 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
7335 sig_type->signature = sig;
7336 sig_type->per_cu.is_debug_types = 1;
7337 if (dwarf2_per_objfile->using_index)
7338 {
7339 sig_type->per_cu.v.quick =
7340 OBSTACK_ZALLOC (&objfile->objfile_obstack,
7341 struct dwarf2_per_cu_quick_data);
7342 }
7343
7344 if (slot == NULL)
7345 {
7346 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7347 sig_type, INSERT);
7348 }
7349 gdb_assert (*slot == NULL);
7350 *slot = sig_type;
7351 /* The rest of sig_type must be filled in by the caller. */
7352 return sig_type;
7353}
7354
a2ce51a0
DE
7355/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7356 Fill in SIG_ENTRY with DWO_ENTRY. */
7357
7358static void
ed2dc618 7359fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
7360 struct signatured_type *sig_entry,
7361 struct dwo_unit *dwo_entry)
7362{
7ee85ab1 7363 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
7364 gdb_assert (! sig_entry->per_cu.queued);
7365 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
7366 if (dwarf2_per_objfile->using_index)
7367 {
7368 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 7369 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
7370 }
7371 else
7372 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 7373 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 7374 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 7375 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
7376 gdb_assert (sig_entry->dwo_unit == NULL);
7377
7378 sig_entry->per_cu.section = dwo_entry->section;
9c541725 7379 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
7380 sig_entry->per_cu.length = dwo_entry->length;
7381 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 7382 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
7383 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7384 sig_entry->dwo_unit = dwo_entry;
7385}
7386
7387/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
7388 If we haven't read the TU yet, create the signatured_type data structure
7389 for a TU to be read in directly from a DWO file, bypassing the stub.
7390 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7391 using .gdb_index, then when reading a CU we want to stay in the DWO file
7392 containing that CU. Otherwise we could end up reading several other DWO
7393 files (due to comdat folding) to process the transitive closure of all the
7394 mentioned TUs, and that can be slow. The current DWO file will have every
7395 type signature that it needs.
a2ce51a0
DE
7396 We only do this for .gdb_index because in the psymtab case we already have
7397 to read all the DWOs to build the type unit groups. */
7398
7399static struct signatured_type *
7400lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7401{
518817b3
SM
7402 struct dwarf2_per_objfile *dwarf2_per_objfile
7403 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
7404 struct objfile *objfile = dwarf2_per_objfile->objfile;
7405 struct dwo_file *dwo_file;
7406 struct dwo_unit find_dwo_entry, *dwo_entry;
7407 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7408 void **slot;
a2ce51a0
DE
7409
7410 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7411
6aa5f3a6
DE
7412 /* If TU skeletons have been removed then we may not have read in any
7413 TUs yet. */
7414 if (dwarf2_per_objfile->signatured_types == NULL)
7415 {
7416 dwarf2_per_objfile->signatured_types
7417 = allocate_signatured_type_table (objfile);
7418 }
a2ce51a0
DE
7419
7420 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
7421 Use the global signatured_types array to do our own comdat-folding
7422 of types. If this is the first time we're reading this TU, and
7423 the TU has an entry in .gdb_index, replace the recorded data from
7424 .gdb_index with this TU. */
a2ce51a0 7425
a2ce51a0 7426 find_sig_entry.signature = sig;
6aa5f3a6
DE
7427 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7428 &find_sig_entry, INSERT);
9a3c8263 7429 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
7430
7431 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
7432 read. Don't reassign the global entry to point to this DWO if that's
7433 the case. Also note that if the TU is already being read, it may not
7434 have come from a DWO, the program may be a mix of Fission-compiled
7435 code and non-Fission-compiled code. */
7436
7437 /* Have we already tried to read this TU?
7438 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7439 needn't exist in the global table yet). */
7440 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
7441 return sig_entry;
7442
6aa5f3a6
DE
7443 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7444 dwo_unit of the TU itself. */
7445 dwo_file = cu->dwo_unit->dwo_file;
7446
a2ce51a0
DE
7447 /* Ok, this is the first time we're reading this TU. */
7448 if (dwo_file->tus == NULL)
7449 return NULL;
7450 find_dwo_entry.signature = sig;
9a3c8263 7451 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
7452 if (dwo_entry == NULL)
7453 return NULL;
7454
6aa5f3a6
DE
7455 /* If the global table doesn't have an entry for this TU, add one. */
7456 if (sig_entry == NULL)
ed2dc618 7457 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 7458
ed2dc618 7459 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 7460 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
7461 return sig_entry;
7462}
7463
a2ce51a0
DE
7464/* Subroutine of lookup_signatured_type.
7465 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
7466 then try the DWP file. If the TU stub (skeleton) has been removed then
7467 it won't be in .gdb_index. */
a2ce51a0
DE
7468
7469static struct signatured_type *
7470lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7471{
518817b3
SM
7472 struct dwarf2_per_objfile *dwarf2_per_objfile
7473 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 7474 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 7475 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
7476 struct dwo_unit *dwo_entry;
7477 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7478 void **slot;
a2ce51a0
DE
7479
7480 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7481 gdb_assert (dwp_file != NULL);
7482
6aa5f3a6
DE
7483 /* If TU skeletons have been removed then we may not have read in any
7484 TUs yet. */
7485 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 7486 {
6aa5f3a6
DE
7487 dwarf2_per_objfile->signatured_types
7488 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
7489 }
7490
6aa5f3a6
DE
7491 find_sig_entry.signature = sig;
7492 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7493 &find_sig_entry, INSERT);
9a3c8263 7494 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
7495
7496 /* Have we already tried to read this TU?
7497 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7498 needn't exist in the global table yet). */
7499 if (sig_entry != NULL)
7500 return sig_entry;
7501
a2ce51a0
DE
7502 if (dwp_file->tus == NULL)
7503 return NULL;
ed2dc618 7504 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 7505 sig, 1 /* is_debug_types */);
a2ce51a0
DE
7506 if (dwo_entry == NULL)
7507 return NULL;
7508
ed2dc618
SM
7509 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7510 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 7511
a2ce51a0
DE
7512 return sig_entry;
7513}
7514
380bca97 7515/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7516 Returns NULL if signature SIG is not present in the table.
7517 It is up to the caller to complain about this. */
348e048f
DE
7518
7519static struct signatured_type *
a2ce51a0 7520lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7521{
518817b3
SM
7522 struct dwarf2_per_objfile *dwarf2_per_objfile
7523 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 7524
a2ce51a0
DE
7525 if (cu->dwo_unit
7526 && dwarf2_per_objfile->using_index)
7527 {
7528 /* We're in a DWO/DWP file, and we're using .gdb_index.
7529 These cases require special processing. */
ed2dc618 7530 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
7531 return lookup_dwo_signatured_type (cu, sig);
7532 else
7533 return lookup_dwp_signatured_type (cu, sig);
7534 }
7535 else
7536 {
7537 struct signatured_type find_entry, *entry;
348e048f 7538
a2ce51a0
DE
7539 if (dwarf2_per_objfile->signatured_types == NULL)
7540 return NULL;
7541 find_entry.signature = sig;
9a3c8263
SM
7542 entry = ((struct signatured_type *)
7543 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7544 return entry;
7545 }
348e048f 7546}
42e7ad6c
DE
7547\f
7548/* Low level DIE reading support. */
348e048f 7549
d85a05f0
DJ
7550/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7551
7552static void
7553init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7554 struct dwarf2_cu *cu,
3019eac3 7555 struct dwarf2_section_info *section,
685af9cd
TT
7556 struct dwo_file *dwo_file,
7557 struct abbrev_table *abbrev_table)
d85a05f0 7558{
fceca515 7559 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7560 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7561 reader->cu = cu;
3019eac3 7562 reader->dwo_file = dwo_file;
dee91e82
DE
7563 reader->die_section = section;
7564 reader->buffer = section->buffer;
f664829e 7565 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7566 reader->comp_dir = NULL;
685af9cd 7567 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
7568}
7569
b0c7bfa9
DE
7570/* Subroutine of init_cutu_and_read_dies to simplify it.
7571 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7572 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7573 already.
7574
7575 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7576 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7577 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7578 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7579 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7580 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7581 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7582 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
7583 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7584 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7585 kept around for at least as long as *RESULT_READER.
7586
b0c7bfa9
DE
7587 The result is non-zero if a valid (non-dummy) DIE was found. */
7588
7589static int
7590read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7591 struct dwo_unit *dwo_unit,
b0c7bfa9 7592 struct die_info *stub_comp_unit_die,
a2ce51a0 7593 const char *stub_comp_dir,
b0c7bfa9 7594 struct die_reader_specs *result_reader,
d521ce57 7595 const gdb_byte **result_info_ptr,
b0c7bfa9 7596 struct die_info **result_comp_unit_die,
685af9cd
TT
7597 int *result_has_children,
7598 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 7599{
ed2dc618 7600 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
7601 struct objfile *objfile = dwarf2_per_objfile->objfile;
7602 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7603 bfd *abfd;
d521ce57 7604 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7605 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7606 int i,num_extra_attrs;
7607 struct dwarf2_section_info *dwo_abbrev_section;
7608 struct attribute *attr;
7609 struct die_info *comp_unit_die;
7610
b0aeadb3
DE
7611 /* At most one of these may be provided. */
7612 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7613
b0c7bfa9
DE
7614 /* These attributes aren't processed until later:
7615 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7616 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7617 referenced later. However, these attributes are found in the stub
7618 which we won't have later. In order to not impose this complication
7619 on the rest of the code, we read them here and copy them to the
7620 DWO CU/TU die. */
b0c7bfa9
DE
7621
7622 stmt_list = NULL;
7623 low_pc = NULL;
7624 high_pc = NULL;
7625 ranges = NULL;
7626 comp_dir = NULL;
7627
7628 if (stub_comp_unit_die != NULL)
7629 {
7630 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7631 DWO file. */
7632 if (! this_cu->is_debug_types)
7633 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7634 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7635 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7636 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7637 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7638
7639 /* There should be a DW_AT_addr_base attribute here (if needed).
7640 We need the value before we can process DW_FORM_GNU_addr_index. */
7641 cu->addr_base = 0;
7642 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7643 if (attr)
7644 cu->addr_base = DW_UNSND (attr);
7645
7646 /* There should be a DW_AT_ranges_base attribute here (if needed).
7647 We need the value before we can process DW_AT_ranges. */
7648 cu->ranges_base = 0;
7649 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7650 if (attr)
7651 cu->ranges_base = DW_UNSND (attr);
7652 }
a2ce51a0
DE
7653 else if (stub_comp_dir != NULL)
7654 {
7655 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7656 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7657 comp_dir->name = DW_AT_comp_dir;
7658 comp_dir->form = DW_FORM_string;
7659 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7660 DW_STRING (comp_dir) = stub_comp_dir;
7661 }
b0c7bfa9
DE
7662
7663 /* Set up for reading the DWO CU/TU. */
7664 cu->dwo_unit = dwo_unit;
685af9cd 7665 dwarf2_section_info *section = dwo_unit->section;
b0c7bfa9 7666 dwarf2_read_section (objfile, section);
a32a8923 7667 abfd = get_section_bfd_owner (section);
9c541725
PA
7668 begin_info_ptr = info_ptr = (section->buffer
7669 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7670 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
7671
7672 if (this_cu->is_debug_types)
7673 {
b0c7bfa9
DE
7674 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7675
ed2dc618
SM
7676 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7677 &cu->header, section,
b0c7bfa9 7678 dwo_abbrev_section,
43988095 7679 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7680 /* This is not an assert because it can be caused by bad debug info. */
43988095 7681 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7682 {
7683 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7684 " TU at offset %s [in module %s]"),
a2ce51a0 7685 hex_string (sig_type->signature),
43988095 7686 hex_string (cu->header.signature),
9d8780f0 7687 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7688 bfd_get_filename (abfd));
7689 }
9c541725 7690 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7691 /* For DWOs coming from DWP files, we don't know the CU length
7692 nor the type's offset in the TU until now. */
7693 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7694 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7695
7696 /* Establish the type offset that can be used to lookup the type.
7697 For DWO files, we don't know it until now. */
9c541725
PA
7698 sig_type->type_offset_in_section
7699 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7700 }
7701 else
7702 {
ed2dc618
SM
7703 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7704 &cu->header, section,
b0c7bfa9 7705 dwo_abbrev_section,
43988095 7706 info_ptr, rcuh_kind::COMPILE);
9c541725 7707 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7708 /* For DWOs coming from DWP files, we don't know the CU length
7709 until now. */
7710 dwo_unit->length = get_cu_length (&cu->header);
7711 }
7712
685af9cd
TT
7713 *result_dwo_abbrev_table
7714 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7715 cu->header.abbrev_sect_off);
7716 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7717 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7718
7719 /* Read in the die, but leave space to copy over the attributes
7720 from the stub. This has the benefit of simplifying the rest of
7721 the code - all the work to maintain the illusion of a single
7722 DW_TAG_{compile,type}_unit DIE is done here. */
7723 num_extra_attrs = ((stmt_list != NULL)
7724 + (low_pc != NULL)
7725 + (high_pc != NULL)
7726 + (ranges != NULL)
7727 + (comp_dir != NULL));
7728 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7729 result_has_children, num_extra_attrs);
7730
7731 /* Copy over the attributes from the stub to the DIE we just read in. */
7732 comp_unit_die = *result_comp_unit_die;
7733 i = comp_unit_die->num_attrs;
7734 if (stmt_list != NULL)
7735 comp_unit_die->attrs[i++] = *stmt_list;
7736 if (low_pc != NULL)
7737 comp_unit_die->attrs[i++] = *low_pc;
7738 if (high_pc != NULL)
7739 comp_unit_die->attrs[i++] = *high_pc;
7740 if (ranges != NULL)
7741 comp_unit_die->attrs[i++] = *ranges;
7742 if (comp_dir != NULL)
7743 comp_unit_die->attrs[i++] = *comp_dir;
7744 comp_unit_die->num_attrs += num_extra_attrs;
7745
b4f54984 7746 if (dwarf_die_debug)
bf6af496
DE
7747 {
7748 fprintf_unfiltered (gdb_stdlog,
7749 "Read die from %s@0x%x of %s:\n",
a32a8923 7750 get_section_name (section),
bf6af496
DE
7751 (unsigned) (begin_info_ptr - section->buffer),
7752 bfd_get_filename (abfd));
b4f54984 7753 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7754 }
7755
a2ce51a0
DE
7756 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7757 TUs by skipping the stub and going directly to the entry in the DWO file.
7758 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7759 to get it via circuitous means. Blech. */
7760 if (comp_dir != NULL)
7761 result_reader->comp_dir = DW_STRING (comp_dir);
7762
b0c7bfa9
DE
7763 /* Skip dummy compilation units. */
7764 if (info_ptr >= begin_info_ptr + dwo_unit->length
7765 || peek_abbrev_code (abfd, info_ptr) == 0)
7766 return 0;
7767
7768 *result_info_ptr = info_ptr;
7769 return 1;
7770}
7771
7772/* Subroutine of init_cutu_and_read_dies to simplify it.
7773 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7774 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7775
7776static struct dwo_unit *
7777lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7778 struct die_info *comp_unit_die)
7779{
7780 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9
DE
7781 ULONGEST signature;
7782 struct dwo_unit *dwo_unit;
7783 const char *comp_dir, *dwo_name;
7784
a2ce51a0
DE
7785 gdb_assert (cu != NULL);
7786
b0c7bfa9 7787 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
7788 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7789 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7790
7791 if (this_cu->is_debug_types)
7792 {
7793 struct signatured_type *sig_type;
7794
7795 /* Since this_cu is the first member of struct signatured_type,
7796 we can go from a pointer to one to a pointer to the other. */
7797 sig_type = (struct signatured_type *) this_cu;
7798 signature = sig_type->signature;
7799 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7800 }
7801 else
7802 {
7803 struct attribute *attr;
7804
7805 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7806 if (! attr)
7807 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7808 " [in module %s]"),
e3b94546 7809 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9
DE
7810 signature = DW_UNSND (attr);
7811 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7812 signature);
7813 }
7814
b0c7bfa9
DE
7815 return dwo_unit;
7816}
7817
a2ce51a0 7818/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6 7819 See it for a description of the parameters.
fcd3b13d 7820 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0
DE
7821
7822static void
6aa5f3a6
DE
7823init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7824 int use_existing_cu, int keep,
a2ce51a0
DE
7825 die_reader_func_ftype *die_reader_func,
7826 void *data)
7827{
fcd3b13d 7828 std::unique_ptr<dwarf2_cu> new_cu;
a2ce51a0 7829 struct signatured_type *sig_type;
a2ce51a0
DE
7830 struct die_reader_specs reader;
7831 const gdb_byte *info_ptr;
7832 struct die_info *comp_unit_die;
7833 int has_children;
ed2dc618 7834 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
a2ce51a0
DE
7835
7836 /* Verify we can do the following downcast, and that we have the
7837 data we need. */
7838 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7839 sig_type = (struct signatured_type *) this_cu;
7840 gdb_assert (sig_type->dwo_unit != NULL);
7841
6aa5f3a6
DE
7842 if (use_existing_cu && this_cu->cu != NULL)
7843 {
7844 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6
DE
7845 /* There's no need to do the rereading_dwo_cu handling that
7846 init_cutu_and_read_dies does since we don't read the stub. */
7847 }
7848 else
7849 {
7850 /* If !use_existing_cu, this_cu->cu must be NULL. */
7851 gdb_assert (this_cu->cu == NULL);
fcd3b13d 7852 new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7853 }
7854
7855 /* A future optimization, if needed, would be to use an existing
7856 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7857 could share abbrev tables. */
a2ce51a0 7858
685af9cd
TT
7859 /* The abbreviation table used by READER, this must live at least as long as
7860 READER. */
7861 abbrev_table_up dwo_abbrev_table;
7862
a2ce51a0 7863 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7864 NULL /* stub_comp_unit_die */,
7865 sig_type->dwo_unit->dwo_file->comp_dir,
7866 &reader, &info_ptr,
685af9cd
TT
7867 &comp_unit_die, &has_children,
7868 &dwo_abbrev_table) == 0)
a2ce51a0
DE
7869 {
7870 /* Dummy die. */
a2ce51a0
DE
7871 return;
7872 }
7873
7874 /* All the "real" work is done here. */
7875 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7876
6aa5f3a6 7877 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
7878 but the alternative is making the latter more complex.
7879 This function is only for the special case of using DWO files directly:
7880 no point in overly complicating the general case just to handle this. */
fcd3b13d 7881 if (new_cu != NULL && keep)
a2ce51a0 7882 {
fcd3b13d
SM
7883 /* Link this CU into read_in_chain. */
7884 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7885 dwarf2_per_objfile->read_in_chain = this_cu;
7886 /* The chain owns it now. */
7887 new_cu.release ();
a2ce51a0 7888 }
a2ce51a0
DE
7889}
7890
fd820528 7891/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7892 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7893
f4dc4d17
DE
7894 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7895 Otherwise the table specified in the comp unit header is read in and used.
7896 This is an optimization for when we already have the abbrev table.
7897
dee91e82
DE
7898 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7899 Otherwise, a new CU is allocated with xmalloc.
7900
7901 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7902 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7903
7904 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 7905 linker) then DIE_READER_FUNC will not get called. */
aaa75496 7906
70221824 7907static void
fd820528 7908init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 7909 struct abbrev_table *abbrev_table,
fd820528
DE
7910 int use_existing_cu, int keep,
7911 die_reader_func_ftype *die_reader_func,
7912 void *data)
c906108c 7913{
ed2dc618 7914 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7915 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7916 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7917 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7918 struct dwarf2_cu *cu;
d521ce57 7919 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7920 struct die_reader_specs reader;
d85a05f0 7921 struct die_info *comp_unit_die;
dee91e82 7922 int has_children;
d85a05f0 7923 struct attribute *attr;
dee91e82 7924 struct signatured_type *sig_type = NULL;
4bdcc0c1 7925 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7926 /* Non-zero if CU currently points to a DWO file and we need to
7927 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7928 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7929 int rereading_dwo_cu = 0;
c906108c 7930
b4f54984 7931 if (dwarf_die_debug)
9d8780f0 7932 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7933 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7934 sect_offset_str (this_cu->sect_off));
09406207 7935
dee91e82
DE
7936 if (use_existing_cu)
7937 gdb_assert (keep);
23745b47 7938
a2ce51a0
DE
7939 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7940 file (instead of going through the stub), short-circuit all of this. */
7941 if (this_cu->reading_dwo_directly)
7942 {
7943 /* Narrow down the scope of possibilities to have to understand. */
7944 gdb_assert (this_cu->is_debug_types);
7945 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
7946 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7947 die_reader_func, data);
a2ce51a0
DE
7948 return;
7949 }
7950
dee91e82
DE
7951 /* This is cheap if the section is already read in. */
7952 dwarf2_read_section (objfile, section);
7953
9c541725 7954 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7955
7956 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7957
fcd3b13d 7958 std::unique_ptr<dwarf2_cu> new_cu;
dee91e82
DE
7959 if (use_existing_cu && this_cu->cu != NULL)
7960 {
7961 cu = this_cu->cu;
42e7ad6c
DE
7962 /* If this CU is from a DWO file we need to start over, we need to
7963 refetch the attributes from the skeleton CU.
7964 This could be optimized by retrieving those attributes from when we
7965 were here the first time: the previous comp_unit_die was stored in
7966 comp_unit_obstack. But there's no data yet that we need this
7967 optimization. */
7968 if (cu->dwo_unit != NULL)
7969 rereading_dwo_cu = 1;
dee91e82
DE
7970 }
7971 else
7972 {
7973 /* If !use_existing_cu, this_cu->cu must be NULL. */
7974 gdb_assert (this_cu->cu == NULL);
fcd3b13d
SM
7975 new_cu.reset (new dwarf2_cu (this_cu));
7976 cu = new_cu.get ();
42e7ad6c 7977 }
dee91e82 7978
b0c7bfa9 7979 /* Get the header. */
9c541725 7980 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7981 {
7982 /* We already have the header, there's no need to read it in again. */
9c541725 7983 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7984 }
7985 else
7986 {
3019eac3 7987 if (this_cu->is_debug_types)
dee91e82 7988 {
ed2dc618
SM
7989 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7990 &cu->header, section,
4bdcc0c1 7991 abbrev_section, info_ptr,
43988095 7992 rcuh_kind::TYPE);
dee91e82 7993
42e7ad6c
DE
7994 /* Since per_cu is the first member of struct signatured_type,
7995 we can go from a pointer to one to a pointer to the other. */
7996 sig_type = (struct signatured_type *) this_cu;
43988095 7997 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7998 gdb_assert (sig_type->type_offset_in_tu
7999 == cu->header.type_cu_offset_in_tu);
8000 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 8001
42e7ad6c
DE
8002 /* LENGTH has not been set yet for type units if we're
8003 using .gdb_index. */
1ce1cefd 8004 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
8005
8006 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
8007 sig_type->type_offset_in_section =
8008 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
8009
8010 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
8011 }
8012 else
8013 {
ed2dc618
SM
8014 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
8015 &cu->header, section,
4bdcc0c1 8016 abbrev_section,
43988095
JK
8017 info_ptr,
8018 rcuh_kind::COMPILE);
dee91e82 8019
9c541725 8020 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 8021 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 8022 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
8023 }
8024 }
10b3939b 8025
6caca83c 8026 /* Skip dummy compilation units. */
dee91e82 8027 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 8028 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 8029 return;
6caca83c 8030
433df2d4
DE
8031 /* If we don't have them yet, read the abbrevs for this compilation unit.
8032 And if we need to read them now, make sure they're freed when we're
685af9cd
TT
8033 done (own the table through ABBREV_TABLE_HOLDER). */
8034 abbrev_table_up abbrev_table_holder;
f4dc4d17 8035 if (abbrev_table != NULL)
685af9cd
TT
8036 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
8037 else
f4dc4d17 8038 {
685af9cd
TT
8039 abbrev_table_holder
8040 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
8041 cu->header.abbrev_sect_off);
8042 abbrev_table = abbrev_table_holder.get ();
42e7ad6c 8043 }
af703f96 8044
dee91e82 8045 /* Read the top level CU/TU die. */
685af9cd 8046 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
dee91e82 8047 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 8048
b0c7bfa9 8049 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
8050 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
8051 table from the DWO file and pass the ownership over to us. It will be
8052 referenced from READER, so we must make sure to free it after we're done
8053 with READER.
8054
b0c7bfa9
DE
8055 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
8056 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3 8057 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
685af9cd 8058 abbrev_table_up dwo_abbrev_table;
3019eac3
DE
8059 if (attr)
8060 {
3019eac3 8061 struct dwo_unit *dwo_unit;
b0c7bfa9 8062 struct die_info *dwo_comp_unit_die;
3019eac3
DE
8063
8064 if (has_children)
6a506a2d
DE
8065 {
8066 complaint (&symfile_complaints,
8067 _("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
8068 " has children (offset %s) [in module %s]"),
8069 sect_offset_str (this_cu->sect_off),
8070 bfd_get_filename (abfd));
6a506a2d 8071 }
b0c7bfa9 8072 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 8073 if (dwo_unit != NULL)
3019eac3 8074 {
6a506a2d 8075 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 8076 comp_unit_die, NULL,
6a506a2d 8077 &reader, &info_ptr,
685af9cd
TT
8078 &dwo_comp_unit_die, &has_children,
8079 &dwo_abbrev_table) == 0)
6a506a2d
DE
8080 {
8081 /* Dummy die. */
6a506a2d
DE
8082 return;
8083 }
8084 comp_unit_die = dwo_comp_unit_die;
8085 }
8086 else
8087 {
8088 /* Yikes, we couldn't find the rest of the DIE, we only have
8089 the stub. A complaint has already been logged. There's
8090 not much more we can do except pass on the stub DIE to
8091 die_reader_func. We don't want to throw an error on bad
8092 debug info. */
3019eac3
DE
8093 }
8094 }
8095
b0c7bfa9 8096 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
8097 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8098
b0c7bfa9 8099 /* Done, clean up. */
fcd3b13d 8100 if (new_cu != NULL && keep)
348e048f 8101 {
fcd3b13d
SM
8102 /* Link this CU into read_in_chain. */
8103 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
8104 dwarf2_per_objfile->read_in_chain = this_cu;
8105 /* The chain owns it now. */
8106 new_cu.release ();
348e048f 8107 }
dee91e82
DE
8108}
8109
33e80786
DE
8110/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
8111 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
8112 to have already done the lookup to find the DWO file).
dee91e82
DE
8113
8114 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 8115 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
8116
8117 We fill in THIS_CU->length.
8118
8119 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
8120 linker) then DIE_READER_FUNC will not get called.
8121
8122 THIS_CU->cu is always freed when done.
3019eac3
DE
8123 This is done in order to not leave THIS_CU->cu in a state where we have
8124 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
8125
8126static void
8127init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 8128 struct dwo_file *dwo_file,
dee91e82
DE
8129 die_reader_func_ftype *die_reader_func,
8130 void *data)
8131{
ed2dc618 8132 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 8133 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 8134 struct dwarf2_section_info *section = this_cu->section;
a32a8923 8135 bfd *abfd = get_section_bfd_owner (section);
33e80786 8136 struct dwarf2_section_info *abbrev_section;
d521ce57 8137 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 8138 struct die_reader_specs reader;
dee91e82
DE
8139 struct die_info *comp_unit_die;
8140 int has_children;
8141
b4f54984 8142 if (dwarf_die_debug)
9d8780f0 8143 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 8144 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 8145 sect_offset_str (this_cu->sect_off));
09406207 8146
dee91e82
DE
8147 gdb_assert (this_cu->cu == NULL);
8148
33e80786
DE
8149 abbrev_section = (dwo_file != NULL
8150 ? &dwo_file->sections.abbrev
8151 : get_abbrev_section_for_cu (this_cu));
8152
dee91e82
DE
8153 /* This is cheap if the section is already read in. */
8154 dwarf2_read_section (objfile, section);
8155
fcd3b13d 8156 struct dwarf2_cu cu (this_cu);
dee91e82 8157
9c541725 8158 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618
SM
8159 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
8160 &cu.header, section,
4bdcc0c1 8161 abbrev_section, info_ptr,
43988095
JK
8162 (this_cu->is_debug_types
8163 ? rcuh_kind::TYPE
8164 : rcuh_kind::COMPILE));
dee91e82 8165
1ce1cefd 8166 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
8167
8168 /* Skip dummy compilation units. */
8169 if (info_ptr >= begin_info_ptr + this_cu->length
8170 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 8171 return;
72bf9492 8172
685af9cd
TT
8173 abbrev_table_up abbrev_table
8174 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
8175 cu.header.abbrev_sect_off);
dee91e82 8176
685af9cd 8177 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
dee91e82
DE
8178 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
8179
8180 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
dee91e82
DE
8181}
8182
3019eac3
DE
8183/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
8184 does not lookup the specified DWO file.
8185 This cannot be used to read DWO files.
dee91e82
DE
8186
8187 THIS_CU->cu is always freed when done.
3019eac3
DE
8188 This is done in order to not leave THIS_CU->cu in a state where we have
8189 to care whether it refers to the "main" CU or the DWO CU.
8190 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
8191
8192static void
8193init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
8194 die_reader_func_ftype *die_reader_func,
8195 void *data)
8196{
33e80786 8197 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 8198}
0018ea6f
DE
8199\f
8200/* Type Unit Groups.
dee91e82 8201
0018ea6f
DE
8202 Type Unit Groups are a way to collapse the set of all TUs (type units) into
8203 a more manageable set. The grouping is done by DW_AT_stmt_list entry
8204 so that all types coming from the same compilation (.o file) are grouped
8205 together. A future step could be to put the types in the same symtab as
8206 the CU the types ultimately came from. */
ff013f42 8207
f4dc4d17
DE
8208static hashval_t
8209hash_type_unit_group (const void *item)
8210{
9a3c8263
SM
8211 const struct type_unit_group *tu_group
8212 = (const struct type_unit_group *) item;
f4dc4d17 8213
094b34ac 8214 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 8215}
348e048f
DE
8216
8217static int
f4dc4d17 8218eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 8219{
9a3c8263
SM
8220 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
8221 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 8222
094b34ac 8223 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 8224}
348e048f 8225
f4dc4d17
DE
8226/* Allocate a hash table for type unit groups. */
8227
8228static htab_t
ed2dc618 8229allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
8230{
8231 return htab_create_alloc_ex (3,
8232 hash_type_unit_group,
8233 eq_type_unit_group,
8234 NULL,
ed2dc618 8235 &objfile->objfile_obstack,
f4dc4d17
DE
8236 hashtab_obstack_allocate,
8237 dummy_obstack_deallocate);
8238}
dee91e82 8239
f4dc4d17
DE
8240/* Type units that don't have DW_AT_stmt_list are grouped into their own
8241 partial symtabs. We combine several TUs per psymtab to not let the size
8242 of any one psymtab grow too big. */
8243#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
8244#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 8245
094b34ac 8246/* Helper routine for get_type_unit_group.
f4dc4d17
DE
8247 Create the type_unit_group object used to hold one or more TUs. */
8248
8249static struct type_unit_group *
094b34ac 8250create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 8251{
518817b3
SM
8252 struct dwarf2_per_objfile *dwarf2_per_objfile
8253 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 8254 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 8255 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 8256 struct type_unit_group *tu_group;
f4dc4d17
DE
8257
8258 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8259 struct type_unit_group);
094b34ac 8260 per_cu = &tu_group->per_cu;
518817b3 8261 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 8262
094b34ac
DE
8263 if (dwarf2_per_objfile->using_index)
8264 {
8265 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8266 struct dwarf2_per_cu_quick_data);
094b34ac
DE
8267 }
8268 else
8269 {
9c541725 8270 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac
DE
8271 struct partial_symtab *pst;
8272 char *name;
8273
8274 /* Give the symtab a useful name for debug purposes. */
8275 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
8276 name = xstrprintf ("<type_units_%d>",
8277 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
8278 else
8279 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
8280
8281 pst = create_partial_symtab (per_cu, name);
8282 pst->anonymous = 1;
f4dc4d17 8283
094b34ac
DE
8284 xfree (name);
8285 }
f4dc4d17 8286
094b34ac 8287 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 8288 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
8289
8290 return tu_group;
8291}
8292
094b34ac
DE
8293/* Look up the type_unit_group for type unit CU, and create it if necessary.
8294 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
8295
8296static struct type_unit_group *
ff39bb5e 8297get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 8298{
518817b3
SM
8299 struct dwarf2_per_objfile *dwarf2_per_objfile
8300 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8301 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8302 struct type_unit_group *tu_group;
8303 void **slot;
8304 unsigned int line_offset;
8305 struct type_unit_group type_unit_group_for_lookup;
8306
8307 if (dwarf2_per_objfile->type_unit_groups == NULL)
8308 {
8309 dwarf2_per_objfile->type_unit_groups =
ed2dc618 8310 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
8311 }
8312
8313 /* Do we need to create a new group, or can we use an existing one? */
8314
8315 if (stmt_list)
8316 {
8317 line_offset = DW_UNSND (stmt_list);
8318 ++tu_stats->nr_symtab_sharers;
8319 }
8320 else
8321 {
8322 /* Ugh, no stmt_list. Rare, but we have to handle it.
8323 We can do various things here like create one group per TU or
8324 spread them over multiple groups to split up the expansion work.
8325 To avoid worst case scenarios (too many groups or too large groups)
8326 we, umm, group them in bunches. */
8327 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
8328 | (tu_stats->nr_stmt_less_type_units
8329 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
8330 ++tu_stats->nr_stmt_less_type_units;
8331 }
8332
094b34ac 8333 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 8334 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
8335 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
8336 &type_unit_group_for_lookup, INSERT);
8337 if (*slot != NULL)
8338 {
9a3c8263 8339 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
8340 gdb_assert (tu_group != NULL);
8341 }
8342 else
8343 {
9c541725 8344 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 8345 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
8346 *slot = tu_group;
8347 ++tu_stats->nr_symtabs;
8348 }
8349
8350 return tu_group;
8351}
0018ea6f
DE
8352\f
8353/* Partial symbol tables. */
8354
8355/* Create a psymtab named NAME and assign it to PER_CU.
8356
8357 The caller must fill in the following details:
8358 dirname, textlow, texthigh. */
8359
8360static struct partial_symtab *
8361create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8362{
e3b94546 8363 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
0018ea6f
DE
8364 struct partial_symtab *pst;
8365
18a94d75 8366 pst = start_psymtab_common (objfile, name, 0,
af5bf4ad
SM
8367 objfile->global_psymbols,
8368 objfile->static_psymbols);
0018ea6f
DE
8369
8370 pst->psymtabs_addrmap_supported = 1;
8371
8372 /* This is the glue that links PST into GDB's symbol API. */
8373 pst->read_symtab_private = per_cu;
8374 pst->read_symtab = dwarf2_read_symtab;
8375 per_cu->v.psymtab = pst;
8376
8377 return pst;
8378}
8379
b93601f3
TT
8380/* The DATA object passed to process_psymtab_comp_unit_reader has this
8381 type. */
8382
8383struct process_psymtab_comp_unit_data
8384{
8385 /* True if we are reading a DW_TAG_partial_unit. */
8386
8387 int want_partial_unit;
8388
8389 /* The "pretend" language that is used if the CU doesn't declare a
8390 language. */
8391
8392 enum language pretend_language;
8393};
8394
0018ea6f
DE
8395/* die_reader_func for process_psymtab_comp_unit. */
8396
8397static void
8398process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8399 const gdb_byte *info_ptr,
0018ea6f
DE
8400 struct die_info *comp_unit_die,
8401 int has_children,
8402 void *data)
8403{
8404 struct dwarf2_cu *cu = reader->cu;
518817b3 8405 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 8406 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 8407 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
8408 CORE_ADDR baseaddr;
8409 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8410 struct partial_symtab *pst;
3a2b436a 8411 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 8412 const char *filename;
9a3c8263
SM
8413 struct process_psymtab_comp_unit_data *info
8414 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 8415
b93601f3 8416 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
8417 return;
8418
8419 gdb_assert (! per_cu->is_debug_types);
8420
b93601f3 8421 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
8422
8423 cu->list_in_scope = &file_symbols;
8424
8425 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
8426 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8427 if (filename == NULL)
0018ea6f 8428 filename = "";
0018ea6f
DE
8429
8430 pst = create_partial_symtab (per_cu, filename);
8431
8432 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 8433 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
8434
8435 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8436
8437 dwarf2_find_base_address (comp_unit_die, cu);
8438
8439 /* Possibly set the default values of LOWPC and HIGHPC from
8440 `DW_AT_ranges'. */
3a2b436a
JK
8441 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8442 &best_highpc, cu, pst);
8443 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
0018ea6f
DE
8444 /* Store the contiguous range if it is not empty; it can be empty for
8445 CUs with no code. */
8446 addrmap_set_empty (objfile->psymtabs_addrmap,
3e29f34a
MR
8447 gdbarch_adjust_dwarf2_addr (gdbarch,
8448 best_lowpc + baseaddr),
8449 gdbarch_adjust_dwarf2_addr (gdbarch,
8450 best_highpc + baseaddr) - 1,
8451 pst);
0018ea6f
DE
8452
8453 /* Check if comp unit has_children.
8454 If so, read the rest of the partial symbols from this comp unit.
8455 If not, there's no more debug_info for this comp unit. */
8456 if (has_children)
8457 {
8458 struct partial_die_info *first_die;
8459 CORE_ADDR lowpc, highpc;
8460
8461 lowpc = ((CORE_ADDR) -1);
8462 highpc = ((CORE_ADDR) 0);
8463
8464 first_die = load_partial_dies (reader, info_ptr, 1);
8465
8466 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 8467 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
8468
8469 /* If we didn't find a lowpc, set it to highpc to avoid
8470 complaints from `maint check'. */
8471 if (lowpc == ((CORE_ADDR) -1))
8472 lowpc = highpc;
8473
8474 /* If the compilation unit didn't have an explicit address range,
8475 then use the information extracted from its child dies. */
e385593e 8476 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
8477 {
8478 best_lowpc = lowpc;
8479 best_highpc = highpc;
8480 }
8481 }
3e29f34a
MR
8482 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
8483 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 8484
8763cede 8485 end_psymtab_common (objfile, pst);
0018ea6f
DE
8486
8487 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8488 {
8489 int i;
8490 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8491 struct dwarf2_per_cu_data *iter;
8492
8493 /* Fill in 'dependencies' here; we fill in 'users' in a
8494 post-pass. */
8495 pst->number_of_dependencies = len;
8d749320
SM
8496 pst->dependencies =
8497 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
8498 for (i = 0;
8499 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8500 i, iter);
8501 ++i)
8502 pst->dependencies[i] = iter->v.psymtab;
8503
8504 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8505 }
8506
8507 /* Get the list of files included in the current compilation unit,
8508 and build a psymtab for each of them. */
8509 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8510
b4f54984 8511 if (dwarf_read_debug)
0018ea6f
DE
8512 {
8513 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8514
8515 fprintf_unfiltered (gdb_stdlog,
9d8780f0 8516 "Psymtab for %s unit @%s: %s - %s"
0018ea6f
DE
8517 ", %d global, %d static syms\n",
8518 per_cu->is_debug_types ? "type" : "comp",
9d8780f0 8519 sect_offset_str (per_cu->sect_off),
0018ea6f
DE
8520 paddress (gdbarch, pst->textlow),
8521 paddress (gdbarch, pst->texthigh),
8522 pst->n_global_syms, pst->n_static_syms);
8523 }
8524}
8525
8526/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8527 Process compilation unit THIS_CU for a psymtab. */
8528
8529static void
8530process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8531 int want_partial_unit,
8532 enum language pretend_language)
0018ea6f
DE
8533{
8534 /* If this compilation unit was already read in, free the
8535 cached copy in order to read it in again. This is
8536 necessary because we skipped some symbols when we first
8537 read in the compilation unit (see load_partial_dies).
8538 This problem could be avoided, but the benefit is unclear. */
8539 if (this_cu->cu != NULL)
8540 free_one_cached_comp_unit (this_cu);
8541
f1902523
JK
8542 if (this_cu->is_debug_types)
8543 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8544 NULL);
8545 else
8546 {
8547 process_psymtab_comp_unit_data info;
8548 info.want_partial_unit = want_partial_unit;
8549 info.pretend_language = pretend_language;
8550 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8551 process_psymtab_comp_unit_reader, &info);
8552 }
0018ea6f
DE
8553
8554 /* Age out any secondary CUs. */
ed2dc618 8555 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 8556}
f4dc4d17
DE
8557
8558/* Reader function for build_type_psymtabs. */
8559
8560static void
8561build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8562 const gdb_byte *info_ptr,
f4dc4d17
DE
8563 struct die_info *type_unit_die,
8564 int has_children,
8565 void *data)
8566{
ed2dc618 8567 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 8568 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8569 struct objfile *objfile = dwarf2_per_objfile->objfile;
8570 struct dwarf2_cu *cu = reader->cu;
8571 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8572 struct signatured_type *sig_type;
f4dc4d17
DE
8573 struct type_unit_group *tu_group;
8574 struct attribute *attr;
8575 struct partial_die_info *first_die;
8576 CORE_ADDR lowpc, highpc;
8577 struct partial_symtab *pst;
8578
8579 gdb_assert (data == NULL);
0186c6a7
DE
8580 gdb_assert (per_cu->is_debug_types);
8581 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8582
8583 if (! has_children)
8584 return;
8585
8586 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8587 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8588
0186c6a7 8589 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
8590
8591 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8592 cu->list_in_scope = &file_symbols;
8593 pst = create_partial_symtab (per_cu, "");
8594 pst->anonymous = 1;
8595
8596 first_die = load_partial_dies (reader, info_ptr, 1);
8597
8598 lowpc = (CORE_ADDR) -1;
8599 highpc = (CORE_ADDR) 0;
8600 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8601
8763cede 8602 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8603}
8604
73051182
DE
8605/* Struct used to sort TUs by their abbreviation table offset. */
8606
8607struct tu_abbrev_offset
8608{
8609 struct signatured_type *sig_type;
8610 sect_offset abbrev_offset;
8611};
8612
8613/* Helper routine for build_type_psymtabs_1, passed to qsort. */
8614
8615static int
8616sort_tu_by_abbrev_offset (const void *ap, const void *bp)
8617{
9a3c8263
SM
8618 const struct tu_abbrev_offset * const *a
8619 = (const struct tu_abbrev_offset * const*) ap;
8620 const struct tu_abbrev_offset * const *b
8621 = (const struct tu_abbrev_offset * const*) bp;
9c541725
PA
8622 sect_offset aoff = (*a)->abbrev_offset;
8623 sect_offset boff = (*b)->abbrev_offset;
73051182
DE
8624
8625 return (aoff > boff) - (aoff < boff);
8626}
8627
8628/* Efficiently read all the type units.
8629 This does the bulk of the work for build_type_psymtabs.
8630
8631 The efficiency is because we sort TUs by the abbrev table they use and
8632 only read each abbrev table once. In one program there are 200K TUs
8633 sharing 8K abbrev tables.
8634
8635 The main purpose of this function is to support building the
8636 dwarf2_per_objfile->type_unit_groups table.
8637 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8638 can collapse the search space by grouping them by stmt_list.
8639 The savings can be significant, in the same program from above the 200K TUs
8640 share 8K stmt_list tables.
8641
8642 FUNC is expected to call get_type_unit_group, which will create the
8643 struct type_unit_group if necessary and add it to
8644 dwarf2_per_objfile->type_unit_groups. */
8645
8646static void
ed2dc618 8647build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 8648{
73051182
DE
8649 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8650 struct cleanup *cleanups;
685af9cd 8651 abbrev_table_up abbrev_table;
73051182
DE
8652 sect_offset abbrev_offset;
8653 struct tu_abbrev_offset *sorted_by_abbrev;
73051182
DE
8654 int i;
8655
8656 /* It's up to the caller to not call us multiple times. */
8657 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8658
8659 if (dwarf2_per_objfile->n_type_units == 0)
8660 return;
8661
8662 /* TUs typically share abbrev tables, and there can be way more TUs than
8663 abbrev tables. Sort by abbrev table to reduce the number of times we
8664 read each abbrev table in.
8665 Alternatives are to punt or to maintain a cache of abbrev tables.
8666 This is simpler and efficient enough for now.
8667
8668 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8669 symtab to use). Typically TUs with the same abbrev offset have the same
8670 stmt_list value too so in practice this should work well.
8671
8672 The basic algorithm here is:
8673
8674 sort TUs by abbrev table
8675 for each TU with same abbrev table:
8676 read abbrev table if first user
8677 read TU top level DIE
8678 [IWBN if DWO skeletons had DW_AT_stmt_list]
8679 call FUNC */
8680
b4f54984 8681 if (dwarf_read_debug)
73051182
DE
8682 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8683
8684 /* Sort in a separate table to maintain the order of all_type_units
8685 for .gdb_index: TU indices directly index all_type_units. */
8686 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
8687 dwarf2_per_objfile->n_type_units);
8688 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8689 {
8690 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
8691
8692 sorted_by_abbrev[i].sig_type = sig_type;
8693 sorted_by_abbrev[i].abbrev_offset =
ed2dc618
SM
8694 read_abbrev_offset (dwarf2_per_objfile,
8695 sig_type->per_cu.section,
9c541725 8696 sig_type->per_cu.sect_off);
73051182
DE
8697 }
8698 cleanups = make_cleanup (xfree, sorted_by_abbrev);
8699 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
8700 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
8701
9c541725 8702 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182
DE
8703
8704 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8705 {
8706 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
8707
8708 /* Switch to the next abbrev table if necessary. */
8709 if (abbrev_table == NULL
9c541725 8710 || tu->abbrev_offset != abbrev_offset)
73051182 8711 {
73051182
DE
8712 abbrev_offset = tu->abbrev_offset;
8713 abbrev_table =
ed2dc618
SM
8714 abbrev_table_read_table (dwarf2_per_objfile,
8715 &dwarf2_per_objfile->abbrev,
73051182
DE
8716 abbrev_offset);
8717 ++tu_stats->nr_uniq_abbrev_tables;
8718 }
8719
685af9cd
TT
8720 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table.get (),
8721 0, 0, build_type_psymtabs_reader, NULL);
73051182
DE
8722 }
8723
73051182 8724 do_cleanups (cleanups);
6aa5f3a6 8725}
73051182 8726
6aa5f3a6
DE
8727/* Print collected type unit statistics. */
8728
8729static void
ed2dc618 8730print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8731{
8732 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8733
8734 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8735 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
8736 dwarf2_per_objfile->n_type_units);
8737 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8738 tu_stats->nr_uniq_abbrev_tables);
8739 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8740 tu_stats->nr_symtabs);
8741 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8742 tu_stats->nr_symtab_sharers);
8743 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8744 tu_stats->nr_stmt_less_type_units);
8745 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8746 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8747}
8748
f4dc4d17
DE
8749/* Traversal function for build_type_psymtabs. */
8750
8751static int
8752build_type_psymtab_dependencies (void **slot, void *info)
8753{
ed2dc618
SM
8754 struct dwarf2_per_objfile *dwarf2_per_objfile
8755 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8756 struct objfile *objfile = dwarf2_per_objfile->objfile;
8757 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8758 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 8759 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
8760 int len = VEC_length (sig_type_ptr, tu_group->tus);
8761 struct signatured_type *iter;
f4dc4d17
DE
8762 int i;
8763
8764 gdb_assert (len > 0);
0186c6a7 8765 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8766
8767 pst->number_of_dependencies = len;
8d749320
SM
8768 pst->dependencies =
8769 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 8770 for (i = 0;
0186c6a7 8771 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
8772 ++i)
8773 {
0186c6a7
DE
8774 gdb_assert (iter->per_cu.is_debug_types);
8775 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8776 iter->type_unit_group = tu_group;
f4dc4d17
DE
8777 }
8778
0186c6a7 8779 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
8780
8781 return 1;
8782}
8783
8784/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8785 Build partial symbol tables for the .debug_types comp-units. */
8786
8787static void
ed2dc618 8788build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8789{
ed2dc618 8790 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8791 return;
8792
ed2dc618 8793 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8794}
f4dc4d17 8795
6aa5f3a6
DE
8796/* Traversal function for process_skeletonless_type_unit.
8797 Read a TU in a DWO file and build partial symbols for it. */
8798
8799static int
8800process_skeletonless_type_unit (void **slot, void *info)
8801{
8802 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8803 struct dwarf2_per_objfile *dwarf2_per_objfile
8804 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8805 struct signatured_type find_entry, *entry;
8806
8807 /* If this TU doesn't exist in the global table, add it and read it in. */
8808
8809 if (dwarf2_per_objfile->signatured_types == NULL)
8810 {
8811 dwarf2_per_objfile->signatured_types
ed2dc618 8812 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8813 }
8814
8815 find_entry.signature = dwo_unit->signature;
8816 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8817 INSERT);
8818 /* If we've already seen this type there's nothing to do. What's happening
8819 is we're doing our own version of comdat-folding here. */
8820 if (*slot != NULL)
8821 return 1;
8822
8823 /* This does the job that create_all_type_units would have done for
8824 this TU. */
ed2dc618
SM
8825 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8826 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8827 *slot = entry;
8828
8829 /* This does the job that build_type_psymtabs_1 would have done. */
8830 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8831 build_type_psymtabs_reader, NULL);
8832
8833 return 1;
8834}
8835
8836/* Traversal function for process_skeletonless_type_units. */
8837
8838static int
8839process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8840{
8841 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8842
8843 if (dwo_file->tus != NULL)
8844 {
8845 htab_traverse_noresize (dwo_file->tus,
8846 process_skeletonless_type_unit, info);
8847 }
8848
8849 return 1;
8850}
8851
8852/* Scan all TUs of DWO files, verifying we've processed them.
8853 This is needed in case a TU was emitted without its skeleton.
8854 Note: This can't be done until we know what all the DWO files are. */
8855
8856static void
ed2dc618 8857process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8858{
8859 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8860 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8861 && dwarf2_per_objfile->dwo_files != NULL)
8862 {
8863 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8864 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8865 dwarf2_per_objfile);
6aa5f3a6 8866 }
348e048f
DE
8867}
8868
ed2dc618 8869/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8870
8871static void
ed2dc618 8872set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad
TT
8873{
8874 int i;
8875
8876 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8877 {
ed2dc618 8878 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
95554aad
TT
8879 struct partial_symtab *pst = per_cu->v.psymtab;
8880 int j;
8881
36586728
TT
8882 if (pst == NULL)
8883 continue;
8884
95554aad
TT
8885 for (j = 0; j < pst->number_of_dependencies; ++j)
8886 {
8887 /* Set the 'user' field only if it is not already set. */
8888 if (pst->dependencies[j]->user == NULL)
8889 pst->dependencies[j]->user = pst;
8890 }
8891 }
8892}
8893
93311388
DE
8894/* Build the partial symbol table by doing a quick pass through the
8895 .debug_info and .debug_abbrev sections. */
72bf9492 8896
93311388 8897static void
ed2dc618 8898dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8899{
791afaa2 8900 struct cleanup *back_to;
21b2bd31 8901 int i;
ed2dc618 8902 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8903
b4f54984 8904 if (dwarf_read_debug)
45cfd468
DE
8905 {
8906 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8907 objfile_name (objfile));
45cfd468
DE
8908 }
8909
98bfdba5
PA
8910 dwarf2_per_objfile->reading_partial_symbols = 1;
8911
be391dca 8912 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8913
93311388
DE
8914 /* Any cached compilation units will be linked by the per-objfile
8915 read_in_chain. Make sure to free them when we're done. */
ed2dc618 8916 back_to = make_cleanup (free_cached_comp_units, dwarf2_per_objfile);
72bf9492 8917
ed2dc618 8918 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8919
ed2dc618 8920 create_all_comp_units (dwarf2_per_objfile);
c906108c 8921
60606b2c
TT
8922 /* Create a temporary address map on a temporary obstack. We later
8923 copy this to the final obstack. */
8268c778 8924 auto_obstack temp_obstack;
791afaa2
TT
8925
8926 scoped_restore save_psymtabs_addrmap
8927 = make_scoped_restore (&objfile->psymtabs_addrmap,
8928 addrmap_create_mutable (&temp_obstack));
72bf9492 8929
21b2bd31 8930 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 8931 {
ed2dc618 8932 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
aaa75496 8933
b93601f3 8934 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 8935 }
ff013f42 8936
6aa5f3a6 8937 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8938 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8939
8940 /* Now that all TUs have been processed we can fill in the dependencies. */
8941 if (dwarf2_per_objfile->type_unit_groups != NULL)
8942 {
8943 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8944 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8945 }
8946
b4f54984 8947 if (dwarf_read_debug)
ed2dc618 8948 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8949
ed2dc618 8950 set_partial_user (dwarf2_per_objfile);
95554aad 8951
ff013f42
JK
8952 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8953 &objfile->objfile_obstack);
791afaa2
TT
8954 /* At this point we want to keep the address map. */
8955 save_psymtabs_addrmap.release ();
ff013f42 8956
ae038cb0 8957 do_cleanups (back_to);
45cfd468 8958
b4f54984 8959 if (dwarf_read_debug)
45cfd468 8960 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8961 objfile_name (objfile));
ae038cb0
DJ
8962}
8963
3019eac3 8964/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
8965
8966static void
dee91e82 8967load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8968 const gdb_byte *info_ptr,
dee91e82
DE
8969 struct die_info *comp_unit_die,
8970 int has_children,
8971 void *data)
ae038cb0 8972{
dee91e82 8973 struct dwarf2_cu *cu = reader->cu;
ae038cb0 8974
95554aad 8975 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 8976
ae038cb0
DJ
8977 /* Check if comp unit has_children.
8978 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 8979 If not, there's no more debug_info for this comp unit. */
d85a05f0 8980 if (has_children)
dee91e82
DE
8981 load_partial_dies (reader, info_ptr, 0);
8982}
98bfdba5 8983
dee91e82
DE
8984/* Load the partial DIEs for a secondary CU into memory.
8985 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8986
dee91e82
DE
8987static void
8988load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8989{
f4dc4d17
DE
8990 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8991 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
8992}
8993
ae038cb0 8994static void
ed2dc618 8995read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8996 struct dwarf2_section_info *section,
f1902523 8997 struct dwarf2_section_info *abbrev_section,
36586728
TT
8998 unsigned int is_dwz,
8999 int *n_allocated,
9000 int *n_comp_units,
9001 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 9002{
d521ce57 9003 const gdb_byte *info_ptr;
ed2dc618 9004 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 9005
b4f54984 9006 if (dwarf_read_debug)
bf6af496 9007 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
9008 get_section_name (section),
9009 get_section_file_name (section));
bf6af496 9010
36586728 9011 dwarf2_read_section (objfile, section);
ae038cb0 9012
36586728 9013 info_ptr = section->buffer;
6e70227d 9014
36586728 9015 while (info_ptr < section->buffer + section->size)
ae038cb0 9016 {
ae038cb0 9017 struct dwarf2_per_cu_data *this_cu;
ae038cb0 9018
9c541725 9019 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 9020
f1902523 9021 comp_unit_head cu_header;
ed2dc618
SM
9022 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
9023 abbrev_section, info_ptr,
9024 rcuh_kind::COMPILE);
ae038cb0
DJ
9025
9026 /* Save the compilation unit for later lookup. */
f1902523
JK
9027 if (cu_header.unit_type != DW_UT_type)
9028 {
9029 this_cu = XOBNEW (&objfile->objfile_obstack,
9030 struct dwarf2_per_cu_data);
9031 memset (this_cu, 0, sizeof (*this_cu));
9032 }
9033 else
9034 {
9035 auto sig_type = XOBNEW (&objfile->objfile_obstack,
9036 struct signatured_type);
9037 memset (sig_type, 0, sizeof (*sig_type));
9038 sig_type->signature = cu_header.signature;
9039 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
9040 this_cu = &sig_type->per_cu;
9041 }
9042 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 9043 this_cu->sect_off = sect_off;
f1902523 9044 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 9045 this_cu->is_dwz = is_dwz;
e3b94546 9046 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 9047 this_cu->section = section;
ae038cb0 9048
36586728 9049 if (*n_comp_units == *n_allocated)
ae038cb0 9050 {
36586728 9051 *n_allocated *= 2;
224c3ddb
SM
9052 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
9053 *all_comp_units, *n_allocated);
ae038cb0 9054 }
36586728
TT
9055 (*all_comp_units)[*n_comp_units] = this_cu;
9056 ++*n_comp_units;
ae038cb0
DJ
9057
9058 info_ptr = info_ptr + this_cu->length;
9059 }
36586728
TT
9060}
9061
9062/* Create a list of all compilation units in OBJFILE.
9063 This is only done for -readnow and building partial symtabs. */
9064
9065static void
ed2dc618 9066create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728
TT
9067{
9068 int n_allocated;
9069 int n_comp_units;
9070 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 9071 struct dwz_file *dwz;
ed2dc618 9072 struct objfile *objfile = dwarf2_per_objfile->objfile;
36586728
TT
9073
9074 n_comp_units = 0;
9075 n_allocated = 10;
8d749320 9076 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
36586728 9077
ed2dc618 9078 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
f1902523 9079 &dwarf2_per_objfile->abbrev, 0,
36586728
TT
9080 &n_allocated, &n_comp_units, &all_comp_units);
9081
ed2dc618 9082 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 9083 if (dwz != NULL)
ed2dc618
SM
9084 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
9085 1, &n_allocated, &n_comp_units,
4db1a1dc 9086 &all_comp_units);
ae038cb0 9087
8d749320
SM
9088 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
9089 struct dwarf2_per_cu_data *,
9090 n_comp_units);
ae038cb0
DJ
9091 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
9092 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
9093 xfree (all_comp_units);
9094 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
9095}
9096
5734ee8b 9097/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 9098 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 9099 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
9100 DW_AT_ranges). See the comments of add_partial_subprogram on how
9101 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 9102
72bf9492
DJ
9103static void
9104scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
9105 CORE_ADDR *highpc, int set_addrmap,
9106 struct dwarf2_cu *cu)
c906108c 9107{
72bf9492 9108 struct partial_die_info *pdi;
c906108c 9109
91c24f0a
DC
9110 /* Now, march along the PDI's, descending into ones which have
9111 interesting children but skipping the children of the other ones,
9112 until we reach the end of the compilation unit. */
c906108c 9113
72bf9492 9114 pdi = first_die;
91c24f0a 9115
72bf9492
DJ
9116 while (pdi != NULL)
9117 {
52356b79 9118 pdi->fixup (cu);
c906108c 9119
f55ee35c 9120 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
9121 children, so we need to look at them. Ditto for anonymous
9122 enums. */
933c6fe4 9123
72bf9492 9124 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 9125 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
9126 || pdi->tag == DW_TAG_imported_unit
9127 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 9128 {
72bf9492 9129 switch (pdi->tag)
c906108c
SS
9130 {
9131 case DW_TAG_subprogram:
b1dc1806 9132 case DW_TAG_inlined_subroutine:
cdc07690 9133 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 9134 break;
72929c62 9135 case DW_TAG_constant:
c906108c
SS
9136 case DW_TAG_variable:
9137 case DW_TAG_typedef:
91c24f0a 9138 case DW_TAG_union_type:
72bf9492 9139 if (!pdi->is_declaration)
63d06c5c 9140 {
72bf9492 9141 add_partial_symbol (pdi, cu);
63d06c5c
DC
9142 }
9143 break;
c906108c 9144 case DW_TAG_class_type:
680b30c7 9145 case DW_TAG_interface_type:
c906108c 9146 case DW_TAG_structure_type:
72bf9492 9147 if (!pdi->is_declaration)
c906108c 9148 {
72bf9492 9149 add_partial_symbol (pdi, cu);
c906108c 9150 }
e98c9e7c
TT
9151 if (cu->language == language_rust && pdi->has_children)
9152 scan_partial_symbols (pdi->die_child, lowpc, highpc,
9153 set_addrmap, cu);
c906108c 9154 break;
91c24f0a 9155 case DW_TAG_enumeration_type:
72bf9492
DJ
9156 if (!pdi->is_declaration)
9157 add_partial_enumeration (pdi, cu);
c906108c
SS
9158 break;
9159 case DW_TAG_base_type:
a02abb62 9160 case DW_TAG_subrange_type:
c906108c 9161 /* File scope base type definitions are added to the partial
c5aa993b 9162 symbol table. */
72bf9492 9163 add_partial_symbol (pdi, cu);
c906108c 9164 break;
d9fa45fe 9165 case DW_TAG_namespace:
cdc07690 9166 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 9167 break;
5d7cb8df 9168 case DW_TAG_module:
cdc07690 9169 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 9170 break;
95554aad
TT
9171 case DW_TAG_imported_unit:
9172 {
9173 struct dwarf2_per_cu_data *per_cu;
9174
f4dc4d17
DE
9175 /* For now we don't handle imported units in type units. */
9176 if (cu->per_cu->is_debug_types)
9177 {
9178 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9179 " supported in type units [in module %s]"),
518817b3 9180 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
9181 }
9182
e3b94546
SM
9183 per_cu = dwarf2_find_containing_comp_unit
9184 (pdi->d.sect_off, pdi->is_dwz,
518817b3 9185 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
9186
9187 /* Go read the partial unit, if needed. */
9188 if (per_cu->v.psymtab == NULL)
b93601f3 9189 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 9190
f4dc4d17 9191 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 9192 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
9193 }
9194 break;
74921315
KS
9195 case DW_TAG_imported_declaration:
9196 add_partial_symbol (pdi, cu);
9197 break;
c906108c
SS
9198 default:
9199 break;
9200 }
9201 }
9202
72bf9492
DJ
9203 /* If the die has a sibling, skip to the sibling. */
9204
9205 pdi = pdi->die_sibling;
9206 }
9207}
9208
9209/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 9210
72bf9492 9211 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 9212 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
9213 Enumerators are an exception; they use the scope of their parent
9214 enumeration type, i.e. the name of the enumeration type is not
9215 prepended to the enumerator.
91c24f0a 9216
72bf9492
DJ
9217 There are two complexities. One is DW_AT_specification; in this
9218 case "parent" means the parent of the target of the specification,
9219 instead of the direct parent of the DIE. The other is compilers
9220 which do not emit DW_TAG_namespace; in this case we try to guess
9221 the fully qualified name of structure types from their members'
9222 linkage names. This must be done using the DIE's children rather
9223 than the children of any DW_AT_specification target. We only need
9224 to do this for structures at the top level, i.e. if the target of
9225 any DW_AT_specification (if any; otherwise the DIE itself) does not
9226 have a parent. */
9227
9228/* Compute the scope prefix associated with PDI's parent, in
9229 compilation unit CU. The result will be allocated on CU's
9230 comp_unit_obstack, or a copy of the already allocated PDI->NAME
9231 field. NULL is returned if no prefix is necessary. */
15d034d0 9232static const char *
72bf9492
DJ
9233partial_die_parent_scope (struct partial_die_info *pdi,
9234 struct dwarf2_cu *cu)
9235{
15d034d0 9236 const char *grandparent_scope;
72bf9492 9237 struct partial_die_info *parent, *real_pdi;
91c24f0a 9238
72bf9492
DJ
9239 /* We need to look at our parent DIE; if we have a DW_AT_specification,
9240 then this means the parent of the specification DIE. */
9241
9242 real_pdi = pdi;
72bf9492 9243 while (real_pdi->has_specification)
36586728
TT
9244 real_pdi = find_partial_die (real_pdi->spec_offset,
9245 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
9246
9247 parent = real_pdi->die_parent;
9248 if (parent == NULL)
9249 return NULL;
9250
9251 if (parent->scope_set)
9252 return parent->scope;
9253
52356b79 9254 parent->fixup (cu);
72bf9492 9255
10b3939b 9256 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 9257
acebe513
UW
9258 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
9259 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
9260 Work around this problem here. */
9261 if (cu->language == language_cplus
6e70227d 9262 && parent->tag == DW_TAG_namespace
acebe513
UW
9263 && strcmp (parent->name, "::") == 0
9264 && grandparent_scope == NULL)
9265 {
9266 parent->scope = NULL;
9267 parent->scope_set = 1;
9268 return NULL;
9269 }
9270
9c6c53f7
SA
9271 if (pdi->tag == DW_TAG_enumerator)
9272 /* Enumerators should not get the name of the enumeration as a prefix. */
9273 parent->scope = grandparent_scope;
9274 else if (parent->tag == DW_TAG_namespace
f55ee35c 9275 || parent->tag == DW_TAG_module
72bf9492
DJ
9276 || parent->tag == DW_TAG_structure_type
9277 || parent->tag == DW_TAG_class_type
680b30c7 9278 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
9279 || parent->tag == DW_TAG_union_type
9280 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
9281 {
9282 if (grandparent_scope == NULL)
9283 parent->scope = parent->name;
9284 else
3e43a32a
MS
9285 parent->scope = typename_concat (&cu->comp_unit_obstack,
9286 grandparent_scope,
f55ee35c 9287 parent->name, 0, cu);
72bf9492 9288 }
72bf9492
DJ
9289 else
9290 {
9291 /* FIXME drow/2004-04-01: What should we be doing with
9292 function-local names? For partial symbols, we should probably be
9293 ignoring them. */
9294 complaint (&symfile_complaints,
9d8780f0
SM
9295 _("unhandled containing DIE tag %d for DIE at %s"),
9296 parent->tag, sect_offset_str (pdi->sect_off));
72bf9492 9297 parent->scope = grandparent_scope;
c906108c
SS
9298 }
9299
72bf9492
DJ
9300 parent->scope_set = 1;
9301 return parent->scope;
9302}
9303
9304/* Return the fully scoped name associated with PDI, from compilation unit
9305 CU. The result will be allocated with malloc. */
4568ecf9 9306
72bf9492
DJ
9307static char *
9308partial_die_full_name (struct partial_die_info *pdi,
9309 struct dwarf2_cu *cu)
9310{
15d034d0 9311 const char *parent_scope;
72bf9492 9312
98bfdba5
PA
9313 /* If this is a template instantiation, we can not work out the
9314 template arguments from partial DIEs. So, unfortunately, we have
9315 to go through the full DIEs. At least any work we do building
9316 types here will be reused if full symbols are loaded later. */
9317 if (pdi->has_template_arguments)
9318 {
52356b79 9319 pdi->fixup (cu);
98bfdba5
PA
9320
9321 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
9322 {
9323 struct die_info *die;
9324 struct attribute attr;
9325 struct dwarf2_cu *ref_cu = cu;
9326
b64f50a1 9327 /* DW_FORM_ref_addr is using section offset. */
b4069958 9328 attr.name = (enum dwarf_attribute) 0;
98bfdba5 9329 attr.form = DW_FORM_ref_addr;
9c541725 9330 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
9331 die = follow_die_ref (NULL, &attr, &ref_cu);
9332
9333 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
9334 }
9335 }
9336
72bf9492
DJ
9337 parent_scope = partial_die_parent_scope (pdi, cu);
9338 if (parent_scope == NULL)
9339 return NULL;
9340 else
f55ee35c 9341 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
9342}
9343
9344static void
72bf9492 9345add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 9346{
518817b3
SM
9347 struct dwarf2_per_objfile *dwarf2_per_objfile
9348 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 9349 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9350 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 9351 CORE_ADDR addr = 0;
15d034d0 9352 const char *actual_name = NULL;
e142c38c 9353 CORE_ADDR baseaddr;
15d034d0 9354 char *built_actual_name;
e142c38c
DJ
9355
9356 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9357
15d034d0
TT
9358 built_actual_name = partial_die_full_name (pdi, cu);
9359 if (built_actual_name != NULL)
9360 actual_name = built_actual_name;
63d06c5c 9361
72bf9492
DJ
9362 if (actual_name == NULL)
9363 actual_name = pdi->name;
9364
c906108c
SS
9365 switch (pdi->tag)
9366 {
b1dc1806 9367 case DW_TAG_inlined_subroutine:
c906108c 9368 case DW_TAG_subprogram:
3e29f34a 9369 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 9370 if (pdi->is_external || cu->language == language_ada)
c906108c 9371 {
2cfa0c8d
JB
9372 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
9373 of the global scope. But in Ada, we want to be able to access
9374 nested procedures globally. So all Ada subprograms are stored
9375 in the global scope. */
f47fb265 9376 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9377 built_actual_name != NULL,
f47fb265
MS
9378 VAR_DOMAIN, LOC_BLOCK,
9379 &objfile->global_psymbols,
1762568f 9380 addr, cu->language, objfile);
c906108c
SS
9381 }
9382 else
9383 {
f47fb265 9384 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9385 built_actual_name != NULL,
f47fb265
MS
9386 VAR_DOMAIN, LOC_BLOCK,
9387 &objfile->static_psymbols,
1762568f 9388 addr, cu->language, objfile);
c906108c 9389 }
0c1b455e
TT
9390
9391 if (pdi->main_subprogram && actual_name != NULL)
9392 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 9393 break;
72929c62
JB
9394 case DW_TAG_constant:
9395 {
af5bf4ad 9396 std::vector<partial_symbol *> *list;
72929c62
JB
9397
9398 if (pdi->is_external)
9399 list = &objfile->global_psymbols;
9400 else
9401 list = &objfile->static_psymbols;
f47fb265 9402 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9403 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 9404 list, 0, cu->language, objfile);
72929c62
JB
9405 }
9406 break;
c906108c 9407 case DW_TAG_variable:
95554aad
TT
9408 if (pdi->d.locdesc)
9409 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 9410
95554aad 9411 if (pdi->d.locdesc
caac4577
JG
9412 && addr == 0
9413 && !dwarf2_per_objfile->has_section_at_zero)
9414 {
9415 /* A global or static variable may also have been stripped
9416 out by the linker if unused, in which case its address
9417 will be nullified; do not add such variables into partial
9418 symbol table then. */
9419 }
9420 else if (pdi->is_external)
c906108c
SS
9421 {
9422 /* Global Variable.
9423 Don't enter into the minimal symbol tables as there is
9424 a minimal symbol table entry from the ELF symbols already.
9425 Enter into partial symbol table if it has a location
9426 descriptor or a type.
9427 If the location descriptor is missing, new_symbol will create
9428 a LOC_UNRESOLVED symbol, the address of the variable will then
9429 be determined from the minimal symbol table whenever the variable
9430 is referenced.
9431 The address for the partial symbol table entry is not
9432 used by GDB, but it comes in handy for debugging partial symbol
9433 table building. */
9434
95554aad 9435 if (pdi->d.locdesc || pdi->has_type)
f47fb265 9436 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9437 built_actual_name != NULL,
f47fb265
MS
9438 VAR_DOMAIN, LOC_STATIC,
9439 &objfile->global_psymbols,
1762568f 9440 addr + baseaddr,
f47fb265 9441 cu->language, objfile);
c906108c
SS
9442 }
9443 else
9444 {
ff908ebf
AW
9445 int has_loc = pdi->d.locdesc != NULL;
9446
9447 /* Static Variable. Skip symbols whose value we cannot know (those
9448 without location descriptors or constant values). */
9449 if (!has_loc && !pdi->has_const_value)
decbce07 9450 {
15d034d0 9451 xfree (built_actual_name);
decbce07
MS
9452 return;
9453 }
ff908ebf 9454
f47fb265 9455 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9456 built_actual_name != NULL,
f47fb265
MS
9457 VAR_DOMAIN, LOC_STATIC,
9458 &objfile->static_psymbols,
ff908ebf 9459 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 9460 cu->language, objfile);
c906108c
SS
9461 }
9462 break;
9463 case DW_TAG_typedef:
9464 case DW_TAG_base_type:
a02abb62 9465 case DW_TAG_subrange_type:
38d518c9 9466 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9467 built_actual_name != NULL,
176620f1 9468 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 9469 &objfile->static_psymbols,
1762568f 9470 0, cu->language, objfile);
c906108c 9471 break;
74921315 9472 case DW_TAG_imported_declaration:
72bf9492
DJ
9473 case DW_TAG_namespace:
9474 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9475 built_actual_name != NULL,
72bf9492
DJ
9476 VAR_DOMAIN, LOC_TYPEDEF,
9477 &objfile->global_psymbols,
1762568f 9478 0, cu->language, objfile);
72bf9492 9479 break;
530e8392
KB
9480 case DW_TAG_module:
9481 add_psymbol_to_list (actual_name, strlen (actual_name),
9482 built_actual_name != NULL,
9483 MODULE_DOMAIN, LOC_TYPEDEF,
9484 &objfile->global_psymbols,
1762568f 9485 0, cu->language, objfile);
530e8392 9486 break;
c906108c 9487 case DW_TAG_class_type:
680b30c7 9488 case DW_TAG_interface_type:
c906108c
SS
9489 case DW_TAG_structure_type:
9490 case DW_TAG_union_type:
9491 case DW_TAG_enumeration_type:
fa4028e9
JB
9492 /* Skip external references. The DWARF standard says in the section
9493 about "Structure, Union, and Class Type Entries": "An incomplete
9494 structure, union or class type is represented by a structure,
9495 union or class entry that does not have a byte size attribute
9496 and that has a DW_AT_declaration attribute." */
9497 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 9498 {
15d034d0 9499 xfree (built_actual_name);
decbce07
MS
9500 return;
9501 }
fa4028e9 9502
63d06c5c
DC
9503 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9504 static vs. global. */
38d518c9 9505 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9506 built_actual_name != NULL,
176620f1 9507 STRUCT_DOMAIN, LOC_TYPEDEF,
9c37b5ae 9508 cu->language == language_cplus
63d06c5c
DC
9509 ? &objfile->global_psymbols
9510 : &objfile->static_psymbols,
1762568f 9511 0, cu->language, objfile);
c906108c 9512
c906108c
SS
9513 break;
9514 case DW_TAG_enumerator:
38d518c9 9515 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9516 built_actual_name != NULL,
176620f1 9517 VAR_DOMAIN, LOC_CONST,
9c37b5ae 9518 cu->language == language_cplus
f6fe98ef
DJ
9519 ? &objfile->global_psymbols
9520 : &objfile->static_psymbols,
1762568f 9521 0, cu->language, objfile);
c906108c
SS
9522 break;
9523 default:
9524 break;
9525 }
5c4e30ca 9526
15d034d0 9527 xfree (built_actual_name);
c906108c
SS
9528}
9529
5c4e30ca
DC
9530/* Read a partial die corresponding to a namespace; also, add a symbol
9531 corresponding to that namespace to the symbol table. NAMESPACE is
9532 the name of the enclosing namespace. */
91c24f0a 9533
72bf9492
DJ
9534static void
9535add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 9536 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9537 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 9538{
72bf9492 9539 /* Add a symbol for the namespace. */
e7c27a73 9540
72bf9492 9541 add_partial_symbol (pdi, cu);
5c4e30ca
DC
9542
9543 /* Now scan partial symbols in that namespace. */
9544
91c24f0a 9545 if (pdi->has_children)
cdc07690 9546 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
9547}
9548
5d7cb8df
JK
9549/* Read a partial die corresponding to a Fortran module. */
9550
9551static void
9552add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 9553 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 9554{
530e8392
KB
9555 /* Add a symbol for the namespace. */
9556
9557 add_partial_symbol (pdi, cu);
9558
f55ee35c 9559 /* Now scan partial symbols in that module. */
5d7cb8df
JK
9560
9561 if (pdi->has_children)
cdc07690 9562 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
9563}
9564
b1dc1806
XR
9565/* Read a partial die corresponding to a subprogram or an inlined
9566 subprogram and create a partial symbol for that subprogram.
9567 When the CU language allows it, this routine also defines a partial
9568 symbol for each nested subprogram that this subprogram contains.
9569 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9570 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 9571
cdc07690
YQ
9572 PDI may also be a lexical block, in which case we simply search
9573 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9574 Again, this is only performed when the CU language allows this
9575 type of definitions. */
9576
9577static void
9578add_partial_subprogram (struct partial_die_info *pdi,
9579 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9580 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 9581{
b1dc1806 9582 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
9583 {
9584 if (pdi->has_pc_info)
9585 {
9586 if (pdi->lowpc < *lowpc)
9587 *lowpc = pdi->lowpc;
9588 if (pdi->highpc > *highpc)
9589 *highpc = pdi->highpc;
cdc07690 9590 if (set_addrmap)
5734ee8b 9591 {
518817b3 9592 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
9593 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9594 CORE_ADDR baseaddr;
9595 CORE_ADDR highpc;
9596 CORE_ADDR lowpc;
5734ee8b
DJ
9597
9598 baseaddr = ANOFFSET (objfile->section_offsets,
9599 SECT_OFF_TEXT (objfile));
3e29f34a
MR
9600 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9601 pdi->lowpc + baseaddr);
9602 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9603 pdi->highpc + baseaddr);
9604 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 9605 cu->per_cu->v.psymtab);
5734ee8b 9606 }
481860b3
GB
9607 }
9608
9609 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9610 {
bc30ff58 9611 if (!pdi->is_declaration)
e8d05480
JB
9612 /* Ignore subprogram DIEs that do not have a name, they are
9613 illegal. Do not emit a complaint at this point, we will
9614 do so when we convert this psymtab into a symtab. */
9615 if (pdi->name)
9616 add_partial_symbol (pdi, cu);
bc30ff58
JB
9617 }
9618 }
6e70227d 9619
bc30ff58
JB
9620 if (! pdi->has_children)
9621 return;
9622
9623 if (cu->language == language_ada)
9624 {
9625 pdi = pdi->die_child;
9626 while (pdi != NULL)
9627 {
52356b79 9628 pdi->fixup (cu);
bc30ff58 9629 if (pdi->tag == DW_TAG_subprogram
b1dc1806 9630 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 9631 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9632 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9633 pdi = pdi->die_sibling;
9634 }
9635 }
9636}
9637
91c24f0a
DC
9638/* Read a partial die corresponding to an enumeration type. */
9639
72bf9492
DJ
9640static void
9641add_partial_enumeration (struct partial_die_info *enum_pdi,
9642 struct dwarf2_cu *cu)
91c24f0a 9643{
72bf9492 9644 struct partial_die_info *pdi;
91c24f0a
DC
9645
9646 if (enum_pdi->name != NULL)
72bf9492
DJ
9647 add_partial_symbol (enum_pdi, cu);
9648
9649 pdi = enum_pdi->die_child;
9650 while (pdi)
91c24f0a 9651 {
72bf9492 9652 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 9653 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 9654 else
72bf9492
DJ
9655 add_partial_symbol (pdi, cu);
9656 pdi = pdi->die_sibling;
91c24f0a 9657 }
91c24f0a
DC
9658}
9659
6caca83c
CC
9660/* Return the initial uleb128 in the die at INFO_PTR. */
9661
9662static unsigned int
d521ce57 9663peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9664{
9665 unsigned int bytes_read;
9666
9667 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9668}
9669
685af9cd
TT
9670/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9671 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9672
4bb7a0a7
DJ
9673 Return the corresponding abbrev, or NULL if the number is zero (indicating
9674 an empty DIE). In either case *BYTES_READ will be set to the length of
9675 the initial number. */
9676
9677static struct abbrev_info *
685af9cd
TT
9678peek_die_abbrev (const die_reader_specs &reader,
9679 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 9680{
685af9cd 9681 dwarf2_cu *cu = reader.cu;
518817b3 9682 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
9683 unsigned int abbrev_number
9684 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
9685
9686 if (abbrev_number == 0)
9687 return NULL;
9688
685af9cd 9689 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
9690 if (!abbrev)
9691 {
422b9917 9692 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 9693 " at offset %s [in module %s]"),
422b9917 9694 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 9695 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9696 }
9697
9698 return abbrev;
9699}
9700
93311388
DE
9701/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9702 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9703 DIE. Any children of the skipped DIEs will also be skipped. */
9704
d521ce57
TT
9705static const gdb_byte *
9706skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9707{
4bb7a0a7
DJ
9708 while (1)
9709 {
685af9cd
TT
9710 unsigned int bytes_read;
9711 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9712
4bb7a0a7
DJ
9713 if (abbrev == NULL)
9714 return info_ptr + bytes_read;
9715 else
dee91e82 9716 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9717 }
9718}
9719
93311388
DE
9720/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9721 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9722 abbrev corresponding to that skipped uleb128 should be passed in
9723 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9724 children. */
9725
d521ce57
TT
9726static const gdb_byte *
9727skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9728 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9729{
9730 unsigned int bytes_read;
9731 struct attribute attr;
dee91e82
DE
9732 bfd *abfd = reader->abfd;
9733 struct dwarf2_cu *cu = reader->cu;
d521ce57 9734 const gdb_byte *buffer = reader->buffer;
f664829e 9735 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9736 unsigned int form, i;
9737
9738 for (i = 0; i < abbrev->num_attrs; i++)
9739 {
9740 /* The only abbrev we care about is DW_AT_sibling. */
9741 if (abbrev->attrs[i].name == DW_AT_sibling)
9742 {
dee91e82 9743 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9744 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9745 complaint (&symfile_complaints,
9746 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9747 else
b9502d3f 9748 {
9c541725
PA
9749 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9750 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9751
9752 if (sibling_ptr < info_ptr)
9753 complaint (&symfile_complaints,
9754 _("DW_AT_sibling points backwards"));
22869d73
KS
9755 else if (sibling_ptr > reader->buffer_end)
9756 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9757 else
9758 return sibling_ptr;
9759 }
4bb7a0a7
DJ
9760 }
9761
9762 /* If it isn't DW_AT_sibling, skip this attribute. */
9763 form = abbrev->attrs[i].form;
9764 skip_attribute:
9765 switch (form)
9766 {
4bb7a0a7 9767 case DW_FORM_ref_addr:
ae411497
TT
9768 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9769 and later it is offset sized. */
9770 if (cu->header.version == 2)
9771 info_ptr += cu->header.addr_size;
9772 else
9773 info_ptr += cu->header.offset_size;
9774 break;
36586728
TT
9775 case DW_FORM_GNU_ref_alt:
9776 info_ptr += cu->header.offset_size;
9777 break;
ae411497 9778 case DW_FORM_addr:
4bb7a0a7
DJ
9779 info_ptr += cu->header.addr_size;
9780 break;
9781 case DW_FORM_data1:
9782 case DW_FORM_ref1:
9783 case DW_FORM_flag:
9784 info_ptr += 1;
9785 break;
2dc7f7b3 9786 case DW_FORM_flag_present:
43988095 9787 case DW_FORM_implicit_const:
2dc7f7b3 9788 break;
4bb7a0a7
DJ
9789 case DW_FORM_data2:
9790 case DW_FORM_ref2:
9791 info_ptr += 2;
9792 break;
9793 case DW_FORM_data4:
9794 case DW_FORM_ref4:
9795 info_ptr += 4;
9796 break;
9797 case DW_FORM_data8:
9798 case DW_FORM_ref8:
55f1336d 9799 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9800 info_ptr += 8;
9801 break;
0224619f
JK
9802 case DW_FORM_data16:
9803 info_ptr += 16;
9804 break;
4bb7a0a7 9805 case DW_FORM_string:
9b1c24c8 9806 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9807 info_ptr += bytes_read;
9808 break;
2dc7f7b3 9809 case DW_FORM_sec_offset:
4bb7a0a7 9810 case DW_FORM_strp:
36586728 9811 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9812 info_ptr += cu->header.offset_size;
9813 break;
2dc7f7b3 9814 case DW_FORM_exprloc:
4bb7a0a7
DJ
9815 case DW_FORM_block:
9816 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9817 info_ptr += bytes_read;
9818 break;
9819 case DW_FORM_block1:
9820 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9821 break;
9822 case DW_FORM_block2:
9823 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9824 break;
9825 case DW_FORM_block4:
9826 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9827 break;
9828 case DW_FORM_sdata:
9829 case DW_FORM_udata:
9830 case DW_FORM_ref_udata:
3019eac3
DE
9831 case DW_FORM_GNU_addr_index:
9832 case DW_FORM_GNU_str_index:
d521ce57 9833 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9834 break;
9835 case DW_FORM_indirect:
9836 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9837 info_ptr += bytes_read;
9838 /* We need to continue parsing from here, so just go back to
9839 the top. */
9840 goto skip_attribute;
9841
9842 default:
3e43a32a
MS
9843 error (_("Dwarf Error: Cannot handle %s "
9844 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9845 dwarf_form_name (form),
9846 bfd_get_filename (abfd));
9847 }
9848 }
9849
9850 if (abbrev->has_children)
dee91e82 9851 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9852 else
9853 return info_ptr;
9854}
9855
93311388 9856/* Locate ORIG_PDI's sibling.
dee91e82 9857 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9858
d521ce57 9859static const gdb_byte *
dee91e82
DE
9860locate_pdi_sibling (const struct die_reader_specs *reader,
9861 struct partial_die_info *orig_pdi,
d521ce57 9862 const gdb_byte *info_ptr)
91c24f0a
DC
9863{
9864 /* Do we know the sibling already? */
72bf9492 9865
91c24f0a
DC
9866 if (orig_pdi->sibling)
9867 return orig_pdi->sibling;
9868
9869 /* Are there any children to deal with? */
9870
9871 if (!orig_pdi->has_children)
9872 return info_ptr;
9873
4bb7a0a7 9874 /* Skip the children the long way. */
91c24f0a 9875
dee91e82 9876 return skip_children (reader, info_ptr);
91c24f0a
DC
9877}
9878
257e7a09 9879/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9880 not NULL. */
c906108c
SS
9881
9882static void
257e7a09
YQ
9883dwarf2_read_symtab (struct partial_symtab *self,
9884 struct objfile *objfile)
c906108c 9885{
ed2dc618
SM
9886 struct dwarf2_per_objfile *dwarf2_per_objfile
9887 = get_dwarf2_per_objfile (objfile);
9888
257e7a09 9889 if (self->readin)
c906108c 9890 {
442e4d9c 9891 warning (_("bug: psymtab for %s is already read in."),
257e7a09 9892 self->filename);
442e4d9c
YQ
9893 }
9894 else
9895 {
9896 if (info_verbose)
c906108c 9897 {
442e4d9c 9898 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 9899 self->filename);
442e4d9c 9900 gdb_flush (gdb_stdout);
c906108c 9901 }
c906108c 9902
442e4d9c
YQ
9903 /* If this psymtab is constructed from a debug-only objfile, the
9904 has_section_at_zero flag will not necessarily be correct. We
9905 can get the correct value for this flag by looking at the data
9906 associated with the (presumably stripped) associated objfile. */
9907 if (objfile->separate_debug_objfile_backlink)
9908 {
9909 struct dwarf2_per_objfile *dpo_backlink
ed2dc618 9910 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9a619af0 9911
442e4d9c
YQ
9912 dwarf2_per_objfile->has_section_at_zero
9913 = dpo_backlink->has_section_at_zero;
9914 }
b2ab525c 9915
442e4d9c 9916 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 9917
257e7a09 9918 psymtab_to_symtab_1 (self);
c906108c 9919
442e4d9c
YQ
9920 /* Finish up the debug error message. */
9921 if (info_verbose)
9922 printf_filtered (_("done.\n"));
c906108c 9923 }
95554aad 9924
ed2dc618 9925 process_cu_includes (dwarf2_per_objfile);
c906108c 9926}
9cdd5dbd
DE
9927\f
9928/* Reading in full CUs. */
c906108c 9929
10b3939b
DJ
9930/* Add PER_CU to the queue. */
9931
9932static void
95554aad
TT
9933queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9934 enum language pretend_language)
10b3939b
DJ
9935{
9936 struct dwarf2_queue_item *item;
9937
9938 per_cu->queued = 1;
8d749320 9939 item = XNEW (struct dwarf2_queue_item);
10b3939b 9940 item->per_cu = per_cu;
95554aad 9941 item->pretend_language = pretend_language;
10b3939b
DJ
9942 item->next = NULL;
9943
9944 if (dwarf2_queue == NULL)
9945 dwarf2_queue = item;
9946 else
9947 dwarf2_queue_tail->next = item;
9948
9949 dwarf2_queue_tail = item;
9950}
9951
89e63ee4
DE
9952/* If PER_CU is not yet queued, add it to the queue.
9953 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9954 dependency.
0907af0c 9955 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9956 meaning either PER_CU is already queued or it is already loaded.
9957
9958 N.B. There is an invariant here that if a CU is queued then it is loaded.
9959 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9960
9961static int
89e63ee4 9962maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9963 struct dwarf2_per_cu_data *per_cu,
9964 enum language pretend_language)
9965{
9966 /* We may arrive here during partial symbol reading, if we need full
9967 DIEs to process an unusual case (e.g. template arguments). Do
9968 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9969 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9970 {
9971 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9972 return 1;
9973 return 0;
9974 }
9975
9976 /* Mark the dependence relation so that we don't flush PER_CU
9977 too early. */
89e63ee4
DE
9978 if (dependent_cu != NULL)
9979 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9980
9981 /* If it's already on the queue, we have nothing to do. */
9982 if (per_cu->queued)
9983 return 0;
9984
9985 /* If the compilation unit is already loaded, just mark it as
9986 used. */
9987 if (per_cu->cu != NULL)
9988 {
9989 per_cu->cu->last_used = 0;
9990 return 0;
9991 }
9992
9993 /* Add it to the queue. */
9994 queue_comp_unit (per_cu, pretend_language);
9995
9996 return 1;
9997}
9998
10b3939b
DJ
9999/* Process the queue. */
10000
10001static void
ed2dc618 10002process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
10003{
10004 struct dwarf2_queue_item *item, *next_item;
10005
b4f54984 10006 if (dwarf_read_debug)
45cfd468
DE
10007 {
10008 fprintf_unfiltered (gdb_stdlog,
10009 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 10010 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
10011 }
10012
03dd20cc
DJ
10013 /* The queue starts out with one item, but following a DIE reference
10014 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
10015 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
10016 {
cc12ce38
DE
10017 if ((dwarf2_per_objfile->using_index
10018 ? !item->per_cu->v.quick->compunit_symtab
10019 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10020 /* Skip dummy CUs. */
10021 && item->per_cu->cu != NULL)
f4dc4d17
DE
10022 {
10023 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 10024 unsigned int debug_print_threshold;
247f5c4f 10025 char buf[100];
f4dc4d17 10026
247f5c4f 10027 if (per_cu->is_debug_types)
f4dc4d17 10028 {
247f5c4f
DE
10029 struct signatured_type *sig_type =
10030 (struct signatured_type *) per_cu;
10031
9d8780f0 10032 sprintf (buf, "TU %s at offset %s",
73be47f5 10033 hex_string (sig_type->signature),
9d8780f0 10034 sect_offset_str (per_cu->sect_off));
73be47f5
DE
10035 /* There can be 100s of TUs.
10036 Only print them in verbose mode. */
10037 debug_print_threshold = 2;
f4dc4d17 10038 }
247f5c4f 10039 else
73be47f5 10040 {
9d8780f0
SM
10041 sprintf (buf, "CU at offset %s",
10042 sect_offset_str (per_cu->sect_off));
73be47f5
DE
10043 debug_print_threshold = 1;
10044 }
247f5c4f 10045
b4f54984 10046 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 10047 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
10048
10049 if (per_cu->is_debug_types)
10050 process_full_type_unit (per_cu, item->pretend_language);
10051 else
10052 process_full_comp_unit (per_cu, item->pretend_language);
10053
b4f54984 10054 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 10055 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 10056 }
10b3939b
DJ
10057
10058 item->per_cu->queued = 0;
10059 next_item = item->next;
10060 xfree (item);
10061 }
10062
10063 dwarf2_queue_tail = NULL;
45cfd468 10064
b4f54984 10065 if (dwarf_read_debug)
45cfd468
DE
10066 {
10067 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 10068 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 10069 }
10b3939b
DJ
10070}
10071
10b3939b
DJ
10072/* Read in full symbols for PST, and anything it depends on. */
10073
c906108c 10074static void
fba45db2 10075psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 10076{
10b3939b 10077 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
10078 int i;
10079
95554aad
TT
10080 if (pst->readin)
10081 return;
10082
aaa75496 10083 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
10084 if (!pst->dependencies[i]->readin
10085 && pst->dependencies[i]->user == NULL)
aaa75496
JB
10086 {
10087 /* Inform about additional files that need to be read in. */
10088 if (info_verbose)
10089 {
a3f17187 10090 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
10091 fputs_filtered (" ", gdb_stdout);
10092 wrap_here ("");
10093 fputs_filtered ("and ", gdb_stdout);
10094 wrap_here ("");
10095 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 10096 wrap_here (""); /* Flush output. */
aaa75496
JB
10097 gdb_flush (gdb_stdout);
10098 }
10099 psymtab_to_symtab_1 (pst->dependencies[i]);
10100 }
10101
9a3c8263 10102 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
10103
10104 if (per_cu == NULL)
aaa75496
JB
10105 {
10106 /* It's an include file, no symbols to read for it.
10107 Everything is in the parent symtab. */
10108 pst->readin = 1;
10109 return;
10110 }
c906108c 10111
a0f42c21 10112 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
10113}
10114
dee91e82
DE
10115/* Trivial hash function for die_info: the hash value of a DIE
10116 is its offset in .debug_info for this objfile. */
10b3939b 10117
dee91e82
DE
10118static hashval_t
10119die_hash (const void *item)
10b3939b 10120{
9a3c8263 10121 const struct die_info *die = (const struct die_info *) item;
6502dd73 10122
9c541725 10123 return to_underlying (die->sect_off);
dee91e82 10124}
63d06c5c 10125
dee91e82
DE
10126/* Trivial comparison function for die_info structures: two DIEs
10127 are equal if they have the same offset. */
98bfdba5 10128
dee91e82
DE
10129static int
10130die_eq (const void *item_lhs, const void *item_rhs)
10131{
9a3c8263
SM
10132 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
10133 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 10134
9c541725 10135 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 10136}
c906108c 10137
dee91e82
DE
10138/* die_reader_func for load_full_comp_unit.
10139 This is identical to read_signatured_type_reader,
10140 but is kept separate for now. */
c906108c 10141
dee91e82
DE
10142static void
10143load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 10144 const gdb_byte *info_ptr,
dee91e82
DE
10145 struct die_info *comp_unit_die,
10146 int has_children,
10147 void *data)
10148{
10149 struct dwarf2_cu *cu = reader->cu;
9a3c8263 10150 enum language *language_ptr = (enum language *) data;
6caca83c 10151
dee91e82
DE
10152 gdb_assert (cu->die_hash == NULL);
10153 cu->die_hash =
10154 htab_create_alloc_ex (cu->header.length / 12,
10155 die_hash,
10156 die_eq,
10157 NULL,
10158 &cu->comp_unit_obstack,
10159 hashtab_obstack_allocate,
10160 dummy_obstack_deallocate);
e142c38c 10161
dee91e82
DE
10162 if (has_children)
10163 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
10164 &info_ptr, comp_unit_die);
10165 cu->dies = comp_unit_die;
10166 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
10167
10168 /* We try not to read any attributes in this function, because not
9cdd5dbd 10169 all CUs needed for references have been loaded yet, and symbol
10b3939b 10170 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
10171 or we won't be able to build types correctly.
10172 Similarly, if we do not read the producer, we can not apply
10173 producer-specific interpretation. */
95554aad 10174 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 10175}
10b3939b 10176
dee91e82 10177/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 10178
dee91e82 10179static void
95554aad
TT
10180load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
10181 enum language pretend_language)
dee91e82 10182{
3019eac3 10183 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 10184
f4dc4d17
DE
10185 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
10186 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
10187}
10188
3da10d80
KS
10189/* Add a DIE to the delayed physname list. */
10190
10191static void
10192add_to_method_list (struct type *type, int fnfield_index, int index,
10193 const char *name, struct die_info *die,
10194 struct dwarf2_cu *cu)
10195{
10196 struct delayed_method_info mi;
10197 mi.type = type;
10198 mi.fnfield_index = fnfield_index;
10199 mi.index = index;
10200 mi.name = name;
10201 mi.die = die;
c89b44cd 10202 cu->method_list.push_back (mi);
3da10d80
KS
10203}
10204
3693fdb3
PA
10205/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
10206 "const" / "volatile". If so, decrements LEN by the length of the
10207 modifier and return true. Otherwise return false. */
10208
10209template<size_t N>
10210static bool
10211check_modifier (const char *physname, size_t &len, const char (&mod)[N])
10212{
10213 size_t mod_len = sizeof (mod) - 1;
10214 if (len > mod_len && startswith (physname + (len - mod_len), mod))
10215 {
10216 len -= mod_len;
10217 return true;
10218 }
10219 return false;
10220}
10221
3da10d80
KS
10222/* Compute the physnames of any methods on the CU's method list.
10223
10224 The computation of method physnames is delayed in order to avoid the
10225 (bad) condition that one of the method's formal parameters is of an as yet
10226 incomplete type. */
10227
10228static void
10229compute_delayed_physnames (struct dwarf2_cu *cu)
10230{
3693fdb3 10231 /* Only C++ delays computing physnames. */
c89b44cd 10232 if (cu->method_list.empty ())
3693fdb3
PA
10233 return;
10234 gdb_assert (cu->language == language_cplus);
10235
c89b44cd 10236 for (struct delayed_method_info &mi : cu->method_list)
3da10d80 10237 {
1d06ead6 10238 const char *physname;
3da10d80 10239 struct fn_fieldlist *fn_flp
c89b44cd
TT
10240 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
10241 physname = dwarf2_physname (mi.name, mi.die, cu);
10242 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 10243 = physname ? physname : "";
3693fdb3
PA
10244
10245 /* Since there's no tag to indicate whether a method is a
10246 const/volatile overload, extract that information out of the
10247 demangled name. */
10248 if (physname != NULL)
10249 {
10250 size_t len = strlen (physname);
10251
10252 while (1)
10253 {
10254 if (physname[len] == ')') /* shortcut */
10255 break;
10256 else if (check_modifier (physname, len, " const"))
c89b44cd 10257 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 10258 else if (check_modifier (physname, len, " volatile"))
c89b44cd 10259 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
10260 else
10261 break;
10262 }
10263 }
3da10d80 10264 }
c89b44cd
TT
10265
10266 /* The list is no longer needed. */
10267 cu->method_list.clear ();
3da10d80
KS
10268}
10269
a766d390
DE
10270/* Go objects should be embedded in a DW_TAG_module DIE,
10271 and it's not clear if/how imported objects will appear.
10272 To keep Go support simple until that's worked out,
10273 go back through what we've read and create something usable.
10274 We could do this while processing each DIE, and feels kinda cleaner,
10275 but that way is more invasive.
10276 This is to, for example, allow the user to type "p var" or "b main"
10277 without having to specify the package name, and allow lookups
10278 of module.object to work in contexts that use the expression
10279 parser. */
10280
10281static void
10282fixup_go_packaging (struct dwarf2_cu *cu)
10283{
10284 char *package_name = NULL;
10285 struct pending *list;
10286 int i;
10287
10288 for (list = global_symbols; list != NULL; list = list->next)
10289 {
10290 for (i = 0; i < list->nsyms; ++i)
10291 {
10292 struct symbol *sym = list->symbol[i];
10293
10294 if (SYMBOL_LANGUAGE (sym) == language_go
10295 && SYMBOL_CLASS (sym) == LOC_BLOCK)
10296 {
10297 char *this_package_name = go_symbol_package_name (sym);
10298
10299 if (this_package_name == NULL)
10300 continue;
10301 if (package_name == NULL)
10302 package_name = this_package_name;
10303 else
10304 {
518817b3
SM
10305 struct objfile *objfile
10306 = cu->per_cu->dwarf2_per_objfile->objfile;
a766d390
DE
10307 if (strcmp (package_name, this_package_name) != 0)
10308 complaint (&symfile_complaints,
10309 _("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
10310 (symbol_symtab (sym) != NULL
10311 ? symtab_to_filename_for_display
10312 (symbol_symtab (sym))
e3b94546 10313 : objfile_name (objfile)),
a766d390
DE
10314 this_package_name, package_name);
10315 xfree (this_package_name);
10316 }
10317 }
10318 }
10319 }
10320
10321 if (package_name != NULL)
10322 {
518817b3 10323 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 10324 const char *saved_package_name
224c3ddb
SM
10325 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
10326 package_name,
10327 strlen (package_name));
19f392bc
UW
10328 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
10329 saved_package_name);
a766d390
DE
10330 struct symbol *sym;
10331
10332 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10333
e623cf5d 10334 sym = allocate_symbol (objfile);
f85f34ed 10335 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
10336 SYMBOL_SET_NAMES (sym, saved_package_name,
10337 strlen (saved_package_name), 0, objfile);
a766d390
DE
10338 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
10339 e.g., "main" finds the "main" module and not C's main(). */
10340 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 10341 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
10342 SYMBOL_TYPE (sym) = type;
10343
10344 add_symbol_to_list (sym, &global_symbols);
10345
10346 xfree (package_name);
10347 }
10348}
10349
c9317f21
TT
10350/* Allocate a fully-qualified name consisting of the two parts on the
10351 obstack. */
10352
10353static const char *
10354rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
10355{
10356 return obconcat (obstack, p1, "::", p2, (char *) NULL);
10357}
10358
10359/* A helper that allocates a struct discriminant_info to attach to a
10360 union type. */
10361
10362static struct discriminant_info *
10363alloc_discriminant_info (struct type *type, int discriminant_index,
10364 int default_index)
10365{
10366 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10367 gdb_assert (default_index == -1
10368 || (default_index > 0 && default_index < TYPE_NFIELDS (type)));
10369
10370 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
10371
10372 struct discriminant_info *disc
10373 = ((struct discriminant_info *)
10374 TYPE_ZALLOC (type,
10375 offsetof (struct discriminant_info, discriminants)
10376 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
10377 disc->default_index = default_index;
10378 disc->discriminant_index = discriminant_index;
10379
10380 struct dynamic_prop prop;
10381 prop.kind = PROP_UNDEFINED;
10382 prop.data.baton = disc;
10383
10384 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
10385
10386 return disc;
10387}
10388
10389/* Some versions of rustc emitted enums in an unusual way.
10390
10391 Ordinary enums were emitted as unions. The first element of each
10392 structure in the union was named "RUST$ENUM$DISR". This element
10393 held the discriminant.
10394
10395 These versions of Rust also implemented the "non-zero"
10396 optimization. When the enum had two values, and one is empty and
10397 the other holds a pointer that cannot be zero, the pointer is used
10398 as the discriminant, with a zero value meaning the empty variant.
10399 Here, the union's first member is of the form
10400 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
10401 where the fieldnos are the indices of the fields that should be
10402 traversed in order to find the field (which may be several fields deep)
10403 and the variantname is the name of the variant of the case when the
10404 field is zero.
10405
10406 This function recognizes whether TYPE is of one of these forms,
10407 and, if so, smashes it to be a variant type. */
10408
10409static void
10410quirk_rust_enum (struct type *type, struct objfile *objfile)
10411{
10412 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10413
10414 /* We don't need to deal with empty enums. */
10415 if (TYPE_NFIELDS (type) == 0)
10416 return;
10417
10418#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
10419 if (TYPE_NFIELDS (type) == 1
10420 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
10421 {
10422 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
10423
10424 /* Decode the field name to find the offset of the
10425 discriminant. */
10426 ULONGEST bit_offset = 0;
10427 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10428 while (name[0] >= '0' && name[0] <= '9')
10429 {
10430 char *tail;
10431 unsigned long index = strtoul (name, &tail, 10);
10432 name = tail;
10433 if (*name != '$'
10434 || index >= TYPE_NFIELDS (field_type)
10435 || (TYPE_FIELD_LOC_KIND (field_type, index)
10436 != FIELD_LOC_KIND_BITPOS))
10437 {
10438 complaint (&symfile_complaints,
10439 _("Could not parse Rust enum encoding string \"%s\""
10440 "[in module %s]"),
10441 TYPE_FIELD_NAME (type, 0),
10442 objfile_name (objfile));
10443 return;
10444 }
10445 ++name;
10446
10447 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10448 field_type = TYPE_FIELD_TYPE (field_type, index);
10449 }
10450
10451 /* Make a union to hold the variants. */
10452 struct type *union_type = alloc_type (objfile);
10453 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10454 TYPE_NFIELDS (union_type) = 3;
10455 TYPE_FIELDS (union_type)
10456 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10457 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10458
10459 /* Put the discriminant must at index 0. */
10460 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10461 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10462 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10463 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10464
10465 /* The order of fields doesn't really matter, so put the real
10466 field at index 1 and the data-less field at index 2. */
10467 struct discriminant_info *disc
10468 = alloc_discriminant_info (union_type, 0, 1);
10469 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10470 TYPE_FIELD_NAME (union_type, 1)
10471 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10472 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10473 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10474 TYPE_FIELD_NAME (union_type, 1));
10475
10476 const char *dataless_name
10477 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10478 name);
10479 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10480 dataless_name);
10481 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10482 /* NAME points into the original discriminant name, which
10483 already has the correct lifetime. */
10484 TYPE_FIELD_NAME (union_type, 2) = name;
10485 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10486 disc->discriminants[2] = 0;
10487
10488 /* Smash this type to be a structure type. We have to do this
10489 because the type has already been recorded. */
10490 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10491 TYPE_NFIELDS (type) = 1;
10492 TYPE_FIELDS (type)
10493 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10494
10495 /* Install the variant part. */
10496 TYPE_FIELD_TYPE (type, 0) = union_type;
10497 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10498 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10499 }
10500 else if (TYPE_NFIELDS (type) == 1)
10501 {
10502 /* We assume that a union with a single field is a univariant
10503 enum. */
10504 /* Smash this type to be a structure type. We have to do this
10505 because the type has already been recorded. */
10506 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10507
10508 /* Make a union to hold the variants. */
10509 struct type *union_type = alloc_type (objfile);
10510 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10511 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10512 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10513 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10514
10515 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10516 const char *variant_name
10517 = rust_last_path_segment (TYPE_NAME (field_type));
10518 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10519 TYPE_NAME (field_type)
10520 = rust_fully_qualify (&objfile->objfile_obstack,
10521 TYPE_NAME (field_type), variant_name);
10522
10523 /* Install the union in the outer struct type. */
10524 TYPE_NFIELDS (type) = 1;
10525 TYPE_FIELDS (type)
10526 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10527 TYPE_FIELD_TYPE (type, 0) = union_type;
10528 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10529 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10530
10531 alloc_discriminant_info (union_type, -1, 0);
10532 }
10533 else
10534 {
10535 struct type *disr_type = nullptr;
10536 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10537 {
10538 disr_type = TYPE_FIELD_TYPE (type, i);
10539
10540 if (TYPE_NFIELDS (disr_type) == 0)
10541 {
10542 /* Could be data-less variant, so keep going. */
10543 }
10544 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10545 "RUST$ENUM$DISR") != 0)
10546 {
10547 /* Not a Rust enum. */
10548 return;
10549 }
10550 else
10551 {
10552 /* Found one. */
10553 break;
10554 }
10555 }
10556
10557 /* If we got here without a discriminant, then it's probably
10558 just a union. */
10559 if (disr_type == nullptr)
10560 return;
10561
10562 /* Smash this type to be a structure type. We have to do this
10563 because the type has already been recorded. */
10564 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10565
10566 /* Make a union to hold the variants. */
10567 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10568 struct type *union_type = alloc_type (objfile);
10569 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10570 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10571 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10572 TYPE_FIELDS (union_type)
10573 = (struct field *) TYPE_ZALLOC (union_type,
10574 (TYPE_NFIELDS (union_type)
10575 * sizeof (struct field)));
10576
10577 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10578 TYPE_NFIELDS (type) * sizeof (struct field));
10579
10580 /* Install the discriminant at index 0 in the union. */
10581 TYPE_FIELD (union_type, 0) = *disr_field;
10582 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10583 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10584
10585 /* Install the union in the outer struct type. */
10586 TYPE_FIELD_TYPE (type, 0) = union_type;
10587 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10588 TYPE_NFIELDS (type) = 1;
10589
10590 /* Set the size and offset of the union type. */
10591 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10592
10593 /* We need a way to find the correct discriminant given a
10594 variant name. For convenience we build a map here. */
10595 struct type *enum_type = FIELD_TYPE (*disr_field);
10596 std::unordered_map<std::string, ULONGEST> discriminant_map;
10597 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10598 {
10599 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10600 {
10601 const char *name
10602 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10603 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10604 }
10605 }
10606
10607 int n_fields = TYPE_NFIELDS (union_type);
10608 struct discriminant_info *disc
10609 = alloc_discriminant_info (union_type, 0, -1);
10610 /* Skip the discriminant here. */
10611 for (int i = 1; i < n_fields; ++i)
10612 {
10613 /* Find the final word in the name of this variant's type.
10614 That name can be used to look up the correct
10615 discriminant. */
10616 const char *variant_name
10617 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10618 i)));
10619
10620 auto iter = discriminant_map.find (variant_name);
10621 if (iter != discriminant_map.end ())
10622 disc->discriminants[i] = iter->second;
10623
10624 /* Remove the discriminant field. */
10625 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10626 --TYPE_NFIELDS (sub_type);
10627 ++TYPE_FIELDS (sub_type);
10628 TYPE_FIELD_NAME (union_type, i) = variant_name;
10629 TYPE_NAME (sub_type)
10630 = rust_fully_qualify (&objfile->objfile_obstack,
10631 TYPE_NAME (type), variant_name);
10632 }
10633 }
10634}
10635
10636/* Rewrite some Rust unions to be structures with variants parts. */
10637
10638static void
10639rust_union_quirks (struct dwarf2_cu *cu)
10640{
10641 gdb_assert (cu->language == language_rust);
10642 for (struct type *type : cu->rust_unions)
10643 quirk_rust_enum (type, cu->per_cu->dwarf2_per_objfile->objfile);
10644}
10645
95554aad
TT
10646/* Return the symtab for PER_CU. This works properly regardless of
10647 whether we're using the index or psymtabs. */
10648
43f3e411
DE
10649static struct compunit_symtab *
10650get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 10651{
ed2dc618 10652 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
10653 ? per_cu->v.quick->compunit_symtab
10654 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10655}
10656
10657/* A helper function for computing the list of all symbol tables
10658 included by PER_CU. */
10659
10660static void
43f3e411 10661recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 10662 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10663 struct dwarf2_per_cu_data *per_cu,
43f3e411 10664 struct compunit_symtab *immediate_parent)
95554aad
TT
10665{
10666 void **slot;
10667 int ix;
43f3e411 10668 struct compunit_symtab *cust;
95554aad
TT
10669 struct dwarf2_per_cu_data *iter;
10670
10671 slot = htab_find_slot (all_children, per_cu, INSERT);
10672 if (*slot != NULL)
10673 {
10674 /* This inclusion and its children have been processed. */
10675 return;
10676 }
10677
10678 *slot = per_cu;
10679 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10680 cust = get_compunit_symtab (per_cu);
10681 if (cust != NULL)
ec94af83
DE
10682 {
10683 /* If this is a type unit only add its symbol table if we haven't
10684 seen it yet (type unit per_cu's can share symtabs). */
10685 if (per_cu->is_debug_types)
10686 {
43f3e411 10687 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10688 if (*slot == NULL)
10689 {
43f3e411
DE
10690 *slot = cust;
10691 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10692 if (cust->user == NULL)
10693 cust->user = immediate_parent;
ec94af83
DE
10694 }
10695 }
10696 else
f9125b6c 10697 {
43f3e411
DE
10698 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10699 if (cust->user == NULL)
10700 cust->user = immediate_parent;
f9125b6c 10701 }
ec94af83 10702 }
95554aad
TT
10703
10704 for (ix = 0;
796a7ff8 10705 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 10706 ++ix)
ec94af83
DE
10707 {
10708 recursively_compute_inclusions (result, all_children,
43f3e411 10709 all_type_symtabs, iter, cust);
ec94af83 10710 }
95554aad
TT
10711}
10712
43f3e411 10713/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10714 PER_CU. */
10715
10716static void
43f3e411 10717compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10718{
f4dc4d17
DE
10719 gdb_assert (! per_cu->is_debug_types);
10720
796a7ff8 10721 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
10722 {
10723 int ix, len;
ec94af83 10724 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
10725 struct compunit_symtab *compunit_symtab_iter;
10726 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 10727 htab_t all_children, all_type_symtabs;
43f3e411 10728 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10729
10730 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10731 if (cust == NULL)
95554aad
TT
10732 return;
10733
10734 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10735 NULL, xcalloc, xfree);
ec94af83
DE
10736 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10737 NULL, xcalloc, xfree);
95554aad
TT
10738
10739 for (ix = 0;
796a7ff8 10740 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 10741 ix, per_cu_iter);
95554aad 10742 ++ix)
ec94af83
DE
10743 {
10744 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 10745 all_type_symtabs, per_cu_iter,
43f3e411 10746 cust);
ec94af83 10747 }
95554aad 10748
ec94af83 10749 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
10750 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10751 cust->includes
ed2dc618 10752 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 10753 struct compunit_symtab *, len + 1);
95554aad 10754 for (ix = 0;
43f3e411
DE
10755 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10756 compunit_symtab_iter);
95554aad 10757 ++ix)
43f3e411
DE
10758 cust->includes[ix] = compunit_symtab_iter;
10759 cust->includes[len] = NULL;
95554aad 10760
43f3e411 10761 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 10762 htab_delete (all_children);
ec94af83 10763 htab_delete (all_type_symtabs);
95554aad
TT
10764 }
10765}
10766
10767/* Compute the 'includes' field for the symtabs of all the CUs we just
10768 read. */
10769
10770static void
ed2dc618 10771process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad
TT
10772{
10773 int ix;
10774 struct dwarf2_per_cu_data *iter;
10775
10776 for (ix = 0;
10777 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10778 ix, iter);
10779 ++ix)
f4dc4d17
DE
10780 {
10781 if (! iter->is_debug_types)
43f3e411 10782 compute_compunit_symtab_includes (iter);
f4dc4d17 10783 }
95554aad
TT
10784
10785 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10786}
10787
9cdd5dbd 10788/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10789 already been loaded into memory. */
10790
10791static void
95554aad
TT
10792process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10793 enum language pretend_language)
10b3939b 10794{
10b3939b 10795 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10796 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10797 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10798 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10799 CORE_ADDR lowpc, highpc;
43f3e411 10800 struct compunit_symtab *cust;
10b3939b 10801 CORE_ADDR baseaddr;
4359dff1 10802 struct block *static_block;
3e29f34a 10803 CORE_ADDR addr;
10b3939b
DJ
10804
10805 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10806
10b3939b 10807 buildsym_init ();
33c7c59d 10808 scoped_free_pendings free_pending;
c89b44cd
TT
10809
10810 /* Clear the list here in case something was left over. */
10811 cu->method_list.clear ();
10b3939b
DJ
10812
10813 cu->list_in_scope = &file_symbols;
c906108c 10814
95554aad
TT
10815 cu->language = pretend_language;
10816 cu->language_defn = language_def (cu->language);
10817
c906108c 10818 /* Do line number decoding in read_file_scope () */
10b3939b 10819 process_die (cu->dies, cu);
c906108c 10820
a766d390
DE
10821 /* For now fudge the Go package. */
10822 if (cu->language == language_go)
10823 fixup_go_packaging (cu);
10824
3da10d80
KS
10825 /* Now that we have processed all the DIEs in the CU, all the types
10826 should be complete, and it should now be safe to compute all of the
10827 physnames. */
10828 compute_delayed_physnames (cu);
3da10d80 10829
c9317f21
TT
10830 if (cu->language == language_rust)
10831 rust_union_quirks (cu);
10832
fae299cd
DC
10833 /* Some compilers don't define a DW_AT_high_pc attribute for the
10834 compilation unit. If the DW_AT_high_pc is missing, synthesize
10835 it, by scanning the DIE's below the compilation unit. */
10b3939b 10836 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10837
3e29f34a
MR
10838 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10839 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10840
10841 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10842 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10843 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10844 addrmap to help ensure it has an accurate map of pc values belonging to
10845 this comp unit. */
10846 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10847
43f3e411
DE
10848 cust = end_symtab_from_static_block (static_block,
10849 SECT_OFF_TEXT (objfile), 0);
c906108c 10850
43f3e411 10851 if (cust != NULL)
c906108c 10852 {
df15bd07 10853 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10854
8be455d7
JK
10855 /* Set symtab language to language from DW_AT_language. If the
10856 compilation is from a C file generated by language preprocessors, do
10857 not set the language if it was already deduced by start_subfile. */
43f3e411 10858 if (!(cu->language == language_c
40e3ad0e 10859 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10860 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10861
10862 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10863 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10864 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10865 there were bugs in prologue debug info, fixed later in GCC-4.5
10866 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10867
10868 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10869 needed, it would be wrong due to missing DW_AT_producer there.
10870
10871 Still one can confuse GDB by using non-standard GCC compilation
10872 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10873 */
ab260dad 10874 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10875 cust->locations_valid = 1;
e0d00bc7
JK
10876
10877 if (gcc_4_minor >= 5)
43f3e411 10878 cust->epilogue_unwind_valid = 1;
96408a79 10879
43f3e411 10880 cust->call_site_htab = cu->call_site_htab;
c906108c 10881 }
9291a0cd
TT
10882
10883 if (dwarf2_per_objfile->using_index)
43f3e411 10884 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10885 else
10886 {
10887 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10888 pst->compunit_symtab = cust;
9291a0cd
TT
10889 pst->readin = 1;
10890 }
c906108c 10891
95554aad
TT
10892 /* Push it for inclusion processing later. */
10893 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
f4dc4d17 10894}
45cfd468 10895
f4dc4d17
DE
10896/* Generate full symbol information for type unit PER_CU, whose DIEs have
10897 already been loaded into memory. */
10898
10899static void
10900process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10901 enum language pretend_language)
10902{
10903 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10904 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10905 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10906 struct compunit_symtab *cust;
0186c6a7
DE
10907 struct signatured_type *sig_type;
10908
10909 gdb_assert (per_cu->is_debug_types);
10910 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
10911
10912 buildsym_init ();
33c7c59d 10913 scoped_free_pendings free_pending;
c89b44cd
TT
10914
10915 /* Clear the list here in case something was left over. */
10916 cu->method_list.clear ();
f4dc4d17
DE
10917
10918 cu->list_in_scope = &file_symbols;
10919
10920 cu->language = pretend_language;
10921 cu->language_defn = language_def (cu->language);
10922
10923 /* The symbol tables are set up in read_type_unit_scope. */
10924 process_die (cu->dies, cu);
10925
10926 /* For now fudge the Go package. */
10927 if (cu->language == language_go)
10928 fixup_go_packaging (cu);
10929
10930 /* Now that we have processed all the DIEs in the CU, all the types
10931 should be complete, and it should now be safe to compute all of the
10932 physnames. */
10933 compute_delayed_physnames (cu);
f4dc4d17 10934
c9317f21
TT
10935 if (cu->language == language_rust)
10936 rust_union_quirks (cu);
10937
f4dc4d17
DE
10938 /* TUs share symbol tables.
10939 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10940 of it with end_expandable_symtab. Otherwise, complete the addition of
10941 this TU's symbols to the existing symtab. */
43f3e411 10942 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10943 {
43f3e411
DE
10944 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10945 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10946
43f3e411 10947 if (cust != NULL)
f4dc4d17
DE
10948 {
10949 /* Set symtab language to language from DW_AT_language. If the
10950 compilation is from a C file generated by language preprocessors,
10951 do not set the language if it was already deduced by
10952 start_subfile. */
43f3e411
DE
10953 if (!(cu->language == language_c
10954 && COMPUNIT_FILETABS (cust)->language != language_c))
10955 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10956 }
10957 }
10958 else
10959 {
0ab9ce85 10960 augment_type_symtab ();
43f3e411 10961 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10962 }
10963
10964 if (dwarf2_per_objfile->using_index)
43f3e411 10965 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10966 else
10967 {
10968 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10969 pst->compunit_symtab = cust;
f4dc4d17 10970 pst->readin = 1;
45cfd468 10971 }
c906108c
SS
10972}
10973
95554aad
TT
10974/* Process an imported unit DIE. */
10975
10976static void
10977process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10978{
10979 struct attribute *attr;
10980
f4dc4d17
DE
10981 /* For now we don't handle imported units in type units. */
10982 if (cu->per_cu->is_debug_types)
10983 {
10984 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10985 " supported in type units [in module %s]"),
518817b3 10986 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10987 }
10988
95554aad
TT
10989 attr = dwarf2_attr (die, DW_AT_import, cu);
10990 if (attr != NULL)
10991 {
9c541725
PA
10992 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10993 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10994 dwarf2_per_cu_data *per_cu
e3b94546 10995 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10996 cu->per_cu->dwarf2_per_objfile);
95554aad 10997
69d751e3 10998 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
10999 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
11000 load_full_comp_unit (per_cu, cu->language);
11001
796a7ff8 11002 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
11003 per_cu);
11004 }
11005}
11006
4c8aa72d
PA
11007/* RAII object that represents a process_die scope: i.e.,
11008 starts/finishes processing a DIE. */
11009class process_die_scope
adde2bff 11010{
4c8aa72d
PA
11011public:
11012 process_die_scope (die_info *die, dwarf2_cu *cu)
11013 : m_die (die), m_cu (cu)
11014 {
11015 /* We should only be processing DIEs not already in process. */
11016 gdb_assert (!m_die->in_process);
11017 m_die->in_process = true;
11018 }
8c3cb9fa 11019
4c8aa72d
PA
11020 ~process_die_scope ()
11021 {
11022 m_die->in_process = false;
11023
11024 /* If we're done processing the DIE for the CU that owns the line
11025 header, we don't need the line header anymore. */
11026 if (m_cu->line_header_die_owner == m_die)
11027 {
11028 delete m_cu->line_header;
11029 m_cu->line_header = NULL;
11030 m_cu->line_header_die_owner = NULL;
11031 }
11032 }
11033
11034private:
11035 die_info *m_die;
11036 dwarf2_cu *m_cu;
11037};
adde2bff 11038
c906108c
SS
11039/* Process a die and its children. */
11040
11041static void
e7c27a73 11042process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11043{
4c8aa72d 11044 process_die_scope scope (die, cu);
adde2bff 11045
c906108c
SS
11046 switch (die->tag)
11047 {
11048 case DW_TAG_padding:
11049 break;
11050 case DW_TAG_compile_unit:
95554aad 11051 case DW_TAG_partial_unit:
e7c27a73 11052 read_file_scope (die, cu);
c906108c 11053 break;
348e048f
DE
11054 case DW_TAG_type_unit:
11055 read_type_unit_scope (die, cu);
11056 break;
c906108c 11057 case DW_TAG_subprogram:
c906108c 11058 case DW_TAG_inlined_subroutine:
edb3359d 11059 read_func_scope (die, cu);
c906108c
SS
11060 break;
11061 case DW_TAG_lexical_block:
14898363
L
11062 case DW_TAG_try_block:
11063 case DW_TAG_catch_block:
e7c27a73 11064 read_lexical_block_scope (die, cu);
c906108c 11065 break;
216f72a1 11066 case DW_TAG_call_site:
96408a79
SA
11067 case DW_TAG_GNU_call_site:
11068 read_call_site_scope (die, cu);
11069 break;
c906108c 11070 case DW_TAG_class_type:
680b30c7 11071 case DW_TAG_interface_type:
c906108c
SS
11072 case DW_TAG_structure_type:
11073 case DW_TAG_union_type:
134d01f1 11074 process_structure_scope (die, cu);
c906108c
SS
11075 break;
11076 case DW_TAG_enumeration_type:
134d01f1 11077 process_enumeration_scope (die, cu);
c906108c 11078 break;
134d01f1 11079
f792889a
DJ
11080 /* These dies have a type, but processing them does not create
11081 a symbol or recurse to process the children. Therefore we can
11082 read them on-demand through read_type_die. */
c906108c 11083 case DW_TAG_subroutine_type:
72019c9c 11084 case DW_TAG_set_type:
c906108c 11085 case DW_TAG_array_type:
c906108c 11086 case DW_TAG_pointer_type:
c906108c 11087 case DW_TAG_ptr_to_member_type:
c906108c 11088 case DW_TAG_reference_type:
4297a3f0 11089 case DW_TAG_rvalue_reference_type:
c906108c 11090 case DW_TAG_string_type:
c906108c 11091 break;
134d01f1 11092
c906108c 11093 case DW_TAG_base_type:
a02abb62 11094 case DW_TAG_subrange_type:
cb249c71 11095 case DW_TAG_typedef:
134d01f1
DJ
11096 /* Add a typedef symbol for the type definition, if it has a
11097 DW_AT_name. */
f792889a 11098 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 11099 break;
c906108c 11100 case DW_TAG_common_block:
e7c27a73 11101 read_common_block (die, cu);
c906108c
SS
11102 break;
11103 case DW_TAG_common_inclusion:
11104 break;
d9fa45fe 11105 case DW_TAG_namespace:
4d4ec4e5 11106 cu->processing_has_namespace_info = 1;
e7c27a73 11107 read_namespace (die, cu);
d9fa45fe 11108 break;
5d7cb8df 11109 case DW_TAG_module:
4d4ec4e5 11110 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
11111 read_module (die, cu);
11112 break;
d9fa45fe 11113 case DW_TAG_imported_declaration:
74921315
KS
11114 cu->processing_has_namespace_info = 1;
11115 if (read_namespace_alias (die, cu))
11116 break;
11117 /* The declaration is not a global namespace alias: fall through. */
d9fa45fe 11118 case DW_TAG_imported_module:
4d4ec4e5 11119 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
11120 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
11121 || cu->language != language_fortran))
11122 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
11123 dwarf_tag_name (die->tag));
11124 read_import_statement (die, cu);
d9fa45fe 11125 break;
95554aad
TT
11126
11127 case DW_TAG_imported_unit:
11128 process_imported_unit_die (die, cu);
11129 break;
11130
71a3c369
TT
11131 case DW_TAG_variable:
11132 read_variable (die, cu);
11133 break;
11134
c906108c 11135 default:
e7c27a73 11136 new_symbol (die, NULL, cu);
c906108c
SS
11137 break;
11138 }
11139}
ca69b9e6
DE
11140\f
11141/* DWARF name computation. */
c906108c 11142
94af9270
KS
11143/* A helper function for dwarf2_compute_name which determines whether DIE
11144 needs to have the name of the scope prepended to the name listed in the
11145 die. */
11146
11147static int
11148die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
11149{
1c809c68
TT
11150 struct attribute *attr;
11151
94af9270
KS
11152 switch (die->tag)
11153 {
11154 case DW_TAG_namespace:
11155 case DW_TAG_typedef:
11156 case DW_TAG_class_type:
11157 case DW_TAG_interface_type:
11158 case DW_TAG_structure_type:
11159 case DW_TAG_union_type:
11160 case DW_TAG_enumeration_type:
11161 case DW_TAG_enumerator:
11162 case DW_TAG_subprogram:
08a76f8a 11163 case DW_TAG_inlined_subroutine:
94af9270 11164 case DW_TAG_member:
74921315 11165 case DW_TAG_imported_declaration:
94af9270
KS
11166 return 1;
11167
11168 case DW_TAG_variable:
c2b0a229 11169 case DW_TAG_constant:
94af9270
KS
11170 /* We only need to prefix "globally" visible variables. These include
11171 any variable marked with DW_AT_external or any variable that
11172 lives in a namespace. [Variables in anonymous namespaces
11173 require prefixing, but they are not DW_AT_external.] */
11174
11175 if (dwarf2_attr (die, DW_AT_specification, cu))
11176 {
11177 struct dwarf2_cu *spec_cu = cu;
9a619af0 11178
94af9270
KS
11179 return die_needs_namespace (die_specification (die, &spec_cu),
11180 spec_cu);
11181 }
11182
1c809c68 11183 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
11184 if (attr == NULL && die->parent->tag != DW_TAG_namespace
11185 && die->parent->tag != DW_TAG_module)
1c809c68
TT
11186 return 0;
11187 /* A variable in a lexical block of some kind does not need a
11188 namespace, even though in C++ such variables may be external
11189 and have a mangled name. */
11190 if (die->parent->tag == DW_TAG_lexical_block
11191 || die->parent->tag == DW_TAG_try_block
1054b214
TT
11192 || die->parent->tag == DW_TAG_catch_block
11193 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
11194 return 0;
11195 return 1;
94af9270
KS
11196
11197 default:
11198 return 0;
11199 }
11200}
11201
73b9be8b
KS
11202/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
11203 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
11204 defined for the given DIE. */
11205
11206static struct attribute *
11207dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
11208{
11209 struct attribute *attr;
11210
11211 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
11212 if (attr == NULL)
11213 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
11214
11215 return attr;
11216}
11217
11218/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
11219 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
11220 defined for the given DIE. */
11221
11222static const char *
11223dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
11224{
11225 const char *linkage_name;
11226
11227 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
11228 if (linkage_name == NULL)
11229 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
11230
11231 return linkage_name;
11232}
11233
94af9270 11234/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 11235 compute the physname for the object, which include a method's:
9c37b5ae 11236 - formal parameters (C++),
a766d390 11237 - receiver type (Go),
a766d390
DE
11238
11239 The term "physname" is a bit confusing.
11240 For C++, for example, it is the demangled name.
11241 For Go, for example, it's the mangled name.
94af9270 11242
af6b7be1
JB
11243 For Ada, return the DIE's linkage name rather than the fully qualified
11244 name. PHYSNAME is ignored..
11245
94af9270
KS
11246 The result is allocated on the objfile_obstack and canonicalized. */
11247
11248static const char *
15d034d0
TT
11249dwarf2_compute_name (const char *name,
11250 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
11251 int physname)
11252{
518817b3 11253 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 11254
94af9270
KS
11255 if (name == NULL)
11256 name = dwarf2_name (die, cu);
11257
2ee7123e
DE
11258 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
11259 but otherwise compute it by typename_concat inside GDB.
11260 FIXME: Actually this is not really true, or at least not always true.
11261 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
5e2db402 11262 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
11263 will set the demangled name to the result of dwarf2_full_name, and it is
11264 the demangled name that GDB uses if it exists. */
f55ee35c
JK
11265 if (cu->language == language_ada
11266 || (cu->language == language_fortran && physname))
11267 {
11268 /* For Ada unit, we prefer the linkage name over the name, as
11269 the former contains the exported name, which the user expects
11270 to be able to reference. Ideally, we want the user to be able
11271 to reference this entity using either natural or linkage name,
11272 but we haven't started looking at this enhancement yet. */
73b9be8b 11273 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 11274
2ee7123e
DE
11275 if (linkage_name != NULL)
11276 return linkage_name;
f55ee35c
JK
11277 }
11278
94af9270
KS
11279 /* These are the only languages we know how to qualify names in. */
11280 if (name != NULL
9c37b5ae 11281 && (cu->language == language_cplus
c44af4eb
TT
11282 || cu->language == language_fortran || cu->language == language_d
11283 || cu->language == language_rust))
94af9270
KS
11284 {
11285 if (die_needs_namespace (die, cu))
11286 {
0d5cff50 11287 const char *prefix;
34a68019 11288 const char *canonical_name = NULL;
94af9270 11289
d7e74731
PA
11290 string_file buf;
11291
94af9270 11292 prefix = determine_prefix (die, cu);
94af9270
KS
11293 if (*prefix != '\0')
11294 {
f55ee35c
JK
11295 char *prefixed_name = typename_concat (NULL, prefix, name,
11296 physname, cu);
9a619af0 11297
d7e74731 11298 buf.puts (prefixed_name);
94af9270
KS
11299 xfree (prefixed_name);
11300 }
11301 else
d7e74731 11302 buf.puts (name);
94af9270 11303
98bfdba5
PA
11304 /* Template parameters may be specified in the DIE's DW_AT_name, or
11305 as children with DW_TAG_template_type_param or
11306 DW_TAG_value_type_param. If the latter, add them to the name
11307 here. If the name already has template parameters, then
11308 skip this step; some versions of GCC emit both, and
11309 it is more efficient to use the pre-computed name.
11310
11311 Something to keep in mind about this process: it is very
11312 unlikely, or in some cases downright impossible, to produce
11313 something that will match the mangled name of a function.
11314 If the definition of the function has the same debug info,
11315 we should be able to match up with it anyway. But fallbacks
11316 using the minimal symbol, for instance to find a method
11317 implemented in a stripped copy of libstdc++, will not work.
11318 If we do not have debug info for the definition, we will have to
11319 match them up some other way.
11320
11321 When we do name matching there is a related problem with function
11322 templates; two instantiated function templates are allowed to
11323 differ only by their return types, which we do not add here. */
11324
11325 if (cu->language == language_cplus && strchr (name, '<') == NULL)
11326 {
11327 struct attribute *attr;
11328 struct die_info *child;
11329 int first = 1;
11330
11331 die->building_fullname = 1;
11332
11333 for (child = die->child; child != NULL; child = child->sibling)
11334 {
11335 struct type *type;
12df843f 11336 LONGEST value;
d521ce57 11337 const gdb_byte *bytes;
98bfdba5
PA
11338 struct dwarf2_locexpr_baton *baton;
11339 struct value *v;
11340
11341 if (child->tag != DW_TAG_template_type_param
11342 && child->tag != DW_TAG_template_value_param)
11343 continue;
11344
11345 if (first)
11346 {
d7e74731 11347 buf.puts ("<");
98bfdba5
PA
11348 first = 0;
11349 }
11350 else
d7e74731 11351 buf.puts (", ");
98bfdba5
PA
11352
11353 attr = dwarf2_attr (child, DW_AT_type, cu);
11354 if (attr == NULL)
11355 {
11356 complaint (&symfile_complaints,
11357 _("template parameter missing DW_AT_type"));
d7e74731 11358 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
11359 continue;
11360 }
11361 type = die_type (child, cu);
11362
11363 if (child->tag == DW_TAG_template_type_param)
11364 {
d7e74731 11365 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
11366 continue;
11367 }
11368
11369 attr = dwarf2_attr (child, DW_AT_const_value, cu);
11370 if (attr == NULL)
11371 {
11372 complaint (&symfile_complaints,
3e43a32a
MS
11373 _("template parameter missing "
11374 "DW_AT_const_value"));
d7e74731 11375 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
11376 continue;
11377 }
11378
11379 dwarf2_const_value_attr (attr, type, name,
11380 &cu->comp_unit_obstack, cu,
11381 &value, &bytes, &baton);
11382
11383 if (TYPE_NOSIGN (type))
11384 /* GDB prints characters as NUMBER 'CHAR'. If that's
11385 changed, this can use value_print instead. */
d7e74731 11386 c_printchar (value, type, &buf);
98bfdba5
PA
11387 else
11388 {
11389 struct value_print_options opts;
11390
11391 if (baton != NULL)
11392 v = dwarf2_evaluate_loc_desc (type, NULL,
11393 baton->data,
11394 baton->size,
11395 baton->per_cu);
11396 else if (bytes != NULL)
11397 {
11398 v = allocate_value (type);
11399 memcpy (value_contents_writeable (v), bytes,
11400 TYPE_LENGTH (type));
11401 }
11402 else
11403 v = value_from_longest (type, value);
11404
3e43a32a
MS
11405 /* Specify decimal so that we do not depend on
11406 the radix. */
98bfdba5
PA
11407 get_formatted_print_options (&opts, 'd');
11408 opts.raw = 1;
d7e74731 11409 value_print (v, &buf, &opts);
98bfdba5
PA
11410 release_value (v);
11411 value_free (v);
11412 }
11413 }
11414
11415 die->building_fullname = 0;
11416
11417 if (!first)
11418 {
11419 /* Close the argument list, with a space if necessary
11420 (nested templates). */
d7e74731
PA
11421 if (!buf.empty () && buf.string ().back () == '>')
11422 buf.puts (" >");
98bfdba5 11423 else
d7e74731 11424 buf.puts (">");
98bfdba5
PA
11425 }
11426 }
11427
9c37b5ae 11428 /* For C++ methods, append formal parameter type
94af9270 11429 information, if PHYSNAME. */
6e70227d 11430
94af9270 11431 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 11432 && cu->language == language_cplus)
94af9270
KS
11433 {
11434 struct type *type = read_type_die (die, cu);
11435
d7e74731 11436 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 11437 &type_print_raw_options);
94af9270 11438
9c37b5ae 11439 if (cu->language == language_cplus)
94af9270 11440 {
60430eff
DJ
11441 /* Assume that an artificial first parameter is
11442 "this", but do not crash if it is not. RealView
11443 marks unnamed (and thus unused) parameters as
11444 artificial; there is no way to differentiate
11445 the two cases. */
94af9270
KS
11446 if (TYPE_NFIELDS (type) > 0
11447 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 11448 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
11449 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11450 0))))
d7e74731 11451 buf.puts (" const");
94af9270
KS
11452 }
11453 }
11454
d7e74731 11455 const std::string &intermediate_name = buf.string ();
94af9270
KS
11456
11457 if (cu->language == language_cplus)
34a68019 11458 canonical_name
322a8516 11459 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
11460 &objfile->per_bfd->storage_obstack);
11461
11462 /* If we only computed INTERMEDIATE_NAME, or if
11463 INTERMEDIATE_NAME is already canonical, then we need to
11464 copy it to the appropriate obstack. */
322a8516 11465 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
11466 name = ((const char *)
11467 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
11468 intermediate_name.c_str (),
11469 intermediate_name.length ()));
34a68019
TT
11470 else
11471 name = canonical_name;
94af9270
KS
11472 }
11473 }
11474
11475 return name;
11476}
11477
0114d602
DJ
11478/* Return the fully qualified name of DIE, based on its DW_AT_name.
11479 If scope qualifiers are appropriate they will be added. The result
34a68019 11480 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
11481 not have a name. NAME may either be from a previous call to
11482 dwarf2_name or NULL.
11483
9c37b5ae 11484 The output string will be canonicalized (if C++). */
0114d602
DJ
11485
11486static const char *
15d034d0 11487dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 11488{
94af9270
KS
11489 return dwarf2_compute_name (name, die, cu, 0);
11490}
0114d602 11491
94af9270
KS
11492/* Construct a physname for the given DIE in CU. NAME may either be
11493 from a previous call to dwarf2_name or NULL. The result will be
11494 allocated on the objfile_objstack or NULL if the DIE does not have a
11495 name.
0114d602 11496
9c37b5ae 11497 The output string will be canonicalized (if C++). */
0114d602 11498
94af9270 11499static const char *
15d034d0 11500dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 11501{
518817b3 11502 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 11503 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
11504 int need_copy = 1;
11505
11506 /* In this case dwarf2_compute_name is just a shortcut not building anything
11507 on its own. */
11508 if (!die_needs_namespace (die, cu))
11509 return dwarf2_compute_name (name, die, cu, 1);
11510
73b9be8b 11511 mangled = dw2_linkage_name (die, cu);
900e11f9 11512
e98c9e7c
TT
11513 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11514 See https://github.com/rust-lang/rust/issues/32925. */
11515 if (cu->language == language_rust && mangled != NULL
11516 && strchr (mangled, '{') != NULL)
11517 mangled = NULL;
11518
900e11f9
JK
11519 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11520 has computed. */
791afaa2 11521 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 11522 if (mangled != NULL)
900e11f9 11523 {
900e11f9 11524
a766d390
DE
11525 if (cu->language == language_go)
11526 {
5e2db402
TT
11527 /* This is a lie, but we already lie to the caller new_symbol.
11528 new_symbol assumes we return the mangled name.
a766d390 11529 This just undoes that lie until things are cleaned up. */
a766d390
DE
11530 }
11531 else
11532 {
0eb876f5
JB
11533 /* Use DMGL_RET_DROP for C++ template functions to suppress
11534 their return type. It is easier for GDB users to search
11535 for such functions as `name(params)' than `long name(params)'.
11536 In such case the minimal symbol names do not match the full
11537 symbol names but for template functions there is never a need
11538 to look up their definition from their declaration so
11539 the only disadvantage remains the minimal symbol variant
11540 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
11541 demangled.reset (gdb_demangle (mangled,
11542 (DMGL_PARAMS | DMGL_ANSI
11543 | DMGL_RET_DROP)));
a766d390 11544 }
900e11f9 11545 if (demangled)
791afaa2 11546 canon = demangled.get ();
900e11f9
JK
11547 else
11548 {
11549 canon = mangled;
11550 need_copy = 0;
11551 }
11552 }
11553
11554 if (canon == NULL || check_physname)
11555 {
11556 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11557
11558 if (canon != NULL && strcmp (physname, canon) != 0)
11559 {
11560 /* It may not mean a bug in GDB. The compiler could also
11561 compute DW_AT_linkage_name incorrectly. But in such case
11562 GDB would need to be bug-to-bug compatible. */
11563
11564 complaint (&symfile_complaints,
11565 _("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
11566 "(from linkage <%s>) - DIE at %s [in module %s]"),
11567 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 11568 objfile_name (objfile));
900e11f9
JK
11569
11570 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11571 is available here - over computed PHYSNAME. It is safer
11572 against both buggy GDB and buggy compilers. */
11573
11574 retval = canon;
11575 }
11576 else
11577 {
11578 retval = physname;
11579 need_copy = 0;
11580 }
11581 }
11582 else
11583 retval = canon;
11584
11585 if (need_copy)
224c3ddb
SM
11586 retval = ((const char *)
11587 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11588 retval, strlen (retval)));
900e11f9 11589
900e11f9 11590 return retval;
0114d602
DJ
11591}
11592
74921315
KS
11593/* Inspect DIE in CU for a namespace alias. If one exists, record
11594 a new symbol for it.
11595
11596 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11597
11598static int
11599read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11600{
11601 struct attribute *attr;
11602
11603 /* If the die does not have a name, this is not a namespace
11604 alias. */
11605 attr = dwarf2_attr (die, DW_AT_name, cu);
11606 if (attr != NULL)
11607 {
11608 int num;
11609 struct die_info *d = die;
11610 struct dwarf2_cu *imported_cu = cu;
11611
11612 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11613 keep inspecting DIEs until we hit the underlying import. */
11614#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11615 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11616 {
11617 attr = dwarf2_attr (d, DW_AT_import, cu);
11618 if (attr == NULL)
11619 break;
11620
11621 d = follow_die_ref (d, attr, &imported_cu);
11622 if (d->tag != DW_TAG_imported_declaration)
11623 break;
11624 }
11625
11626 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11627 {
11628 complaint (&symfile_complaints,
9d8780f0
SM
11629 _("DIE at %s has too many recursively imported "
11630 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
11631 return 0;
11632 }
11633
11634 if (attr != NULL)
11635 {
11636 struct type *type;
9c541725 11637 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11638
9c541725 11639 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11640 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11641 {
11642 /* This declaration is a global namespace alias. Add
11643 a symbol for it whose type is the aliased namespace. */
11644 new_symbol (die, type, cu);
11645 return 1;
11646 }
11647 }
11648 }
11649
11650 return 0;
11651}
11652
22cee43f
PMR
11653/* Return the using directives repository (global or local?) to use in the
11654 current context for LANGUAGE.
11655
11656 For Ada, imported declarations can materialize renamings, which *may* be
11657 global. However it is impossible (for now?) in DWARF to distinguish
11658 "external" imported declarations and "static" ones. As all imported
11659 declarations seem to be static in all other languages, make them all CU-wide
11660 global only in Ada. */
11661
11662static struct using_direct **
11663using_directives (enum language language)
11664{
11665 if (language == language_ada && context_stack_depth == 0)
11666 return &global_using_directives;
11667 else
11668 return &local_using_directives;
11669}
11670
27aa8d6a
SW
11671/* Read the import statement specified by the given die and record it. */
11672
11673static void
11674read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11675{
518817b3 11676 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 11677 struct attribute *import_attr;
32019081 11678 struct die_info *imported_die, *child_die;
de4affc9 11679 struct dwarf2_cu *imported_cu;
27aa8d6a 11680 const char *imported_name;
794684b6 11681 const char *imported_name_prefix;
13387711
SW
11682 const char *canonical_name;
11683 const char *import_alias;
11684 const char *imported_declaration = NULL;
794684b6 11685 const char *import_prefix;
eb1e02fd 11686 std::vector<const char *> excludes;
13387711 11687
27aa8d6a
SW
11688 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11689 if (import_attr == NULL)
11690 {
11691 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11692 dwarf_tag_name (die->tag));
11693 return;
11694 }
11695
de4affc9
CC
11696 imported_cu = cu;
11697 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11698 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11699 if (imported_name == NULL)
11700 {
11701 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11702
11703 The import in the following code:
11704 namespace A
11705 {
11706 typedef int B;
11707 }
11708
11709 int main ()
11710 {
11711 using A::B;
11712 B b;
11713 return b;
11714 }
11715
11716 ...
11717 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11718 <52> DW_AT_decl_file : 1
11719 <53> DW_AT_decl_line : 6
11720 <54> DW_AT_import : <0x75>
11721 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11722 <59> DW_AT_name : B
11723 <5b> DW_AT_decl_file : 1
11724 <5c> DW_AT_decl_line : 2
11725 <5d> DW_AT_type : <0x6e>
11726 ...
11727 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11728 <76> DW_AT_byte_size : 4
11729 <77> DW_AT_encoding : 5 (signed)
11730
11731 imports the wrong die ( 0x75 instead of 0x58 ).
11732 This case will be ignored until the gcc bug is fixed. */
11733 return;
11734 }
11735
82856980
SW
11736 /* Figure out the local name after import. */
11737 import_alias = dwarf2_name (die, cu);
27aa8d6a 11738
794684b6
SW
11739 /* Figure out where the statement is being imported to. */
11740 import_prefix = determine_prefix (die, cu);
11741
11742 /* Figure out what the scope of the imported die is and prepend it
11743 to the name of the imported die. */
de4affc9 11744 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11745
f55ee35c
JK
11746 if (imported_die->tag != DW_TAG_namespace
11747 && imported_die->tag != DW_TAG_module)
794684b6 11748 {
13387711
SW
11749 imported_declaration = imported_name;
11750 canonical_name = imported_name_prefix;
794684b6 11751 }
13387711 11752 else if (strlen (imported_name_prefix) > 0)
12aaed36 11753 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11754 imported_name_prefix,
11755 (cu->language == language_d ? "." : "::"),
11756 imported_name, (char *) NULL);
13387711
SW
11757 else
11758 canonical_name = imported_name;
794684b6 11759
32019081
JK
11760 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11761 for (child_die = die->child; child_die && child_die->tag;
11762 child_die = sibling_die (child_die))
11763 {
11764 /* DWARF-4: A Fortran use statement with a “rename list” may be
11765 represented by an imported module entry with an import attribute
11766 referring to the module and owned entries corresponding to those
11767 entities that are renamed as part of being imported. */
11768
11769 if (child_die->tag != DW_TAG_imported_declaration)
11770 {
11771 complaint (&symfile_complaints,
11772 _("child DW_TAG_imported_declaration expected "
9d8780f0
SM
11773 "- DIE at %s [in module %s]"),
11774 sect_offset_str (child_die->sect_off),
11775 objfile_name (objfile));
32019081
JK
11776 continue;
11777 }
11778
11779 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11780 if (import_attr == NULL)
11781 {
11782 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11783 dwarf_tag_name (child_die->tag));
11784 continue;
11785 }
11786
11787 imported_cu = cu;
11788 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11789 &imported_cu);
11790 imported_name = dwarf2_name (imported_die, imported_cu);
11791 if (imported_name == NULL)
11792 {
11793 complaint (&symfile_complaints,
11794 _("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11795 "imported name - DIE at %s [in module %s]"),
11796 sect_offset_str (child_die->sect_off),
11797 objfile_name (objfile));
32019081
JK
11798 continue;
11799 }
11800
eb1e02fd 11801 excludes.push_back (imported_name);
32019081
JK
11802
11803 process_die (child_die, cu);
11804 }
11805
22cee43f
PMR
11806 add_using_directive (using_directives (cu->language),
11807 import_prefix,
11808 canonical_name,
11809 import_alias,
11810 imported_declaration,
11811 excludes,
11812 0,
11813 &objfile->objfile_obstack);
27aa8d6a
SW
11814}
11815
5230b05a
WT
11816/* ICC<14 does not output the required DW_AT_declaration on incomplete
11817 types, but gives them a size of zero. Starting with version 14,
11818 ICC is compatible with GCC. */
11819
11820static int
11821producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11822{
11823 if (!cu->checked_producer)
11824 check_producer (cu);
11825
11826 return cu->producer_is_icc_lt_14;
11827}
11828
1b80a9fa
JK
11829/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11830 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11831 this, it was first present in GCC release 4.3.0. */
11832
11833static int
11834producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11835{
11836 if (!cu->checked_producer)
11837 check_producer (cu);
11838
11839 return cu->producer_is_gcc_lt_4_3;
11840}
11841
d721ba37
PA
11842static file_and_directory
11843find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11844{
d721ba37
PA
11845 file_and_directory res;
11846
9291a0cd
TT
11847 /* Find the filename. Do not use dwarf2_name here, since the filename
11848 is not a source language identifier. */
d721ba37
PA
11849 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11850 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11851
d721ba37
PA
11852 if (res.comp_dir == NULL
11853 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11854 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11855 {
d721ba37
PA
11856 res.comp_dir_storage = ldirname (res.name);
11857 if (!res.comp_dir_storage.empty ())
11858 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11859 }
d721ba37 11860 if (res.comp_dir != NULL)
9291a0cd
TT
11861 {
11862 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11863 directory, get rid of it. */
d721ba37 11864 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11865
d721ba37
PA
11866 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11867 res.comp_dir = cp + 1;
9291a0cd
TT
11868 }
11869
d721ba37
PA
11870 if (res.name == NULL)
11871 res.name = "<unknown>";
11872
11873 return res;
9291a0cd
TT
11874}
11875
f4dc4d17
DE
11876/* Handle DW_AT_stmt_list for a compilation unit.
11877 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11878 COMP_DIR is the compilation directory. LOWPC is passed to
11879 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11880
11881static void
11882handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11883 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11884{
518817b3
SM
11885 struct dwarf2_per_objfile *dwarf2_per_objfile
11886 = cu->per_cu->dwarf2_per_objfile;
527f3840 11887 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11888 struct attribute *attr;
527f3840
JK
11889 struct line_header line_header_local;
11890 hashval_t line_header_local_hash;
527f3840
JK
11891 void **slot;
11892 int decode_mapping;
2ab95328 11893
f4dc4d17
DE
11894 gdb_assert (! cu->per_cu->is_debug_types);
11895
2ab95328 11896 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11897 if (attr == NULL)
11898 return;
11899
9c541725 11900 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11901
11902 /* The line header hash table is only created if needed (it exists to
11903 prevent redundant reading of the line table for partial_units).
11904 If we're given a partial_unit, we'll need it. If we're given a
11905 compile_unit, then use the line header hash table if it's already
11906 created, but don't create one just yet. */
11907
11908 if (dwarf2_per_objfile->line_header_hash == NULL
11909 && die->tag == DW_TAG_partial_unit)
2ab95328 11910 {
527f3840
JK
11911 dwarf2_per_objfile->line_header_hash
11912 = htab_create_alloc_ex (127, line_header_hash_voidp,
11913 line_header_eq_voidp,
11914 free_line_header_voidp,
11915 &objfile->objfile_obstack,
11916 hashtab_obstack_allocate,
11917 dummy_obstack_deallocate);
11918 }
2ab95328 11919
9c541725 11920 line_header_local.sect_off = line_offset;
527f3840
JK
11921 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11922 line_header_local_hash = line_header_hash (&line_header_local);
11923 if (dwarf2_per_objfile->line_header_hash != NULL)
11924 {
11925 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11926 &line_header_local,
11927 line_header_local_hash, NO_INSERT);
11928
11929 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11930 is not present in *SLOT (since if there is something in *SLOT then
11931 it will be for a partial_unit). */
11932 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11933 {
527f3840 11934 gdb_assert (*slot != NULL);
9a3c8263 11935 cu->line_header = (struct line_header *) *slot;
527f3840 11936 return;
dee91e82 11937 }
2ab95328 11938 }
527f3840
JK
11939
11940 /* dwarf_decode_line_header does not yet provide sufficient information.
11941 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11942 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11943 if (lh == NULL)
527f3840 11944 return;
4c8aa72d
PA
11945
11946 cu->line_header = lh.release ();
11947 cu->line_header_die_owner = die;
527f3840
JK
11948
11949 if (dwarf2_per_objfile->line_header_hash == NULL)
11950 slot = NULL;
11951 else
11952 {
11953 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11954 &line_header_local,
11955 line_header_local_hash, INSERT);
11956 gdb_assert (slot != NULL);
11957 }
11958 if (slot != NULL && *slot == NULL)
11959 {
11960 /* This newly decoded line number information unit will be owned
11961 by line_header_hash hash table. */
11962 *slot = cu->line_header;
4c8aa72d 11963 cu->line_header_die_owner = NULL;
527f3840
JK
11964 }
11965 else
11966 {
11967 /* We cannot free any current entry in (*slot) as that struct line_header
11968 may be already used by multiple CUs. Create only temporary decoded
11969 line_header for this CU - it may happen at most once for each line
11970 number information unit. And if we're not using line_header_hash
11971 then this is what we want as well. */
11972 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11973 }
11974 decode_mapping = (die->tag != DW_TAG_partial_unit);
11975 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11976 decode_mapping);
fff8551c 11977
2ab95328
TT
11978}
11979
95554aad 11980/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11981
c906108c 11982static void
e7c27a73 11983read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11984{
518817b3
SM
11985 struct dwarf2_per_objfile *dwarf2_per_objfile
11986 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11987 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11988 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11989 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11990 CORE_ADDR highpc = ((CORE_ADDR) 0);
11991 struct attribute *attr;
c906108c 11992 struct die_info *child_die;
e142c38c 11993 CORE_ADDR baseaddr;
6e70227d 11994
e142c38c 11995 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11996
fae299cd 11997 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11998
11999 /* If we didn't find a lowpc, set it to highpc to avoid complaints
12000 from finish_block. */
2acceee2 12001 if (lowpc == ((CORE_ADDR) -1))
c906108c 12002 lowpc = highpc;
3e29f34a 12003 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 12004
d721ba37 12005 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 12006
95554aad 12007 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 12008
f4b8a18d
KW
12009 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
12010 standardised yet. As a workaround for the language detection we fall
12011 back to the DW_AT_producer string. */
12012 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
12013 cu->language = language_opencl;
12014
3019eac3
DE
12015 /* Similar hack for Go. */
12016 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
12017 set_cu_language (DW_LANG_Go, cu);
12018
d721ba37 12019 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
12020
12021 /* Decode line number information if present. We do this before
12022 processing child DIEs, so that the line header table is available
12023 for DW_AT_decl_file. */
d721ba37 12024 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
12025
12026 /* Process all dies in compilation unit. */
12027 if (die->child != NULL)
12028 {
12029 child_die = die->child;
12030 while (child_die && child_die->tag)
12031 {
12032 process_die (child_die, cu);
12033 child_die = sibling_die (child_die);
12034 }
12035 }
12036
12037 /* Decode macro information, if present. Dwarf 2 macro information
12038 refers to information in the line number info statement program
12039 header, so we can only read it if we've read the header
12040 successfully. */
0af92d60
JK
12041 attr = dwarf2_attr (die, DW_AT_macros, cu);
12042 if (attr == NULL)
12043 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
12044 if (attr && cu->line_header)
12045 {
12046 if (dwarf2_attr (die, DW_AT_macro_info, cu))
12047 complaint (&symfile_complaints,
0af92d60 12048 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 12049
43f3e411 12050 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
12051 }
12052 else
12053 {
12054 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
12055 if (attr && cu->line_header)
12056 {
12057 unsigned int macro_offset = DW_UNSND (attr);
12058
43f3e411 12059 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
12060 }
12061 }
3019eac3
DE
12062}
12063
f4dc4d17
DE
12064/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
12065 Create the set of symtabs used by this TU, or if this TU is sharing
12066 symtabs with another TU and the symtabs have already been created
12067 then restore those symtabs in the line header.
12068 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
12069
12070static void
f4dc4d17 12071setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 12072{
f4dc4d17
DE
12073 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
12074 struct type_unit_group *tu_group;
12075 int first_time;
3019eac3 12076 struct attribute *attr;
9c541725 12077 unsigned int i;
0186c6a7 12078 struct signatured_type *sig_type;
3019eac3 12079
f4dc4d17 12080 gdb_assert (per_cu->is_debug_types);
0186c6a7 12081 sig_type = (struct signatured_type *) per_cu;
3019eac3 12082
f4dc4d17 12083 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 12084
f4dc4d17 12085 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 12086 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
12087 if (sig_type->type_unit_group == NULL)
12088 sig_type->type_unit_group = get_type_unit_group (cu, attr);
12089 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
12090
12091 /* If we've already processed this stmt_list there's no real need to
12092 do it again, we could fake it and just recreate the part we need
12093 (file name,index -> symtab mapping). If data shows this optimization
12094 is useful we can do it then. */
43f3e411 12095 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
12096
12097 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
12098 debug info. */
fff8551c 12099 line_header_up lh;
f4dc4d17 12100 if (attr != NULL)
3019eac3 12101 {
9c541725 12102 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
f4dc4d17
DE
12103 lh = dwarf_decode_line_header (line_offset, cu);
12104 }
12105 if (lh == NULL)
12106 {
12107 if (first_time)
12108 dwarf2_start_symtab (cu, "", NULL, 0);
12109 else
12110 {
12111 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 12112 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 12113 }
f4dc4d17 12114 return;
3019eac3
DE
12115 }
12116
4c8aa72d
PA
12117 cu->line_header = lh.release ();
12118 cu->line_header_die_owner = die;
3019eac3 12119
f4dc4d17
DE
12120 if (first_time)
12121 {
43f3e411 12122 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 12123
1fd60fc0
DE
12124 /* Note: We don't assign tu_group->compunit_symtab yet because we're
12125 still initializing it, and our caller (a few levels up)
12126 process_full_type_unit still needs to know if this is the first
12127 time. */
12128
4c8aa72d
PA
12129 tu_group->num_symtabs = cu->line_header->file_names.size ();
12130 tu_group->symtabs = XNEWVEC (struct symtab *,
12131 cu->line_header->file_names.size ());
3019eac3 12132
4c8aa72d 12133 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 12134 {
4c8aa72d 12135 file_entry &fe = cu->line_header->file_names[i];
3019eac3 12136
4c8aa72d 12137 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
3019eac3 12138
f4dc4d17
DE
12139 if (current_subfile->symtab == NULL)
12140 {
4c8aa72d
PA
12141 /* NOTE: start_subfile will recognize when it's been
12142 passed a file it has already seen. So we can't
12143 assume there's a simple mapping from
12144 cu->line_header->file_names to subfiles, plus
12145 cu->line_header->file_names may contain dups. */
43f3e411
DE
12146 current_subfile->symtab
12147 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
12148 }
12149
8c43009f
PA
12150 fe.symtab = current_subfile->symtab;
12151 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
12152 }
12153 }
12154 else
3019eac3 12155 {
0ab9ce85 12156 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 12157
4c8aa72d 12158 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 12159 {
4c8aa72d 12160 file_entry &fe = cu->line_header->file_names[i];
f4dc4d17 12161
4c8aa72d 12162 fe.symtab = tu_group->symtabs[i];
f4dc4d17 12163 }
3019eac3
DE
12164 }
12165
f4dc4d17
DE
12166 /* The main symtab is allocated last. Type units don't have DW_AT_name
12167 so they don't have a "real" (so to speak) symtab anyway.
12168 There is later code that will assign the main symtab to all symbols
12169 that don't have one. We need to handle the case of a symbol with a
12170 missing symtab (DW_AT_decl_file) anyway. */
12171}
3019eac3 12172
f4dc4d17
DE
12173/* Process DW_TAG_type_unit.
12174 For TUs we want to skip the first top level sibling if it's not the
12175 actual type being defined by this TU. In this case the first top
12176 level sibling is there to provide context only. */
3019eac3 12177
f4dc4d17
DE
12178static void
12179read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
12180{
12181 struct die_info *child_die;
3019eac3 12182
f4dc4d17
DE
12183 prepare_one_comp_unit (cu, die, language_minimal);
12184
12185 /* Initialize (or reinitialize) the machinery for building symtabs.
12186 We do this before processing child DIEs, so that the line header table
12187 is available for DW_AT_decl_file. */
12188 setup_type_unit_groups (die, cu);
12189
12190 if (die->child != NULL)
12191 {
12192 child_die = die->child;
12193 while (child_die && child_die->tag)
12194 {
12195 process_die (child_die, cu);
12196 child_die = sibling_die (child_die);
12197 }
12198 }
3019eac3
DE
12199}
12200\f
80626a55
DE
12201/* DWO/DWP files.
12202
12203 http://gcc.gnu.org/wiki/DebugFission
12204 http://gcc.gnu.org/wiki/DebugFissionDWP
12205
12206 To simplify handling of both DWO files ("object" files with the DWARF info)
12207 and DWP files (a file with the DWOs packaged up into one file), we treat
12208 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
12209
12210static hashval_t
12211hash_dwo_file (const void *item)
12212{
9a3c8263 12213 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 12214 hashval_t hash;
3019eac3 12215
a2ce51a0
DE
12216 hash = htab_hash_string (dwo_file->dwo_name);
12217 if (dwo_file->comp_dir != NULL)
12218 hash += htab_hash_string (dwo_file->comp_dir);
12219 return hash;
3019eac3
DE
12220}
12221
12222static int
12223eq_dwo_file (const void *item_lhs, const void *item_rhs)
12224{
9a3c8263
SM
12225 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
12226 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 12227
a2ce51a0
DE
12228 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
12229 return 0;
12230 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
12231 return lhs->comp_dir == rhs->comp_dir;
12232 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
12233}
12234
12235/* Allocate a hash table for DWO files. */
12236
12237static htab_t
ed2dc618 12238allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 12239{
3019eac3
DE
12240 return htab_create_alloc_ex (41,
12241 hash_dwo_file,
12242 eq_dwo_file,
12243 NULL,
12244 &objfile->objfile_obstack,
12245 hashtab_obstack_allocate,
12246 dummy_obstack_deallocate);
12247}
12248
80626a55
DE
12249/* Lookup DWO file DWO_NAME. */
12250
12251static void **
ed2dc618
SM
12252lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
12253 const char *dwo_name,
12254 const char *comp_dir)
80626a55
DE
12255{
12256 struct dwo_file find_entry;
12257 void **slot;
12258
12259 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
12260 dwarf2_per_objfile->dwo_files
12261 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55
DE
12262
12263 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
12264 find_entry.dwo_name = dwo_name;
12265 find_entry.comp_dir = comp_dir;
80626a55
DE
12266 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
12267
12268 return slot;
12269}
12270
3019eac3
DE
12271static hashval_t
12272hash_dwo_unit (const void *item)
12273{
9a3c8263 12274 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
12275
12276 /* This drops the top 32 bits of the id, but is ok for a hash. */
12277 return dwo_unit->signature;
12278}
12279
12280static int
12281eq_dwo_unit (const void *item_lhs, const void *item_rhs)
12282{
9a3c8263
SM
12283 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
12284 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
12285
12286 /* The signature is assumed to be unique within the DWO file.
12287 So while object file CU dwo_id's always have the value zero,
12288 that's OK, assuming each object file DWO file has only one CU,
12289 and that's the rule for now. */
12290 return lhs->signature == rhs->signature;
12291}
12292
12293/* Allocate a hash table for DWO CUs,TUs.
12294 There is one of these tables for each of CUs,TUs for each DWO file. */
12295
12296static htab_t
12297allocate_dwo_unit_table (struct objfile *objfile)
12298{
12299 /* Start out with a pretty small number.
12300 Generally DWO files contain only one CU and maybe some TUs. */
12301 return htab_create_alloc_ex (3,
12302 hash_dwo_unit,
12303 eq_dwo_unit,
12304 NULL,
12305 &objfile->objfile_obstack,
12306 hashtab_obstack_allocate,
12307 dummy_obstack_deallocate);
12308}
12309
80626a55 12310/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 12311
19c3d4c9 12312struct create_dwo_cu_data
3019eac3
DE
12313{
12314 struct dwo_file *dwo_file;
19c3d4c9 12315 struct dwo_unit dwo_unit;
3019eac3
DE
12316};
12317
19c3d4c9 12318/* die_reader_func for create_dwo_cu. */
3019eac3
DE
12319
12320static void
19c3d4c9
DE
12321create_dwo_cu_reader (const struct die_reader_specs *reader,
12322 const gdb_byte *info_ptr,
12323 struct die_info *comp_unit_die,
12324 int has_children,
12325 void *datap)
3019eac3
DE
12326{
12327 struct dwarf2_cu *cu = reader->cu;
9c541725 12328 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 12329 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 12330 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 12331 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 12332 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 12333 struct attribute *attr;
3019eac3
DE
12334
12335 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
12336 if (attr == NULL)
12337 {
19c3d4c9 12338 complaint (&symfile_complaints,
9d8780f0 12339 _("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 12340 " its dwo_id [in module %s]"),
9d8780f0 12341 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
12342 return;
12343 }
12344
3019eac3
DE
12345 dwo_unit->dwo_file = dwo_file;
12346 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 12347 dwo_unit->section = section;
9c541725 12348 dwo_unit->sect_off = sect_off;
3019eac3
DE
12349 dwo_unit->length = cu->per_cu->length;
12350
b4f54984 12351 if (dwarf_read_debug)
9d8780f0
SM
12352 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
12353 sect_offset_str (sect_off),
9c541725 12354 hex_string (dwo_unit->signature));
3019eac3
DE
12355}
12356
33c5cd75 12357/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 12358 Note: This function processes DWO files only, not DWP files. */
3019eac3 12359
33c5cd75 12360static void
ed2dc618
SM
12361create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12362 struct dwo_file &dwo_file, dwarf2_section_info &section,
33c5cd75 12363 htab_t &cus_htab)
3019eac3
DE
12364{
12365 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 12366 const gdb_byte *info_ptr, *end_ptr;
3019eac3 12367
33c5cd75
DB
12368 dwarf2_read_section (objfile, &section);
12369 info_ptr = section.buffer;
3019eac3
DE
12370
12371 if (info_ptr == NULL)
33c5cd75 12372 return;
3019eac3 12373
b4f54984 12374 if (dwarf_read_debug)
19c3d4c9
DE
12375 {
12376 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
12377 get_section_name (&section),
12378 get_section_file_name (&section));
19c3d4c9 12379 }
3019eac3 12380
33c5cd75 12381 end_ptr = info_ptr + section.size;
3019eac3
DE
12382 while (info_ptr < end_ptr)
12383 {
12384 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
12385 struct create_dwo_cu_data create_dwo_cu_data;
12386 struct dwo_unit *dwo_unit;
12387 void **slot;
12388 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 12389
19c3d4c9
DE
12390 memset (&create_dwo_cu_data.dwo_unit, 0,
12391 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3 12392 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 12393 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 12394 per_cu.is_debug_types = 0;
33c5cd75
DB
12395 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12396 per_cu.section = &section;
c5ed0576 12397 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
12398
12399 init_cutu_and_read_dies_no_follow (
12400 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12401 info_ptr += per_cu.length;
12402
12403 // If the unit could not be parsed, skip it.
12404 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12405 continue;
3019eac3 12406
33c5cd75
DB
12407 if (cus_htab == NULL)
12408 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 12409
33c5cd75
DB
12410 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12411 *dwo_unit = create_dwo_cu_data.dwo_unit;
12412 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12413 gdb_assert (slot != NULL);
12414 if (*slot != NULL)
19c3d4c9 12415 {
33c5cd75
DB
12416 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12417 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 12418
33c5cd75 12419 complaint (&symfile_complaints,
9d8780f0
SM
12420 _("debug cu entry at offset %s is duplicate to"
12421 " the entry at offset %s, signature %s"),
12422 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 12423 hex_string (dwo_unit->signature));
19c3d4c9 12424 }
33c5cd75 12425 *slot = (void *)dwo_unit;
3019eac3 12426 }
3019eac3
DE
12427}
12428
80626a55
DE
12429/* DWP file .debug_{cu,tu}_index section format:
12430 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12431
d2415c6c
DE
12432 DWP Version 1:
12433
80626a55
DE
12434 Both index sections have the same format, and serve to map a 64-bit
12435 signature to a set of section numbers. Each section begins with a header,
12436 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12437 indexes, and a pool of 32-bit section numbers. The index sections will be
12438 aligned at 8-byte boundaries in the file.
12439
d2415c6c
DE
12440 The index section header consists of:
12441
12442 V, 32 bit version number
12443 -, 32 bits unused
12444 N, 32 bit number of compilation units or type units in the index
12445 M, 32 bit number of slots in the hash table
80626a55 12446
d2415c6c 12447 Numbers are recorded using the byte order of the application binary.
80626a55 12448
d2415c6c
DE
12449 The hash table begins at offset 16 in the section, and consists of an array
12450 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12451 order of the application binary). Unused slots in the hash table are 0.
12452 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 12453
d2415c6c
DE
12454 The parallel table begins immediately after the hash table
12455 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12456 array of 32-bit indexes (using the byte order of the application binary),
12457 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12458 table contains a 32-bit index into the pool of section numbers. For unused
12459 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 12460
73869dc2
DE
12461 The pool of section numbers begins immediately following the hash table
12462 (at offset 16 + 12 * M from the beginning of the section). The pool of
12463 section numbers consists of an array of 32-bit words (using the byte order
12464 of the application binary). Each item in the array is indexed starting
12465 from 0. The hash table entry provides the index of the first section
12466 number in the set. Additional section numbers in the set follow, and the
12467 set is terminated by a 0 entry (section number 0 is not used in ELF).
12468
12469 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12470 section must be the first entry in the set, and the .debug_abbrev.dwo must
12471 be the second entry. Other members of the set may follow in any order.
12472
12473 ---
12474
12475 DWP Version 2:
12476
12477 DWP Version 2 combines all the .debug_info, etc. sections into one,
12478 and the entries in the index tables are now offsets into these sections.
12479 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12480 section.
12481
12482 Index Section Contents:
12483 Header
12484 Hash Table of Signatures dwp_hash_table.hash_table
12485 Parallel Table of Indices dwp_hash_table.unit_table
12486 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12487 Table of Section Sizes dwp_hash_table.v2.sizes
12488
12489 The index section header consists of:
12490
12491 V, 32 bit version number
12492 L, 32 bit number of columns in the table of section offsets
12493 N, 32 bit number of compilation units or type units in the index
12494 M, 32 bit number of slots in the hash table
12495
12496 Numbers are recorded using the byte order of the application binary.
12497
12498 The hash table has the same format as version 1.
12499 The parallel table of indices has the same format as version 1,
12500 except that the entries are origin-1 indices into the table of sections
12501 offsets and the table of section sizes.
12502
12503 The table of offsets begins immediately following the parallel table
12504 (at offset 16 + 12 * M from the beginning of the section). The table is
12505 a two-dimensional array of 32-bit words (using the byte order of the
12506 application binary), with L columns and N+1 rows, in row-major order.
12507 Each row in the array is indexed starting from 0. The first row provides
12508 a key to the remaining rows: each column in this row provides an identifier
12509 for a debug section, and the offsets in the same column of subsequent rows
12510 refer to that section. The section identifiers are:
12511
12512 DW_SECT_INFO 1 .debug_info.dwo
12513 DW_SECT_TYPES 2 .debug_types.dwo
12514 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12515 DW_SECT_LINE 4 .debug_line.dwo
12516 DW_SECT_LOC 5 .debug_loc.dwo
12517 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12518 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12519 DW_SECT_MACRO 8 .debug_macro.dwo
12520
12521 The offsets provided by the CU and TU index sections are the base offsets
12522 for the contributions made by each CU or TU to the corresponding section
12523 in the package file. Each CU and TU header contains an abbrev_offset
12524 field, used to find the abbreviations table for that CU or TU within the
12525 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12526 be interpreted as relative to the base offset given in the index section.
12527 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12528 should be interpreted as relative to the base offset for .debug_line.dwo,
12529 and offsets into other debug sections obtained from DWARF attributes should
12530 also be interpreted as relative to the corresponding base offset.
12531
12532 The table of sizes begins immediately following the table of offsets.
12533 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12534 with L columns and N rows, in row-major order. Each row in the array is
12535 indexed starting from 1 (row 0 is shared by the two tables).
12536
12537 ---
12538
12539 Hash table lookup is handled the same in version 1 and 2:
12540
12541 We assume that N and M will not exceed 2^32 - 1.
12542 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12543
d2415c6c
DE
12544 Given a 64-bit compilation unit signature or a type signature S, an entry
12545 in the hash table is located as follows:
80626a55 12546
d2415c6c
DE
12547 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12548 the low-order k bits all set to 1.
80626a55 12549
d2415c6c 12550 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12551
d2415c6c
DE
12552 3) If the hash table entry at index H matches the signature, use that
12553 entry. If the hash table entry at index H is unused (all zeroes),
12554 terminate the search: the signature is not present in the table.
80626a55 12555
d2415c6c 12556 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12557
d2415c6c 12558 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12559 to stop at an unused slot or find the match. */
80626a55
DE
12560
12561/* Create a hash table to map DWO IDs to their CU/TU entry in
12562 .debug_{info,types}.dwo in DWP_FILE.
12563 Returns NULL if there isn't one.
12564 Note: This function processes DWP files only, not DWO files. */
12565
12566static struct dwp_hash_table *
ed2dc618
SM
12567create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12568 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
12569{
12570 struct objfile *objfile = dwarf2_per_objfile->objfile;
12571 bfd *dbfd = dwp_file->dbfd;
948f8e3d 12572 const gdb_byte *index_ptr, *index_end;
80626a55 12573 struct dwarf2_section_info *index;
73869dc2 12574 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12575 struct dwp_hash_table *htab;
12576
12577 if (is_debug_types)
12578 index = &dwp_file->sections.tu_index;
12579 else
12580 index = &dwp_file->sections.cu_index;
12581
12582 if (dwarf2_section_empty_p (index))
12583 return NULL;
12584 dwarf2_read_section (objfile, index);
12585
12586 index_ptr = index->buffer;
12587 index_end = index_ptr + index->size;
12588
12589 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12590 index_ptr += 4;
12591 if (version == 2)
12592 nr_columns = read_4_bytes (dbfd, index_ptr);
12593 else
12594 nr_columns = 0;
12595 index_ptr += 4;
80626a55
DE
12596 nr_units = read_4_bytes (dbfd, index_ptr);
12597 index_ptr += 4;
12598 nr_slots = read_4_bytes (dbfd, index_ptr);
12599 index_ptr += 4;
12600
73869dc2 12601 if (version != 1 && version != 2)
80626a55 12602 {
21aa081e 12603 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12604 " [in module %s]"),
21aa081e 12605 pulongest (version), dwp_file->name);
80626a55
DE
12606 }
12607 if (nr_slots != (nr_slots & -nr_slots))
12608 {
21aa081e 12609 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12610 " is not power of 2 [in module %s]"),
21aa081e 12611 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12612 }
12613
12614 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12615 htab->version = version;
12616 htab->nr_columns = nr_columns;
80626a55
DE
12617 htab->nr_units = nr_units;
12618 htab->nr_slots = nr_slots;
12619 htab->hash_table = index_ptr;
12620 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12621
12622 /* Exit early if the table is empty. */
12623 if (nr_slots == 0 || nr_units == 0
12624 || (version == 2 && nr_columns == 0))
12625 {
12626 /* All must be zero. */
12627 if (nr_slots != 0 || nr_units != 0
12628 || (version == 2 && nr_columns != 0))
12629 {
12630 complaint (&symfile_complaints,
12631 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12632 " all zero [in modules %s]"),
12633 dwp_file->name);
12634 }
12635 return htab;
12636 }
12637
12638 if (version == 1)
12639 {
12640 htab->section_pool.v1.indices =
12641 htab->unit_table + sizeof (uint32_t) * nr_slots;
12642 /* It's harder to decide whether the section is too small in v1.
12643 V1 is deprecated anyway so we punt. */
12644 }
12645 else
12646 {
12647 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12648 int *ids = htab->section_pool.v2.section_ids;
12649 /* Reverse map for error checking. */
12650 int ids_seen[DW_SECT_MAX + 1];
12651 int i;
12652
12653 if (nr_columns < 2)
12654 {
12655 error (_("Dwarf Error: bad DWP hash table, too few columns"
12656 " in section table [in module %s]"),
12657 dwp_file->name);
12658 }
12659 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12660 {
12661 error (_("Dwarf Error: bad DWP hash table, too many columns"
12662 " in section table [in module %s]"),
12663 dwp_file->name);
12664 }
12665 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12666 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12667 for (i = 0; i < nr_columns; ++i)
12668 {
12669 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12670
12671 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12672 {
12673 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12674 " in section table [in module %s]"),
12675 id, dwp_file->name);
12676 }
12677 if (ids_seen[id] != -1)
12678 {
12679 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12680 " id %d in section table [in module %s]"),
12681 id, dwp_file->name);
12682 }
12683 ids_seen[id] = i;
12684 ids[i] = id;
12685 }
12686 /* Must have exactly one info or types section. */
12687 if (((ids_seen[DW_SECT_INFO] != -1)
12688 + (ids_seen[DW_SECT_TYPES] != -1))
12689 != 1)
12690 {
12691 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12692 " DWO info/types section [in module %s]"),
12693 dwp_file->name);
12694 }
12695 /* Must have an abbrev section. */
12696 if (ids_seen[DW_SECT_ABBREV] == -1)
12697 {
12698 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12699 " section [in module %s]"),
12700 dwp_file->name);
12701 }
12702 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12703 htab->section_pool.v2.sizes =
12704 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12705 * nr_units * nr_columns);
12706 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12707 * nr_units * nr_columns))
12708 > index_end)
12709 {
12710 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12711 " [in module %s]"),
12712 dwp_file->name);
12713 }
12714 }
80626a55
DE
12715
12716 return htab;
12717}
12718
12719/* Update SECTIONS with the data from SECTP.
12720
12721 This function is like the other "locate" section routines that are
12722 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12723 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12724
12725 The result is non-zero for success, or zero if an error was found. */
12726
12727static int
73869dc2
DE
12728locate_v1_virtual_dwo_sections (asection *sectp,
12729 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12730{
12731 const struct dwop_section_names *names = &dwop_section_names;
12732
12733 if (section_is_p (sectp->name, &names->abbrev_dwo))
12734 {
12735 /* There can be only one. */
049412e3 12736 if (sections->abbrev.s.section != NULL)
80626a55 12737 return 0;
049412e3 12738 sections->abbrev.s.section = sectp;
80626a55
DE
12739 sections->abbrev.size = bfd_get_section_size (sectp);
12740 }
12741 else if (section_is_p (sectp->name, &names->info_dwo)
12742 || section_is_p (sectp->name, &names->types_dwo))
12743 {
12744 /* There can be only one. */
049412e3 12745 if (sections->info_or_types.s.section != NULL)
80626a55 12746 return 0;
049412e3 12747 sections->info_or_types.s.section = sectp;
80626a55
DE
12748 sections->info_or_types.size = bfd_get_section_size (sectp);
12749 }
12750 else if (section_is_p (sectp->name, &names->line_dwo))
12751 {
12752 /* There can be only one. */
049412e3 12753 if (sections->line.s.section != NULL)
80626a55 12754 return 0;
049412e3 12755 sections->line.s.section = sectp;
80626a55
DE
12756 sections->line.size = bfd_get_section_size (sectp);
12757 }
12758 else if (section_is_p (sectp->name, &names->loc_dwo))
12759 {
12760 /* There can be only one. */
049412e3 12761 if (sections->loc.s.section != NULL)
80626a55 12762 return 0;
049412e3 12763 sections->loc.s.section = sectp;
80626a55
DE
12764 sections->loc.size = bfd_get_section_size (sectp);
12765 }
12766 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12767 {
12768 /* There can be only one. */
049412e3 12769 if (sections->macinfo.s.section != NULL)
80626a55 12770 return 0;
049412e3 12771 sections->macinfo.s.section = sectp;
80626a55
DE
12772 sections->macinfo.size = bfd_get_section_size (sectp);
12773 }
12774 else if (section_is_p (sectp->name, &names->macro_dwo))
12775 {
12776 /* There can be only one. */
049412e3 12777 if (sections->macro.s.section != NULL)
80626a55 12778 return 0;
049412e3 12779 sections->macro.s.section = sectp;
80626a55
DE
12780 sections->macro.size = bfd_get_section_size (sectp);
12781 }
12782 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12783 {
12784 /* There can be only one. */
049412e3 12785 if (sections->str_offsets.s.section != NULL)
80626a55 12786 return 0;
049412e3 12787 sections->str_offsets.s.section = sectp;
80626a55
DE
12788 sections->str_offsets.size = bfd_get_section_size (sectp);
12789 }
12790 else
12791 {
12792 /* No other kind of section is valid. */
12793 return 0;
12794 }
12795
12796 return 1;
12797}
12798
73869dc2
DE
12799/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12800 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12801 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12802 This is for DWP version 1 files. */
80626a55
DE
12803
12804static struct dwo_unit *
ed2dc618
SM
12805create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12806 struct dwp_file *dwp_file,
73869dc2
DE
12807 uint32_t unit_index,
12808 const char *comp_dir,
12809 ULONGEST signature, int is_debug_types)
80626a55
DE
12810{
12811 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12812 const struct dwp_hash_table *dwp_htab =
12813 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
12814 bfd *dbfd = dwp_file->dbfd;
12815 const char *kind = is_debug_types ? "TU" : "CU";
12816 struct dwo_file *dwo_file;
12817 struct dwo_unit *dwo_unit;
73869dc2 12818 struct virtual_v1_dwo_sections sections;
80626a55 12819 void **dwo_file_slot;
80626a55
DE
12820 int i;
12821
73869dc2
DE
12822 gdb_assert (dwp_file->version == 1);
12823
b4f54984 12824 if (dwarf_read_debug)
80626a55 12825 {
73869dc2 12826 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12827 kind,
73869dc2 12828 pulongest (unit_index), hex_string (signature),
80626a55
DE
12829 dwp_file->name);
12830 }
12831
19ac8c2e 12832 /* Fetch the sections of this DWO unit.
80626a55
DE
12833 Put a limit on the number of sections we look for so that bad data
12834 doesn't cause us to loop forever. */
12835
73869dc2 12836#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12837 (1 /* .debug_info or .debug_types */ \
12838 + 1 /* .debug_abbrev */ \
12839 + 1 /* .debug_line */ \
12840 + 1 /* .debug_loc */ \
12841 + 1 /* .debug_str_offsets */ \
19ac8c2e 12842 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12843 + 1 /* trailing zero */)
12844
12845 memset (&sections, 0, sizeof (sections));
80626a55 12846
73869dc2 12847 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12848 {
12849 asection *sectp;
12850 uint32_t section_nr =
12851 read_4_bytes (dbfd,
73869dc2
DE
12852 dwp_htab->section_pool.v1.indices
12853 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12854
12855 if (section_nr == 0)
12856 break;
12857 if (section_nr >= dwp_file->num_sections)
12858 {
12859 error (_("Dwarf Error: bad DWP hash table, section number too large"
12860 " [in module %s]"),
12861 dwp_file->name);
12862 }
12863
12864 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12865 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12866 {
12867 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12868 " [in module %s]"),
12869 dwp_file->name);
12870 }
12871 }
12872
12873 if (i < 2
a32a8923
DE
12874 || dwarf2_section_empty_p (&sections.info_or_types)
12875 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12876 {
12877 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12878 " [in module %s]"),
12879 dwp_file->name);
12880 }
73869dc2 12881 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12882 {
12883 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12884 " [in module %s]"),
12885 dwp_file->name);
12886 }
12887
12888 /* It's easier for the rest of the code if we fake a struct dwo_file and
12889 have dwo_unit "live" in that. At least for now.
12890
12891 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12892 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12893 file, we can combine them back into a virtual DWO file to save space
12894 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12895 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12896
791afaa2
TT
12897 std::string virtual_dwo_name =
12898 string_printf ("virtual-dwo/%d-%d-%d-%d",
12899 get_section_id (&sections.abbrev),
12900 get_section_id (&sections.line),
12901 get_section_id (&sections.loc),
12902 get_section_id (&sections.str_offsets));
80626a55 12903 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12904 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12905 virtual_dwo_name.c_str (),
12906 comp_dir);
80626a55
DE
12907 /* Create one if necessary. */
12908 if (*dwo_file_slot == NULL)
12909 {
b4f54984 12910 if (dwarf_read_debug)
80626a55
DE
12911 {
12912 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12913 virtual_dwo_name.c_str ());
80626a55
DE
12914 }
12915 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12916 dwo_file->dwo_name
12917 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12918 virtual_dwo_name.c_str (),
12919 virtual_dwo_name.size ());
0ac5b59e 12920 dwo_file->comp_dir = comp_dir;
80626a55
DE
12921 dwo_file->sections.abbrev = sections.abbrev;
12922 dwo_file->sections.line = sections.line;
12923 dwo_file->sections.loc = sections.loc;
12924 dwo_file->sections.macinfo = sections.macinfo;
12925 dwo_file->sections.macro = sections.macro;
12926 dwo_file->sections.str_offsets = sections.str_offsets;
12927 /* The "str" section is global to the entire DWP file. */
12928 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12929 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12930 there's no need to record it in dwo_file.
12931 Also, we can't simply record type sections in dwo_file because
12932 we record a pointer into the vector in dwo_unit. As we collect more
12933 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12934 for it, invalidating all copies of pointers into the previous
12935 contents. */
80626a55
DE
12936 *dwo_file_slot = dwo_file;
12937 }
12938 else
12939 {
b4f54984 12940 if (dwarf_read_debug)
80626a55
DE
12941 {
12942 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12943 virtual_dwo_name.c_str ());
80626a55 12944 }
9a3c8263 12945 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12946 }
80626a55
DE
12947
12948 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12949 dwo_unit->dwo_file = dwo_file;
12950 dwo_unit->signature = signature;
8d749320
SM
12951 dwo_unit->section =
12952 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12953 *dwo_unit->section = sections.info_or_types;
57d63ce2 12954 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12955
12956 return dwo_unit;
12957}
12958
73869dc2
DE
12959/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12960 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12961 piece within that section used by a TU/CU, return a virtual section
12962 of just that piece. */
12963
12964static struct dwarf2_section_info
ed2dc618
SM
12965create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12966 struct dwarf2_section_info *section,
73869dc2
DE
12967 bfd_size_type offset, bfd_size_type size)
12968{
12969 struct dwarf2_section_info result;
12970 asection *sectp;
12971
12972 gdb_assert (section != NULL);
12973 gdb_assert (!section->is_virtual);
12974
12975 memset (&result, 0, sizeof (result));
12976 result.s.containing_section = section;
12977 result.is_virtual = 1;
12978
12979 if (size == 0)
12980 return result;
12981
12982 sectp = get_section_bfd_section (section);
12983
12984 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12985 bounds of the real section. This is a pretty-rare event, so just
12986 flag an error (easier) instead of a warning and trying to cope. */
12987 if (sectp == NULL
12988 || offset + size > bfd_get_section_size (sectp))
12989 {
73869dc2
DE
12990 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12991 " in section %s [in module %s]"),
12992 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12993 objfile_name (dwarf2_per_objfile->objfile));
12994 }
12995
12996 result.virtual_offset = offset;
12997 result.size = size;
12998 return result;
12999}
13000
13001/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
13002 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
13003 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
13004 This is for DWP version 2 files. */
13005
13006static struct dwo_unit *
ed2dc618
SM
13007create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
13008 struct dwp_file *dwp_file,
73869dc2
DE
13009 uint32_t unit_index,
13010 const char *comp_dir,
13011 ULONGEST signature, int is_debug_types)
13012{
13013 struct objfile *objfile = dwarf2_per_objfile->objfile;
13014 const struct dwp_hash_table *dwp_htab =
13015 is_debug_types ? dwp_file->tus : dwp_file->cus;
13016 bfd *dbfd = dwp_file->dbfd;
13017 const char *kind = is_debug_types ? "TU" : "CU";
13018 struct dwo_file *dwo_file;
13019 struct dwo_unit *dwo_unit;
13020 struct virtual_v2_dwo_sections sections;
13021 void **dwo_file_slot;
73869dc2
DE
13022 int i;
13023
13024 gdb_assert (dwp_file->version == 2);
13025
b4f54984 13026 if (dwarf_read_debug)
73869dc2
DE
13027 {
13028 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
13029 kind,
13030 pulongest (unit_index), hex_string (signature),
13031 dwp_file->name);
13032 }
13033
13034 /* Fetch the section offsets of this DWO unit. */
13035
13036 memset (&sections, 0, sizeof (sections));
73869dc2
DE
13037
13038 for (i = 0; i < dwp_htab->nr_columns; ++i)
13039 {
13040 uint32_t offset = read_4_bytes (dbfd,
13041 dwp_htab->section_pool.v2.offsets
13042 + (((unit_index - 1) * dwp_htab->nr_columns
13043 + i)
13044 * sizeof (uint32_t)));
13045 uint32_t size = read_4_bytes (dbfd,
13046 dwp_htab->section_pool.v2.sizes
13047 + (((unit_index - 1) * dwp_htab->nr_columns
13048 + i)
13049 * sizeof (uint32_t)));
13050
13051 switch (dwp_htab->section_pool.v2.section_ids[i])
13052 {
13053 case DW_SECT_INFO:
13054 case DW_SECT_TYPES:
13055 sections.info_or_types_offset = offset;
13056 sections.info_or_types_size = size;
13057 break;
13058 case DW_SECT_ABBREV:
13059 sections.abbrev_offset = offset;
13060 sections.abbrev_size = size;
13061 break;
13062 case DW_SECT_LINE:
13063 sections.line_offset = offset;
13064 sections.line_size = size;
13065 break;
13066 case DW_SECT_LOC:
13067 sections.loc_offset = offset;
13068 sections.loc_size = size;
13069 break;
13070 case DW_SECT_STR_OFFSETS:
13071 sections.str_offsets_offset = offset;
13072 sections.str_offsets_size = size;
13073 break;
13074 case DW_SECT_MACINFO:
13075 sections.macinfo_offset = offset;
13076 sections.macinfo_size = size;
13077 break;
13078 case DW_SECT_MACRO:
13079 sections.macro_offset = offset;
13080 sections.macro_size = size;
13081 break;
13082 }
13083 }
13084
13085 /* It's easier for the rest of the code if we fake a struct dwo_file and
13086 have dwo_unit "live" in that. At least for now.
13087
13088 The DWP file can be made up of a random collection of CUs and TUs.
13089 However, for each CU + set of TUs that came from the same original DWO
13090 file, we can combine them back into a virtual DWO file to save space
13091 (fewer struct dwo_file objects to allocate). Remember that for really
13092 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
13093
791afaa2
TT
13094 std::string virtual_dwo_name =
13095 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
13096 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
13097 (long) (sections.line_size ? sections.line_offset : 0),
13098 (long) (sections.loc_size ? sections.loc_offset : 0),
13099 (long) (sections.str_offsets_size
13100 ? sections.str_offsets_offset : 0));
73869dc2 13101 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
13102 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13103 virtual_dwo_name.c_str (),
13104 comp_dir);
73869dc2
DE
13105 /* Create one if necessary. */
13106 if (*dwo_file_slot == NULL)
13107 {
b4f54984 13108 if (dwarf_read_debug)
73869dc2
DE
13109 {
13110 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 13111 virtual_dwo_name.c_str ());
73869dc2
DE
13112 }
13113 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
13114 dwo_file->dwo_name
13115 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
13116 virtual_dwo_name.c_str (),
13117 virtual_dwo_name.size ());
73869dc2
DE
13118 dwo_file->comp_dir = comp_dir;
13119 dwo_file->sections.abbrev =
ed2dc618 13120 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
13121 sections.abbrev_offset, sections.abbrev_size);
13122 dwo_file->sections.line =
ed2dc618 13123 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
13124 sections.line_offset, sections.line_size);
13125 dwo_file->sections.loc =
ed2dc618 13126 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
13127 sections.loc_offset, sections.loc_size);
13128 dwo_file->sections.macinfo =
ed2dc618 13129 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
13130 sections.macinfo_offset, sections.macinfo_size);
13131 dwo_file->sections.macro =
ed2dc618 13132 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
13133 sections.macro_offset, sections.macro_size);
13134 dwo_file->sections.str_offsets =
ed2dc618
SM
13135 create_dwp_v2_section (dwarf2_per_objfile,
13136 &dwp_file->sections.str_offsets,
73869dc2
DE
13137 sections.str_offsets_offset,
13138 sections.str_offsets_size);
13139 /* The "str" section is global to the entire DWP file. */
13140 dwo_file->sections.str = dwp_file->sections.str;
13141 /* The info or types section is assigned below to dwo_unit,
13142 there's no need to record it in dwo_file.
13143 Also, we can't simply record type sections in dwo_file because
13144 we record a pointer into the vector in dwo_unit. As we collect more
13145 types we'll grow the vector and eventually have to reallocate space
13146 for it, invalidating all copies of pointers into the previous
13147 contents. */
13148 *dwo_file_slot = dwo_file;
13149 }
13150 else
13151 {
b4f54984 13152 if (dwarf_read_debug)
73869dc2
DE
13153 {
13154 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 13155 virtual_dwo_name.c_str ());
73869dc2 13156 }
9a3c8263 13157 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 13158 }
73869dc2
DE
13159
13160 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
13161 dwo_unit->dwo_file = dwo_file;
13162 dwo_unit->signature = signature;
8d749320
SM
13163 dwo_unit->section =
13164 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
13165 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
13166 is_debug_types
73869dc2
DE
13167 ? &dwp_file->sections.types
13168 : &dwp_file->sections.info,
13169 sections.info_or_types_offset,
13170 sections.info_or_types_size);
13171 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
13172
13173 return dwo_unit;
13174}
13175
57d63ce2
DE
13176/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
13177 Returns NULL if the signature isn't found. */
80626a55
DE
13178
13179static struct dwo_unit *
ed2dc618
SM
13180lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
13181 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 13182 ULONGEST signature, int is_debug_types)
80626a55 13183{
57d63ce2
DE
13184 const struct dwp_hash_table *dwp_htab =
13185 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 13186 bfd *dbfd = dwp_file->dbfd;
57d63ce2 13187 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
13188 uint32_t hash = signature & mask;
13189 uint32_t hash2 = ((signature >> 32) & mask) | 1;
13190 unsigned int i;
13191 void **slot;
870f88f7 13192 struct dwo_unit find_dwo_cu;
80626a55
DE
13193
13194 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
13195 find_dwo_cu.signature = signature;
19ac8c2e
DE
13196 slot = htab_find_slot (is_debug_types
13197 ? dwp_file->loaded_tus
13198 : dwp_file->loaded_cus,
13199 &find_dwo_cu, INSERT);
80626a55
DE
13200
13201 if (*slot != NULL)
9a3c8263 13202 return (struct dwo_unit *) *slot;
80626a55
DE
13203
13204 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 13205 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
13206 {
13207 ULONGEST signature_in_table;
13208
13209 signature_in_table =
57d63ce2 13210 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
13211 if (signature_in_table == signature)
13212 {
57d63ce2
DE
13213 uint32_t unit_index =
13214 read_4_bytes (dbfd,
13215 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 13216
73869dc2
DE
13217 if (dwp_file->version == 1)
13218 {
ed2dc618
SM
13219 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
13220 dwp_file, unit_index,
73869dc2
DE
13221 comp_dir, signature,
13222 is_debug_types);
13223 }
13224 else
13225 {
ed2dc618
SM
13226 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
13227 dwp_file, unit_index,
73869dc2
DE
13228 comp_dir, signature,
13229 is_debug_types);
13230 }
9a3c8263 13231 return (struct dwo_unit *) *slot;
80626a55
DE
13232 }
13233 if (signature_in_table == 0)
13234 return NULL;
13235 hash = (hash + hash2) & mask;
13236 }
13237
13238 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
13239 " [in module %s]"),
13240 dwp_file->name);
13241}
13242
ab5088bf 13243/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
13244 Open the file specified by FILE_NAME and hand it off to BFD for
13245 preliminary analysis. Return a newly initialized bfd *, which
13246 includes a canonicalized copy of FILE_NAME.
80626a55 13247 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
13248 SEARCH_CWD is true if the current directory is to be searched.
13249 It will be searched before debug-file-directory.
13aaf454
DE
13250 If successful, the file is added to the bfd include table of the
13251 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 13252 If unable to find/open the file, return NULL.
3019eac3
DE
13253 NOTE: This function is derived from symfile_bfd_open. */
13254
192b62ce 13255static gdb_bfd_ref_ptr
ed2dc618
SM
13256try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13257 const char *file_name, int is_dwp, int search_cwd)
3019eac3 13258{
24b9144d 13259 int desc;
9c02c129
DE
13260 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
13261 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
13262 to debug_file_directory. */
e0cc99a6 13263 const char *search_path;
9c02c129
DE
13264 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
13265
e0cc99a6 13266 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
13267 if (search_cwd)
13268 {
13269 if (*debug_file_directory != '\0')
e0cc99a6
TT
13270 {
13271 search_path_holder.reset (concat (".", dirname_separator_string,
13272 debug_file_directory,
13273 (char *) NULL));
13274 search_path = search_path_holder.get ();
13275 }
6ac97d4c 13276 else
e0cc99a6 13277 search_path = ".";
6ac97d4c 13278 }
9c02c129 13279 else
e0cc99a6 13280 search_path = debug_file_directory;
3019eac3 13281
24b9144d 13282 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
13283 if (is_dwp)
13284 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
13285
13286 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 13287 desc = openp (search_path, flags, file_name,
3019eac3
DE
13288 O_RDONLY | O_BINARY, &absolute_name);
13289 if (desc < 0)
13290 return NULL;
13291
e0cc99a6
TT
13292 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
13293 gnutarget, desc));
9c02c129
DE
13294 if (sym_bfd == NULL)
13295 return NULL;
192b62ce 13296 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 13297
192b62ce
TT
13298 if (!bfd_check_format (sym_bfd.get (), bfd_object))
13299 return NULL;
3019eac3 13300
13aaf454
DE
13301 /* Success. Record the bfd as having been included by the objfile's bfd.
13302 This is important because things like demangled_names_hash lives in the
13303 objfile's per_bfd space and may have references to things like symbol
13304 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 13305 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 13306
3019eac3
DE
13307 return sym_bfd;
13308}
13309
ab5088bf 13310/* Try to open DWO file FILE_NAME.
3019eac3
DE
13311 COMP_DIR is the DW_AT_comp_dir attribute.
13312 The result is the bfd handle of the file.
13313 If there is a problem finding or opening the file, return NULL.
13314 Upon success, the canonicalized path of the file is stored in the bfd,
13315 same as symfile_bfd_open. */
13316
192b62ce 13317static gdb_bfd_ref_ptr
ed2dc618
SM
13318open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13319 const char *file_name, const char *comp_dir)
3019eac3 13320{
80626a55 13321 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
13322 return try_open_dwop_file (dwarf2_per_objfile, file_name,
13323 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
13324
13325 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
13326
13327 if (comp_dir != NULL)
13328 {
b36cec19
PA
13329 char *path_to_try = concat (comp_dir, SLASH_STRING,
13330 file_name, (char *) NULL);
3019eac3
DE
13331
13332 /* NOTE: If comp_dir is a relative path, this will also try the
13333 search path, which seems useful. */
ed2dc618
SM
13334 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
13335 path_to_try,
13336 0 /*is_dwp*/,
192b62ce 13337 1 /*search_cwd*/));
3019eac3
DE
13338 xfree (path_to_try);
13339 if (abfd != NULL)
13340 return abfd;
13341 }
13342
13343 /* That didn't work, try debug-file-directory, which, despite its name,
13344 is a list of paths. */
13345
13346 if (*debug_file_directory == '\0')
13347 return NULL;
13348
ed2dc618
SM
13349 return try_open_dwop_file (dwarf2_per_objfile, file_name,
13350 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
13351}
13352
80626a55
DE
13353/* This function is mapped across the sections and remembers the offset and
13354 size of each of the DWO debugging sections we are interested in. */
13355
13356static void
13357dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
13358{
9a3c8263 13359 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
13360 const struct dwop_section_names *names = &dwop_section_names;
13361
13362 if (section_is_p (sectp->name, &names->abbrev_dwo))
13363 {
049412e3 13364 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
13365 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
13366 }
13367 else if (section_is_p (sectp->name, &names->info_dwo))
13368 {
049412e3 13369 dwo_sections->info.s.section = sectp;
80626a55
DE
13370 dwo_sections->info.size = bfd_get_section_size (sectp);
13371 }
13372 else if (section_is_p (sectp->name, &names->line_dwo))
13373 {
049412e3 13374 dwo_sections->line.s.section = sectp;
80626a55
DE
13375 dwo_sections->line.size = bfd_get_section_size (sectp);
13376 }
13377 else if (section_is_p (sectp->name, &names->loc_dwo))
13378 {
049412e3 13379 dwo_sections->loc.s.section = sectp;
80626a55
DE
13380 dwo_sections->loc.size = bfd_get_section_size (sectp);
13381 }
13382 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13383 {
049412e3 13384 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
13385 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
13386 }
13387 else if (section_is_p (sectp->name, &names->macro_dwo))
13388 {
049412e3 13389 dwo_sections->macro.s.section = sectp;
80626a55
DE
13390 dwo_sections->macro.size = bfd_get_section_size (sectp);
13391 }
13392 else if (section_is_p (sectp->name, &names->str_dwo))
13393 {
049412e3 13394 dwo_sections->str.s.section = sectp;
80626a55
DE
13395 dwo_sections->str.size = bfd_get_section_size (sectp);
13396 }
13397 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13398 {
049412e3 13399 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
13400 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
13401 }
13402 else if (section_is_p (sectp->name, &names->types_dwo))
13403 {
13404 struct dwarf2_section_info type_section;
13405
13406 memset (&type_section, 0, sizeof (type_section));
049412e3 13407 type_section.s.section = sectp;
80626a55
DE
13408 type_section.size = bfd_get_section_size (sectp);
13409 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
13410 &type_section);
13411 }
13412}
13413
ab5088bf 13414/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 13415 by PER_CU. This is for the non-DWP case.
80626a55 13416 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
13417
13418static struct dwo_file *
0ac5b59e
DE
13419open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13420 const char *dwo_name, const char *comp_dir)
3019eac3 13421{
ed2dc618 13422 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 13423 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 13424 struct dwo_file *dwo_file;
3019eac3
DE
13425 struct cleanup *cleanups;
13426
ed2dc618 13427 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
80626a55
DE
13428 if (dbfd == NULL)
13429 {
b4f54984 13430 if (dwarf_read_debug)
80626a55
DE
13431 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13432 return NULL;
13433 }
13434 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
13435 dwo_file->dwo_name = dwo_name;
13436 dwo_file->comp_dir = comp_dir;
192b62ce 13437 dwo_file->dbfd = dbfd.release ();
3019eac3 13438
ed2dc618
SM
13439 free_dwo_file_cleanup_data *cleanup_data = XNEW (free_dwo_file_cleanup_data);
13440 cleanup_data->dwo_file = dwo_file;
13441 cleanup_data->dwarf2_per_objfile = dwarf2_per_objfile;
13442
13443 cleanups = make_cleanup (free_dwo_file_cleanup, cleanup_data);
3019eac3 13444
192b62ce
TT
13445 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
13446 &dwo_file->sections);
3019eac3 13447
ed2dc618
SM
13448 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13449 dwo_file->cus);
3019eac3 13450
ed2dc618
SM
13451 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file,
13452 dwo_file->sections.types, dwo_file->tus);
3019eac3
DE
13453
13454 discard_cleanups (cleanups);
13455
b4f54984 13456 if (dwarf_read_debug)
80626a55
DE
13457 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13458
3019eac3
DE
13459 return dwo_file;
13460}
13461
80626a55 13462/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
13463 size of each of the DWP debugging sections common to version 1 and 2 that
13464 we are interested in. */
3019eac3 13465
80626a55 13466static void
73869dc2
DE
13467dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13468 void *dwp_file_ptr)
3019eac3 13469{
9a3c8263 13470 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
13471 const struct dwop_section_names *names = &dwop_section_names;
13472 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 13473
80626a55 13474 /* Record the ELF section number for later lookup: this is what the
73869dc2 13475 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
13476 gdb_assert (elf_section_nr < dwp_file->num_sections);
13477 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 13478
80626a55
DE
13479 /* Look for specific sections that we need. */
13480 if (section_is_p (sectp->name, &names->str_dwo))
13481 {
049412e3 13482 dwp_file->sections.str.s.section = sectp;
80626a55
DE
13483 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13484 }
13485 else if (section_is_p (sectp->name, &names->cu_index))
13486 {
049412e3 13487 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
13488 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13489 }
13490 else if (section_is_p (sectp->name, &names->tu_index))
13491 {
049412e3 13492 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
13493 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13494 }
13495}
3019eac3 13496
73869dc2
DE
13497/* This function is mapped across the sections and remembers the offset and
13498 size of each of the DWP version 2 debugging sections that we are interested
13499 in. This is split into a separate function because we don't know if we
13500 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13501
13502static void
13503dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13504{
9a3c8263 13505 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
13506 const struct dwop_section_names *names = &dwop_section_names;
13507 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13508
13509 /* Record the ELF section number for later lookup: this is what the
13510 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13511 gdb_assert (elf_section_nr < dwp_file->num_sections);
13512 dwp_file->elf_sections[elf_section_nr] = sectp;
13513
13514 /* Look for specific sections that we need. */
13515 if (section_is_p (sectp->name, &names->abbrev_dwo))
13516 {
049412e3 13517 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
13518 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13519 }
13520 else if (section_is_p (sectp->name, &names->info_dwo))
13521 {
049412e3 13522 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
13523 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13524 }
13525 else if (section_is_p (sectp->name, &names->line_dwo))
13526 {
049412e3 13527 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
13528 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13529 }
13530 else if (section_is_p (sectp->name, &names->loc_dwo))
13531 {
049412e3 13532 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
13533 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13534 }
13535 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13536 {
049412e3 13537 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
13538 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13539 }
13540 else if (section_is_p (sectp->name, &names->macro_dwo))
13541 {
049412e3 13542 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
13543 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13544 }
13545 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13546 {
049412e3 13547 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
13548 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13549 }
13550 else if (section_is_p (sectp->name, &names->types_dwo))
13551 {
049412e3 13552 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
13553 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13554 }
13555}
13556
80626a55 13557/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13558
80626a55
DE
13559static hashval_t
13560hash_dwp_loaded_cutus (const void *item)
13561{
9a3c8263 13562 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13563
80626a55
DE
13564 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13565 return dwo_unit->signature;
3019eac3
DE
13566}
13567
80626a55 13568/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13569
80626a55
DE
13570static int
13571eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13572{
9a3c8263
SM
13573 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13574 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13575
80626a55
DE
13576 return dua->signature == dub->signature;
13577}
3019eac3 13578
80626a55 13579/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13580
80626a55
DE
13581static htab_t
13582allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13583{
13584 return htab_create_alloc_ex (3,
13585 hash_dwp_loaded_cutus,
13586 eq_dwp_loaded_cutus,
13587 NULL,
13588 &objfile->objfile_obstack,
13589 hashtab_obstack_allocate,
13590 dummy_obstack_deallocate);
13591}
3019eac3 13592
ab5088bf
DE
13593/* Try to open DWP file FILE_NAME.
13594 The result is the bfd handle of the file.
13595 If there is a problem finding or opening the file, return NULL.
13596 Upon success, the canonicalized path of the file is stored in the bfd,
13597 same as symfile_bfd_open. */
13598
192b62ce 13599static gdb_bfd_ref_ptr
ed2dc618
SM
13600open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13601 const char *file_name)
ab5088bf 13602{
ed2dc618
SM
13603 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13604 1 /*is_dwp*/,
192b62ce 13605 1 /*search_cwd*/));
6ac97d4c
DE
13606 if (abfd != NULL)
13607 return abfd;
13608
13609 /* Work around upstream bug 15652.
13610 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13611 [Whether that's a "bug" is debatable, but it is getting in our way.]
13612 We have no real idea where the dwp file is, because gdb's realpath-ing
13613 of the executable's path may have discarded the needed info.
13614 [IWBN if the dwp file name was recorded in the executable, akin to
13615 .gnu_debuglink, but that doesn't exist yet.]
13616 Strip the directory from FILE_NAME and search again. */
13617 if (*debug_file_directory != '\0')
13618 {
13619 /* Don't implicitly search the current directory here.
13620 If the user wants to search "." to handle this case,
13621 it must be added to debug-file-directory. */
ed2dc618
SM
13622 return try_open_dwop_file (dwarf2_per_objfile,
13623 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
13624 0 /*search_cwd*/);
13625 }
13626
13627 return NULL;
ab5088bf
DE
13628}
13629
80626a55
DE
13630/* Initialize the use of the DWP file for the current objfile.
13631 By convention the name of the DWP file is ${objfile}.dwp.
13632 The result is NULL if it can't be found. */
a766d390 13633
80626a55 13634static struct dwp_file *
ed2dc618 13635open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
13636{
13637 struct objfile *objfile = dwarf2_per_objfile->objfile;
13638 struct dwp_file *dwp_file;
80626a55 13639
82bf32bc
JK
13640 /* Try to find first .dwp for the binary file before any symbolic links
13641 resolving. */
6c447423
DE
13642
13643 /* If the objfile is a debug file, find the name of the real binary
13644 file and get the name of dwp file from there. */
d721ba37 13645 std::string dwp_name;
6c447423
DE
13646 if (objfile->separate_debug_objfile_backlink != NULL)
13647 {
13648 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13649 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13650
d721ba37 13651 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13652 }
13653 else
d721ba37
PA
13654 dwp_name = objfile->original_name;
13655
13656 dwp_name += ".dwp";
80626a55 13657
ed2dc618 13658 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13659 if (dbfd == NULL
13660 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13661 {
13662 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13663 dwp_name = objfile_name (objfile);
13664 dwp_name += ".dwp";
ed2dc618 13665 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
13666 }
13667
80626a55
DE
13668 if (dbfd == NULL)
13669 {
b4f54984 13670 if (dwarf_read_debug)
d721ba37 13671 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
80626a55 13672 return NULL;
3019eac3 13673 }
80626a55 13674 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
192b62ce
TT
13675 dwp_file->name = bfd_get_filename (dbfd.get ());
13676 dwp_file->dbfd = dbfd.release ();
c906108c 13677
80626a55 13678 /* +1: section 0 is unused */
192b62ce 13679 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
13680 dwp_file->elf_sections =
13681 OBSTACK_CALLOC (&objfile->objfile_obstack,
13682 dwp_file->num_sections, asection *);
13683
192b62ce
TT
13684 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13685 dwp_file);
80626a55 13686
ed2dc618 13687 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 0);
80626a55 13688
ed2dc618 13689 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 1);
80626a55 13690
73869dc2 13691 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13692 if (dwp_file->cus && dwp_file->tus
13693 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13694 {
13695 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13696 pretty bizarre. We use pulongest here because that's the established
4d65956b 13697 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13698 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13699 " TU version %s [in DWP file %s]"),
13700 pulongest (dwp_file->cus->version),
d721ba37 13701 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13702 }
08302ed2
DE
13703
13704 if (dwp_file->cus)
13705 dwp_file->version = dwp_file->cus->version;
13706 else if (dwp_file->tus)
13707 dwp_file->version = dwp_file->tus->version;
13708 else
13709 dwp_file->version = 2;
73869dc2
DE
13710
13711 if (dwp_file->version == 2)
192b62ce
TT
13712 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13713 dwp_file);
73869dc2 13714
19ac8c2e
DE
13715 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13716 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13717
b4f54984 13718 if (dwarf_read_debug)
80626a55
DE
13719 {
13720 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13721 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13722 " %s CUs, %s TUs\n",
13723 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13724 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13725 }
13726
13727 return dwp_file;
3019eac3 13728}
c906108c 13729
ab5088bf
DE
13730/* Wrapper around open_and_init_dwp_file, only open it once. */
13731
13732static struct dwp_file *
ed2dc618 13733get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
13734{
13735 if (! dwarf2_per_objfile->dwp_checked)
13736 {
ed2dc618
SM
13737 dwarf2_per_objfile->dwp_file
13738 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
13739 dwarf2_per_objfile->dwp_checked = 1;
13740 }
13741 return dwarf2_per_objfile->dwp_file;
13742}
13743
80626a55
DE
13744/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13745 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13746 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13747 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13748 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13749
13750 This is called, for example, when wanting to read a variable with a
13751 complex location. Therefore we don't want to do file i/o for every call.
13752 Therefore we don't want to look for a DWO file on every call.
13753 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13754 then we check if we've already seen DWO_NAME, and only THEN do we check
13755 for a DWO file.
13756
1c658ad5 13757 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13758 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13759
3019eac3 13760static struct dwo_unit *
80626a55
DE
13761lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13762 const char *dwo_name, const char *comp_dir,
13763 ULONGEST signature, int is_debug_types)
3019eac3 13764{
ed2dc618 13765 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 13766 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13767 const char *kind = is_debug_types ? "TU" : "CU";
13768 void **dwo_file_slot;
3019eac3 13769 struct dwo_file *dwo_file;
80626a55 13770 struct dwp_file *dwp_file;
cb1df416 13771
6a506a2d
DE
13772 /* First see if there's a DWP file.
13773 If we have a DWP file but didn't find the DWO inside it, don't
13774 look for the original DWO file. It makes gdb behave differently
13775 depending on whether one is debugging in the build tree. */
cf2c3c16 13776
ed2dc618 13777 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 13778 if (dwp_file != NULL)
cf2c3c16 13779 {
80626a55
DE
13780 const struct dwp_hash_table *dwp_htab =
13781 is_debug_types ? dwp_file->tus : dwp_file->cus;
13782
13783 if (dwp_htab != NULL)
13784 {
13785 struct dwo_unit *dwo_cutu =
ed2dc618 13786 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 13787 signature, is_debug_types);
80626a55
DE
13788
13789 if (dwo_cutu != NULL)
13790 {
b4f54984 13791 if (dwarf_read_debug)
80626a55
DE
13792 {
13793 fprintf_unfiltered (gdb_stdlog,
13794 "Virtual DWO %s %s found: @%s\n",
13795 kind, hex_string (signature),
13796 host_address_to_string (dwo_cutu));
13797 }
13798 return dwo_cutu;
13799 }
13800 }
13801 }
6a506a2d 13802 else
80626a55 13803 {
6a506a2d 13804 /* No DWP file, look for the DWO file. */
80626a55 13805
ed2dc618
SM
13806 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13807 dwo_name, comp_dir);
6a506a2d 13808 if (*dwo_file_slot == NULL)
80626a55 13809 {
6a506a2d
DE
13810 /* Read in the file and build a table of the CUs/TUs it contains. */
13811 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13812 }
6a506a2d 13813 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13814 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13815
6a506a2d 13816 if (dwo_file != NULL)
19c3d4c9 13817 {
6a506a2d
DE
13818 struct dwo_unit *dwo_cutu = NULL;
13819
13820 if (is_debug_types && dwo_file->tus)
13821 {
13822 struct dwo_unit find_dwo_cutu;
13823
13824 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13825 find_dwo_cutu.signature = signature;
9a3c8263
SM
13826 dwo_cutu
13827 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13828 }
33c5cd75 13829 else if (!is_debug_types && dwo_file->cus)
80626a55 13830 {
33c5cd75
DB
13831 struct dwo_unit find_dwo_cutu;
13832
13833 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13834 find_dwo_cutu.signature = signature;
13835 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13836 &find_dwo_cutu);
6a506a2d
DE
13837 }
13838
13839 if (dwo_cutu != NULL)
13840 {
b4f54984 13841 if (dwarf_read_debug)
6a506a2d
DE
13842 {
13843 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13844 kind, dwo_name, hex_string (signature),
13845 host_address_to_string (dwo_cutu));
13846 }
13847 return dwo_cutu;
80626a55
DE
13848 }
13849 }
2e276125 13850 }
9cdd5dbd 13851
80626a55
DE
13852 /* We didn't find it. This could mean a dwo_id mismatch, or
13853 someone deleted the DWO/DWP file, or the search path isn't set up
13854 correctly to find the file. */
13855
b4f54984 13856 if (dwarf_read_debug)
80626a55
DE
13857 {
13858 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13859 kind, dwo_name, hex_string (signature));
13860 }
3019eac3 13861
6656a72d
DE
13862 /* This is a warning and not a complaint because it can be caused by
13863 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13864 {
13865 /* Print the name of the DWP file if we looked there, helps the user
13866 better diagnose the problem. */
791afaa2 13867 std::string dwp_text;
43942612
DE
13868
13869 if (dwp_file != NULL)
791afaa2
TT
13870 dwp_text = string_printf (" [in DWP file %s]",
13871 lbasename (dwp_file->name));
43942612 13872
9d8780f0 13873 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
13874 " [in module %s]"),
13875 kind, dwo_name, hex_string (signature),
791afaa2 13876 dwp_text.c_str (),
43942612 13877 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 13878 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 13879 }
3019eac3 13880 return NULL;
5fb290d7
DJ
13881}
13882
80626a55
DE
13883/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13884 See lookup_dwo_cutu_unit for details. */
13885
13886static struct dwo_unit *
13887lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13888 const char *dwo_name, const char *comp_dir,
13889 ULONGEST signature)
13890{
13891 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13892}
13893
13894/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13895 See lookup_dwo_cutu_unit for details. */
13896
13897static struct dwo_unit *
13898lookup_dwo_type_unit (struct signatured_type *this_tu,
13899 const char *dwo_name, const char *comp_dir)
13900{
13901 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13902}
13903
89e63ee4
DE
13904/* Traversal function for queue_and_load_all_dwo_tus. */
13905
13906static int
13907queue_and_load_dwo_tu (void **slot, void *info)
13908{
13909 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13910 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13911 ULONGEST signature = dwo_unit->signature;
13912 struct signatured_type *sig_type =
13913 lookup_dwo_signatured_type (per_cu->cu, signature);
13914
13915 if (sig_type != NULL)
13916 {
13917 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13918
13919 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13920 a real dependency of PER_CU on SIG_TYPE. That is detected later
13921 while processing PER_CU. */
13922 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13923 load_full_type_unit (sig_cu);
13924 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13925 }
13926
13927 return 1;
13928}
13929
13930/* Queue all TUs contained in the DWO of PER_CU to be read in.
13931 The DWO may have the only definition of the type, though it may not be
13932 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13933 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13934
13935static void
13936queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13937{
13938 struct dwo_unit *dwo_unit;
13939 struct dwo_file *dwo_file;
13940
13941 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13942 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13943 gdb_assert (per_cu->cu != NULL);
13944
13945 dwo_unit = per_cu->cu->dwo_unit;
13946 gdb_assert (dwo_unit != NULL);
13947
13948 dwo_file = dwo_unit->dwo_file;
13949 if (dwo_file->tus != NULL)
13950 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13951}
13952
3019eac3
DE
13953/* Free all resources associated with DWO_FILE.
13954 Close the DWO file and munmap the sections.
13955 All memory should be on the objfile obstack. */
348e048f
DE
13956
13957static void
3019eac3 13958free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 13959{
348e048f 13960
5c6fa7ab 13961 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 13962 gdb_bfd_unref (dwo_file->dbfd);
348e048f 13963
3019eac3
DE
13964 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13965}
348e048f 13966
3019eac3 13967/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 13968
3019eac3
DE
13969static void
13970free_dwo_file_cleanup (void *arg)
13971{
ed2dc618
SM
13972 struct free_dwo_file_cleanup_data *data
13973 = (struct free_dwo_file_cleanup_data *) arg;
13974 struct objfile *objfile = data->dwarf2_per_objfile->objfile;
348e048f 13975
ed2dc618
SM
13976 free_dwo_file (data->dwo_file, objfile);
13977
13978 xfree (data);
3019eac3 13979}
348e048f 13980
3019eac3 13981/* Traversal function for free_dwo_files. */
2ab95328 13982
3019eac3
DE
13983static int
13984free_dwo_file_from_slot (void **slot, void *info)
13985{
13986 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13987 struct objfile *objfile = (struct objfile *) info;
348e048f 13988
3019eac3 13989 free_dwo_file (dwo_file, objfile);
348e048f 13990
3019eac3
DE
13991 return 1;
13992}
348e048f 13993
3019eac3 13994/* Free all resources associated with DWO_FILES. */
348e048f 13995
3019eac3
DE
13996static void
13997free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13998{
13999 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 14000}
3019eac3
DE
14001\f
14002/* Read in various DIEs. */
348e048f 14003
d389af10 14004/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
14005 Inherit only the children of the DW_AT_abstract_origin DIE not being
14006 already referenced by DW_AT_abstract_origin from the children of the
14007 current DIE. */
d389af10
JK
14008
14009static void
14010inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
14011{
14012 struct die_info *child_die;
791afaa2 14013 sect_offset *offsetp;
d389af10
JK
14014 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
14015 struct die_info *origin_die;
14016 /* Iterator of the ORIGIN_DIE children. */
14017 struct die_info *origin_child_die;
d389af10 14018 struct attribute *attr;
cd02d79d
PA
14019 struct dwarf2_cu *origin_cu;
14020 struct pending **origin_previous_list_in_scope;
d389af10
JK
14021
14022 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14023 if (!attr)
14024 return;
14025
cd02d79d
PA
14026 /* Note that following die references may follow to a die in a
14027 different cu. */
14028
14029 origin_cu = cu;
14030 origin_die = follow_die_ref (die, attr, &origin_cu);
14031
14032 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
14033 symbols in. */
14034 origin_previous_list_in_scope = origin_cu->list_in_scope;
14035 origin_cu->list_in_scope = cu->list_in_scope;
14036
edb3359d
DJ
14037 if (die->tag != origin_die->tag
14038 && !(die->tag == DW_TAG_inlined_subroutine
14039 && origin_die->tag == DW_TAG_subprogram))
d389af10 14040 complaint (&symfile_complaints,
9d8780f0
SM
14041 _("DIE %s and its abstract origin %s have different tags"),
14042 sect_offset_str (die->sect_off),
14043 sect_offset_str (origin_die->sect_off));
d389af10 14044
791afaa2 14045 std::vector<sect_offset> offsets;
d389af10 14046
3ea89b92
PMR
14047 for (child_die = die->child;
14048 child_die && child_die->tag;
14049 child_die = sibling_die (child_die))
14050 {
14051 struct die_info *child_origin_die;
14052 struct dwarf2_cu *child_origin_cu;
14053
14054 /* We are trying to process concrete instance entries:
216f72a1 14055 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
14056 it's not relevant to our analysis here. i.e. detecting DIEs that are
14057 present in the abstract instance but not referenced in the concrete
14058 one. */
216f72a1
JK
14059 if (child_die->tag == DW_TAG_call_site
14060 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
14061 continue;
14062
c38f313d
DJ
14063 /* For each CHILD_DIE, find the corresponding child of
14064 ORIGIN_DIE. If there is more than one layer of
14065 DW_AT_abstract_origin, follow them all; there shouldn't be,
14066 but GCC versions at least through 4.4 generate this (GCC PR
14067 40573). */
3ea89b92
PMR
14068 child_origin_die = child_die;
14069 child_origin_cu = cu;
c38f313d
DJ
14070 while (1)
14071 {
cd02d79d
PA
14072 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
14073 child_origin_cu);
c38f313d
DJ
14074 if (attr == NULL)
14075 break;
cd02d79d
PA
14076 child_origin_die = follow_die_ref (child_origin_die, attr,
14077 &child_origin_cu);
c38f313d
DJ
14078 }
14079
d389af10
JK
14080 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
14081 counterpart may exist. */
c38f313d 14082 if (child_origin_die != child_die)
d389af10 14083 {
edb3359d
DJ
14084 if (child_die->tag != child_origin_die->tag
14085 && !(child_die->tag == DW_TAG_inlined_subroutine
14086 && child_origin_die->tag == DW_TAG_subprogram))
d389af10 14087 complaint (&symfile_complaints,
9d8780f0 14088 _("Child DIE %s and its abstract origin %s have "
9c541725 14089 "different tags"),
9d8780f0
SM
14090 sect_offset_str (child_die->sect_off),
14091 sect_offset_str (child_origin_die->sect_off));
c38f313d
DJ
14092 if (child_origin_die->parent != origin_die)
14093 complaint (&symfile_complaints,
9d8780f0 14094 _("Child DIE %s and its abstract origin %s have "
9c541725 14095 "different parents"),
9d8780f0
SM
14096 sect_offset_str (child_die->sect_off),
14097 sect_offset_str (child_origin_die->sect_off));
c38f313d 14098 else
791afaa2 14099 offsets.push_back (child_origin_die->sect_off);
d389af10 14100 }
d389af10 14101 }
791afaa2
TT
14102 std::sort (offsets.begin (), offsets.end ());
14103 sect_offset *offsets_end = offsets.data () + offsets.size ();
14104 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 14105 if (offsetp[-1] == *offsetp)
3e43a32a 14106 complaint (&symfile_complaints,
9d8780f0
SM
14107 _("Multiple children of DIE %s refer "
14108 "to DIE %s as their abstract origin"),
14109 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 14110
791afaa2 14111 offsetp = offsets.data ();
d389af10
JK
14112 origin_child_die = origin_die->child;
14113 while (origin_child_die && origin_child_die->tag)
14114 {
14115 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 14116 while (offsetp < offsets_end
9c541725 14117 && *offsetp < origin_child_die->sect_off)
d389af10 14118 offsetp++;
b64f50a1 14119 if (offsetp >= offsets_end
9c541725 14120 || *offsetp > origin_child_die->sect_off)
d389af10 14121 {
adde2bff
DE
14122 /* Found that ORIGIN_CHILD_DIE is really not referenced.
14123 Check whether we're already processing ORIGIN_CHILD_DIE.
14124 This can happen with mutually referenced abstract_origins.
14125 PR 16581. */
14126 if (!origin_child_die->in_process)
14127 process_die (origin_child_die, origin_cu);
d389af10
JK
14128 }
14129 origin_child_die = sibling_die (origin_child_die);
14130 }
cd02d79d 14131 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
14132}
14133
c906108c 14134static void
e7c27a73 14135read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14136{
518817b3 14137 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14138 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 14139 struct context_stack *newobj;
c906108c
SS
14140 CORE_ADDR lowpc;
14141 CORE_ADDR highpc;
14142 struct die_info *child_die;
edb3359d 14143 struct attribute *attr, *call_line, *call_file;
15d034d0 14144 const char *name;
e142c38c 14145 CORE_ADDR baseaddr;
801e3a5b 14146 struct block *block;
edb3359d 14147 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 14148 std::vector<struct symbol *> template_args;
34eaf542 14149 struct template_symbol *templ_func = NULL;
edb3359d
DJ
14150
14151 if (inlined_func)
14152 {
14153 /* If we do not have call site information, we can't show the
14154 caller of this inlined function. That's too confusing, so
14155 only use the scope for local variables. */
14156 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
14157 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
14158 if (call_line == NULL || call_file == NULL)
14159 {
14160 read_lexical_block_scope (die, cu);
14161 return;
14162 }
14163 }
c906108c 14164
e142c38c
DJ
14165 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14166
94af9270 14167 name = dwarf2_name (die, cu);
c906108c 14168
e8d05480
JB
14169 /* Ignore functions with missing or empty names. These are actually
14170 illegal according to the DWARF standard. */
14171 if (name == NULL)
14172 {
14173 complaint (&symfile_complaints,
9d8780f0
SM
14174 _("missing name for subprogram DIE at %s"),
14175 sect_offset_str (die->sect_off));
e8d05480
JB
14176 return;
14177 }
14178
14179 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 14180 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 14181 <= PC_BOUNDS_INVALID)
e8d05480 14182 {
ae4d0c03
PM
14183 attr = dwarf2_attr (die, DW_AT_external, cu);
14184 if (!attr || !DW_UNSND (attr))
14185 complaint (&symfile_complaints,
3e43a32a 14186 _("cannot get low and high bounds "
9d8780f0
SM
14187 "for subprogram DIE at %s"),
14188 sect_offset_str (die->sect_off));
e8d05480
JB
14189 return;
14190 }
c906108c 14191
3e29f34a
MR
14192 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14193 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 14194
34eaf542
TT
14195 /* If we have any template arguments, then we must allocate a
14196 different sort of symbol. */
14197 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
14198 {
14199 if (child_die->tag == DW_TAG_template_type_param
14200 || child_die->tag == DW_TAG_template_value_param)
14201 {
e623cf5d 14202 templ_func = allocate_template_symbol (objfile);
cf724bc9 14203 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
14204 break;
14205 }
14206 }
14207
fe978cb0 14208 newobj = push_context (0, lowpc);
5e2db402
TT
14209 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
14210 (struct symbol *) templ_func);
4c2df51b 14211
4cecd739
DJ
14212 /* If there is a location expression for DW_AT_frame_base, record
14213 it. */
e142c38c 14214 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 14215 if (attr)
fe978cb0 14216 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 14217
63e43d3a
PMR
14218 /* If there is a location for the static link, record it. */
14219 newobj->static_link = NULL;
14220 attr = dwarf2_attr (die, DW_AT_static_link, cu);
14221 if (attr)
14222 {
224c3ddb
SM
14223 newobj->static_link
14224 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
14225 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
14226 }
14227
e142c38c 14228 cu->list_in_scope = &local_symbols;
c906108c 14229
639d11d3 14230 if (die->child != NULL)
c906108c 14231 {
639d11d3 14232 child_die = die->child;
c906108c
SS
14233 while (child_die && child_die->tag)
14234 {
34eaf542
TT
14235 if (child_die->tag == DW_TAG_template_type_param
14236 || child_die->tag == DW_TAG_template_value_param)
14237 {
14238 struct symbol *arg = new_symbol (child_die, NULL, cu);
14239
f1078f66 14240 if (arg != NULL)
2f4732b0 14241 template_args.push_back (arg);
34eaf542
TT
14242 }
14243 else
14244 process_die (child_die, cu);
c906108c
SS
14245 child_die = sibling_die (child_die);
14246 }
14247 }
14248
d389af10
JK
14249 inherit_abstract_dies (die, cu);
14250
4a811a97
UW
14251 /* If we have a DW_AT_specification, we might need to import using
14252 directives from the context of the specification DIE. See the
14253 comment in determine_prefix. */
14254 if (cu->language == language_cplus
14255 && dwarf2_attr (die, DW_AT_specification, cu))
14256 {
14257 struct dwarf2_cu *spec_cu = cu;
14258 struct die_info *spec_die = die_specification (die, &spec_cu);
14259
14260 while (spec_die)
14261 {
14262 child_die = spec_die->child;
14263 while (child_die && child_die->tag)
14264 {
14265 if (child_die->tag == DW_TAG_imported_module)
14266 process_die (child_die, spec_cu);
14267 child_die = sibling_die (child_die);
14268 }
14269
14270 /* In some cases, GCC generates specification DIEs that
14271 themselves contain DW_AT_specification attributes. */
14272 spec_die = die_specification (spec_die, &spec_cu);
14273 }
14274 }
14275
fe978cb0 14276 newobj = pop_context ();
c906108c 14277 /* Make a block for the local symbols within. */
fe978cb0 14278 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
63e43d3a 14279 newobj->static_link, lowpc, highpc);
801e3a5b 14280
df8a16a1 14281 /* For C++, set the block's scope. */
45280282
IB
14282 if ((cu->language == language_cplus
14283 || cu->language == language_fortran
c44af4eb
TT
14284 || cu->language == language_d
14285 || cu->language == language_rust)
4d4ec4e5 14286 && cu->processing_has_namespace_info)
195a3f6c
TT
14287 block_set_scope (block, determine_prefix (die, cu),
14288 &objfile->objfile_obstack);
df8a16a1 14289
801e3a5b
JB
14290 /* If we have address ranges, record them. */
14291 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 14292
fe978cb0 14293 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 14294
34eaf542 14295 /* Attach template arguments to function. */
2f4732b0 14296 if (!template_args.empty ())
34eaf542
TT
14297 {
14298 gdb_assert (templ_func != NULL);
14299
2f4732b0 14300 templ_func->n_template_arguments = template_args.size ();
34eaf542 14301 templ_func->template_arguments
8d749320
SM
14302 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
14303 templ_func->n_template_arguments);
34eaf542 14304 memcpy (templ_func->template_arguments,
2f4732b0 14305 template_args.data (),
34eaf542 14306 (templ_func->n_template_arguments * sizeof (struct symbol *)));
34eaf542
TT
14307 }
14308
208d8187
JB
14309 /* In C++, we can have functions nested inside functions (e.g., when
14310 a function declares a class that has methods). This means that
14311 when we finish processing a function scope, we may need to go
14312 back to building a containing block's symbol lists. */
fe978cb0 14313 local_symbols = newobj->locals;
22cee43f 14314 local_using_directives = newobj->local_using_directives;
208d8187 14315
921e78cf
JB
14316 /* If we've finished processing a top-level function, subsequent
14317 symbols go in the file symbol list. */
14318 if (outermost_context_p ())
e142c38c 14319 cu->list_in_scope = &file_symbols;
c906108c
SS
14320}
14321
14322/* Process all the DIES contained within a lexical block scope. Start
14323 a new scope, process the dies, and then close the scope. */
14324
14325static void
e7c27a73 14326read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14327{
518817b3 14328 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14329 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 14330 struct context_stack *newobj;
c906108c
SS
14331 CORE_ADDR lowpc, highpc;
14332 struct die_info *child_die;
e142c38c
DJ
14333 CORE_ADDR baseaddr;
14334
14335 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
14336
14337 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
14338 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
14339 as multiple lexical blocks? Handling children in a sane way would
6e70227d 14340 be nasty. Might be easier to properly extend generic blocks to
af34e669 14341 describe ranges. */
e385593e
JK
14342 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
14343 {
14344 case PC_BOUNDS_NOT_PRESENT:
14345 /* DW_TAG_lexical_block has no attributes, process its children as if
14346 there was no wrapping by that DW_TAG_lexical_block.
14347 GCC does no longer produces such DWARF since GCC r224161. */
14348 for (child_die = die->child;
14349 child_die != NULL && child_die->tag;
14350 child_die = sibling_die (child_die))
14351 process_die (child_die, cu);
14352 return;
14353 case PC_BOUNDS_INVALID:
14354 return;
14355 }
3e29f34a
MR
14356 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14357 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
14358
14359 push_context (0, lowpc);
639d11d3 14360 if (die->child != NULL)
c906108c 14361 {
639d11d3 14362 child_die = die->child;
c906108c
SS
14363 while (child_die && child_die->tag)
14364 {
e7c27a73 14365 process_die (child_die, cu);
c906108c
SS
14366 child_die = sibling_die (child_die);
14367 }
14368 }
3ea89b92 14369 inherit_abstract_dies (die, cu);
fe978cb0 14370 newobj = pop_context ();
c906108c 14371
22cee43f 14372 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 14373 {
801e3a5b 14374 struct block *block
63e43d3a 14375 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
fe978cb0 14376 newobj->start_addr, highpc);
801e3a5b
JB
14377
14378 /* Note that recording ranges after traversing children, as we
14379 do here, means that recording a parent's ranges entails
14380 walking across all its children's ranges as they appear in
14381 the address map, which is quadratic behavior.
14382
14383 It would be nicer to record the parent's ranges before
14384 traversing its children, simply overriding whatever you find
14385 there. But since we don't even decide whether to create a
14386 block until after we've traversed its children, that's hard
14387 to do. */
14388 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 14389 }
fe978cb0 14390 local_symbols = newobj->locals;
22cee43f 14391 local_using_directives = newobj->local_using_directives;
c906108c
SS
14392}
14393
216f72a1 14394/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
14395
14396static void
14397read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
14398{
518817b3 14399 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
14400 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14401 CORE_ADDR pc, baseaddr;
14402 struct attribute *attr;
14403 struct call_site *call_site, call_site_local;
14404 void **slot;
14405 int nparams;
14406 struct die_info *child_die;
14407
14408 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14409
216f72a1
JK
14410 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
14411 if (attr == NULL)
14412 {
14413 /* This was a pre-DWARF-5 GNU extension alias
14414 for DW_AT_call_return_pc. */
14415 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14416 }
96408a79
SA
14417 if (!attr)
14418 {
14419 complaint (&symfile_complaints,
216f72a1 14420 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
14421 "DIE %s [in module %s]"),
14422 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14423 return;
14424 }
31aa7e4e 14425 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 14426 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
14427
14428 if (cu->call_site_htab == NULL)
14429 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
14430 NULL, &objfile->objfile_obstack,
14431 hashtab_obstack_allocate, NULL);
14432 call_site_local.pc = pc;
14433 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
14434 if (*slot != NULL)
14435 {
14436 complaint (&symfile_complaints,
216f72a1 14437 _("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
14438 "DIE %s [in module %s]"),
14439 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 14440 objfile_name (objfile));
96408a79
SA
14441 return;
14442 }
14443
14444 /* Count parameters at the caller. */
14445
14446 nparams = 0;
14447 for (child_die = die->child; child_die && child_die->tag;
14448 child_die = sibling_die (child_die))
14449 {
216f72a1
JK
14450 if (child_die->tag != DW_TAG_call_site_parameter
14451 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14452 {
14453 complaint (&symfile_complaints,
216f72a1 14454 _("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
14455 "DW_TAG_call_site child DIE %s [in module %s]"),
14456 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 14457 objfile_name (objfile));
96408a79
SA
14458 continue;
14459 }
14460
14461 nparams++;
14462 }
14463
224c3ddb
SM
14464 call_site
14465 = ((struct call_site *)
14466 obstack_alloc (&objfile->objfile_obstack,
14467 sizeof (*call_site)
14468 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
14469 *slot = call_site;
14470 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14471 call_site->pc = pc;
14472
216f72a1
JK
14473 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14474 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
14475 {
14476 struct die_info *func_die;
14477
14478 /* Skip also over DW_TAG_inlined_subroutine. */
14479 for (func_die = die->parent;
14480 func_die && func_die->tag != DW_TAG_subprogram
14481 && func_die->tag != DW_TAG_subroutine_type;
14482 func_die = func_die->parent);
14483
216f72a1
JK
14484 /* DW_AT_call_all_calls is a superset
14485 of DW_AT_call_all_tail_calls. */
96408a79 14486 if (func_die
216f72a1 14487 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 14488 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 14489 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
14490 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14491 {
14492 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14493 not complete. But keep CALL_SITE for look ups via call_site_htab,
14494 both the initial caller containing the real return address PC and
14495 the final callee containing the current PC of a chain of tail
14496 calls do not need to have the tail call list complete. But any
14497 function candidate for a virtual tail call frame searched via
14498 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14499 determined unambiguously. */
14500 }
14501 else
14502 {
14503 struct type *func_type = NULL;
14504
14505 if (func_die)
14506 func_type = get_die_type (func_die, cu);
14507 if (func_type != NULL)
14508 {
14509 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14510
14511 /* Enlist this call site to the function. */
14512 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14513 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14514 }
14515 else
14516 complaint (&symfile_complaints,
216f72a1 14517 _("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
14518 "DIE %s [in module %s]"),
14519 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14520 }
14521 }
14522
216f72a1
JK
14523 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14524 if (attr == NULL)
14525 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14526 if (attr == NULL)
14527 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14528 if (attr == NULL)
216f72a1
JK
14529 {
14530 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14531 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14532 }
96408a79
SA
14533 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14534 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14535 /* Keep NULL DWARF_BLOCK. */;
14536 else if (attr_form_is_block (attr))
14537 {
14538 struct dwarf2_locexpr_baton *dlbaton;
14539
8d749320 14540 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
14541 dlbaton->data = DW_BLOCK (attr)->data;
14542 dlbaton->size = DW_BLOCK (attr)->size;
14543 dlbaton->per_cu = cu->per_cu;
14544
14545 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14546 }
7771576e 14547 else if (attr_form_is_ref (attr))
96408a79 14548 {
96408a79
SA
14549 struct dwarf2_cu *target_cu = cu;
14550 struct die_info *target_die;
14551
ac9ec31b 14552 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 14553 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
14554 if (die_is_declaration (target_die, target_cu))
14555 {
7d45c7c3 14556 const char *target_physname;
9112db09
JK
14557
14558 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14559 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14560 if (target_physname == NULL)
9112db09 14561 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
14562 if (target_physname == NULL)
14563 complaint (&symfile_complaints,
216f72a1 14564 _("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14565 "physname, for referencing DIE %s [in module %s]"),
14566 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14567 else
7d455152 14568 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14569 }
14570 else
14571 {
14572 CORE_ADDR lowpc;
14573
14574 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14575 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14576 <= PC_BOUNDS_INVALID)
96408a79 14577 complaint (&symfile_complaints,
216f72a1 14578 _("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14579 "low pc, for referencing DIE %s [in module %s]"),
14580 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14581 else
3e29f34a
MR
14582 {
14583 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14584 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14585 }
96408a79
SA
14586 }
14587 }
14588 else
14589 complaint (&symfile_complaints,
216f72a1 14590 _("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14591 "block nor reference, for DIE %s [in module %s]"),
14592 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14593
14594 call_site->per_cu = cu->per_cu;
14595
14596 for (child_die = die->child;
14597 child_die && child_die->tag;
14598 child_die = sibling_die (child_die))
14599 {
96408a79 14600 struct call_site_parameter *parameter;
1788b2d3 14601 struct attribute *loc, *origin;
96408a79 14602
216f72a1
JK
14603 if (child_die->tag != DW_TAG_call_site_parameter
14604 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14605 {
14606 /* Already printed the complaint above. */
14607 continue;
14608 }
14609
14610 gdb_assert (call_site->parameter_count < nparams);
14611 parameter = &call_site->parameter[call_site->parameter_count];
14612
1788b2d3
JK
14613 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14614 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14615 register is contained in DW_AT_call_value. */
96408a79 14616
24c5c679 14617 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14618 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14619 if (origin == NULL)
14620 {
14621 /* This was a pre-DWARF-5 GNU extension alias
14622 for DW_AT_call_parameter. */
14623 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14624 }
7771576e 14625 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14626 {
1788b2d3 14627 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14628
14629 sect_offset sect_off
14630 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14631 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14632 {
14633 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14634 binding can be done only inside one CU. Such referenced DIE
14635 therefore cannot be even moved to DW_TAG_partial_unit. */
14636 complaint (&symfile_complaints,
216f72a1 14637 _("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14638 "DW_TAG_call_site child DIE %s [in module %s]"),
14639 sect_offset_str (child_die->sect_off),
9c541725 14640 objfile_name (objfile));
d76b7dbc
JK
14641 continue;
14642 }
9c541725
PA
14643 parameter->u.param_cu_off
14644 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14645 }
14646 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
14647 {
14648 complaint (&symfile_complaints,
14649 _("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14650 "DW_TAG_call_site child DIE %s [in module %s]"),
14651 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14652 continue;
14653 }
24c5c679 14654 else
96408a79 14655 {
24c5c679
JK
14656 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14657 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14658 if (parameter->u.dwarf_reg != -1)
14659 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14660 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14661 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14662 &parameter->u.fb_offset))
14663 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14664 else
14665 {
14666 complaint (&symfile_complaints,
14667 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14668 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14669 "DW_TAG_call_site child DIE %s "
24c5c679 14670 "[in module %s]"),
9d8780f0 14671 sect_offset_str (child_die->sect_off),
9c541725 14672 objfile_name (objfile));
24c5c679
JK
14673 continue;
14674 }
96408a79
SA
14675 }
14676
216f72a1
JK
14677 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14678 if (attr == NULL)
14679 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14680 if (!attr_form_is_block (attr))
14681 {
14682 complaint (&symfile_complaints,
216f72a1 14683 _("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14684 "DW_TAG_call_site child DIE %s [in module %s]"),
14685 sect_offset_str (child_die->sect_off),
9c541725 14686 objfile_name (objfile));
96408a79
SA
14687 continue;
14688 }
14689 parameter->value = DW_BLOCK (attr)->data;
14690 parameter->value_size = DW_BLOCK (attr)->size;
14691
14692 /* Parameters are not pre-cleared by memset above. */
14693 parameter->data_value = NULL;
14694 parameter->data_value_size = 0;
14695 call_site->parameter_count++;
14696
216f72a1
JK
14697 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14698 if (attr == NULL)
14699 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
14700 if (attr)
14701 {
14702 if (!attr_form_is_block (attr))
14703 complaint (&symfile_complaints,
216f72a1 14704 _("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14705 "DW_TAG_call_site child DIE %s [in module %s]"),
14706 sect_offset_str (child_die->sect_off),
9c541725 14707 objfile_name (objfile));
96408a79
SA
14708 else
14709 {
14710 parameter->data_value = DW_BLOCK (attr)->data;
14711 parameter->data_value_size = DW_BLOCK (attr)->size;
14712 }
14713 }
14714 }
14715}
14716
71a3c369
TT
14717/* Helper function for read_variable. If DIE represents a virtual
14718 table, then return the type of the concrete object that is
14719 associated with the virtual table. Otherwise, return NULL. */
14720
14721static struct type *
14722rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14723{
14724 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14725 if (attr == NULL)
14726 return NULL;
14727
14728 /* Find the type DIE. */
14729 struct die_info *type_die = NULL;
14730 struct dwarf2_cu *type_cu = cu;
14731
14732 if (attr_form_is_ref (attr))
14733 type_die = follow_die_ref (die, attr, &type_cu);
14734 if (type_die == NULL)
14735 return NULL;
14736
14737 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14738 return NULL;
14739 return die_containing_type (type_die, type_cu);
14740}
14741
14742/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14743
14744static void
14745read_variable (struct die_info *die, struct dwarf2_cu *cu)
14746{
14747 struct rust_vtable_symbol *storage = NULL;
14748
14749 if (cu->language == language_rust)
14750 {
14751 struct type *containing_type = rust_containing_type (die, cu);
14752
14753 if (containing_type != NULL)
14754 {
518817b3 14755 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369
TT
14756
14757 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14758 struct rust_vtable_symbol);
14759 initialize_objfile_symbol (storage);
14760 storage->concrete_type = containing_type;
cf724bc9 14761 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14762 }
14763 }
14764
5e2db402 14765 new_symbol (die, NULL, cu, storage);
71a3c369
TT
14766}
14767
43988095
JK
14768/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14769 reading .debug_rnglists.
14770 Callback's type should be:
14771 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14772 Return true if the attributes are present and valid, otherwise,
14773 return false. */
14774
14775template <typename Callback>
14776static bool
14777dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14778 Callback &&callback)
14779{
ed2dc618 14780 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14781 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14782 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 14783 bfd *obfd = objfile->obfd;
43988095
JK
14784 /* Base address selection entry. */
14785 CORE_ADDR base;
14786 int found_base;
43988095 14787 const gdb_byte *buffer;
43988095
JK
14788 CORE_ADDR baseaddr;
14789 bool overflow = false;
14790
14791 found_base = cu->base_known;
14792 base = cu->base_address;
14793
14794 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14795 if (offset >= dwarf2_per_objfile->rnglists.size)
14796 {
14797 complaint (&symfile_complaints,
14798 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14799 offset);
14800 return false;
14801 }
14802 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14803
14804 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14805
14806 while (1)
14807 {
7814882a
JK
14808 /* Initialize it due to a false compiler warning. */
14809 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14810 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14811 + dwarf2_per_objfile->rnglists.size);
14812 unsigned int bytes_read;
14813
14814 if (buffer == buf_end)
14815 {
14816 overflow = true;
14817 break;
14818 }
14819 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14820 switch (rlet)
14821 {
14822 case DW_RLE_end_of_list:
14823 break;
14824 case DW_RLE_base_address:
14825 if (buffer + cu->header.addr_size > buf_end)
14826 {
14827 overflow = true;
14828 break;
14829 }
14830 base = read_address (obfd, buffer, cu, &bytes_read);
14831 found_base = 1;
14832 buffer += bytes_read;
14833 break;
14834 case DW_RLE_start_length:
14835 if (buffer + cu->header.addr_size > buf_end)
14836 {
14837 overflow = true;
14838 break;
14839 }
14840 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14841 buffer += bytes_read;
14842 range_end = (range_beginning
14843 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14844 buffer += bytes_read;
14845 if (buffer > buf_end)
14846 {
14847 overflow = true;
14848 break;
14849 }
14850 break;
14851 case DW_RLE_offset_pair:
14852 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14853 buffer += bytes_read;
14854 if (buffer > buf_end)
14855 {
14856 overflow = true;
14857 break;
14858 }
14859 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14860 buffer += bytes_read;
14861 if (buffer > buf_end)
14862 {
14863 overflow = true;
14864 break;
14865 }
14866 break;
14867 case DW_RLE_start_end:
14868 if (buffer + 2 * cu->header.addr_size > buf_end)
14869 {
14870 overflow = true;
14871 break;
14872 }
14873 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14874 buffer += bytes_read;
14875 range_end = read_address (obfd, buffer, cu, &bytes_read);
14876 buffer += bytes_read;
14877 break;
14878 default:
14879 complaint (&symfile_complaints,
14880 _("Invalid .debug_rnglists data (no base address)"));
14881 return false;
14882 }
14883 if (rlet == DW_RLE_end_of_list || overflow)
14884 break;
14885 if (rlet == DW_RLE_base_address)
14886 continue;
14887
14888 if (!found_base)
14889 {
14890 /* We have no valid base address for the ranges
14891 data. */
14892 complaint (&symfile_complaints,
14893 _("Invalid .debug_rnglists data (no base address)"));
14894 return false;
14895 }
14896
14897 if (range_beginning > range_end)
14898 {
14899 /* Inverted range entries are invalid. */
14900 complaint (&symfile_complaints,
14901 _("Invalid .debug_rnglists data (inverted range)"));
14902 return false;
14903 }
14904
14905 /* Empty range entries have no effect. */
14906 if (range_beginning == range_end)
14907 continue;
14908
14909 range_beginning += base;
14910 range_end += base;
14911
14912 /* A not-uncommon case of bad debug info.
14913 Don't pollute the addrmap with bad data. */
14914 if (range_beginning + baseaddr == 0
14915 && !dwarf2_per_objfile->has_section_at_zero)
14916 {
14917 complaint (&symfile_complaints,
14918 _(".debug_rnglists entry has start address of zero"
14919 " [in module %s]"), objfile_name (objfile));
14920 continue;
14921 }
14922
14923 callback (range_beginning, range_end);
14924 }
14925
14926 if (overflow)
14927 {
14928 complaint (&symfile_complaints,
14929 _("Offset %d is not terminated "
14930 "for DW_AT_ranges attribute"),
14931 offset);
14932 return false;
14933 }
14934
14935 return true;
14936}
14937
14938/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14939 Callback's type should be:
14940 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14941 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14942
43988095 14943template <typename Callback>
43039443 14944static int
5f46c5a5 14945dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14946 Callback &&callback)
43039443 14947{
ed2dc618 14948 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14949 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14950 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14951 struct comp_unit_head *cu_header = &cu->header;
14952 bfd *obfd = objfile->obfd;
14953 unsigned int addr_size = cu_header->addr_size;
14954 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14955 /* Base address selection entry. */
14956 CORE_ADDR base;
14957 int found_base;
14958 unsigned int dummy;
d521ce57 14959 const gdb_byte *buffer;
ff013f42 14960 CORE_ADDR baseaddr;
43039443 14961
43988095
JK
14962 if (cu_header->version >= 5)
14963 return dwarf2_rnglists_process (offset, cu, callback);
14964
d00adf39
DE
14965 found_base = cu->base_known;
14966 base = cu->base_address;
43039443 14967
be391dca 14968 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14969 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
14970 {
14971 complaint (&symfile_complaints,
14972 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14973 offset);
14974 return 0;
14975 }
dce234bc 14976 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14977
e7030f15 14978 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 14979
43039443
JK
14980 while (1)
14981 {
14982 CORE_ADDR range_beginning, range_end;
14983
14984 range_beginning = read_address (obfd, buffer, cu, &dummy);
14985 buffer += addr_size;
14986 range_end = read_address (obfd, buffer, cu, &dummy);
14987 buffer += addr_size;
14988 offset += 2 * addr_size;
14989
14990 /* An end of list marker is a pair of zero addresses. */
14991 if (range_beginning == 0 && range_end == 0)
14992 /* Found the end of list entry. */
14993 break;
14994
14995 /* Each base address selection entry is a pair of 2 values.
14996 The first is the largest possible address, the second is
14997 the base address. Check for a base address here. */
14998 if ((range_beginning & mask) == mask)
14999 {
28d2bfb9
AB
15000 /* If we found the largest possible address, then we already
15001 have the base address in range_end. */
15002 base = range_end;
43039443
JK
15003 found_base = 1;
15004 continue;
15005 }
15006
15007 if (!found_base)
15008 {
15009 /* We have no valid base address for the ranges
15010 data. */
15011 complaint (&symfile_complaints,
15012 _("Invalid .debug_ranges data (no base address)"));
15013 return 0;
15014 }
15015
9277c30c
UW
15016 if (range_beginning > range_end)
15017 {
15018 /* Inverted range entries are invalid. */
15019 complaint (&symfile_complaints,
15020 _("Invalid .debug_ranges data (inverted range)"));
15021 return 0;
15022 }
15023
15024 /* Empty range entries have no effect. */
15025 if (range_beginning == range_end)
15026 continue;
15027
43039443
JK
15028 range_beginning += base;
15029 range_end += base;
15030
01093045
DE
15031 /* A not-uncommon case of bad debug info.
15032 Don't pollute the addrmap with bad data. */
15033 if (range_beginning + baseaddr == 0
15034 && !dwarf2_per_objfile->has_section_at_zero)
15035 {
15036 complaint (&symfile_complaints,
15037 _(".debug_ranges entry has start address of zero"
4262abfb 15038 " [in module %s]"), objfile_name (objfile));
01093045
DE
15039 continue;
15040 }
15041
5f46c5a5
JK
15042 callback (range_beginning, range_end);
15043 }
15044
15045 return 1;
15046}
15047
15048/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
15049 Return 1 if the attributes are present and valid, otherwise, return 0.
15050 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
15051
15052static int
15053dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
15054 CORE_ADDR *high_return, struct dwarf2_cu *cu,
15055 struct partial_symtab *ranges_pst)
15056{
518817b3 15057 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5
JK
15058 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15059 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
15060 SECT_OFF_TEXT (objfile));
15061 int low_set = 0;
15062 CORE_ADDR low = 0;
15063 CORE_ADDR high = 0;
15064 int retval;
15065
15066 retval = dwarf2_ranges_process (offset, cu,
15067 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
15068 {
9277c30c 15069 if (ranges_pst != NULL)
3e29f34a
MR
15070 {
15071 CORE_ADDR lowpc;
15072 CORE_ADDR highpc;
15073
15074 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
15075 range_beginning + baseaddr);
15076 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
15077 range_end + baseaddr);
15078 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
15079 ranges_pst);
15080 }
ff013f42 15081
43039443
JK
15082 /* FIXME: This is recording everything as a low-high
15083 segment of consecutive addresses. We should have a
15084 data structure for discontiguous block ranges
15085 instead. */
15086 if (! low_set)
15087 {
15088 low = range_beginning;
15089 high = range_end;
15090 low_set = 1;
15091 }
15092 else
15093 {
15094 if (range_beginning < low)
15095 low = range_beginning;
15096 if (range_end > high)
15097 high = range_end;
15098 }
5f46c5a5
JK
15099 });
15100 if (!retval)
15101 return 0;
43039443
JK
15102
15103 if (! low_set)
15104 /* If the first entry is an end-of-list marker, the range
15105 describes an empty scope, i.e. no instructions. */
15106 return 0;
15107
15108 if (low_return)
15109 *low_return = low;
15110 if (high_return)
15111 *high_return = high;
15112 return 1;
15113}
15114
3a2b436a
JK
15115/* Get low and high pc attributes from a die. See enum pc_bounds_kind
15116 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 15117 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 15118
3a2b436a 15119static enum pc_bounds_kind
af34e669 15120dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
15121 CORE_ADDR *highpc, struct dwarf2_cu *cu,
15122 struct partial_symtab *pst)
c906108c 15123{
518817b3
SM
15124 struct dwarf2_per_objfile *dwarf2_per_objfile
15125 = cu->per_cu->dwarf2_per_objfile;
c906108c 15126 struct attribute *attr;
91da1414 15127 struct attribute *attr_high;
af34e669
DJ
15128 CORE_ADDR low = 0;
15129 CORE_ADDR high = 0;
e385593e 15130 enum pc_bounds_kind ret;
c906108c 15131
91da1414
MW
15132 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
15133 if (attr_high)
af34e669 15134 {
e142c38c 15135 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 15136 if (attr)
91da1414 15137 {
31aa7e4e
JB
15138 low = attr_value_as_address (attr);
15139 high = attr_value_as_address (attr_high);
15140 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
15141 high += low;
91da1414 15142 }
af34e669
DJ
15143 else
15144 /* Found high w/o low attribute. */
e385593e 15145 return PC_BOUNDS_INVALID;
af34e669
DJ
15146
15147 /* Found consecutive range of addresses. */
3a2b436a 15148 ret = PC_BOUNDS_HIGH_LOW;
af34e669 15149 }
c906108c 15150 else
af34e669 15151 {
e142c38c 15152 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
15153 if (attr != NULL)
15154 {
ab435259
DE
15155 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
15156 We take advantage of the fact that DW_AT_ranges does not appear
15157 in DW_TAG_compile_unit of DWO files. */
15158 int need_ranges_base = die->tag != DW_TAG_compile_unit;
15159 unsigned int ranges_offset = (DW_UNSND (attr)
15160 + (need_ranges_base
15161 ? cu->ranges_base
15162 : 0));
2e3cf129 15163
af34e669 15164 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 15165 .debug_ranges section. */
2e3cf129 15166 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 15167 return PC_BOUNDS_INVALID;
43039443 15168 /* Found discontinuous range of addresses. */
3a2b436a 15169 ret = PC_BOUNDS_RANGES;
af34e669 15170 }
e385593e
JK
15171 else
15172 return PC_BOUNDS_NOT_PRESENT;
af34e669 15173 }
c906108c 15174
48fbe735 15175 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 15176 if (high <= low)
e385593e 15177 return PC_BOUNDS_INVALID;
c906108c
SS
15178
15179 /* When using the GNU linker, .gnu.linkonce. sections are used to
15180 eliminate duplicate copies of functions and vtables and such.
15181 The linker will arbitrarily choose one and discard the others.
15182 The AT_*_pc values for such functions refer to local labels in
15183 these sections. If the section from that file was discarded, the
15184 labels are not in the output, so the relocs get a value of 0.
15185 If this is a discarded function, mark the pc bounds as invalid,
15186 so that GDB will ignore it. */
72dca2f5 15187 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 15188 return PC_BOUNDS_INVALID;
c906108c
SS
15189
15190 *lowpc = low;
96408a79
SA
15191 if (highpc)
15192 *highpc = high;
af34e669 15193 return ret;
c906108c
SS
15194}
15195
b084d499
JB
15196/* Assuming that DIE represents a subprogram DIE or a lexical block, get
15197 its low and high PC addresses. Do nothing if these addresses could not
15198 be determined. Otherwise, set LOWPC to the low address if it is smaller,
15199 and HIGHPC to the high address if greater than HIGHPC. */
15200
15201static void
15202dwarf2_get_subprogram_pc_bounds (struct die_info *die,
15203 CORE_ADDR *lowpc, CORE_ADDR *highpc,
15204 struct dwarf2_cu *cu)
15205{
15206 CORE_ADDR low, high;
15207 struct die_info *child = die->child;
15208
e385593e 15209 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 15210 {
325fac50
PA
15211 *lowpc = std::min (*lowpc, low);
15212 *highpc = std::max (*highpc, high);
b084d499
JB
15213 }
15214
15215 /* If the language does not allow nested subprograms (either inside
15216 subprograms or lexical blocks), we're done. */
15217 if (cu->language != language_ada)
15218 return;
6e70227d 15219
b084d499
JB
15220 /* Check all the children of the given DIE. If it contains nested
15221 subprograms, then check their pc bounds. Likewise, we need to
15222 check lexical blocks as well, as they may also contain subprogram
15223 definitions. */
15224 while (child && child->tag)
15225 {
15226 if (child->tag == DW_TAG_subprogram
15227 || child->tag == DW_TAG_lexical_block)
15228 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
15229 child = sibling_die (child);
15230 }
15231}
15232
fae299cd
DC
15233/* Get the low and high pc's represented by the scope DIE, and store
15234 them in *LOWPC and *HIGHPC. If the correct values can't be
15235 determined, set *LOWPC to -1 and *HIGHPC to 0. */
15236
15237static void
15238get_scope_pc_bounds (struct die_info *die,
15239 CORE_ADDR *lowpc, CORE_ADDR *highpc,
15240 struct dwarf2_cu *cu)
15241{
15242 CORE_ADDR best_low = (CORE_ADDR) -1;
15243 CORE_ADDR best_high = (CORE_ADDR) 0;
15244 CORE_ADDR current_low, current_high;
15245
3a2b436a 15246 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 15247 >= PC_BOUNDS_RANGES)
fae299cd
DC
15248 {
15249 best_low = current_low;
15250 best_high = current_high;
15251 }
15252 else
15253 {
15254 struct die_info *child = die->child;
15255
15256 while (child && child->tag)
15257 {
15258 switch (child->tag) {
15259 case DW_TAG_subprogram:
b084d499 15260 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
15261 break;
15262 case DW_TAG_namespace:
f55ee35c 15263 case DW_TAG_module:
fae299cd
DC
15264 /* FIXME: carlton/2004-01-16: Should we do this for
15265 DW_TAG_class_type/DW_TAG_structure_type, too? I think
15266 that current GCC's always emit the DIEs corresponding
15267 to definitions of methods of classes as children of a
15268 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
15269 the DIEs giving the declarations, which could be
15270 anywhere). But I don't see any reason why the
15271 standards says that they have to be there. */
15272 get_scope_pc_bounds (child, &current_low, &current_high, cu);
15273
15274 if (current_low != ((CORE_ADDR) -1))
15275 {
325fac50
PA
15276 best_low = std::min (best_low, current_low);
15277 best_high = std::max (best_high, current_high);
fae299cd
DC
15278 }
15279 break;
15280 default:
0963b4bd 15281 /* Ignore. */
fae299cd
DC
15282 break;
15283 }
15284
15285 child = sibling_die (child);
15286 }
15287 }
15288
15289 *lowpc = best_low;
15290 *highpc = best_high;
15291}
15292
801e3a5b
JB
15293/* Record the address ranges for BLOCK, offset by BASEADDR, as given
15294 in DIE. */
380bca97 15295
801e3a5b
JB
15296static void
15297dwarf2_record_block_ranges (struct die_info *die, struct block *block,
15298 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
15299{
518817b3 15300 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 15301 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 15302 struct attribute *attr;
91da1414 15303 struct attribute *attr_high;
801e3a5b 15304
91da1414
MW
15305 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
15306 if (attr_high)
801e3a5b 15307 {
801e3a5b
JB
15308 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
15309 if (attr)
15310 {
31aa7e4e
JB
15311 CORE_ADDR low = attr_value_as_address (attr);
15312 CORE_ADDR high = attr_value_as_address (attr_high);
15313
15314 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
15315 high += low;
9a619af0 15316
3e29f34a
MR
15317 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
15318 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
15319 record_block_range (block, low, high - 1);
801e3a5b
JB
15320 }
15321 }
15322
15323 attr = dwarf2_attr (die, DW_AT_ranges, cu);
15324 if (attr)
15325 {
ab435259
DE
15326 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
15327 We take advantage of the fact that DW_AT_ranges does not appear
15328 in DW_TAG_compile_unit of DWO files. */
15329 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
15330
15331 /* The value of the DW_AT_ranges attribute is the offset of the
15332 address range list in the .debug_ranges section. */
ab435259
DE
15333 unsigned long offset = (DW_UNSND (attr)
15334 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 15335 const gdb_byte *buffer;
801e3a5b
JB
15336
15337 /* For some target architectures, but not others, the
15338 read_address function sign-extends the addresses it returns.
15339 To recognize base address selection entries, we need a
15340 mask. */
15341 unsigned int addr_size = cu->header.addr_size;
15342 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
15343
15344 /* The base address, to which the next pair is relative. Note
15345 that this 'base' is a DWARF concept: most entries in a range
15346 list are relative, to reduce the number of relocs against the
15347 debugging information. This is separate from this function's
15348 'baseaddr' argument, which GDB uses to relocate debugging
15349 information from a shared library based on the address at
15350 which the library was loaded. */
d00adf39
DE
15351 CORE_ADDR base = cu->base_address;
15352 int base_known = cu->base_known;
801e3a5b 15353
5f46c5a5
JK
15354 dwarf2_ranges_process (offset, cu,
15355 [&] (CORE_ADDR start, CORE_ADDR end)
15356 {
58fdfd2c
JK
15357 start += baseaddr;
15358 end += baseaddr;
5f46c5a5
JK
15359 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
15360 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
15361 record_block_range (block, start, end - 1);
15362 });
801e3a5b
JB
15363 }
15364}
15365
685b1105
JK
15366/* Check whether the producer field indicates either of GCC < 4.6, or the
15367 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 15368
685b1105
JK
15369static void
15370check_producer (struct dwarf2_cu *cu)
60d5a603 15371{
38360086 15372 int major, minor;
60d5a603
JK
15373
15374 if (cu->producer == NULL)
15375 {
15376 /* For unknown compilers expect their behavior is DWARF version
15377 compliant.
15378
15379 GCC started to support .debug_types sections by -gdwarf-4 since
15380 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
15381 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
15382 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
15383 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 15384 }
b1ffba5a 15385 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 15386 {
38360086
MW
15387 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
15388 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 15389 }
5230b05a
WT
15390 else if (producer_is_icc (cu->producer, &major, &minor))
15391 cu->producer_is_icc_lt_14 = major < 14;
685b1105
JK
15392 else
15393 {
15394 /* For other non-GCC compilers, expect their behavior is DWARF version
15395 compliant. */
60d5a603
JK
15396 }
15397
ba919b58 15398 cu->checked_producer = 1;
685b1105 15399}
ba919b58 15400
685b1105
JK
15401/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
15402 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
15403 during 4.6.0 experimental. */
15404
15405static int
15406producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
15407{
15408 if (!cu->checked_producer)
15409 check_producer (cu);
15410
15411 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
15412}
15413
15414/* Return the default accessibility type if it is not overriden by
15415 DW_AT_accessibility. */
15416
15417static enum dwarf_access_attribute
15418dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
15419{
15420 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15421 {
15422 /* The default DWARF 2 accessibility for members is public, the default
15423 accessibility for inheritance is private. */
15424
15425 if (die->tag != DW_TAG_inheritance)
15426 return DW_ACCESS_public;
15427 else
15428 return DW_ACCESS_private;
15429 }
15430 else
15431 {
15432 /* DWARF 3+ defines the default accessibility a different way. The same
15433 rules apply now for DW_TAG_inheritance as for the members and it only
15434 depends on the container kind. */
15435
15436 if (die->parent->tag == DW_TAG_class_type)
15437 return DW_ACCESS_private;
15438 else
15439 return DW_ACCESS_public;
15440 }
15441}
15442
74ac6d43
TT
15443/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15444 offset. If the attribute was not found return 0, otherwise return
15445 1. If it was found but could not properly be handled, set *OFFSET
15446 to 0. */
15447
15448static int
15449handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15450 LONGEST *offset)
15451{
15452 struct attribute *attr;
15453
15454 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15455 if (attr != NULL)
15456 {
15457 *offset = 0;
15458
15459 /* Note that we do not check for a section offset first here.
15460 This is because DW_AT_data_member_location is new in DWARF 4,
15461 so if we see it, we can assume that a constant form is really
15462 a constant and not a section offset. */
15463 if (attr_form_is_constant (attr))
15464 *offset = dwarf2_get_attr_constant_value (attr, 0);
15465 else if (attr_form_is_section_offset (attr))
15466 dwarf2_complex_location_expr_complaint ();
15467 else if (attr_form_is_block (attr))
15468 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15469 else
15470 dwarf2_complex_location_expr_complaint ();
15471
15472 return 1;
15473 }
15474
15475 return 0;
15476}
15477
c906108c
SS
15478/* Add an aggregate field to the field list. */
15479
15480static void
107d2387 15481dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 15482 struct dwarf2_cu *cu)
6e70227d 15483{
518817b3 15484 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 15485 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
15486 struct nextfield *new_field;
15487 struct attribute *attr;
15488 struct field *fp;
15d034d0 15489 const char *fieldname = "";
c906108c
SS
15490
15491 /* Allocate a new field list entry and link it in. */
8d749320 15492 new_field = XNEW (struct nextfield);
b8c9b27d 15493 make_cleanup (xfree, new_field);
c906108c 15494 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
15495
15496 if (die->tag == DW_TAG_inheritance)
15497 {
15498 new_field->next = fip->baseclasses;
15499 fip->baseclasses = new_field;
15500 }
15501 else
15502 {
15503 new_field->next = fip->fields;
15504 fip->fields = new_field;
15505 }
c906108c
SS
15506 fip->nfields++;
15507
e142c38c 15508 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
15509 if (attr)
15510 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
15511 else
15512 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
15513 if (new_field->accessibility != DW_ACCESS_public)
15514 fip->non_public_fields = 1;
60d5a603 15515
e142c38c 15516 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
15517 if (attr)
15518 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
15519 else
15520 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15521
15522 fp = &new_field->field;
a9a9bd0f 15523
e142c38c 15524 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15525 {
74ac6d43
TT
15526 LONGEST offset;
15527
a9a9bd0f 15528 /* Data member other than a C++ static data member. */
6e70227d 15529
c906108c 15530 /* Get type of field. */
e7c27a73 15531 fp->type = die_type (die, cu);
c906108c 15532
d6a843b5 15533 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15534
c906108c 15535 /* Get bit size of field (zero if none). */
e142c38c 15536 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
15537 if (attr)
15538 {
15539 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15540 }
15541 else
15542 {
15543 FIELD_BITSIZE (*fp) = 0;
15544 }
15545
15546 /* Get bit offset of field. */
74ac6d43
TT
15547 if (handle_data_member_location (die, cu, &offset))
15548 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 15549 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
15550 if (attr)
15551 {
5e2b427d 15552 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
15553 {
15554 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
15555 additional bit offset from the MSB of the containing
15556 anonymous object to the MSB of the field. We don't
15557 have to do anything special since we don't need to
15558 know the size of the anonymous object. */
f41f5e61 15559 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
15560 }
15561 else
15562 {
15563 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
15564 MSB of the anonymous object, subtract off the number of
15565 bits from the MSB of the field to the MSB of the
15566 object, and then subtract off the number of bits of
15567 the field itself. The result is the bit offset of
15568 the LSB of the field. */
c906108c
SS
15569 int anonymous_size;
15570 int bit_offset = DW_UNSND (attr);
15571
e142c38c 15572 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15573 if (attr)
15574 {
15575 /* The size of the anonymous object containing
15576 the bit field is explicit, so use the
15577 indicated size (in bytes). */
15578 anonymous_size = DW_UNSND (attr);
15579 }
15580 else
15581 {
15582 /* The size of the anonymous object containing
15583 the bit field must be inferred from the type
15584 attribute of the data member containing the
15585 bit field. */
15586 anonymous_size = TYPE_LENGTH (fp->type);
15587 }
f41f5e61
PA
15588 SET_FIELD_BITPOS (*fp,
15589 (FIELD_BITPOS (*fp)
15590 + anonymous_size * bits_per_byte
15591 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15592 }
15593 }
da5b30da
AA
15594 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15595 if (attr != NULL)
15596 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15597 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
15598
15599 /* Get name of field. */
39cbfefa
DJ
15600 fieldname = dwarf2_name (die, cu);
15601 if (fieldname == NULL)
15602 fieldname = "";
d8151005
DJ
15603
15604 /* The name is already allocated along with this objfile, so we don't
15605 need to duplicate it for the type. */
15606 fp->name = fieldname;
c906108c
SS
15607
15608 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15609 pointer or virtual base class pointer) to private. */
e142c38c 15610 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15611 {
d48cc9dd 15612 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
15613 new_field->accessibility = DW_ACCESS_private;
15614 fip->non_public_fields = 1;
15615 }
15616 }
a9a9bd0f 15617 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15618 {
a9a9bd0f
DC
15619 /* C++ static member. */
15620
15621 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15622 is a declaration, but all versions of G++ as of this writing
15623 (so through at least 3.2.1) incorrectly generate
15624 DW_TAG_variable tags. */
6e70227d 15625
ff355380 15626 const char *physname;
c906108c 15627
a9a9bd0f 15628 /* Get name of field. */
39cbfefa
DJ
15629 fieldname = dwarf2_name (die, cu);
15630 if (fieldname == NULL)
c906108c
SS
15631 return;
15632
254e6b9e 15633 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15634 if (attr
15635 /* Only create a symbol if this is an external value.
15636 new_symbol checks this and puts the value in the global symbol
15637 table, which we want. If it is not external, new_symbol
15638 will try to put the value in cu->list_in_scope which is wrong. */
15639 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15640 {
15641 /* A static const member, not much different than an enum as far as
15642 we're concerned, except that we can support more types. */
15643 new_symbol (die, NULL, cu);
15644 }
15645
2df3850c 15646 /* Get physical name. */
ff355380 15647 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15648
d8151005
DJ
15649 /* The name is already allocated along with this objfile, so we don't
15650 need to duplicate it for the type. */
15651 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15652 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15653 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15654 }
15655 else if (die->tag == DW_TAG_inheritance)
15656 {
74ac6d43 15657 LONGEST offset;
d4b96c9a 15658
74ac6d43
TT
15659 /* C++ base class field. */
15660 if (handle_data_member_location (die, cu, &offset))
15661 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15662 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15663 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
15664 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
15665 fip->nbaseclasses++;
15666 }
2ddeaf8a
TT
15667 else if (die->tag == DW_TAG_variant_part)
15668 {
15669 /* process_structure_scope will treat this DIE as a union. */
15670 process_structure_scope (die, cu);
15671
15672 /* The variant part is relative to the start of the enclosing
15673 structure. */
15674 SET_FIELD_BITPOS (*fp, 0);
15675 fp->type = get_die_type (die, cu);
15676 fp->artificial = 1;
15677 fp->name = "<<variant>>";
15678 }
15679 else
15680 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15681}
15682
883fd55a
KS
15683/* Can the type given by DIE define another type? */
15684
15685static bool
15686type_can_define_types (const struct die_info *die)
15687{
15688 switch (die->tag)
15689 {
15690 case DW_TAG_typedef:
15691 case DW_TAG_class_type:
15692 case DW_TAG_structure_type:
15693 case DW_TAG_union_type:
15694 case DW_TAG_enumeration_type:
15695 return true;
15696
15697 default:
15698 return false;
15699 }
15700}
15701
15702/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15703
15704static void
883fd55a
KS
15705dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15706 struct dwarf2_cu *cu)
6e70227d 15707{
883fd55a
KS
15708 struct decl_field_list *new_field;
15709 struct decl_field *fp;
98751a41
JK
15710
15711 /* Allocate a new field list entry and link it in. */
883fd55a 15712 new_field = XCNEW (struct decl_field_list);
98751a41
JK
15713 make_cleanup (xfree, new_field);
15714
883fd55a 15715 gdb_assert (type_can_define_types (die));
98751a41
JK
15716
15717 fp = &new_field->field;
15718
883fd55a 15719 /* Get name of field. NULL is okay here, meaning an anonymous type. */
98751a41 15720 fp->name = dwarf2_name (die, cu);
98751a41
JK
15721 fp->type = read_type_die (die, cu);
15722
c191a687
KS
15723 /* Save accessibility. */
15724 enum dwarf_access_attribute accessibility;
15725 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15726 if (attr != NULL)
15727 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15728 else
15729 accessibility = dwarf2_default_access_attribute (die, cu);
15730 switch (accessibility)
15731 {
15732 case DW_ACCESS_public:
15733 /* The assumed value if neither private nor protected. */
15734 break;
15735 case DW_ACCESS_private:
15736 fp->is_private = 1;
15737 break;
15738 case DW_ACCESS_protected:
15739 fp->is_protected = 1;
15740 break;
15741 default:
37534686
KS
15742 complaint (&symfile_complaints,
15743 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15744 }
15745
883fd55a
KS
15746 if (die->tag == DW_TAG_typedef)
15747 {
15748 new_field->next = fip->typedef_field_list;
15749 fip->typedef_field_list = new_field;
15750 fip->typedef_field_list_count++;
15751 }
15752 else
15753 {
15754 new_field->next = fip->nested_types_list;
15755 fip->nested_types_list = new_field;
15756 fip->nested_types_list_count++;
15757 }
98751a41
JK
15758}
15759
c906108c
SS
15760/* Create the vector of fields, and attach it to the type. */
15761
15762static void
fba45db2 15763dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15764 struct dwarf2_cu *cu)
c906108c
SS
15765{
15766 int nfields = fip->nfields;
15767
15768 /* Record the field count, allocate space for the array of fields,
15769 and create blank accessibility bitfields if necessary. */
15770 TYPE_NFIELDS (type) = nfields;
15771 TYPE_FIELDS (type) = (struct field *)
15772 TYPE_ALLOC (type, sizeof (struct field) * nfields);
15773 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
15774
b4ba55a1 15775 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15776 {
15777 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15778
15779 TYPE_FIELD_PRIVATE_BITS (type) =
15780 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15781 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15782
15783 TYPE_FIELD_PROTECTED_BITS (type) =
15784 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15785 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15786
774b6a14
TT
15787 TYPE_FIELD_IGNORE_BITS (type) =
15788 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15789 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15790 }
15791
15792 /* If the type has baseclasses, allocate and clear a bit vector for
15793 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 15794 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
15795 {
15796 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 15797 unsigned char *pointer;
c906108c
SS
15798
15799 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15800 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15801 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
15802 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
15803 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
15804 }
15805
2ddeaf8a
TT
15806 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15807 {
15808 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15809
15810 int index = nfields - 1;
15811 struct nextfield *field = fip->fields;
15812
15813 while (index >= 0)
15814 {
15815 if (field->variant.is_discriminant)
15816 di->discriminant_index = index;
15817 else if (field->variant.default_branch)
15818 di->default_index = index;
15819 else
15820 di->discriminants[index] = field->variant.discriminant_value;
15821
15822 --index;
15823 field = field->next;
15824 }
15825 }
15826
3e43a32a
MS
15827 /* Copy the saved-up fields into the field vector. Start from the head of
15828 the list, adding to the tail of the field array, so that they end up in
15829 the same order in the array in which they were added to the list. */
c906108c
SS
15830 while (nfields-- > 0)
15831 {
7d0ccb61
DJ
15832 struct nextfield *fieldp;
15833
15834 if (fip->fields)
15835 {
15836 fieldp = fip->fields;
15837 fip->fields = fieldp->next;
15838 }
15839 else
15840 {
15841 fieldp = fip->baseclasses;
15842 fip->baseclasses = fieldp->next;
15843 }
15844
15845 TYPE_FIELD (type, nfields) = fieldp->field;
15846 switch (fieldp->accessibility)
c906108c 15847 {
c5aa993b 15848 case DW_ACCESS_private:
b4ba55a1
JB
15849 if (cu->language != language_ada)
15850 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 15851 break;
c906108c 15852
c5aa993b 15853 case DW_ACCESS_protected:
b4ba55a1
JB
15854 if (cu->language != language_ada)
15855 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 15856 break;
c906108c 15857
c5aa993b
JM
15858 case DW_ACCESS_public:
15859 break;
c906108c 15860
c5aa993b
JM
15861 default:
15862 /* Unknown accessibility. Complain and treat it as public. */
15863 {
e2e0b3e5 15864 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 15865 fieldp->accessibility);
c5aa993b
JM
15866 }
15867 break;
c906108c
SS
15868 }
15869 if (nfields < fip->nbaseclasses)
15870 {
7d0ccb61 15871 switch (fieldp->virtuality)
c906108c 15872 {
c5aa993b
JM
15873 case DW_VIRTUALITY_virtual:
15874 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15875 if (cu->language == language_ada)
a73c6dcd 15876 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
15877 SET_TYPE_FIELD_VIRTUAL (type, nfields);
15878 break;
c906108c
SS
15879 }
15880 }
c906108c
SS
15881 }
15882}
15883
7d27a96d
TT
15884/* Return true if this member function is a constructor, false
15885 otherwise. */
15886
15887static int
15888dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15889{
15890 const char *fieldname;
fe978cb0 15891 const char *type_name;
7d27a96d
TT
15892 int len;
15893
15894 if (die->parent == NULL)
15895 return 0;
15896
15897 if (die->parent->tag != DW_TAG_structure_type
15898 && die->parent->tag != DW_TAG_union_type
15899 && die->parent->tag != DW_TAG_class_type)
15900 return 0;
15901
15902 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15903 type_name = dwarf2_name (die->parent, cu);
15904 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15905 return 0;
15906
15907 len = strlen (fieldname);
fe978cb0
PA
15908 return (strncmp (fieldname, type_name, len) == 0
15909 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15910}
15911
c906108c
SS
15912/* Add a member function to the proper fieldlist. */
15913
15914static void
107d2387 15915dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15916 struct type *type, struct dwarf2_cu *cu)
c906108c 15917{
518817b3 15918 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15919 struct attribute *attr;
15920 struct fnfieldlist *flp;
15921 int i;
15922 struct fn_field *fnp;
15d034d0 15923 const char *fieldname;
c906108c 15924 struct nextfnfield *new_fnfield;
f792889a 15925 struct type *this_type;
60d5a603 15926 enum dwarf_access_attribute accessibility;
c906108c 15927
b4ba55a1 15928 if (cu->language == language_ada)
a73c6dcd 15929 error (_("unexpected member function in Ada type"));
b4ba55a1 15930
2df3850c 15931 /* Get name of member function. */
39cbfefa
DJ
15932 fieldname = dwarf2_name (die, cu);
15933 if (fieldname == NULL)
2df3850c 15934 return;
c906108c 15935
c906108c
SS
15936 /* Look up member function name in fieldlist. */
15937 for (i = 0; i < fip->nfnfields; i++)
15938 {
27bfe10e 15939 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
15940 break;
15941 }
15942
15943 /* Create new list element if necessary. */
15944 if (i < fip->nfnfields)
15945 flp = &fip->fnfieldlists[i];
15946 else
15947 {
15948 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
15949 {
15950 fip->fnfieldlists = (struct fnfieldlist *)
15951 xrealloc (fip->fnfieldlists,
15952 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 15953 * sizeof (struct fnfieldlist));
c906108c 15954 if (fip->nfnfields == 0)
c13c43fd 15955 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
15956 }
15957 flp = &fip->fnfieldlists[fip->nfnfields];
15958 flp->name = fieldname;
15959 flp->length = 0;
15960 flp->head = NULL;
3da10d80 15961 i = fip->nfnfields++;
c906108c
SS
15962 }
15963
15964 /* Create a new member function field and chain it to the field list
0963b4bd 15965 entry. */
8d749320 15966 new_fnfield = XNEW (struct nextfnfield);
b8c9b27d 15967 make_cleanup (xfree, new_fnfield);
c906108c
SS
15968 memset (new_fnfield, 0, sizeof (struct nextfnfield));
15969 new_fnfield->next = flp->head;
15970 flp->head = new_fnfield;
15971 flp->length++;
15972
15973 /* Fill in the member function field info. */
15974 fnp = &new_fnfield->fnfield;
3da10d80
KS
15975
15976 /* Delay processing of the physname until later. */
9c37b5ae 15977 if (cu->language == language_cplus)
3da10d80
KS
15978 {
15979 add_to_method_list (type, i, flp->length - 1, fieldname,
15980 die, cu);
15981 }
15982 else
15983 {
1d06ead6 15984 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15985 fnp->physname = physname ? physname : "";
15986 }
15987
c906108c 15988 fnp->type = alloc_type (objfile);
f792889a
DJ
15989 this_type = read_type_die (die, cu);
15990 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15991 {
f792889a 15992 int nparams = TYPE_NFIELDS (this_type);
c906108c 15993
f792889a 15994 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15995 of the method itself (TYPE_CODE_METHOD). */
15996 smash_to_method_type (fnp->type, type,
f792889a
DJ
15997 TYPE_TARGET_TYPE (this_type),
15998 TYPE_FIELDS (this_type),
15999 TYPE_NFIELDS (this_type),
16000 TYPE_VARARGS (this_type));
c906108c
SS
16001
16002 /* Handle static member functions.
c5aa993b 16003 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
16004 member functions. G++ helps GDB by marking the first
16005 parameter for non-static member functions (which is the this
16006 pointer) as artificial. We obtain this information from
16007 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 16008 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
16009 fnp->voffset = VOFFSET_STATIC;
16010 }
16011 else
e2e0b3e5 16012 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 16013 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
16014
16015 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 16016 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 16017 fnp->fcontext = die_containing_type (die, cu);
c906108c 16018
3e43a32a
MS
16019 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
16020 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
16021
16022 /* Get accessibility. */
e142c38c 16023 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 16024 if (attr)
aead7601 16025 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
16026 else
16027 accessibility = dwarf2_default_access_attribute (die, cu);
16028 switch (accessibility)
c906108c 16029 {
60d5a603
JK
16030 case DW_ACCESS_private:
16031 fnp->is_private = 1;
16032 break;
16033 case DW_ACCESS_protected:
16034 fnp->is_protected = 1;
16035 break;
c906108c
SS
16036 }
16037
b02dede2 16038 /* Check for artificial methods. */
e142c38c 16039 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
16040 if (attr && DW_UNSND (attr) != 0)
16041 fnp->is_artificial = 1;
16042
7d27a96d
TT
16043 fnp->is_constructor = dwarf2_is_constructor (die, cu);
16044
0d564a31 16045 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
16046 function. For older versions of GCC, this is an offset in the
16047 appropriate virtual table, as specified by DW_AT_containing_type.
16048 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
16049 to the object address. */
16050
e142c38c 16051 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 16052 if (attr)
8e19ed76 16053 {
aec5aa8b 16054 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 16055 {
aec5aa8b
TT
16056 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
16057 {
16058 /* Old-style GCC. */
16059 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
16060 }
16061 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
16062 || (DW_BLOCK (attr)->size > 1
16063 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
16064 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
16065 {
aec5aa8b
TT
16066 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
16067 if ((fnp->voffset % cu->header.addr_size) != 0)
16068 dwarf2_complex_location_expr_complaint ();
16069 else
16070 fnp->voffset /= cu->header.addr_size;
16071 fnp->voffset += 2;
16072 }
16073 else
16074 dwarf2_complex_location_expr_complaint ();
16075
16076 if (!fnp->fcontext)
7e993ebf
KS
16077 {
16078 /* If there is no `this' field and no DW_AT_containing_type,
16079 we cannot actually find a base class context for the
16080 vtable! */
16081 if (TYPE_NFIELDS (this_type) == 0
16082 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
16083 {
16084 complaint (&symfile_complaints,
16085 _("cannot determine context for virtual member "
9d8780f0
SM
16086 "function \"%s\" (offset %s)"),
16087 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
16088 }
16089 else
16090 {
16091 fnp->fcontext
16092 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
16093 }
16094 }
aec5aa8b 16095 }
3690dd37 16096 else if (attr_form_is_section_offset (attr))
8e19ed76 16097 {
4d3c2250 16098 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
16099 }
16100 else
16101 {
4d3c2250
KB
16102 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
16103 fieldname);
8e19ed76 16104 }
0d564a31 16105 }
d48cc9dd
DJ
16106 else
16107 {
16108 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
16109 if (attr && DW_UNSND (attr))
16110 {
16111 /* GCC does this, as of 2008-08-25; PR debug/37237. */
16112 complaint (&symfile_complaints,
9d8780f0 16113 _("Member function \"%s\" (offset %s) is virtual "
3e43a32a 16114 "but the vtable offset is not specified"),
9d8780f0 16115 fieldname, sect_offset_str (die->sect_off));
9655fd1a 16116 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
16117 TYPE_CPLUS_DYNAMIC (type) = 1;
16118 }
16119 }
c906108c
SS
16120}
16121
16122/* Create the vector of member function fields, and attach it to the type. */
16123
16124static void
fba45db2 16125dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 16126 struct dwarf2_cu *cu)
c906108c
SS
16127{
16128 struct fnfieldlist *flp;
c906108c
SS
16129 int i;
16130
b4ba55a1 16131 if (cu->language == language_ada)
a73c6dcd 16132 error (_("unexpected member functions in Ada type"));
b4ba55a1 16133
c906108c
SS
16134 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16135 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
16136 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
16137
16138 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
16139 {
16140 struct nextfnfield *nfp = flp->head;
16141 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
16142 int k;
16143
16144 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
16145 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
16146 fn_flp->fn_fields = (struct fn_field *)
16147 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
16148 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 16149 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
16150 }
16151
16152 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
16153}
16154
1168df01
JB
16155/* Returns non-zero if NAME is the name of a vtable member in CU's
16156 language, zero otherwise. */
16157static int
16158is_vtable_name (const char *name, struct dwarf2_cu *cu)
16159{
16160 static const char vptr[] = "_vptr";
16161
9c37b5ae
TT
16162 /* Look for the C++ form of the vtable. */
16163 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
16164 return 1;
16165
16166 return 0;
16167}
16168
c0dd20ea 16169/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
16170 functions, with the ABI-specified layout. If TYPE describes
16171 such a structure, smash it into a member function type.
61049d3b
DJ
16172
16173 GCC shouldn't do this; it should just output pointer to member DIEs.
16174 This is GCC PR debug/28767. */
c0dd20ea 16175
0b92b5bb
TT
16176static void
16177quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 16178{
09e2d7c7 16179 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
16180
16181 /* Check for a structure with no name and two children. */
0b92b5bb
TT
16182 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
16183 return;
c0dd20ea
DJ
16184
16185 /* Check for __pfn and __delta members. */
0b92b5bb
TT
16186 if (TYPE_FIELD_NAME (type, 0) == NULL
16187 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
16188 || TYPE_FIELD_NAME (type, 1) == NULL
16189 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
16190 return;
c0dd20ea
DJ
16191
16192 /* Find the type of the method. */
0b92b5bb 16193 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
16194 if (pfn_type == NULL
16195 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
16196 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 16197 return;
c0dd20ea
DJ
16198
16199 /* Look for the "this" argument. */
16200 pfn_type = TYPE_TARGET_TYPE (pfn_type);
16201 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 16202 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 16203 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 16204 return;
c0dd20ea 16205
09e2d7c7 16206 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 16207 new_type = alloc_type (objfile);
09e2d7c7 16208 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
16209 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
16210 TYPE_VARARGS (pfn_type));
0b92b5bb 16211 smash_to_methodptr_type (type, new_type);
c0dd20ea 16212}
1168df01 16213
685b1105 16214
c906108c 16215/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
16216 (definition) to create a type for the structure or union. Fill in
16217 the type's name and general properties; the members will not be
83655187
DE
16218 processed until process_structure_scope. A symbol table entry for
16219 the type will also not be done until process_structure_scope (assuming
16220 the type has a name).
c906108c 16221
c767944b
DJ
16222 NOTE: we need to call these functions regardless of whether or not the
16223 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 16224 structure or union. This gets the type entered into our set of
83655187 16225 user defined types. */
c906108c 16226
f792889a 16227static struct type *
134d01f1 16228read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16229{
518817b3 16230 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
16231 struct type *type;
16232 struct attribute *attr;
15d034d0 16233 const char *name;
c906108c 16234
348e048f
DE
16235 /* If the definition of this type lives in .debug_types, read that type.
16236 Don't follow DW_AT_specification though, that will take us back up
16237 the chain and we want to go down. */
45e58e77 16238 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
16239 if (attr)
16240 {
ac9ec31b 16241 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16242
ac9ec31b 16243 /* The type's CU may not be the same as CU.
02142a6c 16244 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16245 return set_die_type (die, type, cu);
16246 }
16247
c0dd20ea 16248 type = alloc_type (objfile);
c906108c 16249 INIT_CPLUS_SPECIFIC (type);
93311388 16250
39cbfefa
DJ
16251 name = dwarf2_name (die, cu);
16252 if (name != NULL)
c906108c 16253 {
987504bb 16254 if (cu->language == language_cplus
c44af4eb
TT
16255 || cu->language == language_d
16256 || cu->language == language_rust)
63d06c5c 16257 {
15d034d0 16258 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
16259
16260 /* dwarf2_full_name might have already finished building the DIE's
16261 type. If so, there is no need to continue. */
16262 if (get_die_type (die, cu) != NULL)
16263 return get_die_type (die, cu);
16264
16265 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
16266 if (die->tag == DW_TAG_structure_type
16267 || die->tag == DW_TAG_class_type)
16268 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
16269 }
16270 else
16271 {
d8151005
DJ
16272 /* The name is already allocated along with this objfile, so
16273 we don't need to duplicate it for the type. */
7d455152 16274 TYPE_TAG_NAME (type) = name;
94af9270
KS
16275 if (die->tag == DW_TAG_class_type)
16276 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 16277 }
c906108c
SS
16278 }
16279
16280 if (die->tag == DW_TAG_structure_type)
16281 {
16282 TYPE_CODE (type) = TYPE_CODE_STRUCT;
16283 }
16284 else if (die->tag == DW_TAG_union_type)
16285 {
16286 TYPE_CODE (type) = TYPE_CODE_UNION;
16287 }
2ddeaf8a
TT
16288 else if (die->tag == DW_TAG_variant_part)
16289 {
16290 TYPE_CODE (type) = TYPE_CODE_UNION;
16291 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
16292 }
c906108c
SS
16293 else
16294 {
4753d33b 16295 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
16296 }
16297
0cc2414c
TT
16298 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16299 TYPE_DECLARED_CLASS (type) = 1;
16300
e142c38c 16301 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16302 if (attr)
16303 {
155bfbd3
JB
16304 if (attr_form_is_constant (attr))
16305 TYPE_LENGTH (type) = DW_UNSND (attr);
16306 else
16307 {
16308 /* For the moment, dynamic type sizes are not supported
16309 by GDB's struct type. The actual size is determined
16310 on-demand when resolving the type of a given object,
16311 so set the type's length to zero for now. Otherwise,
16312 we record an expression as the length, and that expression
16313 could lead to a very large value, which could eventually
16314 lead to us trying to allocate that much memory when creating
16315 a value of that type. */
16316 TYPE_LENGTH (type) = 0;
16317 }
c906108c
SS
16318 }
16319 else
16320 {
16321 TYPE_LENGTH (type) = 0;
16322 }
16323
5230b05a 16324 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 16325 {
5230b05a
WT
16326 /* ICC<14 does not output the required DW_AT_declaration on
16327 incomplete types, but gives them a size of zero. */
422b1cb0 16328 TYPE_STUB (type) = 1;
685b1105
JK
16329 }
16330 else
16331 TYPE_STUB_SUPPORTED (type) = 1;
16332
dc718098 16333 if (die_is_declaration (die, cu))
876cecd0 16334 TYPE_STUB (type) = 1;
a6c727b2
DJ
16335 else if (attr == NULL && die->child == NULL
16336 && producer_is_realview (cu->producer))
16337 /* RealView does not output the required DW_AT_declaration
16338 on incomplete types. */
16339 TYPE_STUB (type) = 1;
dc718098 16340
c906108c
SS
16341 /* We need to add the type field to the die immediately so we don't
16342 infinitely recurse when dealing with pointers to the structure
0963b4bd 16343 type within the structure itself. */
1c379e20 16344 set_die_type (die, type, cu);
c906108c 16345
7e314c57
JK
16346 /* set_die_type should be already done. */
16347 set_descriptive_type (type, die, cu);
16348
c767944b
DJ
16349 return type;
16350}
16351
2ddeaf8a
TT
16352/* A helper for process_structure_scope that handles a single member
16353 DIE. */
16354
16355static void
16356handle_struct_member_die (struct die_info *child_die, struct type *type,
16357 struct field_info *fi,
16358 std::vector<struct symbol *> *template_args,
16359 struct dwarf2_cu *cu)
16360{
16361 if (child_die->tag == DW_TAG_member
16362 || child_die->tag == DW_TAG_variable
16363 || child_die->tag == DW_TAG_variant_part)
16364 {
16365 /* NOTE: carlton/2002-11-05: A C++ static data member
16366 should be a DW_TAG_member that is a declaration, but
16367 all versions of G++ as of this writing (so through at
16368 least 3.2.1) incorrectly generate DW_TAG_variable
16369 tags for them instead. */
16370 dwarf2_add_field (fi, child_die, cu);
16371 }
16372 else if (child_die->tag == DW_TAG_subprogram)
16373 {
16374 /* Rust doesn't have member functions in the C++ sense.
16375 However, it does emit ordinary functions as children
16376 of a struct DIE. */
16377 if (cu->language == language_rust)
16378 read_func_scope (child_die, cu);
16379 else
16380 {
16381 /* C++ member function. */
16382 dwarf2_add_member_fn (fi, child_die, type, cu);
16383 }
16384 }
16385 else if (child_die->tag == DW_TAG_inheritance)
16386 {
16387 /* C++ base class field. */
16388 dwarf2_add_field (fi, child_die, cu);
16389 }
16390 else if (type_can_define_types (child_die))
16391 dwarf2_add_type_defn (fi, child_die, cu);
16392 else if (child_die->tag == DW_TAG_template_type_param
16393 || child_die->tag == DW_TAG_template_value_param)
16394 {
16395 struct symbol *arg = new_symbol (child_die, NULL, cu);
16396
16397 if (arg != NULL)
16398 template_args->push_back (arg);
16399 }
16400 else if (child_die->tag == DW_TAG_variant)
16401 {
16402 /* In a variant we want to get the discriminant and also add a
16403 field for our sole member child. */
16404 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
16405
16406 for (struct die_info *variant_child = child_die->child;
16407 variant_child != NULL;
16408 variant_child = sibling_die (variant_child))
16409 {
16410 if (variant_child->tag == DW_TAG_member)
16411 {
16412 handle_struct_member_die (variant_child, type, fi,
16413 template_args, cu);
16414 /* Only handle the one. */
16415 break;
16416 }
16417 }
16418
16419 /* We don't handle this but we might as well report it if we see
16420 it. */
16421 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
16422 complaint (&symfile_complaints,
16423 _("DW_AT_discr_list is not supported yet"
16424 " - DIE at %s [in module %s]"),
16425 sect_offset_str (child_die->sect_off),
16426 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16427
16428 /* The first field was just added, so we can stash the
16429 discriminant there. */
16430 gdb_assert (fi->fields != NULL);
16431 if (discr == NULL)
16432 fi->fields->variant.default_branch = true;
16433 else
16434 fi->fields->variant.discriminant_value = DW_UNSND (discr);
16435 }
16436}
16437
c767944b
DJ
16438/* Finish creating a structure or union type, including filling in
16439 its members and creating a symbol for it. */
16440
16441static void
16442process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16443{
518817b3 16444 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 16445 struct die_info *child_die;
c767944b
DJ
16446 struct type *type;
16447
16448 type = get_die_type (die, cu);
16449 if (type == NULL)
16450 type = read_structure_type (die, cu);
16451
2ddeaf8a
TT
16452 /* When reading a DW_TAG_variant_part, we need to notice when we
16453 read the discriminant member, so we can record it later in the
16454 discriminant_info. */
16455 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
16456 sect_offset discr_offset;
16457
16458 if (is_variant_part)
16459 {
16460 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16461 if (discr == NULL)
16462 {
16463 /* Maybe it's a univariant form, an extension we support.
16464 In this case arrange not to check the offset. */
16465 is_variant_part = false;
16466 }
16467 else if (attr_form_is_ref (discr))
16468 {
16469 struct dwarf2_cu *target_cu = cu;
16470 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16471
16472 discr_offset = target_die->sect_off;
16473 }
16474 else
16475 {
16476 complaint (&symfile_complaints,
16477 _("DW_AT_discr does not have DIE reference form"
16478 " - DIE at %s [in module %s]"),
16479 sect_offset_str (die->sect_off),
16480 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16481 is_variant_part = false;
16482 }
16483 }
16484
e142c38c 16485 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16486 {
16487 struct field_info fi;
2f4732b0 16488 std::vector<struct symbol *> template_args;
c767944b 16489 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
16490
16491 memset (&fi, 0, sizeof (struct field_info));
16492
639d11d3 16493 child_die = die->child;
c906108c
SS
16494
16495 while (child_die && child_die->tag)
16496 {
2ddeaf8a 16497 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 16498
2ddeaf8a
TT
16499 if (is_variant_part && discr_offset == child_die->sect_off)
16500 fi.fields->variant.is_discriminant = true;
34eaf542 16501
c906108c
SS
16502 child_die = sibling_die (child_die);
16503 }
16504
34eaf542 16505 /* Attach template arguments to type. */
2f4732b0 16506 if (!template_args.empty ())
34eaf542
TT
16507 {
16508 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16509 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16510 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16511 = XOBNEWVEC (&objfile->objfile_obstack,
16512 struct symbol *,
16513 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16514 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16515 template_args.data (),
34eaf542
TT
16516 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16517 * sizeof (struct symbol *)));
34eaf542
TT
16518 }
16519
c906108c
SS
16520 /* Attach fields and member functions to the type. */
16521 if (fi.nfields)
e7c27a73 16522 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
16523 if (fi.nfnfields)
16524 {
e7c27a73 16525 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16526
c5aa993b 16527 /* Get the type which refers to the base class (possibly this
c906108c 16528 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16529 class from the DW_AT_containing_type attribute. This use of
16530 DW_AT_containing_type is a GNU extension. */
c906108c 16531
e142c38c 16532 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16533 {
e7c27a73 16534 struct type *t = die_containing_type (die, cu);
c906108c 16535
ae6ae975 16536 set_type_vptr_basetype (type, t);
c906108c
SS
16537 if (type == t)
16538 {
c906108c
SS
16539 int i;
16540
16541 /* Our own class provides vtbl ptr. */
16542 for (i = TYPE_NFIELDS (t) - 1;
16543 i >= TYPE_N_BASECLASSES (t);
16544 --i)
16545 {
0d5cff50 16546 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16547
1168df01 16548 if (is_vtable_name (fieldname, cu))
c906108c 16549 {
ae6ae975 16550 set_type_vptr_fieldno (type, i);
c906108c
SS
16551 break;
16552 }
16553 }
16554
16555 /* Complain if virtual function table field not found. */
16556 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 16557 complaint (&symfile_complaints,
3e43a32a
MS
16558 _("virtual function table pointer "
16559 "not found when defining class '%s'"),
4d3c2250
KB
16560 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
16561 "");
c906108c
SS
16562 }
16563 else
16564 {
ae6ae975 16565 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16566 }
16567 }
f6235d4c 16568 else if (cu->producer
61012eef 16569 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16570 {
16571 /* The IBM XLC compiler does not provide direct indication
16572 of the containing type, but the vtable pointer is
16573 always named __vfp. */
16574
16575 int i;
16576
16577 for (i = TYPE_NFIELDS (type) - 1;
16578 i >= TYPE_N_BASECLASSES (type);
16579 --i)
16580 {
16581 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16582 {
ae6ae975
DE
16583 set_type_vptr_fieldno (type, i);
16584 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16585 break;
16586 }
16587 }
16588 }
c906108c 16589 }
98751a41
JK
16590
16591 /* Copy fi.typedef_field_list linked list elements content into the
16592 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16593 if (fi.typedef_field_list)
16594 {
16595 int i = fi.typedef_field_list_count;
16596
a0d7a4ff 16597 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16598 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16599 = ((struct decl_field *)
224c3ddb 16600 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
98751a41
JK
16601 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
16602
16603 /* Reverse the list order to keep the debug info elements order. */
16604 while (--i >= 0)
16605 {
883fd55a 16606 struct decl_field *dest, *src;
6e70227d 16607
98751a41
JK
16608 dest = &TYPE_TYPEDEF_FIELD (type, i);
16609 src = &fi.typedef_field_list->field;
16610 fi.typedef_field_list = fi.typedef_field_list->next;
16611 *dest = *src;
16612 }
16613 }
c767944b 16614
883fd55a
KS
16615 /* Copy fi.nested_types_list linked list elements content into the
16616 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16617 if (fi.nested_types_list != NULL && cu->language != language_ada)
16618 {
16619 int i = fi.nested_types_list_count;
16620
16621 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16622 TYPE_NESTED_TYPES_ARRAY (type)
16623 = ((struct decl_field *)
16624 TYPE_ALLOC (type, sizeof (struct decl_field) * i));
16625 TYPE_NESTED_TYPES_COUNT (type) = i;
16626
16627 /* Reverse the list order to keep the debug info elements order. */
16628 while (--i >= 0)
16629 {
16630 struct decl_field *dest, *src;
16631
16632 dest = &TYPE_NESTED_TYPES_FIELD (type, i);
16633 src = &fi.nested_types_list->field;
16634 fi.nested_types_list = fi.nested_types_list->next;
16635 *dest = *src;
16636 }
16637 }
16638
c767944b 16639 do_cleanups (back_to);
c906108c 16640 }
63d06c5c 16641
bb5ed363 16642 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16643 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16644 cu->rust_unions.push_back (type);
0b92b5bb 16645
90aeadfc
DC
16646 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16647 snapshots) has been known to create a die giving a declaration
16648 for a class that has, as a child, a die giving a definition for a
16649 nested class. So we have to process our children even if the
16650 current die is a declaration. Normally, of course, a declaration
16651 won't have any children at all. */
134d01f1 16652
ca040673
DE
16653 child_die = die->child;
16654
90aeadfc
DC
16655 while (child_die != NULL && child_die->tag)
16656 {
16657 if (child_die->tag == DW_TAG_member
16658 || child_die->tag == DW_TAG_variable
34eaf542
TT
16659 || child_die->tag == DW_TAG_inheritance
16660 || child_die->tag == DW_TAG_template_value_param
16661 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16662 {
90aeadfc 16663 /* Do nothing. */
134d01f1 16664 }
90aeadfc
DC
16665 else
16666 process_die (child_die, cu);
134d01f1 16667
90aeadfc 16668 child_die = sibling_die (child_die);
134d01f1
DJ
16669 }
16670
fa4028e9
JB
16671 /* Do not consider external references. According to the DWARF standard,
16672 these DIEs are identified by the fact that they have no byte_size
16673 attribute, and a declaration attribute. */
16674 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16675 || !die_is_declaration (die, cu))
c767944b 16676 new_symbol (die, type, cu);
134d01f1
DJ
16677}
16678
55426c9d
JB
16679/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16680 update TYPE using some information only available in DIE's children. */
16681
16682static void
16683update_enumeration_type_from_children (struct die_info *die,
16684 struct type *type,
16685 struct dwarf2_cu *cu)
16686{
60f7655a 16687 struct die_info *child_die;
55426c9d
JB
16688 int unsigned_enum = 1;
16689 int flag_enum = 1;
16690 ULONGEST mask = 0;
55426c9d 16691
8268c778 16692 auto_obstack obstack;
55426c9d 16693
60f7655a
DE
16694 for (child_die = die->child;
16695 child_die != NULL && child_die->tag;
16696 child_die = sibling_die (child_die))
55426c9d
JB
16697 {
16698 struct attribute *attr;
16699 LONGEST value;
16700 const gdb_byte *bytes;
16701 struct dwarf2_locexpr_baton *baton;
16702 const char *name;
60f7655a 16703
55426c9d
JB
16704 if (child_die->tag != DW_TAG_enumerator)
16705 continue;
16706
16707 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16708 if (attr == NULL)
16709 continue;
16710
16711 name = dwarf2_name (child_die, cu);
16712 if (name == NULL)
16713 name = "<anonymous enumerator>";
16714
16715 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16716 &value, &bytes, &baton);
16717 if (value < 0)
16718 {
16719 unsigned_enum = 0;
16720 flag_enum = 0;
16721 }
16722 else if ((mask & value) != 0)
16723 flag_enum = 0;
16724 else
16725 mask |= value;
16726
16727 /* If we already know that the enum type is neither unsigned, nor
16728 a flag type, no need to look at the rest of the enumerates. */
16729 if (!unsigned_enum && !flag_enum)
16730 break;
55426c9d
JB
16731 }
16732
16733 if (unsigned_enum)
16734 TYPE_UNSIGNED (type) = 1;
16735 if (flag_enum)
16736 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16737}
16738
134d01f1
DJ
16739/* Given a DW_AT_enumeration_type die, set its type. We do not
16740 complete the type's fields yet, or create any symbols. */
c906108c 16741
f792889a 16742static struct type *
134d01f1 16743read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16744{
518817b3 16745 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16746 struct type *type;
c906108c 16747 struct attribute *attr;
0114d602 16748 const char *name;
134d01f1 16749
348e048f
DE
16750 /* If the definition of this type lives in .debug_types, read that type.
16751 Don't follow DW_AT_specification though, that will take us back up
16752 the chain and we want to go down. */
45e58e77 16753 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
16754 if (attr)
16755 {
ac9ec31b 16756 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16757
ac9ec31b 16758 /* The type's CU may not be the same as CU.
02142a6c 16759 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16760 return set_die_type (die, type, cu);
16761 }
16762
c906108c
SS
16763 type = alloc_type (objfile);
16764
16765 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16766 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16767 if (name != NULL)
7d455152 16768 TYPE_TAG_NAME (type) = name;
c906108c 16769
0626fc76
TT
16770 attr = dwarf2_attr (die, DW_AT_type, cu);
16771 if (attr != NULL)
16772 {
16773 struct type *underlying_type = die_type (die, cu);
16774
16775 TYPE_TARGET_TYPE (type) = underlying_type;
16776 }
16777
e142c38c 16778 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16779 if (attr)
16780 {
16781 TYPE_LENGTH (type) = DW_UNSND (attr);
16782 }
16783 else
16784 {
16785 TYPE_LENGTH (type) = 0;
16786 }
16787
137033e9
JB
16788 /* The enumeration DIE can be incomplete. In Ada, any type can be
16789 declared as private in the package spec, and then defined only
16790 inside the package body. Such types are known as Taft Amendment
16791 Types. When another package uses such a type, an incomplete DIE
16792 may be generated by the compiler. */
02eb380e 16793 if (die_is_declaration (die, cu))
876cecd0 16794 TYPE_STUB (type) = 1;
02eb380e 16795
0626fc76
TT
16796 /* Finish the creation of this type by using the enum's children.
16797 We must call this even when the underlying type has been provided
16798 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16799 update_enumeration_type_from_children (die, type, cu);
16800
0626fc76
TT
16801 /* If this type has an underlying type that is not a stub, then we
16802 may use its attributes. We always use the "unsigned" attribute
16803 in this situation, because ordinarily we guess whether the type
16804 is unsigned -- but the guess can be wrong and the underlying type
16805 can tell us the reality. However, we defer to a local size
16806 attribute if one exists, because this lets the compiler override
16807 the underlying type if needed. */
16808 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16809 {
16810 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16811 if (TYPE_LENGTH (type) == 0)
16812 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16813 }
16814
3d567982
TT
16815 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16816
f792889a 16817 return set_die_type (die, type, cu);
134d01f1
DJ
16818}
16819
16820/* Given a pointer to a die which begins an enumeration, process all
16821 the dies that define the members of the enumeration, and create the
16822 symbol for the enumeration type.
16823
16824 NOTE: We reverse the order of the element list. */
16825
16826static void
16827process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16828{
f792889a 16829 struct type *this_type;
134d01f1 16830
f792889a
DJ
16831 this_type = get_die_type (die, cu);
16832 if (this_type == NULL)
16833 this_type = read_enumeration_type (die, cu);
9dc481d3 16834
639d11d3 16835 if (die->child != NULL)
c906108c 16836 {
9dc481d3
DE
16837 struct die_info *child_die;
16838 struct symbol *sym;
16839 struct field *fields = NULL;
16840 int num_fields = 0;
15d034d0 16841 const char *name;
9dc481d3 16842
639d11d3 16843 child_die = die->child;
c906108c
SS
16844 while (child_die && child_die->tag)
16845 {
16846 if (child_die->tag != DW_TAG_enumerator)
16847 {
e7c27a73 16848 process_die (child_die, cu);
c906108c
SS
16849 }
16850 else
16851 {
39cbfefa
DJ
16852 name = dwarf2_name (child_die, cu);
16853 if (name)
c906108c 16854 {
f792889a 16855 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
16856
16857 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16858 {
16859 fields = (struct field *)
16860 xrealloc (fields,
16861 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 16862 * sizeof (struct field));
c906108c
SS
16863 }
16864
3567439c 16865 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 16866 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 16867 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
16868 FIELD_BITSIZE (fields[num_fields]) = 0;
16869
16870 num_fields++;
16871 }
16872 }
16873
16874 child_die = sibling_die (child_die);
16875 }
16876
16877 if (num_fields)
16878 {
f792889a
DJ
16879 TYPE_NFIELDS (this_type) = num_fields;
16880 TYPE_FIELDS (this_type) = (struct field *)
16881 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16882 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 16883 sizeof (struct field) * num_fields);
b8c9b27d 16884 xfree (fields);
c906108c 16885 }
c906108c 16886 }
134d01f1 16887
6c83ed52
TT
16888 /* If we are reading an enum from a .debug_types unit, and the enum
16889 is a declaration, and the enum is not the signatured type in the
16890 unit, then we do not want to add a symbol for it. Adding a
16891 symbol would in some cases obscure the true definition of the
16892 enum, giving users an incomplete type when the definition is
16893 actually available. Note that we do not want to do this for all
16894 enums which are just declarations, because C++0x allows forward
16895 enum declarations. */
3019eac3 16896 if (cu->per_cu->is_debug_types
6c83ed52
TT
16897 && die_is_declaration (die, cu))
16898 {
52dc124a 16899 struct signatured_type *sig_type;
6c83ed52 16900
c0f78cd4 16901 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16902 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16903 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16904 return;
16905 }
16906
f792889a 16907 new_symbol (die, this_type, cu);
c906108c
SS
16908}
16909
16910/* Extract all information from a DW_TAG_array_type DIE and put it in
16911 the DIE's type field. For now, this only handles one dimensional
16912 arrays. */
16913
f792889a 16914static struct type *
e7c27a73 16915read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16916{
518817b3 16917 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16918 struct die_info *child_die;
7e314c57 16919 struct type *type;
c906108c 16920 struct type *element_type, *range_type, *index_type;
c906108c 16921 struct attribute *attr;
15d034d0 16922 const char *name;
a405673c 16923 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16924 unsigned int bit_stride = 0;
c906108c 16925
e7c27a73 16926 element_type = die_type (die, cu);
c906108c 16927
7e314c57
JK
16928 /* The die_type call above may have already set the type for this DIE. */
16929 type = get_die_type (die, cu);
16930 if (type)
16931 return type;
16932
dc53a7ad
JB
16933 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16934 if (attr != NULL)
a405673c
JB
16935 {
16936 int stride_ok;
16937
16938 byte_stride_prop
16939 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16940 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16941 if (!stride_ok)
16942 {
16943 complaint (&symfile_complaints,
16944 _("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16945 " - DIE at %s [in module %s]"),
16946 sect_offset_str (die->sect_off),
518817b3 16947 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16948 /* Ignore this attribute. We will likely not be able to print
16949 arrays of this type correctly, but there is little we can do
16950 to help if we cannot read the attribute's value. */
16951 byte_stride_prop = NULL;
16952 }
16953 }
dc53a7ad
JB
16954
16955 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16956 if (attr != NULL)
16957 bit_stride = DW_UNSND (attr);
16958
c906108c
SS
16959 /* Irix 6.2 native cc creates array types without children for
16960 arrays with unspecified length. */
639d11d3 16961 if (die->child == NULL)
c906108c 16962 {
46bf5051 16963 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16964 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16965 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16966 byte_stride_prop, bit_stride);
f792889a 16967 return set_die_type (die, type, cu);
c906108c
SS
16968 }
16969
791afaa2 16970 std::vector<struct type *> range_types;
639d11d3 16971 child_die = die->child;
c906108c
SS
16972 while (child_die && child_die->tag)
16973 {
16974 if (child_die->tag == DW_TAG_subrange_type)
16975 {
f792889a 16976 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16977
f792889a 16978 if (child_type != NULL)
a02abb62 16979 {
0963b4bd
MS
16980 /* The range type was succesfully read. Save it for the
16981 array type creation. */
791afaa2 16982 range_types.push_back (child_type);
a02abb62 16983 }
c906108c
SS
16984 }
16985 child_die = sibling_die (child_die);
16986 }
16987
16988 /* Dwarf2 dimensions are output from left to right, create the
16989 necessary array types in backwards order. */
7ca2d3a3 16990
c906108c 16991 type = element_type;
7ca2d3a3
DL
16992
16993 if (read_array_order (die, cu) == DW_ORD_col_major)
16994 {
16995 int i = 0;
9a619af0 16996
791afaa2 16997 while (i < range_types.size ())
dc53a7ad 16998 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16999 byte_stride_prop, bit_stride);
7ca2d3a3
DL
17000 }
17001 else
17002 {
791afaa2 17003 size_t ndim = range_types.size ();
7ca2d3a3 17004 while (ndim-- > 0)
dc53a7ad 17005 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 17006 byte_stride_prop, bit_stride);
7ca2d3a3 17007 }
c906108c 17008
f5f8a009
EZ
17009 /* Understand Dwarf2 support for vector types (like they occur on
17010 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
17011 array type. This is not part of the Dwarf2/3 standard yet, but a
17012 custom vendor extension. The main difference between a regular
17013 array and the vector variant is that vectors are passed by value
17014 to functions. */
e142c38c 17015 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 17016 if (attr)
ea37ba09 17017 make_vector_type (type);
f5f8a009 17018
dbc98a8b
KW
17019 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
17020 implementation may choose to implement triple vectors using this
17021 attribute. */
17022 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17023 if (attr)
17024 {
17025 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
17026 TYPE_LENGTH (type) = DW_UNSND (attr);
17027 else
3e43a32a
MS
17028 complaint (&symfile_complaints,
17029 _("DW_AT_byte_size for array type smaller "
17030 "than the total size of elements"));
dbc98a8b
KW
17031 }
17032
39cbfefa
DJ
17033 name = dwarf2_name (die, cu);
17034 if (name)
17035 TYPE_NAME (type) = name;
6e70227d 17036
0963b4bd 17037 /* Install the type in the die. */
7e314c57
JK
17038 set_die_type (die, type, cu);
17039
17040 /* set_die_type should be already done. */
b4ba55a1
JB
17041 set_descriptive_type (type, die, cu);
17042
7e314c57 17043 return type;
c906108c
SS
17044}
17045
7ca2d3a3 17046static enum dwarf_array_dim_ordering
6e70227d 17047read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
17048{
17049 struct attribute *attr;
17050
17051 attr = dwarf2_attr (die, DW_AT_ordering, cu);
17052
aead7601
SM
17053 if (attr)
17054 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 17055
0963b4bd
MS
17056 /* GNU F77 is a special case, as at 08/2004 array type info is the
17057 opposite order to the dwarf2 specification, but data is still
17058 laid out as per normal fortran.
7ca2d3a3 17059
0963b4bd
MS
17060 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17061 version checking. */
7ca2d3a3 17062
905e0470
PM
17063 if (cu->language == language_fortran
17064 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
17065 {
17066 return DW_ORD_row_major;
17067 }
17068
6e70227d 17069 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
17070 {
17071 case array_column_major:
17072 return DW_ORD_col_major;
17073 case array_row_major:
17074 default:
17075 return DW_ORD_row_major;
17076 };
17077}
17078
72019c9c 17079/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 17080 the DIE's type field. */
72019c9c 17081
f792889a 17082static struct type *
72019c9c
GM
17083read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17084{
7e314c57
JK
17085 struct type *domain_type, *set_type;
17086 struct attribute *attr;
f792889a 17087
7e314c57
JK
17088 domain_type = die_type (die, cu);
17089
17090 /* The die_type call above may have already set the type for this DIE. */
17091 set_type = get_die_type (die, cu);
17092 if (set_type)
17093 return set_type;
17094
17095 set_type = create_set_type (NULL, domain_type);
17096
17097 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
17098 if (attr)
17099 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 17100
f792889a 17101 return set_die_type (die, set_type, cu);
72019c9c 17102}
7ca2d3a3 17103
0971de02
TT
17104/* A helper for read_common_block that creates a locexpr baton.
17105 SYM is the symbol which we are marking as computed.
17106 COMMON_DIE is the DIE for the common block.
17107 COMMON_LOC is the location expression attribute for the common
17108 block itself.
17109 MEMBER_LOC is the location expression attribute for the particular
17110 member of the common block that we are processing.
17111 CU is the CU from which the above come. */
17112
17113static void
17114mark_common_block_symbol_computed (struct symbol *sym,
17115 struct die_info *common_die,
17116 struct attribute *common_loc,
17117 struct attribute *member_loc,
17118 struct dwarf2_cu *cu)
17119{
518817b3
SM
17120 struct dwarf2_per_objfile *dwarf2_per_objfile
17121 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
17122 struct objfile *objfile = dwarf2_per_objfile->objfile;
17123 struct dwarf2_locexpr_baton *baton;
17124 gdb_byte *ptr;
17125 unsigned int cu_off;
17126 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
17127 LONGEST offset = 0;
17128
17129 gdb_assert (common_loc && member_loc);
17130 gdb_assert (attr_form_is_block (common_loc));
17131 gdb_assert (attr_form_is_block (member_loc)
17132 || attr_form_is_constant (member_loc));
17133
8d749320 17134 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
17135 baton->per_cu = cu->per_cu;
17136 gdb_assert (baton->per_cu);
17137
17138 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17139
17140 if (attr_form_is_constant (member_loc))
17141 {
17142 offset = dwarf2_get_attr_constant_value (member_loc, 0);
17143 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17144 }
17145 else
17146 baton->size += DW_BLOCK (member_loc)->size;
17147
224c3ddb 17148 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
17149 baton->data = ptr;
17150
17151 *ptr++ = DW_OP_call4;
9c541725 17152 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
17153 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17154 ptr += 4;
17155
17156 if (attr_form_is_constant (member_loc))
17157 {
17158 *ptr++ = DW_OP_addr;
17159 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17160 ptr += cu->header.addr_size;
17161 }
17162 else
17163 {
17164 /* We have to copy the data here, because DW_OP_call4 will only
17165 use a DW_AT_location attribute. */
17166 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
17167 ptr += DW_BLOCK (member_loc)->size;
17168 }
17169
17170 *ptr++ = DW_OP_plus;
17171 gdb_assert (ptr - baton->data == baton->size);
17172
0971de02 17173 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 17174 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
17175}
17176
4357ac6c
TT
17177/* Create appropriate locally-scoped variables for all the
17178 DW_TAG_common_block entries. Also create a struct common_block
17179 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
17180 is used to sepate the common blocks name namespace from regular
17181 variable names. */
c906108c
SS
17182
17183static void
e7c27a73 17184read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17185{
0971de02
TT
17186 struct attribute *attr;
17187
17188 attr = dwarf2_attr (die, DW_AT_location, cu);
17189 if (attr)
17190 {
17191 /* Support the .debug_loc offsets. */
17192 if (attr_form_is_block (attr))
17193 {
17194 /* Ok. */
17195 }
17196 else if (attr_form_is_section_offset (attr))
17197 {
17198 dwarf2_complex_location_expr_complaint ();
17199 attr = NULL;
17200 }
17201 else
17202 {
17203 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17204 "common block member");
17205 attr = NULL;
17206 }
17207 }
17208
639d11d3 17209 if (die->child != NULL)
c906108c 17210 {
518817b3 17211 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
17212 struct die_info *child_die;
17213 size_t n_entries = 0, size;
17214 struct common_block *common_block;
17215 struct symbol *sym;
74ac6d43 17216
4357ac6c
TT
17217 for (child_die = die->child;
17218 child_die && child_die->tag;
17219 child_die = sibling_die (child_die))
17220 ++n_entries;
17221
17222 size = (sizeof (struct common_block)
17223 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
17224 common_block
17225 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17226 size);
4357ac6c
TT
17227 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17228 common_block->n_entries = 0;
17229
17230 for (child_die = die->child;
17231 child_die && child_die->tag;
17232 child_die = sibling_die (child_die))
17233 {
17234 /* Create the symbol in the DW_TAG_common_block block in the current
17235 symbol scope. */
e7c27a73 17236 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
17237 if (sym != NULL)
17238 {
17239 struct attribute *member_loc;
17240
17241 common_block->contents[common_block->n_entries++] = sym;
17242
17243 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17244 cu);
17245 if (member_loc)
17246 {
17247 /* GDB has handled this for a long time, but it is
17248 not specified by DWARF. It seems to have been
17249 emitted by gfortran at least as recently as:
17250 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17251 complaint (&symfile_complaints,
17252 _("Variable in common block has "
17253 "DW_AT_data_member_location "
9d8780f0
SM
17254 "- DIE at %s [in module %s]"),
17255 sect_offset_str (child_die->sect_off),
518817b3 17256 objfile_name (objfile));
0971de02
TT
17257
17258 if (attr_form_is_section_offset (member_loc))
17259 dwarf2_complex_location_expr_complaint ();
17260 else if (attr_form_is_constant (member_loc)
17261 || attr_form_is_block (member_loc))
17262 {
17263 if (attr)
17264 mark_common_block_symbol_computed (sym, die, attr,
17265 member_loc, cu);
17266 }
17267 else
17268 dwarf2_complex_location_expr_complaint ();
17269 }
17270 }
c906108c 17271 }
4357ac6c
TT
17272
17273 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17274 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
17275 }
17276}
17277
0114d602 17278/* Create a type for a C++ namespace. */
d9fa45fe 17279
0114d602
DJ
17280static struct type *
17281read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 17282{
518817b3 17283 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17284 const char *previous_prefix, *name;
9219021c 17285 int is_anonymous;
0114d602
DJ
17286 struct type *type;
17287
17288 /* For extensions, reuse the type of the original namespace. */
17289 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17290 {
17291 struct die_info *ext_die;
17292 struct dwarf2_cu *ext_cu = cu;
9a619af0 17293
0114d602
DJ
17294 ext_die = dwarf2_extension (die, &ext_cu);
17295 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
17296
17297 /* EXT_CU may not be the same as CU.
02142a6c 17298 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
17299 return set_die_type (die, type, cu);
17300 }
9219021c 17301
e142c38c 17302 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
17303
17304 /* Now build the name of the current namespace. */
17305
0114d602
DJ
17306 previous_prefix = determine_prefix (die, cu);
17307 if (previous_prefix[0] != '\0')
17308 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 17309 previous_prefix, name, 0, cu);
0114d602
DJ
17310
17311 /* Create the type. */
19f392bc 17312 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602
DJ
17313 TYPE_TAG_NAME (type) = TYPE_NAME (type);
17314
60531b24 17315 return set_die_type (die, type, cu);
0114d602
DJ
17316}
17317
22cee43f 17318/* Read a namespace scope. */
0114d602
DJ
17319
17320static void
17321read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17322{
518817b3 17323 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17324 int is_anonymous;
9219021c 17325
5c4e30ca
DC
17326 /* Add a symbol associated to this if we haven't seen the namespace
17327 before. Also, add a using directive if it's an anonymous
17328 namespace. */
9219021c 17329
f2f0e013 17330 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
17331 {
17332 struct type *type;
17333
0114d602 17334 type = read_type_die (die, cu);
e7c27a73 17335 new_symbol (die, type, cu);
5c4e30ca 17336
e8e80198 17337 namespace_name (die, &is_anonymous, cu);
5c4e30ca 17338 if (is_anonymous)
0114d602
DJ
17339 {
17340 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 17341
eb1e02fd 17342 std::vector<const char *> excludes;
22cee43f
PMR
17343 add_using_directive (using_directives (cu->language),
17344 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 17345 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 17346 }
5c4e30ca 17347 }
9219021c 17348
639d11d3 17349 if (die->child != NULL)
d9fa45fe 17350 {
639d11d3 17351 struct die_info *child_die = die->child;
6e70227d 17352
d9fa45fe
DC
17353 while (child_die && child_die->tag)
17354 {
e7c27a73 17355 process_die (child_die, cu);
d9fa45fe
DC
17356 child_die = sibling_die (child_die);
17357 }
17358 }
38d518c9
EZ
17359}
17360
f55ee35c
JK
17361/* Read a Fortran module as type. This DIE can be only a declaration used for
17362 imported module. Still we need that type as local Fortran "use ... only"
17363 declaration imports depend on the created type in determine_prefix. */
17364
17365static struct type *
17366read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17367{
518817b3 17368 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 17369 const char *module_name;
f55ee35c
JK
17370 struct type *type;
17371
17372 module_name = dwarf2_name (die, cu);
17373 if (!module_name)
3e43a32a 17374 complaint (&symfile_complaints,
9d8780f0
SM
17375 _("DW_TAG_module has no name, offset %s"),
17376 sect_offset_str (die->sect_off));
19f392bc 17377 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c
JK
17378
17379 /* determine_prefix uses TYPE_TAG_NAME. */
17380 TYPE_TAG_NAME (type) = TYPE_NAME (type);
17381
17382 return set_die_type (die, type, cu);
17383}
17384
5d7cb8df
JK
17385/* Read a Fortran module. */
17386
17387static void
17388read_module (struct die_info *die, struct dwarf2_cu *cu)
17389{
17390 struct die_info *child_die = die->child;
530e8392
KB
17391 struct type *type;
17392
17393 type = read_type_die (die, cu);
17394 new_symbol (die, type, cu);
5d7cb8df 17395
5d7cb8df
JK
17396 while (child_die && child_die->tag)
17397 {
17398 process_die (child_die, cu);
17399 child_die = sibling_die (child_die);
17400 }
17401}
17402
38d518c9
EZ
17403/* Return the name of the namespace represented by DIE. Set
17404 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17405 namespace. */
17406
17407static const char *
e142c38c 17408namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
17409{
17410 struct die_info *current_die;
17411 const char *name = NULL;
17412
17413 /* Loop through the extensions until we find a name. */
17414
17415 for (current_die = die;
17416 current_die != NULL;
f2f0e013 17417 current_die = dwarf2_extension (die, &cu))
38d518c9 17418 {
96553a0c
DE
17419 /* We don't use dwarf2_name here so that we can detect the absence
17420 of a name -> anonymous namespace. */
7d45c7c3 17421 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17422
38d518c9
EZ
17423 if (name != NULL)
17424 break;
17425 }
17426
17427 /* Is it an anonymous namespace? */
17428
17429 *is_anonymous = (name == NULL);
17430 if (*is_anonymous)
2b1dbab0 17431 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17432
17433 return name;
d9fa45fe
DC
17434}
17435
c906108c
SS
17436/* Extract all information from a DW_TAG_pointer_type DIE and add to
17437 the user defined type vector. */
17438
f792889a 17439static struct type *
e7c27a73 17440read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17441{
518817b3
SM
17442 struct gdbarch *gdbarch
17443 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 17444 struct comp_unit_head *cu_header = &cu->header;
c906108c 17445 struct type *type;
8b2dbe47
KB
17446 struct attribute *attr_byte_size;
17447 struct attribute *attr_address_class;
17448 int byte_size, addr_class;
7e314c57
JK
17449 struct type *target_type;
17450
17451 target_type = die_type (die, cu);
c906108c 17452
7e314c57
JK
17453 /* The die_type call above may have already set the type for this DIE. */
17454 type = get_die_type (die, cu);
17455 if (type)
17456 return type;
17457
17458 type = lookup_pointer_type (target_type);
8b2dbe47 17459
e142c38c 17460 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
17461 if (attr_byte_size)
17462 byte_size = DW_UNSND (attr_byte_size);
c906108c 17463 else
8b2dbe47
KB
17464 byte_size = cu_header->addr_size;
17465
e142c38c 17466 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
17467 if (attr_address_class)
17468 addr_class = DW_UNSND (attr_address_class);
17469 else
17470 addr_class = DW_ADDR_none;
17471
17472 /* If the pointer size or address class is different than the
17473 default, create a type variant marked as such and set the
17474 length accordingly. */
17475 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 17476 {
5e2b427d 17477 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
17478 {
17479 int type_flags;
17480
849957d9 17481 type_flags = gdbarch_address_class_type_flags
5e2b427d 17482 (gdbarch, byte_size, addr_class);
876cecd0
TT
17483 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17484 == 0);
8b2dbe47
KB
17485 type = make_type_with_address_space (type, type_flags);
17486 }
17487 else if (TYPE_LENGTH (type) != byte_size)
17488 {
3e43a32a
MS
17489 complaint (&symfile_complaints,
17490 _("invalid pointer size %d"), byte_size);
8b2dbe47 17491 }
6e70227d 17492 else
9a619af0
MS
17493 {
17494 /* Should we also complain about unhandled address classes? */
17495 }
c906108c 17496 }
8b2dbe47
KB
17497
17498 TYPE_LENGTH (type) = byte_size;
f792889a 17499 return set_die_type (die, type, cu);
c906108c
SS
17500}
17501
17502/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17503 the user defined type vector. */
17504
f792889a 17505static struct type *
e7c27a73 17506read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17507{
17508 struct type *type;
17509 struct type *to_type;
17510 struct type *domain;
17511
e7c27a73
DJ
17512 to_type = die_type (die, cu);
17513 domain = die_containing_type (die, cu);
0d5de010 17514
7e314c57
JK
17515 /* The calls above may have already set the type for this DIE. */
17516 type = get_die_type (die, cu);
17517 if (type)
17518 return type;
17519
0d5de010
DJ
17520 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17521 type = lookup_methodptr_type (to_type);
7078baeb
TT
17522 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17523 {
518817b3
SM
17524 struct type *new_type
17525 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
17526
17527 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17528 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17529 TYPE_VARARGS (to_type));
17530 type = lookup_methodptr_type (new_type);
17531 }
0d5de010
DJ
17532 else
17533 type = lookup_memberptr_type (to_type, domain);
c906108c 17534
f792889a 17535 return set_die_type (die, type, cu);
c906108c
SS
17536}
17537
4297a3f0 17538/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17539 the user defined type vector. */
17540
f792889a 17541static struct type *
4297a3f0
AV
17542read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17543 enum type_code refcode)
c906108c 17544{
e7c27a73 17545 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17546 struct type *type, *target_type;
c906108c
SS
17547 struct attribute *attr;
17548
4297a3f0
AV
17549 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17550
7e314c57
JK
17551 target_type = die_type (die, cu);
17552
17553 /* The die_type call above may have already set the type for this DIE. */
17554 type = get_die_type (die, cu);
17555 if (type)
17556 return type;
17557
4297a3f0 17558 type = lookup_reference_type (target_type, refcode);
e142c38c 17559 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17560 if (attr)
17561 {
17562 TYPE_LENGTH (type) = DW_UNSND (attr);
17563 }
17564 else
17565 {
107d2387 17566 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17567 }
f792889a 17568 return set_die_type (die, type, cu);
c906108c
SS
17569}
17570
cf363f18
MW
17571/* Add the given cv-qualifiers to the element type of the array. GCC
17572 outputs DWARF type qualifiers that apply to an array, not the
17573 element type. But GDB relies on the array element type to carry
17574 the cv-qualifiers. This mimics section 6.7.3 of the C99
17575 specification. */
17576
17577static struct type *
17578add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17579 struct type *base_type, int cnst, int voltl)
17580{
17581 struct type *el_type, *inner_array;
17582
17583 base_type = copy_type (base_type);
17584 inner_array = base_type;
17585
17586 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17587 {
17588 TYPE_TARGET_TYPE (inner_array) =
17589 copy_type (TYPE_TARGET_TYPE (inner_array));
17590 inner_array = TYPE_TARGET_TYPE (inner_array);
17591 }
17592
17593 el_type = TYPE_TARGET_TYPE (inner_array);
17594 cnst |= TYPE_CONST (el_type);
17595 voltl |= TYPE_VOLATILE (el_type);
17596 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17597
17598 return set_die_type (die, base_type, cu);
17599}
17600
f792889a 17601static struct type *
e7c27a73 17602read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17603{
f792889a 17604 struct type *base_type, *cv_type;
c906108c 17605
e7c27a73 17606 base_type = die_type (die, cu);
7e314c57
JK
17607
17608 /* The die_type call above may have already set the type for this DIE. */
17609 cv_type = get_die_type (die, cu);
17610 if (cv_type)
17611 return cv_type;
17612
2f608a3a
KW
17613 /* In case the const qualifier is applied to an array type, the element type
17614 is so qualified, not the array type (section 6.7.3 of C99). */
17615 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 17616 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17617
f792889a
DJ
17618 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17619 return set_die_type (die, cv_type, cu);
c906108c
SS
17620}
17621
f792889a 17622static struct type *
e7c27a73 17623read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17624{
f792889a 17625 struct type *base_type, *cv_type;
c906108c 17626
e7c27a73 17627 base_type = die_type (die, cu);
7e314c57
JK
17628
17629 /* The die_type call above may have already set the type for this DIE. */
17630 cv_type = get_die_type (die, cu);
17631 if (cv_type)
17632 return cv_type;
17633
cf363f18
MW
17634 /* In case the volatile qualifier is applied to an array type, the
17635 element type is so qualified, not the array type (section 6.7.3
17636 of C99). */
17637 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17638 return add_array_cv_type (die, cu, base_type, 0, 1);
17639
f792889a
DJ
17640 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17641 return set_die_type (die, cv_type, cu);
c906108c
SS
17642}
17643
06d66ee9
TT
17644/* Handle DW_TAG_restrict_type. */
17645
17646static struct type *
17647read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17648{
17649 struct type *base_type, *cv_type;
17650
17651 base_type = die_type (die, cu);
17652
17653 /* The die_type call above may have already set the type for this DIE. */
17654 cv_type = get_die_type (die, cu);
17655 if (cv_type)
17656 return cv_type;
17657
17658 cv_type = make_restrict_type (base_type);
17659 return set_die_type (die, cv_type, cu);
17660}
17661
a2c2acaf
MW
17662/* Handle DW_TAG_atomic_type. */
17663
17664static struct type *
17665read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17666{
17667 struct type *base_type, *cv_type;
17668
17669 base_type = die_type (die, cu);
17670
17671 /* The die_type call above may have already set the type for this DIE. */
17672 cv_type = get_die_type (die, cu);
17673 if (cv_type)
17674 return cv_type;
17675
17676 cv_type = make_atomic_type (base_type);
17677 return set_die_type (die, cv_type, cu);
17678}
17679
c906108c
SS
17680/* Extract all information from a DW_TAG_string_type DIE and add to
17681 the user defined type vector. It isn't really a user defined type,
17682 but it behaves like one, with other DIE's using an AT_user_def_type
17683 attribute to reference it. */
17684
f792889a 17685static struct type *
e7c27a73 17686read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17687{
518817b3 17688 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 17689 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17690 struct type *type, *range_type, *index_type, *char_type;
17691 struct attribute *attr;
17692 unsigned int length;
17693
e142c38c 17694 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
17695 if (attr)
17696 {
17697 length = DW_UNSND (attr);
17698 }
17699 else
17700 {
0963b4bd 17701 /* Check for the DW_AT_byte_size attribute. */
e142c38c 17702 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
17703 if (attr)
17704 {
17705 length = DW_UNSND (attr);
17706 }
17707 else
17708 {
17709 length = 1;
17710 }
c906108c 17711 }
6ccb9162 17712
46bf5051 17713 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17714 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
17715 char_type = language_string_char_type (cu->language_defn, gdbarch);
17716 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17717
f792889a 17718 return set_die_type (die, type, cu);
c906108c
SS
17719}
17720
4d804846
JB
17721/* Assuming that DIE corresponds to a function, returns nonzero
17722 if the function is prototyped. */
17723
17724static int
17725prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17726{
17727 struct attribute *attr;
17728
17729 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17730 if (attr && (DW_UNSND (attr) != 0))
17731 return 1;
17732
17733 /* The DWARF standard implies that the DW_AT_prototyped attribute
17734 is only meaninful for C, but the concept also extends to other
17735 languages that allow unprototyped functions (Eg: Objective C).
17736 For all other languages, assume that functions are always
17737 prototyped. */
17738 if (cu->language != language_c
17739 && cu->language != language_objc
17740 && cu->language != language_opencl)
17741 return 1;
17742
17743 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17744 prototyped and unprototyped functions; default to prototyped,
17745 since that is more common in modern code (and RealView warns
17746 about unprototyped functions). */
17747 if (producer_is_realview (cu->producer))
17748 return 1;
17749
17750 return 0;
17751}
17752
c906108c
SS
17753/* Handle DIES due to C code like:
17754
17755 struct foo
c5aa993b
JM
17756 {
17757 int (*funcp)(int a, long l);
17758 int b;
17759 };
c906108c 17760
0963b4bd 17761 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17762
f792889a 17763static struct type *
e7c27a73 17764read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17765{
518817b3 17766 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17767 struct type *type; /* Type that this function returns. */
17768 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17769 struct attribute *attr;
17770
e7c27a73 17771 type = die_type (die, cu);
7e314c57
JK
17772
17773 /* The die_type call above may have already set the type for this DIE. */
17774 ftype = get_die_type (die, cu);
17775 if (ftype)
17776 return ftype;
17777
0c8b41f1 17778 ftype = lookup_function_type (type);
c906108c 17779
4d804846 17780 if (prototyped_function_p (die, cu))
a6c727b2 17781 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17782
c055b101
CV
17783 /* Store the calling convention in the type if it's available in
17784 the subroutine die. Otherwise set the calling convention to
17785 the default value DW_CC_normal. */
17786 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
17787 if (attr)
17788 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17789 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17790 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17791 else
17792 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17793
743649fd
MW
17794 /* Record whether the function returns normally to its caller or not
17795 if the DWARF producer set that information. */
17796 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17797 if (attr && (DW_UNSND (attr) != 0))
17798 TYPE_NO_RETURN (ftype) = 1;
17799
76c10ea2
GM
17800 /* We need to add the subroutine type to the die immediately so
17801 we don't infinitely recurse when dealing with parameters
0963b4bd 17802 declared as the same subroutine type. */
76c10ea2 17803 set_die_type (die, ftype, cu);
6e70227d 17804
639d11d3 17805 if (die->child != NULL)
c906108c 17806 {
bb5ed363 17807 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17808 struct die_info *child_die;
8072405b 17809 int nparams, iparams;
c906108c
SS
17810
17811 /* Count the number of parameters.
17812 FIXME: GDB currently ignores vararg functions, but knows about
17813 vararg member functions. */
8072405b 17814 nparams = 0;
639d11d3 17815 child_die = die->child;
c906108c
SS
17816 while (child_die && child_die->tag)
17817 {
17818 if (child_die->tag == DW_TAG_formal_parameter)
17819 nparams++;
17820 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17821 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17822 child_die = sibling_die (child_die);
17823 }
17824
17825 /* Allocate storage for parameters and fill them in. */
17826 TYPE_NFIELDS (ftype) = nparams;
17827 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17828 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17829
8072405b
JK
17830 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17831 even if we error out during the parameters reading below. */
17832 for (iparams = 0; iparams < nparams; iparams++)
17833 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17834
17835 iparams = 0;
639d11d3 17836 child_die = die->child;
c906108c
SS
17837 while (child_die && child_die->tag)
17838 {
17839 if (child_die->tag == DW_TAG_formal_parameter)
17840 {
3ce3b1ba
PA
17841 struct type *arg_type;
17842
17843 /* DWARF version 2 has no clean way to discern C++
17844 static and non-static member functions. G++ helps
17845 GDB by marking the first parameter for non-static
17846 member functions (which is the this pointer) as
17847 artificial. We pass this information to
17848 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17849
17850 DWARF version 3 added DW_AT_object_pointer, which GCC
17851 4.5 does not yet generate. */
e142c38c 17852 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
17853 if (attr)
17854 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17855 else
9c37b5ae 17856 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17857 arg_type = die_type (child_die, cu);
17858
17859 /* RealView does not mark THIS as const, which the testsuite
17860 expects. GCC marks THIS as const in method definitions,
17861 but not in the class specifications (GCC PR 43053). */
17862 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17863 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17864 {
17865 int is_this = 0;
17866 struct dwarf2_cu *arg_cu = cu;
17867 const char *name = dwarf2_name (child_die, cu);
17868
17869 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17870 if (attr)
17871 {
17872 /* If the compiler emits this, use it. */
17873 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17874 is_this = 1;
17875 }
17876 else if (name && strcmp (name, "this") == 0)
17877 /* Function definitions will have the argument names. */
17878 is_this = 1;
17879 else if (name == NULL && iparams == 0)
17880 /* Declarations may not have the names, so like
17881 elsewhere in GDB, assume an artificial first
17882 argument is "this". */
17883 is_this = 1;
17884
17885 if (is_this)
17886 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17887 arg_type, 0);
17888 }
17889
17890 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17891 iparams++;
17892 }
17893 child_die = sibling_die (child_die);
17894 }
17895 }
17896
76c10ea2 17897 return ftype;
c906108c
SS
17898}
17899
f792889a 17900static struct type *
e7c27a73 17901read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17902{
518817b3 17903 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17904 const char *name = NULL;
3c8e0968 17905 struct type *this_type, *target_type;
c906108c 17906
94af9270 17907 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17908 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17909 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17910 set_die_type (die, this_type, cu);
3c8e0968
DE
17911 target_type = die_type (die, cu);
17912 if (target_type != this_type)
17913 TYPE_TARGET_TYPE (this_type) = target_type;
17914 else
17915 {
17916 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17917 spec and cause infinite loops in GDB. */
17918 complaint (&symfile_complaints,
17919 _("Self-referential DW_TAG_typedef "
9d8780f0
SM
17920 "- DIE at %s [in module %s]"),
17921 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17922 TYPE_TARGET_TYPE (this_type) = NULL;
17923 }
f792889a 17924 return this_type;
c906108c
SS
17925}
17926
9b790ce7
UW
17927/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17928 (which may be different from NAME) to the architecture back-end to allow
17929 it to guess the correct format if necessary. */
17930
17931static struct type *
17932dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17933 const char *name_hint)
17934{
17935 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17936 const struct floatformat **format;
17937 struct type *type;
17938
17939 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17940 if (format)
17941 type = init_float_type (objfile, bits, name, format);
17942 else
77b7c781 17943 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17944
17945 return type;
17946}
17947
c906108c
SS
17948/* Find a representation of a given base type and install
17949 it in the TYPE field of the die. */
17950
f792889a 17951static struct type *
e7c27a73 17952read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17953{
518817b3 17954 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17955 struct type *type;
17956 struct attribute *attr;
19f392bc 17957 int encoding = 0, bits = 0;
15d034d0 17958 const char *name;
c906108c 17959
e142c38c 17960 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
17961 if (attr)
17962 {
17963 encoding = DW_UNSND (attr);
17964 }
e142c38c 17965 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17966 if (attr)
17967 {
19f392bc 17968 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 17969 }
39cbfefa 17970 name = dwarf2_name (die, cu);
6ccb9162 17971 if (!name)
c906108c 17972 {
6ccb9162
UW
17973 complaint (&symfile_complaints,
17974 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 17975 }
6ccb9162
UW
17976
17977 switch (encoding)
c906108c 17978 {
6ccb9162
UW
17979 case DW_ATE_address:
17980 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17981 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17982 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17983 break;
17984 case DW_ATE_boolean:
19f392bc 17985 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17986 break;
17987 case DW_ATE_complex_float:
9b790ce7 17988 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 17989 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17990 break;
17991 case DW_ATE_decimal_float:
19f392bc 17992 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17993 break;
17994 case DW_ATE_float:
9b790ce7 17995 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
17996 break;
17997 case DW_ATE_signed:
19f392bc 17998 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
17999 break;
18000 case DW_ATE_unsigned:
3b2b8fea
TT
18001 if (cu->language == language_fortran
18002 && name
61012eef 18003 && startswith (name, "character("))
19f392bc
UW
18004 type = init_character_type (objfile, bits, 1, name);
18005 else
18006 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
18007 break;
18008 case DW_ATE_signed_char:
6e70227d 18009 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
18010 || cu->language == language_pascal
18011 || cu->language == language_fortran)
19f392bc
UW
18012 type = init_character_type (objfile, bits, 0, name);
18013 else
18014 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
18015 break;
18016 case DW_ATE_unsigned_char:
868a0084 18017 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 18018 || cu->language == language_pascal
c44af4eb
TT
18019 || cu->language == language_fortran
18020 || cu->language == language_rust)
19f392bc
UW
18021 type = init_character_type (objfile, bits, 1, name);
18022 else
18023 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 18024 break;
75079b2b 18025 case DW_ATE_UTF:
53e710ac
PA
18026 {
18027 gdbarch *arch = get_objfile_arch (objfile);
18028
18029 if (bits == 16)
18030 type = builtin_type (arch)->builtin_char16;
18031 else if (bits == 32)
18032 type = builtin_type (arch)->builtin_char32;
18033 else
18034 {
18035 complaint (&symfile_complaints,
18036 _("unsupported DW_ATE_UTF bit size: '%d'"),
18037 bits);
18038 type = init_integer_type (objfile, bits, 1, name);
18039 }
18040 return set_die_type (die, type, cu);
18041 }
75079b2b
TT
18042 break;
18043
6ccb9162
UW
18044 default:
18045 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
18046 dwarf_type_encoding_name (encoding));
77b7c781 18047 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 18048 break;
c906108c 18049 }
6ccb9162 18050
0114d602 18051 if (name && strcmp (name, "char") == 0)
876cecd0 18052 TYPE_NOSIGN (type) = 1;
0114d602 18053
f792889a 18054 return set_die_type (die, type, cu);
c906108c
SS
18055}
18056
80180f79
SA
18057/* Parse dwarf attribute if it's a block, reference or constant and put the
18058 resulting value of the attribute into struct bound_prop.
18059 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18060
18061static int
18062attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18063 struct dwarf2_cu *cu, struct dynamic_prop *prop)
18064{
18065 struct dwarf2_property_baton *baton;
518817b3
SM
18066 struct obstack *obstack
18067 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79
SA
18068
18069 if (attr == NULL || prop == NULL)
18070 return 0;
18071
18072 if (attr_form_is_block (attr))
18073 {
8d749320 18074 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
18075 baton->referenced_type = NULL;
18076 baton->locexpr.per_cu = cu->per_cu;
18077 baton->locexpr.size = DW_BLOCK (attr)->size;
18078 baton->locexpr.data = DW_BLOCK (attr)->data;
18079 prop->data.baton = baton;
18080 prop->kind = PROP_LOCEXPR;
18081 gdb_assert (prop->data.baton != NULL);
18082 }
18083 else if (attr_form_is_ref (attr))
18084 {
18085 struct dwarf2_cu *target_cu = cu;
18086 struct die_info *target_die;
18087 struct attribute *target_attr;
18088
18089 target_die = follow_die_ref (die, attr, &target_cu);
18090 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
18091 if (target_attr == NULL)
18092 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18093 target_cu);
80180f79
SA
18094 if (target_attr == NULL)
18095 return 0;
18096
df25ebbd 18097 switch (target_attr->name)
80180f79 18098 {
df25ebbd
JB
18099 case DW_AT_location:
18100 if (attr_form_is_section_offset (target_attr))
18101 {
8d749320 18102 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
18103 baton->referenced_type = die_type (target_die, target_cu);
18104 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18105 prop->data.baton = baton;
18106 prop->kind = PROP_LOCLIST;
18107 gdb_assert (prop->data.baton != NULL);
18108 }
18109 else if (attr_form_is_block (target_attr))
18110 {
8d749320 18111 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
18112 baton->referenced_type = die_type (target_die, target_cu);
18113 baton->locexpr.per_cu = cu->per_cu;
18114 baton->locexpr.size = DW_BLOCK (target_attr)->size;
18115 baton->locexpr.data = DW_BLOCK (target_attr)->data;
18116 prop->data.baton = baton;
18117 prop->kind = PROP_LOCEXPR;
18118 gdb_assert (prop->data.baton != NULL);
18119 }
18120 else
18121 {
18122 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18123 "dynamic property");
18124 return 0;
18125 }
18126 break;
18127 case DW_AT_data_member_location:
18128 {
18129 LONGEST offset;
18130
18131 if (!handle_data_member_location (target_die, target_cu,
18132 &offset))
18133 return 0;
18134
8d749320 18135 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
18136 baton->referenced_type = read_type_die (target_die->parent,
18137 target_cu);
df25ebbd
JB
18138 baton->offset_info.offset = offset;
18139 baton->offset_info.type = die_type (target_die, target_cu);
18140 prop->data.baton = baton;
18141 prop->kind = PROP_ADDR_OFFSET;
18142 break;
18143 }
80180f79
SA
18144 }
18145 }
18146 else if (attr_form_is_constant (attr))
18147 {
18148 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
18149 prop->kind = PROP_CONST;
18150 }
18151 else
18152 {
18153 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18154 dwarf2_name (die, cu));
18155 return 0;
18156 }
18157
18158 return 1;
18159}
18160
a02abb62
JB
18161/* Read the given DW_AT_subrange DIE. */
18162
f792889a 18163static struct type *
a02abb62
JB
18164read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18165{
4c9ad8c2 18166 struct type *base_type, *orig_base_type;
a02abb62
JB
18167 struct type *range_type;
18168 struct attribute *attr;
729efb13 18169 struct dynamic_prop low, high;
4fae6e18 18170 int low_default_is_valid;
c451ebe5 18171 int high_bound_is_count = 0;
15d034d0 18172 const char *name;
43bbcdc2 18173 LONGEST negative_mask;
e77813c8 18174
4c9ad8c2
TT
18175 orig_base_type = die_type (die, cu);
18176 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18177 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18178 creating the range type, but we use the result of check_typedef
18179 when examining properties of the type. */
18180 base_type = check_typedef (orig_base_type);
a02abb62 18181
7e314c57
JK
18182 /* The die_type call above may have already set the type for this DIE. */
18183 range_type = get_die_type (die, cu);
18184 if (range_type)
18185 return range_type;
18186
729efb13
SA
18187 low.kind = PROP_CONST;
18188 high.kind = PROP_CONST;
18189 high.data.const_val = 0;
18190
4fae6e18
JK
18191 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18192 omitting DW_AT_lower_bound. */
18193 switch (cu->language)
6e70227d 18194 {
4fae6e18
JK
18195 case language_c:
18196 case language_cplus:
729efb13 18197 low.data.const_val = 0;
4fae6e18
JK
18198 low_default_is_valid = 1;
18199 break;
18200 case language_fortran:
729efb13 18201 low.data.const_val = 1;
4fae6e18
JK
18202 low_default_is_valid = 1;
18203 break;
18204 case language_d:
4fae6e18 18205 case language_objc:
c44af4eb 18206 case language_rust:
729efb13 18207 low.data.const_val = 0;
4fae6e18
JK
18208 low_default_is_valid = (cu->header.version >= 4);
18209 break;
18210 case language_ada:
18211 case language_m2:
18212 case language_pascal:
729efb13 18213 low.data.const_val = 1;
4fae6e18
JK
18214 low_default_is_valid = (cu->header.version >= 4);
18215 break;
18216 default:
729efb13 18217 low.data.const_val = 0;
4fae6e18
JK
18218 low_default_is_valid = 0;
18219 break;
a02abb62
JB
18220 }
18221
e142c38c 18222 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 18223 if (attr)
11c1ba78 18224 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
18225 else if (!low_default_is_valid)
18226 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
9d8780f0
SM
18227 "- DIE at %s [in module %s]"),
18228 sect_offset_str (die->sect_off),
518817b3 18229 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 18230
e142c38c 18231 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 18232 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
18233 {
18234 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 18235 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 18236 {
c451ebe5
SA
18237 /* If bounds are constant do the final calculation here. */
18238 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
18239 high.data.const_val = low.data.const_val + high.data.const_val - 1;
18240 else
18241 high_bound_is_count = 1;
c2ff108b 18242 }
e77813c8
PM
18243 }
18244
18245 /* Dwarf-2 specifications explicitly allows to create subrange types
18246 without specifying a base type.
18247 In that case, the base type must be set to the type of
18248 the lower bound, upper bound or count, in that order, if any of these
18249 three attributes references an object that has a type.
18250 If no base type is found, the Dwarf-2 specifications say that
18251 a signed integer type of size equal to the size of an address should
18252 be used.
18253 For the following C code: `extern char gdb_int [];'
18254 GCC produces an empty range DIE.
18255 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 18256 high bound or count are not yet handled by this code. */
e77813c8
PM
18257 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
18258 {
518817b3 18259 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e77813c8
PM
18260 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18261 int addr_size = gdbarch_addr_bit (gdbarch) /8;
18262 struct type *int_type = objfile_type (objfile)->builtin_int;
18263
18264 /* Test "int", "long int", and "long long int" objfile types,
18265 and select the first one having a size above or equal to the
18266 architecture address size. */
18267 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
18268 base_type = int_type;
18269 else
18270 {
18271 int_type = objfile_type (objfile)->builtin_long;
18272 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
18273 base_type = int_type;
18274 else
18275 {
18276 int_type = objfile_type (objfile)->builtin_long_long;
18277 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
18278 base_type = int_type;
18279 }
18280 }
18281 }
a02abb62 18282
dbb9c2b1
JB
18283 /* Normally, the DWARF producers are expected to use a signed
18284 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18285 But this is unfortunately not always the case, as witnessed
18286 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18287 is used instead. To work around that ambiguity, we treat
18288 the bounds as signed, and thus sign-extend their values, when
18289 the base type is signed. */
6e70227d 18290 negative_mask =
66c6502d 18291 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
18292 if (low.kind == PROP_CONST
18293 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18294 low.data.const_val |= negative_mask;
18295 if (high.kind == PROP_CONST
18296 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18297 high.data.const_val |= negative_mask;
43bbcdc2 18298
729efb13 18299 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 18300
c451ebe5
SA
18301 if (high_bound_is_count)
18302 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18303
c2ff108b
JK
18304 /* Ada expects an empty array on no boundary attributes. */
18305 if (attr == NULL && cu->language != language_ada)
729efb13 18306 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 18307
39cbfefa
DJ
18308 name = dwarf2_name (die, cu);
18309 if (name)
18310 TYPE_NAME (range_type) = name;
6e70227d 18311
e142c38c 18312 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
18313 if (attr)
18314 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18315
7e314c57
JK
18316 set_die_type (die, range_type, cu);
18317
18318 /* set_die_type should be already done. */
b4ba55a1
JB
18319 set_descriptive_type (range_type, die, cu);
18320
7e314c57 18321 return range_type;
a02abb62 18322}
6e70227d 18323
f792889a 18324static struct type *
81a17f79
JB
18325read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18326{
18327 struct type *type;
81a17f79 18328
518817b3
SM
18329 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18330 NULL);
0114d602 18331 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 18332
74a2f8ff
JB
18333 /* In Ada, an unspecified type is typically used when the description
18334 of the type is defered to a different unit. When encountering
18335 such a type, we treat it as a stub, and try to resolve it later on,
18336 when needed. */
18337 if (cu->language == language_ada)
18338 TYPE_STUB (type) = 1;
18339
f792889a 18340 return set_die_type (die, type, cu);
81a17f79 18341}
a02abb62 18342
639d11d3
DC
18343/* Read a single die and all its descendents. Set the die's sibling
18344 field to NULL; set other fields in the die correctly, and set all
18345 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18346 location of the info_ptr after reading all of those dies. PARENT
18347 is the parent of the die in question. */
18348
18349static struct die_info *
dee91e82 18350read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18351 const gdb_byte *info_ptr,
18352 const gdb_byte **new_info_ptr,
dee91e82 18353 struct die_info *parent)
639d11d3
DC
18354{
18355 struct die_info *die;
d521ce57 18356 const gdb_byte *cur_ptr;
639d11d3
DC
18357 int has_children;
18358
bf6af496 18359 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
18360 if (die == NULL)
18361 {
18362 *new_info_ptr = cur_ptr;
18363 return NULL;
18364 }
93311388 18365 store_in_ref_table (die, reader->cu);
639d11d3
DC
18366
18367 if (has_children)
bf6af496 18368 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18369 else
18370 {
18371 die->child = NULL;
18372 *new_info_ptr = cur_ptr;
18373 }
18374
18375 die->sibling = NULL;
18376 die->parent = parent;
18377 return die;
18378}
18379
18380/* Read a die, all of its descendents, and all of its siblings; set
18381 all of the fields of all of the dies correctly. Arguments are as
18382 in read_die_and_children. */
18383
18384static struct die_info *
bf6af496 18385read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18386 const gdb_byte *info_ptr,
18387 const gdb_byte **new_info_ptr,
bf6af496 18388 struct die_info *parent)
639d11d3
DC
18389{
18390 struct die_info *first_die, *last_sibling;
d521ce57 18391 const gdb_byte *cur_ptr;
639d11d3 18392
c906108c 18393 cur_ptr = info_ptr;
639d11d3
DC
18394 first_die = last_sibling = NULL;
18395
18396 while (1)
c906108c 18397 {
639d11d3 18398 struct die_info *die
dee91e82 18399 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18400
1d325ec1 18401 if (die == NULL)
c906108c 18402 {
639d11d3
DC
18403 *new_info_ptr = cur_ptr;
18404 return first_die;
c906108c 18405 }
1d325ec1
DJ
18406
18407 if (!first_die)
18408 first_die = die;
c906108c 18409 else
1d325ec1
DJ
18410 last_sibling->sibling = die;
18411
18412 last_sibling = die;
c906108c 18413 }
c906108c
SS
18414}
18415
bf6af496
DE
18416/* Read a die, all of its descendents, and all of its siblings; set
18417 all of the fields of all of the dies correctly. Arguments are as
18418 in read_die_and_children.
18419 This the main entry point for reading a DIE and all its children. */
18420
18421static struct die_info *
18422read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18423 const gdb_byte *info_ptr,
18424 const gdb_byte **new_info_ptr,
bf6af496
DE
18425 struct die_info *parent)
18426{
18427 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18428 new_info_ptr, parent);
18429
b4f54984 18430 if (dwarf_die_debug)
bf6af496
DE
18431 {
18432 fprintf_unfiltered (gdb_stdlog,
18433 "Read die from %s@0x%x of %s:\n",
a32a8923 18434 get_section_name (reader->die_section),
bf6af496
DE
18435 (unsigned) (info_ptr - reader->die_section->buffer),
18436 bfd_get_filename (reader->abfd));
b4f54984 18437 dump_die (die, dwarf_die_debug);
bf6af496
DE
18438 }
18439
18440 return die;
18441}
18442
3019eac3
DE
18443/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18444 attributes.
18445 The caller is responsible for filling in the extra attributes
18446 and updating (*DIEP)->num_attrs.
18447 Set DIEP to point to a newly allocated die with its information,
18448 except for its child, sibling, and parent fields.
18449 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 18450
d521ce57 18451static const gdb_byte *
3019eac3 18452read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18453 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 18454 int *has_children, int num_extra_attrs)
93311388 18455{
b64f50a1 18456 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18457 struct abbrev_info *abbrev;
18458 struct die_info *die;
18459 struct dwarf2_cu *cu = reader->cu;
18460 bfd *abfd = reader->abfd;
18461
9c541725 18462 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18463 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18464 info_ptr += bytes_read;
18465 if (!abbrev_number)
18466 {
18467 *diep = NULL;
18468 *has_children = 0;
18469 return info_ptr;
18470 }
18471
685af9cd 18472 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18473 if (!abbrev)
348e048f
DE
18474 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18475 abbrev_number,
18476 bfd_get_filename (abfd));
18477
3019eac3 18478 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18479 die->sect_off = sect_off;
93311388
DE
18480 die->tag = abbrev->tag;
18481 die->abbrev = abbrev_number;
18482
3019eac3
DE
18483 /* Make the result usable.
18484 The caller needs to update num_attrs after adding the extra
18485 attributes. */
93311388
DE
18486 die->num_attrs = abbrev->num_attrs;
18487
18488 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
18489 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18490 info_ptr);
93311388
DE
18491
18492 *diep = die;
18493 *has_children = abbrev->has_children;
18494 return info_ptr;
18495}
18496
3019eac3
DE
18497/* Read a die and all its attributes.
18498 Set DIEP to point to a newly allocated die with its information,
18499 except for its child, sibling, and parent fields.
18500 Set HAS_CHILDREN to tell whether the die has children or not. */
18501
d521ce57 18502static const gdb_byte *
3019eac3 18503read_full_die (const struct die_reader_specs *reader,
d521ce57 18504 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
18505 int *has_children)
18506{
d521ce57 18507 const gdb_byte *result;
bf6af496
DE
18508
18509 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18510
b4f54984 18511 if (dwarf_die_debug)
bf6af496
DE
18512 {
18513 fprintf_unfiltered (gdb_stdlog,
18514 "Read die from %s@0x%x of %s:\n",
a32a8923 18515 get_section_name (reader->die_section),
bf6af496
DE
18516 (unsigned) (info_ptr - reader->die_section->buffer),
18517 bfd_get_filename (reader->abfd));
b4f54984 18518 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18519 }
18520
18521 return result;
3019eac3 18522}
433df2d4
DE
18523\f
18524/* Abbreviation tables.
3019eac3 18525
433df2d4 18526 In DWARF version 2, the description of the debugging information is
c906108c
SS
18527 stored in a separate .debug_abbrev section. Before we read any
18528 dies from a section we read in all abbreviations and install them
433df2d4
DE
18529 in a hash table. */
18530
18531/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18532
685af9cd
TT
18533struct abbrev_info *
18534abbrev_table::alloc_abbrev ()
433df2d4
DE
18535{
18536 struct abbrev_info *abbrev;
18537
685af9cd 18538 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
433df2d4 18539 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 18540
433df2d4
DE
18541 return abbrev;
18542}
18543
18544/* Add an abbreviation to the table. */
c906108c 18545
685af9cd
TT
18546void
18547abbrev_table::add_abbrev (unsigned int abbrev_number,
18548 struct abbrev_info *abbrev)
433df2d4
DE
18549{
18550 unsigned int hash_number;
18551
18552 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768
YQ
18553 abbrev->next = m_abbrevs[hash_number];
18554 m_abbrevs[hash_number] = abbrev;
433df2d4 18555}
dee91e82 18556
433df2d4
DE
18557/* Look up an abbrev in the table.
18558 Returns NULL if the abbrev is not found. */
18559
685af9cd
TT
18560struct abbrev_info *
18561abbrev_table::lookup_abbrev (unsigned int abbrev_number)
c906108c 18562{
433df2d4
DE
18563 unsigned int hash_number;
18564 struct abbrev_info *abbrev;
18565
18566 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768 18567 abbrev = m_abbrevs[hash_number];
433df2d4
DE
18568
18569 while (abbrev)
18570 {
18571 if (abbrev->number == abbrev_number)
18572 return abbrev;
18573 abbrev = abbrev->next;
18574 }
18575 return NULL;
18576}
18577
18578/* Read in an abbrev table. */
18579
685af9cd 18580static abbrev_table_up
ed2dc618
SM
18581abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18582 struct dwarf2_section_info *section,
9c541725 18583 sect_offset sect_off)
433df2d4
DE
18584{
18585 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 18586 bfd *abfd = get_section_bfd_owner (section);
d521ce57 18587 const gdb_byte *abbrev_ptr;
c906108c
SS
18588 struct abbrev_info *cur_abbrev;
18589 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 18590 unsigned int abbrev_form;
f3dd6933
DJ
18591 struct attr_abbrev *cur_attrs;
18592 unsigned int allocated_attrs;
c906108c 18593
685af9cd 18594 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
c906108c 18595
433df2d4 18596 dwarf2_read_section (objfile, section);
9c541725 18597 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
18598 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18599 abbrev_ptr += bytes_read;
18600
f3dd6933 18601 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 18602 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 18603
0963b4bd 18604 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
18605 while (abbrev_number)
18606 {
685af9cd 18607 cur_abbrev = abbrev_table->alloc_abbrev ();
c906108c
SS
18608
18609 /* read in abbrev header */
18610 cur_abbrev->number = abbrev_number;
aead7601
SM
18611 cur_abbrev->tag
18612 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
18613 abbrev_ptr += bytes_read;
18614 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18615 abbrev_ptr += 1;
18616
18617 /* now read in declarations */
22d2f3ab 18618 for (;;)
c906108c 18619 {
43988095
JK
18620 LONGEST implicit_const;
18621
22d2f3ab
JK
18622 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18623 abbrev_ptr += bytes_read;
18624 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18625 abbrev_ptr += bytes_read;
43988095
JK
18626 if (abbrev_form == DW_FORM_implicit_const)
18627 {
18628 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18629 &bytes_read);
18630 abbrev_ptr += bytes_read;
18631 }
18632 else
18633 {
18634 /* Initialize it due to a false compiler warning. */
18635 implicit_const = -1;
18636 }
22d2f3ab
JK
18637
18638 if (abbrev_name == 0)
18639 break;
18640
f3dd6933 18641 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 18642 {
f3dd6933
DJ
18643 allocated_attrs += ATTR_ALLOC_CHUNK;
18644 cur_attrs
224c3ddb 18645 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 18646 }
ae038cb0 18647
aead7601
SM
18648 cur_attrs[cur_abbrev->num_attrs].name
18649 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 18650 cur_attrs[cur_abbrev->num_attrs].form
aead7601 18651 = (enum dwarf_form) abbrev_form;
43988095 18652 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 18653 ++cur_abbrev->num_attrs;
c906108c
SS
18654 }
18655
8d749320
SM
18656 cur_abbrev->attrs =
18657 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18658 cur_abbrev->num_attrs);
f3dd6933
DJ
18659 memcpy (cur_abbrev->attrs, cur_attrs,
18660 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18661
685af9cd 18662 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
c906108c
SS
18663
18664 /* Get next abbreviation.
18665 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18666 always properly terminated with an abbrev number of 0.
18667 Exit loop if we encounter an abbreviation which we have
18668 already read (which means we are about to read the abbreviations
18669 for the next compile unit) or if the end of the abbreviation
18670 table is reached. */
433df2d4 18671 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18672 break;
18673 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18674 abbrev_ptr += bytes_read;
685af9cd 18675 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
c906108c
SS
18676 break;
18677 }
f3dd6933
DJ
18678
18679 xfree (cur_attrs);
433df2d4 18680 return abbrev_table;
c906108c
SS
18681}
18682
72bf9492
DJ
18683/* Returns nonzero if TAG represents a type that we might generate a partial
18684 symbol for. */
18685
18686static int
18687is_type_tag_for_partial (int tag)
18688{
18689 switch (tag)
18690 {
18691#if 0
18692 /* Some types that would be reasonable to generate partial symbols for,
18693 that we don't at present. */
18694 case DW_TAG_array_type:
18695 case DW_TAG_file_type:
18696 case DW_TAG_ptr_to_member_type:
18697 case DW_TAG_set_type:
18698 case DW_TAG_string_type:
18699 case DW_TAG_subroutine_type:
18700#endif
18701 case DW_TAG_base_type:
18702 case DW_TAG_class_type:
680b30c7 18703 case DW_TAG_interface_type:
72bf9492
DJ
18704 case DW_TAG_enumeration_type:
18705 case DW_TAG_structure_type:
18706 case DW_TAG_subrange_type:
18707 case DW_TAG_typedef:
18708 case DW_TAG_union_type:
18709 return 1;
18710 default:
18711 return 0;
18712 }
18713}
18714
18715/* Load all DIEs that are interesting for partial symbols into memory. */
18716
18717static struct partial_die_info *
dee91e82 18718load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18719 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18720{
dee91e82 18721 struct dwarf2_cu *cu = reader->cu;
518817b3 18722 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18723 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18724 unsigned int bytes_read;
5afb4e99 18725 unsigned int load_all = 0;
72bf9492
DJ
18726 int nesting_level = 1;
18727
18728 parent_die = NULL;
18729 last_die = NULL;
18730
7adf1e79
DE
18731 gdb_assert (cu->per_cu != NULL);
18732 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18733 load_all = 1;
18734
72bf9492
DJ
18735 cu->partial_dies
18736 = htab_create_alloc_ex (cu->header.length / 12,
18737 partial_die_hash,
18738 partial_die_eq,
18739 NULL,
18740 &cu->comp_unit_obstack,
18741 hashtab_obstack_allocate,
18742 dummy_obstack_deallocate);
18743
72bf9492
DJ
18744 while (1)
18745 {
685af9cd 18746 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18747
18748 /* A NULL abbrev means the end of a series of children. */
18749 if (abbrev == NULL)
18750 {
18751 if (--nesting_level == 0)
cd9983dd
YQ
18752 return first_die;
18753
72bf9492
DJ
18754 info_ptr += bytes_read;
18755 last_die = parent_die;
18756 parent_die = parent_die->die_parent;
18757 continue;
18758 }
18759
98bfdba5
PA
18760 /* Check for template arguments. We never save these; if
18761 they're seen, we just mark the parent, and go on our way. */
18762 if (parent_die != NULL
18763 && cu->language == language_cplus
18764 && (abbrev->tag == DW_TAG_template_type_param
18765 || abbrev->tag == DW_TAG_template_value_param))
18766 {
18767 parent_die->has_template_arguments = 1;
18768
18769 if (!load_all)
18770 {
18771 /* We don't need a partial DIE for the template argument. */
dee91e82 18772 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18773 continue;
18774 }
18775 }
18776
0d99eb77 18777 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18778 Skip their other children. */
18779 if (!load_all
18780 && cu->language == language_cplus
18781 && parent_die != NULL
18782 && parent_die->tag == DW_TAG_subprogram)
18783 {
dee91e82 18784 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18785 continue;
18786 }
18787
5afb4e99
DJ
18788 /* Check whether this DIE is interesting enough to save. Normally
18789 we would not be interested in members here, but there may be
18790 later variables referencing them via DW_AT_specification (for
18791 static members). */
18792 if (!load_all
18793 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18794 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18795 && abbrev->tag != DW_TAG_enumerator
18796 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18797 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18798 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18799 && abbrev->tag != DW_TAG_variable
5afb4e99 18800 && abbrev->tag != DW_TAG_namespace
f55ee35c 18801 && abbrev->tag != DW_TAG_module
95554aad 18802 && abbrev->tag != DW_TAG_member
74921315
KS
18803 && abbrev->tag != DW_TAG_imported_unit
18804 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18805 {
18806 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18807 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18808 continue;
18809 }
18810
6f06d47b
YQ
18811 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18812 abbrev);
cd9983dd 18813
48fbe735 18814 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18815
18816 /* This two-pass algorithm for processing partial symbols has a
18817 high cost in cache pressure. Thus, handle some simple cases
18818 here which cover the majority of C partial symbols. DIEs
18819 which neither have specification tags in them, nor could have
18820 specification tags elsewhere pointing at them, can simply be
18821 processed and discarded.
18822
18823 This segment is also optional; scan_partial_symbols and
18824 add_partial_symbol will handle these DIEs if we chain
18825 them in normally. When compilers which do not emit large
18826 quantities of duplicate debug information are more common,
18827 this code can probably be removed. */
18828
18829 /* Any complete simple types at the top level (pretty much all
18830 of them, for a language without namespaces), can be processed
18831 directly. */
18832 if (parent_die == NULL
cd9983dd
YQ
18833 && pdi.has_specification == 0
18834 && pdi.is_declaration == 0
18835 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18836 || pdi.tag == DW_TAG_base_type
18837 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18838 {
cd9983dd
YQ
18839 if (building_psymtab && pdi.name != NULL)
18840 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
72bf9492 18841 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 18842 &objfile->static_psymbols,
1762568f 18843 0, cu->language, objfile);
cd9983dd 18844 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18845 continue;
18846 }
18847
d8228535
JK
18848 /* The exception for DW_TAG_typedef with has_children above is
18849 a workaround of GCC PR debug/47510. In the case of this complaint
18850 type_name_no_tag_or_error will error on such types later.
18851
18852 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18853 it could not find the child DIEs referenced later, this is checked
18854 above. In correct DWARF DW_TAG_typedef should have no children. */
18855
cd9983dd 18856 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
d8228535
JK
18857 complaint (&symfile_complaints,
18858 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18859 "- DIE at %s [in module %s]"),
cd9983dd 18860 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18861
72bf9492
DJ
18862 /* If we're at the second level, and we're an enumerator, and
18863 our parent has no specification (meaning possibly lives in a
18864 namespace elsewhere), then we can add the partial symbol now
18865 instead of queueing it. */
cd9983dd 18866 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18867 && parent_die != NULL
18868 && parent_die->die_parent == NULL
18869 && parent_die->tag == DW_TAG_enumeration_type
18870 && parent_die->has_specification == 0)
18871 {
cd9983dd 18872 if (pdi.name == NULL)
3e43a32a
MS
18873 complaint (&symfile_complaints,
18874 _("malformed enumerator DIE ignored"));
72bf9492 18875 else if (building_psymtab)
cd9983dd 18876 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
72bf9492 18877 VAR_DOMAIN, LOC_CONST,
9c37b5ae 18878 cu->language == language_cplus
bb5ed363
DE
18879 ? &objfile->global_psymbols
18880 : &objfile->static_psymbols,
1762568f 18881 0, cu->language, objfile);
72bf9492 18882
cd9983dd 18883 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18884 continue;
18885 }
18886
cd9983dd 18887 struct partial_die_info *part_die
6f06d47b 18888 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18889
72bf9492
DJ
18890 /* We'll save this DIE so link it in. */
18891 part_die->die_parent = parent_die;
18892 part_die->die_sibling = NULL;
18893 part_die->die_child = NULL;
18894
18895 if (last_die && last_die == parent_die)
18896 last_die->die_child = part_die;
18897 else if (last_die)
18898 last_die->die_sibling = part_die;
18899
18900 last_die = part_die;
18901
18902 if (first_die == NULL)
18903 first_die = part_die;
18904
18905 /* Maybe add the DIE to the hash table. Not all DIEs that we
18906 find interesting need to be in the hash table, because we
18907 also have the parent/sibling/child chains; only those that we
18908 might refer to by offset later during partial symbol reading.
18909
18910 For now this means things that might have be the target of a
18911 DW_AT_specification, DW_AT_abstract_origin, or
18912 DW_AT_extension. DW_AT_extension will refer only to
18913 namespaces; DW_AT_abstract_origin refers to functions (and
18914 many things under the function DIE, but we do not recurse
18915 into function DIEs during partial symbol reading) and
18916 possibly variables as well; DW_AT_specification refers to
18917 declarations. Declarations ought to have the DW_AT_declaration
18918 flag. It happens that GCC forgets to put it in sometimes, but
18919 only for functions, not for types.
18920
18921 Adding more things than necessary to the hash table is harmless
18922 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18923 wasted time in find_partial_die, when we reread the compilation
18924 unit with load_all_dies set. */
72bf9492 18925
5afb4e99 18926 if (load_all
72929c62 18927 || abbrev->tag == DW_TAG_constant
5afb4e99 18928 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18929 || abbrev->tag == DW_TAG_variable
18930 || abbrev->tag == DW_TAG_namespace
18931 || part_die->is_declaration)
18932 {
18933 void **slot;
18934
18935 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18936 to_underlying (part_die->sect_off),
18937 INSERT);
72bf9492
DJ
18938 *slot = part_die;
18939 }
18940
72bf9492 18941 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18942 we have no reason to follow the children of structures; for other
98bfdba5
PA
18943 languages we have to, so that we can get at method physnames
18944 to infer fully qualified class names, for DW_AT_specification,
18945 and for C++ template arguments. For C++, we also look one level
18946 inside functions to find template arguments (if the name of the
18947 function does not already contain the template arguments).
bc30ff58
JB
18948
18949 For Ada, we need to scan the children of subprograms and lexical
18950 blocks as well because Ada allows the definition of nested
18951 entities that could be interesting for the debugger, such as
18952 nested subprograms for instance. */
72bf9492 18953 if (last_die->has_children
5afb4e99
DJ
18954 && (load_all
18955 || last_die->tag == DW_TAG_namespace
f55ee35c 18956 || last_die->tag == DW_TAG_module
72bf9492 18957 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18958 || (cu->language == language_cplus
18959 && last_die->tag == DW_TAG_subprogram
18960 && (last_die->name == NULL
18961 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18962 || (cu->language != language_c
18963 && (last_die->tag == DW_TAG_class_type
680b30c7 18964 || last_die->tag == DW_TAG_interface_type
72bf9492 18965 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
18966 || last_die->tag == DW_TAG_union_type))
18967 || (cu->language == language_ada
18968 && (last_die->tag == DW_TAG_subprogram
18969 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18970 {
18971 nesting_level++;
18972 parent_die = last_die;
18973 continue;
18974 }
18975
18976 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18977 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18978
18979 /* Back to the top, do it again. */
18980 }
18981}
18982
6f06d47b
YQ
18983partial_die_info::partial_die_info (sect_offset sect_off_,
18984 struct abbrev_info *abbrev)
18985 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18986{
18987}
18988
35cc7ed7
YQ
18989/* Read a minimal amount of information into the minimal die structure.
18990 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18991
48fbe735
YQ
18992const gdb_byte *
18993partial_die_info::read (const struct die_reader_specs *reader,
18994 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18995{
dee91e82 18996 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18997 struct dwarf2_per_objfile *dwarf2_per_objfile
18998 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18999 unsigned int i;
c5aa993b 19000 int has_low_pc_attr = 0;
c906108c 19001 int has_high_pc_attr = 0;
91da1414 19002 int high_pc_relative = 0;
c906108c 19003
fd0a254f 19004 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 19005 {
48fbe735
YQ
19006 struct attribute attr;
19007
fd0a254f 19008 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
c906108c
SS
19009
19010 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 19011 partial symbol table. */
c906108c
SS
19012 switch (attr.name)
19013 {
19014 case DW_AT_name:
48fbe735 19015 switch (tag)
71c25dea
TT
19016 {
19017 case DW_TAG_compile_unit:
95554aad 19018 case DW_TAG_partial_unit:
348e048f 19019 case DW_TAG_type_unit:
71c25dea
TT
19020 /* Compilation units have a DW_AT_name that is a filename, not
19021 a source language identifier. */
19022 case DW_TAG_enumeration_type:
19023 case DW_TAG_enumerator:
19024 /* These tags always have simple identifiers already; no need
19025 to canonicalize them. */
48fbe735 19026 name = DW_STRING (&attr);
71c25dea
TT
19027 break;
19028 default:
48fbe735
YQ
19029 {
19030 struct objfile *objfile = dwarf2_per_objfile->objfile;
19031
19032 name
19033 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
19034 &objfile->per_bfd->storage_obstack);
19035 }
71c25dea
TT
19036 break;
19037 }
c906108c 19038 break;
31ef98ae 19039 case DW_AT_linkage_name:
c906108c 19040 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
19041 /* Note that both forms of linkage name might appear. We
19042 assume they will be the same, and we only store the last
19043 one we see. */
94af9270 19044 if (cu->language == language_ada)
48fbe735
YQ
19045 name = DW_STRING (&attr);
19046 linkage_name = DW_STRING (&attr);
c906108c
SS
19047 break;
19048 case DW_AT_low_pc:
19049 has_low_pc_attr = 1;
48fbe735 19050 lowpc = attr_value_as_address (&attr);
c906108c
SS
19051 break;
19052 case DW_AT_high_pc:
19053 has_high_pc_attr = 1;
48fbe735 19054 highpc = attr_value_as_address (&attr);
31aa7e4e
JB
19055 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
19056 high_pc_relative = 1;
c906108c
SS
19057 break;
19058 case DW_AT_location:
0963b4bd 19059 /* Support the .debug_loc offsets. */
8e19ed76
PS
19060 if (attr_form_is_block (&attr))
19061 {
48fbe735 19062 d.locdesc = DW_BLOCK (&attr);
8e19ed76 19063 }
3690dd37 19064 else if (attr_form_is_section_offset (&attr))
8e19ed76 19065 {
4d3c2250 19066 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
19067 }
19068 else
19069 {
4d3c2250
KB
19070 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19071 "partial symbol information");
8e19ed76 19072 }
c906108c 19073 break;
c906108c 19074 case DW_AT_external:
48fbe735 19075 is_external = DW_UNSND (&attr);
c906108c
SS
19076 break;
19077 case DW_AT_declaration:
48fbe735 19078 is_declaration = DW_UNSND (&attr);
c906108c
SS
19079 break;
19080 case DW_AT_type:
48fbe735 19081 has_type = 1;
c906108c
SS
19082 break;
19083 case DW_AT_abstract_origin:
19084 case DW_AT_specification:
72bf9492 19085 case DW_AT_extension:
48fbe735
YQ
19086 has_specification = 1;
19087 spec_offset = dwarf2_get_ref_die_offset (&attr);
19088 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 19089 || cu->per_cu->is_dwz);
c906108c
SS
19090 break;
19091 case DW_AT_sibling:
19092 /* Ignore absolute siblings, they might point outside of
19093 the current compile unit. */
19094 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
19095 complaint (&symfile_complaints,
19096 _("ignoring absolute DW_AT_sibling"));
c906108c 19097 else
b9502d3f 19098 {
48fbe735 19099 const gdb_byte *buffer = reader->buffer;
9c541725
PA
19100 sect_offset off = dwarf2_get_ref_die_offset (&attr);
19101 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
19102
19103 if (sibling_ptr < info_ptr)
19104 complaint (&symfile_complaints,
19105 _("DW_AT_sibling points backwards"));
22869d73
KS
19106 else if (sibling_ptr > reader->buffer_end)
19107 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 19108 else
48fbe735 19109 sibling = sibling_ptr;
b9502d3f 19110 }
c906108c 19111 break;
fa4028e9 19112 case DW_AT_byte_size:
48fbe735 19113 has_byte_size = 1;
fa4028e9 19114 break;
ff908ebf 19115 case DW_AT_const_value:
48fbe735 19116 has_const_value = 1;
ff908ebf 19117 break;
68511cec
CES
19118 case DW_AT_calling_convention:
19119 /* DWARF doesn't provide a way to identify a program's source-level
19120 entry point. DW_AT_calling_convention attributes are only meant
19121 to describe functions' calling conventions.
19122
19123 However, because it's a necessary piece of information in
0c1b455e
TT
19124 Fortran, and before DWARF 4 DW_CC_program was the only
19125 piece of debugging information whose definition refers to
19126 a 'main program' at all, several compilers marked Fortran
19127 main programs with DW_CC_program --- even when those
19128 functions use the standard calling conventions.
19129
19130 Although DWARF now specifies a way to provide this
19131 information, we support this practice for backward
19132 compatibility. */
68511cec 19133 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 19134 && cu->language == language_fortran)
48fbe735 19135 main_subprogram = 1;
68511cec 19136 break;
481860b3
GB
19137 case DW_AT_inline:
19138 if (DW_UNSND (&attr) == DW_INL_inlined
19139 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 19140 may_be_inlined = 1;
481860b3 19141 break;
95554aad
TT
19142
19143 case DW_AT_import:
48fbe735 19144 if (tag == DW_TAG_imported_unit)
36586728 19145 {
48fbe735
YQ
19146 d.sect_off = dwarf2_get_ref_die_offset (&attr);
19147 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19148 || cu->per_cu->is_dwz);
19149 }
95554aad
TT
19150 break;
19151
0c1b455e 19152 case DW_AT_main_subprogram:
48fbe735 19153 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
19154 break;
19155
c906108c
SS
19156 default:
19157 break;
19158 }
19159 }
19160
91da1414 19161 if (high_pc_relative)
48fbe735 19162 highpc += lowpc;
91da1414 19163
9373cf26
JK
19164 if (has_low_pc_attr && has_high_pc_attr)
19165 {
19166 /* When using the GNU linker, .gnu.linkonce. sections are used to
19167 eliminate duplicate copies of functions and vtables and such.
19168 The linker will arbitrarily choose one and discard the others.
19169 The AT_*_pc values for such functions refer to local labels in
19170 these sections. If the section from that file was discarded, the
19171 labels are not in the output, so the relocs get a value of 0.
19172 If this is a discarded function, mark the pc bounds as invalid,
19173 so that GDB will ignore it. */
48fbe735 19174 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 19175 {
48fbe735 19176 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 19177 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
19178
19179 complaint (&symfile_complaints,
19180 _("DW_AT_low_pc %s is zero "
9d8780f0 19181 "for DIE at %s [in module %s]"),
48fbe735
YQ
19182 paddress (gdbarch, lowpc),
19183 sect_offset_str (sect_off),
9d8780f0 19184 objfile_name (objfile));
9373cf26
JK
19185 }
19186 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19187 else if (lowpc >= highpc)
9373cf26 19188 {
48fbe735 19189 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 19190 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
19191
19192 complaint (&symfile_complaints,
19193 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19194 "for DIE at %s [in module %s]"),
48fbe735
YQ
19195 paddress (gdbarch, lowpc),
19196 paddress (gdbarch, highpc),
19197 sect_offset_str (sect_off),
9c541725 19198 objfile_name (objfile));
9373cf26
JK
19199 }
19200 else
48fbe735 19201 has_pc_info = 1;
9373cf26 19202 }
85cbf3d3 19203
c906108c
SS
19204 return info_ptr;
19205}
19206
72bf9492
DJ
19207/* Find a cached partial DIE at OFFSET in CU. */
19208
d590ff25
YQ
19209struct partial_die_info *
19210dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19211{
19212 struct partial_die_info *lookup_die = NULL;
6f06d47b 19213 struct partial_die_info part_die (sect_off);
72bf9492 19214
9a3c8263 19215 lookup_die = ((struct partial_die_info *)
d590ff25 19216 htab_find_with_hash (partial_dies, &part_die,
9c541725 19217 to_underlying (sect_off)));
72bf9492 19218
72bf9492
DJ
19219 return lookup_die;
19220}
19221
348e048f
DE
19222/* Find a partial DIE at OFFSET, which may or may not be in CU,
19223 except in the case of .debug_types DIEs which do not reference
19224 outside their CU (they do however referencing other types via
55f1336d 19225 DW_FORM_ref_sig8). */
72bf9492
DJ
19226
19227static struct partial_die_info *
9c541725 19228find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19229{
518817b3
SM
19230 struct dwarf2_per_objfile *dwarf2_per_objfile
19231 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19232 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
19233 struct dwarf2_per_cu_data *per_cu = NULL;
19234 struct partial_die_info *pd = NULL;
72bf9492 19235
36586728 19236 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 19237 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 19238 {
d590ff25 19239 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
19240 if (pd != NULL)
19241 return pd;
0d99eb77
DE
19242 /* We missed recording what we needed.
19243 Load all dies and try again. */
19244 per_cu = cu->per_cu;
5afb4e99 19245 }
0d99eb77
DE
19246 else
19247 {
19248 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 19249 if (cu->per_cu->is_debug_types)
0d99eb77 19250 {
9d8780f0
SM
19251 error (_("Dwarf Error: Type Unit at offset %s contains"
19252 " external reference to offset %s [in module %s].\n"),
19253 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19254 bfd_get_filename (objfile->obfd));
19255 }
9c541725 19256 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 19257 dwarf2_per_objfile);
72bf9492 19258
0d99eb77
DE
19259 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19260 load_partial_comp_unit (per_cu);
ae038cb0 19261
0d99eb77 19262 per_cu->cu->last_used = 0;
d590ff25 19263 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 19264 }
5afb4e99 19265
dee91e82
DE
19266 /* If we didn't find it, and not all dies have been loaded,
19267 load them all and try again. */
19268
5afb4e99
DJ
19269 if (pd == NULL && per_cu->load_all_dies == 0)
19270 {
5afb4e99 19271 per_cu->load_all_dies = 1;
fd820528
DE
19272
19273 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19274 THIS_CU->cu may already be in use. So we can't just free it and
19275 replace its DIEs with the ones we read in. Instead, we leave those
19276 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19277 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19278 set. */
dee91e82 19279 load_partial_comp_unit (per_cu);
5afb4e99 19280
d590ff25 19281 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
19282 }
19283
19284 if (pd == NULL)
19285 internal_error (__FILE__, __LINE__,
9d8780f0 19286 _("could not find partial DIE %s "
3e43a32a 19287 "in cache [from module %s]\n"),
9d8780f0 19288 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
5afb4e99 19289 return pd;
72bf9492
DJ
19290}
19291
abc72ce4
DE
19292/* See if we can figure out if the class lives in a namespace. We do
19293 this by looking for a member function; its demangled name will
19294 contain namespace info, if there is any. */
19295
19296static void
19297guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19298 struct dwarf2_cu *cu)
19299{
19300 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19301 what template types look like, because the demangler
19302 frequently doesn't give the same name as the debug info. We
19303 could fix this by only using the demangled name to get the
19304 prefix (but see comment in read_structure_type). */
19305
19306 struct partial_die_info *real_pdi;
19307 struct partial_die_info *child_pdi;
19308
19309 /* If this DIE (this DIE's specification, if any) has a parent, then
19310 we should not do this. We'll prepend the parent's fully qualified
19311 name when we create the partial symbol. */
19312
19313 real_pdi = struct_pdi;
19314 while (real_pdi->has_specification)
36586728
TT
19315 real_pdi = find_partial_die (real_pdi->spec_offset,
19316 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
19317
19318 if (real_pdi->die_parent != NULL)
19319 return;
19320
19321 for (child_pdi = struct_pdi->die_child;
19322 child_pdi != NULL;
19323 child_pdi = child_pdi->die_sibling)
19324 {
19325 if (child_pdi->tag == DW_TAG_subprogram
19326 && child_pdi->linkage_name != NULL)
19327 {
19328 char *actual_class_name
19329 = language_class_name_from_physname (cu->language_defn,
19330 child_pdi->linkage_name);
19331 if (actual_class_name != NULL)
19332 {
518817b3 19333 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 19334 struct_pdi->name
224c3ddb 19335 = ((const char *)
e3b94546 19336 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb
SM
19337 actual_class_name,
19338 strlen (actual_class_name)));
abc72ce4
DE
19339 xfree (actual_class_name);
19340 }
19341 break;
19342 }
19343 }
19344}
19345
52356b79
YQ
19346void
19347partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19348{
abc72ce4
DE
19349 /* Once we've fixed up a die, there's no point in doing so again.
19350 This also avoids a memory leak if we were to call
19351 guess_partial_die_structure_name multiple times. */
52356b79 19352 if (fixup_called)
abc72ce4
DE
19353 return;
19354
72bf9492
DJ
19355 /* If we found a reference attribute and the DIE has no name, try
19356 to find a name in the referred to DIE. */
19357
52356b79 19358 if (name == NULL && has_specification)
72bf9492
DJ
19359 {
19360 struct partial_die_info *spec_die;
72bf9492 19361
52356b79 19362 spec_die = find_partial_die (spec_offset, spec_is_dwz, cu);
72bf9492 19363
52356b79 19364 spec_die->fixup (cu);
72bf9492
DJ
19365
19366 if (spec_die->name)
19367 {
52356b79 19368 name = spec_die->name;
72bf9492
DJ
19369
19370 /* Copy DW_AT_external attribute if it is set. */
19371 if (spec_die->is_external)
52356b79 19372 is_external = spec_die->is_external;
72bf9492
DJ
19373 }
19374 }
19375
19376 /* Set default names for some unnamed DIEs. */
72bf9492 19377
52356b79
YQ
19378 if (name == NULL && tag == DW_TAG_namespace)
19379 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 19380
abc72ce4
DE
19381 /* If there is no parent die to provide a namespace, and there are
19382 children, see if we can determine the namespace from their linkage
122d1940 19383 name. */
abc72ce4 19384 if (cu->language == language_cplus
518817b3
SM
19385 && !VEC_empty (dwarf2_section_info_def,
19386 cu->per_cu->dwarf2_per_objfile->types)
52356b79
YQ
19387 && die_parent == NULL
19388 && has_children
19389 && (tag == DW_TAG_class_type
19390 || tag == DW_TAG_structure_type
19391 || tag == DW_TAG_union_type))
19392 guess_partial_die_structure_name (this, cu);
abc72ce4 19393
53832f31
TT
19394 /* GCC might emit a nameless struct or union that has a linkage
19395 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
19396 if (name == NULL
19397 && (tag == DW_TAG_class_type
19398 || tag == DW_TAG_interface_type
19399 || tag == DW_TAG_structure_type
19400 || tag == DW_TAG_union_type)
19401 && linkage_name != NULL)
53832f31
TT
19402 {
19403 char *demangled;
19404
52356b79 19405 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
53832f31
TT
19406 if (demangled)
19407 {
96408a79
SA
19408 const char *base;
19409
19410 /* Strip any leading namespaces/classes, keep only the base name.
19411 DW_AT_name for named DIEs does not contain the prefixes. */
19412 base = strrchr (demangled, ':');
19413 if (base && base > demangled && base[-1] == ':')
19414 base++;
19415 else
19416 base = demangled;
19417
518817b3 19418 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
52356b79 19419 name
224c3ddb 19420 = ((const char *)
e3b94546 19421 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb 19422 base, strlen (base)));
53832f31
TT
19423 xfree (demangled);
19424 }
19425 }
19426
52356b79 19427 fixup_called = 1;
72bf9492
DJ
19428}
19429
a8329558 19430/* Read an attribute value described by an attribute form. */
c906108c 19431
d521ce57 19432static const gdb_byte *
dee91e82
DE
19433read_attribute_value (const struct die_reader_specs *reader,
19434 struct attribute *attr, unsigned form,
43988095 19435 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 19436{
dee91e82 19437 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19438 struct dwarf2_per_objfile *dwarf2_per_objfile
19439 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19440 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 19441 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 19442 bfd *abfd = reader->abfd;
e7c27a73 19443 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19444 unsigned int bytes_read;
19445 struct dwarf_block *blk;
19446
aead7601 19447 attr->form = (enum dwarf_form) form;
a8329558 19448 switch (form)
c906108c 19449 {
c906108c 19450 case DW_FORM_ref_addr:
ae411497 19451 if (cu->header.version == 2)
4568ecf9 19452 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 19453 else
4568ecf9
DE
19454 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19455 &cu->header, &bytes_read);
ae411497
TT
19456 info_ptr += bytes_read;
19457 break;
36586728
TT
19458 case DW_FORM_GNU_ref_alt:
19459 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19460 info_ptr += bytes_read;
19461 break;
ae411497 19462 case DW_FORM_addr:
e7c27a73 19463 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 19464 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 19465 info_ptr += bytes_read;
c906108c
SS
19466 break;
19467 case DW_FORM_block2:
7b5a2f43 19468 blk = dwarf_alloc_block (cu);
c906108c
SS
19469 blk->size = read_2_bytes (abfd, info_ptr);
19470 info_ptr += 2;
19471 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19472 info_ptr += blk->size;
19473 DW_BLOCK (attr) = blk;
19474 break;
19475 case DW_FORM_block4:
7b5a2f43 19476 blk = dwarf_alloc_block (cu);
c906108c
SS
19477 blk->size = read_4_bytes (abfd, info_ptr);
19478 info_ptr += 4;
19479 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19480 info_ptr += blk->size;
19481 DW_BLOCK (attr) = blk;
19482 break;
19483 case DW_FORM_data2:
19484 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19485 info_ptr += 2;
19486 break;
19487 case DW_FORM_data4:
19488 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19489 info_ptr += 4;
19490 break;
19491 case DW_FORM_data8:
19492 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19493 info_ptr += 8;
19494 break;
0224619f
JK
19495 case DW_FORM_data16:
19496 blk = dwarf_alloc_block (cu);
19497 blk->size = 16;
19498 blk->data = read_n_bytes (abfd, info_ptr, 16);
19499 info_ptr += 16;
19500 DW_BLOCK (attr) = blk;
19501 break;
2dc7f7b3
TT
19502 case DW_FORM_sec_offset:
19503 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19504 info_ptr += bytes_read;
19505 break;
c906108c 19506 case DW_FORM_string:
9b1c24c8 19507 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19508 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19509 info_ptr += bytes_read;
19510 break;
4bdf3d34 19511 case DW_FORM_strp:
36586728
TT
19512 if (!cu->per_cu->is_dwz)
19513 {
ed2dc618
SM
19514 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19515 abfd, info_ptr, cu_header,
36586728
TT
19516 &bytes_read);
19517 DW_STRING_IS_CANONICAL (attr) = 0;
19518 info_ptr += bytes_read;
19519 break;
19520 }
19521 /* FALLTHROUGH */
43988095
JK
19522 case DW_FORM_line_strp:
19523 if (!cu->per_cu->is_dwz)
19524 {
ed2dc618
SM
19525 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19526 abfd, info_ptr,
43988095
JK
19527 cu_header, &bytes_read);
19528 DW_STRING_IS_CANONICAL (attr) = 0;
19529 info_ptr += bytes_read;
19530 break;
19531 }
19532 /* FALLTHROUGH */
36586728
TT
19533 case DW_FORM_GNU_strp_alt:
19534 {
ed2dc618 19535 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19536 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19537 &bytes_read);
19538
ed2dc618
SM
19539 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19540 dwz, str_offset);
36586728
TT
19541 DW_STRING_IS_CANONICAL (attr) = 0;
19542 info_ptr += bytes_read;
19543 }
4bdf3d34 19544 break;
2dc7f7b3 19545 case DW_FORM_exprloc:
c906108c 19546 case DW_FORM_block:
7b5a2f43 19547 blk = dwarf_alloc_block (cu);
c906108c
SS
19548 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19549 info_ptr += bytes_read;
19550 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19551 info_ptr += blk->size;
19552 DW_BLOCK (attr) = blk;
19553 break;
19554 case DW_FORM_block1:
7b5a2f43 19555 blk = dwarf_alloc_block (cu);
c906108c
SS
19556 blk->size = read_1_byte (abfd, info_ptr);
19557 info_ptr += 1;
19558 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19559 info_ptr += blk->size;
19560 DW_BLOCK (attr) = blk;
19561 break;
19562 case DW_FORM_data1:
19563 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19564 info_ptr += 1;
19565 break;
19566 case DW_FORM_flag:
19567 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19568 info_ptr += 1;
19569 break;
2dc7f7b3
TT
19570 case DW_FORM_flag_present:
19571 DW_UNSND (attr) = 1;
19572 break;
c906108c
SS
19573 case DW_FORM_sdata:
19574 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19575 info_ptr += bytes_read;
19576 break;
19577 case DW_FORM_udata:
19578 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19579 info_ptr += bytes_read;
19580 break;
19581 case DW_FORM_ref1:
9c541725 19582 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19583 + read_1_byte (abfd, info_ptr));
c906108c
SS
19584 info_ptr += 1;
19585 break;
19586 case DW_FORM_ref2:
9c541725 19587 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19588 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19589 info_ptr += 2;
19590 break;
19591 case DW_FORM_ref4:
9c541725 19592 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19593 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19594 info_ptr += 4;
19595 break;
613e1657 19596 case DW_FORM_ref8:
9c541725 19597 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19598 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19599 info_ptr += 8;
19600 break;
55f1336d 19601 case DW_FORM_ref_sig8:
ac9ec31b 19602 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19603 info_ptr += 8;
19604 break;
c906108c 19605 case DW_FORM_ref_udata:
9c541725 19606 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19607 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19608 info_ptr += bytes_read;
19609 break;
c906108c 19610 case DW_FORM_indirect:
a8329558
KW
19611 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19612 info_ptr += bytes_read;
43988095
JK
19613 if (form == DW_FORM_implicit_const)
19614 {
19615 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19616 info_ptr += bytes_read;
19617 }
19618 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19619 info_ptr);
19620 break;
19621 case DW_FORM_implicit_const:
19622 DW_SND (attr) = implicit_const;
a8329558 19623 break;
3019eac3
DE
19624 case DW_FORM_GNU_addr_index:
19625 if (reader->dwo_file == NULL)
19626 {
19627 /* For now flag a hard error.
19628 Later we can turn this into a complaint. */
19629 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19630 dwarf_form_name (form),
19631 bfd_get_filename (abfd));
19632 }
19633 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19634 info_ptr += bytes_read;
19635 break;
19636 case DW_FORM_GNU_str_index:
19637 if (reader->dwo_file == NULL)
19638 {
19639 /* For now flag a hard error.
19640 Later we can turn this into a complaint if warranted. */
19641 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19642 dwarf_form_name (form),
19643 bfd_get_filename (abfd));
19644 }
19645 {
19646 ULONGEST str_index =
19647 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19648
342587c4 19649 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
19650 DW_STRING_IS_CANONICAL (attr) = 0;
19651 info_ptr += bytes_read;
19652 }
19653 break;
c906108c 19654 default:
8a3fe4f8 19655 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19656 dwarf_form_name (form),
19657 bfd_get_filename (abfd));
c906108c 19658 }
28e94949 19659
36586728 19660 /* Super hack. */
7771576e 19661 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19662 attr->form = DW_FORM_GNU_ref_alt;
19663
28e94949
JB
19664 /* We have seen instances where the compiler tried to emit a byte
19665 size attribute of -1 which ended up being encoded as an unsigned
19666 0xffffffff. Although 0xffffffff is technically a valid size value,
19667 an object of this size seems pretty unlikely so we can relatively
19668 safely treat these cases as if the size attribute was invalid and
19669 treat them as zero by default. */
19670 if (attr->name == DW_AT_byte_size
19671 && form == DW_FORM_data4
19672 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19673 {
19674 complaint
19675 (&symfile_complaints,
43bbcdc2
PH
19676 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19677 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19678 DW_UNSND (attr) = 0;
19679 }
28e94949 19680
c906108c
SS
19681 return info_ptr;
19682}
19683
a8329558
KW
19684/* Read an attribute described by an abbreviated attribute. */
19685
d521ce57 19686static const gdb_byte *
dee91e82
DE
19687read_attribute (const struct die_reader_specs *reader,
19688 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 19689 const gdb_byte *info_ptr)
a8329558
KW
19690{
19691 attr->name = abbrev->name;
43988095
JK
19692 return read_attribute_value (reader, attr, abbrev->form,
19693 abbrev->implicit_const, info_ptr);
a8329558
KW
19694}
19695
0963b4bd 19696/* Read dwarf information from a buffer. */
c906108c
SS
19697
19698static unsigned int
a1855c1d 19699read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19700{
fe1b8b76 19701 return bfd_get_8 (abfd, buf);
c906108c
SS
19702}
19703
19704static int
a1855c1d 19705read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19706{
fe1b8b76 19707 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19708}
19709
19710static unsigned int
a1855c1d 19711read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19712{
fe1b8b76 19713 return bfd_get_16 (abfd, buf);
c906108c
SS
19714}
19715
21ae7a4d 19716static int
a1855c1d 19717read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19718{
19719 return bfd_get_signed_16 (abfd, buf);
19720}
19721
c906108c 19722static unsigned int
a1855c1d 19723read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19724{
fe1b8b76 19725 return bfd_get_32 (abfd, buf);
c906108c
SS
19726}
19727
21ae7a4d 19728static int
a1855c1d 19729read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19730{
19731 return bfd_get_signed_32 (abfd, buf);
19732}
19733
93311388 19734static ULONGEST
a1855c1d 19735read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19736{
fe1b8b76 19737 return bfd_get_64 (abfd, buf);
c906108c
SS
19738}
19739
19740static CORE_ADDR
d521ce57 19741read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19742 unsigned int *bytes_read)
c906108c 19743{
e7c27a73 19744 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19745 CORE_ADDR retval = 0;
19746
107d2387 19747 if (cu_header->signed_addr_p)
c906108c 19748 {
107d2387
AC
19749 switch (cu_header->addr_size)
19750 {
19751 case 2:
fe1b8b76 19752 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19753 break;
19754 case 4:
fe1b8b76 19755 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19756 break;
19757 case 8:
fe1b8b76 19758 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19759 break;
19760 default:
8e65ff28 19761 internal_error (__FILE__, __LINE__,
e2e0b3e5 19762 _("read_address: bad switch, signed [in module %s]"),
659b0389 19763 bfd_get_filename (abfd));
107d2387
AC
19764 }
19765 }
19766 else
19767 {
19768 switch (cu_header->addr_size)
19769 {
19770 case 2:
fe1b8b76 19771 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19772 break;
19773 case 4:
fe1b8b76 19774 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19775 break;
19776 case 8:
fe1b8b76 19777 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19778 break;
19779 default:
8e65ff28 19780 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19781 _("read_address: bad switch, "
19782 "unsigned [in module %s]"),
659b0389 19783 bfd_get_filename (abfd));
107d2387 19784 }
c906108c 19785 }
64367e0a 19786
107d2387
AC
19787 *bytes_read = cu_header->addr_size;
19788 return retval;
c906108c
SS
19789}
19790
f7ef9339 19791/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19792 specification allows the initial length to take up either 4 bytes
19793 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19794 bytes describe the length and all offsets will be 8 bytes in length
19795 instead of 4.
19796
f7ef9339
KB
19797 An older, non-standard 64-bit format is also handled by this
19798 function. The older format in question stores the initial length
19799 as an 8-byte quantity without an escape value. Lengths greater
19800 than 2^32 aren't very common which means that the initial 4 bytes
19801 is almost always zero. Since a length value of zero doesn't make
19802 sense for the 32-bit format, this initial zero can be considered to
19803 be an escape value which indicates the presence of the older 64-bit
19804 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19805 greater than 4GB. If it becomes necessary to handle lengths
19806 somewhat larger than 4GB, we could allow other small values (such
19807 as the non-sensical values of 1, 2, and 3) to also be used as
19808 escape values indicating the presence of the old format.
f7ef9339 19809
917c78fc
MK
19810 The value returned via bytes_read should be used to increment the
19811 relevant pointer after calling read_initial_length().
c764a876 19812
613e1657
KB
19813 [ Note: read_initial_length() and read_offset() are based on the
19814 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19815 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19816 from:
19817
f7ef9339 19818 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19819
613e1657
KB
19820 This document is only a draft and is subject to change. (So beware.)
19821
f7ef9339 19822 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19823 determined empirically by examining 64-bit ELF files produced by
19824 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19825
19826 - Kevin, July 16, 2002
613e1657
KB
19827 ] */
19828
19829static LONGEST
d521ce57 19830read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19831{
fe1b8b76 19832 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19833
dd373385 19834 if (length == 0xffffffff)
613e1657 19835 {
fe1b8b76 19836 length = bfd_get_64 (abfd, buf + 4);
613e1657 19837 *bytes_read = 12;
613e1657 19838 }
dd373385 19839 else if (length == 0)
f7ef9339 19840 {
dd373385 19841 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19842 length = bfd_get_64 (abfd, buf);
f7ef9339 19843 *bytes_read = 8;
f7ef9339 19844 }
613e1657
KB
19845 else
19846 {
19847 *bytes_read = 4;
613e1657
KB
19848 }
19849
c764a876
DE
19850 return length;
19851}
dd373385 19852
c764a876
DE
19853/* Cover function for read_initial_length.
19854 Returns the length of the object at BUF, and stores the size of the
19855 initial length in *BYTES_READ and stores the size that offsets will be in
19856 *OFFSET_SIZE.
19857 If the initial length size is not equivalent to that specified in
19858 CU_HEADER then issue a complaint.
19859 This is useful when reading non-comp-unit headers. */
dd373385 19860
c764a876 19861static LONGEST
d521ce57 19862read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19863 const struct comp_unit_head *cu_header,
19864 unsigned int *bytes_read,
19865 unsigned int *offset_size)
19866{
19867 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19868
19869 gdb_assert (cu_header->initial_length_size == 4
19870 || cu_header->initial_length_size == 8
19871 || cu_header->initial_length_size == 12);
19872
19873 if (cu_header->initial_length_size != *bytes_read)
19874 complaint (&symfile_complaints,
19875 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19876
c764a876 19877 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19878 return length;
613e1657
KB
19879}
19880
19881/* Read an offset from the data stream. The size of the offset is
917c78fc 19882 given by cu_header->offset_size. */
613e1657
KB
19883
19884static LONGEST
d521ce57
TT
19885read_offset (bfd *abfd, const gdb_byte *buf,
19886 const struct comp_unit_head *cu_header,
891d2f0b 19887 unsigned int *bytes_read)
c764a876
DE
19888{
19889 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19890
c764a876
DE
19891 *bytes_read = cu_header->offset_size;
19892 return offset;
19893}
19894
19895/* Read an offset from the data stream. */
19896
19897static LONGEST
d521ce57 19898read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19899{
19900 LONGEST retval = 0;
19901
c764a876 19902 switch (offset_size)
613e1657
KB
19903 {
19904 case 4:
fe1b8b76 19905 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19906 break;
19907 case 8:
fe1b8b76 19908 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19909 break;
19910 default:
8e65ff28 19911 internal_error (__FILE__, __LINE__,
c764a876 19912 _("read_offset_1: bad switch [in module %s]"),
659b0389 19913 bfd_get_filename (abfd));
613e1657
KB
19914 }
19915
917c78fc 19916 return retval;
613e1657
KB
19917}
19918
d521ce57
TT
19919static const gdb_byte *
19920read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19921{
19922 /* If the size of a host char is 8 bits, we can return a pointer
19923 to the buffer, otherwise we have to copy the data to a buffer
19924 allocated on the temporary obstack. */
4bdf3d34 19925 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19926 return buf;
c906108c
SS
19927}
19928
d521ce57
TT
19929static const char *
19930read_direct_string (bfd *abfd, const gdb_byte *buf,
19931 unsigned int *bytes_read_ptr)
c906108c
SS
19932{
19933 /* If the size of a host char is 8 bits, we can return a pointer
19934 to the string, otherwise we have to copy the string to a buffer
19935 allocated on the temporary obstack. */
4bdf3d34 19936 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19937 if (*buf == '\0')
19938 {
19939 *bytes_read_ptr = 1;
19940 return NULL;
19941 }
d521ce57
TT
19942 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19943 return (const char *) buf;
4bdf3d34
JJ
19944}
19945
43988095
JK
19946/* Return pointer to string at section SECT offset STR_OFFSET with error
19947 reporting strings FORM_NAME and SECT_NAME. */
19948
d521ce57 19949static const char *
ed2dc618
SM
19950read_indirect_string_at_offset_from (struct objfile *objfile,
19951 bfd *abfd, LONGEST str_offset,
43988095
JK
19952 struct dwarf2_section_info *sect,
19953 const char *form_name,
19954 const char *sect_name)
19955{
ed2dc618 19956 dwarf2_read_section (objfile, sect);
43988095
JK
19957 if (sect->buffer == NULL)
19958 error (_("%s used without %s section [in module %s]"),
19959 form_name, sect_name, bfd_get_filename (abfd));
19960 if (str_offset >= sect->size)
19961 error (_("%s pointing outside of %s section [in module %s]"),
19962 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19963 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19964 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19965 return NULL;
43988095
JK
19966 return (const char *) (sect->buffer + str_offset);
19967}
19968
19969/* Return pointer to string at .debug_str offset STR_OFFSET. */
19970
19971static const char *
ed2dc618
SM
19972read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19973 bfd *abfd, LONGEST str_offset)
43988095 19974{
ed2dc618
SM
19975 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19976 abfd, str_offset,
43988095
JK
19977 &dwarf2_per_objfile->str,
19978 "DW_FORM_strp", ".debug_str");
19979}
19980
19981/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19982
19983static const char *
ed2dc618
SM
19984read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19985 bfd *abfd, LONGEST str_offset)
43988095 19986{
ed2dc618
SM
19987 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19988 abfd, str_offset,
43988095
JK
19989 &dwarf2_per_objfile->line_str,
19990 "DW_FORM_line_strp",
19991 ".debug_line_str");
c906108c
SS
19992}
19993
36586728
TT
19994/* Read a string at offset STR_OFFSET in the .debug_str section from
19995 the .dwz file DWZ. Throw an error if the offset is too large. If
19996 the string consists of a single NUL byte, return NULL; otherwise
19997 return a pointer to the string. */
19998
d521ce57 19999static const char *
ed2dc618
SM
20000read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
20001 LONGEST str_offset)
36586728 20002{
ed2dc618 20003 dwarf2_read_section (objfile, &dwz->str);
36586728
TT
20004
20005 if (dwz->str.buffer == NULL)
20006 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
20007 "section [in module %s]"),
20008 bfd_get_filename (dwz->dwz_bfd));
20009 if (str_offset >= dwz->str.size)
20010 error (_("DW_FORM_GNU_strp_alt pointing outside of "
20011 ".debug_str section [in module %s]"),
20012 bfd_get_filename (dwz->dwz_bfd));
20013 gdb_assert (HOST_CHAR_BIT == 8);
20014 if (dwz->str.buffer[str_offset] == '\0')
20015 return NULL;
d521ce57 20016 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
20017}
20018
43988095
JK
20019/* Return pointer to string at .debug_str offset as read from BUF.
20020 BUF is assumed to be in a compilation unit described by CU_HEADER.
20021 Return *BYTES_READ_PTR count of bytes read from BUF. */
20022
d521ce57 20023static const char *
ed2dc618
SM
20024read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
20025 const gdb_byte *buf,
cf2c3c16
TT
20026 const struct comp_unit_head *cu_header,
20027 unsigned int *bytes_read_ptr)
20028{
20029 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20030
ed2dc618 20031 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
20032}
20033
43988095
JK
20034/* Return pointer to string at .debug_line_str offset as read from BUF.
20035 BUF is assumed to be in a compilation unit described by CU_HEADER.
20036 Return *BYTES_READ_PTR count of bytes read from BUF. */
20037
20038static const char *
ed2dc618
SM
20039read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
20040 bfd *abfd, const gdb_byte *buf,
43988095
JK
20041 const struct comp_unit_head *cu_header,
20042 unsigned int *bytes_read_ptr)
20043{
20044 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20045
ed2dc618
SM
20046 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
20047 str_offset);
43988095
JK
20048}
20049
20050ULONGEST
d521ce57 20051read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 20052 unsigned int *bytes_read_ptr)
c906108c 20053{
12df843f 20054 ULONGEST result;
ce5d95e1 20055 unsigned int num_read;
870f88f7 20056 int shift;
c906108c
SS
20057 unsigned char byte;
20058
20059 result = 0;
20060 shift = 0;
20061 num_read = 0;
c906108c
SS
20062 while (1)
20063 {
fe1b8b76 20064 byte = bfd_get_8 (abfd, buf);
c906108c
SS
20065 buf++;
20066 num_read++;
12df843f 20067 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
20068 if ((byte & 128) == 0)
20069 {
20070 break;
20071 }
20072 shift += 7;
20073 }
20074 *bytes_read_ptr = num_read;
20075 return result;
20076}
20077
12df843f 20078static LONGEST
d521ce57
TT
20079read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
20080 unsigned int *bytes_read_ptr)
c906108c 20081{
12df843f 20082 LONGEST result;
870f88f7 20083 int shift, num_read;
c906108c
SS
20084 unsigned char byte;
20085
20086 result = 0;
20087 shift = 0;
c906108c 20088 num_read = 0;
c906108c
SS
20089 while (1)
20090 {
fe1b8b76 20091 byte = bfd_get_8 (abfd, buf);
c906108c
SS
20092 buf++;
20093 num_read++;
12df843f 20094 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
20095 shift += 7;
20096 if ((byte & 128) == 0)
20097 {
20098 break;
20099 }
20100 }
77e0b926 20101 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 20102 result |= -(((LONGEST) 1) << shift);
c906108c
SS
20103 *bytes_read_ptr = num_read;
20104 return result;
20105}
20106
3019eac3
DE
20107/* Given index ADDR_INDEX in .debug_addr, fetch the value.
20108 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
20109 ADDR_SIZE is the size of addresses from the CU header. */
20110
20111static CORE_ADDR
ed2dc618
SM
20112read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
20113 unsigned int addr_index, ULONGEST addr_base, int addr_size)
3019eac3
DE
20114{
20115 struct objfile *objfile = dwarf2_per_objfile->objfile;
20116 bfd *abfd = objfile->obfd;
20117 const gdb_byte *info_ptr;
20118
20119 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
20120 if (dwarf2_per_objfile->addr.buffer == NULL)
20121 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20122 objfile_name (objfile));
3019eac3
DE
20123 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
20124 error (_("DW_FORM_addr_index pointing outside of "
20125 ".debug_addr section [in module %s]"),
4262abfb 20126 objfile_name (objfile));
3019eac3
DE
20127 info_ptr = (dwarf2_per_objfile->addr.buffer
20128 + addr_base + addr_index * addr_size);
20129 if (addr_size == 4)
20130 return bfd_get_32 (abfd, info_ptr);
20131 else
20132 return bfd_get_64 (abfd, info_ptr);
20133}
20134
20135/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20136
20137static CORE_ADDR
20138read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20139{
518817b3
SM
20140 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
20141 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20142}
20143
20144/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20145
20146static CORE_ADDR
d521ce57 20147read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20148 unsigned int *bytes_read)
20149{
518817b3 20150 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
20151 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20152
20153 return read_addr_index (cu, addr_index);
20154}
20155
20156/* Data structure to pass results from dwarf2_read_addr_index_reader
20157 back to dwarf2_read_addr_index. */
20158
20159struct dwarf2_read_addr_index_data
20160{
20161 ULONGEST addr_base;
20162 int addr_size;
20163};
20164
20165/* die_reader_func for dwarf2_read_addr_index. */
20166
20167static void
20168dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 20169 const gdb_byte *info_ptr,
3019eac3
DE
20170 struct die_info *comp_unit_die,
20171 int has_children,
20172 void *data)
20173{
20174 struct dwarf2_cu *cu = reader->cu;
20175 struct dwarf2_read_addr_index_data *aidata =
20176 (struct dwarf2_read_addr_index_data *) data;
20177
20178 aidata->addr_base = cu->addr_base;
20179 aidata->addr_size = cu->header.addr_size;
20180}
20181
20182/* Given an index in .debug_addr, fetch the value.
20183 NOTE: This can be called during dwarf expression evaluation,
20184 long after the debug information has been read, and thus per_cu->cu
20185 may no longer exist. */
20186
20187CORE_ADDR
20188dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
20189 unsigned int addr_index)
20190{
ed2dc618
SM
20191 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
20192 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
20193 struct dwarf2_cu *cu = per_cu->cu;
20194 ULONGEST addr_base;
20195 int addr_size;
20196
3019eac3
DE
20197 /* We need addr_base and addr_size.
20198 If we don't have PER_CU->cu, we have to get it.
20199 Nasty, but the alternative is storing the needed info in PER_CU,
20200 which at this point doesn't seem justified: it's not clear how frequently
20201 it would get used and it would increase the size of every PER_CU.
20202 Entry points like dwarf2_per_cu_addr_size do a similar thing
20203 so we're not in uncharted territory here.
20204 Alas we need to be a bit more complicated as addr_base is contained
20205 in the DIE.
20206
20207 We don't need to read the entire CU(/TU).
20208 We just need the header and top level die.
a1b64ce1 20209
3019eac3 20210 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20211 For now we skip this optimization. */
3019eac3
DE
20212
20213 if (cu != NULL)
20214 {
20215 addr_base = cu->addr_base;
20216 addr_size = cu->header.addr_size;
20217 }
20218 else
20219 {
20220 struct dwarf2_read_addr_index_data aidata;
20221
a1b64ce1
DE
20222 /* Note: We can't use init_cutu_and_read_dies_simple here,
20223 we need addr_base. */
20224 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
20225 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
20226 addr_base = aidata.addr_base;
20227 addr_size = aidata.addr_size;
20228 }
20229
ed2dc618
SM
20230 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20231 addr_size);
3019eac3
DE
20232}
20233
57d63ce2
DE
20234/* Given a DW_FORM_GNU_str_index, fetch the string.
20235 This is only used by the Fission support. */
3019eac3 20236
d521ce57 20237static const char *
342587c4 20238read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3 20239{
ed2dc618 20240 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
20241 struct dwarf2_per_objfile *dwarf2_per_objfile
20242 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20243 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 20244 const char *objf_name = objfile_name (objfile);
3019eac3 20245 bfd *abfd = objfile->obfd;
73869dc2
DE
20246 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
20247 struct dwarf2_section_info *str_offsets_section =
20248 &reader->dwo_file->sections.str_offsets;
d521ce57 20249 const gdb_byte *info_ptr;
3019eac3 20250 ULONGEST str_offset;
57d63ce2 20251 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 20252
73869dc2
DE
20253 dwarf2_read_section (objfile, str_section);
20254 dwarf2_read_section (objfile, str_offsets_section);
20255 if (str_section->buffer == NULL)
57d63ce2 20256 error (_("%s used without .debug_str.dwo section"
9d8780f0
SM
20257 " in CU at offset %s [in module %s]"),
20258 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20259 if (str_offsets_section->buffer == NULL)
57d63ce2 20260 error (_("%s used without .debug_str_offsets.dwo section"
9d8780f0
SM
20261 " in CU at offset %s [in module %s]"),
20262 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20263 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 20264 error (_("%s pointing outside of .debug_str_offsets.dwo"
9d8780f0
SM
20265 " section in CU at offset %s [in module %s]"),
20266 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20267 info_ptr = (str_offsets_section->buffer
3019eac3
DE
20268 + str_index * cu->header.offset_size);
20269 if (cu->header.offset_size == 4)
20270 str_offset = bfd_get_32 (abfd, info_ptr);
20271 else
20272 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20273 if (str_offset >= str_section->size)
57d63ce2 20274 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20275 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20276 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20277 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20278}
20279
3019eac3
DE
20280/* Return the length of an LEB128 number in BUF. */
20281
20282static int
20283leb128_size (const gdb_byte *buf)
20284{
20285 const gdb_byte *begin = buf;
20286 gdb_byte byte;
20287
20288 while (1)
20289 {
20290 byte = *buf++;
20291 if ((byte & 128) == 0)
20292 return buf - begin;
20293 }
20294}
20295
c906108c 20296static void
e142c38c 20297set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20298{
20299 switch (lang)
20300 {
20301 case DW_LANG_C89:
76bee0cc 20302 case DW_LANG_C99:
0cfd832f 20303 case DW_LANG_C11:
c906108c 20304 case DW_LANG_C:
d1be3247 20305 case DW_LANG_UPC:
e142c38c 20306 cu->language = language_c;
c906108c 20307 break;
9c37b5ae 20308 case DW_LANG_Java:
c906108c 20309 case DW_LANG_C_plus_plus:
0cfd832f
MW
20310 case DW_LANG_C_plus_plus_11:
20311 case DW_LANG_C_plus_plus_14:
e142c38c 20312 cu->language = language_cplus;
c906108c 20313 break;
6aecb9c2
JB
20314 case DW_LANG_D:
20315 cu->language = language_d;
20316 break;
c906108c
SS
20317 case DW_LANG_Fortran77:
20318 case DW_LANG_Fortran90:
b21b22e0 20319 case DW_LANG_Fortran95:
f7de9aab
MW
20320 case DW_LANG_Fortran03:
20321 case DW_LANG_Fortran08:
e142c38c 20322 cu->language = language_fortran;
c906108c 20323 break;
a766d390
DE
20324 case DW_LANG_Go:
20325 cu->language = language_go;
20326 break;
c906108c 20327 case DW_LANG_Mips_Assembler:
e142c38c 20328 cu->language = language_asm;
c906108c
SS
20329 break;
20330 case DW_LANG_Ada83:
8aaf0b47 20331 case DW_LANG_Ada95:
bc5f45f8
JB
20332 cu->language = language_ada;
20333 break;
72019c9c
GM
20334 case DW_LANG_Modula2:
20335 cu->language = language_m2;
20336 break;
fe8e67fd
PM
20337 case DW_LANG_Pascal83:
20338 cu->language = language_pascal;
20339 break;
22566fbd
DJ
20340 case DW_LANG_ObjC:
20341 cu->language = language_objc;
20342 break;
c44af4eb
TT
20343 case DW_LANG_Rust:
20344 case DW_LANG_Rust_old:
20345 cu->language = language_rust;
20346 break;
c906108c
SS
20347 case DW_LANG_Cobol74:
20348 case DW_LANG_Cobol85:
c906108c 20349 default:
e142c38c 20350 cu->language = language_minimal;
c906108c
SS
20351 break;
20352 }
e142c38c 20353 cu->language_defn = language_def (cu->language);
c906108c
SS
20354}
20355
20356/* Return the named attribute or NULL if not there. */
20357
20358static struct attribute *
e142c38c 20359dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20360{
a48e046c 20361 for (;;)
c906108c 20362 {
a48e046c
TT
20363 unsigned int i;
20364 struct attribute *spec = NULL;
20365
20366 for (i = 0; i < die->num_attrs; ++i)
20367 {
20368 if (die->attrs[i].name == name)
20369 return &die->attrs[i];
20370 if (die->attrs[i].name == DW_AT_specification
20371 || die->attrs[i].name == DW_AT_abstract_origin)
20372 spec = &die->attrs[i];
20373 }
20374
20375 if (!spec)
20376 break;
c906108c 20377
f2f0e013 20378 die = follow_die_ref (die, spec, &cu);
f2f0e013 20379 }
c5aa993b 20380
c906108c
SS
20381 return NULL;
20382}
20383
348e048f
DE
20384/* Return the named attribute or NULL if not there,
20385 but do not follow DW_AT_specification, etc.
20386 This is for use in contexts where we're reading .debug_types dies.
20387 Following DW_AT_specification, DW_AT_abstract_origin will take us
20388 back up the chain, and we want to go down. */
20389
20390static struct attribute *
45e58e77 20391dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
20392{
20393 unsigned int i;
20394
20395 for (i = 0; i < die->num_attrs; ++i)
20396 if (die->attrs[i].name == name)
20397 return &die->attrs[i];
20398
20399 return NULL;
20400}
20401
7d45c7c3
KB
20402/* Return the string associated with a string-typed attribute, or NULL if it
20403 is either not found or is of an incorrect type. */
20404
20405static const char *
20406dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20407{
20408 struct attribute *attr;
20409 const char *str = NULL;
20410
20411 attr = dwarf2_attr (die, name, cu);
20412
20413 if (attr != NULL)
20414 {
43988095 20415 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438
L
20416 || attr->form == DW_FORM_string
20417 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 20418 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
20419 str = DW_STRING (attr);
20420 else
20421 complaint (&symfile_complaints,
20422 _("string type expected for attribute %s for "
9d8780f0
SM
20423 "DIE at %s in module %s"),
20424 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 20425 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
20426 }
20427
20428 return str;
20429}
20430
05cf31d1
JB
20431/* Return non-zero iff the attribute NAME is defined for the given DIE,
20432 and holds a non-zero value. This function should only be used for
2dc7f7b3 20433 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20434
20435static int
20436dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20437{
20438 struct attribute *attr = dwarf2_attr (die, name, cu);
20439
20440 return (attr && DW_UNSND (attr));
20441}
20442
3ca72b44 20443static int
e142c38c 20444die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20445{
05cf31d1
JB
20446 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20447 which value is non-zero. However, we have to be careful with
20448 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20449 (via dwarf2_flag_true_p) follows this attribute. So we may
20450 end up accidently finding a declaration attribute that belongs
20451 to a different DIE referenced by the specification attribute,
20452 even though the given DIE does not have a declaration attribute. */
20453 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20454 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20455}
20456
63d06c5c 20457/* Return the die giving the specification for DIE, if there is
f2f0e013 20458 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20459 containing the return value on output. If there is no
20460 specification, but there is an abstract origin, that is
20461 returned. */
63d06c5c
DC
20462
20463static struct die_info *
f2f0e013 20464die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20465{
f2f0e013
DJ
20466 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20467 *spec_cu);
63d06c5c 20468
edb3359d
DJ
20469 if (spec_attr == NULL)
20470 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20471
63d06c5c
DC
20472 if (spec_attr == NULL)
20473 return NULL;
20474 else
f2f0e013 20475 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20476}
c906108c 20477
527f3840
JK
20478/* Stub for free_line_header to match void * callback types. */
20479
20480static void
20481free_line_header_voidp (void *arg)
20482{
9a3c8263 20483 struct line_header *lh = (struct line_header *) arg;
527f3840 20484
fff8551c 20485 delete lh;
527f3840
JK
20486}
20487
fff8551c
PA
20488void
20489line_header::add_include_dir (const char *include_dir)
c906108c 20490{
27e0867f 20491 if (dwarf_line_debug >= 2)
fff8551c
PA
20492 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20493 include_dirs.size () + 1, include_dir);
27e0867f 20494
fff8551c 20495 include_dirs.push_back (include_dir);
debd256d 20496}
6e70227d 20497
fff8551c
PA
20498void
20499line_header::add_file_name (const char *name,
ecfb656c 20500 dir_index d_index,
fff8551c
PA
20501 unsigned int mod_time,
20502 unsigned int length)
debd256d 20503{
27e0867f
DE
20504 if (dwarf_line_debug >= 2)
20505 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 20506 (unsigned) file_names.size () + 1, name);
27e0867f 20507
ecfb656c 20508 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 20509}
6e70227d 20510
83769d0b 20511/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20512
20513static struct dwarf2_section_info *
20514get_debug_line_section (struct dwarf2_cu *cu)
20515{
20516 struct dwarf2_section_info *section;
518817b3
SM
20517 struct dwarf2_per_objfile *dwarf2_per_objfile
20518 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
20519
20520 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20521 DWO file. */
20522 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20523 section = &cu->dwo_unit->dwo_file->sections.line;
20524 else if (cu->per_cu->is_dwz)
20525 {
ed2dc618 20526 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
20527
20528 section = &dwz->line;
20529 }
20530 else
20531 section = &dwarf2_per_objfile->line;
20532
20533 return section;
20534}
20535
43988095
JK
20536/* Read directory or file name entry format, starting with byte of
20537 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20538 entries count and the entries themselves in the described entry
20539 format. */
20540
20541static void
ed2dc618
SM
20542read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20543 bfd *abfd, const gdb_byte **bufp,
43988095
JK
20544 struct line_header *lh,
20545 const struct comp_unit_head *cu_header,
20546 void (*callback) (struct line_header *lh,
20547 const char *name,
ecfb656c 20548 dir_index d_index,
43988095
JK
20549 unsigned int mod_time,
20550 unsigned int length))
20551{
20552 gdb_byte format_count, formati;
20553 ULONGEST data_count, datai;
20554 const gdb_byte *buf = *bufp;
20555 const gdb_byte *format_header_data;
43988095
JK
20556 unsigned int bytes_read;
20557
20558 format_count = read_1_byte (abfd, buf);
20559 buf += 1;
20560 format_header_data = buf;
20561 for (formati = 0; formati < format_count; formati++)
20562 {
20563 read_unsigned_leb128 (abfd, buf, &bytes_read);
20564 buf += bytes_read;
20565 read_unsigned_leb128 (abfd, buf, &bytes_read);
20566 buf += bytes_read;
20567 }
20568
20569 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20570 buf += bytes_read;
20571 for (datai = 0; datai < data_count; datai++)
20572 {
20573 const gdb_byte *format = format_header_data;
20574 struct file_entry fe;
20575
43988095
JK
20576 for (formati = 0; formati < format_count; formati++)
20577 {
ecfb656c 20578 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20579 format += bytes_read;
43988095 20580
ecfb656c 20581 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20582 format += bytes_read;
ecfb656c
PA
20583
20584 gdb::optional<const char *> string;
20585 gdb::optional<unsigned int> uint;
20586
43988095
JK
20587 switch (form)
20588 {
20589 case DW_FORM_string:
ecfb656c 20590 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
20591 buf += bytes_read;
20592 break;
20593
20594 case DW_FORM_line_strp:
ed2dc618
SM
20595 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20596 abfd, buf,
ecfb656c
PA
20597 cu_header,
20598 &bytes_read));
43988095
JK
20599 buf += bytes_read;
20600 break;
20601
20602 case DW_FORM_data1:
ecfb656c 20603 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
20604 buf += 1;
20605 break;
20606
20607 case DW_FORM_data2:
ecfb656c 20608 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
20609 buf += 2;
20610 break;
20611
20612 case DW_FORM_data4:
ecfb656c 20613 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
20614 buf += 4;
20615 break;
20616
20617 case DW_FORM_data8:
ecfb656c 20618 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
20619 buf += 8;
20620 break;
20621
20622 case DW_FORM_udata:
ecfb656c 20623 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
20624 buf += bytes_read;
20625 break;
20626
20627 case DW_FORM_block:
20628 /* It is valid only for DW_LNCT_timestamp which is ignored by
20629 current GDB. */
20630 break;
20631 }
ecfb656c
PA
20632
20633 switch (content_type)
20634 {
20635 case DW_LNCT_path:
20636 if (string.has_value ())
20637 fe.name = *string;
20638 break;
20639 case DW_LNCT_directory_index:
20640 if (uint.has_value ())
20641 fe.d_index = (dir_index) *uint;
20642 break;
20643 case DW_LNCT_timestamp:
20644 if (uint.has_value ())
20645 fe.mod_time = *uint;
20646 break;
20647 case DW_LNCT_size:
20648 if (uint.has_value ())
20649 fe.length = *uint;
20650 break;
20651 case DW_LNCT_MD5:
20652 break;
20653 default:
20654 complaint (&symfile_complaints,
20655 _("Unknown format content type %s"),
20656 pulongest (content_type));
20657 }
43988095
JK
20658 }
20659
ecfb656c 20660 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20661 }
20662
20663 *bufp = buf;
20664}
20665
debd256d 20666/* Read the statement program header starting at OFFSET in
3019eac3 20667 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20668 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20669 Returns NULL if there is a problem reading the header, e.g., if it
20670 has a version we don't understand.
debd256d
JB
20671
20672 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20673 the returned object point into the dwarf line section buffer,
20674 and must not be freed. */
ae2de4f8 20675
fff8551c 20676static line_header_up
9c541725 20677dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20678{
d521ce57 20679 const gdb_byte *line_ptr;
c764a876 20680 unsigned int bytes_read, offset_size;
debd256d 20681 int i;
d521ce57 20682 const char *cur_dir, *cur_file;
3019eac3
DE
20683 struct dwarf2_section_info *section;
20684 bfd *abfd;
518817b3
SM
20685 struct dwarf2_per_objfile *dwarf2_per_objfile
20686 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20687
36586728 20688 section = get_debug_line_section (cu);
3019eac3
DE
20689 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20690 if (section->buffer == NULL)
debd256d 20691 {
3019eac3
DE
20692 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20693 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20694 else
20695 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
20696 return 0;
20697 }
20698
fceca515
DE
20699 /* We can't do this until we know the section is non-empty.
20700 Only then do we know we have such a section. */
a32a8923 20701 abfd = get_section_bfd_owner (section);
fceca515 20702
a738430d
MK
20703 /* Make sure that at least there's room for the total_length field.
20704 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20705 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20706 {
4d3c2250 20707 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20708 return 0;
20709 }
20710
fff8551c 20711 line_header_up lh (new line_header ());
debd256d 20712
9c541725 20713 lh->sect_off = sect_off;
527f3840
JK
20714 lh->offset_in_dwz = cu->per_cu->is_dwz;
20715
9c541725 20716 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20717
a738430d 20718 /* Read in the header. */
6e70227d 20719 lh->total_length =
c764a876
DE
20720 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20721 &bytes_read, &offset_size);
debd256d 20722 line_ptr += bytes_read;
3019eac3 20723 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20724 {
4d3c2250 20725 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20726 return 0;
20727 }
20728 lh->statement_program_end = line_ptr + lh->total_length;
20729 lh->version = read_2_bytes (abfd, line_ptr);
20730 line_ptr += 2;
43988095 20731 if (lh->version > 5)
cd366ee8
DE
20732 {
20733 /* This is a version we don't understand. The format could have
20734 changed in ways we don't handle properly so just punt. */
20735 complaint (&symfile_complaints,
20736 _("unsupported version in .debug_line section"));
20737 return NULL;
20738 }
43988095
JK
20739 if (lh->version >= 5)
20740 {
20741 gdb_byte segment_selector_size;
20742
20743 /* Skip address size. */
20744 read_1_byte (abfd, line_ptr);
20745 line_ptr += 1;
20746
20747 segment_selector_size = read_1_byte (abfd, line_ptr);
20748 line_ptr += 1;
20749 if (segment_selector_size != 0)
20750 {
20751 complaint (&symfile_complaints,
20752 _("unsupported segment selector size %u "
20753 "in .debug_line section"),
20754 segment_selector_size);
20755 return NULL;
20756 }
20757 }
c764a876
DE
20758 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20759 line_ptr += offset_size;
debd256d
JB
20760 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20761 line_ptr += 1;
2dc7f7b3
TT
20762 if (lh->version >= 4)
20763 {
20764 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20765 line_ptr += 1;
20766 }
20767 else
20768 lh->maximum_ops_per_instruction = 1;
20769
20770 if (lh->maximum_ops_per_instruction == 0)
20771 {
20772 lh->maximum_ops_per_instruction = 1;
20773 complaint (&symfile_complaints,
3e43a32a
MS
20774 _("invalid maximum_ops_per_instruction "
20775 "in `.debug_line' section"));
2dc7f7b3
TT
20776 }
20777
debd256d
JB
20778 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20779 line_ptr += 1;
20780 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20781 line_ptr += 1;
20782 lh->line_range = read_1_byte (abfd, line_ptr);
20783 line_ptr += 1;
20784 lh->opcode_base = read_1_byte (abfd, line_ptr);
20785 line_ptr += 1;
fff8551c 20786 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20787
20788 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20789 for (i = 1; i < lh->opcode_base; ++i)
20790 {
20791 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20792 line_ptr += 1;
20793 }
20794
43988095 20795 if (lh->version >= 5)
debd256d 20796 {
43988095 20797 /* Read directory table. */
ed2dc618
SM
20798 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20799 &cu->header,
fff8551c 20800 [] (struct line_header *lh, const char *name,
ecfb656c 20801 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20802 unsigned int length)
20803 {
20804 lh->add_include_dir (name);
20805 });
debd256d 20806
43988095 20807 /* Read file name table. */
ed2dc618
SM
20808 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20809 &cu->header,
fff8551c 20810 [] (struct line_header *lh, const char *name,
ecfb656c 20811 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20812 unsigned int length)
20813 {
ecfb656c 20814 lh->add_file_name (name, d_index, mod_time, length);
fff8551c 20815 });
43988095
JK
20816 }
20817 else
debd256d 20818 {
43988095
JK
20819 /* Read directory table. */
20820 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20821 {
20822 line_ptr += bytes_read;
fff8551c 20823 lh->add_include_dir (cur_dir);
43988095 20824 }
debd256d
JB
20825 line_ptr += bytes_read;
20826
43988095
JK
20827 /* Read file name table. */
20828 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20829 {
ecfb656c
PA
20830 unsigned int mod_time, length;
20831 dir_index d_index;
43988095
JK
20832
20833 line_ptr += bytes_read;
ecfb656c 20834 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20835 line_ptr += bytes_read;
20836 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20837 line_ptr += bytes_read;
20838 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20839 line_ptr += bytes_read;
20840
ecfb656c 20841 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20842 }
20843 line_ptr += bytes_read;
debd256d 20844 }
6e70227d 20845 lh->statement_program_start = line_ptr;
debd256d 20846
3019eac3 20847 if (line_ptr > (section->buffer + section->size))
4d3c2250 20848 complaint (&symfile_complaints,
3e43a32a
MS
20849 _("line number info header doesn't "
20850 "fit in `.debug_line' section"));
debd256d 20851
debd256d
JB
20852 return lh;
20853}
c906108c 20854
c6da4cef
DE
20855/* Subroutine of dwarf_decode_lines to simplify it.
20856 Return the file name of the psymtab for included file FILE_INDEX
20857 in line header LH of PST.
20858 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20859 If space for the result is malloc'd, *NAME_HOLDER will be set.
20860 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20861
d521ce57 20862static const char *
c6da4cef
DE
20863psymtab_include_file_name (const struct line_header *lh, int file_index,
20864 const struct partial_symtab *pst,
c89b44cd
TT
20865 const char *comp_dir,
20866 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20867{
8c43009f 20868 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
20869 const char *include_name = fe.name;
20870 const char *include_name_to_compare = include_name;
72b9f47f 20871 const char *pst_filename;
c6da4cef
DE
20872 int file_is_pst;
20873
8c43009f 20874 const char *dir_name = fe.include_dir (lh);
c6da4cef 20875
c89b44cd 20876 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20877 if (!IS_ABSOLUTE_PATH (include_name)
20878 && (dir_name != NULL || comp_dir != NULL))
20879 {
20880 /* Avoid creating a duplicate psymtab for PST.
20881 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20882 Before we do the comparison, however, we need to account
20883 for DIR_NAME and COMP_DIR.
20884 First prepend dir_name (if non-NULL). If we still don't
20885 have an absolute path prepend comp_dir (if non-NULL).
20886 However, the directory we record in the include-file's
20887 psymtab does not contain COMP_DIR (to match the
20888 corresponding symtab(s)).
20889
20890 Example:
20891
20892 bash$ cd /tmp
20893 bash$ gcc -g ./hello.c
20894 include_name = "hello.c"
20895 dir_name = "."
20896 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20897 DW_AT_name = "./hello.c"
20898
20899 */
c6da4cef
DE
20900
20901 if (dir_name != NULL)
20902 {
c89b44cd
TT
20903 name_holder->reset (concat (dir_name, SLASH_STRING,
20904 include_name, (char *) NULL));
20905 include_name = name_holder->get ();
c6da4cef 20906 include_name_to_compare = include_name;
c6da4cef
DE
20907 }
20908 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20909 {
c89b44cd
TT
20910 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20911 include_name, (char *) NULL));
20912 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20913 }
20914 }
20915
20916 pst_filename = pst->filename;
c89b44cd 20917 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20918 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20919 {
c89b44cd
TT
20920 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20921 pst_filename, (char *) NULL));
20922 pst_filename = copied_name.get ();
c6da4cef
DE
20923 }
20924
1e3fad37 20925 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20926
c6da4cef
DE
20927 if (file_is_pst)
20928 return NULL;
20929 return include_name;
20930}
20931
d9b3de22
DE
20932/* State machine to track the state of the line number program. */
20933
6f77053d 20934class lnp_state_machine
d9b3de22 20935{
6f77053d
PA
20936public:
20937 /* Initialize a machine state for the start of a line number
20938 program. */
20939 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20940
8c43009f
PA
20941 file_entry *current_file ()
20942 {
20943 /* lh->file_names is 0-based, but the file name numbers in the
20944 statement program are 1-based. */
6f77053d
PA
20945 return m_line_header->file_name_at (m_file);
20946 }
20947
20948 /* Record the line in the state machine. END_SEQUENCE is true if
20949 we're processing the end of a sequence. */
20950 void record_line (bool end_sequence);
20951
20952 /* Check address and if invalid nop-out the rest of the lines in this
20953 sequence. */
20954 void check_line_address (struct dwarf2_cu *cu,
20955 const gdb_byte *line_ptr,
20956 CORE_ADDR lowpc, CORE_ADDR address);
20957
20958 void handle_set_discriminator (unsigned int discriminator)
20959 {
20960 m_discriminator = discriminator;
20961 m_line_has_non_zero_discriminator |= discriminator != 0;
20962 }
20963
20964 /* Handle DW_LNE_set_address. */
20965 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20966 {
20967 m_op_index = 0;
20968 address += baseaddr;
20969 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20970 }
20971
20972 /* Handle DW_LNS_advance_pc. */
20973 void handle_advance_pc (CORE_ADDR adjust);
20974
20975 /* Handle a special opcode. */
20976 void handle_special_opcode (unsigned char op_code);
20977
20978 /* Handle DW_LNS_advance_line. */
20979 void handle_advance_line (int line_delta)
20980 {
20981 advance_line (line_delta);
20982 }
20983
20984 /* Handle DW_LNS_set_file. */
20985 void handle_set_file (file_name_index file);
20986
20987 /* Handle DW_LNS_negate_stmt. */
20988 void handle_negate_stmt ()
20989 {
20990 m_is_stmt = !m_is_stmt;
20991 }
20992
20993 /* Handle DW_LNS_const_add_pc. */
20994 void handle_const_add_pc ();
20995
20996 /* Handle DW_LNS_fixed_advance_pc. */
20997 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20998 {
20999 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21000 m_op_index = 0;
21001 }
21002
21003 /* Handle DW_LNS_copy. */
21004 void handle_copy ()
21005 {
21006 record_line (false);
21007 m_discriminator = 0;
21008 }
21009
21010 /* Handle DW_LNE_end_sequence. */
21011 void handle_end_sequence ()
21012 {
21013 m_record_line_callback = ::record_line;
21014 }
21015
21016private:
21017 /* Advance the line by LINE_DELTA. */
21018 void advance_line (int line_delta)
21019 {
21020 m_line += line_delta;
21021
21022 if (line_delta != 0)
21023 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
21024 }
21025
6f77053d
PA
21026 gdbarch *m_gdbarch;
21027
21028 /* True if we're recording lines.
21029 Otherwise we're building partial symtabs and are just interested in
21030 finding include files mentioned by the line number program. */
21031 bool m_record_lines_p;
21032
8c43009f 21033 /* The line number header. */
6f77053d 21034 line_header *m_line_header;
8c43009f 21035
6f77053d
PA
21036 /* These are part of the standard DWARF line number state machine,
21037 and initialized according to the DWARF spec. */
d9b3de22 21038
6f77053d 21039 unsigned char m_op_index = 0;
8c43009f 21040 /* The line table index (1-based) of the current file. */
6f77053d
PA
21041 file_name_index m_file = (file_name_index) 1;
21042 unsigned int m_line = 1;
21043
21044 /* These are initialized in the constructor. */
21045
21046 CORE_ADDR m_address;
21047 bool m_is_stmt;
21048 unsigned int m_discriminator;
d9b3de22
DE
21049
21050 /* Additional bits of state we need to track. */
21051
21052 /* The last file that we called dwarf2_start_subfile for.
21053 This is only used for TLLs. */
6f77053d 21054 unsigned int m_last_file = 0;
d9b3de22 21055 /* The last file a line number was recorded for. */
6f77053d 21056 struct subfile *m_last_subfile = NULL;
d9b3de22
DE
21057
21058 /* The function to call to record a line. */
6f77053d 21059 record_line_ftype *m_record_line_callback = NULL;
d9b3de22
DE
21060
21061 /* The last line number that was recorded, used to coalesce
21062 consecutive entries for the same line. This can happen, for
21063 example, when discriminators are present. PR 17276. */
6f77053d
PA
21064 unsigned int m_last_line = 0;
21065 bool m_line_has_non_zero_discriminator = false;
8c43009f 21066};
d9b3de22 21067
6f77053d
PA
21068void
21069lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21070{
21071 CORE_ADDR addr_adj = (((m_op_index + adjust)
21072 / m_line_header->maximum_ops_per_instruction)
21073 * m_line_header->minimum_instruction_length);
21074 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21075 m_op_index = ((m_op_index + adjust)
21076 % m_line_header->maximum_ops_per_instruction);
21077}
d9b3de22 21078
6f77053d
PA
21079void
21080lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 21081{
6f77053d
PA
21082 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21083 CORE_ADDR addr_adj = (((m_op_index
21084 + (adj_opcode / m_line_header->line_range))
21085 / m_line_header->maximum_ops_per_instruction)
21086 * m_line_header->minimum_instruction_length);
21087 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21088 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
21089 % m_line_header->maximum_ops_per_instruction);
d9b3de22 21090
6f77053d
PA
21091 int line_delta = (m_line_header->line_base
21092 + (adj_opcode % m_line_header->line_range));
21093 advance_line (line_delta);
21094 record_line (false);
21095 m_discriminator = 0;
21096}
d9b3de22 21097
6f77053d
PA
21098void
21099lnp_state_machine::handle_set_file (file_name_index file)
21100{
21101 m_file = file;
21102
21103 const file_entry *fe = current_file ();
21104 if (fe == NULL)
21105 dwarf2_debug_line_missing_file_complaint ();
21106 else if (m_record_lines_p)
21107 {
21108 const char *dir = fe->include_dir (m_line_header);
21109
21110 m_last_subfile = current_subfile;
21111 m_line_has_non_zero_discriminator = m_discriminator != 0;
21112 dwarf2_start_subfile (fe->name, dir);
21113 }
21114}
21115
21116void
21117lnp_state_machine::handle_const_add_pc ()
21118{
21119 CORE_ADDR adjust
21120 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21121
21122 CORE_ADDR addr_adj
21123 = (((m_op_index + adjust)
21124 / m_line_header->maximum_ops_per_instruction)
21125 * m_line_header->minimum_instruction_length);
21126
21127 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21128 m_op_index = ((m_op_index + adjust)
21129 % m_line_header->maximum_ops_per_instruction);
21130}
d9b3de22 21131
c91513d8
PP
21132/* Ignore this record_line request. */
21133
21134static void
21135noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
21136{
21137 return;
21138}
21139
a05a36a5
DE
21140/* Return non-zero if we should add LINE to the line number table.
21141 LINE is the line to add, LAST_LINE is the last line that was added,
21142 LAST_SUBFILE is the subfile for LAST_LINE.
21143 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21144 had a non-zero discriminator.
21145
21146 We have to be careful in the presence of discriminators.
21147 E.g., for this line:
21148
21149 for (i = 0; i < 100000; i++);
21150
21151 clang can emit four line number entries for that one line,
21152 each with a different discriminator.
21153 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21154
21155 However, we want gdb to coalesce all four entries into one.
21156 Otherwise the user could stepi into the middle of the line and
21157 gdb would get confused about whether the pc really was in the
21158 middle of the line.
21159
21160 Things are further complicated by the fact that two consecutive
21161 line number entries for the same line is a heuristic used by gcc
21162 to denote the end of the prologue. So we can't just discard duplicate
21163 entries, we have to be selective about it. The heuristic we use is
21164 that we only collapse consecutive entries for the same line if at least
21165 one of those entries has a non-zero discriminator. PR 17276.
21166
21167 Note: Addresses in the line number state machine can never go backwards
21168 within one sequence, thus this coalescing is ok. */
21169
21170static int
21171dwarf_record_line_p (unsigned int line, unsigned int last_line,
21172 int line_has_non_zero_discriminator,
21173 struct subfile *last_subfile)
21174{
21175 if (current_subfile != last_subfile)
21176 return 1;
21177 if (line != last_line)
21178 return 1;
21179 /* Same line for the same file that we've seen already.
21180 As a last check, for pr 17276, only record the line if the line
21181 has never had a non-zero discriminator. */
21182 if (!line_has_non_zero_discriminator)
21183 return 1;
21184 return 0;
21185}
21186
252a6764
DE
21187/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
21188 in the line table of subfile SUBFILE. */
21189
21190static void
d9b3de22
DE
21191dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21192 unsigned int line, CORE_ADDR address,
21193 record_line_ftype p_record_line)
252a6764
DE
21194{
21195 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21196
27e0867f
DE
21197 if (dwarf_line_debug)
21198 {
21199 fprintf_unfiltered (gdb_stdlog,
21200 "Recording line %u, file %s, address %s\n",
21201 line, lbasename (subfile->name),
21202 paddress (gdbarch, address));
21203 }
21204
d5962de5 21205 (*p_record_line) (subfile, line, addr);
252a6764
DE
21206}
21207
21208/* Subroutine of dwarf_decode_lines_1 to simplify it.
21209 Mark the end of a set of line number records.
d9b3de22 21210 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
21211 If SUBFILE is NULL the request is ignored. */
21212
21213static void
21214dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21215 CORE_ADDR address, record_line_ftype p_record_line)
21216{
27e0867f
DE
21217 if (subfile == NULL)
21218 return;
21219
21220 if (dwarf_line_debug)
21221 {
21222 fprintf_unfiltered (gdb_stdlog,
21223 "Finishing current line, file %s, address %s\n",
21224 lbasename (subfile->name),
21225 paddress (gdbarch, address));
21226 }
21227
d9b3de22
DE
21228 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
21229}
21230
6f77053d
PA
21231void
21232lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21233{
d9b3de22
DE
21234 if (dwarf_line_debug)
21235 {
21236 fprintf_unfiltered (gdb_stdlog,
21237 "Processing actual line %u: file %u,"
21238 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
21239 m_line, to_underlying (m_file),
21240 paddress (m_gdbarch, m_address),
21241 m_is_stmt, m_discriminator);
d9b3de22
DE
21242 }
21243
6f77053d 21244 file_entry *fe = current_file ();
8c43009f
PA
21245
21246 if (fe == NULL)
d9b3de22
DE
21247 dwarf2_debug_line_missing_file_complaint ();
21248 /* For now we ignore lines not starting on an instruction boundary.
21249 But not when processing end_sequence for compatibility with the
21250 previous version of the code. */
6f77053d 21251 else if (m_op_index == 0 || end_sequence)
d9b3de22 21252 {
8c43009f 21253 fe->included_p = 1;
6f77053d 21254 if (m_record_lines_p && m_is_stmt)
d9b3de22 21255 {
6f77053d 21256 if (m_last_subfile != current_subfile || end_sequence)
d9b3de22 21257 {
6f77053d
PA
21258 dwarf_finish_line (m_gdbarch, m_last_subfile,
21259 m_address, m_record_line_callback);
d9b3de22
DE
21260 }
21261
21262 if (!end_sequence)
21263 {
6f77053d
PA
21264 if (dwarf_record_line_p (m_line, m_last_line,
21265 m_line_has_non_zero_discriminator,
21266 m_last_subfile))
d9b3de22 21267 {
6f77053d
PA
21268 dwarf_record_line_1 (m_gdbarch, current_subfile,
21269 m_line, m_address,
21270 m_record_line_callback);
d9b3de22 21271 }
6f77053d
PA
21272 m_last_subfile = current_subfile;
21273 m_last_line = m_line;
d9b3de22
DE
21274 }
21275 }
21276 }
21277}
21278
6f77053d
PA
21279lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
21280 bool record_lines_p)
d9b3de22 21281{
6f77053d
PA
21282 m_gdbarch = arch;
21283 m_record_lines_p = record_lines_p;
21284 m_line_header = lh;
d9b3de22 21285
6f77053d 21286 m_record_line_callback = ::record_line;
d9b3de22 21287
d9b3de22
DE
21288 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21289 was a line entry for it so that the backend has a chance to adjust it
21290 and also record it in case it needs it. This is currently used by MIPS
21291 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21292 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21293 m_is_stmt = lh->default_is_stmt;
21294 m_discriminator = 0;
252a6764
DE
21295}
21296
6f77053d
PA
21297void
21298lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21299 const gdb_byte *line_ptr,
21300 CORE_ADDR lowpc, CORE_ADDR address)
924c2928
DE
21301{
21302 /* If address < lowpc then it's not a usable value, it's outside the
21303 pc range of the CU. However, we restrict the test to only address
21304 values of zero to preserve GDB's previous behaviour which is to
21305 handle the specific case of a function being GC'd by the linker. */
21306
21307 if (address == 0 && address < lowpc)
21308 {
21309 /* This line table is for a function which has been
21310 GCd by the linker. Ignore it. PR gdb/12528 */
21311
518817b3 21312 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
21313 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21314
21315 complaint (&symfile_complaints,
21316 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21317 line_offset, objfile_name (objfile));
6f77053d
PA
21318 m_record_line_callback = noop_record_line;
21319 /* Note: record_line_callback is left as noop_record_line until
21320 we see DW_LNE_end_sequence. */
924c2928
DE
21321 }
21322}
21323
f3f5162e 21324/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21325 Process the line number information in LH.
21326 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21327 program in order to set included_p for every referenced header. */
debd256d 21328
c906108c 21329static void
43f3e411
DE
21330dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21331 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21332{
d521ce57
TT
21333 const gdb_byte *line_ptr, *extended_end;
21334 const gdb_byte *line_end;
a8c50c1f 21335 unsigned int bytes_read, extended_len;
699ca60a 21336 unsigned char op_code, extended_op;
e142c38c 21337 CORE_ADDR baseaddr;
518817b3 21338 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21339 bfd *abfd = objfile->obfd;
fbf65064 21340 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
21341 /* True if we're recording line info (as opposed to building partial
21342 symtabs and just interested in finding include files mentioned by
21343 the line number program). */
21344 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
21345
21346 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21347
debd256d
JB
21348 line_ptr = lh->statement_program_start;
21349 line_end = lh->statement_program_end;
c906108c
SS
21350
21351 /* Read the statement sequences until there's nothing left. */
21352 while (line_ptr < line_end)
21353 {
6f77053d
PA
21354 /* The DWARF line number program state machine. Reset the state
21355 machine at the start of each sequence. */
21356 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
21357 bool end_sequence = false;
d9b3de22 21358
8c43009f 21359 if (record_lines_p)
c906108c 21360 {
8c43009f
PA
21361 /* Start a subfile for the current file of the state
21362 machine. */
21363 const file_entry *fe = state_machine.current_file ();
21364
21365 if (fe != NULL)
21366 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
c906108c
SS
21367 }
21368
a738430d 21369 /* Decode the table. */
d9b3de22 21370 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21371 {
21372 op_code = read_1_byte (abfd, line_ptr);
21373 line_ptr += 1;
9aa1fe7e 21374
debd256d 21375 if (op_code >= lh->opcode_base)
6e70227d 21376 {
8e07a239 21377 /* Special opcode. */
6f77053d 21378 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21379 }
21380 else switch (op_code)
c906108c
SS
21381 {
21382 case DW_LNS_extended_op:
3e43a32a
MS
21383 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21384 &bytes_read);
473b7be6 21385 line_ptr += bytes_read;
a8c50c1f 21386 extended_end = line_ptr + extended_len;
c906108c
SS
21387 extended_op = read_1_byte (abfd, line_ptr);
21388 line_ptr += 1;
21389 switch (extended_op)
21390 {
21391 case DW_LNE_end_sequence:
6f77053d
PA
21392 state_machine.handle_end_sequence ();
21393 end_sequence = true;
c906108c
SS
21394 break;
21395 case DW_LNE_set_address:
d9b3de22
DE
21396 {
21397 CORE_ADDR address
21398 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 21399 line_ptr += bytes_read;
6f77053d
PA
21400
21401 state_machine.check_line_address (cu, line_ptr,
21402 lowpc, address);
21403 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21404 }
c906108c
SS
21405 break;
21406 case DW_LNE_define_file:
debd256d 21407 {
d521ce57 21408 const char *cur_file;
ecfb656c
PA
21409 unsigned int mod_time, length;
21410 dir_index dindex;
6e70227d 21411
3e43a32a
MS
21412 cur_file = read_direct_string (abfd, line_ptr,
21413 &bytes_read);
debd256d 21414 line_ptr += bytes_read;
ecfb656c 21415 dindex = (dir_index)
debd256d
JB
21416 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21417 line_ptr += bytes_read;
21418 mod_time =
21419 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21420 line_ptr += bytes_read;
21421 length =
21422 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21423 line_ptr += bytes_read;
ecfb656c 21424 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 21425 }
c906108c 21426 break;
d0c6ba3d 21427 case DW_LNE_set_discriminator:
6f77053d
PA
21428 {
21429 /* The discriminator is not interesting to the
21430 debugger; just ignore it. We still need to
21431 check its value though:
21432 if there are consecutive entries for the same
21433 (non-prologue) line we want to coalesce them.
21434 PR 17276. */
21435 unsigned int discr
21436 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21437 line_ptr += bytes_read;
21438
21439 state_machine.handle_set_discriminator (discr);
21440 }
d0c6ba3d 21441 break;
c906108c 21442 default:
4d3c2250 21443 complaint (&symfile_complaints,
e2e0b3e5 21444 _("mangled .debug_line section"));
debd256d 21445 return;
c906108c 21446 }
a8c50c1f
DJ
21447 /* Make sure that we parsed the extended op correctly. If e.g.
21448 we expected a different address size than the producer used,
21449 we may have read the wrong number of bytes. */
21450 if (line_ptr != extended_end)
21451 {
21452 complaint (&symfile_complaints,
21453 _("mangled .debug_line section"));
21454 return;
21455 }
c906108c
SS
21456 break;
21457 case DW_LNS_copy:
6f77053d 21458 state_machine.handle_copy ();
c906108c
SS
21459 break;
21460 case DW_LNS_advance_pc:
2dc7f7b3
TT
21461 {
21462 CORE_ADDR adjust
21463 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21464 line_ptr += bytes_read;
6f77053d
PA
21465
21466 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21467 }
c906108c
SS
21468 break;
21469 case DW_LNS_advance_line:
a05a36a5
DE
21470 {
21471 int line_delta
21472 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21473 line_ptr += bytes_read;
6f77053d
PA
21474
21475 state_machine.handle_advance_line (line_delta);
a05a36a5 21476 }
c906108c
SS
21477 break;
21478 case DW_LNS_set_file:
d9b3de22 21479 {
6f77053d 21480 file_name_index file
ecfb656c
PA
21481 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21482 &bytes_read);
d9b3de22 21483 line_ptr += bytes_read;
8c43009f 21484
6f77053d 21485 state_machine.handle_set_file (file);
d9b3de22 21486 }
c906108c
SS
21487 break;
21488 case DW_LNS_set_column:
0ad93d4f 21489 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21490 line_ptr += bytes_read;
21491 break;
21492 case DW_LNS_negate_stmt:
6f77053d 21493 state_machine.handle_negate_stmt ();
c906108c
SS
21494 break;
21495 case DW_LNS_set_basic_block:
c906108c 21496 break;
c2c6d25f
JM
21497 /* Add to the address register of the state machine the
21498 address increment value corresponding to special opcode
a738430d
MK
21499 255. I.e., this value is scaled by the minimum
21500 instruction length since special opcode 255 would have
b021a221 21501 scaled the increment. */
c906108c 21502 case DW_LNS_const_add_pc:
6f77053d 21503 state_machine.handle_const_add_pc ();
c906108c
SS
21504 break;
21505 case DW_LNS_fixed_advance_pc:
3e29f34a 21506 {
6f77053d 21507 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21508 line_ptr += 2;
6f77053d
PA
21509
21510 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21511 }
c906108c 21512 break;
9aa1fe7e 21513 default:
a738430d
MK
21514 {
21515 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21516 int i;
a738430d 21517
debd256d 21518 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21519 {
21520 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21521 line_ptr += bytes_read;
21522 }
21523 }
c906108c
SS
21524 }
21525 }
d9b3de22
DE
21526
21527 if (!end_sequence)
21528 dwarf2_debug_line_missing_end_sequence_complaint ();
21529
21530 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21531 in which case we still finish recording the last line). */
6f77053d 21532 state_machine.record_line (true);
c906108c 21533 }
f3f5162e
DE
21534}
21535
21536/* Decode the Line Number Program (LNP) for the given line_header
21537 structure and CU. The actual information extracted and the type
21538 of structures created from the LNP depends on the value of PST.
21539
21540 1. If PST is NULL, then this procedure uses the data from the program
21541 to create all necessary symbol tables, and their linetables.
21542
21543 2. If PST is not NULL, this procedure reads the program to determine
21544 the list of files included by the unit represented by PST, and
21545 builds all the associated partial symbol tables.
21546
21547 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21548 It is used for relative paths in the line table.
21549 NOTE: When processing partial symtabs (pst != NULL),
21550 comp_dir == pst->dirname.
21551
21552 NOTE: It is important that psymtabs have the same file name (via strcmp)
21553 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21554 symtab we don't use it in the name of the psymtabs we create.
21555 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21556 A good testcase for this is mb-inline.exp.
21557
527f3840
JK
21558 LOWPC is the lowest address in CU (or 0 if not known).
21559
21560 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21561 for its PC<->lines mapping information. Otherwise only the filename
21562 table is read in. */
f3f5162e
DE
21563
21564static void
21565dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 21566 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 21567 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21568{
518817b3 21569 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21570 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21571
527f3840
JK
21572 if (decode_mapping)
21573 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21574
21575 if (decode_for_pst_p)
21576 {
21577 int file_index;
21578
21579 /* Now that we're done scanning the Line Header Program, we can
21580 create the psymtab of each included file. */
fff8551c 21581 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
21582 if (lh->file_names[file_index].included_p == 1)
21583 {
c89b44cd 21584 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21585 const char *include_name =
c89b44cd
TT
21586 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21587 &name_holder);
c6da4cef 21588 if (include_name != NULL)
aaa75496
JB
21589 dwarf2_create_include_psymtab (include_name, pst, objfile);
21590 }
21591 }
cb1df416
DJ
21592 else
21593 {
21594 /* Make sure a symtab is created for every file, even files
21595 which contain only variables (i.e. no code with associated
21596 line numbers). */
43f3e411 21597 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 21598 int i;
cb1df416 21599
fff8551c 21600 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 21601 {
8c43009f 21602 file_entry &fe = lh->file_names[i];
9a619af0 21603
8c43009f 21604 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
cb1df416 21605
cb1df416 21606 if (current_subfile->symtab == NULL)
43f3e411
DE
21607 {
21608 current_subfile->symtab
21609 = allocate_symtab (cust, current_subfile->name);
21610 }
8c43009f 21611 fe.symtab = current_subfile->symtab;
cb1df416
DJ
21612 }
21613 }
c906108c
SS
21614}
21615
21616/* Start a subfile for DWARF. FILENAME is the name of the file and
21617 DIRNAME the name of the source directory which contains FILENAME
4d663531 21618 or NULL if not known.
c906108c
SS
21619 This routine tries to keep line numbers from identical absolute and
21620 relative file names in a common subfile.
21621
21622 Using the `list' example from the GDB testsuite, which resides in
21623 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21624 of /srcdir/list0.c yields the following debugging information for list0.c:
21625
c5aa993b 21626 DW_AT_name: /srcdir/list0.c
4d663531 21627 DW_AT_comp_dir: /compdir
357e46e7 21628 files.files[0].name: list0.h
c5aa993b 21629 files.files[0].dir: /srcdir
357e46e7 21630 files.files[1].name: list0.c
c5aa993b 21631 files.files[1].dir: /srcdir
c906108c
SS
21632
21633 The line number information for list0.c has to end up in a single
4f1520fb
FR
21634 subfile, so that `break /srcdir/list0.c:1' works as expected.
21635 start_subfile will ensure that this happens provided that we pass the
21636 concatenation of files.files[1].dir and files.files[1].name as the
21637 subfile's name. */
c906108c
SS
21638
21639static void
4d663531 21640dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 21641{
d521ce57 21642 char *copy = NULL;
4f1520fb 21643
4d663531 21644 /* In order not to lose the line information directory,
4f1520fb
FR
21645 we concatenate it to the filename when it makes sense.
21646 Note that the Dwarf3 standard says (speaking of filenames in line
21647 information): ``The directory index is ignored for file names
21648 that represent full path names''. Thus ignoring dirname in the
21649 `else' branch below isn't an issue. */
c906108c 21650
d5166ae1 21651 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
21652 {
21653 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21654 filename = copy;
21655 }
c906108c 21656
4d663531 21657 start_subfile (filename);
4f1520fb 21658
d521ce57
TT
21659 if (copy != NULL)
21660 xfree (copy);
c906108c
SS
21661}
21662
f4dc4d17
DE
21663/* Start a symtab for DWARF.
21664 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21665
43f3e411 21666static struct compunit_symtab *
f4dc4d17 21667dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 21668 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 21669{
43f3e411 21670 struct compunit_symtab *cust
518817b3
SM
21671 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21672 low_pc, cu->language);
43f3e411 21673
f4dc4d17
DE
21674 record_debugformat ("DWARF 2");
21675 record_producer (cu->producer);
21676
21677 /* We assume that we're processing GCC output. */
21678 processing_gcc_compilation = 2;
21679
4d4ec4e5 21680 cu->processing_has_namespace_info = 0;
43f3e411
DE
21681
21682 return cust;
f4dc4d17
DE
21683}
21684
4c2df51b
DJ
21685static void
21686var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21687 struct dwarf2_cu *cu)
4c2df51b 21688{
518817b3 21689 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21690 struct comp_unit_head *cu_header = &cu->header;
21691
4c2df51b
DJ
21692 /* NOTE drow/2003-01-30: There used to be a comment and some special
21693 code here to turn a symbol with DW_AT_external and a
21694 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21695 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21696 with some versions of binutils) where shared libraries could have
21697 relocations against symbols in their debug information - the
21698 minimal symbol would have the right address, but the debug info
21699 would not. It's no longer necessary, because we will explicitly
21700 apply relocations when we read in the debug information now. */
21701
21702 /* A DW_AT_location attribute with no contents indicates that a
21703 variable has been optimized away. */
21704 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21705 {
f1e6e072 21706 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21707 return;
21708 }
21709
21710 /* Handle one degenerate form of location expression specially, to
21711 preserve GDB's previous behavior when section offsets are
3019eac3
DE
21712 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21713 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21714
21715 if (attr_form_is_block (attr)
3019eac3
DE
21716 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21717 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21718 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21719 && (DW_BLOCK (attr)->size
21720 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21721 {
891d2f0b 21722 unsigned int dummy;
4c2df51b 21723
3019eac3
DE
21724 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21725 SYMBOL_VALUE_ADDRESS (sym) =
21726 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21727 else
21728 SYMBOL_VALUE_ADDRESS (sym) =
21729 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 21730 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
21731 fixup_symbol_section (sym, objfile);
21732 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21733 SYMBOL_SECTION (sym));
4c2df51b
DJ
21734 return;
21735 }
21736
21737 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21738 expression evaluator, and use LOC_COMPUTED only when necessary
21739 (i.e. when the value of a register or memory location is
21740 referenced, or a thread-local block, etc.). Then again, it might
21741 not be worthwhile. I'm assuming that it isn't unless performance
21742 or memory numbers show me otherwise. */
21743
f1e6e072 21744 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21745
f1e6e072 21746 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 21747 cu->has_loclist = 1;
4c2df51b
DJ
21748}
21749
c906108c
SS
21750/* Given a pointer to a DWARF information entry, figure out if we need
21751 to make a symbol table entry for it, and if so, create a new entry
21752 and return a pointer to it.
21753 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21754 used the passed type.
21755 If SPACE is not NULL, use it to hold the new symbol. If it is
21756 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21757
21758static struct symbol *
5e2db402
TT
21759new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21760 struct symbol *space)
c906108c 21761{
518817b3
SM
21762 struct dwarf2_per_objfile *dwarf2_per_objfile
21763 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21764 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21765 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21766 struct symbol *sym = NULL;
15d034d0 21767 const char *name;
c906108c
SS
21768 struct attribute *attr = NULL;
21769 struct attribute *attr2 = NULL;
e142c38c 21770 CORE_ADDR baseaddr;
e37fd15a
SW
21771 struct pending **list_to_add = NULL;
21772
edb3359d 21773 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
21774
21775 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21776
94af9270 21777 name = dwarf2_name (die, cu);
c906108c
SS
21778 if (name)
21779 {
94af9270 21780 const char *linkagename;
34eaf542 21781 int suppress_add = 0;
94af9270 21782
34eaf542
TT
21783 if (space)
21784 sym = space;
21785 else
e623cf5d 21786 sym = allocate_symbol (objfile);
c906108c 21787 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21788
21789 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 21790 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
21791 linkagename = dwarf2_physname (name, die, cu);
21792 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 21793
f55ee35c
JK
21794 /* Fortran does not have mangling standard and the mangling does differ
21795 between gfortran, iFort etc. */
21796 if (cu->language == language_fortran
b250c185 21797 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 21798 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 21799 dwarf2_full_name (name, die, cu),
29df156d 21800 NULL);
f55ee35c 21801
c906108c 21802 /* Default assumptions.
c5aa993b 21803 Use the passed type or decode it from the die. */
176620f1 21804 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21805 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21806 if (type != NULL)
21807 SYMBOL_TYPE (sym) = type;
21808 else
e7c27a73 21809 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21810 attr = dwarf2_attr (die,
21811 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21812 cu);
c906108c
SS
21813 if (attr)
21814 {
21815 SYMBOL_LINE (sym) = DW_UNSND (attr);
21816 }
cb1df416 21817
edb3359d
DJ
21818 attr = dwarf2_attr (die,
21819 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21820 cu);
cb1df416
DJ
21821 if (attr)
21822 {
ecfb656c 21823 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21824 struct file_entry *fe;
9a619af0 21825
ecfb656c
PA
21826 if (cu->line_header != NULL)
21827 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21828 else
21829 fe = NULL;
21830
21831 if (fe == NULL)
cb1df416
DJ
21832 complaint (&symfile_complaints,
21833 _("file index out of range"));
8c43009f
PA
21834 else
21835 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21836 }
21837
c906108c
SS
21838 switch (die->tag)
21839 {
21840 case DW_TAG_label:
e142c38c 21841 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 21842 if (attr)
3e29f34a
MR
21843 {
21844 CORE_ADDR addr;
21845
21846 addr = attr_value_as_address (attr);
21847 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21848 SYMBOL_VALUE_ADDRESS (sym) = addr;
21849 }
0f5238ed
TT
21850 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21851 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21852 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 21853 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21854 break;
21855 case DW_TAG_subprogram:
21856 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21857 finish_block. */
f1e6e072 21858 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21859 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
21860 if ((attr2 && (DW_UNSND (attr2) != 0))
21861 || cu->language == language_ada)
c906108c 21862 {
2cfa0c8d
JB
21863 /* Subprograms marked external are stored as a global symbol.
21864 Ada subprograms, whether marked external or not, are always
21865 stored as a global symbol, because we want to be able to
21866 access them globally. For instance, we want to be able
21867 to break on a nested subprogram without having to
21868 specify the context. */
e37fd15a 21869 list_to_add = &global_symbols;
c906108c
SS
21870 }
21871 else
21872 {
e37fd15a 21873 list_to_add = cu->list_in_scope;
c906108c
SS
21874 }
21875 break;
edb3359d
DJ
21876 case DW_TAG_inlined_subroutine:
21877 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21878 finish_block. */
f1e6e072 21879 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21880 SYMBOL_INLINED (sym) = 1;
481860b3 21881 list_to_add = cu->list_in_scope;
edb3359d 21882 break;
34eaf542
TT
21883 case DW_TAG_template_value_param:
21884 suppress_add = 1;
21885 /* Fall through. */
72929c62 21886 case DW_TAG_constant:
c906108c 21887 case DW_TAG_variable:
254e6b9e 21888 case DW_TAG_member:
0963b4bd
MS
21889 /* Compilation with minimal debug info may result in
21890 variables with missing type entries. Change the
21891 misleading `void' type to something sensible. */
c906108c 21892 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21893 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21894
e142c38c 21895 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21896 /* In the case of DW_TAG_member, we should only be called for
21897 static const members. */
21898 if (die->tag == DW_TAG_member)
21899 {
3863f96c
DE
21900 /* dwarf2_add_field uses die_is_declaration,
21901 so we do the same. */
254e6b9e
DE
21902 gdb_assert (die_is_declaration (die, cu));
21903 gdb_assert (attr);
21904 }
c906108c
SS
21905 if (attr)
21906 {
e7c27a73 21907 dwarf2_const_value (attr, sym, cu);
e142c38c 21908 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21909 if (!suppress_add)
34eaf542
TT
21910 {
21911 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 21912 list_to_add = &global_symbols;
34eaf542 21913 else
e37fd15a 21914 list_to_add = cu->list_in_scope;
34eaf542 21915 }
c906108c
SS
21916 break;
21917 }
e142c38c 21918 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21919 if (attr)
21920 {
e7c27a73 21921 var_decode_location (attr, sym, cu);
e142c38c 21922 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21923
21924 /* Fortran explicitly imports any global symbols to the local
21925 scope by DW_TAG_common_block. */
21926 if (cu->language == language_fortran && die->parent
21927 && die->parent->tag == DW_TAG_common_block)
21928 attr2 = NULL;
21929
caac4577
JG
21930 if (SYMBOL_CLASS (sym) == LOC_STATIC
21931 && SYMBOL_VALUE_ADDRESS (sym) == 0
21932 && !dwarf2_per_objfile->has_section_at_zero)
21933 {
21934 /* When a static variable is eliminated by the linker,
21935 the corresponding debug information is not stripped
21936 out, but the variable address is set to null;
21937 do not add such variables into symbol table. */
21938 }
21939 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21940 {
f55ee35c
JK
21941 /* Workaround gfortran PR debug/40040 - it uses
21942 DW_AT_location for variables in -fPIC libraries which may
21943 get overriden by other libraries/executable and get
21944 a different address. Resolve it by the minimal symbol
21945 which may come from inferior's executable using copy
21946 relocation. Make this workaround only for gfortran as for
21947 other compilers GDB cannot guess the minimal symbol
21948 Fortran mangling kind. */
21949 if (cu->language == language_fortran && die->parent
21950 && die->parent->tag == DW_TAG_module
21951 && cu->producer
28586665 21952 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 21953 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 21954
1c809c68
TT
21955 /* A variable with DW_AT_external is never static,
21956 but it may be block-scoped. */
21957 list_to_add = (cu->list_in_scope == &file_symbols
21958 ? &global_symbols : cu->list_in_scope);
1c809c68 21959 }
c906108c 21960 else
e37fd15a 21961 list_to_add = cu->list_in_scope;
c906108c
SS
21962 }
21963 else
21964 {
21965 /* We do not know the address of this symbol.
c5aa993b
JM
21966 If it is an external symbol and we have type information
21967 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21968 The address of the variable will then be determined from
21969 the minimal symbol table whenever the variable is
21970 referenced. */
e142c38c 21971 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21972
21973 /* Fortran explicitly imports any global symbols to the local
21974 scope by DW_TAG_common_block. */
21975 if (cu->language == language_fortran && die->parent
21976 && die->parent->tag == DW_TAG_common_block)
21977 {
21978 /* SYMBOL_CLASS doesn't matter here because
21979 read_common_block is going to reset it. */
21980 if (!suppress_add)
21981 list_to_add = cu->list_in_scope;
21982 }
21983 else if (attr2 && (DW_UNSND (attr2) != 0)
21984 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21985 {
0fe7935b
DJ
21986 /* A variable with DW_AT_external is never static, but it
21987 may be block-scoped. */
21988 list_to_add = (cu->list_in_scope == &file_symbols
21989 ? &global_symbols : cu->list_in_scope);
21990
f1e6e072 21991 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21992 }
442ddf59
JK
21993 else if (!die_is_declaration (die, cu))
21994 {
21995 /* Use the default LOC_OPTIMIZED_OUT class. */
21996 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21997 if (!suppress_add)
21998 list_to_add = cu->list_in_scope;
442ddf59 21999 }
c906108c
SS
22000 }
22001 break;
22002 case DW_TAG_formal_parameter:
edb3359d
DJ
22003 /* If we are inside a function, mark this as an argument. If
22004 not, we might be looking at an argument to an inlined function
22005 when we do not have enough information to show inlined frames;
22006 pretend it's a local variable in that case so that the user can
22007 still see it. */
22008 if (context_stack_depth > 0
22009 && context_stack[context_stack_depth - 1].name != NULL)
22010 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 22011 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
22012 if (attr)
22013 {
e7c27a73 22014 var_decode_location (attr, sym, cu);
c906108c 22015 }
e142c38c 22016 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
22017 if (attr)
22018 {
e7c27a73 22019 dwarf2_const_value (attr, sym, cu);
c906108c 22020 }
f346a30d 22021
e37fd15a 22022 list_to_add = cu->list_in_scope;
c906108c
SS
22023 break;
22024 case DW_TAG_unspecified_parameters:
22025 /* From varargs functions; gdb doesn't seem to have any
22026 interest in this information, so just ignore it for now.
22027 (FIXME?) */
22028 break;
34eaf542
TT
22029 case DW_TAG_template_type_param:
22030 suppress_add = 1;
22031 /* Fall through. */
c906108c 22032 case DW_TAG_class_type:
680b30c7 22033 case DW_TAG_interface_type:
c906108c
SS
22034 case DW_TAG_structure_type:
22035 case DW_TAG_union_type:
72019c9c 22036 case DW_TAG_set_type:
c906108c 22037 case DW_TAG_enumeration_type:
f1e6e072 22038 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22039 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 22040
63d06c5c 22041 {
9c37b5ae 22042 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
22043 really ever be static objects: otherwise, if you try
22044 to, say, break of a class's method and you're in a file
22045 which doesn't mention that class, it won't work unless
22046 the check for all static symbols in lookup_symbol_aux
22047 saves you. See the OtherFileClass tests in
22048 gdb.c++/namespace.exp. */
22049
e37fd15a 22050 if (!suppress_add)
34eaf542 22051 {
34eaf542 22052 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 22053 && cu->language == language_cplus
34eaf542 22054 ? &global_symbols : cu->list_in_scope);
63d06c5c 22055
64382290 22056 /* The semantics of C++ state that "struct foo {
9c37b5ae 22057 ... }" also defines a typedef for "foo". */
64382290 22058 if (cu->language == language_cplus
45280282 22059 || cu->language == language_ada
c44af4eb
TT
22060 || cu->language == language_d
22061 || cu->language == language_rust)
64382290
TT
22062 {
22063 /* The symbol's name is already allocated along
22064 with this objfile, so we don't need to
22065 duplicate it for the type. */
22066 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
22067 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
22068 }
63d06c5c
DC
22069 }
22070 }
c906108c
SS
22071 break;
22072 case DW_TAG_typedef:
f1e6e072 22073 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 22074 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22075 list_to_add = cu->list_in_scope;
63d06c5c 22076 break;
c906108c 22077 case DW_TAG_base_type:
a02abb62 22078 case DW_TAG_subrange_type:
f1e6e072 22079 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22080 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22081 list_to_add = cu->list_in_scope;
c906108c
SS
22082 break;
22083 case DW_TAG_enumerator:
e142c38c 22084 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
22085 if (attr)
22086 {
e7c27a73 22087 dwarf2_const_value (attr, sym, cu);
c906108c 22088 }
63d06c5c
DC
22089 {
22090 /* NOTE: carlton/2003-11-10: See comment above in the
22091 DW_TAG_class_type, etc. block. */
22092
e142c38c 22093 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 22094 && cu->language == language_cplus
e142c38c 22095 ? &global_symbols : cu->list_in_scope);
63d06c5c 22096 }
c906108c 22097 break;
74921315 22098 case DW_TAG_imported_declaration:
5c4e30ca 22099 case DW_TAG_namespace:
f1e6e072 22100 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 22101 list_to_add = &global_symbols;
5c4e30ca 22102 break;
530e8392
KB
22103 case DW_TAG_module:
22104 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22105 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22106 list_to_add = &global_symbols;
22107 break;
4357ac6c 22108 case DW_TAG_common_block:
f1e6e072 22109 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
22110 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22111 add_symbol_to_list (sym, cu->list_in_scope);
22112 break;
c906108c
SS
22113 default:
22114 /* Not a tag we recognize. Hopefully we aren't processing
22115 trash data, but since we must specifically ignore things
22116 we don't recognize, there is nothing else we should do at
0963b4bd 22117 this point. */
e2e0b3e5 22118 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 22119 dwarf_tag_name (die->tag));
c906108c
SS
22120 break;
22121 }
df8a16a1 22122
e37fd15a
SW
22123 if (suppress_add)
22124 {
22125 sym->hash_next = objfile->template_symbols;
22126 objfile->template_symbols = sym;
22127 list_to_add = NULL;
22128 }
22129
22130 if (list_to_add != NULL)
22131 add_symbol_to_list (sym, list_to_add);
22132
df8a16a1
DJ
22133 /* For the benefit of old versions of GCC, check for anonymous
22134 namespaces based on the demangled name. */
4d4ec4e5 22135 if (!cu->processing_has_namespace_info
94af9270 22136 && cu->language == language_cplus)
a10964d1 22137 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
22138 }
22139 return (sym);
22140}
22141
98bfdba5
PA
22142/* Given an attr with a DW_FORM_dataN value in host byte order,
22143 zero-extend it as appropriate for the symbol's type. The DWARF
22144 standard (v4) is not entirely clear about the meaning of using
22145 DW_FORM_dataN for a constant with a signed type, where the type is
22146 wider than the data. The conclusion of a discussion on the DWARF
22147 list was that this is unspecified. We choose to always zero-extend
22148 because that is the interpretation long in use by GCC. */
c906108c 22149
98bfdba5 22150static gdb_byte *
ff39bb5e 22151dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 22152 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 22153{
518817b3 22154 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
22155 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22156 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
22157 LONGEST l = DW_UNSND (attr);
22158
22159 if (bits < sizeof (*value) * 8)
22160 {
22161 l &= ((LONGEST) 1 << bits) - 1;
22162 *value = l;
22163 }
22164 else if (bits == sizeof (*value) * 8)
22165 *value = l;
22166 else
22167 {
224c3ddb 22168 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
22169 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22170 return bytes;
22171 }
22172
22173 return NULL;
22174}
22175
22176/* Read a constant value from an attribute. Either set *VALUE, or if
22177 the value does not fit in *VALUE, set *BYTES - either already
22178 allocated on the objfile obstack, or newly allocated on OBSTACK,
22179 or, set *BATON, if we translated the constant to a location
22180 expression. */
22181
22182static void
ff39bb5e 22183dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
22184 const char *name, struct obstack *obstack,
22185 struct dwarf2_cu *cu,
d521ce57 22186 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
22187 struct dwarf2_locexpr_baton **baton)
22188{
518817b3 22189 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 22190 struct comp_unit_head *cu_header = &cu->header;
c906108c 22191 struct dwarf_block *blk;
98bfdba5
PA
22192 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22193 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22194
22195 *value = 0;
22196 *bytes = NULL;
22197 *baton = NULL;
c906108c
SS
22198
22199 switch (attr->form)
22200 {
22201 case DW_FORM_addr:
3019eac3 22202 case DW_FORM_GNU_addr_index:
ac56253d 22203 {
ac56253d
TT
22204 gdb_byte *data;
22205
98bfdba5
PA
22206 if (TYPE_LENGTH (type) != cu_header->addr_size)
22207 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22208 cu_header->addr_size,
98bfdba5 22209 TYPE_LENGTH (type));
ac56253d
TT
22210 /* Symbols of this form are reasonably rare, so we just
22211 piggyback on the existing location code rather than writing
22212 a new implementation of symbol_computed_ops. */
8d749320 22213 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
22214 (*baton)->per_cu = cu->per_cu;
22215 gdb_assert ((*baton)->per_cu);
ac56253d 22216
98bfdba5 22217 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22218 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22219 (*baton)->data = data;
ac56253d
TT
22220
22221 data[0] = DW_OP_addr;
22222 store_unsigned_integer (&data[1], cu_header->addr_size,
22223 byte_order, DW_ADDR (attr));
22224 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22225 }
c906108c 22226 break;
4ac36638 22227 case DW_FORM_string:
93b5768b 22228 case DW_FORM_strp:
3019eac3 22229 case DW_FORM_GNU_str_index:
36586728 22230 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
22231 /* DW_STRING is already allocated on the objfile obstack, point
22232 directly to it. */
d521ce57 22233 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 22234 break;
c906108c
SS
22235 case DW_FORM_block1:
22236 case DW_FORM_block2:
22237 case DW_FORM_block4:
22238 case DW_FORM_block:
2dc7f7b3 22239 case DW_FORM_exprloc:
0224619f 22240 case DW_FORM_data16:
c906108c 22241 blk = DW_BLOCK (attr);
98bfdba5
PA
22242 if (TYPE_LENGTH (type) != blk->size)
22243 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22244 TYPE_LENGTH (type));
22245 *bytes = blk->data;
c906108c 22246 break;
2df3850c
JM
22247
22248 /* The DW_AT_const_value attributes are supposed to carry the
22249 symbol's value "represented as it would be on the target
22250 architecture." By the time we get here, it's already been
22251 converted to host endianness, so we just need to sign- or
22252 zero-extend it as appropriate. */
22253 case DW_FORM_data1:
3aef2284 22254 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22255 break;
c906108c 22256 case DW_FORM_data2:
3aef2284 22257 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22258 break;
c906108c 22259 case DW_FORM_data4:
3aef2284 22260 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22261 break;
c906108c 22262 case DW_FORM_data8:
3aef2284 22263 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22264 break;
22265
c906108c 22266 case DW_FORM_sdata:
663c44ac 22267 case DW_FORM_implicit_const:
98bfdba5 22268 *value = DW_SND (attr);
2df3850c
JM
22269 break;
22270
c906108c 22271 case DW_FORM_udata:
98bfdba5 22272 *value = DW_UNSND (attr);
c906108c 22273 break;
2df3850c 22274
c906108c 22275 default:
4d3c2250 22276 complaint (&symfile_complaints,
e2e0b3e5 22277 _("unsupported const value attribute form: '%s'"),
4d3c2250 22278 dwarf_form_name (attr->form));
98bfdba5 22279 *value = 0;
c906108c
SS
22280 break;
22281 }
22282}
22283
2df3850c 22284
98bfdba5
PA
22285/* Copy constant value from an attribute to a symbol. */
22286
2df3850c 22287static void
ff39bb5e 22288dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22289 struct dwarf2_cu *cu)
2df3850c 22290{
518817b3 22291 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 22292 LONGEST value;
d521ce57 22293 const gdb_byte *bytes;
98bfdba5 22294 struct dwarf2_locexpr_baton *baton;
2df3850c 22295
98bfdba5
PA
22296 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22297 SYMBOL_PRINT_NAME (sym),
22298 &objfile->objfile_obstack, cu,
22299 &value, &bytes, &baton);
2df3850c 22300
98bfdba5
PA
22301 if (baton != NULL)
22302 {
98bfdba5 22303 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22304 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22305 }
22306 else if (bytes != NULL)
22307 {
22308 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22309 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22310 }
22311 else
22312 {
22313 SYMBOL_VALUE (sym) = value;
f1e6e072 22314 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22315 }
2df3850c
JM
22316}
22317
c906108c
SS
22318/* Return the type of the die in question using its DW_AT_type attribute. */
22319
22320static struct type *
e7c27a73 22321die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22322{
c906108c 22323 struct attribute *type_attr;
c906108c 22324
e142c38c 22325 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22326 if (!type_attr)
22327 {
518817b3 22328 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22329 /* A missing DW_AT_type represents a void type. */
518817b3 22330 return objfile_type (objfile)->builtin_void;
c906108c 22331 }
348e048f 22332
673bfd45 22333 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22334}
22335
b4ba55a1
JB
22336/* True iff CU's producer generates GNAT Ada auxiliary information
22337 that allows to find parallel types through that information instead
22338 of having to do expensive parallel lookups by type name. */
22339
22340static int
22341need_gnat_info (struct dwarf2_cu *cu)
22342{
de4cb04a
JB
22343 /* Assume that the Ada compiler was GNAT, which always produces
22344 the auxiliary information. */
22345 return (cu->language == language_ada);
b4ba55a1
JB
22346}
22347
b4ba55a1
JB
22348/* Return the auxiliary type of the die in question using its
22349 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22350 attribute is not present. */
22351
22352static struct type *
22353die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22354{
b4ba55a1 22355 struct attribute *type_attr;
b4ba55a1
JB
22356
22357 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22358 if (!type_attr)
22359 return NULL;
22360
673bfd45 22361 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22362}
22363
22364/* If DIE has a descriptive_type attribute, then set the TYPE's
22365 descriptive type accordingly. */
22366
22367static void
22368set_descriptive_type (struct type *type, struct die_info *die,
22369 struct dwarf2_cu *cu)
22370{
22371 struct type *descriptive_type = die_descriptive_type (die, cu);
22372
22373 if (descriptive_type)
22374 {
22375 ALLOCATE_GNAT_AUX_TYPE (type);
22376 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22377 }
22378}
22379
c906108c
SS
22380/* Return the containing type of the die in question using its
22381 DW_AT_containing_type attribute. */
22382
22383static struct type *
e7c27a73 22384die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22385{
c906108c 22386 struct attribute *type_attr;
518817b3 22387 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22388
e142c38c 22389 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22390 if (!type_attr)
22391 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22392 "[in module %s]"), objfile_name (objfile));
33ac96f0 22393
673bfd45 22394 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22395}
22396
ac9ec31b
DE
22397/* Return an error marker type to use for the ill formed type in DIE/CU. */
22398
22399static struct type *
22400build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22401{
518817b3
SM
22402 struct dwarf2_per_objfile *dwarf2_per_objfile
22403 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
22404 struct objfile *objfile = dwarf2_per_objfile->objfile;
22405 char *message, *saved;
22406
9d8780f0 22407 message = xstrprintf (_("<unknown type in %s, CU %s, DIE %s>"),
4262abfb 22408 objfile_name (objfile),
9d8780f0
SM
22409 sect_offset_str (cu->header.sect_off),
22410 sect_offset_str (die->sect_off));
224c3ddb
SM
22411 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
22412 message, strlen (message));
ac9ec31b
DE
22413 xfree (message);
22414
19f392bc 22415 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22416}
22417
673bfd45 22418/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22419 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22420 DW_AT_containing_type.
673bfd45
DE
22421 If there is no type substitute an error marker. */
22422
c906108c 22423static struct type *
ff39bb5e 22424lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22425 struct dwarf2_cu *cu)
c906108c 22426{
518817b3
SM
22427 struct dwarf2_per_objfile *dwarf2_per_objfile
22428 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22429 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
22430 struct type *this_type;
22431
ac9ec31b
DE
22432 gdb_assert (attr->name == DW_AT_type
22433 || attr->name == DW_AT_GNAT_descriptive_type
22434 || attr->name == DW_AT_containing_type);
22435
673bfd45
DE
22436 /* First see if we have it cached. */
22437
36586728
TT
22438 if (attr->form == DW_FORM_GNU_ref_alt)
22439 {
22440 struct dwarf2_per_cu_data *per_cu;
9c541725 22441 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 22442
ed2dc618
SM
22443 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22444 dwarf2_per_objfile);
9c541725 22445 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 22446 }
7771576e 22447 else if (attr_form_is_ref (attr))
673bfd45 22448 {
9c541725 22449 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 22450
9c541725 22451 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 22452 }
55f1336d 22453 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22454 {
ac9ec31b 22455 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 22456
ac9ec31b 22457 return get_signatured_type (die, signature, cu);
673bfd45
DE
22458 }
22459 else
22460 {
ac9ec31b
DE
22461 complaint (&symfile_complaints,
22462 _("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22463 " at %s [in module %s]"),
22464 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22465 objfile_name (objfile));
ac9ec31b 22466 return build_error_marker_type (cu, die);
673bfd45
DE
22467 }
22468
22469 /* If not cached we need to read it in. */
22470
22471 if (this_type == NULL)
22472 {
ac9ec31b 22473 struct die_info *type_die = NULL;
673bfd45
DE
22474 struct dwarf2_cu *type_cu = cu;
22475
7771576e 22476 if (attr_form_is_ref (attr))
ac9ec31b
DE
22477 type_die = follow_die_ref (die, attr, &type_cu);
22478 if (type_die == NULL)
22479 return build_error_marker_type (cu, die);
22480 /* If we find the type now, it's probably because the type came
3019eac3
DE
22481 from an inter-CU reference and the type's CU got expanded before
22482 ours. */
ac9ec31b 22483 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22484 }
22485
22486 /* If we still don't have a type use an error marker. */
22487
22488 if (this_type == NULL)
ac9ec31b 22489 return build_error_marker_type (cu, die);
673bfd45 22490
f792889a 22491 return this_type;
c906108c
SS
22492}
22493
673bfd45
DE
22494/* Return the type in DIE, CU.
22495 Returns NULL for invalid types.
22496
02142a6c 22497 This first does a lookup in die_type_hash,
673bfd45
DE
22498 and only reads the die in if necessary.
22499
22500 NOTE: This can be called when reading in partial or full symbols. */
22501
f792889a 22502static struct type *
e7c27a73 22503read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22504{
f792889a
DJ
22505 struct type *this_type;
22506
22507 this_type = get_die_type (die, cu);
22508 if (this_type)
22509 return this_type;
22510
673bfd45
DE
22511 return read_type_die_1 (die, cu);
22512}
22513
22514/* Read the type in DIE, CU.
22515 Returns NULL for invalid types. */
22516
22517static struct type *
22518read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22519{
22520 struct type *this_type = NULL;
22521
c906108c
SS
22522 switch (die->tag)
22523 {
22524 case DW_TAG_class_type:
680b30c7 22525 case DW_TAG_interface_type:
c906108c
SS
22526 case DW_TAG_structure_type:
22527 case DW_TAG_union_type:
f792889a 22528 this_type = read_structure_type (die, cu);
c906108c
SS
22529 break;
22530 case DW_TAG_enumeration_type:
f792889a 22531 this_type = read_enumeration_type (die, cu);
c906108c
SS
22532 break;
22533 case DW_TAG_subprogram:
22534 case DW_TAG_subroutine_type:
edb3359d 22535 case DW_TAG_inlined_subroutine:
f792889a 22536 this_type = read_subroutine_type (die, cu);
c906108c
SS
22537 break;
22538 case DW_TAG_array_type:
f792889a 22539 this_type = read_array_type (die, cu);
c906108c 22540 break;
72019c9c 22541 case DW_TAG_set_type:
f792889a 22542 this_type = read_set_type (die, cu);
72019c9c 22543 break;
c906108c 22544 case DW_TAG_pointer_type:
f792889a 22545 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22546 break;
22547 case DW_TAG_ptr_to_member_type:
f792889a 22548 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22549 break;
22550 case DW_TAG_reference_type:
4297a3f0
AV
22551 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22552 break;
22553 case DW_TAG_rvalue_reference_type:
22554 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22555 break;
22556 case DW_TAG_const_type:
f792889a 22557 this_type = read_tag_const_type (die, cu);
c906108c
SS
22558 break;
22559 case DW_TAG_volatile_type:
f792889a 22560 this_type = read_tag_volatile_type (die, cu);
c906108c 22561 break;
06d66ee9
TT
22562 case DW_TAG_restrict_type:
22563 this_type = read_tag_restrict_type (die, cu);
22564 break;
c906108c 22565 case DW_TAG_string_type:
f792889a 22566 this_type = read_tag_string_type (die, cu);
c906108c
SS
22567 break;
22568 case DW_TAG_typedef:
f792889a 22569 this_type = read_typedef (die, cu);
c906108c 22570 break;
a02abb62 22571 case DW_TAG_subrange_type:
f792889a 22572 this_type = read_subrange_type (die, cu);
a02abb62 22573 break;
c906108c 22574 case DW_TAG_base_type:
f792889a 22575 this_type = read_base_type (die, cu);
c906108c 22576 break;
81a17f79 22577 case DW_TAG_unspecified_type:
f792889a 22578 this_type = read_unspecified_type (die, cu);
81a17f79 22579 break;
0114d602
DJ
22580 case DW_TAG_namespace:
22581 this_type = read_namespace_type (die, cu);
22582 break;
f55ee35c
JK
22583 case DW_TAG_module:
22584 this_type = read_module_type (die, cu);
22585 break;
a2c2acaf
MW
22586 case DW_TAG_atomic_type:
22587 this_type = read_tag_atomic_type (die, cu);
22588 break;
c906108c 22589 default:
3e43a32a
MS
22590 complaint (&symfile_complaints,
22591 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 22592 dwarf_tag_name (die->tag));
c906108c
SS
22593 break;
22594 }
63d06c5c 22595
f792889a 22596 return this_type;
63d06c5c
DC
22597}
22598
abc72ce4
DE
22599/* See if we can figure out if the class lives in a namespace. We do
22600 this by looking for a member function; its demangled name will
22601 contain namespace info, if there is any.
22602 Return the computed name or NULL.
22603 Space for the result is allocated on the objfile's obstack.
22604 This is the full-die version of guess_partial_die_structure_name.
22605 In this case we know DIE has no useful parent. */
22606
22607static char *
22608guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22609{
22610 struct die_info *spec_die;
22611 struct dwarf2_cu *spec_cu;
22612 struct die_info *child;
518817b3 22613 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
22614
22615 spec_cu = cu;
22616 spec_die = die_specification (die, &spec_cu);
22617 if (spec_die != NULL)
22618 {
22619 die = spec_die;
22620 cu = spec_cu;
22621 }
22622
22623 for (child = die->child;
22624 child != NULL;
22625 child = child->sibling)
22626 {
22627 if (child->tag == DW_TAG_subprogram)
22628 {
73b9be8b 22629 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22630
7d45c7c3 22631 if (linkage_name != NULL)
abc72ce4
DE
22632 {
22633 char *actual_name
22634 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 22635 linkage_name);
abc72ce4
DE
22636 char *name = NULL;
22637
22638 if (actual_name != NULL)
22639 {
15d034d0 22640 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22641
22642 if (die_name != NULL
22643 && strcmp (die_name, actual_name) != 0)
22644 {
22645 /* Strip off the class name from the full name.
22646 We want the prefix. */
22647 int die_name_len = strlen (die_name);
22648 int actual_name_len = strlen (actual_name);
22649
22650 /* Test for '::' as a sanity check. */
22651 if (actual_name_len > die_name_len + 2
3e43a32a
MS
22652 && actual_name[actual_name_len
22653 - die_name_len - 1] == ':')
224c3ddb 22654 name = (char *) obstack_copy0 (
e3b94546 22655 &objfile->per_bfd->storage_obstack,
224c3ddb 22656 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
22657 }
22658 }
22659 xfree (actual_name);
22660 return name;
22661 }
22662 }
22663 }
22664
22665 return NULL;
22666}
22667
96408a79
SA
22668/* GCC might emit a nameless typedef that has a linkage name. Determine the
22669 prefix part in such case. See
22670 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22671
a121b7c1 22672static const char *
96408a79
SA
22673anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22674{
22675 struct attribute *attr;
e6a959d6 22676 const char *base;
96408a79
SA
22677
22678 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22679 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22680 return NULL;
22681
7d45c7c3 22682 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22683 return NULL;
22684
73b9be8b 22685 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22686 if (attr == NULL || DW_STRING (attr) == NULL)
22687 return NULL;
22688
22689 /* dwarf2_name had to be already called. */
22690 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22691
22692 /* Strip the base name, keep any leading namespaces/classes. */
22693 base = strrchr (DW_STRING (attr), ':');
22694 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22695 return "";
22696
518817b3 22697 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e3b94546 22698 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb
SM
22699 DW_STRING (attr),
22700 &base[-1] - DW_STRING (attr));
96408a79
SA
22701}
22702
fdde2d81 22703/* Return the name of the namespace/class that DIE is defined within,
0114d602 22704 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22705
0114d602
DJ
22706 For example, if we're within the method foo() in the following
22707 code:
22708
22709 namespace N {
22710 class C {
22711 void foo () {
22712 }
22713 };
22714 }
22715
22716 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22717
0d5cff50 22718static const char *
e142c38c 22719determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22720{
518817b3
SM
22721 struct dwarf2_per_objfile *dwarf2_per_objfile
22722 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22723 struct die_info *parent, *spec_die;
22724 struct dwarf2_cu *spec_cu;
22725 struct type *parent_type;
a121b7c1 22726 const char *retval;
63d06c5c 22727
9c37b5ae 22728 if (cu->language != language_cplus
c44af4eb
TT
22729 && cu->language != language_fortran && cu->language != language_d
22730 && cu->language != language_rust)
0114d602
DJ
22731 return "";
22732
96408a79
SA
22733 retval = anonymous_struct_prefix (die, cu);
22734 if (retval)
22735 return retval;
22736
0114d602
DJ
22737 /* We have to be careful in the presence of DW_AT_specification.
22738 For example, with GCC 3.4, given the code
22739
22740 namespace N {
22741 void foo() {
22742 // Definition of N::foo.
22743 }
22744 }
22745
22746 then we'll have a tree of DIEs like this:
22747
22748 1: DW_TAG_compile_unit
22749 2: DW_TAG_namespace // N
22750 3: DW_TAG_subprogram // declaration of N::foo
22751 4: DW_TAG_subprogram // definition of N::foo
22752 DW_AT_specification // refers to die #3
22753
22754 Thus, when processing die #4, we have to pretend that we're in
22755 the context of its DW_AT_specification, namely the contex of die
22756 #3. */
22757 spec_cu = cu;
22758 spec_die = die_specification (die, &spec_cu);
22759 if (spec_die == NULL)
22760 parent = die->parent;
22761 else
63d06c5c 22762 {
0114d602
DJ
22763 parent = spec_die->parent;
22764 cu = spec_cu;
63d06c5c 22765 }
0114d602
DJ
22766
22767 if (parent == NULL)
22768 return "";
98bfdba5
PA
22769 else if (parent->building_fullname)
22770 {
22771 const char *name;
22772 const char *parent_name;
22773
22774 /* It has been seen on RealView 2.2 built binaries,
22775 DW_TAG_template_type_param types actually _defined_ as
22776 children of the parent class:
22777
22778 enum E {};
22779 template class <class Enum> Class{};
22780 Class<enum E> class_e;
22781
22782 1: DW_TAG_class_type (Class)
22783 2: DW_TAG_enumeration_type (E)
22784 3: DW_TAG_enumerator (enum1:0)
22785 3: DW_TAG_enumerator (enum2:1)
22786 ...
22787 2: DW_TAG_template_type_param
22788 DW_AT_type DW_FORM_ref_udata (E)
22789
22790 Besides being broken debug info, it can put GDB into an
22791 infinite loop. Consider:
22792
22793 When we're building the full name for Class<E>, we'll start
22794 at Class, and go look over its template type parameters,
22795 finding E. We'll then try to build the full name of E, and
22796 reach here. We're now trying to build the full name of E,
22797 and look over the parent DIE for containing scope. In the
22798 broken case, if we followed the parent DIE of E, we'd again
22799 find Class, and once again go look at its template type
22800 arguments, etc., etc. Simply don't consider such parent die
22801 as source-level parent of this die (it can't be, the language
22802 doesn't allow it), and break the loop here. */
22803 name = dwarf2_name (die, cu);
22804 parent_name = dwarf2_name (parent, cu);
22805 complaint (&symfile_complaints,
22806 _("template param type '%s' defined within parent '%s'"),
22807 name ? name : "<unknown>",
22808 parent_name ? parent_name : "<unknown>");
22809 return "";
22810 }
63d06c5c 22811 else
0114d602
DJ
22812 switch (parent->tag)
22813 {
63d06c5c 22814 case DW_TAG_namespace:
0114d602 22815 parent_type = read_type_die (parent, cu);
acebe513
UW
22816 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22817 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22818 Work around this problem here. */
22819 if (cu->language == language_cplus
22820 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22821 return "";
0114d602
DJ
22822 /* We give a name to even anonymous namespaces. */
22823 return TYPE_TAG_NAME (parent_type);
63d06c5c 22824 case DW_TAG_class_type:
680b30c7 22825 case DW_TAG_interface_type:
63d06c5c 22826 case DW_TAG_structure_type:
0114d602 22827 case DW_TAG_union_type:
f55ee35c 22828 case DW_TAG_module:
0114d602
DJ
22829 parent_type = read_type_die (parent, cu);
22830 if (TYPE_TAG_NAME (parent_type) != NULL)
22831 return TYPE_TAG_NAME (parent_type);
22832 else
22833 /* An anonymous structure is only allowed non-static data
22834 members; no typedefs, no member functions, et cetera.
22835 So it does not need a prefix. */
22836 return "";
abc72ce4 22837 case DW_TAG_compile_unit:
95554aad 22838 case DW_TAG_partial_unit:
abc72ce4
DE
22839 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22840 if (cu->language == language_cplus
8b70b953 22841 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
22842 && die->child != NULL
22843 && (die->tag == DW_TAG_class_type
22844 || die->tag == DW_TAG_structure_type
22845 || die->tag == DW_TAG_union_type))
22846 {
22847 char *name = guess_full_die_structure_name (die, cu);
22848 if (name != NULL)
22849 return name;
22850 }
22851 return "";
3d567982
TT
22852 case DW_TAG_enumeration_type:
22853 parent_type = read_type_die (parent, cu);
22854 if (TYPE_DECLARED_CLASS (parent_type))
22855 {
22856 if (TYPE_TAG_NAME (parent_type) != NULL)
22857 return TYPE_TAG_NAME (parent_type);
22858 return "";
22859 }
22860 /* Fall through. */
63d06c5c 22861 default:
8176b9b8 22862 return determine_prefix (parent, cu);
63d06c5c 22863 }
63d06c5c
DC
22864}
22865
3e43a32a
MS
22866/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22867 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22868 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22869 an obconcat, otherwise allocate storage for the result. The CU argument is
22870 used to determine the language and hence, the appropriate separator. */
987504bb 22871
f55ee35c 22872#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22873
22874static char *
f55ee35c
JK
22875typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22876 int physname, struct dwarf2_cu *cu)
63d06c5c 22877{
f55ee35c 22878 const char *lead = "";
5c315b68 22879 const char *sep;
63d06c5c 22880
3e43a32a
MS
22881 if (suffix == NULL || suffix[0] == '\0'
22882 || prefix == NULL || prefix[0] == '\0')
987504bb 22883 sep = "";
45280282
IB
22884 else if (cu->language == language_d)
22885 {
22886 /* For D, the 'main' function could be defined in any module, but it
22887 should never be prefixed. */
22888 if (strcmp (suffix, "D main") == 0)
22889 {
22890 prefix = "";
22891 sep = "";
22892 }
22893 else
22894 sep = ".";
22895 }
f55ee35c
JK
22896 else if (cu->language == language_fortran && physname)
22897 {
22898 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22899 DW_AT_MIPS_linkage_name is preferred and used instead. */
22900
22901 lead = "__";
22902 sep = "_MOD_";
22903 }
987504bb
JJ
22904 else
22905 sep = "::";
63d06c5c 22906
6dd47d34
DE
22907 if (prefix == NULL)
22908 prefix = "";
22909 if (suffix == NULL)
22910 suffix = "";
22911
987504bb
JJ
22912 if (obs == NULL)
22913 {
3e43a32a 22914 char *retval
224c3ddb
SM
22915 = ((char *)
22916 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22917
f55ee35c
JK
22918 strcpy (retval, lead);
22919 strcat (retval, prefix);
6dd47d34
DE
22920 strcat (retval, sep);
22921 strcat (retval, suffix);
63d06c5c
DC
22922 return retval;
22923 }
987504bb
JJ
22924 else
22925 {
22926 /* We have an obstack. */
f55ee35c 22927 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22928 }
63d06c5c
DC
22929}
22930
c906108c
SS
22931/* Return sibling of die, NULL if no sibling. */
22932
f9aca02d 22933static struct die_info *
fba45db2 22934sibling_die (struct die_info *die)
c906108c 22935{
639d11d3 22936 return die->sibling;
c906108c
SS
22937}
22938
71c25dea
TT
22939/* Get name of a die, return NULL if not found. */
22940
15d034d0
TT
22941static const char *
22942dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
22943 struct obstack *obstack)
22944{
22945 if (name && cu->language == language_cplus)
22946 {
2f408ecb 22947 std::string canon_name = cp_canonicalize_string (name);
71c25dea 22948
2f408ecb 22949 if (!canon_name.empty ())
71c25dea 22950 {
2f408ecb
PA
22951 if (canon_name != name)
22952 name = (const char *) obstack_copy0 (obstack,
22953 canon_name.c_str (),
22954 canon_name.length ());
71c25dea
TT
22955 }
22956 }
22957
22958 return name;
c906108c
SS
22959}
22960
96553a0c
DE
22961/* Get name of a die, return NULL if not found.
22962 Anonymous namespaces are converted to their magic string. */
9219021c 22963
15d034d0 22964static const char *
e142c38c 22965dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22966{
22967 struct attribute *attr;
518817b3 22968 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 22969
e142c38c 22970 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22971 if ((!attr || !DW_STRING (attr))
96553a0c 22972 && die->tag != DW_TAG_namespace
53832f31
TT
22973 && die->tag != DW_TAG_class_type
22974 && die->tag != DW_TAG_interface_type
22975 && die->tag != DW_TAG_structure_type
22976 && die->tag != DW_TAG_union_type)
71c25dea
TT
22977 return NULL;
22978
22979 switch (die->tag)
22980 {
22981 case DW_TAG_compile_unit:
95554aad 22982 case DW_TAG_partial_unit:
71c25dea
TT
22983 /* Compilation units have a DW_AT_name that is a filename, not
22984 a source language identifier. */
22985 case DW_TAG_enumeration_type:
22986 case DW_TAG_enumerator:
22987 /* These tags always have simple identifiers already; no need
22988 to canonicalize them. */
22989 return DW_STRING (attr);
907af001 22990
96553a0c
DE
22991 case DW_TAG_namespace:
22992 if (attr != NULL && DW_STRING (attr) != NULL)
22993 return DW_STRING (attr);
22994 return CP_ANONYMOUS_NAMESPACE_STR;
22995
907af001
UW
22996 case DW_TAG_class_type:
22997 case DW_TAG_interface_type:
22998 case DW_TAG_structure_type:
22999 case DW_TAG_union_type:
23000 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23001 structures or unions. These were of the form "._%d" in GCC 4.1,
23002 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23003 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 23004 if (attr && DW_STRING (attr)
61012eef
GB
23005 && (startswith (DW_STRING (attr), "._")
23006 || startswith (DW_STRING (attr), "<anonymous")))
907af001 23007 return NULL;
53832f31
TT
23008
23009 /* GCC might emit a nameless typedef that has a linkage name. See
23010 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23011 if (!attr || DW_STRING (attr) == NULL)
23012 {
df5c6c50 23013 char *demangled = NULL;
53832f31 23014
73b9be8b 23015 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
23016 if (attr == NULL || DW_STRING (attr) == NULL)
23017 return NULL;
23018
df5c6c50
JK
23019 /* Avoid demangling DW_STRING (attr) the second time on a second
23020 call for the same DIE. */
23021 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 23022 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
23023
23024 if (demangled)
23025 {
e6a959d6 23026 const char *base;
96408a79 23027
53832f31 23028 /* FIXME: we already did this for the partial symbol... */
34a68019 23029 DW_STRING (attr)
224c3ddb 23030 = ((const char *)
e3b94546 23031 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb 23032 demangled, strlen (demangled)));
53832f31
TT
23033 DW_STRING_IS_CANONICAL (attr) = 1;
23034 xfree (demangled);
96408a79
SA
23035
23036 /* Strip any leading namespaces/classes, keep only the base name.
23037 DW_AT_name for named DIEs does not contain the prefixes. */
23038 base = strrchr (DW_STRING (attr), ':');
23039 if (base && base > DW_STRING (attr) && base[-1] == ':')
23040 return &base[1];
23041 else
23042 return DW_STRING (attr);
53832f31
TT
23043 }
23044 }
907af001
UW
23045 break;
23046
71c25dea 23047 default:
907af001
UW
23048 break;
23049 }
23050
23051 if (!DW_STRING_IS_CANONICAL (attr))
23052 {
23053 DW_STRING (attr)
23054 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 23055 &objfile->per_bfd->storage_obstack);
907af001 23056 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 23057 }
907af001 23058 return DW_STRING (attr);
9219021c
DC
23059}
23060
23061/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
23062 is none. *EXT_CU is the CU containing DIE on input, and the CU
23063 containing the return value on output. */
9219021c
DC
23064
23065static struct die_info *
f2f0e013 23066dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
23067{
23068 struct attribute *attr;
9219021c 23069
f2f0e013 23070 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
23071 if (attr == NULL)
23072 return NULL;
23073
f2f0e013 23074 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
23075}
23076
c906108c
SS
23077/* Convert a DIE tag into its string name. */
23078
f39c6ffd 23079static const char *
aa1ee363 23080dwarf_tag_name (unsigned tag)
c906108c 23081{
f39c6ffd
TT
23082 const char *name = get_DW_TAG_name (tag);
23083
23084 if (name == NULL)
23085 return "DW_TAG_<unknown>";
23086
23087 return name;
c906108c
SS
23088}
23089
23090/* Convert a DWARF attribute code into its string name. */
23091
f39c6ffd 23092static const char *
aa1ee363 23093dwarf_attr_name (unsigned attr)
c906108c 23094{
f39c6ffd
TT
23095 const char *name;
23096
c764a876 23097#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
23098 if (attr == DW_AT_MIPS_fde)
23099 return "DW_AT_MIPS_fde";
23100#else
23101 if (attr == DW_AT_HP_block_index)
23102 return "DW_AT_HP_block_index";
c764a876 23103#endif
f39c6ffd
TT
23104
23105 name = get_DW_AT_name (attr);
23106
23107 if (name == NULL)
23108 return "DW_AT_<unknown>";
23109
23110 return name;
c906108c
SS
23111}
23112
23113/* Convert a DWARF value form code into its string name. */
23114
f39c6ffd 23115static const char *
aa1ee363 23116dwarf_form_name (unsigned form)
c906108c 23117{
f39c6ffd
TT
23118 const char *name = get_DW_FORM_name (form);
23119
23120 if (name == NULL)
23121 return "DW_FORM_<unknown>";
23122
23123 return name;
c906108c
SS
23124}
23125
a121b7c1 23126static const char *
fba45db2 23127dwarf_bool_name (unsigned mybool)
c906108c
SS
23128{
23129 if (mybool)
23130 return "TRUE";
23131 else
23132 return "FALSE";
23133}
23134
23135/* Convert a DWARF type code into its string name. */
23136
f39c6ffd 23137static const char *
aa1ee363 23138dwarf_type_encoding_name (unsigned enc)
c906108c 23139{
f39c6ffd 23140 const char *name = get_DW_ATE_name (enc);
c906108c 23141
f39c6ffd
TT
23142 if (name == NULL)
23143 return "DW_ATE_<unknown>";
c906108c 23144
f39c6ffd 23145 return name;
c906108c 23146}
c906108c 23147
f9aca02d 23148static void
d97bc12b 23149dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
23150{
23151 unsigned int i;
23152
d97bc12b 23153 print_spaces (indent, f);
9d8780f0 23154 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 23155 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 23156 sect_offset_str (die->sect_off));
d97bc12b
DE
23157
23158 if (die->parent != NULL)
23159 {
23160 print_spaces (indent, f);
9d8780f0
SM
23161 fprintf_unfiltered (f, " parent at offset: %s\n",
23162 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
23163 }
23164
23165 print_spaces (indent, f);
23166 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 23167 dwarf_bool_name (die->child != NULL));
c906108c 23168
d97bc12b
DE
23169 print_spaces (indent, f);
23170 fprintf_unfiltered (f, " attributes:\n");
23171
c906108c
SS
23172 for (i = 0; i < die->num_attrs; ++i)
23173 {
d97bc12b
DE
23174 print_spaces (indent, f);
23175 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
23176 dwarf_attr_name (die->attrs[i].name),
23177 dwarf_form_name (die->attrs[i].form));
d97bc12b 23178
c906108c
SS
23179 switch (die->attrs[i].form)
23180 {
c906108c 23181 case DW_FORM_addr:
3019eac3 23182 case DW_FORM_GNU_addr_index:
d97bc12b 23183 fprintf_unfiltered (f, "address: ");
5af949e3 23184 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
23185 break;
23186 case DW_FORM_block2:
23187 case DW_FORM_block4:
23188 case DW_FORM_block:
23189 case DW_FORM_block1:
56eb65bd
SP
23190 fprintf_unfiltered (f, "block: size %s",
23191 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 23192 break;
2dc7f7b3 23193 case DW_FORM_exprloc:
56eb65bd
SP
23194 fprintf_unfiltered (f, "expression: size %s",
23195 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 23196 break;
0224619f
JK
23197 case DW_FORM_data16:
23198 fprintf_unfiltered (f, "constant of 16 bytes");
23199 break;
4568ecf9
DE
23200 case DW_FORM_ref_addr:
23201 fprintf_unfiltered (f, "ref address: ");
23202 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23203 break;
36586728
TT
23204 case DW_FORM_GNU_ref_alt:
23205 fprintf_unfiltered (f, "alt ref address: ");
23206 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23207 break;
10b3939b
DJ
23208 case DW_FORM_ref1:
23209 case DW_FORM_ref2:
23210 case DW_FORM_ref4:
4568ecf9
DE
23211 case DW_FORM_ref8:
23212 case DW_FORM_ref_udata:
d97bc12b 23213 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 23214 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 23215 break;
c906108c
SS
23216 case DW_FORM_data1:
23217 case DW_FORM_data2:
23218 case DW_FORM_data4:
ce5d95e1 23219 case DW_FORM_data8:
c906108c
SS
23220 case DW_FORM_udata:
23221 case DW_FORM_sdata:
43bbcdc2
PH
23222 fprintf_unfiltered (f, "constant: %s",
23223 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 23224 break;
2dc7f7b3
TT
23225 case DW_FORM_sec_offset:
23226 fprintf_unfiltered (f, "section offset: %s",
23227 pulongest (DW_UNSND (&die->attrs[i])));
23228 break;
55f1336d 23229 case DW_FORM_ref_sig8:
ac9ec31b
DE
23230 fprintf_unfiltered (f, "signature: %s",
23231 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 23232 break;
c906108c 23233 case DW_FORM_string:
4bdf3d34 23234 case DW_FORM_strp:
43988095 23235 case DW_FORM_line_strp:
3019eac3 23236 case DW_FORM_GNU_str_index:
36586728 23237 case DW_FORM_GNU_strp_alt:
8285870a 23238 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 23239 DW_STRING (&die->attrs[i])
8285870a
JK
23240 ? DW_STRING (&die->attrs[i]) : "",
23241 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
23242 break;
23243 case DW_FORM_flag:
23244 if (DW_UNSND (&die->attrs[i]))
d97bc12b 23245 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23246 else
d97bc12b 23247 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23248 break;
2dc7f7b3
TT
23249 case DW_FORM_flag_present:
23250 fprintf_unfiltered (f, "flag: TRUE");
23251 break;
a8329558 23252 case DW_FORM_indirect:
0963b4bd
MS
23253 /* The reader will have reduced the indirect form to
23254 the "base form" so this form should not occur. */
3e43a32a
MS
23255 fprintf_unfiltered (f,
23256 "unexpected attribute form: DW_FORM_indirect");
a8329558 23257 break;
663c44ac
JK
23258 case DW_FORM_implicit_const:
23259 fprintf_unfiltered (f, "constant: %s",
23260 plongest (DW_SND (&die->attrs[i])));
23261 break;
c906108c 23262 default:
d97bc12b 23263 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23264 die->attrs[i].form);
d97bc12b 23265 break;
c906108c 23266 }
d97bc12b 23267 fprintf_unfiltered (f, "\n");
c906108c
SS
23268 }
23269}
23270
f9aca02d 23271static void
d97bc12b 23272dump_die_for_error (struct die_info *die)
c906108c 23273{
d97bc12b
DE
23274 dump_die_shallow (gdb_stderr, 0, die);
23275}
23276
23277static void
23278dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23279{
23280 int indent = level * 4;
23281
23282 gdb_assert (die != NULL);
23283
23284 if (level >= max_level)
23285 return;
23286
23287 dump_die_shallow (f, indent, die);
23288
23289 if (die->child != NULL)
c906108c 23290 {
d97bc12b
DE
23291 print_spaces (indent, f);
23292 fprintf_unfiltered (f, " Children:");
23293 if (level + 1 < max_level)
23294 {
23295 fprintf_unfiltered (f, "\n");
23296 dump_die_1 (f, level + 1, max_level, die->child);
23297 }
23298 else
23299 {
3e43a32a
MS
23300 fprintf_unfiltered (f,
23301 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23302 }
23303 }
23304
23305 if (die->sibling != NULL && level > 0)
23306 {
23307 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23308 }
23309}
23310
d97bc12b
DE
23311/* This is called from the pdie macro in gdbinit.in.
23312 It's not static so gcc will keep a copy callable from gdb. */
23313
23314void
23315dump_die (struct die_info *die, int max_level)
23316{
23317 dump_die_1 (gdb_stdlog, 0, max_level, die);
23318}
23319
f9aca02d 23320static void
51545339 23321store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23322{
51545339 23323 void **slot;
c906108c 23324
9c541725
PA
23325 slot = htab_find_slot_with_hash (cu->die_hash, die,
23326 to_underlying (die->sect_off),
b64f50a1 23327 INSERT);
51545339
DJ
23328
23329 *slot = die;
c906108c
SS
23330}
23331
b64f50a1
JK
23332/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23333 required kind. */
23334
23335static sect_offset
ff39bb5e 23336dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 23337{
7771576e 23338 if (attr_form_is_ref (attr))
9c541725 23339 return (sect_offset) DW_UNSND (attr);
93311388
DE
23340
23341 complaint (&symfile_complaints,
23342 _("unsupported die ref attribute form: '%s'"),
23343 dwarf_form_name (attr->form));
9c541725 23344 return {};
c906108c
SS
23345}
23346
43bbcdc2
PH
23347/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23348 * the value held by the attribute is not constant. */
a02abb62 23349
43bbcdc2 23350static LONGEST
ff39bb5e 23351dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 23352{
663c44ac 23353 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
23354 return DW_SND (attr);
23355 else if (attr->form == DW_FORM_udata
23356 || attr->form == DW_FORM_data1
23357 || attr->form == DW_FORM_data2
23358 || attr->form == DW_FORM_data4
23359 || attr->form == DW_FORM_data8)
23360 return DW_UNSND (attr);
23361 else
23362 {
0224619f 23363 /* For DW_FORM_data16 see attr_form_is_constant. */
3e43a32a
MS
23364 complaint (&symfile_complaints,
23365 _("Attribute value is not a constant (%s)"),
a02abb62
JB
23366 dwarf_form_name (attr->form));
23367 return default_value;
23368 }
23369}
23370
348e048f
DE
23371/* Follow reference or signature attribute ATTR of SRC_DIE.
23372 On entry *REF_CU is the CU of SRC_DIE.
23373 On exit *REF_CU is the CU of the result. */
23374
23375static struct die_info *
ff39bb5e 23376follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23377 struct dwarf2_cu **ref_cu)
23378{
23379 struct die_info *die;
23380
7771576e 23381 if (attr_form_is_ref (attr))
348e048f 23382 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23383 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23384 die = follow_die_sig (src_die, attr, ref_cu);
23385 else
23386 {
23387 dump_die_for_error (src_die);
23388 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 23389 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
23390 }
23391
23392 return die;
03dd20cc
DJ
23393}
23394
5c631832 23395/* Follow reference OFFSET.
673bfd45
DE
23396 On entry *REF_CU is the CU of the source die referencing OFFSET.
23397 On exit *REF_CU is the CU of the result.
23398 Returns NULL if OFFSET is invalid. */
f504f079 23399
f9aca02d 23400static struct die_info *
9c541725 23401follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23402 struct dwarf2_cu **ref_cu)
c906108c 23403{
10b3939b 23404 struct die_info temp_die;
f2f0e013 23405 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
23406 struct dwarf2_per_objfile *dwarf2_per_objfile
23407 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23408 struct objfile *objfile = dwarf2_per_objfile->objfile;
10b3939b 23409
348e048f
DE
23410 gdb_assert (cu->per_cu != NULL);
23411
98bfdba5
PA
23412 target_cu = cu;
23413
3019eac3 23414 if (cu->per_cu->is_debug_types)
348e048f
DE
23415 {
23416 /* .debug_types CUs cannot reference anything outside their CU.
23417 If they need to, they have to reference a signatured type via
55f1336d 23418 DW_FORM_ref_sig8. */
9c541725 23419 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 23420 return NULL;
348e048f 23421 }
36586728 23422 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 23423 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
23424 {
23425 struct dwarf2_per_cu_data *per_cu;
9a619af0 23426
9c541725 23427 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 23428 dwarf2_per_objfile);
03dd20cc
DJ
23429
23430 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
23431 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23432 load_full_comp_unit (per_cu, cu->language);
03dd20cc 23433
10b3939b
DJ
23434 target_cu = per_cu->cu;
23435 }
98bfdba5
PA
23436 else if (cu->dies == NULL)
23437 {
23438 /* We're loading full DIEs during partial symbol reading. */
23439 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 23440 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 23441 }
c906108c 23442
f2f0e013 23443 *ref_cu = target_cu;
9c541725 23444 temp_die.sect_off = sect_off;
9a3c8263 23445 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23446 &temp_die,
23447 to_underlying (sect_off));
5c631832 23448}
10b3939b 23449
5c631832
JK
23450/* Follow reference attribute ATTR of SRC_DIE.
23451 On entry *REF_CU is the CU of SRC_DIE.
23452 On exit *REF_CU is the CU of the result. */
23453
23454static struct die_info *
ff39bb5e 23455follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23456 struct dwarf2_cu **ref_cu)
23457{
9c541725 23458 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
23459 struct dwarf2_cu *cu = *ref_cu;
23460 struct die_info *die;
23461
9c541725 23462 die = follow_die_offset (sect_off,
36586728
TT
23463 (attr->form == DW_FORM_GNU_ref_alt
23464 || cu->per_cu->is_dwz),
23465 ref_cu);
5c631832 23466 if (!die)
9d8780f0
SM
23467 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23468 "at %s [in module %s]"),
23469 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 23470 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 23471
5c631832
JK
23472 return die;
23473}
23474
9c541725 23475/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 23476 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
23477 dwarf2_locexpr_baton->data has lifetime of
23478 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
23479
23480struct dwarf2_locexpr_baton
9c541725 23481dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
23482 struct dwarf2_per_cu_data *per_cu,
23483 CORE_ADDR (*get_frame_pc) (void *baton),
23484 void *baton)
5c631832 23485{
918dd910 23486 struct dwarf2_cu *cu;
5c631832
JK
23487 struct die_info *die;
23488 struct attribute *attr;
23489 struct dwarf2_locexpr_baton retval;
e3b94546 23490 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ed2dc618
SM
23491 struct dwarf2_per_objfile *dwarf2_per_objfile
23492 = get_dwarf2_per_objfile (objfile);
8cf6f0b1 23493
918dd910
JK
23494 if (per_cu->cu == NULL)
23495 load_cu (per_cu);
23496 cu = per_cu->cu;
cc12ce38
DE
23497 if (cu == NULL)
23498 {
23499 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23500 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23501 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23502 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23503 }
918dd910 23504
9c541725 23505 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23506 if (!die)
9d8780f0
SM
23507 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23508 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23509
23510 attr = dwarf2_attr (die, DW_AT_location, cu);
23511 if (!attr)
23512 {
e103e986
JK
23513 /* DWARF: "If there is no such attribute, then there is no effect.".
23514 DATA is ignored if SIZE is 0. */
5c631832 23515
e103e986 23516 retval.data = NULL;
5c631832
JK
23517 retval.size = 0;
23518 }
8cf6f0b1
TT
23519 else if (attr_form_is_section_offset (attr))
23520 {
23521 struct dwarf2_loclist_baton loclist_baton;
23522 CORE_ADDR pc = (*get_frame_pc) (baton);
23523 size_t size;
23524
23525 fill_in_loclist_baton (cu, &loclist_baton, attr);
23526
23527 retval.data = dwarf2_find_location_expression (&loclist_baton,
23528 &size, pc);
23529 retval.size = size;
23530 }
5c631832
JK
23531 else
23532 {
23533 if (!attr_form_is_block (attr))
9d8780f0 23534 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23535 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23536 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23537
23538 retval.data = DW_BLOCK (attr)->data;
23539 retval.size = DW_BLOCK (attr)->size;
23540 }
23541 retval.per_cu = cu->per_cu;
918dd910 23542
ed2dc618 23543 age_cached_comp_units (dwarf2_per_objfile);
918dd910 23544
5c631832 23545 return retval;
348e048f
DE
23546}
23547
8b9737bf
TT
23548/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23549 offset. */
23550
23551struct dwarf2_locexpr_baton
23552dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23553 struct dwarf2_per_cu_data *per_cu,
23554 CORE_ADDR (*get_frame_pc) (void *baton),
23555 void *baton)
23556{
9c541725 23557 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23558
9c541725 23559 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
23560}
23561
b6807d98
TT
23562/* Write a constant of a given type as target-ordered bytes into
23563 OBSTACK. */
23564
23565static const gdb_byte *
23566write_constant_as_bytes (struct obstack *obstack,
23567 enum bfd_endian byte_order,
23568 struct type *type,
23569 ULONGEST value,
23570 LONGEST *len)
23571{
23572 gdb_byte *result;
23573
23574 *len = TYPE_LENGTH (type);
224c3ddb 23575 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23576 store_unsigned_integer (result, *len, byte_order, value);
23577
23578 return result;
23579}
23580
23581/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23582 pointer to the constant bytes and set LEN to the length of the
23583 data. If memory is needed, allocate it on OBSTACK. If the DIE
23584 does not have a DW_AT_const_value, return NULL. */
23585
23586const gdb_byte *
9c541725 23587dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
23588 struct dwarf2_per_cu_data *per_cu,
23589 struct obstack *obstack,
23590 LONGEST *len)
23591{
23592 struct dwarf2_cu *cu;
23593 struct die_info *die;
23594 struct attribute *attr;
23595 const gdb_byte *result = NULL;
23596 struct type *type;
23597 LONGEST value;
23598 enum bfd_endian byte_order;
e3b94546 23599 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 23600
b6807d98
TT
23601 if (per_cu->cu == NULL)
23602 load_cu (per_cu);
23603 cu = per_cu->cu;
cc12ce38
DE
23604 if (cu == NULL)
23605 {
23606 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23607 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23608 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23609 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23610 }
b6807d98 23611
9c541725 23612 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23613 if (!die)
9d8780f0
SM
23614 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23615 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23616
23617 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23618 if (attr == NULL)
23619 return NULL;
23620
e3b94546 23621 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23622 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23623
23624 switch (attr->form)
23625 {
23626 case DW_FORM_addr:
23627 case DW_FORM_GNU_addr_index:
23628 {
23629 gdb_byte *tem;
23630
23631 *len = cu->header.addr_size;
224c3ddb 23632 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23633 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23634 result = tem;
23635 }
23636 break;
23637 case DW_FORM_string:
23638 case DW_FORM_strp:
23639 case DW_FORM_GNU_str_index:
23640 case DW_FORM_GNU_strp_alt:
23641 /* DW_STRING is already allocated on the objfile obstack, point
23642 directly to it. */
23643 result = (const gdb_byte *) DW_STRING (attr);
23644 *len = strlen (DW_STRING (attr));
23645 break;
23646 case DW_FORM_block1:
23647 case DW_FORM_block2:
23648 case DW_FORM_block4:
23649 case DW_FORM_block:
23650 case DW_FORM_exprloc:
0224619f 23651 case DW_FORM_data16:
b6807d98
TT
23652 result = DW_BLOCK (attr)->data;
23653 *len = DW_BLOCK (attr)->size;
23654 break;
23655
23656 /* The DW_AT_const_value attributes are supposed to carry the
23657 symbol's value "represented as it would be on the target
23658 architecture." By the time we get here, it's already been
23659 converted to host endianness, so we just need to sign- or
23660 zero-extend it as appropriate. */
23661 case DW_FORM_data1:
23662 type = die_type (die, cu);
23663 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23664 if (result == NULL)
23665 result = write_constant_as_bytes (obstack, byte_order,
23666 type, value, len);
23667 break;
23668 case DW_FORM_data2:
23669 type = die_type (die, cu);
23670 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23671 if (result == NULL)
23672 result = write_constant_as_bytes (obstack, byte_order,
23673 type, value, len);
23674 break;
23675 case DW_FORM_data4:
23676 type = die_type (die, cu);
23677 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23678 if (result == NULL)
23679 result = write_constant_as_bytes (obstack, byte_order,
23680 type, value, len);
23681 break;
23682 case DW_FORM_data8:
23683 type = die_type (die, cu);
23684 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23685 if (result == NULL)
23686 result = write_constant_as_bytes (obstack, byte_order,
23687 type, value, len);
23688 break;
23689
23690 case DW_FORM_sdata:
663c44ac 23691 case DW_FORM_implicit_const:
b6807d98
TT
23692 type = die_type (die, cu);
23693 result = write_constant_as_bytes (obstack, byte_order,
23694 type, DW_SND (attr), len);
23695 break;
23696
23697 case DW_FORM_udata:
23698 type = die_type (die, cu);
23699 result = write_constant_as_bytes (obstack, byte_order,
23700 type, DW_UNSND (attr), len);
23701 break;
23702
23703 default:
23704 complaint (&symfile_complaints,
23705 _("unsupported const value attribute form: '%s'"),
23706 dwarf_form_name (attr->form));
23707 break;
23708 }
23709
23710 return result;
23711}
23712
7942e96e
AA
23713/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23714 valid type for this die is found. */
23715
23716struct type *
9c541725 23717dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23718 struct dwarf2_per_cu_data *per_cu)
23719{
23720 struct dwarf2_cu *cu;
23721 struct die_info *die;
23722
7942e96e
AA
23723 if (per_cu->cu == NULL)
23724 load_cu (per_cu);
23725 cu = per_cu->cu;
23726 if (!cu)
23727 return NULL;
23728
9c541725 23729 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23730 if (!die)
23731 return NULL;
23732
23733 return die_type (die, cu);
23734}
23735
8a9b8146
TT
23736/* Return the type of the DIE at DIE_OFFSET in the CU named by
23737 PER_CU. */
23738
23739struct type *
b64f50a1 23740dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23741 struct dwarf2_per_cu_data *per_cu)
23742{
9c541725 23743 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23744 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23745}
23746
ac9ec31b 23747/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23748 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23749 On exit *REF_CU is the CU of the result.
23750 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23751
23752static struct die_info *
ac9ec31b
DE
23753follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23754 struct dwarf2_cu **ref_cu)
348e048f 23755{
348e048f 23756 struct die_info temp_die;
348e048f
DE
23757 struct dwarf2_cu *sig_cu;
23758 struct die_info *die;
23759
ac9ec31b
DE
23760 /* While it might be nice to assert sig_type->type == NULL here,
23761 we can get here for DW_AT_imported_declaration where we need
23762 the DIE not the type. */
348e048f
DE
23763
23764 /* If necessary, add it to the queue and load its DIEs. */
23765
95554aad 23766 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23767 read_signatured_type (sig_type);
348e048f 23768
348e048f 23769 sig_cu = sig_type->per_cu.cu;
69d751e3 23770 gdb_assert (sig_cu != NULL);
9c541725
PA
23771 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23772 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23773 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23774 to_underlying (temp_die.sect_off));
348e048f
DE
23775 if (die)
23776 {
ed2dc618 23777 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23778 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23779
796a7ff8
DE
23780 /* For .gdb_index version 7 keep track of included TUs.
23781 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23782 if (dwarf2_per_objfile->index_table != NULL
23783 && dwarf2_per_objfile->index_table->version <= 7)
23784 {
23785 VEC_safe_push (dwarf2_per_cu_ptr,
23786 (*ref_cu)->per_cu->imported_symtabs,
23787 sig_cu->per_cu);
23788 }
23789
348e048f
DE
23790 *ref_cu = sig_cu;
23791 return die;
23792 }
23793
ac9ec31b
DE
23794 return NULL;
23795}
23796
23797/* Follow signatured type referenced by ATTR in SRC_DIE.
23798 On entry *REF_CU is the CU of SRC_DIE.
23799 On exit *REF_CU is the CU of the result.
23800 The result is the DIE of the type.
23801 If the referenced type cannot be found an error is thrown. */
23802
23803static struct die_info *
ff39bb5e 23804follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23805 struct dwarf2_cu **ref_cu)
23806{
23807 ULONGEST signature = DW_SIGNATURE (attr);
23808 struct signatured_type *sig_type;
23809 struct die_info *die;
23810
23811 gdb_assert (attr->form == DW_FORM_ref_sig8);
23812
a2ce51a0 23813 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23814 /* sig_type will be NULL if the signatured type is missing from
23815 the debug info. */
23816 if (sig_type == NULL)
23817 {
23818 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23819 " from DIE at %s [in module %s]"),
23820 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23821 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23822 }
23823
23824 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23825 if (die == NULL)
23826 {
23827 dump_die_for_error (src_die);
23828 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23829 " from DIE at %s [in module %s]"),
23830 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23831 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23832 }
23833
23834 return die;
23835}
23836
23837/* Get the type specified by SIGNATURE referenced in DIE/CU,
23838 reading in and processing the type unit if necessary. */
23839
23840static struct type *
23841get_signatured_type (struct die_info *die, ULONGEST signature,
23842 struct dwarf2_cu *cu)
23843{
518817b3
SM
23844 struct dwarf2_per_objfile *dwarf2_per_objfile
23845 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23846 struct signatured_type *sig_type;
23847 struct dwarf2_cu *type_cu;
23848 struct die_info *type_die;
23849 struct type *type;
23850
a2ce51a0 23851 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23852 /* sig_type will be NULL if the signatured type is missing from
23853 the debug info. */
23854 if (sig_type == NULL)
23855 {
23856 complaint (&symfile_complaints,
23857 _("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23858 " from DIE at %s [in module %s]"),
23859 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23860 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23861 return build_error_marker_type (cu, die);
23862 }
23863
23864 /* If we already know the type we're done. */
23865 if (sig_type->type != NULL)
23866 return sig_type->type;
23867
23868 type_cu = cu;
23869 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23870 if (type_die != NULL)
23871 {
23872 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23873 is created. This is important, for example, because for c++ classes
23874 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23875 type = read_type_die (type_die, type_cu);
23876 if (type == NULL)
23877 {
23878 complaint (&symfile_complaints,
23879 _("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23880 " referenced from DIE at %s [in module %s]"),
23881 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23882 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23883 type = build_error_marker_type (cu, die);
23884 }
23885 }
23886 else
23887 {
23888 complaint (&symfile_complaints,
23889 _("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23890 " from DIE at %s [in module %s]"),
23891 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23892 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23893 type = build_error_marker_type (cu, die);
23894 }
23895 sig_type->type = type;
23896
23897 return type;
23898}
23899
23900/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23901 reading in and processing the type unit if necessary. */
23902
23903static struct type *
ff39bb5e 23904get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23905 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23906{
23907 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 23908 if (attr_form_is_ref (attr))
ac9ec31b
DE
23909 {
23910 struct dwarf2_cu *type_cu = cu;
23911 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23912
23913 return read_type_die (type_die, type_cu);
23914 }
23915 else if (attr->form == DW_FORM_ref_sig8)
23916 {
23917 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23918 }
23919 else
23920 {
518817b3
SM
23921 struct dwarf2_per_objfile *dwarf2_per_objfile
23922 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23923
ac9ec31b
DE
23924 complaint (&symfile_complaints,
23925 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23926 " at %s [in module %s]"),
23927 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 23928 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23929 return build_error_marker_type (cu, die);
23930 }
348e048f
DE
23931}
23932
e5fe5e75 23933/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23934
23935static void
e5fe5e75 23936load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 23937{
52dc124a 23938 struct signatured_type *sig_type;
348e048f 23939
f4dc4d17
DE
23940 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23941 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23942
6721b2ec
DE
23943 /* We have the per_cu, but we need the signatured_type.
23944 Fortunately this is an easy translation. */
23945 gdb_assert (per_cu->is_debug_types);
23946 sig_type = (struct signatured_type *) per_cu;
348e048f 23947
6721b2ec 23948 gdb_assert (per_cu->cu == NULL);
348e048f 23949
52dc124a 23950 read_signatured_type (sig_type);
348e048f 23951
6721b2ec 23952 gdb_assert (per_cu->cu != NULL);
348e048f
DE
23953}
23954
dee91e82
DE
23955/* die_reader_func for read_signatured_type.
23956 This is identical to load_full_comp_unit_reader,
23957 but is kept separate for now. */
348e048f
DE
23958
23959static void
dee91e82 23960read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 23961 const gdb_byte *info_ptr,
dee91e82
DE
23962 struct die_info *comp_unit_die,
23963 int has_children,
23964 void *data)
348e048f 23965{
dee91e82 23966 struct dwarf2_cu *cu = reader->cu;
348e048f 23967
dee91e82
DE
23968 gdb_assert (cu->die_hash == NULL);
23969 cu->die_hash =
23970 htab_create_alloc_ex (cu->header.length / 12,
23971 die_hash,
23972 die_eq,
23973 NULL,
23974 &cu->comp_unit_obstack,
23975 hashtab_obstack_allocate,
23976 dummy_obstack_deallocate);
348e048f 23977
dee91e82
DE
23978 if (has_children)
23979 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23980 &info_ptr, comp_unit_die);
23981 cu->dies = comp_unit_die;
23982 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
23983
23984 /* We try not to read any attributes in this function, because not
9cdd5dbd 23985 all CUs needed for references have been loaded yet, and symbol
348e048f 23986 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
23987 or we won't be able to build types correctly.
23988 Similarly, if we do not read the producer, we can not apply
23989 producer-specific interpretation. */
95554aad 23990 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 23991}
348e048f 23992
3019eac3
DE
23993/* Read in a signatured type and build its CU and DIEs.
23994 If the type is a stub for the real type in a DWO file,
23995 read in the real type from the DWO file as well. */
dee91e82
DE
23996
23997static void
23998read_signatured_type (struct signatured_type *sig_type)
23999{
24000 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 24001
3019eac3 24002 gdb_assert (per_cu->is_debug_types);
dee91e82 24003 gdb_assert (per_cu->cu == NULL);
348e048f 24004
f4dc4d17
DE
24005 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
24006 read_signatured_type_reader, NULL);
7ee85ab1 24007 sig_type->per_cu.tu_read = 1;
c906108c
SS
24008}
24009
c906108c
SS
24010/* Decode simple location descriptions.
24011 Given a pointer to a dwarf block that defines a location, compute
24012 the location and return the value.
24013
4cecd739
DJ
24014 NOTE drow/2003-11-18: This function is called in two situations
24015 now: for the address of static or global variables (partial symbols
24016 only) and for offsets into structures which are expected to be
24017 (more or less) constant. The partial symbol case should go away,
24018 and only the constant case should remain. That will let this
24019 function complain more accurately. A few special modes are allowed
24020 without complaint for global variables (for instance, global
24021 register values and thread-local values).
c906108c
SS
24022
24023 A location description containing no operations indicates that the
4cecd739 24024 object is optimized out. The return value is 0 for that case.
6b992462
DJ
24025 FIXME drow/2003-11-16: No callers check for this case any more; soon all
24026 callers will only want a very basic result and this can become a
21ae7a4d
JK
24027 complaint.
24028
24029 Note that stack[0] is unused except as a default error return. */
c906108c
SS
24030
24031static CORE_ADDR
e7c27a73 24032decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 24033{
518817b3 24034 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
24035 size_t i;
24036 size_t size = blk->size;
d521ce57 24037 const gdb_byte *data = blk->data;
21ae7a4d
JK
24038 CORE_ADDR stack[64];
24039 int stacki;
24040 unsigned int bytes_read, unsnd;
24041 gdb_byte op;
c906108c 24042
21ae7a4d
JK
24043 i = 0;
24044 stacki = 0;
24045 stack[stacki] = 0;
24046 stack[++stacki] = 0;
24047
24048 while (i < size)
24049 {
24050 op = data[i++];
24051 switch (op)
24052 {
24053 case DW_OP_lit0:
24054 case DW_OP_lit1:
24055 case DW_OP_lit2:
24056 case DW_OP_lit3:
24057 case DW_OP_lit4:
24058 case DW_OP_lit5:
24059 case DW_OP_lit6:
24060 case DW_OP_lit7:
24061 case DW_OP_lit8:
24062 case DW_OP_lit9:
24063 case DW_OP_lit10:
24064 case DW_OP_lit11:
24065 case DW_OP_lit12:
24066 case DW_OP_lit13:
24067 case DW_OP_lit14:
24068 case DW_OP_lit15:
24069 case DW_OP_lit16:
24070 case DW_OP_lit17:
24071 case DW_OP_lit18:
24072 case DW_OP_lit19:
24073 case DW_OP_lit20:
24074 case DW_OP_lit21:
24075 case DW_OP_lit22:
24076 case DW_OP_lit23:
24077 case DW_OP_lit24:
24078 case DW_OP_lit25:
24079 case DW_OP_lit26:
24080 case DW_OP_lit27:
24081 case DW_OP_lit28:
24082 case DW_OP_lit29:
24083 case DW_OP_lit30:
24084 case DW_OP_lit31:
24085 stack[++stacki] = op - DW_OP_lit0;
24086 break;
f1bea926 24087
21ae7a4d
JK
24088 case DW_OP_reg0:
24089 case DW_OP_reg1:
24090 case DW_OP_reg2:
24091 case DW_OP_reg3:
24092 case DW_OP_reg4:
24093 case DW_OP_reg5:
24094 case DW_OP_reg6:
24095 case DW_OP_reg7:
24096 case DW_OP_reg8:
24097 case DW_OP_reg9:
24098 case DW_OP_reg10:
24099 case DW_OP_reg11:
24100 case DW_OP_reg12:
24101 case DW_OP_reg13:
24102 case DW_OP_reg14:
24103 case DW_OP_reg15:
24104 case DW_OP_reg16:
24105 case DW_OP_reg17:
24106 case DW_OP_reg18:
24107 case DW_OP_reg19:
24108 case DW_OP_reg20:
24109 case DW_OP_reg21:
24110 case DW_OP_reg22:
24111 case DW_OP_reg23:
24112 case DW_OP_reg24:
24113 case DW_OP_reg25:
24114 case DW_OP_reg26:
24115 case DW_OP_reg27:
24116 case DW_OP_reg28:
24117 case DW_OP_reg29:
24118 case DW_OP_reg30:
24119 case DW_OP_reg31:
24120 stack[++stacki] = op - DW_OP_reg0;
24121 if (i < size)
24122 dwarf2_complex_location_expr_complaint ();
24123 break;
c906108c 24124
21ae7a4d
JK
24125 case DW_OP_regx:
24126 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24127 i += bytes_read;
24128 stack[++stacki] = unsnd;
24129 if (i < size)
24130 dwarf2_complex_location_expr_complaint ();
24131 break;
c906108c 24132
21ae7a4d
JK
24133 case DW_OP_addr:
24134 stack[++stacki] = read_address (objfile->obfd, &data[i],
24135 cu, &bytes_read);
24136 i += bytes_read;
24137 break;
d53d4ac5 24138
21ae7a4d
JK
24139 case DW_OP_const1u:
24140 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24141 i += 1;
24142 break;
24143
24144 case DW_OP_const1s:
24145 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24146 i += 1;
24147 break;
24148
24149 case DW_OP_const2u:
24150 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24151 i += 2;
24152 break;
24153
24154 case DW_OP_const2s:
24155 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24156 i += 2;
24157 break;
d53d4ac5 24158
21ae7a4d
JK
24159 case DW_OP_const4u:
24160 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24161 i += 4;
24162 break;
24163
24164 case DW_OP_const4s:
24165 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24166 i += 4;
24167 break;
24168
585861ea
JK
24169 case DW_OP_const8u:
24170 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24171 i += 8;
24172 break;
24173
21ae7a4d
JK
24174 case DW_OP_constu:
24175 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24176 &bytes_read);
24177 i += bytes_read;
24178 break;
24179
24180 case DW_OP_consts:
24181 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24182 i += bytes_read;
24183 break;
24184
24185 case DW_OP_dup:
24186 stack[stacki + 1] = stack[stacki];
24187 stacki++;
24188 break;
24189
24190 case DW_OP_plus:
24191 stack[stacki - 1] += stack[stacki];
24192 stacki--;
24193 break;
24194
24195 case DW_OP_plus_uconst:
24196 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24197 &bytes_read);
24198 i += bytes_read;
24199 break;
24200
24201 case DW_OP_minus:
24202 stack[stacki - 1] -= stack[stacki];
24203 stacki--;
24204 break;
24205
24206 case DW_OP_deref:
24207 /* If we're not the last op, then we definitely can't encode
24208 this using GDB's address_class enum. This is valid for partial
24209 global symbols, although the variable's address will be bogus
24210 in the psymtab. */
24211 if (i < size)
24212 dwarf2_complex_location_expr_complaint ();
24213 break;
24214
24215 case DW_OP_GNU_push_tls_address:
4aa4e28b 24216 case DW_OP_form_tls_address:
21ae7a4d
JK
24217 /* The top of the stack has the offset from the beginning
24218 of the thread control block at which the variable is located. */
24219 /* Nothing should follow this operator, so the top of stack would
24220 be returned. */
24221 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24222 address will be bogus in the psymtab. Make it always at least
24223 non-zero to not look as a variable garbage collected by linker
24224 which have DW_OP_addr 0. */
21ae7a4d
JK
24225 if (i < size)
24226 dwarf2_complex_location_expr_complaint ();
585861ea 24227 stack[stacki]++;
21ae7a4d
JK
24228 break;
24229
24230 case DW_OP_GNU_uninit:
24231 break;
24232
3019eac3 24233 case DW_OP_GNU_addr_index:
49f6c839 24234 case DW_OP_GNU_const_index:
3019eac3
DE
24235 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24236 &bytes_read);
24237 i += bytes_read;
24238 break;
24239
21ae7a4d
JK
24240 default:
24241 {
f39c6ffd 24242 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
24243
24244 if (name)
24245 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
24246 name);
24247 else
24248 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
24249 op);
24250 }
24251
24252 return (stack[stacki]);
d53d4ac5 24253 }
3c6e0cb3 24254
21ae7a4d
JK
24255 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24256 outside of the allocated space. Also enforce minimum>0. */
24257 if (stacki >= ARRAY_SIZE (stack) - 1)
24258 {
24259 complaint (&symfile_complaints,
24260 _("location description stack overflow"));
24261 return 0;
24262 }
24263
24264 if (stacki <= 0)
24265 {
24266 complaint (&symfile_complaints,
24267 _("location description stack underflow"));
24268 return 0;
24269 }
24270 }
24271 return (stack[stacki]);
c906108c
SS
24272}
24273
24274/* memory allocation interface */
24275
c906108c 24276static struct dwarf_block *
7b5a2f43 24277dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24278{
8d749320 24279 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24280}
24281
c906108c 24282static struct die_info *
b60c80d6 24283dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24284{
24285 struct die_info *die;
b60c80d6
DJ
24286 size_t size = sizeof (struct die_info);
24287
24288 if (num_attrs > 1)
24289 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24290
b60c80d6 24291 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24292 memset (die, 0, sizeof (struct die_info));
24293 return (die);
24294}
2e276125
JB
24295
24296\f
24297/* Macro support. */
24298
233d95b5
JK
24299/* Return file name relative to the compilation directory of file number I in
24300 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 24301 responsible for freeing it. */
233d95b5 24302
2e276125 24303static char *
233d95b5 24304file_file_name (int file, struct line_header *lh)
2e276125 24305{
6a83a1e6
EZ
24306 /* Is the file number a valid index into the line header's file name
24307 table? Remember that file numbers start with one, not zero. */
fff8551c 24308 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 24309 {
8c43009f 24310 const file_entry &fe = lh->file_names[file - 1];
6e70227d 24311
8c43009f
PA
24312 if (!IS_ABSOLUTE_PATH (fe.name))
24313 {
24314 const char *dir = fe.include_dir (lh);
24315 if (dir != NULL)
24316 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
24317 }
24318 return xstrdup (fe.name);
6a83a1e6 24319 }
2e276125
JB
24320 else
24321 {
6a83a1e6
EZ
24322 /* The compiler produced a bogus file number. We can at least
24323 record the macro definitions made in the file, even if we
24324 won't be able to find the file by name. */
24325 char fake_name[80];
9a619af0 24326
8c042590
PM
24327 xsnprintf (fake_name, sizeof (fake_name),
24328 "<bad macro file number %d>", file);
2e276125 24329
6e70227d 24330 complaint (&symfile_complaints,
6a83a1e6
EZ
24331 _("bad file number in macro information (%d)"),
24332 file);
2e276125 24333
6a83a1e6 24334 return xstrdup (fake_name);
2e276125
JB
24335 }
24336}
24337
233d95b5
JK
24338/* Return the full name of file number I in *LH's file name table.
24339 Use COMP_DIR as the name of the current directory of the
24340 compilation. The result is allocated using xmalloc; the caller is
24341 responsible for freeing it. */
24342static char *
24343file_full_name (int file, struct line_header *lh, const char *comp_dir)
24344{
24345 /* Is the file number a valid index into the line header's file name
24346 table? Remember that file numbers start with one, not zero. */
fff8551c 24347 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
24348 {
24349 char *relative = file_file_name (file, lh);
24350
24351 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24352 return relative;
b36cec19
PA
24353 return reconcat (relative, comp_dir, SLASH_STRING,
24354 relative, (char *) NULL);
233d95b5
JK
24355 }
24356 else
24357 return file_file_name (file, lh);
24358}
24359
2e276125
JB
24360
24361static struct macro_source_file *
24362macro_start_file (int file, int line,
24363 struct macro_source_file *current_file,
43f3e411 24364 struct line_header *lh)
2e276125 24365{
233d95b5
JK
24366 /* File name relative to the compilation directory of this source file. */
24367 char *file_name = file_file_name (file, lh);
2e276125 24368
2e276125 24369 if (! current_file)
abc9d0dc 24370 {
fc474241
DE
24371 /* Note: We don't create a macro table for this compilation unit
24372 at all until we actually get a filename. */
43f3e411 24373 struct macro_table *macro_table = get_macro_table ();
fc474241 24374
abc9d0dc
TT
24375 /* If we have no current file, then this must be the start_file
24376 directive for the compilation unit's main source file. */
fc474241
DE
24377 current_file = macro_set_main (macro_table, file_name);
24378 macro_define_special (macro_table);
abc9d0dc 24379 }
2e276125 24380 else
233d95b5 24381 current_file = macro_include (current_file, line, file_name);
2e276125 24382
233d95b5 24383 xfree (file_name);
6e70227d 24384
2e276125
JB
24385 return current_file;
24386}
24387
2e276125
JB
24388static const char *
24389consume_improper_spaces (const char *p, const char *body)
24390{
24391 if (*p == ' ')
24392 {
4d3c2250 24393 complaint (&symfile_complaints,
3e43a32a
MS
24394 _("macro definition contains spaces "
24395 "in formal argument list:\n`%s'"),
4d3c2250 24396 body);
2e276125
JB
24397
24398 while (*p == ' ')
24399 p++;
24400 }
24401
24402 return p;
24403}
24404
24405
24406static void
24407parse_macro_definition (struct macro_source_file *file, int line,
24408 const char *body)
24409{
24410 const char *p;
24411
24412 /* The body string takes one of two forms. For object-like macro
24413 definitions, it should be:
24414
24415 <macro name> " " <definition>
24416
24417 For function-like macro definitions, it should be:
24418
24419 <macro name> "() " <definition>
24420 or
24421 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24422
24423 Spaces may appear only where explicitly indicated, and in the
24424 <definition>.
24425
24426 The Dwarf 2 spec says that an object-like macro's name is always
24427 followed by a space, but versions of GCC around March 2002 omit
6e70227d 24428 the space when the macro's definition is the empty string.
2e276125
JB
24429
24430 The Dwarf 2 spec says that there should be no spaces between the
24431 formal arguments in a function-like macro's formal argument list,
24432 but versions of GCC around March 2002 include spaces after the
24433 commas. */
24434
24435
24436 /* Find the extent of the macro name. The macro name is terminated
24437 by either a space or null character (for an object-like macro) or
24438 an opening paren (for a function-like macro). */
24439 for (p = body; *p; p++)
24440 if (*p == ' ' || *p == '(')
24441 break;
24442
24443 if (*p == ' ' || *p == '\0')
24444 {
24445 /* It's an object-like macro. */
24446 int name_len = p - body;
3f8a7804 24447 char *name = savestring (body, name_len);
2e276125
JB
24448 const char *replacement;
24449
24450 if (*p == ' ')
24451 replacement = body + name_len + 1;
24452 else
24453 {
4d3c2250 24454 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24455 replacement = body + name_len;
24456 }
6e70227d 24457
2e276125
JB
24458 macro_define_object (file, line, name, replacement);
24459
24460 xfree (name);
24461 }
24462 else if (*p == '(')
24463 {
24464 /* It's a function-like macro. */
3f8a7804 24465 char *name = savestring (body, p - body);
2e276125
JB
24466 int argc = 0;
24467 int argv_size = 1;
8d749320 24468 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
24469
24470 p++;
24471
24472 p = consume_improper_spaces (p, body);
24473
24474 /* Parse the formal argument list. */
24475 while (*p && *p != ')')
24476 {
24477 /* Find the extent of the current argument name. */
24478 const char *arg_start = p;
24479
24480 while (*p && *p != ',' && *p != ')' && *p != ' ')
24481 p++;
24482
24483 if (! *p || p == arg_start)
4d3c2250 24484 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24485 else
24486 {
24487 /* Make sure argv has room for the new argument. */
24488 if (argc >= argv_size)
24489 {
24490 argv_size *= 2;
224c3ddb 24491 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
24492 }
24493
3f8a7804 24494 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
24495 }
24496
24497 p = consume_improper_spaces (p, body);
24498
24499 /* Consume the comma, if present. */
24500 if (*p == ',')
24501 {
24502 p++;
24503
24504 p = consume_improper_spaces (p, body);
24505 }
24506 }
24507
24508 if (*p == ')')
24509 {
24510 p++;
24511
24512 if (*p == ' ')
24513 /* Perfectly formed definition, no complaints. */
24514 macro_define_function (file, line, name,
6e70227d 24515 argc, (const char **) argv,
2e276125
JB
24516 p + 1);
24517 else if (*p == '\0')
24518 {
24519 /* Complain, but do define it. */
4d3c2250 24520 dwarf2_macro_malformed_definition_complaint (body);
2e276125 24521 macro_define_function (file, line, name,
6e70227d 24522 argc, (const char **) argv,
2e276125
JB
24523 p);
24524 }
24525 else
24526 /* Just complain. */
4d3c2250 24527 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24528 }
24529 else
24530 /* Just complain. */
4d3c2250 24531 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24532
24533 xfree (name);
24534 {
24535 int i;
24536
24537 for (i = 0; i < argc; i++)
24538 xfree (argv[i]);
24539 }
24540 xfree (argv);
24541 }
24542 else
4d3c2250 24543 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24544}
24545
cf2c3c16
TT
24546/* Skip some bytes from BYTES according to the form given in FORM.
24547 Returns the new pointer. */
2e276125 24548
d521ce57
TT
24549static const gdb_byte *
24550skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
24551 enum dwarf_form form,
24552 unsigned int offset_size,
24553 struct dwarf2_section_info *section)
2e276125 24554{
cf2c3c16 24555 unsigned int bytes_read;
2e276125 24556
cf2c3c16 24557 switch (form)
2e276125 24558 {
cf2c3c16
TT
24559 case DW_FORM_data1:
24560 case DW_FORM_flag:
24561 ++bytes;
24562 break;
24563
24564 case DW_FORM_data2:
24565 bytes += 2;
24566 break;
24567
24568 case DW_FORM_data4:
24569 bytes += 4;
24570 break;
24571
24572 case DW_FORM_data8:
24573 bytes += 8;
24574 break;
24575
0224619f
JK
24576 case DW_FORM_data16:
24577 bytes += 16;
24578 break;
24579
cf2c3c16
TT
24580 case DW_FORM_string:
24581 read_direct_string (abfd, bytes, &bytes_read);
24582 bytes += bytes_read;
24583 break;
24584
24585 case DW_FORM_sec_offset:
24586 case DW_FORM_strp:
36586728 24587 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
24588 bytes += offset_size;
24589 break;
24590
24591 case DW_FORM_block:
24592 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24593 bytes += bytes_read;
24594 break;
24595
24596 case DW_FORM_block1:
24597 bytes += 1 + read_1_byte (abfd, bytes);
24598 break;
24599 case DW_FORM_block2:
24600 bytes += 2 + read_2_bytes (abfd, bytes);
24601 break;
24602 case DW_FORM_block4:
24603 bytes += 4 + read_4_bytes (abfd, bytes);
24604 break;
24605
24606 case DW_FORM_sdata:
24607 case DW_FORM_udata:
3019eac3
DE
24608 case DW_FORM_GNU_addr_index:
24609 case DW_FORM_GNU_str_index:
d521ce57 24610 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
24611 if (bytes == NULL)
24612 {
24613 dwarf2_section_buffer_overflow_complaint (section);
24614 return NULL;
24615 }
cf2c3c16
TT
24616 break;
24617
663c44ac
JK
24618 case DW_FORM_implicit_const:
24619 break;
24620
cf2c3c16
TT
24621 default:
24622 {
cf2c3c16
TT
24623 complaint (&symfile_complaints,
24624 _("invalid form 0x%x in `%s'"),
a32a8923 24625 form, get_section_name (section));
cf2c3c16
TT
24626 return NULL;
24627 }
2e276125
JB
24628 }
24629
cf2c3c16
TT
24630 return bytes;
24631}
757a13d0 24632
cf2c3c16
TT
24633/* A helper for dwarf_decode_macros that handles skipping an unknown
24634 opcode. Returns an updated pointer to the macro data buffer; or,
24635 on error, issues a complaint and returns NULL. */
757a13d0 24636
d521ce57 24637static const gdb_byte *
cf2c3c16 24638skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24639 const gdb_byte **opcode_definitions,
24640 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24641 bfd *abfd,
24642 unsigned int offset_size,
24643 struct dwarf2_section_info *section)
24644{
24645 unsigned int bytes_read, i;
24646 unsigned long arg;
d521ce57 24647 const gdb_byte *defn;
2e276125 24648
cf2c3c16 24649 if (opcode_definitions[opcode] == NULL)
2e276125 24650 {
cf2c3c16
TT
24651 complaint (&symfile_complaints,
24652 _("unrecognized DW_MACFINO opcode 0x%x"),
24653 opcode);
24654 return NULL;
24655 }
2e276125 24656
cf2c3c16
TT
24657 defn = opcode_definitions[opcode];
24658 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24659 defn += bytes_read;
2e276125 24660
cf2c3c16
TT
24661 for (i = 0; i < arg; ++i)
24662 {
aead7601
SM
24663 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24664 (enum dwarf_form) defn[i], offset_size,
f664829e 24665 section);
cf2c3c16
TT
24666 if (mac_ptr == NULL)
24667 {
24668 /* skip_form_bytes already issued the complaint. */
24669 return NULL;
24670 }
24671 }
757a13d0 24672
cf2c3c16
TT
24673 return mac_ptr;
24674}
757a13d0 24675
cf2c3c16
TT
24676/* A helper function which parses the header of a macro section.
24677 If the macro section is the extended (for now called "GNU") type,
24678 then this updates *OFFSET_SIZE. Returns a pointer to just after
24679 the header, or issues a complaint and returns NULL on error. */
757a13d0 24680
d521ce57
TT
24681static const gdb_byte *
24682dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24683 bfd *abfd,
d521ce57 24684 const gdb_byte *mac_ptr,
cf2c3c16
TT
24685 unsigned int *offset_size,
24686 int section_is_gnu)
24687{
24688 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24689
cf2c3c16
TT
24690 if (section_is_gnu)
24691 {
24692 unsigned int version, flags;
757a13d0 24693
cf2c3c16 24694 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24695 if (version != 4 && version != 5)
cf2c3c16
TT
24696 {
24697 complaint (&symfile_complaints,
24698 _("unrecognized version `%d' in .debug_macro section"),
24699 version);
24700 return NULL;
24701 }
24702 mac_ptr += 2;
757a13d0 24703
cf2c3c16
TT
24704 flags = read_1_byte (abfd, mac_ptr);
24705 ++mac_ptr;
24706 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24707
cf2c3c16
TT
24708 if ((flags & 2) != 0)
24709 /* We don't need the line table offset. */
24710 mac_ptr += *offset_size;
757a13d0 24711
cf2c3c16
TT
24712 /* Vendor opcode descriptions. */
24713 if ((flags & 4) != 0)
24714 {
24715 unsigned int i, count;
757a13d0 24716
cf2c3c16
TT
24717 count = read_1_byte (abfd, mac_ptr);
24718 ++mac_ptr;
24719 for (i = 0; i < count; ++i)
24720 {
24721 unsigned int opcode, bytes_read;
24722 unsigned long arg;
24723
24724 opcode = read_1_byte (abfd, mac_ptr);
24725 ++mac_ptr;
24726 opcode_definitions[opcode] = mac_ptr;
24727 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24728 mac_ptr += bytes_read;
24729 mac_ptr += arg;
24730 }
757a13d0 24731 }
cf2c3c16 24732 }
757a13d0 24733
cf2c3c16
TT
24734 return mac_ptr;
24735}
757a13d0 24736
cf2c3c16 24737/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24738 including DW_MACRO_import. */
cf2c3c16
TT
24739
24740static void
ed2dc618
SM
24741dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24742 bfd *abfd,
d521ce57 24743 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24744 struct macro_source_file *current_file,
43f3e411 24745 struct line_header *lh,
cf2c3c16 24746 struct dwarf2_section_info *section,
36586728 24747 int section_is_gnu, int section_is_dwz,
cf2c3c16 24748 unsigned int offset_size,
8fc3fc34 24749 htab_t include_hash)
cf2c3c16 24750{
4d663531 24751 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24752 enum dwarf_macro_record_type macinfo_type;
24753 int at_commandline;
d521ce57 24754 const gdb_byte *opcode_definitions[256];
757a13d0 24755
cf2c3c16
TT
24756 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24757 &offset_size, section_is_gnu);
24758 if (mac_ptr == NULL)
24759 {
24760 /* We already issued a complaint. */
24761 return;
24762 }
757a13d0
JK
24763
24764 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24765 GDB is still reading the definitions from command line. First
24766 DW_MACINFO_start_file will need to be ignored as it was already executed
24767 to create CURRENT_FILE for the main source holding also the command line
24768 definitions. On first met DW_MACINFO_start_file this flag is reset to
24769 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24770
24771 at_commandline = 1;
24772
24773 do
24774 {
24775 /* Do we at least have room for a macinfo type byte? */
24776 if (mac_ptr >= mac_end)
24777 {
f664829e 24778 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24779 break;
24780 }
24781
aead7601 24782 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24783 mac_ptr++;
24784
cf2c3c16
TT
24785 /* Note that we rely on the fact that the corresponding GNU and
24786 DWARF constants are the same. */
132448f8
SM
24787 DIAGNOSTIC_PUSH
24788 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24789 switch (macinfo_type)
24790 {
24791 /* A zero macinfo type indicates the end of the macro
24792 information. */
24793 case 0:
24794 break;
2e276125 24795
0af92d60
JK
24796 case DW_MACRO_define:
24797 case DW_MACRO_undef:
24798 case DW_MACRO_define_strp:
24799 case DW_MACRO_undef_strp:
24800 case DW_MACRO_define_sup:
24801 case DW_MACRO_undef_sup:
2e276125 24802 {
891d2f0b 24803 unsigned int bytes_read;
2e276125 24804 int line;
d521ce57 24805 const char *body;
cf2c3c16 24806 int is_define;
2e276125 24807
cf2c3c16
TT
24808 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24809 mac_ptr += bytes_read;
24810
0af92d60
JK
24811 if (macinfo_type == DW_MACRO_define
24812 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24813 {
24814 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24815 mac_ptr += bytes_read;
24816 }
24817 else
24818 {
24819 LONGEST str_offset;
24820
24821 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24822 mac_ptr += offset_size;
2e276125 24823
0af92d60
JK
24824 if (macinfo_type == DW_MACRO_define_sup
24825 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24826 || section_is_dwz)
36586728 24827 {
ed2dc618
SM
24828 struct dwz_file *dwz
24829 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24830
ed2dc618
SM
24831 body = read_indirect_string_from_dwz (objfile,
24832 dwz, str_offset);
36586728
TT
24833 }
24834 else
ed2dc618
SM
24835 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24836 abfd, str_offset);
cf2c3c16
TT
24837 }
24838
0af92d60
JK
24839 is_define = (macinfo_type == DW_MACRO_define
24840 || macinfo_type == DW_MACRO_define_strp
24841 || macinfo_type == DW_MACRO_define_sup);
2e276125 24842 if (! current_file)
757a13d0
JK
24843 {
24844 /* DWARF violation as no main source is present. */
24845 complaint (&symfile_complaints,
24846 _("debug info with no main source gives macro %s "
24847 "on line %d: %s"),
cf2c3c16
TT
24848 is_define ? _("definition") : _("undefinition"),
24849 line, body);
757a13d0
JK
24850 break;
24851 }
3e43a32a
MS
24852 if ((line == 0 && !at_commandline)
24853 || (line != 0 && at_commandline))
4d3c2250 24854 complaint (&symfile_complaints,
757a13d0
JK
24855 _("debug info gives %s macro %s with %s line %d: %s"),
24856 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24857 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24858 line == 0 ? _("zero") : _("non-zero"), line, body);
24859
cf2c3c16 24860 if (is_define)
757a13d0 24861 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24862 else
24863 {
0af92d60
JK
24864 gdb_assert (macinfo_type == DW_MACRO_undef
24865 || macinfo_type == DW_MACRO_undef_strp
24866 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24867 macro_undef (current_file, line, body);
24868 }
2e276125
JB
24869 }
24870 break;
24871
0af92d60 24872 case DW_MACRO_start_file:
2e276125 24873 {
891d2f0b 24874 unsigned int bytes_read;
2e276125
JB
24875 int line, file;
24876
24877 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24878 mac_ptr += bytes_read;
24879 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24880 mac_ptr += bytes_read;
24881
3e43a32a
MS
24882 if ((line == 0 && !at_commandline)
24883 || (line != 0 && at_commandline))
757a13d0
JK
24884 complaint (&symfile_complaints,
24885 _("debug info gives source %d included "
24886 "from %s at %s line %d"),
24887 file, at_commandline ? _("command-line") : _("file"),
24888 line == 0 ? _("zero") : _("non-zero"), line);
24889
24890 if (at_commandline)
24891 {
0af92d60 24892 /* This DW_MACRO_start_file was executed in the
cf2c3c16 24893 pass one. */
757a13d0
JK
24894 at_commandline = 0;
24895 }
24896 else
43f3e411 24897 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
24898 }
24899 break;
24900
0af92d60 24901 case DW_MACRO_end_file:
2e276125 24902 if (! current_file)
4d3c2250 24903 complaint (&symfile_complaints,
3e43a32a
MS
24904 _("macro debug info has an unmatched "
24905 "`close_file' directive"));
2e276125
JB
24906 else
24907 {
24908 current_file = current_file->included_by;
24909 if (! current_file)
24910 {
cf2c3c16 24911 enum dwarf_macro_record_type next_type;
2e276125
JB
24912
24913 /* GCC circa March 2002 doesn't produce the zero
24914 type byte marking the end of the compilation
24915 unit. Complain if it's not there, but exit no
24916 matter what. */
24917
24918 /* Do we at least have room for a macinfo type byte? */
24919 if (mac_ptr >= mac_end)
24920 {
f664829e 24921 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
24922 return;
24923 }
24924
24925 /* We don't increment mac_ptr here, so this is just
24926 a look-ahead. */
aead7601
SM
24927 next_type
24928 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24929 mac_ptr);
2e276125 24930 if (next_type != 0)
4d3c2250 24931 complaint (&symfile_complaints,
3e43a32a
MS
24932 _("no terminating 0-type entry for "
24933 "macros in `.debug_macinfo' section"));
2e276125
JB
24934
24935 return;
24936 }
24937 }
24938 break;
24939
0af92d60
JK
24940 case DW_MACRO_import:
24941 case DW_MACRO_import_sup:
cf2c3c16
TT
24942 {
24943 LONGEST offset;
8fc3fc34 24944 void **slot;
a036ba48
TT
24945 bfd *include_bfd = abfd;
24946 struct dwarf2_section_info *include_section = section;
d521ce57 24947 const gdb_byte *include_mac_end = mac_end;
a036ba48 24948 int is_dwz = section_is_dwz;
d521ce57 24949 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
24950
24951 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24952 mac_ptr += offset_size;
24953
0af92d60 24954 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 24955 {
ed2dc618 24956 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 24957
4d663531 24958 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 24959
a036ba48 24960 include_section = &dwz->macro;
a32a8923 24961 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
24962 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24963 is_dwz = 1;
24964 }
24965
24966 new_mac_ptr = include_section->buffer + offset;
24967 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24968
8fc3fc34
TT
24969 if (*slot != NULL)
24970 {
24971 /* This has actually happened; see
24972 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24973 complaint (&symfile_complaints,
0af92d60 24974 _("recursive DW_MACRO_import in "
8fc3fc34
TT
24975 ".debug_macro section"));
24976 }
24977 else
24978 {
d521ce57 24979 *slot = (void *) new_mac_ptr;
36586728 24980
ed2dc618
SM
24981 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24982 include_bfd, new_mac_ptr,
43f3e411 24983 include_mac_end, current_file, lh,
36586728 24984 section, section_is_gnu, is_dwz,
4d663531 24985 offset_size, include_hash);
8fc3fc34 24986
d521ce57 24987 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 24988 }
cf2c3c16
TT
24989 }
24990 break;
24991
2e276125 24992 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
24993 if (!section_is_gnu)
24994 {
24995 unsigned int bytes_read;
2e276125 24996
ac298888
TT
24997 /* This reads the constant, but since we don't recognize
24998 any vendor extensions, we ignore it. */
24999 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
25000 mac_ptr += bytes_read;
25001 read_direct_string (abfd, mac_ptr, &bytes_read);
25002 mac_ptr += bytes_read;
2e276125 25003
cf2c3c16
TT
25004 /* We don't recognize any vendor extensions. */
25005 break;
25006 }
25007 /* FALLTHROUGH */
25008
25009 default:
25010 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 25011 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
25012 section);
25013 if (mac_ptr == NULL)
25014 return;
25015 break;
2e276125 25016 }
132448f8 25017 DIAGNOSTIC_POP
757a13d0 25018 } while (macinfo_type != 0);
2e276125 25019}
8e19ed76 25020
cf2c3c16 25021static void
09262596 25022dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 25023 int section_is_gnu)
cf2c3c16 25024{
518817b3
SM
25025 struct dwarf2_per_objfile *dwarf2_per_objfile
25026 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 25027 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
25028 struct line_header *lh = cu->line_header;
25029 bfd *abfd;
d521ce57 25030 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
25031 struct macro_source_file *current_file = 0;
25032 enum dwarf_macro_record_type macinfo_type;
25033 unsigned int offset_size = cu->header.offset_size;
d521ce57 25034 const gdb_byte *opcode_definitions[256];
8fc3fc34 25035 void **slot;
09262596
DE
25036 struct dwarf2_section_info *section;
25037 const char *section_name;
25038
25039 if (cu->dwo_unit != NULL)
25040 {
25041 if (section_is_gnu)
25042 {
25043 section = &cu->dwo_unit->dwo_file->sections.macro;
25044 section_name = ".debug_macro.dwo";
25045 }
25046 else
25047 {
25048 section = &cu->dwo_unit->dwo_file->sections.macinfo;
25049 section_name = ".debug_macinfo.dwo";
25050 }
25051 }
25052 else
25053 {
25054 if (section_is_gnu)
25055 {
25056 section = &dwarf2_per_objfile->macro;
25057 section_name = ".debug_macro";
25058 }
25059 else
25060 {
25061 section = &dwarf2_per_objfile->macinfo;
25062 section_name = ".debug_macinfo";
25063 }
25064 }
cf2c3c16 25065
bb5ed363 25066 dwarf2_read_section (objfile, section);
cf2c3c16
TT
25067 if (section->buffer == NULL)
25068 {
fceca515 25069 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
25070 return;
25071 }
a32a8923 25072 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
25073
25074 /* First pass: Find the name of the base filename.
25075 This filename is needed in order to process all macros whose definition
25076 (or undefinition) comes from the command line. These macros are defined
25077 before the first DW_MACINFO_start_file entry, and yet still need to be
25078 associated to the base file.
25079
25080 To determine the base file name, we scan the macro definitions until we
25081 reach the first DW_MACINFO_start_file entry. We then initialize
25082 CURRENT_FILE accordingly so that any macro definition found before the
25083 first DW_MACINFO_start_file can still be associated to the base file. */
25084
25085 mac_ptr = section->buffer + offset;
25086 mac_end = section->buffer + section->size;
25087
25088 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
25089 &offset_size, section_is_gnu);
25090 if (mac_ptr == NULL)
25091 {
25092 /* We already issued a complaint. */
25093 return;
25094 }
25095
25096 do
25097 {
25098 /* Do we at least have room for a macinfo type byte? */
25099 if (mac_ptr >= mac_end)
25100 {
25101 /* Complaint is printed during the second pass as GDB will probably
25102 stop the first pass earlier upon finding
25103 DW_MACINFO_start_file. */
25104 break;
25105 }
25106
aead7601 25107 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
25108 mac_ptr++;
25109
25110 /* Note that we rely on the fact that the corresponding GNU and
25111 DWARF constants are the same. */
132448f8
SM
25112 DIAGNOSTIC_PUSH
25113 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
25114 switch (macinfo_type)
25115 {
25116 /* A zero macinfo type indicates the end of the macro
25117 information. */
25118 case 0:
25119 break;
25120
0af92d60
JK
25121 case DW_MACRO_define:
25122 case DW_MACRO_undef:
cf2c3c16
TT
25123 /* Only skip the data by MAC_PTR. */
25124 {
25125 unsigned int bytes_read;
25126
25127 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25128 mac_ptr += bytes_read;
25129 read_direct_string (abfd, mac_ptr, &bytes_read);
25130 mac_ptr += bytes_read;
25131 }
25132 break;
25133
0af92d60 25134 case DW_MACRO_start_file:
cf2c3c16
TT
25135 {
25136 unsigned int bytes_read;
25137 int line, file;
25138
25139 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25140 mac_ptr += bytes_read;
25141 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25142 mac_ptr += bytes_read;
25143
43f3e411 25144 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
25145 }
25146 break;
25147
0af92d60 25148 case DW_MACRO_end_file:
cf2c3c16
TT
25149 /* No data to skip by MAC_PTR. */
25150 break;
25151
0af92d60
JK
25152 case DW_MACRO_define_strp:
25153 case DW_MACRO_undef_strp:
25154 case DW_MACRO_define_sup:
25155 case DW_MACRO_undef_sup:
cf2c3c16
TT
25156 {
25157 unsigned int bytes_read;
25158
25159 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25160 mac_ptr += bytes_read;
25161 mac_ptr += offset_size;
25162 }
25163 break;
25164
0af92d60
JK
25165 case DW_MACRO_import:
25166 case DW_MACRO_import_sup:
cf2c3c16 25167 /* Note that, according to the spec, a transparent include
0af92d60 25168 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
25169 skip this opcode. */
25170 mac_ptr += offset_size;
25171 break;
25172
25173 case DW_MACINFO_vendor_ext:
25174 /* Only skip the data by MAC_PTR. */
25175 if (!section_is_gnu)
25176 {
25177 unsigned int bytes_read;
25178
25179 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25180 mac_ptr += bytes_read;
25181 read_direct_string (abfd, mac_ptr, &bytes_read);
25182 mac_ptr += bytes_read;
25183 }
25184 /* FALLTHROUGH */
25185
25186 default:
25187 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 25188 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
25189 section);
25190 if (mac_ptr == NULL)
25191 return;
25192 break;
25193 }
132448f8 25194 DIAGNOSTIC_POP
cf2c3c16
TT
25195 } while (macinfo_type != 0 && current_file == NULL);
25196
25197 /* Second pass: Process all entries.
25198
25199 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25200 command-line macro definitions/undefinitions. This flag is unset when we
25201 reach the first DW_MACINFO_start_file entry. */
25202
fc4007c9
TT
25203 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25204 htab_eq_pointer,
25205 NULL, xcalloc, xfree));
8fc3fc34 25206 mac_ptr = section->buffer + offset;
fc4007c9 25207 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 25208 *slot = (void *) mac_ptr;
ed2dc618
SM
25209 dwarf_decode_macro_bytes (dwarf2_per_objfile,
25210 abfd, mac_ptr, mac_end,
43f3e411 25211 current_file, lh, section,
fc4007c9
TT
25212 section_is_gnu, 0, offset_size,
25213 include_hash.get ());
cf2c3c16
TT
25214}
25215
8e19ed76 25216/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 25217 if so return true else false. */
380bca97 25218
8e19ed76 25219static int
6e5a29e1 25220attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
25221{
25222 return (attr == NULL ? 0 :
25223 attr->form == DW_FORM_block1
25224 || attr->form == DW_FORM_block2
25225 || attr->form == DW_FORM_block4
2dc7f7b3
TT
25226 || attr->form == DW_FORM_block
25227 || attr->form == DW_FORM_exprloc);
8e19ed76 25228}
4c2df51b 25229
c6a0999f
JB
25230/* Return non-zero if ATTR's value is a section offset --- classes
25231 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25232 You may use DW_UNSND (attr) to retrieve such offsets.
25233
25234 Section 7.5.4, "Attribute Encodings", explains that no attribute
25235 may have a value that belongs to more than one of these classes; it
25236 would be ambiguous if we did, because we use the same forms for all
25237 of them. */
380bca97 25238
3690dd37 25239static int
6e5a29e1 25240attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
25241{
25242 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
25243 || attr->form == DW_FORM_data8
25244 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
25245}
25246
3690dd37
JB
25247/* Return non-zero if ATTR's value falls in the 'constant' class, or
25248 zero otherwise. When this function returns true, you can apply
25249 dwarf2_get_attr_constant_value to it.
25250
25251 However, note that for some attributes you must check
25252 attr_form_is_section_offset before using this test. DW_FORM_data4
25253 and DW_FORM_data8 are members of both the constant class, and of
25254 the classes that contain offsets into other debug sections
25255 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25256 that, if an attribute's can be either a constant or one of the
25257 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
25258 taken as section offsets, not constants.
25259
25260 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25261 cannot handle that. */
380bca97 25262
3690dd37 25263static int
6e5a29e1 25264attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
25265{
25266 switch (attr->form)
25267 {
25268 case DW_FORM_sdata:
25269 case DW_FORM_udata:
25270 case DW_FORM_data1:
25271 case DW_FORM_data2:
25272 case DW_FORM_data4:
25273 case DW_FORM_data8:
663c44ac 25274 case DW_FORM_implicit_const:
3690dd37
JB
25275 return 1;
25276 default:
25277 return 0;
25278 }
25279}
25280
7771576e
SA
25281
25282/* DW_ADDR is always stored already as sect_offset; despite for the forms
25283 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25284
25285static int
6e5a29e1 25286attr_form_is_ref (const struct attribute *attr)
7771576e
SA
25287{
25288 switch (attr->form)
25289 {
25290 case DW_FORM_ref_addr:
25291 case DW_FORM_ref1:
25292 case DW_FORM_ref2:
25293 case DW_FORM_ref4:
25294 case DW_FORM_ref8:
25295 case DW_FORM_ref_udata:
25296 case DW_FORM_GNU_ref_alt:
25297 return 1;
25298 default:
25299 return 0;
25300 }
25301}
25302
3019eac3
DE
25303/* Return the .debug_loc section to use for CU.
25304 For DWO files use .debug_loc.dwo. */
25305
25306static struct dwarf2_section_info *
25307cu_debug_loc_section (struct dwarf2_cu *cu)
25308{
518817b3
SM
25309 struct dwarf2_per_objfile *dwarf2_per_objfile
25310 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 25311
3019eac3 25312 if (cu->dwo_unit)
43988095
JK
25313 {
25314 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25315
25316 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25317 }
25318 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25319 : &dwarf2_per_objfile->loc);
3019eac3
DE
25320}
25321
8cf6f0b1
TT
25322/* A helper function that fills in a dwarf2_loclist_baton. */
25323
25324static void
25325fill_in_loclist_baton (struct dwarf2_cu *cu,
25326 struct dwarf2_loclist_baton *baton,
ff39bb5e 25327 const struct attribute *attr)
8cf6f0b1 25328{
518817b3
SM
25329 struct dwarf2_per_objfile *dwarf2_per_objfile
25330 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
25331 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25332
25333 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
25334
25335 baton->per_cu = cu->per_cu;
25336 gdb_assert (baton->per_cu);
25337 /* We don't know how long the location list is, but make sure we
25338 don't run off the edge of the section. */
3019eac3
DE
25339 baton->size = section->size - DW_UNSND (attr);
25340 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 25341 baton->base_address = cu->base_address;
f664829e 25342 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
25343}
25344
4c2df51b 25345static void
ff39bb5e 25346dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 25347 struct dwarf2_cu *cu, int is_block)
4c2df51b 25348{
518817b3
SM
25349 struct dwarf2_per_objfile *dwarf2_per_objfile
25350 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 25351 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 25352 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 25353
3690dd37 25354 if (attr_form_is_section_offset (attr)
3019eac3 25355 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
25356 the section. If so, fall through to the complaint in the
25357 other branch. */
3019eac3 25358 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 25359 {
0d53c4c4 25360 struct dwarf2_loclist_baton *baton;
4c2df51b 25361
8d749320 25362 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 25363
8cf6f0b1 25364 fill_in_loclist_baton (cu, baton, attr);
be391dca 25365
d00adf39 25366 if (cu->base_known == 0)
0d53c4c4 25367 complaint (&symfile_complaints,
3e43a32a
MS
25368 _("Location list used without "
25369 "specifying the CU base address."));
4c2df51b 25370
f1e6e072
TT
25371 SYMBOL_ACLASS_INDEX (sym) = (is_block
25372 ? dwarf2_loclist_block_index
25373 : dwarf2_loclist_index);
0d53c4c4
DJ
25374 SYMBOL_LOCATION_BATON (sym) = baton;
25375 }
25376 else
25377 {
25378 struct dwarf2_locexpr_baton *baton;
25379
8d749320 25380 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
25381 baton->per_cu = cu->per_cu;
25382 gdb_assert (baton->per_cu);
0d53c4c4
DJ
25383
25384 if (attr_form_is_block (attr))
25385 {
25386 /* Note that we're just copying the block's data pointer
25387 here, not the actual data. We're still pointing into the
6502dd73
DJ
25388 info_buffer for SYM's objfile; right now we never release
25389 that buffer, but when we do clean up properly this may
25390 need to change. */
0d53c4c4
DJ
25391 baton->size = DW_BLOCK (attr)->size;
25392 baton->data = DW_BLOCK (attr)->data;
25393 }
25394 else
25395 {
25396 dwarf2_invalid_attrib_class_complaint ("location description",
25397 SYMBOL_NATURAL_NAME (sym));
25398 baton->size = 0;
0d53c4c4 25399 }
6e70227d 25400
f1e6e072
TT
25401 SYMBOL_ACLASS_INDEX (sym) = (is_block
25402 ? dwarf2_locexpr_block_index
25403 : dwarf2_locexpr_index);
0d53c4c4
DJ
25404 SYMBOL_LOCATION_BATON (sym) = baton;
25405 }
4c2df51b 25406}
6502dd73 25407
9aa1f1e3
TT
25408/* Return the OBJFILE associated with the compilation unit CU. If CU
25409 came from a separate debuginfo file, then the master objfile is
25410 returned. */
ae0d2f24
UW
25411
25412struct objfile *
25413dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25414{
e3b94546 25415 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
25416
25417 /* Return the master objfile, so that we can report and look up the
25418 correct file containing this variable. */
25419 if (objfile->separate_debug_objfile_backlink)
25420 objfile = objfile->separate_debug_objfile_backlink;
25421
25422 return objfile;
25423}
25424
96408a79
SA
25425/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25426 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25427 CU_HEADERP first. */
25428
25429static const struct comp_unit_head *
25430per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25431 struct dwarf2_per_cu_data *per_cu)
25432{
d521ce57 25433 const gdb_byte *info_ptr;
96408a79
SA
25434
25435 if (per_cu->cu)
25436 return &per_cu->cu->header;
25437
9c541725 25438 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
25439
25440 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
25441 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25442 rcuh_kind::COMPILE);
96408a79
SA
25443
25444 return cu_headerp;
25445}
25446
ae0d2f24
UW
25447/* Return the address size given in the compilation unit header for CU. */
25448
98714339 25449int
ae0d2f24
UW
25450dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25451{
96408a79
SA
25452 struct comp_unit_head cu_header_local;
25453 const struct comp_unit_head *cu_headerp;
c471e790 25454
96408a79
SA
25455 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25456
25457 return cu_headerp->addr_size;
ae0d2f24
UW
25458}
25459
9eae7c52
TT
25460/* Return the offset size given in the compilation unit header for CU. */
25461
25462int
25463dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25464{
96408a79
SA
25465 struct comp_unit_head cu_header_local;
25466 const struct comp_unit_head *cu_headerp;
9c6c53f7 25467
96408a79
SA
25468 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25469
25470 return cu_headerp->offset_size;
25471}
25472
25473/* See its dwarf2loc.h declaration. */
25474
25475int
25476dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25477{
25478 struct comp_unit_head cu_header_local;
25479 const struct comp_unit_head *cu_headerp;
25480
25481 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25482
25483 if (cu_headerp->version == 2)
25484 return cu_headerp->addr_size;
25485 else
25486 return cu_headerp->offset_size;
181cebd4
JK
25487}
25488
9aa1f1e3
TT
25489/* Return the text offset of the CU. The returned offset comes from
25490 this CU's objfile. If this objfile came from a separate debuginfo
25491 file, then the offset may be different from the corresponding
25492 offset in the parent objfile. */
25493
25494CORE_ADDR
25495dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25496{
e3b94546 25497 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9aa1f1e3
TT
25498
25499 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25500}
25501
43988095
JK
25502/* Return DWARF version number of PER_CU. */
25503
25504short
25505dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25506{
25507 return per_cu->dwarf_version;
25508}
25509
348e048f
DE
25510/* Locate the .debug_info compilation unit from CU's objfile which contains
25511 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
25512
25513static struct dwarf2_per_cu_data *
9c541725 25514dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 25515 unsigned int offset_in_dwz,
ed2dc618 25516 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25517{
25518 struct dwarf2_per_cu_data *this_cu;
25519 int low, high;
36586728 25520 const sect_offset *cu_off;
ae038cb0 25521
ae038cb0
DJ
25522 low = 0;
25523 high = dwarf2_per_objfile->n_comp_units - 1;
25524 while (high > low)
25525 {
36586728 25526 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 25527 int mid = low + (high - low) / 2;
9a619af0 25528
36586728 25529 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
9c541725 25530 cu_off = &mid_cu->sect_off;
36586728 25531 if (mid_cu->is_dwz > offset_in_dwz
9c541725 25532 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
ae038cb0
DJ
25533 high = mid;
25534 else
25535 low = mid + 1;
25536 }
25537 gdb_assert (low == high);
36586728 25538 this_cu = dwarf2_per_objfile->all_comp_units[low];
9c541725
PA
25539 cu_off = &this_cu->sect_off;
25540 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
ae038cb0 25541 {
36586728 25542 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 25543 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
25544 "offset %s [in module %s]"),
25545 sect_offset_str (sect_off),
ed2dc618 25546 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 25547
9c541725
PA
25548 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25549 <= sect_off);
ae038cb0
DJ
25550 return dwarf2_per_objfile->all_comp_units[low-1];
25551 }
25552 else
25553 {
25554 this_cu = dwarf2_per_objfile->all_comp_units[low];
25555 if (low == dwarf2_per_objfile->n_comp_units - 1
9c541725 25556 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 25557 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 25558 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
25559 return this_cu;
25560 }
25561}
25562
23745b47 25563/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 25564
fcd3b13d
SM
25565dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25566 : per_cu (per_cu_),
25567 mark (0),
25568 has_loclist (0),
25569 checked_producer (0),
25570 producer_is_gxx_lt_4_6 (0),
25571 producer_is_gcc_lt_4_3 (0),
25572 producer_is_icc_lt_14 (0),
25573 processing_has_namespace_info (0)
93311388 25574{
fcd3b13d
SM
25575 per_cu->cu = this;
25576}
25577
25578/* Destroy a dwarf2_cu. */
25579
25580dwarf2_cu::~dwarf2_cu ()
25581{
25582 per_cu->cu = NULL;
9816fde3
JK
25583}
25584
25585/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25586
25587static void
95554aad
TT
25588prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25589 enum language pretend_language)
9816fde3
JK
25590{
25591 struct attribute *attr;
25592
25593 /* Set the language we're debugging. */
25594 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25595 if (attr)
25596 set_cu_language (DW_UNSND (attr), cu);
25597 else
9cded63f 25598 {
95554aad 25599 cu->language = pretend_language;
9cded63f
TT
25600 cu->language_defn = language_def (cu->language);
25601 }
dee91e82 25602
7d45c7c3 25603 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25604}
25605
ae038cb0
DJ
25606/* Free all cached compilation units. */
25607
25608static void
25609free_cached_comp_units (void *data)
25610{
ed2dc618
SM
25611 struct dwarf2_per_objfile *dwarf2_per_objfile
25612 = (struct dwarf2_per_objfile *) data;
25613
330cdd98 25614 dwarf2_per_objfile->free_cached_comp_units ();
ae038cb0
DJ
25615}
25616
25617/* Increase the age counter on each cached compilation unit, and free
25618 any that are too old. */
25619
25620static void
ed2dc618 25621age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25622{
25623 struct dwarf2_per_cu_data *per_cu, **last_chain;
25624
25625 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25626 per_cu = dwarf2_per_objfile->read_in_chain;
25627 while (per_cu != NULL)
25628 {
25629 per_cu->cu->last_used ++;
b4f54984 25630 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
25631 dwarf2_mark (per_cu->cu);
25632 per_cu = per_cu->cu->read_in_chain;
25633 }
25634
25635 per_cu = dwarf2_per_objfile->read_in_chain;
25636 last_chain = &dwarf2_per_objfile->read_in_chain;
25637 while (per_cu != NULL)
25638 {
25639 struct dwarf2_per_cu_data *next_cu;
25640
25641 next_cu = per_cu->cu->read_in_chain;
25642
25643 if (!per_cu->cu->mark)
25644 {
fcd3b13d 25645 delete per_cu->cu;
ae038cb0
DJ
25646 *last_chain = next_cu;
25647 }
25648 else
25649 last_chain = &per_cu->cu->read_in_chain;
25650
25651 per_cu = next_cu;
25652 }
25653}
25654
25655/* Remove a single compilation unit from the cache. */
25656
25657static void
dee91e82 25658free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25659{
25660 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
25661 struct dwarf2_per_objfile *dwarf2_per_objfile
25662 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
25663
25664 per_cu = dwarf2_per_objfile->read_in_chain;
25665 last_chain = &dwarf2_per_objfile->read_in_chain;
25666 while (per_cu != NULL)
25667 {
25668 struct dwarf2_per_cu_data *next_cu;
25669
25670 next_cu = per_cu->cu->read_in_chain;
25671
dee91e82 25672 if (per_cu == target_per_cu)
ae038cb0 25673 {
fcd3b13d 25674 delete per_cu->cu;
dee91e82 25675 per_cu->cu = NULL;
ae038cb0
DJ
25676 *last_chain = next_cu;
25677 break;
25678 }
25679 else
25680 last_chain = &per_cu->cu->read_in_chain;
25681
25682 per_cu = next_cu;
25683 }
25684}
25685
fe3e1990
DJ
25686/* Release all extra memory associated with OBJFILE. */
25687
25688void
25689dwarf2_free_objfile (struct objfile *objfile)
25690{
ed2dc618
SM
25691 struct dwarf2_per_objfile *dwarf2_per_objfile
25692 = get_dwarf2_per_objfile (objfile);
fe3e1990 25693
fd90ace4 25694 delete dwarf2_per_objfile;
fe3e1990
DJ
25695}
25696
dee91e82
DE
25697/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25698 We store these in a hash table separate from the DIEs, and preserve them
25699 when the DIEs are flushed out of cache.
25700
25701 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25702 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25703 or the type may come from a DWO file. Furthermore, while it's more logical
25704 to use per_cu->section+offset, with Fission the section with the data is in
25705 the DWO file but we don't know that section at the point we need it.
25706 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25707 because we can enter the lookup routine, get_die_type_at_offset, from
25708 outside this file, and thus won't necessarily have PER_CU->cu.
25709 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25710
dee91e82 25711struct dwarf2_per_cu_offset_and_type
1c379e20 25712{
dee91e82 25713 const struct dwarf2_per_cu_data *per_cu;
9c541725 25714 sect_offset sect_off;
1c379e20
DJ
25715 struct type *type;
25716};
25717
dee91e82 25718/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25719
25720static hashval_t
dee91e82 25721per_cu_offset_and_type_hash (const void *item)
1c379e20 25722{
9a3c8263
SM
25723 const struct dwarf2_per_cu_offset_and_type *ofs
25724 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25725
9c541725 25726 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25727}
25728
dee91e82 25729/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25730
25731static int
dee91e82 25732per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25733{
9a3c8263
SM
25734 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25735 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25736 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25737 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25738
dee91e82 25739 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25740 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25741}
25742
25743/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25744 table if necessary. For convenience, return TYPE.
25745
25746 The DIEs reading must have careful ordering to:
25747 * Not cause infite loops trying to read in DIEs as a prerequisite for
25748 reading current DIE.
25749 * Not trying to dereference contents of still incompletely read in types
25750 while reading in other DIEs.
25751 * Enable referencing still incompletely read in types just by a pointer to
25752 the type without accessing its fields.
25753
25754 Therefore caller should follow these rules:
25755 * Try to fetch any prerequisite types we may need to build this DIE type
25756 before building the type and calling set_die_type.
e71ec853 25757 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25758 possible before fetching more types to complete the current type.
25759 * Make the type as complete as possible before fetching more types. */
1c379e20 25760
f792889a 25761static struct type *
1c379e20
DJ
25762set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25763{
518817b3
SM
25764 struct dwarf2_per_objfile *dwarf2_per_objfile
25765 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25766 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25767 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25768 struct attribute *attr;
25769 struct dynamic_prop prop;
1c379e20 25770
b4ba55a1
JB
25771 /* For Ada types, make sure that the gnat-specific data is always
25772 initialized (if not already set). There are a few types where
25773 we should not be doing so, because the type-specific area is
25774 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25775 where the type-specific area is used to store the floatformat).
25776 But this is not a problem, because the gnat-specific information
25777 is actually not needed for these types. */
25778 if (need_gnat_info (cu)
25779 && TYPE_CODE (type) != TYPE_CODE_FUNC
25780 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25781 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25782 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25783 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25784 && !HAVE_GNAT_AUX_INFO (type))
25785 INIT_GNAT_SPECIFIC (type);
25786
3f2f83dd
KB
25787 /* Read DW_AT_allocated and set in type. */
25788 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25789 if (attr_form_is_block (attr))
25790 {
25791 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25792 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25793 }
25794 else if (attr != NULL)
25795 {
25796 complaint (&symfile_complaints,
9d8780f0 25797 _("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 25798 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25799 sect_offset_str (die->sect_off));
3f2f83dd
KB
25800 }
25801
25802 /* Read DW_AT_associated and set in type. */
25803 attr = dwarf2_attr (die, DW_AT_associated, cu);
25804 if (attr_form_is_block (attr))
25805 {
25806 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25807 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25808 }
25809 else if (attr != NULL)
25810 {
25811 complaint (&symfile_complaints,
9d8780f0 25812 _("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 25813 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25814 sect_offset_str (die->sect_off));
3f2f83dd
KB
25815 }
25816
3cdcd0ce
JB
25817 /* Read DW_AT_data_location and set in type. */
25818 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25819 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25820 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25821
dee91e82 25822 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25823 {
dee91e82
DE
25824 dwarf2_per_objfile->die_type_hash =
25825 htab_create_alloc_ex (127,
25826 per_cu_offset_and_type_hash,
25827 per_cu_offset_and_type_eq,
25828 NULL,
25829 &objfile->objfile_obstack,
25830 hashtab_obstack_allocate,
25831 dummy_obstack_deallocate);
f792889a 25832 }
1c379e20 25833
dee91e82 25834 ofs.per_cu = cu->per_cu;
9c541725 25835 ofs.sect_off = die->sect_off;
1c379e20 25836 ofs.type = type;
dee91e82
DE
25837 slot = (struct dwarf2_per_cu_offset_and_type **)
25838 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
25839 if (*slot)
25840 complaint (&symfile_complaints,
9d8780f0
SM
25841 _("A problem internal to GDB: DIE %s has type already set"),
25842 sect_offset_str (die->sect_off));
8d749320
SM
25843 *slot = XOBNEW (&objfile->objfile_obstack,
25844 struct dwarf2_per_cu_offset_and_type);
1c379e20 25845 **slot = ofs;
f792889a 25846 return type;
1c379e20
DJ
25847}
25848
9c541725 25849/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25850 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25851
25852static struct type *
9c541725 25853get_die_type_at_offset (sect_offset sect_off,
673bfd45 25854 struct dwarf2_per_cu_data *per_cu)
1c379e20 25855{
dee91e82 25856 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 25857 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 25858
dee91e82 25859 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25860 return NULL;
1c379e20 25861
dee91e82 25862 ofs.per_cu = per_cu;
9c541725 25863 ofs.sect_off = sect_off;
9a3c8263
SM
25864 slot = ((struct dwarf2_per_cu_offset_and_type *)
25865 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25866 if (slot)
25867 return slot->type;
25868 else
25869 return NULL;
25870}
25871
02142a6c 25872/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25873 or return NULL if DIE does not have a saved type. */
25874
25875static struct type *
25876get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25877{
9c541725 25878 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
25879}
25880
10b3939b
DJ
25881/* Add a dependence relationship from CU to REF_PER_CU. */
25882
25883static void
25884dwarf2_add_dependence (struct dwarf2_cu *cu,
25885 struct dwarf2_per_cu_data *ref_per_cu)
25886{
25887 void **slot;
25888
25889 if (cu->dependencies == NULL)
25890 cu->dependencies
25891 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25892 NULL, &cu->comp_unit_obstack,
25893 hashtab_obstack_allocate,
25894 dummy_obstack_deallocate);
25895
25896 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25897 if (*slot == NULL)
25898 *slot = ref_per_cu;
25899}
1c379e20 25900
f504f079
DE
25901/* Subroutine of dwarf2_mark to pass to htab_traverse.
25902 Set the mark field in every compilation unit in the
ae038cb0
DJ
25903 cache that we must keep because we are keeping CU. */
25904
10b3939b
DJ
25905static int
25906dwarf2_mark_helper (void **slot, void *data)
25907{
25908 struct dwarf2_per_cu_data *per_cu;
25909
25910 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
25911
25912 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25913 reading of the chain. As such dependencies remain valid it is not much
25914 useful to track and undo them during QUIT cleanups. */
25915 if (per_cu->cu == NULL)
25916 return 1;
25917
10b3939b
DJ
25918 if (per_cu->cu->mark)
25919 return 1;
25920 per_cu->cu->mark = 1;
25921
25922 if (per_cu->cu->dependencies != NULL)
25923 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25924
25925 return 1;
25926}
25927
f504f079
DE
25928/* Set the mark field in CU and in every other compilation unit in the
25929 cache that we must keep because we are keeping CU. */
25930
ae038cb0
DJ
25931static void
25932dwarf2_mark (struct dwarf2_cu *cu)
25933{
25934 if (cu->mark)
25935 return;
25936 cu->mark = 1;
10b3939b
DJ
25937 if (cu->dependencies != NULL)
25938 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
25939}
25940
25941static void
25942dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25943{
25944 while (per_cu)
25945 {
25946 per_cu->cu->mark = 0;
25947 per_cu = per_cu->cu->read_in_chain;
25948 }
72bf9492
DJ
25949}
25950
72bf9492
DJ
25951/* Trivial hash function for partial_die_info: the hash value of a DIE
25952 is its offset in .debug_info for this objfile. */
25953
25954static hashval_t
25955partial_die_hash (const void *item)
25956{
9a3c8263
SM
25957 const struct partial_die_info *part_die
25958 = (const struct partial_die_info *) item;
9a619af0 25959
9c541725 25960 return to_underlying (part_die->sect_off);
72bf9492
DJ
25961}
25962
25963/* Trivial comparison function for partial_die_info structures: two DIEs
25964 are equal if they have the same offset. */
25965
25966static int
25967partial_die_eq (const void *item_lhs, const void *item_rhs)
25968{
9a3c8263
SM
25969 const struct partial_die_info *part_die_lhs
25970 = (const struct partial_die_info *) item_lhs;
25971 const struct partial_die_info *part_die_rhs
25972 = (const struct partial_die_info *) item_rhs;
9a619af0 25973
9c541725 25974 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25975}
25976
b4f54984
DE
25977static struct cmd_list_element *set_dwarf_cmdlist;
25978static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
25979
25980static void
981a3fb3 25981set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 25982{
b4f54984 25983 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 25984 gdb_stdout);
ae038cb0
DJ
25985}
25986
25987static void
981a3fb3 25988show_dwarf_cmd (const char *args, int from_tty)
6e70227d 25989{
b4f54984 25990 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
25991}
25992
ae2de4f8 25993/* The "save gdb-index" command. */
9291a0cd 25994
437afbb8
JK
25995/* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
25996 error checking. */
25997
25998static void
25999file_write (FILE *file, const void *data, size_t size)
26000{
26001 if (fwrite (data, 1, size, file) != size)
26002 error (_("couldn't data write to file"));
26003}
26004
26005/* Write the contents of VEC to FILE, with error checking. */
26006
26007template<typename Elem, typename Alloc>
26008static void
26009file_write (FILE *file, const std::vector<Elem, Alloc> &vec)
26010{
26011 file_write (file, vec.data (), vec.size () * sizeof (vec[0]));
26012}
26013
bc8f2430
JK
26014/* In-memory buffer to prepare data to be written later to a file. */
26015class data_buf
9291a0cd 26016{
bc8f2430 26017public:
bc8f2430
JK
26018 /* Copy DATA to the end of the buffer. */
26019 template<typename T>
26020 void append_data (const T &data)
26021 {
26022 std::copy (reinterpret_cast<const gdb_byte *> (&data),
26023 reinterpret_cast<const gdb_byte *> (&data + 1),
c2f134ac 26024 grow (sizeof (data)));
bc8f2430 26025 }
b89be57b 26026
c2f134ac
PA
26027 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
26028 terminating zero is appended too. */
bc8f2430
JK
26029 void append_cstr0 (const char *cstr)
26030 {
26031 const size_t size = strlen (cstr) + 1;
c2f134ac
PA
26032 std::copy (cstr, cstr + size, grow (size));
26033 }
26034
437afbb8
JK
26035 /* Store INPUT as ULEB128 to the end of buffer. */
26036 void append_unsigned_leb128 (ULONGEST input)
26037 {
26038 for (;;)
26039 {
26040 gdb_byte output = input & 0x7f;
26041 input >>= 7;
26042 if (input)
26043 output |= 0x80;
26044 append_data (output);
26045 if (input == 0)
26046 break;
26047 }
26048 }
26049
c2f134ac
PA
26050 /* Accept a host-format integer in VAL and append it to the buffer
26051 as a target-format integer which is LEN bytes long. */
26052 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
26053 {
26054 ::store_unsigned_integer (grow (len), len, byte_order, val);
bc8f2430 26055 }
9291a0cd 26056
bc8f2430
JK
26057 /* Return the size of the buffer. */
26058 size_t size () const
26059 {
26060 return m_vec.size ();
26061 }
26062
437afbb8
JK
26063 /* Return true iff the buffer is empty. */
26064 bool empty () const
26065 {
26066 return m_vec.empty ();
26067 }
26068
bc8f2430
JK
26069 /* Write the buffer to FILE. */
26070 void file_write (FILE *file) const
26071 {
437afbb8 26072 ::file_write (file, m_vec);
bc8f2430
JK
26073 }
26074
26075private:
c2f134ac
PA
26076 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
26077 the start of the new block. */
26078 gdb_byte *grow (size_t size)
26079 {
26080 m_vec.resize (m_vec.size () + size);
26081 return &*m_vec.end () - size;
26082 }
26083
d5722aa2 26084 gdb::byte_vector m_vec;
bc8f2430 26085};
9291a0cd
TT
26086
26087/* An entry in the symbol table. */
26088struct symtab_index_entry
26089{
26090 /* The name of the symbol. */
26091 const char *name;
26092 /* The offset of the name in the constant pool. */
26093 offset_type index_offset;
26094 /* A sorted vector of the indices of all the CUs that hold an object
26095 of this name. */
bc8f2430 26096 std::vector<offset_type> cu_indices;
9291a0cd
TT
26097};
26098
26099/* The symbol table. This is a power-of-2-sized hash table. */
26100struct mapped_symtab
26101{
bc8f2430
JK
26102 mapped_symtab ()
26103 {
26104 data.resize (1024);
26105 }
b89be57b 26106
bc8f2430 26107 offset_type n_elements = 0;
4b76cda9 26108 std::vector<symtab_index_entry> data;
bc8f2430 26109};
9291a0cd 26110
bc8f2430 26111/* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
559a7a62
JK
26112 the slot.
26113
26114 Function is used only during write_hash_table so no index format backward
26115 compatibility is needed. */
b89be57b 26116
4b76cda9 26117static symtab_index_entry &
9291a0cd
TT
26118find_slot (struct mapped_symtab *symtab, const char *name)
26119{
559a7a62 26120 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd 26121
bc8f2430
JK
26122 index = hash & (symtab->data.size () - 1);
26123 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
9291a0cd
TT
26124
26125 for (;;)
26126 {
4b76cda9
PA
26127 if (symtab->data[index].name == NULL
26128 || strcmp (name, symtab->data[index].name) == 0)
bc8f2430
JK
26129 return symtab->data[index];
26130 index = (index + step) & (symtab->data.size () - 1);
9291a0cd
TT
26131 }
26132}
26133
26134/* Expand SYMTAB's hash table. */
b89be57b 26135
9291a0cd
TT
26136static void
26137hash_expand (struct mapped_symtab *symtab)
26138{
bc8f2430 26139 auto old_entries = std::move (symtab->data);
9291a0cd 26140
bc8f2430
JK
26141 symtab->data.clear ();
26142 symtab->data.resize (old_entries.size () * 2);
9291a0cd 26143
bc8f2430 26144 for (auto &it : old_entries)
4b76cda9 26145 if (it.name != NULL)
bc8f2430 26146 {
4b76cda9 26147 auto &ref = find_slot (symtab, it.name);
bc8f2430
JK
26148 ref = std::move (it);
26149 }
9291a0cd
TT
26150}
26151
156942c7
DE
26152/* Add an entry to SYMTAB. NAME is the name of the symbol.
26153 CU_INDEX is the index of the CU in which the symbol appears.
26154 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 26155
9291a0cd
TT
26156static void
26157add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 26158 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
26159 offset_type cu_index)
26160{
156942c7 26161 offset_type cu_index_and_attrs;
9291a0cd
TT
26162
26163 ++symtab->n_elements;
bc8f2430 26164 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
9291a0cd
TT
26165 hash_expand (symtab);
26166
4b76cda9
PA
26167 symtab_index_entry &slot = find_slot (symtab, name);
26168 if (slot.name == NULL)
9291a0cd 26169 {
4b76cda9 26170 slot.name = name;
156942c7 26171 /* index_offset is set later. */
9291a0cd 26172 }
156942c7
DE
26173
26174 cu_index_and_attrs = 0;
26175 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
26176 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
26177 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
26178
26179 /* We don't want to record an index value twice as we want to avoid the
26180 duplication.
26181 We process all global symbols and then all static symbols
26182 (which would allow us to avoid the duplication by only having to check
26183 the last entry pushed), but a symbol could have multiple kinds in one CU.
26184 To keep things simple we don't worry about the duplication here and
26185 sort and uniqufy the list after we've processed all symbols. */
4b76cda9 26186 slot.cu_indices.push_back (cu_index_and_attrs);
156942c7
DE
26187}
26188
26189/* Sort and remove duplicates of all symbols' cu_indices lists. */
26190
26191static void
26192uniquify_cu_indices (struct mapped_symtab *symtab)
26193{
4b76cda9 26194 for (auto &entry : symtab->data)
156942c7 26195 {
4b76cda9 26196 if (entry.name != NULL && !entry.cu_indices.empty ())
156942c7 26197 {
4b76cda9 26198 auto &cu_indices = entry.cu_indices;
6fd931f2
PA
26199 std::sort (cu_indices.begin (), cu_indices.end ());
26200 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
26201 cu_indices.erase (from, cu_indices.end ());
156942c7
DE
26202 }
26203 }
9291a0cd
TT
26204}
26205
bc8f2430
JK
26206/* A form of 'const char *' suitable for container keys. Only the
26207 pointer is stored. The strings themselves are compared, not the
26208 pointers. */
26209class c_str_view
9291a0cd 26210{
bc8f2430
JK
26211public:
26212 c_str_view (const char *cstr)
26213 : m_cstr (cstr)
26214 {}
9291a0cd 26215
bc8f2430
JK
26216 bool operator== (const c_str_view &other) const
26217 {
26218 return strcmp (m_cstr, other.m_cstr) == 0;
26219 }
9291a0cd 26220
437afbb8
JK
26221 /* Return the underlying C string. Note, the returned string is
26222 only a reference with lifetime of this object. */
26223 const char *c_str () const
26224 {
26225 return m_cstr;
26226 }
26227
bc8f2430
JK
26228private:
26229 friend class c_str_view_hasher;
26230 const char *const m_cstr;
26231};
9291a0cd 26232
bc8f2430
JK
26233/* A std::unordered_map::hasher for c_str_view that uses the right
26234 hash function for strings in a mapped index. */
26235class c_str_view_hasher
26236{
26237public:
26238 size_t operator () (const c_str_view &x) const
26239 {
26240 return mapped_index_string_hash (INT_MAX, x.m_cstr);
26241 }
26242};
b89be57b 26243
bc8f2430
JK
26244/* A std::unordered_map::hasher for std::vector<>. */
26245template<typename T>
26246class vector_hasher
9291a0cd 26247{
bc8f2430
JK
26248public:
26249 size_t operator () (const std::vector<T> &key) const
26250 {
26251 return iterative_hash (key.data (),
26252 sizeof (key.front ()) * key.size (), 0);
26253 }
26254};
9291a0cd 26255
bc8f2430
JK
26256/* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
26257 constant pool entries going into the data buffer CPOOL. */
3876f04e 26258
bc8f2430
JK
26259static void
26260write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
26261{
26262 {
26263 /* Elements are sorted vectors of the indices of all the CUs that
26264 hold an object of this name. */
26265 std::unordered_map<std::vector<offset_type>, offset_type,
26266 vector_hasher<offset_type>>
26267 symbol_hash_table;
26268
26269 /* We add all the index vectors to the constant pool first, to
26270 ensure alignment is ok. */
4b76cda9 26271 for (symtab_index_entry &entry : symtab->data)
bc8f2430 26272 {
4b76cda9 26273 if (entry.name == NULL)
bc8f2430 26274 continue;
4b76cda9 26275 gdb_assert (entry.index_offset == 0);
70a1152b
PA
26276
26277 /* Finding before inserting is faster than always trying to
26278 insert, because inserting always allocates a node, does the
26279 lookup, and then destroys the new node if another node
26280 already had the same key. C++17 try_emplace will avoid
26281 this. */
26282 const auto found
4b76cda9 26283 = symbol_hash_table.find (entry.cu_indices);
70a1152b
PA
26284 if (found != symbol_hash_table.end ())
26285 {
4b76cda9 26286 entry.index_offset = found->second;
70a1152b
PA
26287 continue;
26288 }
26289
4b76cda9
PA
26290 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
26291 entry.index_offset = cpool.size ();
26292 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
26293 for (const auto index : entry.cu_indices)
26294 cpool.append_data (MAYBE_SWAP (index));
bc8f2430
JK
26295 }
26296 }
9291a0cd
TT
26297
26298 /* Now write out the hash table. */
bc8f2430 26299 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
4b76cda9 26300 for (const auto &entry : symtab->data)
9291a0cd
TT
26301 {
26302 offset_type str_off, vec_off;
26303
4b76cda9 26304 if (entry.name != NULL)
9291a0cd 26305 {
4b76cda9 26306 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
bc8f2430 26307 if (insertpair.second)
4b76cda9 26308 cpool.append_cstr0 (entry.name);
bc8f2430 26309 str_off = insertpair.first->second;
4b76cda9 26310 vec_off = entry.index_offset;
9291a0cd
TT
26311 }
26312 else
26313 {
26314 /* While 0 is a valid constant pool index, it is not valid
26315 to have 0 for both offsets. */
26316 str_off = 0;
26317 vec_off = 0;
26318 }
26319
bc8f2430
JK
26320 output.append_data (MAYBE_SWAP (str_off));
26321 output.append_data (MAYBE_SWAP (vec_off));
9291a0cd 26322 }
9291a0cd
TT
26323}
26324
bc8f2430 26325typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
0a5429f6
DE
26326
26327/* Helper struct for building the address table. */
26328struct addrmap_index_data
26329{
bc8f2430
JK
26330 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
26331 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
26332 {}
26333
0a5429f6 26334 struct objfile *objfile;
bc8f2430
JK
26335 data_buf &addr_vec;
26336 psym_index_map &cu_index_htab;
0a5429f6
DE
26337
26338 /* Non-zero if the previous_* fields are valid.
26339 We can't write an entry until we see the next entry (since it is only then
26340 that we know the end of the entry). */
26341 int previous_valid;
26342 /* Index of the CU in the table of all CUs in the index file. */
26343 unsigned int previous_cu_index;
0963b4bd 26344 /* Start address of the CU. */
0a5429f6
DE
26345 CORE_ADDR previous_cu_start;
26346};
26347
bc8f2430 26348/* Write an address entry to ADDR_VEC. */
b89be57b 26349
9291a0cd 26350static void
bc8f2430 26351add_address_entry (struct objfile *objfile, data_buf &addr_vec,
0a5429f6 26352 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 26353{
9291a0cd
TT
26354 CORE_ADDR baseaddr;
26355
26356 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
26357
c2f134ac
PA
26358 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
26359 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
bc8f2430 26360 addr_vec.append_data (MAYBE_SWAP (cu_index));
0a5429f6
DE
26361}
26362
26363/* Worker function for traversing an addrmap to build the address table. */
26364
26365static int
26366add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
26367{
9a3c8263
SM
26368 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
26369 struct partial_symtab *pst = (struct partial_symtab *) obj;
0a5429f6
DE
26370
26371 if (data->previous_valid)
bc8f2430 26372 add_address_entry (data->objfile, data->addr_vec,
0a5429f6
DE
26373 data->previous_cu_start, start_addr,
26374 data->previous_cu_index);
26375
26376 data->previous_cu_start = start_addr;
26377 if (pst != NULL)
26378 {
bc8f2430
JK
26379 const auto it = data->cu_index_htab.find (pst);
26380 gdb_assert (it != data->cu_index_htab.cend ());
26381 data->previous_cu_index = it->second;
0a5429f6
DE
26382 data->previous_valid = 1;
26383 }
26384 else
bc8f2430 26385 data->previous_valid = 0;
0a5429f6
DE
26386
26387 return 0;
26388}
26389
bc8f2430 26390/* Write OBJFILE's address map to ADDR_VEC.
0a5429f6
DE
26391 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
26392 in the index file. */
26393
26394static void
bc8f2430
JK
26395write_address_map (struct objfile *objfile, data_buf &addr_vec,
26396 psym_index_map &cu_index_htab)
0a5429f6 26397{
bc8f2430 26398 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
0a5429f6
DE
26399
26400 /* When writing the address table, we have to cope with the fact that
26401 the addrmap iterator only provides the start of a region; we have to
26402 wait until the next invocation to get the start of the next region. */
26403
26404 addrmap_index_data.objfile = objfile;
0a5429f6
DE
26405 addrmap_index_data.previous_valid = 0;
26406
26407 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
26408 &addrmap_index_data);
26409
26410 /* It's highly unlikely the last entry (end address = 0xff...ff)
26411 is valid, but we should still handle it.
26412 The end address is recorded as the start of the next region, but that
26413 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
26414 anyway. */
26415 if (addrmap_index_data.previous_valid)
bc8f2430 26416 add_address_entry (objfile, addr_vec,
0a5429f6
DE
26417 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
26418 addrmap_index_data.previous_cu_index);
9291a0cd
TT
26419}
26420
156942c7
DE
26421/* Return the symbol kind of PSYM. */
26422
26423static gdb_index_symbol_kind
26424symbol_kind (struct partial_symbol *psym)
26425{
26426 domain_enum domain = PSYMBOL_DOMAIN (psym);
26427 enum address_class aclass = PSYMBOL_CLASS (psym);
26428
26429 switch (domain)
26430 {
26431 case VAR_DOMAIN:
26432 switch (aclass)
26433 {
26434 case LOC_BLOCK:
26435 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
26436 case LOC_TYPEDEF:
26437 return GDB_INDEX_SYMBOL_KIND_TYPE;
26438 case LOC_COMPUTED:
26439 case LOC_CONST_BYTES:
26440 case LOC_OPTIMIZED_OUT:
26441 case LOC_STATIC:
26442 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
26443 case LOC_CONST:
26444 /* Note: It's currently impossible to recognize psyms as enum values
26445 short of reading the type info. For now punt. */
26446 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
26447 default:
26448 /* There are other LOC_FOO values that one might want to classify
26449 as variables, but dwarf2read.c doesn't currently use them. */
26450 return GDB_INDEX_SYMBOL_KIND_OTHER;
26451 }
26452 case STRUCT_DOMAIN:
26453 return GDB_INDEX_SYMBOL_KIND_TYPE;
26454 default:
26455 return GDB_INDEX_SYMBOL_KIND_OTHER;
26456 }
26457}
26458
9291a0cd 26459/* Add a list of partial symbols to SYMTAB. */
b89be57b 26460
9291a0cd
TT
26461static void
26462write_psymbols (struct mapped_symtab *symtab,
bc8f2430 26463 std::unordered_set<partial_symbol *> &psyms_seen,
9291a0cd
TT
26464 struct partial_symbol **psymp,
26465 int count,
987d643c
TT
26466 offset_type cu_index,
26467 int is_static)
9291a0cd
TT
26468{
26469 for (; count-- > 0; ++psymp)
26470 {
156942c7 26471 struct partial_symbol *psym = *psymp;
987d643c 26472
156942c7 26473 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 26474 error (_("Ada is not currently supported by the index"));
987d643c 26475
987d643c 26476 /* Only add a given psymbol once. */
bc8f2430 26477 if (psyms_seen.insert (psym).second)
987d643c 26478 {
156942c7
DE
26479 gdb_index_symbol_kind kind = symbol_kind (psym);
26480
156942c7
DE
26481 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
26482 is_static, kind, cu_index);
987d643c 26483 }
9291a0cd
TT
26484 }
26485}
26486
1fd400ff
TT
26487/* A helper struct used when iterating over debug_types. */
26488struct signatured_type_index_data
26489{
bc8f2430
JK
26490 signatured_type_index_data (data_buf &types_list_,
26491 std::unordered_set<partial_symbol *> &psyms_seen_)
26492 : types_list (types_list_), psyms_seen (psyms_seen_)
26493 {}
26494
1fd400ff
TT
26495 struct objfile *objfile;
26496 struct mapped_symtab *symtab;
bc8f2430
JK
26497 data_buf &types_list;
26498 std::unordered_set<partial_symbol *> &psyms_seen;
1fd400ff
TT
26499 int cu_index;
26500};
26501
26502/* A helper function that writes a single signatured_type to an
26503 obstack. */
b89be57b 26504
1fd400ff
TT
26505static int
26506write_one_signatured_type (void **slot, void *d)
26507{
9a3c8263
SM
26508 struct signatured_type_index_data *info
26509 = (struct signatured_type_index_data *) d;
1fd400ff 26510 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 26511 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
26512
26513 write_psymbols (info->symtab,
987d643c 26514 info->psyms_seen,
af5bf4ad 26515 &info->objfile->global_psymbols[psymtab->globals_offset],
987d643c
TT
26516 psymtab->n_global_syms, info->cu_index,
26517 0);
1fd400ff 26518 write_psymbols (info->symtab,
987d643c 26519 info->psyms_seen,
af5bf4ad 26520 &info->objfile->static_psymbols[psymtab->statics_offset],
987d643c
TT
26521 psymtab->n_static_syms, info->cu_index,
26522 1);
1fd400ff 26523
c2f134ac
PA
26524 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26525 to_underlying (entry->per_cu.sect_off));
26526 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26527 to_underlying (entry->type_offset_in_tu));
26528 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
1fd400ff
TT
26529
26530 ++info->cu_index;
26531
26532 return 1;
26533}
26534
e8f8bcb3
PA
26535/* Recurse into all "included" dependencies and count their symbols as
26536 if they appeared in this psymtab. */
26537
26538static void
26539recursively_count_psymbols (struct partial_symtab *psymtab,
26540 size_t &psyms_seen)
26541{
26542 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26543 if (psymtab->dependencies[i]->user != NULL)
26544 recursively_count_psymbols (psymtab->dependencies[i],
26545 psyms_seen);
26546
26547 psyms_seen += psymtab->n_global_syms;
26548 psyms_seen += psymtab->n_static_syms;
26549}
26550
95554aad
TT
26551/* Recurse into all "included" dependencies and write their symbols as
26552 if they appeared in this psymtab. */
26553
26554static void
26555recursively_write_psymbols (struct objfile *objfile,
26556 struct partial_symtab *psymtab,
26557 struct mapped_symtab *symtab,
bc8f2430 26558 std::unordered_set<partial_symbol *> &psyms_seen,
95554aad
TT
26559 offset_type cu_index)
26560{
26561 int i;
26562
26563 for (i = 0; i < psymtab->number_of_dependencies; ++i)
26564 if (psymtab->dependencies[i]->user != NULL)
26565 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26566 symtab, psyms_seen, cu_index);
26567
26568 write_psymbols (symtab,
26569 psyms_seen,
af5bf4ad 26570 &objfile->global_psymbols[psymtab->globals_offset],
95554aad
TT
26571 psymtab->n_global_syms, cu_index,
26572 0);
26573 write_psymbols (symtab,
26574 psyms_seen,
af5bf4ad 26575 &objfile->static_psymbols[psymtab->statics_offset],
95554aad
TT
26576 psymtab->n_static_syms, cu_index,
26577 1);
26578}
26579
437afbb8
JK
26580/* DWARF-5 .debug_names builder. */
26581class debug_names
26582{
26583public:
ed2dc618
SM
26584 debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile, bool is_dwarf64,
26585 bfd_endian dwarf5_byte_order)
437afbb8
JK
26586 : m_dwarf5_byte_order (dwarf5_byte_order),
26587 m_dwarf32 (dwarf5_byte_order),
26588 m_dwarf64 (dwarf5_byte_order),
26589 m_dwarf (is_dwarf64
26590 ? static_cast<dwarf &> (m_dwarf64)
26591 : static_cast<dwarf &> (m_dwarf32)),
26592 m_name_table_string_offs (m_dwarf.name_table_string_offs),
ed2dc618
SM
26593 m_name_table_entry_offs (m_dwarf.name_table_entry_offs),
26594 m_debugstrlookup (dwarf2_per_objfile)
437afbb8 26595 {}
9291a0cd 26596
8af5c486
JK
26597 int dwarf5_offset_size () const
26598 {
26599 const bool dwarf5_is_dwarf64 = &m_dwarf == &m_dwarf64;
26600 return dwarf5_is_dwarf64 ? 8 : 4;
26601 }
26602
26603 /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit? */
26604 enum class unit_kind { cu, tu };
26605
437afbb8 26606 /* Insert one symbol. */
8af5c486
JK
26607 void insert (const partial_symbol *psym, int cu_index, bool is_static,
26608 unit_kind kind)
437afbb8
JK
26609 {
26610 const int dwarf_tag = psymbol_tag (psym);
26611 if (dwarf_tag == 0)
26612 return;
26613 const char *const name = SYMBOL_SEARCH_NAME (psym);
26614 const auto insertpair
26615 = m_name_to_value_set.emplace (c_str_view (name),
26616 std::set<symbol_value> ());
26617 std::set<symbol_value> &value_set = insertpair.first->second;
8af5c486 26618 value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static, kind));
437afbb8 26619 }
9291a0cd 26620
437afbb8
JK
26621 /* Build all the tables. All symbols must be already inserted.
26622 This function does not call file_write, caller has to do it
26623 afterwards. */
26624 void build ()
26625 {
26626 /* Verify the build method has not be called twice. */
26627 gdb_assert (m_abbrev_table.empty ());
26628 const size_t name_count = m_name_to_value_set.size ();
26629 m_bucket_table.resize
26630 (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3))));
26631 m_hash_table.reserve (name_count);
26632 m_name_table_string_offs.reserve (name_count);
26633 m_name_table_entry_offs.reserve (name_count);
26634
26635 /* Map each hash of symbol to its name and value. */
26636 struct hash_it_pair
26637 {
26638 uint32_t hash;
26639 decltype (m_name_to_value_set)::const_iterator it;
26640 };
26641 std::vector<std::forward_list<hash_it_pair>> bucket_hash;
26642 bucket_hash.resize (m_bucket_table.size ());
26643 for (decltype (m_name_to_value_set)::const_iterator it
26644 = m_name_to_value_set.cbegin ();
26645 it != m_name_to_value_set.cend ();
26646 ++it)
26647 {
26648 const char *const name = it->first.c_str ();
26649 const uint32_t hash = dwarf5_djb_hash (name);
26650 hash_it_pair hashitpair;
26651 hashitpair.hash = hash;
26652 hashitpair.it = it;
26653 auto &slot = bucket_hash[hash % bucket_hash.size()];
26654 slot.push_front (std::move (hashitpair));
26655 }
26656 for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix)
26657 {
26658 const std::forward_list<hash_it_pair> &hashitlist
26659 = bucket_hash[bucket_ix];
26660 if (hashitlist.empty ())
26661 continue;
26662 uint32_t &bucket_slot = m_bucket_table[bucket_ix];
26663 /* The hashes array is indexed starting at 1. */
26664 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot),
26665 sizeof (bucket_slot), m_dwarf5_byte_order,
26666 m_hash_table.size () + 1);
26667 for (const hash_it_pair &hashitpair : hashitlist)
26668 {
26669 m_hash_table.push_back (0);
26670 store_unsigned_integer (reinterpret_cast<gdb_byte *>
26671 (&m_hash_table.back ()),
26672 sizeof (m_hash_table.back ()),
26673 m_dwarf5_byte_order, hashitpair.hash);
26674 const c_str_view &name = hashitpair.it->first;
26675 const std::set<symbol_value> &value_set = hashitpair.it->second;
26676 m_name_table_string_offs.push_back_reorder
26677 (m_debugstrlookup.lookup (name.c_str ()));
26678 m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
26679 gdb_assert (!value_set.empty ());
26680 for (const symbol_value &value : value_set)
26681 {
26682 int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag,
8af5c486
JK
26683 value.is_static,
26684 value.kind)];
437afbb8
JK
26685 if (idx == 0)
26686 {
26687 idx = m_idx_next++;
26688 m_abbrev_table.append_unsigned_leb128 (idx);
26689 m_abbrev_table.append_unsigned_leb128 (value.dwarf_tag);
8af5c486
JK
26690 m_abbrev_table.append_unsigned_leb128
26691 (value.kind == unit_kind::cu ? DW_IDX_compile_unit
26692 : DW_IDX_type_unit);
437afbb8
JK
26693 m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
26694 m_abbrev_table.append_unsigned_leb128 (value.is_static
26695 ? DW_IDX_GNU_internal
26696 : DW_IDX_GNU_external);
26697 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
26698
26699 /* Terminate attributes list. */
26700 m_abbrev_table.append_unsigned_leb128 (0);
26701 m_abbrev_table.append_unsigned_leb128 (0);
26702 }
9291a0cd 26703
437afbb8
JK
26704 m_entry_pool.append_unsigned_leb128 (idx);
26705 m_entry_pool.append_unsigned_leb128 (value.cu_index);
26706 }
9291a0cd 26707
437afbb8
JK
26708 /* Terminate the list of CUs. */
26709 m_entry_pool.append_unsigned_leb128 (0);
26710 }
26711 }
26712 gdb_assert (m_hash_table.size () == name_count);
987d643c 26713
437afbb8
JK
26714 /* Terminate tags list. */
26715 m_abbrev_table.append_unsigned_leb128 (0);
26716 }
0a5429f6 26717
437afbb8
JK
26718 /* Return .debug_names bucket count. This must be called only after
26719 calling the build method. */
26720 uint32_t bucket_count () const
26721 {
26722 /* Verify the build method has been already called. */
26723 gdb_assert (!m_abbrev_table.empty ());
26724 const uint32_t retval = m_bucket_table.size ();
26725
26726 /* Check for overflow. */
26727 gdb_assert (retval == m_bucket_table.size ());
26728 return retval;
26729 }
26730
26731 /* Return .debug_names names count. This must be called only after
26732 calling the build method. */
26733 uint32_t name_count () const
26734 {
26735 /* Verify the build method has been already called. */
26736 gdb_assert (!m_abbrev_table.empty ());
26737 const uint32_t retval = m_hash_table.size ();
26738
26739 /* Check for overflow. */
26740 gdb_assert (retval == m_hash_table.size ());
26741 return retval;
26742 }
26743
26744 /* Return number of bytes of .debug_names abbreviation table. This
26745 must be called only after calling the build method. */
26746 uint32_t abbrev_table_bytes () const
26747 {
26748 gdb_assert (!m_abbrev_table.empty ());
26749 return m_abbrev_table.size ();
26750 }
26751
26752 /* Recurse into all "included" dependencies and store their symbols
26753 as if they appeared in this psymtab. */
26754 void recursively_write_psymbols
26755 (struct objfile *objfile,
26756 struct partial_symtab *psymtab,
26757 std::unordered_set<partial_symbol *> &psyms_seen,
26758 int cu_index)
26759 {
26760 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26761 if (psymtab->dependencies[i]->user != NULL)
26762 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26763 psyms_seen, cu_index);
26764
26765 write_psymbols (psyms_seen,
26766 &objfile->global_psymbols[psymtab->globals_offset],
8af5c486 26767 psymtab->n_global_syms, cu_index, false, unit_kind::cu);
437afbb8
JK
26768 write_psymbols (psyms_seen,
26769 &objfile->static_psymbols[psymtab->statics_offset],
8af5c486 26770 psymtab->n_static_syms, cu_index, true, unit_kind::cu);
437afbb8
JK
26771 }
26772
26773 /* Return number of bytes the .debug_names section will have. This
26774 must be called only after calling the build method. */
26775 size_t bytes () const
26776 {
26777 /* Verify the build method has been already called. */
26778 gdb_assert (!m_abbrev_table.empty ());
26779 size_t expected_bytes = 0;
26780 expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]);
26781 expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]);
26782 expected_bytes += m_name_table_string_offs.bytes ();
26783 expected_bytes += m_name_table_entry_offs.bytes ();
26784 expected_bytes += m_abbrev_table.size ();
26785 expected_bytes += m_entry_pool.size ();
26786 return expected_bytes;
26787 }
26788
26789 /* Write .debug_names to FILE_NAMES and .debug_str addition to
26790 FILE_STR. This must be called only after calling the build
26791 method. */
26792 void file_write (FILE *file_names, FILE *file_str) const
26793 {
26794 /* Verify the build method has been already called. */
26795 gdb_assert (!m_abbrev_table.empty ());
26796 ::file_write (file_names, m_bucket_table);
26797 ::file_write (file_names, m_hash_table);
26798 m_name_table_string_offs.file_write (file_names);
26799 m_name_table_entry_offs.file_write (file_names);
26800 m_abbrev_table.file_write (file_names);
26801 m_entry_pool.file_write (file_names);
26802 m_debugstrlookup.file_write (file_str);
26803 }
26804
8af5c486
JK
26805 /* A helper user data for write_one_signatured_type. */
26806 class write_one_signatured_type_data
26807 {
26808 public:
26809 write_one_signatured_type_data (debug_names &nametable_,
26810 signatured_type_index_data &&info_)
26811 : nametable (nametable_), info (std::move (info_))
26812 {}
26813 debug_names &nametable;
26814 struct signatured_type_index_data info;
26815 };
26816
26817 /* A helper function to pass write_one_signatured_type to
26818 htab_traverse_noresize. */
26819 static int
26820 write_one_signatured_type (void **slot, void *d)
26821 {
26822 write_one_signatured_type_data *data = (write_one_signatured_type_data *) d;
26823 struct signatured_type_index_data *info = &data->info;
26824 struct signatured_type *entry = (struct signatured_type *) *slot;
26825
26826 data->nametable.write_one_signatured_type (entry, info);
26827
26828 return 1;
26829 }
26830
437afbb8
JK
26831private:
26832
26833 /* Storage for symbol names mapping them to their .debug_str section
26834 offsets. */
26835 class debug_str_lookup
26836 {
26837 public:
26838
26839 /* Object costructor to be called for current DWARF2_PER_OBJFILE.
26840 All .debug_str section strings are automatically stored. */
ed2dc618
SM
26841 debug_str_lookup (struct dwarf2_per_objfile *dwarf2_per_objfile)
26842 : m_abfd (dwarf2_per_objfile->objfile->obfd),
26843 m_dwarf2_per_objfile (dwarf2_per_objfile)
437afbb8
JK
26844 {
26845 dwarf2_read_section (dwarf2_per_objfile->objfile,
26846 &dwarf2_per_objfile->str);
26847 if (dwarf2_per_objfile->str.buffer == NULL)
26848 return;
26849 for (const gdb_byte *data = dwarf2_per_objfile->str.buffer;
26850 data < (dwarf2_per_objfile->str.buffer
26851 + dwarf2_per_objfile->str.size);)
26852 {
26853 const char *const s = reinterpret_cast<const char *> (data);
26854 const auto insertpair
26855 = m_str_table.emplace (c_str_view (s),
26856 data - dwarf2_per_objfile->str.buffer);
26857 if (!insertpair.second)
26858 complaint (&symfile_complaints,
26859 _("Duplicate string \"%s\" in "
26860 ".debug_str section [in module %s]"),
26861 s, bfd_get_filename (m_abfd));
26862 data += strlen (s) + 1;
26863 }
26864 }
26865
26866 /* Return offset of symbol name S in the .debug_str section. Add
26867 such symbol to the section's end if it does not exist there
26868 yet. */
26869 size_t lookup (const char *s)
26870 {
26871 const auto it = m_str_table.find (c_str_view (s));
26872 if (it != m_str_table.end ())
26873 return it->second;
ed2dc618 26874 const size_t offset = (m_dwarf2_per_objfile->str.size
437afbb8
JK
26875 + m_str_add_buf.size ());
26876 m_str_table.emplace (c_str_view (s), offset);
26877 m_str_add_buf.append_cstr0 (s);
26878 return offset;
26879 }
26880
26881 /* Append the end of the .debug_str section to FILE. */
26882 void file_write (FILE *file) const
26883 {
26884 m_str_add_buf.file_write (file);
26885 }
26886
26887 private:
26888 std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
26889 bfd *const m_abfd;
ed2dc618 26890 struct dwarf2_per_objfile *m_dwarf2_per_objfile;
437afbb8
JK
26891
26892 /* Data to add at the end of .debug_str for new needed symbol names. */
26893 data_buf m_str_add_buf;
26894 };
26895
26896 /* Container to map used DWARF tags to their .debug_names abbreviation
26897 tags. */
26898 class index_key
26899 {
26900 public:
8af5c486
JK
26901 index_key (int dwarf_tag_, bool is_static_, unit_kind kind_)
26902 : dwarf_tag (dwarf_tag_), is_static (is_static_), kind (kind_)
437afbb8
JK
26903 {
26904 }
26905
26906 bool
26907 operator== (const index_key &other) const
26908 {
8af5c486
JK
26909 return (dwarf_tag == other.dwarf_tag && is_static == other.is_static
26910 && kind == other.kind);
437afbb8
JK
26911 }
26912
26913 const int dwarf_tag;
26914 const bool is_static;
8af5c486 26915 const unit_kind kind;
437afbb8
JK
26916 };
26917
26918 /* Provide std::unordered_map::hasher for index_key. */
26919 class index_key_hasher
26920 {
26921 public:
26922 size_t
26923 operator () (const index_key &key) const
26924 {
26925 return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static;
26926 }
26927 };
26928
26929 /* Parameters of one symbol entry. */
26930 class symbol_value
26931 {
26932 public:
26933 const int dwarf_tag, cu_index;
26934 const bool is_static;
8af5c486 26935 const unit_kind kind;
437afbb8 26936
8af5c486
JK
26937 symbol_value (int dwarf_tag_, int cu_index_, bool is_static_,
26938 unit_kind kind_)
26939 : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_),
26940 kind (kind_)
437afbb8
JK
26941 {}
26942
26943 bool
26944 operator< (const symbol_value &other) const
26945 {
26946#define X(n) \
26947 do \
26948 { \
26949 if (n < other.n) \
26950 return true; \
26951 if (n > other.n) \
26952 return false; \
26953 } \
26954 while (0)
26955 X (dwarf_tag);
26956 X (is_static);
8af5c486 26957 X (kind);
437afbb8
JK
26958 X (cu_index);
26959#undef X
26960 return false;
26961 }
26962 };
26963
26964 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
26965 output. */
26966 class offset_vec
26967 {
26968 protected:
26969 const bfd_endian dwarf5_byte_order;
26970 public:
26971 explicit offset_vec (bfd_endian dwarf5_byte_order_)
26972 : dwarf5_byte_order (dwarf5_byte_order_)
26973 {}
26974
26975 /* Call std::vector::reserve for NELEM elements. */
26976 virtual void reserve (size_t nelem) = 0;
26977
26978 /* Call std::vector::push_back with store_unsigned_integer byte
26979 reordering for ELEM. */
26980 virtual void push_back_reorder (size_t elem) = 0;
26981
26982 /* Return expected output size in bytes. */
26983 virtual size_t bytes () const = 0;
26984
26985 /* Write name table to FILE. */
26986 virtual void file_write (FILE *file) const = 0;
26987 };
26988
26989 /* Template to unify DWARF-32 and DWARF-64 output. */
26990 template<typename OffsetSize>
26991 class offset_vec_tmpl : public offset_vec
26992 {
26993 public:
26994 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
26995 : offset_vec (dwarf5_byte_order_)
26996 {}
26997
26998 /* Implement offset_vec::reserve. */
26999 void reserve (size_t nelem) override
27000 {
27001 m_vec.reserve (nelem);
27002 }
27003
27004 /* Implement offset_vec::push_back_reorder. */
27005 void push_back_reorder (size_t elem) override
27006 {
27007 m_vec.push_back (elem);
27008 /* Check for overflow. */
27009 gdb_assert (m_vec.back () == elem);
27010 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
27011 sizeof (m_vec.back ()), dwarf5_byte_order, elem);
27012 }
27013
27014 /* Implement offset_vec::bytes. */
27015 size_t bytes () const override
27016 {
27017 return m_vec.size () * sizeof (m_vec[0]);
27018 }
27019
27020 /* Implement offset_vec::file_write. */
27021 void file_write (FILE *file) const override
27022 {
27023 ::file_write (file, m_vec);
27024 }
27025
27026 private:
27027 std::vector<OffsetSize> m_vec;
27028 };
27029
27030 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
27031 respecting name table width. */
27032 class dwarf
27033 {
27034 public:
27035 offset_vec &name_table_string_offs, &name_table_entry_offs;
27036
27037 dwarf (offset_vec &name_table_string_offs_,
27038 offset_vec &name_table_entry_offs_)
27039 : name_table_string_offs (name_table_string_offs_),
27040 name_table_entry_offs (name_table_entry_offs_)
27041 {
27042 }
27043 };
e8f8bcb3 27044
437afbb8
JK
27045 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
27046 respecting name table width. */
27047 template<typename OffsetSize>
27048 class dwarf_tmpl : public dwarf
27049 {
27050 public:
27051 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
27052 : dwarf (m_name_table_string_offs, m_name_table_entry_offs),
27053 m_name_table_string_offs (dwarf5_byte_order_),
27054 m_name_table_entry_offs (dwarf5_byte_order_)
27055 {}
27056
27057 private:
27058 offset_vec_tmpl<OffsetSize> m_name_table_string_offs;
27059 offset_vec_tmpl<OffsetSize> m_name_table_entry_offs;
27060 };
27061
27062 /* Try to reconstruct original DWARF tag for given partial_symbol.
27063 This function is not DWARF-5 compliant but it is sufficient for
27064 GDB as a DWARF-5 index consumer. */
27065 static int psymbol_tag (const struct partial_symbol *psym)
27066 {
27067 domain_enum domain = PSYMBOL_DOMAIN (psym);
27068 enum address_class aclass = PSYMBOL_CLASS (psym);
27069
27070 switch (domain)
27071 {
27072 case VAR_DOMAIN:
27073 switch (aclass)
27074 {
27075 case LOC_BLOCK:
27076 return DW_TAG_subprogram;
27077 case LOC_TYPEDEF:
27078 return DW_TAG_typedef;
27079 case LOC_COMPUTED:
27080 case LOC_CONST_BYTES:
27081 case LOC_OPTIMIZED_OUT:
27082 case LOC_STATIC:
27083 return DW_TAG_variable;
27084 case LOC_CONST:
27085 /* Note: It's currently impossible to recognize psyms as enum values
27086 short of reading the type info. For now punt. */
27087 return DW_TAG_variable;
27088 default:
27089 /* There are other LOC_FOO values that one might want to classify
27090 as variables, but dwarf2read.c doesn't currently use them. */
27091 return DW_TAG_variable;
27092 }
27093 case STRUCT_DOMAIN:
27094 return DW_TAG_structure_type;
27095 default:
27096 return 0;
27097 }
27098 }
27099
27100 /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */
27101 void write_psymbols (std::unordered_set<partial_symbol *> &psyms_seen,
27102 struct partial_symbol **psymp, int count, int cu_index,
8af5c486 27103 bool is_static, unit_kind kind)
437afbb8
JK
27104 {
27105 for (; count-- > 0; ++psymp)
27106 {
27107 struct partial_symbol *psym = *psymp;
27108
27109 if (SYMBOL_LANGUAGE (psym) == language_ada)
27110 error (_("Ada is not currently supported by the index"));
27111
27112 /* Only add a given psymbol once. */
27113 if (psyms_seen.insert (psym).second)
8af5c486 27114 insert (psym, cu_index, is_static, kind);
437afbb8
JK
27115 }
27116 }
27117
8af5c486
JK
27118 /* A helper function that writes a single signatured_type
27119 to a debug_names. */
27120 void
27121 write_one_signatured_type (struct signatured_type *entry,
27122 struct signatured_type_index_data *info)
27123 {
27124 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
27125
27126 write_psymbols (info->psyms_seen,
27127 &info->objfile->global_psymbols[psymtab->globals_offset],
27128 psymtab->n_global_syms, info->cu_index, false,
27129 unit_kind::tu);
27130 write_psymbols (info->psyms_seen,
27131 &info->objfile->static_psymbols[psymtab->statics_offset],
27132 psymtab->n_static_syms, info->cu_index, true,
27133 unit_kind::tu);
27134
27135 info->types_list.append_uint (dwarf5_offset_size (), m_dwarf5_byte_order,
27136 to_underlying (entry->per_cu.sect_off));
27137
27138 ++info->cu_index;
27139 }
27140
437afbb8
JK
27141 /* Store value of each symbol. */
27142 std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher>
27143 m_name_to_value_set;
27144
27145 /* Tables of DWARF-5 .debug_names. They are in object file byte
27146 order. */
27147 std::vector<uint32_t> m_bucket_table;
27148 std::vector<uint32_t> m_hash_table;
27149
27150 const bfd_endian m_dwarf5_byte_order;
27151 dwarf_tmpl<uint32_t> m_dwarf32;
27152 dwarf_tmpl<uint64_t> m_dwarf64;
27153 dwarf &m_dwarf;
27154 offset_vec &m_name_table_string_offs, &m_name_table_entry_offs;
27155 debug_str_lookup m_debugstrlookup;
27156
27157 /* Map each used .debug_names abbreviation tag parameter to its
27158 index value. */
27159 std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
27160
27161 /* Next unused .debug_names abbreviation tag for
27162 m_indexkey_to_idx. */
27163 int m_idx_next = 1;
27164
27165 /* .debug_names abbreviation table. */
27166 data_buf m_abbrev_table;
27167
27168 /* .debug_names entry pool. */
27169 data_buf m_entry_pool;
27170};
27171
27172/* Return iff any of the needed offsets does not fit into 32-bit
27173 .debug_names section. */
27174
27175static bool
ed2dc618 27176check_dwarf64_offsets (struct dwarf2_per_objfile *dwarf2_per_objfile)
437afbb8
JK
27177{
27178 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27179 {
27180 const dwarf2_per_cu_data &per_cu = *dwarf2_per_objfile->all_comp_units[i];
27181
27182 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
27183 return true;
27184 }
27185 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
27186 {
27187 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
27188 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
27189
27190 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
27191 return true;
27192 }
27193 return false;
27194}
27195
27196/* The psyms_seen set is potentially going to be largish (~40k
27197 elements when indexing a -g3 build of GDB itself). Estimate the
27198 number of elements in order to avoid too many rehashes, which
27199 require rebuilding buckets and thus many trips to
27200 malloc/free. */
27201
27202static size_t
ed2dc618 27203psyms_seen_size (struct dwarf2_per_objfile *dwarf2_per_objfile)
437afbb8 27204{
e8f8bcb3
PA
27205 size_t psyms_count = 0;
27206 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27207 {
27208 struct dwarf2_per_cu_data *per_cu
27209 = dwarf2_per_objfile->all_comp_units[i];
27210 struct partial_symtab *psymtab = per_cu->v.psymtab;
27211
27212 if (psymtab != NULL && psymtab->user == NULL)
27213 recursively_count_psymbols (psymtab, psyms_count);
27214 }
27215 /* Generating an index for gdb itself shows a ratio of
27216 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
437afbb8
JK
27217 return psyms_count / 4;
27218}
27219
27220/* Write new .gdb_index section for OBJFILE into OUT_FILE.
27221 Return how many bytes were expected to be written into OUT_FILE. */
27222
27223static size_t
ed2dc618 27224write_gdbindex (struct dwarf2_per_objfile *dwarf2_per_objfile, FILE *out_file)
437afbb8 27225{
ed2dc618 27226 struct objfile *objfile = dwarf2_per_objfile->objfile;
437afbb8
JK
27227 mapped_symtab symtab;
27228 data_buf cu_list;
27229
27230 /* While we're scanning CU's create a table that maps a psymtab pointer
27231 (which is what addrmap records) to its index (which is what is recorded
27232 in the index file). This will later be needed to write the address
27233 table. */
27234 psym_index_map cu_index_htab;
27235 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
27236
27237 /* The CU list is already sorted, so we don't need to do additional
27238 work here. Also, the debug_types entries do not appear in
27239 all_comp_units, but only in their own hash table. */
27240
ed2dc618
SM
27241 std::unordered_set<partial_symbol *> psyms_seen
27242 (psyms_seen_size (dwarf2_per_objfile));
bc8f2430 27243 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd 27244 {
3e43a32a
MS
27245 struct dwarf2_per_cu_data *per_cu
27246 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 27247 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 27248
92fac807
JK
27249 /* CU of a shared file from 'dwz -m' may be unused by this main file.
27250 It may be referenced from a local scope but in such case it does not
27251 need to be present in .gdb_index. */
27252 if (psymtab == NULL)
27253 continue;
27254
95554aad 27255 if (psymtab->user == NULL)
bc8f2430
JK
27256 recursively_write_psymbols (objfile, psymtab, &symtab,
27257 psyms_seen, i);
9291a0cd 27258
bc8f2430
JK
27259 const auto insertpair = cu_index_htab.emplace (psymtab, i);
27260 gdb_assert (insertpair.second);
9291a0cd 27261
c2f134ac
PA
27262 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
27263 to_underlying (per_cu->sect_off));
27264 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
27265 }
27266
0a5429f6 27267 /* Dump the address map. */
bc8f2430
JK
27268 data_buf addr_vec;
27269 write_address_map (objfile, addr_vec, cu_index_htab);
0a5429f6 27270
1fd400ff 27271 /* Write out the .debug_type entries, if any. */
bc8f2430 27272 data_buf types_cu_list;
1fd400ff
TT
27273 if (dwarf2_per_objfile->signatured_types)
27274 {
bc8f2430
JK
27275 signatured_type_index_data sig_data (types_cu_list,
27276 psyms_seen);
1fd400ff
TT
27277
27278 sig_data.objfile = objfile;
bc8f2430 27279 sig_data.symtab = &symtab;
1fd400ff
TT
27280 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
27281 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
27282 write_one_signatured_type, &sig_data);
27283 }
27284
156942c7
DE
27285 /* Now that we've processed all symbols we can shrink their cu_indices
27286 lists. */
bc8f2430 27287 uniquify_cu_indices (&symtab);
156942c7 27288
bc8f2430
JK
27289 data_buf symtab_vec, constant_pool;
27290 write_hash_table (&symtab, symtab_vec, constant_pool);
9291a0cd 27291
bc8f2430
JK
27292 data_buf contents;
27293 const offset_type size_of_contents = 6 * sizeof (offset_type);
27294 offset_type total_len = size_of_contents;
9291a0cd
TT
27295
27296 /* The version number. */
bc8f2430 27297 contents.append_data (MAYBE_SWAP (8));
9291a0cd
TT
27298
27299 /* The offset of the CU list from the start of the file. */
bc8f2430
JK
27300 contents.append_data (MAYBE_SWAP (total_len));
27301 total_len += cu_list.size ();
9291a0cd 27302
1fd400ff 27303 /* The offset of the types CU list from the start of the file. */
bc8f2430
JK
27304 contents.append_data (MAYBE_SWAP (total_len));
27305 total_len += types_cu_list.size ();
1fd400ff 27306
9291a0cd 27307 /* The offset of the address table from the start of the file. */
bc8f2430
JK
27308 contents.append_data (MAYBE_SWAP (total_len));
27309 total_len += addr_vec.size ();
9291a0cd
TT
27310
27311 /* The offset of the symbol table from the start of the file. */
bc8f2430
JK
27312 contents.append_data (MAYBE_SWAP (total_len));
27313 total_len += symtab_vec.size ();
9291a0cd
TT
27314
27315 /* The offset of the constant pool from the start of the file. */
bc8f2430
JK
27316 contents.append_data (MAYBE_SWAP (total_len));
27317 total_len += constant_pool.size ();
9291a0cd 27318
bc8f2430 27319 gdb_assert (contents.size () == size_of_contents);
9291a0cd 27320
bc8f2430
JK
27321 contents.file_write (out_file);
27322 cu_list.file_write (out_file);
27323 types_cu_list.file_write (out_file);
27324 addr_vec.file_write (out_file);
27325 symtab_vec.file_write (out_file);
27326 constant_pool.file_write (out_file);
9291a0cd 27327
437afbb8
JK
27328 return total_len;
27329}
27330
27331/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
27332static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
27333
27334/* Write a new .debug_names section for OBJFILE into OUT_FILE, write
27335 needed addition to .debug_str section to OUT_FILE_STR. Return how
27336 many bytes were expected to be written into OUT_FILE. */
27337
27338static size_t
ed2dc618
SM
27339write_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
27340 FILE *out_file, FILE *out_file_str)
437afbb8 27341{
ed2dc618
SM
27342 const bool dwarf5_is_dwarf64 = check_dwarf64_offsets (dwarf2_per_objfile);
27343 struct objfile *objfile = dwarf2_per_objfile->objfile;
437afbb8
JK
27344 const enum bfd_endian dwarf5_byte_order
27345 = gdbarch_byte_order (get_objfile_arch (objfile));
27346
27347 /* The CU list is already sorted, so we don't need to do additional
27348 work here. Also, the debug_types entries do not appear in
27349 all_comp_units, but only in their own hash table. */
27350 data_buf cu_list;
ed2dc618
SM
27351 debug_names nametable (dwarf2_per_objfile, dwarf5_is_dwarf64,
27352 dwarf5_byte_order);
27353 std::unordered_set<partial_symbol *>
27354 psyms_seen (psyms_seen_size (dwarf2_per_objfile));
437afbb8
JK
27355 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27356 {
27357 const dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
27358 partial_symtab *psymtab = per_cu->v.psymtab;
27359
27360 /* CU of a shared file from 'dwz -m' may be unused by this main
27361 file. It may be referenced from a local scope but in such
27362 case it does not need to be present in .debug_names. */
27363 if (psymtab == NULL)
27364 continue;
27365
27366 if (psymtab->user == NULL)
27367 nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i);
27368
8af5c486 27369 cu_list.append_uint (nametable.dwarf5_offset_size (), dwarf5_byte_order,
437afbb8
JK
27370 to_underlying (per_cu->sect_off));
27371 }
437afbb8 27372
8af5c486 27373 /* Write out the .debug_type entries, if any. */
437afbb8 27374 data_buf types_cu_list;
8af5c486 27375 if (dwarf2_per_objfile->signatured_types)
437afbb8 27376 {
8af5c486
JK
27377 debug_names::write_one_signatured_type_data sig_data (nametable,
27378 signatured_type_index_data (types_cu_list, psyms_seen));
437afbb8 27379
8af5c486
JK
27380 sig_data.info.objfile = objfile;
27381 /* It is used only for gdb_index. */
27382 sig_data.info.symtab = nullptr;
27383 sig_data.info.cu_index = 0;
27384 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
27385 debug_names::write_one_signatured_type,
27386 &sig_data);
437afbb8
JK
27387 }
27388
8af5c486
JK
27389 nametable.build ();
27390
27391 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
27392
437afbb8
JK
27393 const offset_type bytes_of_header
27394 = ((dwarf5_is_dwarf64 ? 12 : 4)
27395 + 2 + 2 + 7 * 4
27396 + sizeof (dwarf5_gdb_augmentation));
27397 size_t expected_bytes = 0;
27398 expected_bytes += bytes_of_header;
27399 expected_bytes += cu_list.size ();
27400 expected_bytes += types_cu_list.size ();
27401 expected_bytes += nametable.bytes ();
27402 data_buf header;
27403
27404 if (!dwarf5_is_dwarf64)
27405 {
27406 const uint64_t size64 = expected_bytes - 4;
27407 gdb_assert (size64 < 0xfffffff0);
27408 header.append_uint (4, dwarf5_byte_order, size64);
27409 }
27410 else
27411 {
27412 header.append_uint (4, dwarf5_byte_order, 0xffffffff);
27413 header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
27414 }
27415
27416 /* The version number. */
27417 header.append_uint (2, dwarf5_byte_order, 5);
27418
27419 /* Padding. */
27420 header.append_uint (2, dwarf5_byte_order, 0);
27421
27422 /* comp_unit_count - The number of CUs in the CU list. */
27423 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_comp_units);
27424
27425 /* local_type_unit_count - The number of TUs in the local TU
27426 list. */
27427 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_type_units);
27428
27429 /* foreign_type_unit_count - The number of TUs in the foreign TU
27430 list. */
27431 header.append_uint (4, dwarf5_byte_order, 0);
27432
27433 /* bucket_count - The number of hash buckets in the hash lookup
27434 table. */
27435 header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ());
27436
27437 /* name_count - The number of unique names in the index. */
27438 header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
27439
27440 /* abbrev_table_size - The size in bytes of the abbreviations
27441 table. */
27442 header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
27443
27444 /* augmentation_string_size - The size in bytes of the augmentation
27445 string. This value is rounded up to a multiple of 4. */
27446 static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, "");
27447 header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation));
27448 header.append_data (dwarf5_gdb_augmentation);
27449
27450 gdb_assert (header.size () == bytes_of_header);
27451
27452 header.file_write (out_file);
27453 cu_list.file_write (out_file);
27454 types_cu_list.file_write (out_file);
27455 nametable.file_write (out_file, out_file_str);
27456
27457 return expected_bytes;
27458}
27459
27460/* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
27461 position is at the end of the file. */
27462
27463static void
27464assert_file_size (FILE *file, const char *filename, size_t expected_size)
27465{
27466 const auto file_size = ftell (file);
27467 if (file_size == -1)
27468 error (_("Can't get `%s' size"), filename);
27469 gdb_assert (file_size == expected_size);
27470}
27471
437afbb8
JK
27472/* Create an index file for OBJFILE in the directory DIR. */
27473
27474static void
ed2dc618
SM
27475write_psymtabs_to_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
27476 const char *dir,
437afbb8
JK
27477 dw_index_kind index_kind)
27478{
ed2dc618
SM
27479 struct objfile *objfile = dwarf2_per_objfile->objfile;
27480
437afbb8
JK
27481 if (dwarf2_per_objfile->using_index)
27482 error (_("Cannot use an index to create the index"));
27483
27484 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
27485 error (_("Cannot make an index when the file has multiple .debug_types sections"));
27486
27487 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
27488 return;
27489
27490 struct stat st;
27491 if (stat (objfile_name (objfile), &st) < 0)
27492 perror_with_name (objfile_name (objfile));
27493
27494 std::string filename (std::string (dir) + SLASH_STRING
27495 + lbasename (objfile_name (objfile))
27496 + (index_kind == dw_index_kind::DEBUG_NAMES
27497 ? INDEX5_SUFFIX : INDEX4_SUFFIX));
27498
27499 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
27500 if (!out_file)
27501 error (_("Can't open `%s' for writing"), filename.c_str ());
27502
27503 /* Order matters here; we want FILE to be closed before FILENAME is
27504 unlinked, because on MS-Windows one cannot delete a file that is
27505 still open. (Don't call anything here that might throw until
27506 file_closer is created.) */
27507 gdb::unlinker unlink_file (filename.c_str ());
27508 gdb_file_up close_out_file (out_file);
27509
27510 if (index_kind == dw_index_kind::DEBUG_NAMES)
27511 {
27512 std::string filename_str (std::string (dir) + SLASH_STRING
27513 + lbasename (objfile_name (objfile))
27514 + DEBUG_STR_SUFFIX);
27515 FILE *out_file_str
27516 = gdb_fopen_cloexec (filename_str.c_str (), "wb").release ();
27517 if (!out_file_str)
27518 error (_("Can't open `%s' for writing"), filename_str.c_str ());
27519 gdb::unlinker unlink_file_str (filename_str.c_str ());
27520 gdb_file_up close_out_file_str (out_file_str);
27521
27522 const size_t total_len
ed2dc618 27523 = write_debug_names (dwarf2_per_objfile, out_file, out_file_str);
437afbb8
JK
27524 assert_file_size (out_file, filename.c_str (), total_len);
27525
27526 /* We want to keep the file .debug_str file too. */
27527 unlink_file_str.keep ();
27528 }
27529 else
27530 {
27531 const size_t total_len
ed2dc618 27532 = write_gdbindex (dwarf2_per_objfile, out_file);
437afbb8
JK
27533 assert_file_size (out_file, filename.c_str (), total_len);
27534 }
27535
bef155c3
TT
27536 /* We want to keep the file. */
27537 unlink_file.keep ();
9291a0cd
TT
27538}
27539
90476074
TT
27540/* Implementation of the `save gdb-index' command.
27541
437afbb8
JK
27542 Note that the .gdb_index file format used by this command is
27543 documented in the GDB manual. Any changes here must be documented
27544 there. */
11570e71 27545
9291a0cd 27546static void
8384c356 27547save_gdb_index_command (const char *arg, int from_tty)
9291a0cd
TT
27548{
27549 struct objfile *objfile;
437afbb8
JK
27550 const char dwarf5space[] = "-dwarf-5 ";
27551 dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
27552
27553 if (!arg)
27554 arg = "";
27555
27556 arg = skip_spaces (arg);
27557 if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
27558 {
27559 index_kind = dw_index_kind::DEBUG_NAMES;
27560 arg += strlen (dwarf5space);
27561 arg = skip_spaces (arg);
27562 }
9291a0cd 27563
437afbb8
JK
27564 if (!*arg)
27565 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
9291a0cd
TT
27566
27567 ALL_OBJFILES (objfile)
27568 {
27569 struct stat st;
27570
27571 /* If the objfile does not correspond to an actual file, skip it. */
4262abfb 27572 if (stat (objfile_name (objfile), &st) < 0)
9291a0cd
TT
27573 continue;
27574
ed2dc618
SM
27575 struct dwarf2_per_objfile *dwarf2_per_objfile
27576 = get_dwarf2_per_objfile (objfile);
9291a0cd 27577
ed2dc618
SM
27578 if (dwarf2_per_objfile != NULL)
27579 {
492d29ea 27580 TRY
9291a0cd 27581 {
ed2dc618 27582 write_psymtabs_to_index (dwarf2_per_objfile, arg, index_kind);
9291a0cd 27583 }
492d29ea
PA
27584 CATCH (except, RETURN_MASK_ERROR)
27585 {
27586 exception_fprintf (gdb_stderr, except,
27587 _("Error while writing index for `%s': "),
27588 objfile_name (objfile));
27589 }
27590 END_CATCH
9291a0cd 27591 }
ed2dc618 27592
9291a0cd 27593 }
dce234bc
PP
27594}
27595
9291a0cd
TT
27596\f
27597
b4f54984 27598int dwarf_always_disassemble;
9eae7c52
TT
27599
27600static void
b4f54984
DE
27601show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
27602 struct cmd_list_element *c, const char *value)
9eae7c52 27603{
3e43a32a
MS
27604 fprintf_filtered (file,
27605 _("Whether to always disassemble "
27606 "DWARF expressions is %s.\n"),
9eae7c52
TT
27607 value);
27608}
27609
900e11f9
JK
27610static void
27611show_check_physname (struct ui_file *file, int from_tty,
27612 struct cmd_list_element *c, const char *value)
27613{
27614 fprintf_filtered (file,
27615 _("Whether to check \"physname\" is %s.\n"),
27616 value);
27617}
27618
6502dd73
DJ
27619void
27620_initialize_dwarf2_read (void)
27621{
96d19272
JK
27622 struct cmd_list_element *c;
27623
fc8e7e75 27624 dwarf2_objfile_data_key = register_objfile_data ();
ae038cb0 27625
b4f54984
DE
27626 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
27627Set DWARF specific variables.\n\
27628Configure DWARF variables such as the cache size"),
27629 &set_dwarf_cmdlist, "maintenance set dwarf ",
ae038cb0
DJ
27630 0/*allow-unknown*/, &maintenance_set_cmdlist);
27631
b4f54984
DE
27632 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
27633Show DWARF specific variables\n\
27634Show DWARF variables such as the cache size"),
27635 &show_dwarf_cmdlist, "maintenance show dwarf ",
ae038cb0
DJ
27636 0/*allow-unknown*/, &maintenance_show_cmdlist);
27637
27638 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
b4f54984
DE
27639 &dwarf_max_cache_age, _("\
27640Set the upper bound on the age of cached DWARF compilation units."), _("\
27641Show the upper bound on the age of cached DWARF compilation units."), _("\
7915a72c
AC
27642A higher limit means that cached compilation units will be stored\n\
27643in memory longer, and more total memory will be used. Zero disables\n\
27644caching, which can slow down startup."),
2c5b56ce 27645 NULL,
b4f54984
DE
27646 show_dwarf_max_cache_age,
27647 &set_dwarf_cmdlist,
27648 &show_dwarf_cmdlist);
d97bc12b 27649
9eae7c52 27650 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
b4f54984 27651 &dwarf_always_disassemble, _("\
9eae7c52
TT
27652Set whether `info address' always disassembles DWARF expressions."), _("\
27653Show whether `info address' always disassembles DWARF expressions."), _("\
27654When enabled, DWARF expressions are always printed in an assembly-like\n\
27655syntax. When disabled, expressions will be printed in a more\n\
27656conversational style, when possible."),
27657 NULL,
b4f54984
DE
27658 show_dwarf_always_disassemble,
27659 &set_dwarf_cmdlist,
27660 &show_dwarf_cmdlist);
27661
27662 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
27663Set debugging of the DWARF reader."), _("\
27664Show debugging of the DWARF reader."), _("\
27665When enabled (non-zero), debugging messages are printed during DWARF\n\
73be47f5
DE
27666reading and symtab expansion. A value of 1 (one) provides basic\n\
27667information. A value greater than 1 provides more verbose information."),
45cfd468
DE
27668 NULL,
27669 NULL,
27670 &setdebuglist, &showdebuglist);
27671
b4f54984
DE
27672 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
27673Set debugging of the DWARF DIE reader."), _("\
27674Show debugging of the DWARF DIE reader."), _("\
d97bc12b
DE
27675When enabled (non-zero), DIEs are dumped after they are read in.\n\
27676The value is the maximum depth to print."),
ccce17b0
YQ
27677 NULL,
27678 NULL,
27679 &setdebuglist, &showdebuglist);
9291a0cd 27680
27e0867f
DE
27681 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
27682Set debugging of the dwarf line reader."), _("\
27683Show debugging of the dwarf line reader."), _("\
27684When enabled (non-zero), line number entries are dumped as they are read in.\n\
27685A value of 1 (one) provides basic information.\n\
27686A value greater than 1 provides more verbose information."),
27687 NULL,
27688 NULL,
27689 &setdebuglist, &showdebuglist);
27690
900e11f9
JK
27691 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
27692Set cross-checking of \"physname\" code against demangler."), _("\
27693Show cross-checking of \"physname\" code against demangler."), _("\
27694When enabled, GDB's internal \"physname\" code is checked against\n\
27695the demangler."),
27696 NULL, show_check_physname,
27697 &setdebuglist, &showdebuglist);
27698
e615022a
DE
27699 add_setshow_boolean_cmd ("use-deprecated-index-sections",
27700 no_class, &use_deprecated_index_sections, _("\
27701Set whether to use deprecated gdb_index sections."), _("\
27702Show whether to use deprecated gdb_index sections."), _("\
27703When enabled, deprecated .gdb_index sections are used anyway.\n\
27704Normally they are ignored either because of a missing feature or\n\
27705performance issue.\n\
27706Warning: This option must be enabled before gdb reads the file."),
27707 NULL,
27708 NULL,
27709 &setlist, &showlist);
27710
96d19272 27711 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 27712 _("\
fc1a9d6e 27713Save a gdb-index file.\n\
437afbb8
JK
27714Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
27715\n\
27716No options create one file with .gdb-index extension for pre-DWARF-5\n\
27717compatible .gdb_index section. With -dwarf-5 creates two files with\n\
27718extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
96d19272
JK
27719 &save_cmdlist);
27720 set_cmd_completer (c, filename_completer);
f1e6e072
TT
27721
27722 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
27723 &dwarf2_locexpr_funcs);
27724 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
27725 &dwarf2_loclist_funcs);
27726
27727 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
27728 &dwarf2_block_frame_base_locexpr_funcs);
27729 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
27730 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
27731
27732#if GDB_SELF_TEST
27733 selftests::register_test ("dw2_expand_symtabs_matching",
27734 selftests::dw2_expand_symtabs_matching::run_test);
27735#endif
6502dd73 27736}
This page took 4.602708 seconds and 4 git commands to generate.