Add myself as a write-after-approval GDB maintainer.
[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>
89
73be47f5
DE
90/* When == 1, print basic high level tracing messages.
91 When > 1, be more verbose.
b4f54984
DE
92 This is in contrast to the low level DIE reading of dwarf_die_debug. */
93static unsigned int dwarf_read_debug = 0;
45cfd468 94
d97bc12b 95/* When non-zero, dump DIEs after they are read in. */
b4f54984 96static unsigned int dwarf_die_debug = 0;
d97bc12b 97
27e0867f
DE
98/* When non-zero, dump line number entries as they are read in. */
99static unsigned int dwarf_line_debug = 0;
100
900e11f9
JK
101/* When non-zero, cross-check physname against demangler. */
102static int check_physname = 0;
103
481860b3 104/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 105static int use_deprecated_index_sections = 0;
481860b3 106
6502dd73
DJ
107static const struct objfile_data *dwarf2_objfile_data_key;
108
f1e6e072
TT
109/* The "aclass" indices for various kinds of computed DWARF symbols. */
110
111static int dwarf2_locexpr_index;
112static int dwarf2_loclist_index;
113static int dwarf2_locexpr_block_index;
114static int dwarf2_loclist_block_index;
115
73869dc2
DE
116/* A descriptor for dwarf sections.
117
118 S.ASECTION, SIZE are typically initialized when the objfile is first
119 scanned. BUFFER, READIN are filled in later when the section is read.
120 If the section contained compressed data then SIZE is updated to record
121 the uncompressed size of the section.
122
123 DWP file format V2 introduces a wrinkle that is easiest to handle by
124 creating the concept of virtual sections contained within a real section.
125 In DWP V2 the sections of the input DWO files are concatenated together
126 into one section, but section offsets are kept relative to the original
127 input section.
128 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
129 the real section this "virtual" section is contained in, and BUFFER,SIZE
130 describe the virtual section. */
131
dce234bc
PP
132struct dwarf2_section_info
133{
73869dc2
DE
134 union
135 {
e5aa3347 136 /* If this is a real section, the bfd section. */
049412e3 137 asection *section;
73869dc2 138 /* If this is a virtual section, pointer to the containing ("real")
e5aa3347 139 section. */
73869dc2
DE
140 struct dwarf2_section_info *containing_section;
141 } s;
19ac8c2e 142 /* Pointer to section data, only valid if readin. */
d521ce57 143 const gdb_byte *buffer;
73869dc2 144 /* The size of the section, real or virtual. */
dce234bc 145 bfd_size_type size;
73869dc2
DE
146 /* If this is a virtual section, the offset in the real section.
147 Only valid if is_virtual. */
148 bfd_size_type virtual_offset;
be391dca 149 /* True if we have tried to read this section. */
73869dc2
DE
150 char readin;
151 /* True if this is a virtual section, False otherwise.
049412e3 152 This specifies which of s.section and s.containing_section to use. */
73869dc2 153 char is_virtual;
dce234bc
PP
154};
155
8b70b953
TT
156typedef struct dwarf2_section_info dwarf2_section_info_def;
157DEF_VEC_O (dwarf2_section_info_def);
158
9291a0cd
TT
159/* All offsets in the index are of this type. It must be
160 architecture-independent. */
161typedef uint32_t offset_type;
162
163DEF_VEC_I (offset_type);
164
156942c7
DE
165/* Ensure only legit values are used. */
166#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
167 do { \
168 gdb_assert ((unsigned int) (value) <= 1); \
169 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
170 } while (0)
171
172/* Ensure only legit values are used. */
173#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
174 do { \
175 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
176 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
177 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
178 } while (0)
179
180/* Ensure we don't use more than the alloted nuber of bits for the CU. */
181#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
182 do { \
183 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
184 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
185 } while (0)
186
3f563c84
PA
187#if WORDS_BIGENDIAN
188
189/* Convert VALUE between big- and little-endian. */
190
191static offset_type
192byte_swap (offset_type value)
193{
194 offset_type result;
195
196 result = (value & 0xff) << 24;
197 result |= (value & 0xff00) << 8;
198 result |= (value & 0xff0000) >> 8;
199 result |= (value & 0xff000000) >> 24;
200 return result;
201}
202
203#define MAYBE_SWAP(V) byte_swap (V)
204
205#else
206#define MAYBE_SWAP(V) static_cast<offset_type> (V)
207#endif /* WORDS_BIGENDIAN */
208
209/* An index into a (C++) symbol name component in a symbol name as
210 recorded in the mapped_index's symbol table. For each C++ symbol
211 in the symbol table, we record one entry for the start of each
212 component in the symbol in a table of name components, and then
213 sort the table, in order to be able to binary search symbol names,
214 ignoring leading namespaces, both completion and regular look up.
215 For example, for symbol "A::B::C", we'll have an entry that points
216 to "A::B::C", another that points to "B::C", and another for "C".
217 Note that function symbols in GDB index have no parameter
218 information, just the function/method names. You can convert a
219 name_component to a "const char *" using the
220 'mapped_index::symbol_name_at(offset_type)' method. */
221
222struct name_component
223{
224 /* Offset in the symbol name where the component starts. Stored as
225 a (32-bit) offset instead of a pointer to save memory and improve
226 locality on 64-bit architectures. */
227 offset_type name_offset;
228
229 /* The symbol's index in the symbol and constant pool tables of a
230 mapped_index. */
231 offset_type idx;
232};
233
44ed8f3e
PA
234/* Base class containing bits shared by both .gdb_index and
235 .debug_name indexes. */
236
237struct mapped_index_base
238{
239 /* The name_component table (a sorted vector). See name_component's
240 description above. */
241 std::vector<name_component> name_components;
242
243 /* How NAME_COMPONENTS is sorted. */
244 enum case_sensitivity name_components_casing;
245
246 /* Return the number of names in the symbol table. */
247 virtual size_t symbol_name_count () const = 0;
248
249 /* Get the name of the symbol at IDX in the symbol table. */
250 virtual const char *symbol_name_at (offset_type idx) const = 0;
251
252 /* Return whether the name at IDX in the symbol table should be
253 ignored. */
254 virtual bool symbol_name_slot_invalid (offset_type idx) const
255 {
256 return false;
257 }
258
259 /* Build the symbol name component sorted vector, if we haven't
260 yet. */
261 void build_name_components ();
262
263 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
264 possible matches for LN_NO_PARAMS in the name component
265 vector. */
266 std::pair<std::vector<name_component>::const_iterator,
267 std::vector<name_component>::const_iterator>
268 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
269
270 /* Prevent deleting/destroying via a base class pointer. */
271protected:
272 ~mapped_index_base() = default;
273};
274
9291a0cd
TT
275/* A description of the mapped index. The file format is described in
276 a comment by the code that writes the index. */
fc898b42 277struct mapped_index final : public mapped_index_base
9291a0cd 278{
f00a2de2
PA
279 /* A slot/bucket in the symbol table hash. */
280 struct symbol_table_slot
281 {
282 const offset_type name;
283 const offset_type vec;
284 };
285
559a7a62
JK
286 /* Index data format version. */
287 int version;
288
9291a0cd
TT
289 /* The total length of the buffer. */
290 off_t total_size;
b11b1f88 291
f00a2de2
PA
292 /* The address table data. */
293 gdb::array_view<const gdb_byte> address_table;
b11b1f88 294
3876f04e 295 /* The symbol table, implemented as a hash table. */
f00a2de2 296 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 297
9291a0cd
TT
298 /* A pointer to the constant pool. */
299 const char *constant_pool;
3f563c84 300
44ed8f3e
PA
301 bool symbol_name_slot_invalid (offset_type idx) const override
302 {
303 const auto &bucket = this->symbol_table[idx];
304 return bucket.name == 0 && bucket.vec;
305 }
5c58de74 306
3f563c84
PA
307 /* Convenience method to get at the name of the symbol at IDX in the
308 symbol table. */
44ed8f3e 309 const char *symbol_name_at (offset_type idx) const override
f00a2de2 310 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 311
44ed8f3e
PA
312 size_t symbol_name_count () const override
313 { return this->symbol_table.size (); }
9291a0cd
TT
314};
315
927aa2e7
JK
316/* A description of the mapped .debug_names.
317 Uninitialized map has CU_COUNT 0. */
fc898b42 318struct mapped_debug_names final : public mapped_index_base
927aa2e7 319{
ed2dc618
SM
320 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
321 : dwarf2_per_objfile (dwarf2_per_objfile_)
322 {}
323
324 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
325 bfd_endian dwarf5_byte_order;
326 bool dwarf5_is_dwarf64;
327 bool augmentation_is_gdb;
328 uint8_t offset_size;
329 uint32_t cu_count = 0;
330 uint32_t tu_count, bucket_count, name_count;
331 const gdb_byte *cu_table_reordered, *tu_table_reordered;
332 const uint32_t *bucket_table_reordered, *hash_table_reordered;
333 const gdb_byte *name_table_string_offs_reordered;
334 const gdb_byte *name_table_entry_offs_reordered;
335 const gdb_byte *entry_pool;
336
337 struct index_val
338 {
339 ULONGEST dwarf_tag;
340 struct attr
341 {
342 /* Attribute name DW_IDX_*. */
343 ULONGEST dw_idx;
344
345 /* Attribute form DW_FORM_*. */
346 ULONGEST form;
347
348 /* Value if FORM is DW_FORM_implicit_const. */
349 LONGEST implicit_const;
350 };
351 std::vector<attr> attr_vec;
352 };
353
354 std::unordered_map<ULONGEST, index_val> abbrev_map;
355
356 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
357
358 /* Implementation of the mapped_index_base virtual interface, for
359 the name_components cache. */
360
361 const char *symbol_name_at (offset_type idx) const override
362 { return namei_to_name (idx); }
363
364 size_t symbol_name_count () const override
365 { return this->name_count; }
927aa2e7
JK
366};
367
95554aad
TT
368typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
369DEF_VEC_P (dwarf2_per_cu_ptr);
370
52059ffd
TT
371struct tu_stats
372{
373 int nr_uniq_abbrev_tables;
374 int nr_symtabs;
375 int nr_symtab_sharers;
376 int nr_stmt_less_type_units;
377 int nr_all_type_units_reallocs;
378};
379
9cdd5dbd
DE
380/* Collection of data recorded per objfile.
381 This hangs off of dwarf2_objfile_data_key. */
382
6502dd73
DJ
383struct dwarf2_per_objfile
384{
330cdd98
PA
385 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
386 dwarf2 section names, or is NULL if the standard ELF names are
387 used. */
388 dwarf2_per_objfile (struct objfile *objfile,
389 const dwarf2_debug_sections *names);
ae038cb0 390
330cdd98
PA
391 ~dwarf2_per_objfile ();
392
d6541620 393 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
330cdd98
PA
394
395 /* Free all cached compilation units. */
396 void free_cached_comp_units ();
397private:
398 /* This function is mapped across the sections and remembers the
399 offset and size of each of the debugging sections we are
400 interested in. */
401 void locate_sections (bfd *abfd, asection *sectp,
402 const dwarf2_debug_sections &names);
403
404public:
405 dwarf2_section_info info {};
406 dwarf2_section_info abbrev {};
407 dwarf2_section_info line {};
408 dwarf2_section_info loc {};
409 dwarf2_section_info loclists {};
410 dwarf2_section_info macinfo {};
411 dwarf2_section_info macro {};
412 dwarf2_section_info str {};
413 dwarf2_section_info line_str {};
414 dwarf2_section_info ranges {};
415 dwarf2_section_info rnglists {};
416 dwarf2_section_info addr {};
417 dwarf2_section_info frame {};
418 dwarf2_section_info eh_frame {};
419 dwarf2_section_info gdb_index {};
927aa2e7
JK
420 dwarf2_section_info debug_names {};
421 dwarf2_section_info debug_aranges {};
330cdd98
PA
422
423 VEC (dwarf2_section_info_def) *types = NULL;
8b70b953 424
be391dca 425 /* Back link. */
330cdd98 426 struct objfile *objfile = NULL;
be391dca 427
d467dd73 428 /* Table of all the compilation units. This is used to locate
10b3939b 429 the target compilation unit of a particular reference. */
330cdd98 430 struct dwarf2_per_cu_data **all_comp_units = NULL;
ae038cb0
DJ
431
432 /* The number of compilation units in ALL_COMP_UNITS. */
330cdd98 433 int n_comp_units = 0;
ae038cb0 434
1fd400ff 435 /* The number of .debug_types-related CUs. */
330cdd98 436 int n_type_units = 0;
1fd400ff 437
6aa5f3a6
DE
438 /* The number of elements allocated in all_type_units.
439 If there are skeleton-less TUs, we add them to all_type_units lazily. */
330cdd98 440 int n_allocated_type_units = 0;
6aa5f3a6 441
a2ce51a0
DE
442 /* The .debug_types-related CUs (TUs).
443 This is stored in malloc space because we may realloc it. */
330cdd98 444 struct signatured_type **all_type_units = NULL;
1fd400ff 445
f4dc4d17
DE
446 /* Table of struct type_unit_group objects.
447 The hash key is the DW_AT_stmt_list value. */
330cdd98 448 htab_t type_unit_groups {};
72dca2f5 449
348e048f
DE
450 /* A table mapping .debug_types signatures to its signatured_type entry.
451 This is NULL if the .debug_types section hasn't been read in yet. */
330cdd98 452 htab_t signatured_types {};
348e048f 453
f4dc4d17
DE
454 /* Type unit statistics, to see how well the scaling improvements
455 are doing. */
330cdd98 456 struct tu_stats tu_stats {};
f4dc4d17
DE
457
458 /* A chain of compilation units that are currently read in, so that
459 they can be freed later. */
330cdd98 460 dwarf2_per_cu_data *read_in_chain = NULL;
f4dc4d17 461
3019eac3
DE
462 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
463 This is NULL if the table hasn't been allocated yet. */
330cdd98 464 htab_t dwo_files {};
3019eac3 465
330cdd98
PA
466 /* True if we've checked for whether there is a DWP file. */
467 bool dwp_checked = false;
80626a55
DE
468
469 /* The DWP file if there is one, or NULL. */
330cdd98 470 struct dwp_file *dwp_file = NULL;
80626a55 471
36586728
TT
472 /* The shared '.dwz' file, if one exists. This is used when the
473 original data was compressed using 'dwz -m'. */
330cdd98 474 struct dwz_file *dwz_file = NULL;
36586728 475
330cdd98 476 /* A flag indicating whether this objfile has a section loaded at a
72dca2f5 477 VMA of 0. */
330cdd98 478 bool has_section_at_zero = false;
9291a0cd 479
ae2de4f8
DE
480 /* True if we are using the mapped index,
481 or we are faking it for OBJF_READNOW's sake. */
330cdd98 482 bool using_index = false;
9291a0cd 483
ae2de4f8 484 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
330cdd98 485 mapped_index *index_table = NULL;
98bfdba5 486
927aa2e7
JK
487 /* The mapped index, or NULL if .debug_names is missing or not being used. */
488 std::unique_ptr<mapped_debug_names> debug_names_table;
489
7b9f3c50 490 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
491 TUs typically share line table entries with a CU, so we maintain a
492 separate table of all line table entries to support the sharing.
493 Note that while there can be way more TUs than CUs, we've already
494 sorted all the TUs into "type unit groups", grouped by their
495 DW_AT_stmt_list value. Therefore the only sharing done here is with a
496 CU and its associated TU group if there is one. */
330cdd98 497 htab_t quick_file_names_table {};
7b9f3c50 498
98bfdba5
PA
499 /* Set during partial symbol reading, to prevent queueing of full
500 symbols. */
330cdd98 501 bool reading_partial_symbols = false;
673bfd45 502
dee91e82 503 /* Table mapping type DIEs to their struct type *.
673bfd45 504 This is NULL if not allocated yet.
02142a6c 505 The mapping is done via (CU/TU + DIE offset) -> type. */
330cdd98 506 htab_t die_type_hash {};
95554aad
TT
507
508 /* The CUs we recently read. */
330cdd98 509 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
527f3840
JK
510
511 /* Table containing line_header indexed by offset and offset_in_dwz. */
330cdd98 512 htab_t line_header_hash {};
bbf2f4df
PA
513
514 /* Table containing all filenames. This is an optional because the
515 table is lazily constructed on first access. */
516 gdb::optional<filename_seen_cache> filenames_cache;
6502dd73
DJ
517};
518
ed2dc618
SM
519/* Get the dwarf2_per_objfile associated to OBJFILE. */
520
521struct dwarf2_per_objfile *
522get_dwarf2_per_objfile (struct objfile *objfile)
523{
524 return ((struct dwarf2_per_objfile *)
525 objfile_data (objfile, dwarf2_objfile_data_key));
526}
527
528/* Set the dwarf2_per_objfile associated to OBJFILE. */
529
530void
531set_dwarf2_per_objfile (struct objfile *objfile,
532 struct dwarf2_per_objfile *dwarf2_per_objfile)
533{
534 gdb_assert (get_dwarf2_per_objfile (objfile) == NULL);
535 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
536}
c906108c 537
251d32d9 538/* Default names of the debugging sections. */
c906108c 539
233a11ab
CS
540/* Note that if the debugging section has been compressed, it might
541 have a name like .zdebug_info. */
542
9cdd5dbd
DE
543static const struct dwarf2_debug_sections dwarf2_elf_names =
544{
251d32d9
TG
545 { ".debug_info", ".zdebug_info" },
546 { ".debug_abbrev", ".zdebug_abbrev" },
547 { ".debug_line", ".zdebug_line" },
548 { ".debug_loc", ".zdebug_loc" },
43988095 549 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 550 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 551 { ".debug_macro", ".zdebug_macro" },
251d32d9 552 { ".debug_str", ".zdebug_str" },
43988095 553 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 554 { ".debug_ranges", ".zdebug_ranges" },
43988095 555 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 556 { ".debug_types", ".zdebug_types" },
3019eac3 557 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
558 { ".debug_frame", ".zdebug_frame" },
559 { ".eh_frame", NULL },
24d3216f 560 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
561 { ".debug_names", ".zdebug_names" },
562 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 563 23
251d32d9 564};
c906108c 565
80626a55 566/* List of DWO/DWP sections. */
3019eac3 567
80626a55 568static const struct dwop_section_names
3019eac3
DE
569{
570 struct dwarf2_section_names abbrev_dwo;
571 struct dwarf2_section_names info_dwo;
572 struct dwarf2_section_names line_dwo;
573 struct dwarf2_section_names loc_dwo;
43988095 574 struct dwarf2_section_names loclists_dwo;
09262596
DE
575 struct dwarf2_section_names macinfo_dwo;
576 struct dwarf2_section_names macro_dwo;
3019eac3
DE
577 struct dwarf2_section_names str_dwo;
578 struct dwarf2_section_names str_offsets_dwo;
579 struct dwarf2_section_names types_dwo;
80626a55
DE
580 struct dwarf2_section_names cu_index;
581 struct dwarf2_section_names tu_index;
3019eac3 582}
80626a55 583dwop_section_names =
3019eac3
DE
584{
585 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
586 { ".debug_info.dwo", ".zdebug_info.dwo" },
587 { ".debug_line.dwo", ".zdebug_line.dwo" },
588 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 589 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
590 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
591 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
592 { ".debug_str.dwo", ".zdebug_str.dwo" },
593 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
594 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
595 { ".debug_cu_index", ".zdebug_cu_index" },
596 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
597};
598
c906108c
SS
599/* local data types */
600
107d2387
AC
601/* The data in a compilation unit header, after target2host
602 translation, looks like this. */
c906108c 603struct comp_unit_head
a738430d 604{
c764a876 605 unsigned int length;
a738430d 606 short version;
a738430d
MK
607 unsigned char addr_size;
608 unsigned char signed_addr_p;
9c541725 609 sect_offset abbrev_sect_off;
57349743 610
a738430d
MK
611 /* Size of file offsets; either 4 or 8. */
612 unsigned int offset_size;
57349743 613
a738430d
MK
614 /* Size of the length field; either 4 or 12. */
615 unsigned int initial_length_size;
57349743 616
43988095
JK
617 enum dwarf_unit_type unit_type;
618
a738430d
MK
619 /* Offset to the first byte of this compilation unit header in the
620 .debug_info section, for resolving relative reference dies. */
9c541725 621 sect_offset sect_off;
57349743 622
d00adf39
DE
623 /* Offset to first die in this cu from the start of the cu.
624 This will be the first byte following the compilation unit header. */
9c541725 625 cu_offset first_die_cu_offset;
43988095
JK
626
627 /* 64-bit signature of this type unit - it is valid only for
628 UNIT_TYPE DW_UT_type. */
629 ULONGEST signature;
630
631 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 632 cu_offset type_cu_offset_in_tu;
a738430d 633};
c906108c 634
3da10d80
KS
635/* Type used for delaying computation of method physnames.
636 See comments for compute_delayed_physnames. */
637struct delayed_method_info
638{
639 /* The type to which the method is attached, i.e., its parent class. */
640 struct type *type;
641
642 /* The index of the method in the type's function fieldlists. */
643 int fnfield_index;
644
645 /* The index of the method in the fieldlist. */
646 int index;
647
648 /* The name of the DIE. */
649 const char *name;
650
651 /* The DIE associated with this method. */
652 struct die_info *die;
653};
654
e7c27a73
DJ
655/* Internal state when decoding a particular compilation unit. */
656struct dwarf2_cu
657{
fcd3b13d
SM
658 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
659 ~dwarf2_cu ();
660
661 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
662
d00adf39 663 /* The header of the compilation unit. */
fcd3b13d 664 struct comp_unit_head header {};
e142c38c 665
d00adf39 666 /* Base address of this compilation unit. */
fcd3b13d 667 CORE_ADDR base_address = 0;
d00adf39
DE
668
669 /* Non-zero if base_address has been set. */
fcd3b13d 670 int base_known = 0;
d00adf39 671
e142c38c 672 /* The language we are debugging. */
fcd3b13d
SM
673 enum language language = language_unknown;
674 const struct language_defn *language_defn = nullptr;
e142c38c 675
fcd3b13d 676 const char *producer = nullptr;
b0f35d58 677
e142c38c
DJ
678 /* The generic symbol table building routines have separate lists for
679 file scope symbols and all all other scopes (local scopes). So
680 we need to select the right one to pass to add_symbol_to_list().
681 We do it by keeping a pointer to the correct list in list_in_scope.
682
683 FIXME: The original dwarf code just treated the file scope as the
684 first local scope, and all other local scopes as nested local
685 scopes, and worked fine. Check to see if we really need to
686 distinguish these in buildsym.c. */
fcd3b13d 687 struct pending **list_in_scope = nullptr;
e142c38c 688
b64f50a1
JK
689 /* Hash table holding all the loaded partial DIEs
690 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 691 htab_t partial_dies = nullptr;
72bf9492
DJ
692
693 /* Storage for things with the same lifetime as this read-in compilation
694 unit, including partial DIEs. */
fcd3b13d 695 auto_obstack comp_unit_obstack;
72bf9492 696
ae038cb0
DJ
697 /* When multiple dwarf2_cu structures are living in memory, this field
698 chains them all together, so that they can be released efficiently.
699 We will probably also want a generation counter so that most-recently-used
700 compilation units are cached... */
fcd3b13d 701 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 702
69d751e3 703 /* Backlink to our per_cu entry. */
ae038cb0
DJ
704 struct dwarf2_per_cu_data *per_cu;
705
706 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 707 int last_used = 0;
ae038cb0 708
b64f50a1
JK
709 /* A hash table of DIE cu_offset for following references with
710 die_info->offset.sect_off as hash. */
fcd3b13d 711 htab_t die_hash = nullptr;
10b3939b
DJ
712
713 /* Full DIEs if read in. */
fcd3b13d 714 struct die_info *dies = nullptr;
10b3939b
DJ
715
716 /* A set of pointers to dwarf2_per_cu_data objects for compilation
717 units referenced by this one. Only set during full symbol processing;
718 partial symbol tables do not have dependencies. */
fcd3b13d 719 htab_t dependencies = nullptr;
10b3939b 720
cb1df416 721 /* Header data from the line table, during full symbol processing. */
fcd3b13d 722 struct line_header *line_header = nullptr;
4c8aa72d
PA
723 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
724 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
725 this is the DW_TAG_compile_unit die for this CU. We'll hold on
726 to the line header as long as this DIE is being processed. See
727 process_die_scope. */
fcd3b13d 728 die_info *line_header_die_owner = nullptr;
cb1df416 729
3da10d80
KS
730 /* A list of methods which need to have physnames computed
731 after all type information has been read. */
c89b44cd 732 std::vector<delayed_method_info> method_list;
3da10d80 733
96408a79 734 /* To be copied to symtab->call_site_htab. */
fcd3b13d 735 htab_t call_site_htab = nullptr;
96408a79 736
034e5797
DE
737 /* Non-NULL if this CU came from a DWO file.
738 There is an invariant here that is important to remember:
739 Except for attributes copied from the top level DIE in the "main"
740 (or "stub") file in preparation for reading the DWO file
741 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
742 Either there isn't a DWO file (in which case this is NULL and the point
743 is moot), or there is and either we're not going to read it (in which
744 case this is NULL) or there is and we are reading it (in which case this
745 is non-NULL). */
fcd3b13d 746 struct dwo_unit *dwo_unit = nullptr;
3019eac3
DE
747
748 /* The DW_AT_addr_base attribute if present, zero otherwise
749 (zero is a valid value though).
1dbab08b 750 Note this value comes from the Fission stub CU/TU's DIE. */
fcd3b13d 751 ULONGEST addr_base = 0;
3019eac3 752
2e3cf129
DE
753 /* The DW_AT_ranges_base attribute if present, zero otherwise
754 (zero is a valid value though).
1dbab08b 755 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 756 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
757 be used without needing to know whether DWO files are in use or not.
758 N.B. This does not apply to DW_AT_ranges appearing in
759 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
760 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
761 DW_AT_ranges_base *would* have to be applied, and we'd have to care
762 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 763 ULONGEST ranges_base = 0;
2e3cf129 764
ae038cb0
DJ
765 /* Mark used when releasing cached dies. */
766 unsigned int mark : 1;
767
8be455d7
JK
768 /* This CU references .debug_loc. See the symtab->locations_valid field.
769 This test is imperfect as there may exist optimized debug code not using
770 any location list and still facing inlining issues if handled as
771 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 772 unsigned int has_loclist : 1;
ba919b58 773
1b80a9fa
JK
774 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
775 if all the producer_is_* fields are valid. This information is cached
776 because profiling CU expansion showed excessive time spent in
777 producer_is_gxx_lt_4_6. */
ba919b58
TT
778 unsigned int checked_producer : 1;
779 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 780 unsigned int producer_is_gcc_lt_4_3 : 1;
5230b05a 781 unsigned int producer_is_icc_lt_14 : 1;
4d4ec4e5
TT
782
783 /* When set, the file that we're processing is known to have
784 debugging info for C++ namespaces. GCC 3.3.x did not produce
785 this information, but later versions do. */
786
787 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
788};
789
10b3939b
DJ
790/* Persistent data held for a compilation unit, even when not
791 processing it. We put a pointer to this structure in the
28dee7f5 792 read_symtab_private field of the psymtab. */
10b3939b 793
ae038cb0
DJ
794struct dwarf2_per_cu_data
795{
36586728 796 /* The start offset and length of this compilation unit.
45452591 797 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
798 initial_length_size.
799 If the DIE refers to a DWO file, this is always of the original die,
800 not the DWO file. */
9c541725 801 sect_offset sect_off;
36586728 802 unsigned int length;
ae038cb0 803
43988095
JK
804 /* DWARF standard version this data has been read from (such as 4 or 5). */
805 short dwarf_version;
806
ae038cb0
DJ
807 /* Flag indicating this compilation unit will be read in before
808 any of the current compilation units are processed. */
c764a876 809 unsigned int queued : 1;
ae038cb0 810
0d99eb77
DE
811 /* This flag will be set when reading partial DIEs if we need to load
812 absolutely all DIEs for this compilation unit, instead of just the ones
813 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
814 hash table and don't find it. */
815 unsigned int load_all_dies : 1;
816
0186c6a7
DE
817 /* Non-zero if this CU is from .debug_types.
818 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
819 this is non-zero. */
3019eac3
DE
820 unsigned int is_debug_types : 1;
821
36586728
TT
822 /* Non-zero if this CU is from the .dwz file. */
823 unsigned int is_dwz : 1;
824
a2ce51a0
DE
825 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
826 This flag is only valid if is_debug_types is true.
827 We can't read a CU directly from a DWO file: There are required
828 attributes in the stub. */
829 unsigned int reading_dwo_directly : 1;
830
7ee85ab1
DE
831 /* Non-zero if the TU has been read.
832 This is used to assist the "Stay in DWO Optimization" for Fission:
833 When reading a DWO, it's faster to read TUs from the DWO instead of
834 fetching them from random other DWOs (due to comdat folding).
835 If the TU has already been read, the optimization is unnecessary
836 (and unwise - we don't want to change where gdb thinks the TU lives
837 "midflight").
838 This flag is only valid if is_debug_types is true. */
839 unsigned int tu_read : 1;
840
3019eac3
DE
841 /* The section this CU/TU lives in.
842 If the DIE refers to a DWO file, this is always the original die,
843 not the DWO file. */
8a0459fd 844 struct dwarf2_section_info *section;
348e048f 845
17ea53c3 846 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
cc12ce38
DE
847 of the CU cache it gets reset to NULL again. This is left as NULL for
848 dummy CUs (a CU header, but nothing else). */
ae038cb0 849 struct dwarf2_cu *cu;
1c379e20 850
e3b94546
SM
851 /* The corresponding dwarf2_per_objfile. */
852 struct dwarf2_per_objfile *dwarf2_per_objfile;
9291a0cd 853
fffbe6a8
YQ
854 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
855 is active. Otherwise, the 'psymtab' field is active. */
9291a0cd
TT
856 union
857 {
858 /* The partial symbol table associated with this compilation unit,
95554aad 859 or NULL for unread partial units. */
9291a0cd
TT
860 struct partial_symtab *psymtab;
861
862 /* Data needed by the "quick" functions. */
863 struct dwarf2_per_cu_quick_data *quick;
864 } v;
95554aad 865
796a7ff8
DE
866 /* The CUs we import using DW_TAG_imported_unit. This is filled in
867 while reading psymtabs, used to compute the psymtab dependencies,
868 and then cleared. Then it is filled in again while reading full
869 symbols, and only deleted when the objfile is destroyed.
870
871 This is also used to work around a difference between the way gold
872 generates .gdb_index version <=7 and the way gdb does. Arguably this
873 is a gold bug. For symbols coming from TUs, gold records in the index
874 the CU that includes the TU instead of the TU itself. This breaks
875 dw2_lookup_symbol: It assumes that if the index says symbol X lives
876 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
877 will find X. Alas TUs live in their own symtab, so after expanding CU Y
878 we need to look in TU Z to find X. Fortunately, this is akin to
879 DW_TAG_imported_unit, so we just use the same mechanism: For
880 .gdb_index version <=7 this also records the TUs that the CU referred
881 to. Concurrently with this change gdb was modified to emit version 8
69d751e3
DE
882 indices so we only pay a price for gold generated indices.
883 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
796a7ff8 884 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
885};
886
348e048f
DE
887/* Entry in the signatured_types hash table. */
888
889struct signatured_type
890{
42e7ad6c 891 /* The "per_cu" object of this type.
ac9ec31b 892 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
893 N.B.: This is the first member so that it's easy to convert pointers
894 between them. */
895 struct dwarf2_per_cu_data per_cu;
896
3019eac3 897 /* The type's signature. */
348e048f
DE
898 ULONGEST signature;
899
3019eac3 900 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
901 If this TU is a DWO stub and the definition lives in a DWO file
902 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
903 cu_offset type_offset_in_tu;
904
905 /* Offset in the section of the type's DIE.
906 If the definition lives in a DWO file, this is the offset in the
907 .debug_types.dwo section.
908 The value is zero until the actual value is known.
909 Zero is otherwise not a valid section offset. */
910 sect_offset type_offset_in_section;
0186c6a7
DE
911
912 /* Type units are grouped by their DW_AT_stmt_list entry so that they
913 can share them. This points to the containing symtab. */
914 struct type_unit_group *type_unit_group;
ac9ec31b
DE
915
916 /* The type.
917 The first time we encounter this type we fully read it in and install it
918 in the symbol tables. Subsequent times we only need the type. */
919 struct type *type;
a2ce51a0
DE
920
921 /* Containing DWO unit.
922 This field is valid iff per_cu.reading_dwo_directly. */
923 struct dwo_unit *dwo_unit;
348e048f
DE
924};
925
0186c6a7
DE
926typedef struct signatured_type *sig_type_ptr;
927DEF_VEC_P (sig_type_ptr);
928
094b34ac
DE
929/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
930 This includes type_unit_group and quick_file_names. */
931
932struct stmt_list_hash
933{
934 /* The DWO unit this table is from or NULL if there is none. */
935 struct dwo_unit *dwo_unit;
936
937 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 938 sect_offset line_sect_off;
094b34ac
DE
939};
940
f4dc4d17
DE
941/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
942 an object of this type. */
943
944struct type_unit_group
945{
0186c6a7 946 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
947 To simplify things we create an artificial CU that "includes" all the
948 type units using this stmt_list so that the rest of the code still has
949 a "per_cu" handle on the symtab.
950 This PER_CU is recognized by having no section. */
8a0459fd 951#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
952 struct dwarf2_per_cu_data per_cu;
953
0186c6a7
DE
954 /* The TUs that share this DW_AT_stmt_list entry.
955 This is added to while parsing type units to build partial symtabs,
956 and is deleted afterwards and not used again. */
957 VEC (sig_type_ptr) *tus;
f4dc4d17 958
43f3e411 959 /* The compunit symtab.
094b34ac 960 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
961 so we create an essentially anonymous symtab as the compunit symtab. */
962 struct compunit_symtab *compunit_symtab;
f4dc4d17 963
094b34ac
DE
964 /* The data used to construct the hash key. */
965 struct stmt_list_hash hash;
f4dc4d17
DE
966
967 /* The number of symtabs from the line header.
968 The value here must match line_header.num_file_names. */
969 unsigned int num_symtabs;
970
971 /* The symbol tables for this TU (obtained from the files listed in
972 DW_AT_stmt_list).
973 WARNING: The order of entries here must match the order of entries
974 in the line header. After the first TU using this type_unit_group, the
975 line header for the subsequent TUs is recreated from this. This is done
976 because we need to use the same symtabs for each TU using the same
977 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
978 there's no guarantee the line header doesn't have duplicate entries. */
979 struct symtab **symtabs;
980};
981
73869dc2 982/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
983
984struct dwo_sections
985{
986 struct dwarf2_section_info abbrev;
3019eac3
DE
987 struct dwarf2_section_info line;
988 struct dwarf2_section_info loc;
43988095 989 struct dwarf2_section_info loclists;
09262596
DE
990 struct dwarf2_section_info macinfo;
991 struct dwarf2_section_info macro;
3019eac3
DE
992 struct dwarf2_section_info str;
993 struct dwarf2_section_info str_offsets;
80626a55
DE
994 /* In the case of a virtual DWO file, these two are unused. */
995 struct dwarf2_section_info info;
3019eac3
DE
996 VEC (dwarf2_section_info_def) *types;
997};
998
c88ee1f0 999/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
1000
1001struct dwo_unit
1002{
1003 /* Backlink to the containing struct dwo_file. */
1004 struct dwo_file *dwo_file;
1005
1006 /* The "id" that distinguishes this CU/TU.
1007 .debug_info calls this "dwo_id", .debug_types calls this "signature".
1008 Since signatures came first, we stick with it for consistency. */
1009 ULONGEST signature;
1010
1011 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 1012 struct dwarf2_section_info *section;
3019eac3 1013
9c541725
PA
1014 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
1015 sect_offset sect_off;
3019eac3
DE
1016 unsigned int length;
1017
1018 /* For types, offset in the type's DIE of the type defined by this TU. */
1019 cu_offset type_offset_in_tu;
1020};
1021
73869dc2
DE
1022/* include/dwarf2.h defines the DWP section codes.
1023 It defines a max value but it doesn't define a min value, which we
1024 use for error checking, so provide one. */
1025
1026enum dwp_v2_section_ids
1027{
1028 DW_SECT_MIN = 1
1029};
1030
80626a55 1031/* Data for one DWO file.
57d63ce2
DE
1032
1033 This includes virtual DWO files (a virtual DWO file is a DWO file as it
1034 appears in a DWP file). DWP files don't really have DWO files per se -
1035 comdat folding of types "loses" the DWO file they came from, and from
1036 a high level view DWP files appear to contain a mass of random types.
1037 However, to maintain consistency with the non-DWP case we pretend DWP
1038 files contain virtual DWO files, and we assign each TU with one virtual
1039 DWO file (generally based on the line and abbrev section offsets -
1040 a heuristic that seems to work in practice). */
3019eac3
DE
1041
1042struct dwo_file
1043{
0ac5b59e 1044 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
1045 For virtual DWO files the name is constructed from the section offsets
1046 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
1047 from related CU+TUs. */
0ac5b59e
DE
1048 const char *dwo_name;
1049
1050 /* The DW_AT_comp_dir attribute. */
1051 const char *comp_dir;
3019eac3 1052
80626a55
DE
1053 /* The bfd, when the file is open. Otherwise this is NULL.
1054 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
1055 bfd *dbfd;
3019eac3 1056
73869dc2
DE
1057 /* The sections that make up this DWO file.
1058 Remember that for virtual DWO files in DWP V2, these are virtual
1059 sections (for lack of a better name). */
3019eac3
DE
1060 struct dwo_sections sections;
1061
33c5cd75
DB
1062 /* The CUs in the file.
1063 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
1064 an extension to handle LLVM's Link Time Optimization output (where
1065 multiple source files may be compiled into a single object/dwo pair). */
1066 htab_t cus;
3019eac3
DE
1067
1068 /* Table of TUs in the file.
1069 Each element is a struct dwo_unit. */
1070 htab_t tus;
1071};
1072
80626a55
DE
1073/* These sections are what may appear in a DWP file. */
1074
1075struct dwp_sections
1076{
73869dc2 1077 /* These are used by both DWP version 1 and 2. */
80626a55
DE
1078 struct dwarf2_section_info str;
1079 struct dwarf2_section_info cu_index;
1080 struct dwarf2_section_info tu_index;
73869dc2
DE
1081
1082 /* These are only used by DWP version 2 files.
1083 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
1084 sections are referenced by section number, and are not recorded here.
1085 In DWP version 2 there is at most one copy of all these sections, each
1086 section being (effectively) comprised of the concatenation of all of the
1087 individual sections that exist in the version 1 format.
1088 To keep the code simple we treat each of these concatenated pieces as a
1089 section itself (a virtual section?). */
1090 struct dwarf2_section_info abbrev;
1091 struct dwarf2_section_info info;
1092 struct dwarf2_section_info line;
1093 struct dwarf2_section_info loc;
1094 struct dwarf2_section_info macinfo;
1095 struct dwarf2_section_info macro;
1096 struct dwarf2_section_info str_offsets;
1097 struct dwarf2_section_info types;
80626a55
DE
1098};
1099
73869dc2
DE
1100/* These sections are what may appear in a virtual DWO file in DWP version 1.
1101 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 1102
73869dc2 1103struct virtual_v1_dwo_sections
80626a55
DE
1104{
1105 struct dwarf2_section_info abbrev;
1106 struct dwarf2_section_info line;
1107 struct dwarf2_section_info loc;
1108 struct dwarf2_section_info macinfo;
1109 struct dwarf2_section_info macro;
1110 struct dwarf2_section_info str_offsets;
1111 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 1112 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
1113 struct dwarf2_section_info info_or_types;
1114};
1115
73869dc2
DE
1116/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
1117 In version 2, the sections of the DWO files are concatenated together
1118 and stored in one section of that name. Thus each ELF section contains
1119 several "virtual" sections. */
1120
1121struct virtual_v2_dwo_sections
1122{
1123 bfd_size_type abbrev_offset;
1124 bfd_size_type abbrev_size;
1125
1126 bfd_size_type line_offset;
1127 bfd_size_type line_size;
1128
1129 bfd_size_type loc_offset;
1130 bfd_size_type loc_size;
1131
1132 bfd_size_type macinfo_offset;
1133 bfd_size_type macinfo_size;
1134
1135 bfd_size_type macro_offset;
1136 bfd_size_type macro_size;
1137
1138 bfd_size_type str_offsets_offset;
1139 bfd_size_type str_offsets_size;
1140
1141 /* Each DWP hash table entry records one CU or one TU.
1142 That is recorded here, and copied to dwo_unit.section. */
1143 bfd_size_type info_or_types_offset;
1144 bfd_size_type info_or_types_size;
1145};
1146
80626a55
DE
1147/* Contents of DWP hash tables. */
1148
1149struct dwp_hash_table
1150{
73869dc2 1151 uint32_t version, nr_columns;
80626a55 1152 uint32_t nr_units, nr_slots;
73869dc2
DE
1153 const gdb_byte *hash_table, *unit_table;
1154 union
1155 {
1156 struct
1157 {
1158 const gdb_byte *indices;
1159 } v1;
1160 struct
1161 {
1162 /* This is indexed by column number and gives the id of the section
1163 in that column. */
1164#define MAX_NR_V2_DWO_SECTIONS \
1165 (1 /* .debug_info or .debug_types */ \
1166 + 1 /* .debug_abbrev */ \
1167 + 1 /* .debug_line */ \
1168 + 1 /* .debug_loc */ \
1169 + 1 /* .debug_str_offsets */ \
1170 + 1 /* .debug_macro or .debug_macinfo */)
1171 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1172 const gdb_byte *offsets;
1173 const gdb_byte *sizes;
1174 } v2;
1175 } section_pool;
80626a55
DE
1176};
1177
1178/* Data for one DWP file. */
1179
1180struct dwp_file
1181{
1182 /* Name of the file. */
1183 const char *name;
1184
73869dc2
DE
1185 /* File format version. */
1186 int version;
1187
93417882 1188 /* The bfd. */
80626a55
DE
1189 bfd *dbfd;
1190
1191 /* Section info for this file. */
1192 struct dwp_sections sections;
1193
57d63ce2 1194 /* Table of CUs in the file. */
80626a55
DE
1195 const struct dwp_hash_table *cus;
1196
1197 /* Table of TUs in the file. */
1198 const struct dwp_hash_table *tus;
1199
19ac8c2e
DE
1200 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1201 htab_t loaded_cus;
1202 htab_t loaded_tus;
80626a55 1203
73869dc2
DE
1204 /* Table to map ELF section numbers to their sections.
1205 This is only needed for the DWP V1 file format. */
80626a55
DE
1206 unsigned int num_sections;
1207 asection **elf_sections;
1208};
1209
36586728
TT
1210/* This represents a '.dwz' file. */
1211
1212struct dwz_file
1213{
1214 /* A dwz file can only contain a few sections. */
1215 struct dwarf2_section_info abbrev;
1216 struct dwarf2_section_info info;
1217 struct dwarf2_section_info str;
1218 struct dwarf2_section_info line;
1219 struct dwarf2_section_info macro;
2ec9a5e0 1220 struct dwarf2_section_info gdb_index;
927aa2e7 1221 struct dwarf2_section_info debug_names;
36586728
TT
1222
1223 /* The dwz's BFD. */
1224 bfd *dwz_bfd;
1225};
1226
0963b4bd
MS
1227/* Struct used to pass misc. parameters to read_die_and_children, et
1228 al. which are used for both .debug_info and .debug_types dies.
1229 All parameters here are unchanging for the life of the call. This
dee91e82 1230 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
1231
1232struct die_reader_specs
1233{
a32a8923 1234 /* The bfd of die_section. */
93311388
DE
1235 bfd* abfd;
1236
1237 /* The CU of the DIE we are parsing. */
1238 struct dwarf2_cu *cu;
1239
80626a55 1240 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
1241 struct dwo_file *dwo_file;
1242
dee91e82 1243 /* The section the die comes from.
3019eac3 1244 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
1245 struct dwarf2_section_info *die_section;
1246
1247 /* die_section->buffer. */
d521ce57 1248 const gdb_byte *buffer;
f664829e
DE
1249
1250 /* The end of the buffer. */
1251 const gdb_byte *buffer_end;
a2ce51a0
DE
1252
1253 /* The value of the DW_AT_comp_dir attribute. */
1254 const char *comp_dir;
685af9cd
TT
1255
1256 /* The abbreviation table to use when reading the DIEs. */
1257 struct abbrev_table *abbrev_table;
93311388
DE
1258};
1259
fd820528 1260/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 1261typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 1262 const gdb_byte *info_ptr,
dee91e82
DE
1263 struct die_info *comp_unit_die,
1264 int has_children,
1265 void *data);
1266
ecfb656c
PA
1267/* A 1-based directory index. This is a strong typedef to prevent
1268 accidentally using a directory index as a 0-based index into an
1269 array/vector. */
1270enum class dir_index : unsigned int {};
1271
1272/* Likewise, a 1-based file name index. */
1273enum class file_name_index : unsigned int {};
1274
52059ffd
TT
1275struct file_entry
1276{
fff8551c
PA
1277 file_entry () = default;
1278
ecfb656c 1279 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
1280 unsigned int mod_time_, unsigned int length_)
1281 : name (name_),
ecfb656c 1282 d_index (d_index_),
fff8551c
PA
1283 mod_time (mod_time_),
1284 length (length_)
1285 {}
1286
ecfb656c
PA
1287 /* Return the include directory at D_INDEX stored in LH. Returns
1288 NULL if D_INDEX is out of bounds. */
8c43009f
PA
1289 const char *include_dir (const line_header *lh) const;
1290
fff8551c
PA
1291 /* The file name. Note this is an observing pointer. The memory is
1292 owned by debug_line_buffer. */
1293 const char *name {};
1294
8c43009f 1295 /* The directory index (1-based). */
ecfb656c 1296 dir_index d_index {};
fff8551c
PA
1297
1298 unsigned int mod_time {};
1299
1300 unsigned int length {};
1301
1302 /* True if referenced by the Line Number Program. */
1303 bool included_p {};
1304
83769d0b 1305 /* The associated symbol table, if any. */
fff8551c 1306 struct symtab *symtab {};
52059ffd
TT
1307};
1308
debd256d
JB
1309/* The line number information for a compilation unit (found in the
1310 .debug_line section) begins with a "statement program header",
1311 which contains the following information. */
1312struct line_header
1313{
fff8551c
PA
1314 line_header ()
1315 : offset_in_dwz {}
1316 {}
1317
1318 /* Add an entry to the include directory table. */
1319 void add_include_dir (const char *include_dir);
1320
1321 /* Add an entry to the file name table. */
ecfb656c 1322 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
1323 unsigned int mod_time, unsigned int length);
1324
ecfb656c 1325 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
8c43009f 1326 is out of bounds. */
ecfb656c 1327 const char *include_dir_at (dir_index index) const
8c43009f 1328 {
ecfb656c
PA
1329 /* Convert directory index number (1-based) to vector index
1330 (0-based). */
1331 size_t vec_index = to_underlying (index) - 1;
1332
1333 if (vec_index >= include_dirs.size ())
8c43009f 1334 return NULL;
ecfb656c 1335 return include_dirs[vec_index];
8c43009f
PA
1336 }
1337
ecfb656c 1338 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
8c43009f 1339 is out of bounds. */
ecfb656c 1340 file_entry *file_name_at (file_name_index index)
8c43009f 1341 {
ecfb656c
PA
1342 /* Convert file name index number (1-based) to vector index
1343 (0-based). */
1344 size_t vec_index = to_underlying (index) - 1;
1345
1346 if (vec_index >= file_names.size ())
fff8551c 1347 return NULL;
ecfb656c 1348 return &file_names[vec_index];
fff8551c
PA
1349 }
1350
1351 /* Const version of the above. */
1352 const file_entry *file_name_at (unsigned int index) const
1353 {
1354 if (index >= file_names.size ())
8c43009f
PA
1355 return NULL;
1356 return &file_names[index];
1357 }
1358
527f3840 1359 /* Offset of line number information in .debug_line section. */
9c541725 1360 sect_offset sect_off {};
527f3840
JK
1361
1362 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1363 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1364
1365 unsigned int total_length {};
1366 unsigned short version {};
1367 unsigned int header_length {};
1368 unsigned char minimum_instruction_length {};
1369 unsigned char maximum_ops_per_instruction {};
1370 unsigned char default_is_stmt {};
1371 int line_base {};
1372 unsigned char line_range {};
1373 unsigned char opcode_base {};
debd256d
JB
1374
1375 /* standard_opcode_lengths[i] is the number of operands for the
1376 standard opcode whose value is i. This means that
1377 standard_opcode_lengths[0] is unused, and the last meaningful
1378 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1379 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1380
fff8551c
PA
1381 /* The include_directories table. Note these are observing
1382 pointers. The memory is owned by debug_line_buffer. */
1383 std::vector<const char *> include_dirs;
debd256d 1384
fff8551c
PA
1385 /* The file_names table. */
1386 std::vector<file_entry> file_names;
debd256d
JB
1387
1388 /* The start and end of the statement program following this
6502dd73 1389 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1390 const gdb_byte *statement_program_start {}, *statement_program_end {};
debd256d 1391};
c906108c 1392
fff8551c
PA
1393typedef std::unique_ptr<line_header> line_header_up;
1394
8c43009f
PA
1395const char *
1396file_entry::include_dir (const line_header *lh) const
1397{
ecfb656c 1398 return lh->include_dir_at (d_index);
8c43009f
PA
1399}
1400
c906108c 1401/* When we construct a partial symbol table entry we only
0963b4bd 1402 need this much information. */
c906108c
SS
1403struct partial_die_info
1404 {
72bf9492 1405 /* Offset of this DIE. */
9c541725 1406 sect_offset sect_off;
72bf9492
DJ
1407
1408 /* DWARF-2 tag for this DIE. */
1409 ENUM_BITFIELD(dwarf_tag) tag : 16;
1410
72bf9492
DJ
1411 /* Assorted flags describing the data found in this DIE. */
1412 unsigned int has_children : 1;
1413 unsigned int is_external : 1;
1414 unsigned int is_declaration : 1;
1415 unsigned int has_type : 1;
1416 unsigned int has_specification : 1;
1417 unsigned int has_pc_info : 1;
481860b3 1418 unsigned int may_be_inlined : 1;
72bf9492 1419
0c1b455e
TT
1420 /* This DIE has been marked DW_AT_main_subprogram. */
1421 unsigned int main_subprogram : 1;
1422
72bf9492
DJ
1423 /* Flag set if the SCOPE field of this structure has been
1424 computed. */
1425 unsigned int scope_set : 1;
1426
fa4028e9
JB
1427 /* Flag set if the DIE has a byte_size attribute. */
1428 unsigned int has_byte_size : 1;
1429
ff908ebf
AW
1430 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1431 unsigned int has_const_value : 1;
1432
98bfdba5
PA
1433 /* Flag set if any of the DIE's children are template arguments. */
1434 unsigned int has_template_arguments : 1;
1435
abc72ce4
DE
1436 /* Flag set if fixup_partial_die has been called on this die. */
1437 unsigned int fixup_called : 1;
1438
36586728
TT
1439 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1440 unsigned int is_dwz : 1;
1441
1442 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1443 unsigned int spec_is_dwz : 1;
1444
72bf9492 1445 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1446 sometimes a default name for unnamed DIEs. */
15d034d0 1447 const char *name;
72bf9492 1448
abc72ce4
DE
1449 /* The linkage name, if present. */
1450 const char *linkage_name;
1451
72bf9492
DJ
1452 /* The scope to prepend to our children. This is generally
1453 allocated on the comp_unit_obstack, so will disappear
1454 when this compilation unit leaves the cache. */
15d034d0 1455 const char *scope;
72bf9492 1456
95554aad
TT
1457 /* Some data associated with the partial DIE. The tag determines
1458 which field is live. */
1459 union
1460 {
1461 /* The location description associated with this DIE, if any. */
1462 struct dwarf_block *locdesc;
1463 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1464 sect_offset sect_off;
95554aad 1465 } d;
72bf9492
DJ
1466
1467 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
1468 CORE_ADDR lowpc;
1469 CORE_ADDR highpc;
72bf9492 1470
93311388 1471 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1472 DW_AT_sibling, if any. */
abc72ce4
DE
1473 /* NOTE: This member isn't strictly necessary, read_partial_die could
1474 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 1475 const gdb_byte *sibling;
72bf9492
DJ
1476
1477 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1478 DW_AT_specification (or DW_AT_abstract_origin or
1479 DW_AT_extension). */
b64f50a1 1480 sect_offset spec_offset;
72bf9492
DJ
1481
1482 /* Pointers to this DIE's parent, first child, and next sibling,
1483 if any. */
1484 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
1485 };
1486
0963b4bd 1487/* This data structure holds the information of an abbrev. */
c906108c
SS
1488struct abbrev_info
1489 {
1490 unsigned int number; /* number identifying abbrev */
1491 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1492 unsigned short has_children; /* boolean */
1493 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1494 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1495 struct abbrev_info *next; /* next in chain */
1496 };
1497
1498struct attr_abbrev
1499 {
9d25dd43
DE
1500 ENUM_BITFIELD(dwarf_attribute) name : 16;
1501 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1502
1503 /* It is valid only if FORM is DW_FORM_implicit_const. */
1504 LONGEST implicit_const;
c906108c
SS
1505 };
1506
433df2d4
DE
1507/* Size of abbrev_table.abbrev_hash_table. */
1508#define ABBREV_HASH_SIZE 121
1509
1510/* Top level data structure to contain an abbreviation table. */
1511
1512struct abbrev_table
1513{
685af9cd
TT
1514 explicit abbrev_table (sect_offset off)
1515 : sect_off (off)
1516 {
4a17f768 1517 m_abbrevs =
685af9cd 1518 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
4a17f768 1519 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
685af9cd
TT
1520 }
1521
1522 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1523
1524 /* Allocate space for a struct abbrev_info object in
1525 ABBREV_TABLE. */
1526 struct abbrev_info *alloc_abbrev ();
1527
1528 /* Add an abbreviation to the table. */
1529 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1530
1531 /* Look up an abbrev in the table.
1532 Returns NULL if the abbrev is not found. */
1533
1534 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1535
1536
f4dc4d17
DE
1537 /* Where the abbrev table came from.
1538 This is used as a sanity check when the table is used. */
685af9cd 1539 const sect_offset sect_off;
433df2d4
DE
1540
1541 /* Storage for the abbrev table. */
685af9cd 1542 auto_obstack abbrev_obstack;
433df2d4 1543
4a17f768
YQ
1544private:
1545
433df2d4
DE
1546 /* Hash table of abbrevs.
1547 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1548 It could be statically allocated, but the previous code didn't so we
1549 don't either. */
4a17f768 1550 struct abbrev_info **m_abbrevs;
433df2d4
DE
1551};
1552
685af9cd
TT
1553typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1554
0963b4bd 1555/* Attributes have a name and a value. */
b60c80d6
DJ
1556struct attribute
1557 {
9d25dd43 1558 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1559 ENUM_BITFIELD(dwarf_form) form : 15;
1560
1561 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1562 field should be in u.str (existing only for DW_STRING) but it is kept
1563 here for better struct attribute alignment. */
1564 unsigned int string_is_canonical : 1;
1565
b60c80d6
DJ
1566 union
1567 {
15d034d0 1568 const char *str;
b60c80d6 1569 struct dwarf_block *blk;
43bbcdc2
PH
1570 ULONGEST unsnd;
1571 LONGEST snd;
b60c80d6 1572 CORE_ADDR addr;
ac9ec31b 1573 ULONGEST signature;
b60c80d6
DJ
1574 }
1575 u;
1576 };
1577
0963b4bd 1578/* This data structure holds a complete die structure. */
c906108c
SS
1579struct die_info
1580 {
76815b17
DE
1581 /* DWARF-2 tag for this DIE. */
1582 ENUM_BITFIELD(dwarf_tag) tag : 16;
1583
1584 /* Number of attributes */
98bfdba5
PA
1585 unsigned char num_attrs;
1586
1587 /* True if we're presently building the full type name for the
1588 type derived from this DIE. */
1589 unsigned char building_fullname : 1;
76815b17 1590
adde2bff
DE
1591 /* True if this die is in process. PR 16581. */
1592 unsigned char in_process : 1;
1593
76815b17
DE
1594 /* Abbrev number */
1595 unsigned int abbrev;
1596
93311388 1597 /* Offset in .debug_info or .debug_types section. */
9c541725 1598 sect_offset sect_off;
78ba4af6
JB
1599
1600 /* The dies in a compilation unit form an n-ary tree. PARENT
1601 points to this die's parent; CHILD points to the first child of
1602 this node; and all the children of a given node are chained
4950bc1c 1603 together via their SIBLING fields. */
639d11d3
DC
1604 struct die_info *child; /* Its first child, if any. */
1605 struct die_info *sibling; /* Its next sibling, if any. */
1606 struct die_info *parent; /* Its parent, if any. */
c906108c 1607
b60c80d6
DJ
1608 /* An array of attributes, with NUM_ATTRS elements. There may be
1609 zero, but it's not common and zero-sized arrays are not
1610 sufficiently portable C. */
1611 struct attribute attrs[1];
c906108c
SS
1612 };
1613
0963b4bd 1614/* Get at parts of an attribute structure. */
c906108c
SS
1615
1616#define DW_STRING(attr) ((attr)->u.str)
8285870a 1617#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1618#define DW_UNSND(attr) ((attr)->u.unsnd)
1619#define DW_BLOCK(attr) ((attr)->u.blk)
1620#define DW_SND(attr) ((attr)->u.snd)
1621#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1622#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1623
0963b4bd 1624/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1625struct dwarf_block
1626 {
56eb65bd 1627 size_t size;
1d6edc3c
JK
1628
1629 /* Valid only if SIZE is not zero. */
d521ce57 1630 const gdb_byte *data;
c906108c
SS
1631 };
1632
c906108c
SS
1633#ifndef ATTR_ALLOC_CHUNK
1634#define ATTR_ALLOC_CHUNK 4
1635#endif
1636
c906108c
SS
1637/* Allocate fields for structs, unions and enums in this size. */
1638#ifndef DW_FIELD_ALLOC_CHUNK
1639#define DW_FIELD_ALLOC_CHUNK 4
1640#endif
1641
c906108c
SS
1642/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1643 but this would require a corresponding change in unpack_field_as_long
1644 and friends. */
1645static int bits_per_byte = 8;
1646
52059ffd
TT
1647struct nextfield
1648{
1649 struct nextfield *next;
1650 int accessibility;
1651 int virtuality;
1652 struct field field;
1653};
1654
1655struct nextfnfield
1656{
1657 struct nextfnfield *next;
1658 struct fn_field fnfield;
1659};
1660
1661struct fnfieldlist
1662{
1663 const char *name;
1664 int length;
1665 struct nextfnfield *head;
1666};
1667
883fd55a 1668struct decl_field_list
52059ffd 1669{
883fd55a
KS
1670 struct decl_field field;
1671 struct decl_field_list *next;
52059ffd
TT
1672};
1673
c906108c
SS
1674/* The routines that read and process dies for a C struct or C++ class
1675 pass lists of data member fields and lists of member function fields
1676 in an instance of a field_info structure, as defined below. */
1677struct field_info
c5aa993b 1678 {
0963b4bd 1679 /* List of data member and baseclasses fields. */
52059ffd 1680 struct nextfield *fields, *baseclasses;
c906108c 1681
7d0ccb61 1682 /* Number of fields (including baseclasses). */
c5aa993b 1683 int nfields;
c906108c 1684
c5aa993b
JM
1685 /* Number of baseclasses. */
1686 int nbaseclasses;
c906108c 1687
c5aa993b
JM
1688 /* Set if the accesibility of one of the fields is not public. */
1689 int non_public_fields;
c906108c 1690
c5aa993b
JM
1691 /* Member function fieldlist array, contains name of possibly overloaded
1692 member function, number of overloaded member functions and a pointer
1693 to the head of the member function field chain. */
52059ffd 1694 struct fnfieldlist *fnfieldlists;
c906108c 1695
c5aa993b
JM
1696 /* Number of entries in the fnfieldlists array. */
1697 int nfnfields;
98751a41
JK
1698
1699 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1700 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
883fd55a 1701 struct decl_field_list *typedef_field_list;
98751a41 1702 unsigned typedef_field_list_count;
883fd55a
KS
1703
1704 /* Nested types defined by this class and the number of elements in this
1705 list. */
1706 struct decl_field_list *nested_types_list;
1707 unsigned nested_types_list_count;
c5aa993b 1708 };
c906108c 1709
10b3939b
DJ
1710/* One item on the queue of compilation units to read in full symbols
1711 for. */
1712struct dwarf2_queue_item
1713{
1714 struct dwarf2_per_cu_data *per_cu;
95554aad 1715 enum language pretend_language;
10b3939b
DJ
1716 struct dwarf2_queue_item *next;
1717};
1718
1719/* The current queue. */
1720static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1721
ae038cb0
DJ
1722/* Loaded secondary compilation units are kept in memory until they
1723 have not been referenced for the processing of this many
1724 compilation units. Set this to zero to disable caching. Cache
1725 sizes of up to at least twenty will improve startup time for
1726 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1727static int dwarf_max_cache_age = 5;
920d2a44 1728static void
b4f54984
DE
1729show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1730 struct cmd_list_element *c, const char *value)
920d2a44 1731{
3e43a32a 1732 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1733 "DWARF compilation units is %s.\n"),
920d2a44
AC
1734 value);
1735}
4390d890 1736\f
c906108c
SS
1737/* local function prototypes */
1738
a32a8923
DE
1739static const char *get_section_name (const struct dwarf2_section_info *);
1740
1741static const char *get_section_file_name (const struct dwarf2_section_info *);
1742
918dd910
JK
1743static void dwarf2_find_base_address (struct die_info *die,
1744 struct dwarf2_cu *cu);
1745
0018ea6f
DE
1746static struct partial_symtab *create_partial_symtab
1747 (struct dwarf2_per_cu_data *per_cu, const char *name);
1748
f1902523
JK
1749static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1750 const gdb_byte *info_ptr,
1751 struct die_info *type_unit_die,
1752 int has_children, void *data);
1753
ed2dc618
SM
1754static void dwarf2_build_psymtabs_hard
1755 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1756
72bf9492
DJ
1757static void scan_partial_symbols (struct partial_die_info *,
1758 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1759 int, struct dwarf2_cu *);
c906108c 1760
72bf9492
DJ
1761static void add_partial_symbol (struct partial_die_info *,
1762 struct dwarf2_cu *);
63d06c5c 1763
72bf9492
DJ
1764static void add_partial_namespace (struct partial_die_info *pdi,
1765 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1766 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1767
5d7cb8df 1768static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1769 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1770 struct dwarf2_cu *cu);
1771
72bf9492
DJ
1772static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1773 struct dwarf2_cu *cu);
91c24f0a 1774
bc30ff58
JB
1775static void add_partial_subprogram (struct partial_die_info *pdi,
1776 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1777 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1778
257e7a09
YQ
1779static void dwarf2_read_symtab (struct partial_symtab *,
1780 struct objfile *);
c906108c 1781
a14ed312 1782static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1783
685af9cd 1784static abbrev_table_up abbrev_table_read_table
ed2dc618
SM
1785 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1786 sect_offset);
433df2d4 1787
d521ce57 1788static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1789
dee91e82 1790static struct partial_die_info *load_partial_dies
d521ce57 1791 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1792
d521ce57
TT
1793static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1794 struct partial_die_info *,
1795 struct abbrev_info *,
1796 unsigned int,
1797 const gdb_byte *);
c906108c 1798
36586728 1799static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1800 struct dwarf2_cu *);
72bf9492
DJ
1801
1802static void fixup_partial_die (struct partial_die_info *,
1803 struct dwarf2_cu *);
1804
d521ce57
TT
1805static const gdb_byte *read_attribute (const struct die_reader_specs *,
1806 struct attribute *, struct attr_abbrev *,
1807 const gdb_byte *);
a8329558 1808
a1855c1d 1809static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1810
a1855c1d 1811static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1812
a1855c1d 1813static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1814
a1855c1d 1815static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1816
a1855c1d 1817static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1818
d521ce57 1819static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1820 unsigned int *);
c906108c 1821
d521ce57 1822static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1823
1824static LONGEST read_checked_initial_length_and_offset
d521ce57 1825 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1826 unsigned int *, unsigned int *);
613e1657 1827
d521ce57
TT
1828static LONGEST read_offset (bfd *, const gdb_byte *,
1829 const struct comp_unit_head *,
c764a876
DE
1830 unsigned int *);
1831
d521ce57 1832static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1833
ed2dc618
SM
1834static sect_offset read_abbrev_offset
1835 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1836 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1837
d521ce57 1838static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1839
d521ce57 1840static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1841
ed2dc618
SM
1842static const char *read_indirect_string
1843 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1844 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1845
ed2dc618
SM
1846static const char *read_indirect_line_string
1847 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1848 const struct comp_unit_head *, unsigned int *);
36586728 1849
ed2dc618
SM
1850static const char *read_indirect_string_at_offset
1851 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1852 LONGEST str_offset);
927aa2e7 1853
ed2dc618
SM
1854static const char *read_indirect_string_from_dwz
1855 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1856
d521ce57 1857static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1858
d521ce57
TT
1859static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1860 const gdb_byte *,
3019eac3
DE
1861 unsigned int *);
1862
d521ce57 1863static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1864 ULONGEST str_index);
3019eac3 1865
e142c38c 1866static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1867
e142c38c
DJ
1868static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1869 struct dwarf2_cu *);
c906108c 1870
348e048f 1871static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1872 unsigned int);
348e048f 1873
7d45c7c3
KB
1874static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1875 struct dwarf2_cu *cu);
1876
05cf31d1
JB
1877static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1878 struct dwarf2_cu *cu);
1879
e142c38c 1880static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1881
e142c38c 1882static struct die_info *die_specification (struct die_info *die,
f2f0e013 1883 struct dwarf2_cu **);
63d06c5c 1884
9c541725 1885static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1886 struct dwarf2_cu *cu);
debd256d 1887
f3f5162e 1888static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1889 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1890 CORE_ADDR, int decode_mapping);
c906108c 1891
4d663531 1892static void dwarf2_start_subfile (const char *, const char *);
c906108c 1893
43f3e411
DE
1894static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1895 const char *, const char *,
1896 CORE_ADDR);
f4dc4d17 1897
a14ed312 1898static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1899 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1900
ff39bb5e 1901static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1902 struct dwarf2_cu *);
c906108c 1903
ff39bb5e 1904static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1905 struct type *type,
1906 const char *name,
1907 struct obstack *obstack,
12df843f 1908 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1909 const gdb_byte **bytes,
98bfdba5 1910 struct dwarf2_locexpr_baton **baton);
2df3850c 1911
e7c27a73 1912static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1913
b4ba55a1
JB
1914static int need_gnat_info (struct dwarf2_cu *);
1915
3e43a32a
MS
1916static struct type *die_descriptive_type (struct die_info *,
1917 struct dwarf2_cu *);
b4ba55a1
JB
1918
1919static void set_descriptive_type (struct type *, struct die_info *,
1920 struct dwarf2_cu *);
1921
e7c27a73
DJ
1922static struct type *die_containing_type (struct die_info *,
1923 struct dwarf2_cu *);
c906108c 1924
ff39bb5e 1925static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1926 struct dwarf2_cu *);
c906108c 1927
f792889a 1928static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1929
673bfd45
DE
1930static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1931
0d5cff50 1932static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1933
6e70227d 1934static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1935 const char *suffix, int physname,
1936 struct dwarf2_cu *cu);
63d06c5c 1937
e7c27a73 1938static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1939
348e048f
DE
1940static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1941
e7c27a73 1942static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1943
e7c27a73 1944static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1945
96408a79
SA
1946static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1947
71a3c369
TT
1948static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1949
ff013f42
JK
1950static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1951 struct dwarf2_cu *, struct partial_symtab *);
1952
3a2b436a 1953/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1954 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1955enum pc_bounds_kind
1956{
e385593e 1957 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1958 PC_BOUNDS_NOT_PRESENT,
1959
e385593e
JK
1960 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1961 were present but they do not form a valid range of PC addresses. */
1962 PC_BOUNDS_INVALID,
1963
3a2b436a
JK
1964 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1965 PC_BOUNDS_RANGES,
1966
1967 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1968 PC_BOUNDS_HIGH_LOW,
1969};
1970
1971static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1972 CORE_ADDR *, CORE_ADDR *,
1973 struct dwarf2_cu *,
1974 struct partial_symtab *);
c906108c 1975
fae299cd
DC
1976static void get_scope_pc_bounds (struct die_info *,
1977 CORE_ADDR *, CORE_ADDR *,
1978 struct dwarf2_cu *);
1979
801e3a5b
JB
1980static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1981 CORE_ADDR, struct dwarf2_cu *);
1982
a14ed312 1983static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1984 struct dwarf2_cu *);
c906108c 1985
a14ed312 1986static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1987 struct type *, struct dwarf2_cu *);
c906108c 1988
a14ed312 1989static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1990 struct die_info *, struct type *,
e7c27a73 1991 struct dwarf2_cu *);
c906108c 1992
a14ed312 1993static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1994 struct type *,
1995 struct dwarf2_cu *);
c906108c 1996
134d01f1 1997static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1998
e7c27a73 1999static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 2000
e7c27a73 2001static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 2002
5d7cb8df
JK
2003static void read_module (struct die_info *die, struct dwarf2_cu *cu);
2004
22cee43f
PMR
2005static struct using_direct **using_directives (enum language);
2006
27aa8d6a
SW
2007static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
2008
74921315
KS
2009static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
2010
f55ee35c
JK
2011static struct type *read_module_type (struct die_info *die,
2012 struct dwarf2_cu *cu);
2013
38d518c9 2014static const char *namespace_name (struct die_info *die,
e142c38c 2015 int *is_anonymous, struct dwarf2_cu *);
38d518c9 2016
134d01f1 2017static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 2018
e7c27a73 2019static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 2020
6e70227d 2021static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
2022 struct dwarf2_cu *);
2023
bf6af496 2024static struct die_info *read_die_and_siblings_1
d521ce57 2025 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 2026 struct die_info *);
639d11d3 2027
dee91e82 2028static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
2029 const gdb_byte *info_ptr,
2030 const gdb_byte **new_info_ptr,
639d11d3
DC
2031 struct die_info *parent);
2032
d521ce57
TT
2033static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
2034 struct die_info **, const gdb_byte *,
2035 int *, int);
3019eac3 2036
d521ce57
TT
2037static const gdb_byte *read_full_die (const struct die_reader_specs *,
2038 struct die_info **, const gdb_byte *,
2039 int *);
93311388 2040
e7c27a73 2041static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 2042
15d034d0
TT
2043static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
2044 struct obstack *);
71c25dea 2045
15d034d0 2046static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 2047
15d034d0 2048static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
2049 struct die_info *die,
2050 struct dwarf2_cu *cu);
2051
ca69b9e6
DE
2052static const char *dwarf2_physname (const char *name, struct die_info *die,
2053 struct dwarf2_cu *cu);
2054
e142c38c 2055static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 2056 struct dwarf2_cu **);
9219021c 2057
f39c6ffd 2058static const char *dwarf_tag_name (unsigned int);
c906108c 2059
f39c6ffd 2060static const char *dwarf_attr_name (unsigned int);
c906108c 2061
f39c6ffd 2062static const char *dwarf_form_name (unsigned int);
c906108c 2063
a121b7c1 2064static const char *dwarf_bool_name (unsigned int);
c906108c 2065
f39c6ffd 2066static const char *dwarf_type_encoding_name (unsigned int);
c906108c 2067
f9aca02d 2068static struct die_info *sibling_die (struct die_info *);
c906108c 2069
d97bc12b
DE
2070static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
2071
2072static void dump_die_for_error (struct die_info *);
2073
2074static void dump_die_1 (struct ui_file *, int level, int max_level,
2075 struct die_info *);
c906108c 2076
d97bc12b 2077/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 2078
51545339 2079static void store_in_ref_table (struct die_info *,
10b3939b 2080 struct dwarf2_cu *);
c906108c 2081
ff39bb5e 2082static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 2083
ff39bb5e 2084static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 2085
348e048f 2086static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 2087 const struct attribute *,
348e048f
DE
2088 struct dwarf2_cu **);
2089
10b3939b 2090static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 2091 const struct attribute *,
f2f0e013 2092 struct dwarf2_cu **);
c906108c 2093
348e048f 2094static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 2095 const struct attribute *,
348e048f
DE
2096 struct dwarf2_cu **);
2097
ac9ec31b
DE
2098static struct type *get_signatured_type (struct die_info *, ULONGEST,
2099 struct dwarf2_cu *);
2100
2101static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 2102 const struct attribute *,
ac9ec31b
DE
2103 struct dwarf2_cu *);
2104
e5fe5e75 2105static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 2106
52dc124a 2107static void read_signatured_type (struct signatured_type *);
348e048f 2108
63e43d3a
PMR
2109static int attr_to_dynamic_prop (const struct attribute *attr,
2110 struct die_info *die, struct dwarf2_cu *cu,
2111 struct dynamic_prop *prop);
2112
c906108c
SS
2113/* memory allocation interface */
2114
7b5a2f43 2115static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 2116
b60c80d6 2117static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 2118
43f3e411 2119static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 2120
6e5a29e1 2121static int attr_form_is_block (const struct attribute *);
8e19ed76 2122
6e5a29e1 2123static int attr_form_is_section_offset (const struct attribute *);
3690dd37 2124
6e5a29e1 2125static int attr_form_is_constant (const struct attribute *);
3690dd37 2126
6e5a29e1 2127static int attr_form_is_ref (const struct attribute *);
7771576e 2128
8cf6f0b1
TT
2129static void fill_in_loclist_baton (struct dwarf2_cu *cu,
2130 struct dwarf2_loclist_baton *baton,
ff39bb5e 2131 const struct attribute *attr);
8cf6f0b1 2132
ff39bb5e 2133static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 2134 struct symbol *sym,
f1e6e072
TT
2135 struct dwarf2_cu *cu,
2136 int is_block);
4c2df51b 2137
d521ce57
TT
2138static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
2139 const gdb_byte *info_ptr,
2140 struct abbrev_info *abbrev);
4bb7a0a7 2141
72bf9492
DJ
2142static hashval_t partial_die_hash (const void *item);
2143
2144static int partial_die_eq (const void *item_lhs, const void *item_rhs);
2145
ae038cb0 2146static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
2147 (sect_offset sect_off, unsigned int offset_in_dwz,
2148 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 2149
9816fde3 2150static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
2151 struct die_info *comp_unit_die,
2152 enum language pretend_language);
93311388 2153
ae038cb0
DJ
2154static void free_cached_comp_units (void *);
2155
ed2dc618 2156static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 2157
dee91e82 2158static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 2159
f792889a
DJ
2160static struct type *set_die_type (struct die_info *, struct type *,
2161 struct dwarf2_cu *);
1c379e20 2162
ed2dc618 2163static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 2164
ed2dc618 2165static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 2166
95554aad
TT
2167static void load_full_comp_unit (struct dwarf2_per_cu_data *,
2168 enum language);
10b3939b 2169
95554aad
TT
2170static void process_full_comp_unit (struct dwarf2_per_cu_data *,
2171 enum language);
10b3939b 2172
f4dc4d17
DE
2173static void process_full_type_unit (struct dwarf2_per_cu_data *,
2174 enum language);
2175
10b3939b
DJ
2176static void dwarf2_add_dependence (struct dwarf2_cu *,
2177 struct dwarf2_per_cu_data *);
2178
ae038cb0
DJ
2179static void dwarf2_mark (struct dwarf2_cu *);
2180
2181static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
2182
b64f50a1 2183static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 2184 struct dwarf2_per_cu_data *);
673bfd45 2185
f792889a 2186static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 2187
9291a0cd
TT
2188static void dwarf2_release_queue (void *dummy);
2189
95554aad
TT
2190static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
2191 enum language pretend_language);
2192
ed2dc618 2193static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 2194
d721ba37
PA
2195/* The return type of find_file_and_directory. Note, the enclosed
2196 string pointers are only valid while this object is valid. */
2197
2198struct file_and_directory
2199{
2200 /* The filename. This is never NULL. */
2201 const char *name;
2202
2203 /* The compilation directory. NULL if not known. If we needed to
2204 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2205 points directly to the DW_AT_comp_dir string attribute owned by
2206 the obstack that owns the DIE. */
2207 const char *comp_dir;
2208
2209 /* If we needed to build a new string for comp_dir, this is what
2210 owns the storage. */
2211 std::string comp_dir_storage;
2212};
2213
2214static file_and_directory find_file_and_directory (struct die_info *die,
2215 struct dwarf2_cu *cu);
9291a0cd
TT
2216
2217static char *file_full_name (int file, struct line_header *lh,
2218 const char *comp_dir);
2219
43988095
JK
2220/* Expected enum dwarf_unit_type for read_comp_unit_head. */
2221enum class rcuh_kind { COMPILE, TYPE };
2222
d521ce57 2223static const gdb_byte *read_and_check_comp_unit_head
ed2dc618
SM
2224 (struct dwarf2_per_objfile* dwarf2_per_objfile,
2225 struct comp_unit_head *header,
36586728 2226 struct dwarf2_section_info *section,
d521ce57 2227 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 2228 rcuh_kind section_kind);
36586728 2229
fd820528 2230static void init_cutu_and_read_dies
f4dc4d17
DE
2231 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2232 int use_existing_cu, int keep,
3019eac3
DE
2233 die_reader_func_ftype *die_reader_func, void *data);
2234
dee91e82
DE
2235static void init_cutu_and_read_dies_simple
2236 (struct dwarf2_per_cu_data *this_cu,
2237 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 2238
673bfd45 2239static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 2240
3019eac3
DE
2241static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2242
57d63ce2 2243static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
2244 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2245 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 2246 ULONGEST signature, int is_debug_types);
a2ce51a0 2247
ed2dc618
SM
2248static struct dwp_file *get_dwp_file
2249 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 2250
3019eac3 2251static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 2252 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
2253
2254static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 2255 (struct signatured_type *, const char *, const char *);
3019eac3 2256
89e63ee4
DE
2257static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2258
3019eac3
DE
2259static void free_dwo_file_cleanup (void *);
2260
ed2dc618
SM
2261struct free_dwo_file_cleanup_data
2262{
2263 struct dwo_file *dwo_file;
2264 struct dwarf2_per_objfile *dwarf2_per_objfile;
2265};
2266
2267static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 2268
1b80a9fa 2269static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2270
2271static void free_line_header_voidp (void *arg);
4390d890
DE
2272\f
2273/* Various complaints about symbol reading that don't abort the process. */
2274
2275static void
2276dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2277{
2278 complaint (&symfile_complaints,
2279 _("statement list doesn't fit in .debug_line section"));
2280}
2281
2282static void
2283dwarf2_debug_line_missing_file_complaint (void)
2284{
2285 complaint (&symfile_complaints,
2286 _(".debug_line section has line data without a file"));
2287}
2288
2289static void
2290dwarf2_debug_line_missing_end_sequence_complaint (void)
2291{
2292 complaint (&symfile_complaints,
2293 _(".debug_line section has line "
2294 "program sequence without an end"));
2295}
2296
2297static void
2298dwarf2_complex_location_expr_complaint (void)
2299{
2300 complaint (&symfile_complaints, _("location expression too complex"));
2301}
2302
2303static void
2304dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2305 int arg3)
2306{
2307 complaint (&symfile_complaints,
2308 _("const value length mismatch for '%s', got %d, expected %d"),
2309 arg1, arg2, arg3);
2310}
2311
2312static void
2313dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2314{
2315 complaint (&symfile_complaints,
2316 _("debug info runs off end of %s section"
2317 " [in module %s]"),
a32a8923
DE
2318 get_section_name (section),
2319 get_section_file_name (section));
4390d890 2320}
1b80a9fa 2321
4390d890
DE
2322static void
2323dwarf2_macro_malformed_definition_complaint (const char *arg1)
2324{
2325 complaint (&symfile_complaints,
2326 _("macro debug info contains a "
2327 "malformed macro definition:\n`%s'"),
2328 arg1);
2329}
2330
2331static void
2332dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2333{
2334 complaint (&symfile_complaints,
2335 _("invalid attribute class or form for '%s' in '%s'"),
2336 arg1, arg2);
2337}
527f3840
JK
2338
2339/* Hash function for line_header_hash. */
2340
2341static hashval_t
2342line_header_hash (const struct line_header *ofs)
2343{
9c541725 2344 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2345}
2346
2347/* Hash function for htab_create_alloc_ex for line_header_hash. */
2348
2349static hashval_t
2350line_header_hash_voidp (const void *item)
2351{
9a3c8263 2352 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2353
2354 return line_header_hash (ofs);
2355}
2356
2357/* Equality function for line_header_hash. */
2358
2359static int
2360line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2361{
9a3c8263
SM
2362 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2363 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2364
9c541725 2365 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2366 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2367}
2368
4390d890 2369\f
9291a0cd 2370
31aa7e4e
JB
2371/* Read the given attribute value as an address, taking the attribute's
2372 form into account. */
2373
2374static CORE_ADDR
2375attr_value_as_address (struct attribute *attr)
2376{
2377 CORE_ADDR addr;
2378
2379 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2380 {
2381 /* Aside from a few clearly defined exceptions, attributes that
2382 contain an address must always be in DW_FORM_addr form.
2383 Unfortunately, some compilers happen to be violating this
2384 requirement by encoding addresses using other forms, such
2385 as DW_FORM_data4 for example. For those broken compilers,
2386 we try to do our best, without any guarantee of success,
2387 to interpret the address correctly. It would also be nice
2388 to generate a complaint, but that would require us to maintain
2389 a list of legitimate cases where a non-address form is allowed,
2390 as well as update callers to pass in at least the CU's DWARF
2391 version. This is more overhead than what we're willing to
2392 expand for a pretty rare case. */
2393 addr = DW_UNSND (attr);
2394 }
2395 else
2396 addr = DW_ADDR (attr);
2397
2398 return addr;
2399}
2400
9291a0cd 2401/* The suffix for an index file. */
437afbb8
JK
2402#define INDEX4_SUFFIX ".gdb-index"
2403#define INDEX5_SUFFIX ".debug_names"
2404#define DEBUG_STR_SUFFIX ".debug_str"
9291a0cd 2405
330cdd98
PA
2406/* See declaration. */
2407
2408dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2409 const dwarf2_debug_sections *names)
2410 : objfile (objfile_)
2411{
2412 if (names == NULL)
2413 names = &dwarf2_elf_names;
2414
2415 bfd *obfd = objfile->obfd;
2416
2417 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2418 locate_sections (obfd, sec, *names);
2419}
2420
2421dwarf2_per_objfile::~dwarf2_per_objfile ()
2422{
2423 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2424 free_cached_comp_units ();
2425
2426 if (quick_file_names_table)
2427 htab_delete (quick_file_names_table);
2428
2429 if (line_header_hash)
2430 htab_delete (line_header_hash);
2431
2432 /* Everything else should be on the objfile obstack. */
2433}
2434
2435/* See declaration. */
2436
2437void
2438dwarf2_per_objfile::free_cached_comp_units ()
2439{
2440 dwarf2_per_cu_data *per_cu = read_in_chain;
2441 dwarf2_per_cu_data **last_chain = &read_in_chain;
2442 while (per_cu != NULL)
2443 {
2444 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2445
fcd3b13d 2446 delete per_cu->cu;
330cdd98
PA
2447 *last_chain = next_cu;
2448 per_cu = next_cu;
2449 }
2450}
2451
c906108c 2452/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2453 information and return true if we have enough to do something.
2454 NAMES points to the dwarf2 section names, or is NULL if the standard
2455 ELF names are used. */
c906108c
SS
2456
2457int
251d32d9
TG
2458dwarf2_has_info (struct objfile *objfile,
2459 const struct dwarf2_debug_sections *names)
c906108c 2460{
97cbe998
SDJ
2461 if (objfile->flags & OBJF_READNEVER)
2462 return 0;
2463
ed2dc618
SM
2464 struct dwarf2_per_objfile *dwarf2_per_objfile
2465 = get_dwarf2_per_objfile (objfile);
2466
2467 if (dwarf2_per_objfile == NULL)
be391dca
TT
2468 {
2469 /* Initialize per-objfile state. */
2470 struct dwarf2_per_objfile *data
8d749320 2471 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
9a619af0 2472
330cdd98 2473 dwarf2_per_objfile = new (data) struct dwarf2_per_objfile (objfile, names);
ed2dc618 2474 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
be391dca 2475 }
73869dc2 2476 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2477 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2478 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2479 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2480}
2481
2482/* Return the containing section of virtual section SECTION. */
2483
2484static struct dwarf2_section_info *
2485get_containing_section (const struct dwarf2_section_info *section)
2486{
2487 gdb_assert (section->is_virtual);
2488 return section->s.containing_section;
c906108c
SS
2489}
2490
a32a8923
DE
2491/* Return the bfd owner of SECTION. */
2492
2493static struct bfd *
2494get_section_bfd_owner (const struct dwarf2_section_info *section)
2495{
73869dc2
DE
2496 if (section->is_virtual)
2497 {
2498 section = get_containing_section (section);
2499 gdb_assert (!section->is_virtual);
2500 }
049412e3 2501 return section->s.section->owner;
a32a8923
DE
2502}
2503
2504/* Return the bfd section of SECTION.
2505 Returns NULL if the section is not present. */
2506
2507static asection *
2508get_section_bfd_section (const struct dwarf2_section_info *section)
2509{
73869dc2
DE
2510 if (section->is_virtual)
2511 {
2512 section = get_containing_section (section);
2513 gdb_assert (!section->is_virtual);
2514 }
049412e3 2515 return section->s.section;
a32a8923
DE
2516}
2517
2518/* Return the name of SECTION. */
2519
2520static const char *
2521get_section_name (const struct dwarf2_section_info *section)
2522{
2523 asection *sectp = get_section_bfd_section (section);
2524
2525 gdb_assert (sectp != NULL);
2526 return bfd_section_name (get_section_bfd_owner (section), sectp);
2527}
2528
2529/* Return the name of the file SECTION is in. */
2530
2531static const char *
2532get_section_file_name (const struct dwarf2_section_info *section)
2533{
2534 bfd *abfd = get_section_bfd_owner (section);
2535
2536 return bfd_get_filename (abfd);
2537}
2538
2539/* Return the id of SECTION.
2540 Returns 0 if SECTION doesn't exist. */
2541
2542static int
2543get_section_id (const struct dwarf2_section_info *section)
2544{
2545 asection *sectp = get_section_bfd_section (section);
2546
2547 if (sectp == NULL)
2548 return 0;
2549 return sectp->id;
2550}
2551
2552/* Return the flags of SECTION.
73869dc2 2553 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2554
2555static int
2556get_section_flags (const struct dwarf2_section_info *section)
2557{
2558 asection *sectp = get_section_bfd_section (section);
2559
2560 gdb_assert (sectp != NULL);
2561 return bfd_get_section_flags (sectp->owner, sectp);
2562}
2563
251d32d9
TG
2564/* When loading sections, we look either for uncompressed section or for
2565 compressed section names. */
233a11ab
CS
2566
2567static int
251d32d9
TG
2568section_is_p (const char *section_name,
2569 const struct dwarf2_section_names *names)
233a11ab 2570{
251d32d9
TG
2571 if (names->normal != NULL
2572 && strcmp (section_name, names->normal) == 0)
2573 return 1;
2574 if (names->compressed != NULL
2575 && strcmp (section_name, names->compressed) == 0)
2576 return 1;
2577 return 0;
233a11ab
CS
2578}
2579
330cdd98 2580/* See declaration. */
c906108c 2581
330cdd98
PA
2582void
2583dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2584 const dwarf2_debug_sections &names)
c906108c 2585{
dc7650b8 2586 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9 2587
dc7650b8
JK
2588 if ((aflag & SEC_HAS_CONTENTS) == 0)
2589 {
2590 }
330cdd98 2591 else if (section_is_p (sectp->name, &names.info))
c906108c 2592 {
330cdd98
PA
2593 this->info.s.section = sectp;
2594 this->info.size = bfd_get_section_size (sectp);
c906108c 2595 }
330cdd98 2596 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2597 {
330cdd98
PA
2598 this->abbrev.s.section = sectp;
2599 this->abbrev.size = bfd_get_section_size (sectp);
c906108c 2600 }
330cdd98 2601 else if (section_is_p (sectp->name, &names.line))
c906108c 2602 {
330cdd98
PA
2603 this->line.s.section = sectp;
2604 this->line.size = bfd_get_section_size (sectp);
c906108c 2605 }
330cdd98 2606 else if (section_is_p (sectp->name, &names.loc))
c906108c 2607 {
330cdd98
PA
2608 this->loc.s.section = sectp;
2609 this->loc.size = bfd_get_section_size (sectp);
c906108c 2610 }
330cdd98 2611 else if (section_is_p (sectp->name, &names.loclists))
43988095 2612 {
330cdd98
PA
2613 this->loclists.s.section = sectp;
2614 this->loclists.size = bfd_get_section_size (sectp);
43988095 2615 }
330cdd98 2616 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2617 {
330cdd98
PA
2618 this->macinfo.s.section = sectp;
2619 this->macinfo.size = bfd_get_section_size (sectp);
c906108c 2620 }
330cdd98 2621 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2622 {
330cdd98
PA
2623 this->macro.s.section = sectp;
2624 this->macro.size = bfd_get_section_size (sectp);
cf2c3c16 2625 }
330cdd98 2626 else if (section_is_p (sectp->name, &names.str))
c906108c 2627 {
330cdd98
PA
2628 this->str.s.section = sectp;
2629 this->str.size = bfd_get_section_size (sectp);
c906108c 2630 }
330cdd98 2631 else if (section_is_p (sectp->name, &names.line_str))
43988095 2632 {
330cdd98
PA
2633 this->line_str.s.section = sectp;
2634 this->line_str.size = bfd_get_section_size (sectp);
43988095 2635 }
330cdd98 2636 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2637 {
330cdd98
PA
2638 this->addr.s.section = sectp;
2639 this->addr.size = bfd_get_section_size (sectp);
3019eac3 2640 }
330cdd98 2641 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2642 {
330cdd98
PA
2643 this->frame.s.section = sectp;
2644 this->frame.size = bfd_get_section_size (sectp);
b6af0555 2645 }
330cdd98 2646 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2647 {
330cdd98
PA
2648 this->eh_frame.s.section = sectp;
2649 this->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2650 }
330cdd98 2651 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2652 {
330cdd98
PA
2653 this->ranges.s.section = sectp;
2654 this->ranges.size = bfd_get_section_size (sectp);
af34e669 2655 }
330cdd98 2656 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2657 {
330cdd98
PA
2658 this->rnglists.s.section = sectp;
2659 this->rnglists.size = bfd_get_section_size (sectp);
43988095 2660 }
330cdd98 2661 else if (section_is_p (sectp->name, &names.types))
348e048f 2662 {
8b70b953
TT
2663 struct dwarf2_section_info type_section;
2664
2665 memset (&type_section, 0, sizeof (type_section));
049412e3 2666 type_section.s.section = sectp;
8b70b953
TT
2667 type_section.size = bfd_get_section_size (sectp);
2668
330cdd98 2669 VEC_safe_push (dwarf2_section_info_def, this->types,
8b70b953 2670 &type_section);
348e048f 2671 }
330cdd98 2672 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2673 {
330cdd98
PA
2674 this->gdb_index.s.section = sectp;
2675 this->gdb_index.size = bfd_get_section_size (sectp);
9291a0cd 2676 }
927aa2e7
JK
2677 else if (section_is_p (sectp->name, &names.debug_names))
2678 {
2679 this->debug_names.s.section = sectp;
2680 this->debug_names.size = bfd_get_section_size (sectp);
2681 }
2682 else if (section_is_p (sectp->name, &names.debug_aranges))
2683 {
2684 this->debug_aranges.s.section = sectp;
2685 this->debug_aranges.size = bfd_get_section_size (sectp);
2686 }
dce234bc 2687
b4e1fd61 2688 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5 2689 && bfd_section_vma (abfd, sectp) == 0)
330cdd98 2690 this->has_section_at_zero = true;
c906108c
SS
2691}
2692
fceca515
DE
2693/* A helper function that decides whether a section is empty,
2694 or not present. */
9e0ac564
TT
2695
2696static int
19ac8c2e 2697dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2698{
73869dc2
DE
2699 if (section->is_virtual)
2700 return section->size == 0;
049412e3 2701 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2702}
2703
3019eac3
DE
2704/* Read the contents of the section INFO.
2705 OBJFILE is the main object file, but not necessarily the file where
a32a8923
DE
2706 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2707 of the DWO file.
dce234bc 2708 If the section is compressed, uncompress it before returning. */
c906108c 2709
dce234bc
PP
2710static void
2711dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 2712{
a32a8923 2713 asection *sectp;
3019eac3 2714 bfd *abfd;
dce234bc 2715 gdb_byte *buf, *retbuf;
c906108c 2716
be391dca
TT
2717 if (info->readin)
2718 return;
dce234bc 2719 info->buffer = NULL;
be391dca 2720 info->readin = 1;
188dd5d6 2721
9e0ac564 2722 if (dwarf2_section_empty_p (info))
dce234bc 2723 return;
c906108c 2724
a32a8923 2725 sectp = get_section_bfd_section (info);
3019eac3 2726
73869dc2
DE
2727 /* If this is a virtual section we need to read in the real one first. */
2728 if (info->is_virtual)
2729 {
2730 struct dwarf2_section_info *containing_section =
2731 get_containing_section (info);
2732
2733 gdb_assert (sectp != NULL);
2734 if ((sectp->flags & SEC_RELOC) != 0)
2735 {
2736 error (_("Dwarf Error: DWP format V2 with relocations is not"
2737 " supported in section %s [in module %s]"),
2738 get_section_name (info), get_section_file_name (info));
2739 }
2740 dwarf2_read_section (objfile, containing_section);
2741 /* Other code should have already caught virtual sections that don't
2742 fit. */
2743 gdb_assert (info->virtual_offset + info->size
2744 <= containing_section->size);
2745 /* If the real section is empty or there was a problem reading the
2746 section we shouldn't get here. */
2747 gdb_assert (containing_section->buffer != NULL);
2748 info->buffer = containing_section->buffer + info->virtual_offset;
2749 return;
2750 }
2751
4bf44c1c
TT
2752 /* If the section has relocations, we must read it ourselves.
2753 Otherwise we attach it to the BFD. */
2754 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2755 {
d521ce57 2756 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2757 return;
dce234bc 2758 }
dce234bc 2759
224c3ddb 2760 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2761 info->buffer = buf;
dce234bc
PP
2762
2763 /* When debugging .o files, we may need to apply relocations; see
2764 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2765 We never compress sections in .o files, so we only need to
2766 try this when the section is not compressed. */
ac8035ab 2767 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2768 if (retbuf != NULL)
2769 {
2770 info->buffer = retbuf;
2771 return;
2772 }
2773
a32a8923
DE
2774 abfd = get_section_bfd_owner (info);
2775 gdb_assert (abfd != NULL);
2776
dce234bc
PP
2777 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2778 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2779 {
2780 error (_("Dwarf Error: Can't read DWARF data"
2781 " in section %s [in module %s]"),
2782 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2783 }
dce234bc
PP
2784}
2785
9e0ac564
TT
2786/* A helper function that returns the size of a section in a safe way.
2787 If you are positive that the section has been read before using the
2788 size, then it is safe to refer to the dwarf2_section_info object's
2789 "size" field directly. In other cases, you must call this
2790 function, because for compressed sections the size field is not set
2791 correctly until the section has been read. */
2792
2793static bfd_size_type
2794dwarf2_section_size (struct objfile *objfile,
2795 struct dwarf2_section_info *info)
2796{
2797 if (!info->readin)
2798 dwarf2_read_section (objfile, info);
2799 return info->size;
2800}
2801
dce234bc 2802/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2803 SECTION_NAME. */
af34e669 2804
dce234bc 2805void
3017a003
TG
2806dwarf2_get_section_info (struct objfile *objfile,
2807 enum dwarf2_section_enum sect,
d521ce57 2808 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2809 bfd_size_type *sizep)
2810{
2811 struct dwarf2_per_objfile *data
9a3c8263
SM
2812 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2813 dwarf2_objfile_data_key);
dce234bc 2814 struct dwarf2_section_info *info;
a3b2a86b
TT
2815
2816 /* We may see an objfile without any DWARF, in which case we just
2817 return nothing. */
2818 if (data == NULL)
2819 {
2820 *sectp = NULL;
2821 *bufp = NULL;
2822 *sizep = 0;
2823 return;
2824 }
3017a003
TG
2825 switch (sect)
2826 {
2827 case DWARF2_DEBUG_FRAME:
2828 info = &data->frame;
2829 break;
2830 case DWARF2_EH_FRAME:
2831 info = &data->eh_frame;
2832 break;
2833 default:
2834 gdb_assert_not_reached ("unexpected section");
2835 }
dce234bc 2836
9e0ac564 2837 dwarf2_read_section (objfile, info);
dce234bc 2838
a32a8923 2839 *sectp = get_section_bfd_section (info);
dce234bc
PP
2840 *bufp = info->buffer;
2841 *sizep = info->size;
2842}
2843
36586728
TT
2844/* A helper function to find the sections for a .dwz file. */
2845
2846static void
2847locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2848{
9a3c8263 2849 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2850
2851 /* Note that we only support the standard ELF names, because .dwz
2852 is ELF-only (at the time of writing). */
2853 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2854 {
049412e3 2855 dwz_file->abbrev.s.section = sectp;
36586728
TT
2856 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2857 }
2858 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2859 {
049412e3 2860 dwz_file->info.s.section = sectp;
36586728
TT
2861 dwz_file->info.size = bfd_get_section_size (sectp);
2862 }
2863 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2864 {
049412e3 2865 dwz_file->str.s.section = sectp;
36586728
TT
2866 dwz_file->str.size = bfd_get_section_size (sectp);
2867 }
2868 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2869 {
049412e3 2870 dwz_file->line.s.section = sectp;
36586728
TT
2871 dwz_file->line.size = bfd_get_section_size (sectp);
2872 }
2873 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2874 {
049412e3 2875 dwz_file->macro.s.section = sectp;
36586728
TT
2876 dwz_file->macro.size = bfd_get_section_size (sectp);
2877 }
2ec9a5e0
TT
2878 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2879 {
049412e3 2880 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2881 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2882 }
927aa2e7
JK
2883 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2884 {
2885 dwz_file->debug_names.s.section = sectp;
2886 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2887 }
36586728
TT
2888}
2889
4db1a1dc
TT
2890/* Open the separate '.dwz' debug file, if needed. Return NULL if
2891 there is no .gnu_debugaltlink section in the file. Error if there
2892 is such a section but the file cannot be found. */
36586728
TT
2893
2894static struct dwz_file *
ed2dc618 2895dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2896{
36586728
TT
2897 const char *filename;
2898 struct dwz_file *result;
acd13123 2899 bfd_size_type buildid_len_arg;
dc294be5
TT
2900 size_t buildid_len;
2901 bfd_byte *buildid;
36586728
TT
2902
2903 if (dwarf2_per_objfile->dwz_file != NULL)
2904 return dwarf2_per_objfile->dwz_file;
2905
4db1a1dc 2906 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2907 gdb::unique_xmalloc_ptr<char> data
2908 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2909 &buildid_len_arg, &buildid));
4db1a1dc
TT
2910 if (data == NULL)
2911 {
2912 if (bfd_get_error () == bfd_error_no_error)
2913 return NULL;
2914 error (_("could not read '.gnu_debugaltlink' section: %s"),
2915 bfd_errmsg (bfd_get_error ()));
2916 }
791afaa2
TT
2917
2918 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2919
acd13123
TT
2920 buildid_len = (size_t) buildid_len_arg;
2921
791afaa2 2922 filename = data.get ();
d721ba37
PA
2923
2924 std::string abs_storage;
36586728
TT
2925 if (!IS_ABSOLUTE_PATH (filename))
2926 {
14278e1f
TT
2927 gdb::unique_xmalloc_ptr<char> abs
2928 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2929
14278e1f 2930 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2931 filename = abs_storage.c_str ();
36586728
TT
2932 }
2933
dc294be5
TT
2934 /* First try the file name given in the section. If that doesn't
2935 work, try to use the build-id instead. */
192b62ce 2936 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2937 if (dwz_bfd != NULL)
36586728 2938 {
192b62ce
TT
2939 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2940 dwz_bfd.release ();
36586728
TT
2941 }
2942
dc294be5
TT
2943 if (dwz_bfd == NULL)
2944 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2945
2946 if (dwz_bfd == NULL)
2947 error (_("could not find '.gnu_debugaltlink' file for %s"),
2948 objfile_name (dwarf2_per_objfile->objfile));
2949
36586728
TT
2950 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2951 struct dwz_file);
192b62ce 2952 result->dwz_bfd = dwz_bfd.release ();
36586728 2953
192b62ce 2954 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
36586728 2955
192b62ce 2956 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
8d2cc612 2957 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2958 return result;
2959}
9291a0cd 2960\f
7b9f3c50
DE
2961/* DWARF quick_symbols_functions support. */
2962
2963/* TUs can share .debug_line entries, and there can be a lot more TUs than
2964 unique line tables, so we maintain a separate table of all .debug_line
2965 derived entries to support the sharing.
2966 All the quick functions need is the list of file names. We discard the
2967 line_header when we're done and don't need to record it here. */
2968struct quick_file_names
2969{
094b34ac
DE
2970 /* The data used to construct the hash key. */
2971 struct stmt_list_hash hash;
7b9f3c50
DE
2972
2973 /* The number of entries in file_names, real_names. */
2974 unsigned int num_file_names;
2975
2976 /* The file names from the line table, after being run through
2977 file_full_name. */
2978 const char **file_names;
2979
2980 /* The file names from the line table after being run through
2981 gdb_realpath. These are computed lazily. */
2982 const char **real_names;
2983};
2984
2985/* When using the index (and thus not using psymtabs), each CU has an
2986 object of this type. This is used to hold information needed by
2987 the various "quick" methods. */
2988struct dwarf2_per_cu_quick_data
2989{
2990 /* The file table. This can be NULL if there was no file table
2991 or it's currently not read in.
2992 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2993 struct quick_file_names *file_names;
2994
2995 /* The corresponding symbol table. This is NULL if symbols for this
2996 CU have not yet been read. */
43f3e411 2997 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2998
2999 /* A temporary mark bit used when iterating over all CUs in
3000 expand_symtabs_matching. */
3001 unsigned int mark : 1;
3002
3003 /* True if we've tried to read the file table and found there isn't one.
3004 There will be no point in trying to read it again next time. */
3005 unsigned int no_file_data : 1;
3006};
3007
094b34ac
DE
3008/* Utility hash function for a stmt_list_hash. */
3009
3010static hashval_t
3011hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
3012{
3013 hashval_t v = 0;
3014
3015 if (stmt_list_hash->dwo_unit != NULL)
3016 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 3017 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
3018 return v;
3019}
3020
3021/* Utility equality function for a stmt_list_hash. */
3022
3023static int
3024eq_stmt_list_entry (const struct stmt_list_hash *lhs,
3025 const struct stmt_list_hash *rhs)
3026{
3027 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
3028 return 0;
3029 if (lhs->dwo_unit != NULL
3030 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
3031 return 0;
3032
9c541725 3033 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
3034}
3035
7b9f3c50
DE
3036/* Hash function for a quick_file_names. */
3037
3038static hashval_t
3039hash_file_name_entry (const void *e)
3040{
9a3c8263
SM
3041 const struct quick_file_names *file_data
3042 = (const struct quick_file_names *) e;
7b9f3c50 3043
094b34ac 3044 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
3045}
3046
3047/* Equality function for a quick_file_names. */
3048
3049static int
3050eq_file_name_entry (const void *a, const void *b)
3051{
9a3c8263
SM
3052 const struct quick_file_names *ea = (const struct quick_file_names *) a;
3053 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 3054
094b34ac 3055 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
3056}
3057
3058/* Delete function for a quick_file_names. */
3059
3060static void
3061delete_file_name_entry (void *e)
3062{
9a3c8263 3063 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
3064 int i;
3065
3066 for (i = 0; i < file_data->num_file_names; ++i)
3067 {
3068 xfree ((void*) file_data->file_names[i]);
3069 if (file_data->real_names)
3070 xfree ((void*) file_data->real_names[i]);
3071 }
3072
3073 /* The space for the struct itself lives on objfile_obstack,
3074 so we don't free it here. */
3075}
3076
3077/* Create a quick_file_names hash table. */
3078
3079static htab_t
3080create_quick_file_names_table (unsigned int nr_initial_entries)
3081{
3082 return htab_create_alloc (nr_initial_entries,
3083 hash_file_name_entry, eq_file_name_entry,
3084 delete_file_name_entry, xcalloc, xfree);
3085}
9291a0cd 3086
918dd910
JK
3087/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
3088 have to be created afterwards. You should call age_cached_comp_units after
3089 processing PER_CU->CU. dw2_setup must have been already called. */
3090
3091static void
3092load_cu (struct dwarf2_per_cu_data *per_cu)
3093{
3019eac3 3094 if (per_cu->is_debug_types)
e5fe5e75 3095 load_full_type_unit (per_cu);
918dd910 3096 else
95554aad 3097 load_full_comp_unit (per_cu, language_minimal);
918dd910 3098
cc12ce38
DE
3099 if (per_cu->cu == NULL)
3100 return; /* Dummy CU. */
2dc860c0
DE
3101
3102 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
3103}
3104
a0f42c21 3105/* Read in the symbols for PER_CU. */
2fdf6df6 3106
9291a0cd 3107static void
a0f42c21 3108dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
3109{
3110 struct cleanup *back_to;
ed2dc618 3111 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 3112
f4dc4d17
DE
3113 /* Skip type_unit_groups, reading the type units they contain
3114 is handled elsewhere. */
3115 if (IS_TYPE_UNIT_GROUP (per_cu))
3116 return;
3117
9291a0cd
TT
3118 back_to = make_cleanup (dwarf2_release_queue, NULL);
3119
95554aad 3120 if (dwarf2_per_objfile->using_index
43f3e411 3121 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
3122 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
3123 {
3124 queue_comp_unit (per_cu, language_minimal);
3125 load_cu (per_cu);
89e63ee4
DE
3126
3127 /* If we just loaded a CU from a DWO, and we're working with an index
3128 that may badly handle TUs, load all the TUs in that DWO as well.
3129 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
3130 if (!per_cu->is_debug_types
cc12ce38 3131 && per_cu->cu != NULL
89e63ee4
DE
3132 && per_cu->cu->dwo_unit != NULL
3133 && dwarf2_per_objfile->index_table != NULL
3134 && dwarf2_per_objfile->index_table->version <= 7
3135 /* DWP files aren't supported yet. */
ed2dc618 3136 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 3137 queue_and_load_all_dwo_tus (per_cu);
95554aad 3138 }
9291a0cd 3139
ed2dc618 3140 process_queue (dwarf2_per_objfile);
9291a0cd
TT
3141
3142 /* Age the cache, releasing compilation units that have not
3143 been used recently. */
ed2dc618 3144 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
3145
3146 do_cleanups (back_to);
3147}
3148
3149/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
3150 the objfile from which this CU came. Returns the resulting symbol
3151 table. */
2fdf6df6 3152
43f3e411 3153static struct compunit_symtab *
a0f42c21 3154dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 3155{
ed2dc618
SM
3156 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3157
95554aad 3158 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 3159 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 3160 {
ed2dc618
SM
3161 struct cleanup *back_to = make_cleanup (free_cached_comp_units,
3162 dwarf2_per_objfile);
c83dd867 3163 scoped_restore decrementer = increment_reading_symtab ();
a0f42c21 3164 dw2_do_instantiate_symtab (per_cu);
ed2dc618 3165 process_cu_includes (dwarf2_per_objfile);
9291a0cd
TT
3166 do_cleanups (back_to);
3167 }
f194fefb 3168
43f3e411 3169 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
3170}
3171
8832e7e3 3172/* Return the CU/TU given its index.
f4dc4d17
DE
3173
3174 This is intended for loops like:
3175
3176 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3177 + dwarf2_per_objfile->n_type_units); ++i)
3178 {
8832e7e3 3179 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
f4dc4d17
DE
3180
3181 ...;
3182 }
3183*/
2fdf6df6 3184
1fd400ff 3185static struct dwarf2_per_cu_data *
ed2dc618
SM
3186dw2_get_cutu (struct dwarf2_per_objfile *dwarf2_per_objfile,
3187 int index)
1fd400ff
TT
3188{
3189 if (index >= dwarf2_per_objfile->n_comp_units)
3190 {
f4dc4d17 3191 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
3192 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3193 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
3194 }
3195
3196 return dwarf2_per_objfile->all_comp_units[index];
3197}
3198
8832e7e3
DE
3199/* Return the CU given its index.
3200 This differs from dw2_get_cutu in that it's for when you know INDEX
3201 refers to a CU. */
f4dc4d17
DE
3202
3203static struct dwarf2_per_cu_data *
ed2dc618 3204dw2_get_cu (struct dwarf2_per_objfile *dwarf2_per_objfile, int index)
f4dc4d17 3205{
8832e7e3 3206 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
f4dc4d17 3207
1fd400ff
TT
3208 return dwarf2_per_objfile->all_comp_units[index];
3209}
3210
4b514bc8
JK
3211/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
3212 objfile_obstack, and constructed with the specified field
3213 values. */
3214
3215static dwarf2_per_cu_data *
ed2dc618 3216create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
3217 struct dwarf2_section_info *section,
3218 int is_dwz,
3219 sect_offset sect_off, ULONGEST length)
3220{
ed2dc618 3221 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
3222 dwarf2_per_cu_data *the_cu
3223 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3224 struct dwarf2_per_cu_data);
3225 the_cu->sect_off = sect_off;
3226 the_cu->length = length;
e3b94546 3227 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
3228 the_cu->section = section;
3229 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3230 struct dwarf2_per_cu_quick_data);
3231 the_cu->is_dwz = is_dwz;
3232 return the_cu;
3233}
3234
2ec9a5e0
TT
3235/* A helper for create_cus_from_index that handles a given list of
3236 CUs. */
2fdf6df6 3237
74a0d9f6 3238static void
2ec9a5e0
TT
3239create_cus_from_index_list (struct objfile *objfile,
3240 const gdb_byte *cu_list, offset_type n_elements,
3241 struct dwarf2_section_info *section,
3242 int is_dwz,
3243 int base_offset)
9291a0cd
TT
3244{
3245 offset_type i;
ed2dc618
SM
3246 struct dwarf2_per_objfile *dwarf2_per_objfile
3247 = get_dwarf2_per_objfile (objfile);
9291a0cd 3248
2ec9a5e0 3249 for (i = 0; i < n_elements; i += 2)
9291a0cd 3250 {
74a0d9f6 3251 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3252
3253 sect_offset sect_off
3254 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3255 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
3256 cu_list += 2 * 8;
3257
4b514bc8 3258 dwarf2_per_objfile->all_comp_units[base_offset + i / 2]
ed2dc618
SM
3259 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3260 sect_off, length);
9291a0cd 3261 }
9291a0cd
TT
3262}
3263
2ec9a5e0 3264/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3265 the CU objects for this objfile. */
2ec9a5e0 3266
74a0d9f6 3267static void
2ec9a5e0
TT
3268create_cus_from_index (struct objfile *objfile,
3269 const gdb_byte *cu_list, offset_type cu_list_elements,
3270 const gdb_byte *dwz_list, offset_type dwz_elements)
3271{
3272 struct dwz_file *dwz;
ed2dc618
SM
3273 struct dwarf2_per_objfile *dwarf2_per_objfile
3274 = get_dwarf2_per_objfile (objfile);
2ec9a5e0
TT
3275
3276 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
8d749320
SM
3277 dwarf2_per_objfile->all_comp_units =
3278 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3279 dwarf2_per_objfile->n_comp_units);
2ec9a5e0 3280
74a0d9f6
JK
3281 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3282 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
3283
3284 if (dwz_elements == 0)
74a0d9f6 3285 return;
2ec9a5e0 3286
ed2dc618 3287 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
74a0d9f6
JK
3288 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3289 cu_list_elements / 2);
2ec9a5e0
TT
3290}
3291
1fd400ff 3292/* Create the signatured type hash table from the index. */
673bfd45 3293
74a0d9f6 3294static void
673bfd45 3295create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 3296 struct dwarf2_section_info *section,
673bfd45
DE
3297 const gdb_byte *bytes,
3298 offset_type elements)
1fd400ff
TT
3299{
3300 offset_type i;
673bfd45 3301 htab_t sig_types_hash;
ed2dc618
SM
3302 struct dwarf2_per_objfile *dwarf2_per_objfile
3303 = get_dwarf2_per_objfile (objfile);
1fd400ff 3304
6aa5f3a6
DE
3305 dwarf2_per_objfile->n_type_units
3306 = dwarf2_per_objfile->n_allocated_type_units
3307 = elements / 3;
8d749320
SM
3308 dwarf2_per_objfile->all_type_units =
3309 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
1fd400ff 3310
673bfd45 3311 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
3312
3313 for (i = 0; i < elements; i += 3)
3314 {
52dc124a 3315 struct signatured_type *sig_type;
9c541725 3316 ULONGEST signature;
1fd400ff 3317 void **slot;
9c541725 3318 cu_offset type_offset_in_tu;
1fd400ff 3319
74a0d9f6 3320 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3321 sect_offset sect_off
3322 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3323 type_offset_in_tu
3324 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3325 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3326 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3327 bytes += 3 * 8;
3328
52dc124a 3329 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3330 struct signatured_type);
52dc124a 3331 sig_type->signature = signature;
9c541725 3332 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3333 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3334 sig_type->per_cu.section = section;
9c541725 3335 sig_type->per_cu.sect_off = sect_off;
e3b94546 3336 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 3337 sig_type->per_cu.v.quick
1fd400ff
TT
3338 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3339 struct dwarf2_per_cu_quick_data);
3340
52dc124a
DE
3341 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3342 *slot = sig_type;
1fd400ff 3343
b4dd5633 3344 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
3345 }
3346
673bfd45 3347 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3348}
3349
927aa2e7
JK
3350/* Create the signatured type hash table from .debug_names. */
3351
3352static void
3353create_signatured_type_table_from_debug_names
ed2dc618 3354 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3355 const mapped_debug_names &map,
3356 struct dwarf2_section_info *section,
3357 struct dwarf2_section_info *abbrev_section)
3358{
ed2dc618
SM
3359 struct objfile *objfile = dwarf2_per_objfile->objfile;
3360
927aa2e7
JK
3361 dwarf2_read_section (objfile, section);
3362 dwarf2_read_section (objfile, abbrev_section);
3363
3364 dwarf2_per_objfile->n_type_units
3365 = dwarf2_per_objfile->n_allocated_type_units
3366 = map.tu_count;
3367 dwarf2_per_objfile->all_type_units
3368 = XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3369
3370 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3371
3372 for (uint32_t i = 0; i < map.tu_count; ++i)
3373 {
3374 struct signatured_type *sig_type;
3375 ULONGEST signature;
3376 void **slot;
3377 cu_offset type_offset_in_tu;
3378
3379 sect_offset sect_off
3380 = (sect_offset) (extract_unsigned_integer
3381 (map.tu_table_reordered + i * map.offset_size,
3382 map.offset_size,
3383 map.dwarf5_byte_order));
3384
3385 comp_unit_head cu_header;
ed2dc618
SM
3386 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3387 abbrev_section,
927aa2e7
JK
3388 section->buffer + to_underlying (sect_off),
3389 rcuh_kind::TYPE);
3390
3391 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3392 struct signatured_type);
3393 sig_type->signature = cu_header.signature;
3394 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3395 sig_type->per_cu.is_debug_types = 1;
3396 sig_type->per_cu.section = section;
3397 sig_type->per_cu.sect_off = sect_off;
e3b94546 3398 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
3399 sig_type->per_cu.v.quick
3400 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3401 struct dwarf2_per_cu_quick_data);
3402
3403 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3404 *slot = sig_type;
3405
3406 dwarf2_per_objfile->all_type_units[i] = sig_type;
3407 }
3408
3409 dwarf2_per_objfile->signatured_types = sig_types_hash;
3410}
3411
9291a0cd
TT
3412/* Read the address map data from the mapped index, and use it to
3413 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3414
9291a0cd 3415static void
ed2dc618
SM
3416create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3417 struct mapped_index *index)
9291a0cd 3418{
ed2dc618 3419 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 3420 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3421 const gdb_byte *iter, *end;
9291a0cd 3422 struct addrmap *mutable_map;
9291a0cd
TT
3423 CORE_ADDR baseaddr;
3424
8268c778
PA
3425 auto_obstack temp_obstack;
3426
9291a0cd
TT
3427 mutable_map = addrmap_create_mutable (&temp_obstack);
3428
f00a2de2
PA
3429 iter = index->address_table.data ();
3430 end = iter + index->address_table.size ();
9291a0cd
TT
3431
3432 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3433
3434 while (iter < end)
3435 {
3436 ULONGEST hi, lo, cu_index;
3437 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3438 iter += 8;
3439 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3440 iter += 8;
3441 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3442 iter += 4;
f652bce2 3443
24a55014 3444 if (lo > hi)
f652bce2 3445 {
24a55014
DE
3446 complaint (&symfile_complaints,
3447 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3448 hex_string (lo), hex_string (hi));
24a55014 3449 continue;
f652bce2 3450 }
24a55014
DE
3451
3452 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
3453 {
3454 complaint (&symfile_complaints,
3455 _(".gdb_index address table has invalid CU number %u"),
3456 (unsigned) cu_index);
24a55014 3457 continue;
f652bce2 3458 }
24a55014 3459
3e29f34a
MR
3460 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3461 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
ed2dc618
SM
3462 addrmap_set_empty (mutable_map, lo, hi - 1,
3463 dw2_get_cutu (dwarf2_per_objfile, cu_index));
9291a0cd
TT
3464 }
3465
3466 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3467 &objfile->objfile_obstack);
9291a0cd
TT
3468}
3469
927aa2e7
JK
3470/* Read the address map data from DWARF-5 .debug_aranges, and use it to
3471 populate the objfile's psymtabs_addrmap. */
3472
3473static void
ed2dc618 3474create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3475 struct dwarf2_section_info *section)
3476{
ed2dc618 3477 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
3478 bfd *abfd = objfile->obfd;
3479 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3480 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3481 SECT_OFF_TEXT (objfile));
3482
3483 auto_obstack temp_obstack;
3484 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3485
3486 std::unordered_map<sect_offset,
3487 dwarf2_per_cu_data *,
3488 gdb::hash_enum<sect_offset>>
3489 debug_info_offset_to_per_cu;
3490 for (int cui = 0; cui < dwarf2_per_objfile->n_comp_units; ++cui)
3491 {
ed2dc618 3492 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, cui);
927aa2e7
JK
3493 const auto insertpair
3494 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3495 if (!insertpair.second)
3496 {
3497 warning (_("Section .debug_aranges in %s has duplicate "
3498 "debug_info_offset %u, ignoring .debug_aranges."),
3499 objfile_name (objfile), to_underlying (per_cu->sect_off));
3500 return;
3501 }
3502 }
3503
3504 dwarf2_read_section (objfile, section);
3505
3506 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3507
3508 const gdb_byte *addr = section->buffer;
3509
3510 while (addr < section->buffer + section->size)
3511 {
3512 const gdb_byte *const entry_addr = addr;
3513 unsigned int bytes_read;
3514
3515 const LONGEST entry_length = read_initial_length (abfd, addr,
3516 &bytes_read);
3517 addr += bytes_read;
3518
3519 const gdb_byte *const entry_end = addr + entry_length;
3520 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3521 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3522 if (addr + entry_length > section->buffer + section->size)
3523 {
3524 warning (_("Section .debug_aranges in %s entry at offset %zu "
3525 "length %s exceeds section length %s, "
3526 "ignoring .debug_aranges."),
3527 objfile_name (objfile), entry_addr - section->buffer,
3528 plongest (bytes_read + entry_length),
3529 pulongest (section->size));
3530 return;
3531 }
3532
3533 /* The version number. */
3534 const uint16_t version = read_2_bytes (abfd, addr);
3535 addr += 2;
3536 if (version != 2)
3537 {
3538 warning (_("Section .debug_aranges in %s entry at offset %zu "
3539 "has unsupported version %d, ignoring .debug_aranges."),
3540 objfile_name (objfile), entry_addr - section->buffer,
3541 version);
3542 return;
3543 }
3544
3545 const uint64_t debug_info_offset
3546 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3547 addr += offset_size;
3548 const auto per_cu_it
3549 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3550 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3551 {
3552 warning (_("Section .debug_aranges in %s entry at offset %zu "
3553 "debug_info_offset %s does not exists, "
3554 "ignoring .debug_aranges."),
3555 objfile_name (objfile), entry_addr - section->buffer,
3556 pulongest (debug_info_offset));
3557 return;
3558 }
3559 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3560
3561 const uint8_t address_size = *addr++;
3562 if (address_size < 1 || address_size > 8)
3563 {
3564 warning (_("Section .debug_aranges in %s entry at offset %zu "
3565 "address_size %u is invalid, ignoring .debug_aranges."),
3566 objfile_name (objfile), entry_addr - section->buffer,
3567 address_size);
3568 return;
3569 }
3570
3571 const uint8_t segment_selector_size = *addr++;
3572 if (segment_selector_size != 0)
3573 {
3574 warning (_("Section .debug_aranges in %s entry at offset %zu "
3575 "segment_selector_size %u is not supported, "
3576 "ignoring .debug_aranges."),
3577 objfile_name (objfile), entry_addr - section->buffer,
3578 segment_selector_size);
3579 return;
3580 }
3581
3582 /* Must pad to an alignment boundary that is twice the address
3583 size. It is undocumented by the DWARF standard but GCC does
3584 use it. */
3585 for (size_t padding = ((-(addr - section->buffer))
3586 & (2 * address_size - 1));
3587 padding > 0; padding--)
3588 if (*addr++ != 0)
3589 {
3590 warning (_("Section .debug_aranges in %s entry at offset %zu "
3591 "padding is not zero, ignoring .debug_aranges."),
3592 objfile_name (objfile), entry_addr - section->buffer);
3593 return;
3594 }
3595
3596 for (;;)
3597 {
3598 if (addr + 2 * address_size > entry_end)
3599 {
3600 warning (_("Section .debug_aranges in %s entry at offset %zu "
3601 "address list is not properly terminated, "
3602 "ignoring .debug_aranges."),
3603 objfile_name (objfile), entry_addr - section->buffer);
3604 return;
3605 }
3606 ULONGEST start = extract_unsigned_integer (addr, address_size,
3607 dwarf5_byte_order);
3608 addr += address_size;
3609 ULONGEST length = extract_unsigned_integer (addr, address_size,
3610 dwarf5_byte_order);
3611 addr += address_size;
3612 if (start == 0 && length == 0)
3613 break;
3614 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3615 {
3616 /* Symbol was eliminated due to a COMDAT group. */
3617 continue;
3618 }
3619 ULONGEST end = start + length;
3620 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3621 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3622 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3623 }
3624 }
3625
3626 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3627 &objfile->objfile_obstack);
3628}
3629
59d7bcaf
JK
3630/* The hash function for strings in the mapped index. This is the same as
3631 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3632 implementation. This is necessary because the hash function is tied to the
3633 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
3634 SYMBOL_HASH_NEXT.
3635
3636 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 3637
9291a0cd 3638static hashval_t
559a7a62 3639mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
3640{
3641 const unsigned char *str = (const unsigned char *) p;
3642 hashval_t r = 0;
3643 unsigned char c;
3644
3645 while ((c = *str++) != 0)
559a7a62
JK
3646 {
3647 if (index_version >= 5)
3648 c = tolower (c);
3649 r = r * 67 + c - 113;
3650 }
9291a0cd
TT
3651
3652 return r;
3653}
3654
3655/* Find a slot in the mapped index INDEX for the object named NAME.
3656 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3657 constant pool and return true. If NAME cannot be found, return
3658 false. */
2fdf6df6 3659
109483d9 3660static bool
9291a0cd
TT
3661find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3662 offset_type **vec_out)
3663{
0cf03b49 3664 offset_type hash;
9291a0cd 3665 offset_type slot, step;
559a7a62 3666 int (*cmp) (const char *, const char *);
9291a0cd 3667
791afaa2 3668 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3669 if (current_language->la_language == language_cplus
45280282
IB
3670 || current_language->la_language == language_fortran
3671 || current_language->la_language == language_d)
0cf03b49
JK
3672 {
3673 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3674 not contain any. */
a8719064 3675
72998fb3 3676 if (strchr (name, '(') != NULL)
0cf03b49 3677 {
109483d9 3678 without_params = cp_remove_params (name);
0cf03b49 3679
72998fb3 3680 if (without_params != NULL)
791afaa2 3681 name = without_params.get ();
0cf03b49
JK
3682 }
3683 }
3684
559a7a62 3685 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3686 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3687 simulate our NAME being searched is also lowercased. */
3688 hash = mapped_index_string_hash ((index->version == 4
3689 && case_sensitivity == case_sensitive_off
3690 ? 5 : index->version),
3691 name);
3692
f00a2de2
PA
3693 slot = hash & (index->symbol_table.size () - 1);
3694 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3695 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3696
3697 for (;;)
3698 {
9291a0cd 3699 const char *str;
f00a2de2
PA
3700
3701 const auto &bucket = index->symbol_table[slot];
3702 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3703 return false;
9291a0cd 3704
f00a2de2 3705 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3706 if (!cmp (name, str))
9291a0cd
TT
3707 {
3708 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3709 + MAYBE_SWAP (bucket.vec));
109483d9 3710 return true;
9291a0cd
TT
3711 }
3712
f00a2de2 3713 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3714 }
3715}
3716
2ec9a5e0
TT
3717/* A helper function that reads the .gdb_index from SECTION and fills
3718 in MAP. FILENAME is the name of the file containing the section;
3719 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3720 ok to use deprecated sections.
3721
3722 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3723 out parameters that are filled in with information about the CU and
3724 TU lists in the section.
3725
3726 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3727
9291a0cd 3728static int
2ec9a5e0
TT
3729read_index_from_section (struct objfile *objfile,
3730 const char *filename,
3731 int deprecated_ok,
3732 struct dwarf2_section_info *section,
3733 struct mapped_index *map,
3734 const gdb_byte **cu_list,
3735 offset_type *cu_list_elements,
3736 const gdb_byte **types_list,
3737 offset_type *types_list_elements)
9291a0cd 3738{
948f8e3d 3739 const gdb_byte *addr;
2ec9a5e0 3740 offset_type version;
b3b272e1 3741 offset_type *metadata;
1fd400ff 3742 int i;
9291a0cd 3743
2ec9a5e0 3744 if (dwarf2_section_empty_p (section))
9291a0cd 3745 return 0;
82430852
JK
3746
3747 /* Older elfutils strip versions could keep the section in the main
3748 executable while splitting it for the separate debug info file. */
a32a8923 3749 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3750 return 0;
3751
2ec9a5e0 3752 dwarf2_read_section (objfile, section);
9291a0cd 3753
2ec9a5e0 3754 addr = section->buffer;
9291a0cd 3755 /* Version check. */
1fd400ff 3756 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3757 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3758 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3759 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3760 indices. */
831adc1f 3761 if (version < 4)
481860b3
GB
3762 {
3763 static int warning_printed = 0;
3764 if (!warning_printed)
3765 {
3766 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3767 filename);
481860b3
GB
3768 warning_printed = 1;
3769 }
3770 return 0;
3771 }
3772 /* Index version 4 uses a different hash function than index version
3773 5 and later.
3774
3775 Versions earlier than 6 did not emit psymbols for inlined
3776 functions. Using these files will cause GDB not to be able to
3777 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3778 indices unless the user has done
3779 "set use-deprecated-index-sections on". */
2ec9a5e0 3780 if (version < 6 && !deprecated_ok)
481860b3
GB
3781 {
3782 static int warning_printed = 0;
3783 if (!warning_printed)
3784 {
e615022a
DE
3785 warning (_("\
3786Skipping deprecated .gdb_index section in %s.\n\
3787Do \"set use-deprecated-index-sections on\" before the file is read\n\
3788to use the section anyway."),
2ec9a5e0 3789 filename);
481860b3
GB
3790 warning_printed = 1;
3791 }
3792 return 0;
3793 }
796a7ff8 3794 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3795 of the TU (for symbols coming from TUs),
3796 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3797 Plus gold-generated indices can have duplicate entries for global symbols,
3798 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3799 These are just performance bugs, and we can't distinguish gdb-generated
3800 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3801
481860b3 3802 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3803 longer backward compatible. */
796a7ff8 3804 if (version > 8)
594e8718 3805 return 0;
9291a0cd 3806
559a7a62 3807 map->version = version;
2ec9a5e0 3808 map->total_size = section->size;
9291a0cd
TT
3809
3810 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3811
3812 i = 0;
2ec9a5e0
TT
3813 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3814 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3815 / 8);
1fd400ff
TT
3816 ++i;
3817
2ec9a5e0
TT
3818 *types_list = addr + MAYBE_SWAP (metadata[i]);
3819 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3820 - MAYBE_SWAP (metadata[i]))
3821 / 8);
987d643c 3822 ++i;
1fd400ff 3823
f00a2de2
PA
3824 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3825 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3826 map->address_table
3827 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3828 ++i;
3829
f00a2de2
PA
3830 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3831 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3832 map->symbol_table
3833 = gdb::array_view<mapped_index::symbol_table_slot>
3834 ((mapped_index::symbol_table_slot *) symbol_table,
3835 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3836
f00a2de2 3837 ++i;
f9d83a0b 3838 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3839
2ec9a5e0
TT
3840 return 1;
3841}
3842
927aa2e7 3843/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3844 elements of all the CUs and return 1. Otherwise, return 0. */
3845
3846static int
3847dwarf2_read_index (struct objfile *objfile)
3848{
3849 struct mapped_index local_map, *map;
3850 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3851 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3852 struct dwz_file *dwz;
ed2dc618
SM
3853 struct dwarf2_per_objfile *dwarf2_per_objfile
3854 = get_dwarf2_per_objfile (objfile);
2ec9a5e0 3855
4262abfb 3856 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0
TT
3857 use_deprecated_index_sections,
3858 &dwarf2_per_objfile->gdb_index, &local_map,
3859 &cu_list, &cu_list_elements,
3860 &types_list, &types_list_elements))
3861 return 0;
3862
0fefef59 3863 /* Don't use the index if it's empty. */
f00a2de2 3864 if (local_map.symbol_table.empty ())
0fefef59
DE
3865 return 0;
3866
2ec9a5e0
TT
3867 /* If there is a .dwz file, read it so we can get its CU list as
3868 well. */
ed2dc618 3869 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3870 if (dwz != NULL)
2ec9a5e0 3871 {
2ec9a5e0
TT
3872 struct mapped_index dwz_map;
3873 const gdb_byte *dwz_types_ignore;
3874 offset_type dwz_types_elements_ignore;
3875
3876 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3877 1,
3878 &dwz->gdb_index, &dwz_map,
3879 &dwz_list, &dwz_list_elements,
3880 &dwz_types_ignore,
3881 &dwz_types_elements_ignore))
3882 {
3883 warning (_("could not read '.gdb_index' section from %s; skipping"),
3884 bfd_get_filename (dwz->dwz_bfd));
3885 return 0;
3886 }
3887 }
3888
74a0d9f6
JK
3889 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3890 dwz_list_elements);
1fd400ff 3891
8b70b953
TT
3892 if (types_list_elements)
3893 {
3894 struct dwarf2_section_info *section;
3895
3896 /* We can only handle a single .debug_types when we have an
3897 index. */
3898 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3899 return 0;
3900
3901 section = VEC_index (dwarf2_section_info_def,
3902 dwarf2_per_objfile->types, 0);
3903
74a0d9f6
JK
3904 create_signatured_type_table_from_index (objfile, section, types_list,
3905 types_list_elements);
8b70b953 3906 }
9291a0cd 3907
ed2dc618 3908 create_addrmap_from_index (dwarf2_per_objfile, &local_map);
2ec9a5e0 3909
8d749320 3910 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3f563c84 3911 map = new (map) mapped_index ();
2ec9a5e0 3912 *map = local_map;
9291a0cd
TT
3913
3914 dwarf2_per_objfile->index_table = map;
3915 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
3916 dwarf2_per_objfile->quick_file_names_table =
3917 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
3918
3919 return 1;
3920}
3921
dee91e82 3922/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3923
dee91e82
DE
3924static void
3925dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3926 const gdb_byte *info_ptr,
dee91e82
DE
3927 struct die_info *comp_unit_die,
3928 int has_children,
3929 void *data)
9291a0cd 3930{
dee91e82 3931 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3932 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3933 struct dwarf2_per_objfile *dwarf2_per_objfile
3934 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3935 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3936 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3937 struct attribute *attr;
dee91e82 3938 int i;
7b9f3c50
DE
3939 void **slot;
3940 struct quick_file_names *qfn;
9291a0cd 3941
0186c6a7
DE
3942 gdb_assert (! this_cu->is_debug_types);
3943
07261596
TT
3944 /* Our callers never want to match partial units -- instead they
3945 will match the enclosing full CU. */
3946 if (comp_unit_die->tag == DW_TAG_partial_unit)
3947 {
3948 this_cu->v.quick->no_file_data = 1;
3949 return;
3950 }
3951
0186c6a7 3952 lh_cu = this_cu;
7b9f3c50 3953 slot = NULL;
dee91e82 3954
fff8551c 3955 line_header_up lh;
9c541725 3956 sect_offset line_offset {};
fff8551c 3957
dee91e82 3958 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3959 if (attr)
3960 {
7b9f3c50
DE
3961 struct quick_file_names find_entry;
3962
9c541725 3963 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3964
3965 /* We may have already read in this line header (TU line header sharing).
3966 If we have we're done. */
094b34ac 3967 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3968 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3969 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3970 &find_entry, INSERT);
3971 if (*slot != NULL)
3972 {
9a3c8263 3973 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3974 return;
7b9f3c50
DE
3975 }
3976
3019eac3 3977 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3978 }
3979 if (lh == NULL)
3980 {
094b34ac 3981 lh_cu->v.quick->no_file_data = 1;
dee91e82 3982 return;
9291a0cd
TT
3983 }
3984
8d749320 3985 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3986 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3987 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3988 gdb_assert (slot != NULL);
3989 *slot = qfn;
9291a0cd 3990
d721ba37 3991 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3992
fff8551c 3993 qfn->num_file_names = lh->file_names.size ();
8d749320 3994 qfn->file_names =
fff8551c
PA
3995 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3996 for (i = 0; i < lh->file_names.size (); ++i)
3997 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3998 qfn->real_names = NULL;
9291a0cd 3999
094b34ac 4000 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
4001}
4002
4003/* A helper for the "quick" functions which attempts to read the line
4004 table for THIS_CU. */
4005
4006static struct quick_file_names *
e4a48d9d 4007dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 4008{
0186c6a7
DE
4009 /* This should never be called for TUs. */
4010 gdb_assert (! this_cu->is_debug_types);
4011 /* Nor type unit groups. */
4012 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 4013
dee91e82
DE
4014 if (this_cu->v.quick->file_names != NULL)
4015 return this_cu->v.quick->file_names;
4016 /* If we know there is no line data, no point in looking again. */
4017 if (this_cu->v.quick->no_file_data)
4018 return NULL;
4019
0186c6a7 4020 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
4021
4022 if (this_cu->v.quick->no_file_data)
4023 return NULL;
4024 return this_cu->v.quick->file_names;
9291a0cd
TT
4025}
4026
4027/* A helper for the "quick" functions which computes and caches the
7b9f3c50 4028 real path for a given file name from the line table. */
2fdf6df6 4029
9291a0cd 4030static const char *
7b9f3c50
DE
4031dw2_get_real_path (struct objfile *objfile,
4032 struct quick_file_names *qfn, int index)
9291a0cd 4033{
7b9f3c50
DE
4034 if (qfn->real_names == NULL)
4035 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 4036 qfn->num_file_names, const char *);
9291a0cd 4037
7b9f3c50 4038 if (qfn->real_names[index] == NULL)
14278e1f 4039 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 4040
7b9f3c50 4041 return qfn->real_names[index];
9291a0cd
TT
4042}
4043
4044static struct symtab *
4045dw2_find_last_source_symtab (struct objfile *objfile)
4046{
ed2dc618
SM
4047 struct dwarf2_per_objfile *dwarf2_per_objfile
4048 = get_dwarf2_per_objfile (objfile);
4049 int index = dwarf2_per_objfile->n_comp_units - 1;
4050 dwarf2_per_cu_data *dwarf_cu = dw2_get_cutu (dwarf2_per_objfile, index);
4051 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu);
ae2de4f8 4052
43f3e411
DE
4053 if (cust == NULL)
4054 return NULL;
ed2dc618 4055
43f3e411 4056 return compunit_primary_filetab (cust);
9291a0cd
TT
4057}
4058
7b9f3c50
DE
4059/* Traversal function for dw2_forget_cached_source_info. */
4060
4061static int
4062dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 4063{
7b9f3c50 4064 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 4065
7b9f3c50 4066 if (file_data->real_names)
9291a0cd 4067 {
7b9f3c50 4068 int i;
9291a0cd 4069
7b9f3c50 4070 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 4071 {
7b9f3c50
DE
4072 xfree ((void*) file_data->real_names[i]);
4073 file_data->real_names[i] = NULL;
9291a0cd
TT
4074 }
4075 }
7b9f3c50
DE
4076
4077 return 1;
4078}
4079
4080static void
4081dw2_forget_cached_source_info (struct objfile *objfile)
4082{
ed2dc618
SM
4083 struct dwarf2_per_objfile *dwarf2_per_objfile
4084 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
4085
4086 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
4087 dw2_free_cached_file_names, NULL);
9291a0cd
TT
4088}
4089
f8eba3c6
TT
4090/* Helper function for dw2_map_symtabs_matching_filename that expands
4091 the symtabs and calls the iterator. */
4092
4093static int
4094dw2_map_expand_apply (struct objfile *objfile,
4095 struct dwarf2_per_cu_data *per_cu,
f5b95b50 4096 const char *name, const char *real_path,
14bc53a8 4097 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 4098{
43f3e411 4099 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
4100
4101 /* Don't visit already-expanded CUs. */
43f3e411 4102 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
4103 return 0;
4104
4105 /* This may expand more than one symtab, and we want to iterate over
4106 all of them. */
a0f42c21 4107 dw2_instantiate_symtab (per_cu);
f8eba3c6 4108
14bc53a8
PA
4109 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
4110 last_made, callback);
f8eba3c6
TT
4111}
4112
4113/* Implementation of the map_symtabs_matching_filename method. */
4114
14bc53a8
PA
4115static bool
4116dw2_map_symtabs_matching_filename
4117 (struct objfile *objfile, const char *name, const char *real_path,
4118 gdb::function_view<bool (symtab *)> callback)
9291a0cd
TT
4119{
4120 int i;
c011a4f4 4121 const char *name_basename = lbasename (name);
ed2dc618
SM
4122 struct dwarf2_per_objfile *dwarf2_per_objfile
4123 = get_dwarf2_per_objfile (objfile);
ae2de4f8 4124
848e3e78
DE
4125 /* The rule is CUs specify all the files, including those used by
4126 any TU, so there's no need to scan TUs here. */
f4dc4d17 4127
ed2dc618 4128 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4129 {
4130 int j;
ed2dc618 4131 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
7b9f3c50 4132 struct quick_file_names *file_data;
9291a0cd 4133
3d7bb9d9 4134 /* We only need to look at symtabs not already expanded. */
43f3e411 4135 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4136 continue;
4137
e4a48d9d 4138 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4139 if (file_data == NULL)
9291a0cd
TT
4140 continue;
4141
7b9f3c50 4142 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4143 {
7b9f3c50 4144 const char *this_name = file_data->file_names[j];
da235a7c 4145 const char *this_real_name;
9291a0cd 4146
af529f8f 4147 if (compare_filenames_for_search (this_name, name))
9291a0cd 4148 {
f5b95b50 4149 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
4150 callback))
4151 return true;
288e77a7 4152 continue;
4aac40c8 4153 }
9291a0cd 4154
c011a4f4
DE
4155 /* Before we invoke realpath, which can get expensive when many
4156 files are involved, do a quick comparison of the basenames. */
4157 if (! basenames_may_differ
4158 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
4159 continue;
4160
da235a7c
JK
4161 this_real_name = dw2_get_real_path (objfile, file_data, j);
4162 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 4163 {
da235a7c 4164 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
4165 callback))
4166 return true;
288e77a7 4167 continue;
da235a7c 4168 }
9291a0cd 4169
da235a7c
JK
4170 if (real_path != NULL)
4171 {
af529f8f
JK
4172 gdb_assert (IS_ABSOLUTE_PATH (real_path));
4173 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 4174 if (this_real_name != NULL
af529f8f 4175 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 4176 {
f5b95b50 4177 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
4178 callback))
4179 return true;
288e77a7 4180 continue;
9291a0cd
TT
4181 }
4182 }
4183 }
4184 }
4185
14bc53a8 4186 return false;
9291a0cd
TT
4187}
4188
da51c347
DE
4189/* Struct used to manage iterating over all CUs looking for a symbol. */
4190
4191struct dw2_symtab_iterator
9291a0cd 4192{
ed2dc618
SM
4193 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
4194 struct dwarf2_per_objfile *dwarf2_per_objfile;
da51c347
DE
4195 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
4196 int want_specific_block;
4197 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
4198 Unused if !WANT_SPECIFIC_BLOCK. */
4199 int block_index;
4200 /* The kind of symbol we're looking for. */
4201 domain_enum domain;
4202 /* The list of CUs from the index entry of the symbol,
4203 or NULL if not found. */
4204 offset_type *vec;
4205 /* The next element in VEC to look at. */
4206 int next;
4207 /* The number of elements in VEC, or zero if there is no match. */
4208 int length;
8943b874
DE
4209 /* Have we seen a global version of the symbol?
4210 If so we can ignore all further global instances.
4211 This is to work around gold/15646, inefficient gold-generated
4212 indices. */
4213 int global_seen;
da51c347 4214};
9291a0cd 4215
da51c347
DE
4216/* Initialize the index symtab iterator ITER.
4217 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
4218 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 4219
9291a0cd 4220static void
da51c347 4221dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 4222 struct dwarf2_per_objfile *dwarf2_per_objfile,
da51c347
DE
4223 int want_specific_block,
4224 int block_index,
4225 domain_enum domain,
4226 const char *name)
4227{
ed2dc618 4228 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
4229 iter->want_specific_block = want_specific_block;
4230 iter->block_index = block_index;
4231 iter->domain = domain;
4232 iter->next = 0;
8943b874 4233 iter->global_seen = 0;
da51c347 4234
ed2dc618
SM
4235 mapped_index *index = dwarf2_per_objfile->index_table;
4236
4237 /* index is NULL if OBJF_READNOW. */
4238 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
4239 iter->length = MAYBE_SWAP (*iter->vec);
4240 else
4241 {
4242 iter->vec = NULL;
4243 iter->length = 0;
4244 }
4245}
4246
4247/* Return the next matching CU or NULL if there are no more. */
4248
4249static struct dwarf2_per_cu_data *
4250dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
4251{
ed2dc618
SM
4252 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
4253
da51c347
DE
4254 for ( ; iter->next < iter->length; ++iter->next)
4255 {
4256 offset_type cu_index_and_attrs =
4257 MAYBE_SWAP (iter->vec[iter->next + 1]);
4258 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 4259 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
4260 int want_static = iter->block_index != GLOBAL_BLOCK;
4261 /* This value is only valid for index versions >= 7. */
4262 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4263 gdb_index_symbol_kind symbol_kind =
4264 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4265 /* Only check the symbol attributes if they're present.
4266 Indices prior to version 7 don't record them,
4267 and indices >= 7 may elide them for certain symbols
4268 (gold does this). */
4269 int attrs_valid =
ed2dc618 4270 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
4271 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4272
3190f0c6
DE
4273 /* Don't crash on bad data. */
4274 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4275 + dwarf2_per_objfile->n_type_units))
4276 {
4277 complaint (&symfile_complaints,
4278 _(".gdb_index entry has bad CU index"
4262abfb
JK
4279 " [in module %s]"),
4280 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
4281 continue;
4282 }
4283
ed2dc618 4284 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
3190f0c6 4285
da51c347 4286 /* Skip if already read in. */
43f3e411 4287 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
4288 continue;
4289
8943b874
DE
4290 /* Check static vs global. */
4291 if (attrs_valid)
4292 {
4293 if (iter->want_specific_block
4294 && want_static != is_static)
4295 continue;
4296 /* Work around gold/15646. */
4297 if (!is_static && iter->global_seen)
4298 continue;
4299 if (!is_static)
4300 iter->global_seen = 1;
4301 }
da51c347
DE
4302
4303 /* Only check the symbol's kind if it has one. */
4304 if (attrs_valid)
4305 {
4306 switch (iter->domain)
4307 {
4308 case VAR_DOMAIN:
4309 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4310 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4311 /* Some types are also in VAR_DOMAIN. */
4312 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4313 continue;
4314 break;
4315 case STRUCT_DOMAIN:
4316 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4317 continue;
4318 break;
4319 case LABEL_DOMAIN:
4320 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4321 continue;
4322 break;
4323 default:
4324 break;
4325 }
4326 }
4327
4328 ++iter->next;
4329 return per_cu;
4330 }
4331
4332 return NULL;
4333}
4334
43f3e411 4335static struct compunit_symtab *
da51c347
DE
4336dw2_lookup_symbol (struct objfile *objfile, int block_index,
4337 const char *name, domain_enum domain)
9291a0cd 4338{
43f3e411 4339 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
4340 struct dwarf2_per_objfile *dwarf2_per_objfile
4341 = get_dwarf2_per_objfile (objfile);
9291a0cd 4342
b5ec771e
PA
4343 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4344
ed2dc618
SM
4345 struct dw2_symtab_iterator iter;
4346 struct dwarf2_per_cu_data *per_cu;
da51c347 4347
ed2dc618 4348 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
9291a0cd 4349
ed2dc618
SM
4350 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4351 {
4352 struct symbol *sym, *with_opaque = NULL;
4353 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
4354 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4355 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4356
ed2dc618
SM
4357 sym = block_find_symbol (block, name, domain,
4358 block_find_non_opaque_type_preferred,
4359 &with_opaque);
b2e2f908 4360
ed2dc618
SM
4361 /* Some caution must be observed with overloaded functions
4362 and methods, since the index will not contain any overload
4363 information (but NAME might contain it). */
da51c347 4364
ed2dc618
SM
4365 if (sym != NULL
4366 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4367 return stab;
4368 if (with_opaque != NULL
4369 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4370 stab_best = stab;
da51c347 4371
ed2dc618 4372 /* Keep looking through other CUs. */
9291a0cd 4373 }
9291a0cd 4374
da51c347 4375 return stab_best;
9291a0cd
TT
4376}
4377
4378static void
4379dw2_print_stats (struct objfile *objfile)
4380{
ed2dc618
SM
4381 struct dwarf2_per_objfile *dwarf2_per_objfile
4382 = get_dwarf2_per_objfile (objfile);
4383 int total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
4384 int count = 0;
9291a0cd 4385
ed2dc618 4386 for (int i = 0; i < total; ++i)
9291a0cd 4387 {
ed2dc618 4388 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
9291a0cd 4389
43f3e411 4390 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4391 ++count;
4392 }
e4a48d9d 4393 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4394 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4395}
4396
779bd270
DE
4397/* This dumps minimal information about the index.
4398 It is called via "mt print objfiles".
4399 One use is to verify .gdb_index has been loaded by the
4400 gdb.dwarf2/gdb-index.exp testcase. */
4401
9291a0cd
TT
4402static void
4403dw2_dump (struct objfile *objfile)
4404{
ed2dc618
SM
4405 struct dwarf2_per_objfile *dwarf2_per_objfile
4406 = get_dwarf2_per_objfile (objfile);
4407
779bd270
DE
4408 gdb_assert (dwarf2_per_objfile->using_index);
4409 printf_filtered (".gdb_index:");
4410 if (dwarf2_per_objfile->index_table != NULL)
4411 {
4412 printf_filtered (" version %d\n",
4413 dwarf2_per_objfile->index_table->version);
4414 }
4415 else
4416 printf_filtered (" faked for \"readnow\"\n");
4417 printf_filtered ("\n");
9291a0cd
TT
4418}
4419
4420static void
3189cb12
DE
4421dw2_relocate (struct objfile *objfile,
4422 const struct section_offsets *new_offsets,
4423 const struct section_offsets *delta)
9291a0cd
TT
4424{
4425 /* There's nothing to relocate here. */
4426}
4427
4428static void
4429dw2_expand_symtabs_for_function (struct objfile *objfile,
4430 const char *func_name)
4431{
ed2dc618
SM
4432 struct dwarf2_per_objfile *dwarf2_per_objfile
4433 = get_dwarf2_per_objfile (objfile);
da51c347 4434
ed2dc618
SM
4435 struct dw2_symtab_iterator iter;
4436 struct dwarf2_per_cu_data *per_cu;
da51c347 4437
ed2dc618
SM
4438 /* Note: It doesn't matter what we pass for block_index here. */
4439 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4440 func_name);
da51c347 4441
ed2dc618
SM
4442 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4443 dw2_instantiate_symtab (per_cu);
da51c347 4444
9291a0cd
TT
4445}
4446
4447static void
4448dw2_expand_all_symtabs (struct objfile *objfile)
4449{
ed2dc618
SM
4450 struct dwarf2_per_objfile *dwarf2_per_objfile
4451 = get_dwarf2_per_objfile (objfile);
4452 int total_units = (dwarf2_per_objfile->n_comp_units
4453 + dwarf2_per_objfile->n_type_units);
9291a0cd 4454
ed2dc618 4455 for (int i = 0; i < total_units; ++i)
9291a0cd 4456 {
ed2dc618
SM
4457 struct dwarf2_per_cu_data *per_cu
4458 = dw2_get_cutu (dwarf2_per_objfile, i);
9291a0cd 4459
a0f42c21 4460 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4461 }
4462}
4463
4464static void
652a8996
JK
4465dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4466 const char *fullname)
9291a0cd 4467{
ed2dc618
SM
4468 struct dwarf2_per_objfile *dwarf2_per_objfile
4469 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
4470
4471 /* We don't need to consider type units here.
4472 This is only called for examining code, e.g. expand_line_sal.
4473 There can be an order of magnitude (or more) more type units
4474 than comp units, and we avoid them if we can. */
4475
ed2dc618 4476 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4477 {
4478 int j;
ed2dc618 4479 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
7b9f3c50 4480 struct quick_file_names *file_data;
9291a0cd 4481
3d7bb9d9 4482 /* We only need to look at symtabs not already expanded. */
43f3e411 4483 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4484 continue;
4485
e4a48d9d 4486 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4487 if (file_data == NULL)
9291a0cd
TT
4488 continue;
4489
7b9f3c50 4490 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4491 {
652a8996
JK
4492 const char *this_fullname = file_data->file_names[j];
4493
4494 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4495 {
a0f42c21 4496 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4497 break;
4498 }
4499 }
4500 }
4501}
4502
9291a0cd 4503static void
ade7ed9e 4504dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 4505 const char * name, domain_enum domain,
ade7ed9e 4506 int global,
40658b94
PH
4507 int (*callback) (struct block *,
4508 struct symbol *, void *),
b5ec771e 4509 void *data, symbol_name_match_type match,
2edb89d3 4510 symbol_compare_ftype *ordered_compare)
9291a0cd 4511{
40658b94 4512 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4513 current language is Ada for a non-Ada objfile using GNU index. As Ada
4514 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4515}
4516
b5ec771e
PA
4517/* Symbol name matcher for .gdb_index names.
4518
4519 Symbol names in .gdb_index have a few particularities:
4520
4521 - There's no indication of which is the language of each symbol.
4522
4523 Since each language has its own symbol name matching algorithm,
4524 and we don't know which language is the right one, we must match
3f563c84
PA
4525 each symbol against all languages. This would be a potential
4526 performance problem if it were not mitigated by the
4527 mapped_index::name_components lookup table, which significantly
4528 reduces the number of times we need to call into this matcher,
4529 making it a non-issue.
b5ec771e
PA
4530
4531 - Symbol names in the index have no overload (parameter)
4532 information. I.e., in C++, "foo(int)" and "foo(long)" both
4533 appear as "foo" in the index, for example.
4534
4535 This means that the lookup names passed to the symbol name
4536 matcher functions must have no parameter information either
4537 because (e.g.) symbol search name "foo" does not match
4538 lookup-name "foo(int)" [while swapping search name for lookup
4539 name would match].
4540*/
4541class gdb_index_symbol_name_matcher
4542{
4543public:
4544 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4545 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4546
4547 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4548 Returns true if any matcher matches. */
4549 bool matches (const char *symbol_name);
4550
4551private:
4552 /* A reference to the lookup name we're matching against. */
4553 const lookup_name_info &m_lookup_name;
4554
4555 /* A vector holding all the different symbol name matchers, for all
4556 languages. */
4557 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4558};
4559
4560gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4561 (const lookup_name_info &lookup_name)
4562 : m_lookup_name (lookup_name)
4563{
4564 /* Prepare the vector of comparison functions upfront, to avoid
4565 doing the same work for each symbol. Care is taken to avoid
4566 matching with the same matcher more than once if/when multiple
4567 languages use the same matcher function. */
4568 auto &matchers = m_symbol_name_matcher_funcs;
4569 matchers.reserve (nr_languages);
4570
4571 matchers.push_back (default_symbol_name_matcher);
4572
4573 for (int i = 0; i < nr_languages; i++)
4574 {
4575 const language_defn *lang = language_def ((enum language) i);
c63d3e8d 4576 symbol_name_matcher_ftype *name_matcher
618daa93 4577 = get_symbol_name_matcher (lang, m_lookup_name);
c63d3e8d
PA
4578
4579 /* Don't insert the same comparison routine more than once.
4580 Note that we do this linear walk instead of a seemingly
4581 cheaper sorted insert, or use a std::set or something like
4582 that, because relative order of function addresses is not
4583 stable. This is not a problem in practice because the number
4584 of supported languages is low, and the cost here is tiny
4585 compared to the number of searches we'll do afterwards using
4586 this object. */
4587 if (name_matcher != default_symbol_name_matcher
4588 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4589 == matchers.end ()))
4590 matchers.push_back (name_matcher);
b5ec771e
PA
4591 }
4592}
4593
4594bool
4595gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4596{
4597 for (auto matches_name : m_symbol_name_matcher_funcs)
4598 if (matches_name (symbol_name, m_lookup_name, NULL))
4599 return true;
4600
4601 return false;
4602}
4603
e1ef7d7a
PA
4604/* Starting from a search name, return the string that finds the upper
4605 bound of all strings that start with SEARCH_NAME in a sorted name
4606 list. Returns the empty string to indicate that the upper bound is
4607 the end of the list. */
4608
4609static std::string
4610make_sort_after_prefix_name (const char *search_name)
4611{
4612 /* When looking to complete "func", we find the upper bound of all
4613 symbols that start with "func" by looking for where we'd insert
4614 the closest string that would follow "func" in lexicographical
4615 order. Usually, that's "func"-with-last-character-incremented,
4616 i.e. "fund". Mind non-ASCII characters, though. Usually those
4617 will be UTF-8 multi-byte sequences, but we can't be certain.
4618 Especially mind the 0xff character, which is a valid character in
4619 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4620 rule out compilers allowing it in identifiers. Note that
4621 conveniently, strcmp/strcasecmp are specified to compare
4622 characters interpreted as unsigned char. So what we do is treat
4623 the whole string as a base 256 number composed of a sequence of
4624 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4625 to 0, and carries 1 to the following more-significant position.
4626 If the very first character in SEARCH_NAME ends up incremented
4627 and carries/overflows, then the upper bound is the end of the
4628 list. The string after the empty string is also the empty
4629 string.
4630
4631 Some examples of this operation:
4632
4633 SEARCH_NAME => "+1" RESULT
4634
4635 "abc" => "abd"
4636 "ab\xff" => "ac"
4637 "\xff" "a" "\xff" => "\xff" "b"
4638 "\xff" => ""
4639 "\xff\xff" => ""
4640 "" => ""
4641
4642 Then, with these symbols for example:
4643
4644 func
4645 func1
4646 fund
4647
4648 completing "func" looks for symbols between "func" and
4649 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4650 which finds "func" and "func1", but not "fund".
4651
4652 And with:
4653
4654 funcÿ (Latin1 'ÿ' [0xff])
4655 funcÿ1
4656 fund
4657
4658 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4659 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4660
4661 And with:
4662
4663 ÿÿ (Latin1 'ÿ' [0xff])
4664 ÿÿ1
4665
4666 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4667 the end of the list.
4668 */
4669 std::string after = search_name;
4670 while (!after.empty () && (unsigned char) after.back () == 0xff)
4671 after.pop_back ();
4672 if (!after.empty ())
4673 after.back () = (unsigned char) after.back () + 1;
4674 return after;
4675}
4676
5c58de74 4677/* See declaration. */
61d96d7e 4678
5c58de74
PA
4679std::pair<std::vector<name_component>::const_iterator,
4680 std::vector<name_component>::const_iterator>
44ed8f3e 4681mapped_index_base::find_name_components_bounds
5c58de74 4682 (const lookup_name_info &lookup_name_without_params) const
3f563c84 4683{
5c58de74
PA
4684 auto *name_cmp
4685 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84
PA
4686
4687 const char *cplus
c62446b1 4688 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
9291a0cd 4689
3f563c84
PA
4690 /* Comparison function object for lower_bound that matches against a
4691 given symbol name. */
4692 auto lookup_compare_lower = [&] (const name_component &elem,
4693 const char *name)
4694 {
5c58de74 4695 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4696 const char *elem_name = elem_qualified + elem.name_offset;
4697 return name_cmp (elem_name, name) < 0;
4698 };
4699
4700 /* Comparison function object for upper_bound that matches against a
4701 given symbol name. */
4702 auto lookup_compare_upper = [&] (const char *name,
4703 const name_component &elem)
4704 {
5c58de74 4705 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4706 const char *elem_name = elem_qualified + elem.name_offset;
4707 return name_cmp (name, elem_name) < 0;
4708 };
4709
5c58de74
PA
4710 auto begin = this->name_components.begin ();
4711 auto end = this->name_components.end ();
3f563c84
PA
4712
4713 /* Find the lower bound. */
4714 auto lower = [&] ()
4715 {
5c58de74 4716 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
3f563c84
PA
4717 return begin;
4718 else
4719 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4720 } ();
4721
4722 /* Find the upper bound. */
4723 auto upper = [&] ()
4724 {
5c58de74 4725 if (lookup_name_without_params.completion_mode ())
3f563c84 4726 {
e1ef7d7a
PA
4727 /* In completion mode, we want UPPER to point past all
4728 symbols names that have the same prefix. I.e., with
4729 these symbols, and completing "func":
4730
4731 function << lower bound
4732 function1
4733 other_function << upper bound
4734
4735 We find the upper bound by looking for the insertion
4736 point of "func"-with-last-character-incremented,
4737 i.e. "fund". */
4738 std::string after = make_sort_after_prefix_name (cplus);
4739 if (after.empty ())
3f563c84 4740 return end;
e6b2f5ef
PA
4741 return std::lower_bound (lower, end, after.c_str (),
4742 lookup_compare_lower);
3f563c84
PA
4743 }
4744 else
4745 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4746 } ();
4747
5c58de74
PA
4748 return {lower, upper};
4749}
4750
4751/* See declaration. */
4752
4753void
44ed8f3e 4754mapped_index_base::build_name_components ()
5c58de74
PA
4755{
4756 if (!this->name_components.empty ())
4757 return;
4758
4759 this->name_components_casing = case_sensitivity;
4760 auto *name_cmp
4761 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4762
4763 /* The code below only knows how to break apart components of C++
4764 symbol names (and other languages that use '::' as
4765 namespace/module separator). If we add support for wild matching
4766 to some language that uses some other operator (E.g., Ada, Go and
4767 D use '.'), then we'll need to try splitting the symbol name
4768 according to that language too. Note that Ada does support wild
4769 matching, but doesn't currently support .gdb_index. */
44ed8f3e
PA
4770 auto count = this->symbol_name_count ();
4771 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4772 {
44ed8f3e 4773 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4774 continue;
4775
4776 const char *name = this->symbol_name_at (idx);
4777
4778 /* Add each name component to the name component table. */
4779 unsigned int previous_len = 0;
4780 for (unsigned int current_len = cp_find_first_component (name);
4781 name[current_len] != '\0';
4782 current_len += cp_find_first_component (name + current_len))
4783 {
4784 gdb_assert (name[current_len] == ':');
4785 this->name_components.push_back ({previous_len, idx});
4786 /* Skip the '::'. */
4787 current_len += 2;
4788 previous_len = current_len;
4789 }
4790 this->name_components.push_back ({previous_len, idx});
4791 }
4792
4793 /* Sort name_components elements by name. */
4794 auto name_comp_compare = [&] (const name_component &left,
4795 const name_component &right)
4796 {
4797 const char *left_qualified = this->symbol_name_at (left.idx);
4798 const char *right_qualified = this->symbol_name_at (right.idx);
4799
4800 const char *left_name = left_qualified + left.name_offset;
4801 const char *right_name = right_qualified + right.name_offset;
4802
4803 return name_cmp (left_name, right_name) < 0;
4804 };
4805
4806 std::sort (this->name_components.begin (),
4807 this->name_components.end (),
4808 name_comp_compare);
4809}
4810
4811/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4812 mapped_index_base instead of the containing objfile. This is split
4813 to a separate function in order to be able to unit test the
4814 name_components matching using a mock mapped_index_base. For each
5c58de74 4815 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4816 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4817
4818static void
4819dw2_expand_symtabs_matching_symbol
44ed8f3e 4820 (mapped_index_base &index,
5c58de74
PA
4821 const lookup_name_info &lookup_name_in,
4822 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4823 enum search_domain kind,
4824 gdb::function_view<void (offset_type)> match_callback)
4825{
4826 lookup_name_info lookup_name_without_params
4827 = lookup_name_in.make_ignore_params ();
4828 gdb_index_symbol_name_matcher lookup_name_matcher
4829 (lookup_name_without_params);
4830
4831 /* Build the symbol name component sorted vector, if we haven't
4832 yet. */
4833 index.build_name_components ();
4834
4835 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4836
3f563c84
PA
4837 /* Now for each symbol name in range, check to see if we have a name
4838 match, and if so, call the MATCH_CALLBACK callback. */
4839
4840 /* The same symbol may appear more than once in the range though.
4841 E.g., if we're looking for symbols that complete "w", and we have
4842 a symbol named "w1::w2", we'll find the two name components for
4843 that same symbol in the range. To be sure we only call the
4844 callback once per symbol, we first collect the symbol name
4845 indexes that matched in a temporary vector and ignore
4846 duplicates. */
4847 std::vector<offset_type> matches;
5c58de74 4848 matches.reserve (std::distance (bounds.first, bounds.second));
3f563c84 4849
5c58de74 4850 for (; bounds.first != bounds.second; ++bounds.first)
3f563c84 4851 {
5c58de74 4852 const char *qualified = index.symbol_name_at (bounds.first->idx);
3f563c84
PA
4853
4854 if (!lookup_name_matcher.matches (qualified)
4855 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
9291a0cd
TT
4856 continue;
4857
5c58de74 4858 matches.push_back (bounds.first->idx);
3f563c84
PA
4859 }
4860
4861 std::sort (matches.begin (), matches.end ());
4862
4863 /* Finally call the callback, once per match. */
4864 ULONGEST prev = -1;
4865 for (offset_type idx : matches)
4866 {
4867 if (prev != idx)
4868 {
4869 match_callback (idx);
4870 prev = idx;
4871 }
4872 }
4873
4874 /* Above we use a type wider than idx's for 'prev', since 0 and
4875 (offset_type)-1 are both possible values. */
4876 static_assert (sizeof (prev) > sizeof (offset_type), "");
4877}
4878
c62446b1
PA
4879#if GDB_SELF_TEST
4880
4881namespace selftests { namespace dw2_expand_symtabs_matching {
4882
a3c5fafd
PA
4883/* A mock .gdb_index/.debug_names-like name index table, enough to
4884 exercise dw2_expand_symtabs_matching_symbol, which works with the
4885 mapped_index_base interface. Builds an index from the symbol list
4886 passed as parameter to the constructor. */
4887class mock_mapped_index : public mapped_index_base
c62446b1
PA
4888{
4889public:
a3c5fafd
PA
4890 mock_mapped_index (gdb::array_view<const char *> symbols)
4891 : m_symbol_table (symbols)
c62446b1
PA
4892 {}
4893
a3c5fafd 4894 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4895
a3c5fafd
PA
4896 /* Return the number of names in the symbol table. */
4897 virtual size_t symbol_name_count () const
c62446b1 4898 {
a3c5fafd 4899 return m_symbol_table.size ();
c62446b1
PA
4900 }
4901
a3c5fafd
PA
4902 /* Get the name of the symbol at IDX in the symbol table. */
4903 virtual const char *symbol_name_at (offset_type idx) const
4904 {
4905 return m_symbol_table[idx];
4906 }
c62446b1 4907
a3c5fafd
PA
4908private:
4909 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4910};
4911
4912/* Convenience function that converts a NULL pointer to a "<null>"
4913 string, to pass to print routines. */
4914
4915static const char *
4916string_or_null (const char *str)
4917{
4918 return str != NULL ? str : "<null>";
4919}
4920
4921/* Check if a lookup_name_info built from
4922 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4923 index. EXPECTED_LIST is the list of expected matches, in expected
4924 matching order. If no match expected, then an empty list is
4925 specified. Returns true on success. On failure prints a warning
4926 indicating the file:line that failed, and returns false. */
4927
4928static bool
4929check_match (const char *file, int line,
4930 mock_mapped_index &mock_index,
4931 const char *name, symbol_name_match_type match_type,
4932 bool completion_mode,
4933 std::initializer_list<const char *> expected_list)
4934{
4935 lookup_name_info lookup_name (name, match_type, completion_mode);
4936
4937 bool matched = true;
4938
4939 auto mismatch = [&] (const char *expected_str,
4940 const char *got)
4941 {
4942 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4943 "expected=\"%s\", got=\"%s\"\n"),
4944 file, line,
4945 (match_type == symbol_name_match_type::FULL
4946 ? "FULL" : "WILD"),
4947 name, string_or_null (expected_str), string_or_null (got));
4948 matched = false;
4949 };
4950
4951 auto expected_it = expected_list.begin ();
4952 auto expected_end = expected_list.end ();
4953
a3c5fafd 4954 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4955 NULL, ALL_DOMAIN,
4956 [&] (offset_type idx)
4957 {
a3c5fafd 4958 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4959 const char *expected_str
4960 = expected_it == expected_end ? NULL : *expected_it++;
4961
4962 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4963 mismatch (expected_str, matched_name);
4964 });
4965
4966 const char *expected_str
4967 = expected_it == expected_end ? NULL : *expected_it++;
4968 if (expected_str != NULL)
4969 mismatch (expected_str, NULL);
4970
4971 return matched;
4972}
4973
4974/* The symbols added to the mock mapped_index for testing (in
4975 canonical form). */
4976static const char *test_symbols[] = {
4977 "function",
4978 "std::bar",
4979 "std::zfunction",
4980 "std::zfunction2",
4981 "w1::w2",
4982 "ns::foo<char*>",
4983 "ns::foo<int>",
4984 "ns::foo<long>",
a20714ff
PA
4985 "ns2::tmpl<int>::foo2",
4986 "(anonymous namespace)::A::B::C",
c62446b1 4987
e1ef7d7a
PA
4988 /* These are used to check that the increment-last-char in the
4989 matching algorithm for completion doesn't match "t1_fund" when
4990 completing "t1_func". */
4991 "t1_func",
4992 "t1_func1",
4993 "t1_fund",
4994 "t1_fund1",
4995
4996 /* A UTF-8 name with multi-byte sequences to make sure that
4997 cp-name-parser understands this as a single identifier ("função"
4998 is "function" in PT). */
4999 u8"u8função",
5000
5001 /* \377 (0xff) is Latin1 'ÿ'. */
5002 "yfunc\377",
5003
5004 /* \377 (0xff) is Latin1 'ÿ'. */
5005 "\377",
5006 "\377\377123",
5007
c62446b1
PA
5008 /* A name with all sorts of complications. Starts with "z" to make
5009 it easier for the completion tests below. */
5010#define Z_SYM_NAME \
5011 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
5012 "::tuple<(anonymous namespace)::ui*, " \
5013 "std::default_delete<(anonymous namespace)::ui>, void>"
5014
5015 Z_SYM_NAME
5016};
5017
a3c5fafd
PA
5018/* Returns true if the mapped_index_base::find_name_component_bounds
5019 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
5020 in completion mode. */
5c58de74
PA
5021
5022static bool
a3c5fafd 5023check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
5024 const char *search_name,
5025 gdb::array_view<const char *> expected_syms)
5026{
5027 lookup_name_info lookup_name (search_name,
5028 symbol_name_match_type::FULL, true);
5029
5030 auto bounds = index.find_name_components_bounds (lookup_name);
5031
5032 size_t distance = std::distance (bounds.first, bounds.second);
5033 if (distance != expected_syms.size ())
5034 return false;
5035
5036 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
5037 {
5038 auto nc_elem = bounds.first + exp_elem;
5039 const char *qualified = index.symbol_name_at (nc_elem->idx);
5040 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
5041 return false;
5042 }
5043
5044 return true;
5045}
5046
5047/* Test the lower-level mapped_index::find_name_component_bounds
5048 method. */
5049
c62446b1 5050static void
5c58de74
PA
5051test_mapped_index_find_name_component_bounds ()
5052{
5053 mock_mapped_index mock_index (test_symbols);
5054
a3c5fafd 5055 mock_index.build_name_components ();
5c58de74
PA
5056
5057 /* Test the lower-level mapped_index::find_name_component_bounds
5058 method in completion mode. */
5059 {
5060 static const char *expected_syms[] = {
5061 "t1_func",
5062 "t1_func1",
5c58de74
PA
5063 };
5064
a3c5fafd 5065 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
5066 "t1_func", expected_syms));
5067 }
5068
5069 /* Check that the increment-last-char in the name matching algorithm
5070 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
5071 {
5072 static const char *expected_syms1[] = {
5073 "\377",
5074 "\377\377123",
5075 };
a3c5fafd 5076 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
5077 "\377", expected_syms1));
5078
5079 static const char *expected_syms2[] = {
5080 "\377\377123",
5081 };
a3c5fafd 5082 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
5083 "\377\377", expected_syms2));
5084 }
5085}
5086
5087/* Test dw2_expand_symtabs_matching_symbol. */
5088
5089static void
5090test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
5091{
5092 mock_mapped_index mock_index (test_symbols);
5093
5094 /* We let all tests run until the end even if some fails, for debug
5095 convenience. */
5096 bool any_mismatch = false;
5097
5098 /* Create the expected symbols list (an initializer_list). Needed
5099 because lists have commas, and we need to pass them to CHECK,
5100 which is a macro. */
5101#define EXPECT(...) { __VA_ARGS__ }
5102
5103 /* Wrapper for check_match that passes down the current
5104 __FILE__/__LINE__. */
5105#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
5106 any_mismatch |= !check_match (__FILE__, __LINE__, \
5107 mock_index, \
5108 NAME, MATCH_TYPE, COMPLETION_MODE, \
5109 EXPECTED_LIST)
5110
5111 /* Identity checks. */
5112 for (const char *sym : test_symbols)
5113 {
5114 /* Should be able to match all existing symbols. */
5115 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
5116 EXPECT (sym));
5117
5118 /* Should be able to match all existing symbols with
5119 parameters. */
5120 std::string with_params = std::string (sym) + "(int)";
5121 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5122 EXPECT (sym));
5123
5124 /* Should be able to match all existing symbols with
5125 parameters and qualifiers. */
5126 with_params = std::string (sym) + " ( int ) const";
5127 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5128 EXPECT (sym));
5129
5130 /* This should really find sym, but cp-name-parser.y doesn't
5131 know about lvalue/rvalue qualifiers yet. */
5132 with_params = std::string (sym) + " ( int ) &&";
5133 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5134 {});
5135 }
5136
e1ef7d7a
PA
5137 /* Check that the name matching algorithm for completion doesn't get
5138 confused with Latin1 'ÿ' / 0xff. */
5139 {
5140 static const char str[] = "\377";
5141 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5142 EXPECT ("\377", "\377\377123"));
5143 }
5144
5145 /* Check that the increment-last-char in the matching algorithm for
5146 completion doesn't match "t1_fund" when completing "t1_func". */
5147 {
5148 static const char str[] = "t1_func";
5149 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5150 EXPECT ("t1_func", "t1_func1"));
5151 }
5152
c62446b1
PA
5153 /* Check that completion mode works at each prefix of the expected
5154 symbol name. */
5155 {
5156 static const char str[] = "function(int)";
5157 size_t len = strlen (str);
5158 std::string lookup;
5159
5160 for (size_t i = 1; i < len; i++)
5161 {
5162 lookup.assign (str, i);
5163 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5164 EXPECT ("function"));
5165 }
5166 }
5167
5168 /* While "w" is a prefix of both components, the match function
5169 should still only be called once. */
5170 {
5171 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
5172 EXPECT ("w1::w2"));
a20714ff
PA
5173 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
5174 EXPECT ("w1::w2"));
c62446b1
PA
5175 }
5176
5177 /* Same, with a "complicated" symbol. */
5178 {
5179 static const char str[] = Z_SYM_NAME;
5180 size_t len = strlen (str);
5181 std::string lookup;
5182
5183 for (size_t i = 1; i < len; i++)
5184 {
5185 lookup.assign (str, i);
5186 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5187 EXPECT (Z_SYM_NAME));
5188 }
5189 }
5190
5191 /* In FULL mode, an incomplete symbol doesn't match. */
5192 {
5193 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
5194 {});
5195 }
5196
5197 /* A complete symbol with parameters matches any overload, since the
5198 index has no overload info. */
5199 {
5200 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
5201 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
5202 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
5203 EXPECT ("std::zfunction", "std::zfunction2"));
5204 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
5205 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
5206 }
5207
5208 /* Check that whitespace is ignored appropriately. A symbol with a
5209 template argument list. */
5210 {
5211 static const char expected[] = "ns::foo<int>";
5212 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
5213 EXPECT (expected));
a20714ff
PA
5214 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
5215 EXPECT (expected));
c62446b1
PA
5216 }
5217
5218 /* Check that whitespace is ignored appropriately. A symbol with a
5219 template argument list that includes a pointer. */
5220 {
5221 static const char expected[] = "ns::foo<char*>";
5222 /* Try both completion and non-completion modes. */
5223 static const bool completion_mode[2] = {false, true};
5224 for (size_t i = 0; i < 2; i++)
5225 {
5226 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
5227 completion_mode[i], EXPECT (expected));
a20714ff
PA
5228 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
5229 completion_mode[i], EXPECT (expected));
c62446b1
PA
5230
5231 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
5232 completion_mode[i], EXPECT (expected));
a20714ff
PA
5233 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
5234 completion_mode[i], EXPECT (expected));
c62446b1
PA
5235 }
5236 }
5237
5238 {
5239 /* Check method qualifiers are ignored. */
5240 static const char expected[] = "ns::foo<char*>";
5241 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
5242 symbol_name_match_type::FULL, true, EXPECT (expected));
5243 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
5244 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
5245 CHECK_MATCH ("foo < char * > ( int ) const",
5246 symbol_name_match_type::WILD, true, EXPECT (expected));
5247 CHECK_MATCH ("foo < char * > ( int ) &&",
5248 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
5249 }
5250
5251 /* Test lookup names that don't match anything. */
5252 {
a20714ff
PA
5253 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
5254 {});
5255
c62446b1
PA
5256 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
5257 {});
5258 }
5259
a20714ff
PA
5260 /* Some wild matching tests, exercising "(anonymous namespace)",
5261 which should not be confused with a parameter list. */
5262 {
5263 static const char *syms[] = {
5264 "A::B::C",
5265 "B::C",
5266 "C",
5267 "A :: B :: C ( int )",
5268 "B :: C ( int )",
5269 "C ( int )",
5270 };
5271
5272 for (const char *s : syms)
5273 {
5274 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
5275 EXPECT ("(anonymous namespace)::A::B::C"));
5276 }
5277 }
5278
5279 {
5280 static const char expected[] = "ns2::tmpl<int>::foo2";
5281 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
5282 EXPECT (expected));
5283 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5284 EXPECT (expected));
5285 }
5286
c62446b1
PA
5287 SELF_CHECK (!any_mismatch);
5288
5289#undef EXPECT
5290#undef CHECK_MATCH
5291}
5292
5c58de74
PA
5293static void
5294run_test ()
5295{
5296 test_mapped_index_find_name_component_bounds ();
5297 test_dw2_expand_symtabs_matching_symbol ();
5298}
5299
c62446b1
PA
5300}} // namespace selftests::dw2_expand_symtabs_matching
5301
5302#endif /* GDB_SELF_TEST */
5303
4b514bc8
JK
5304/* If FILE_MATCHER is NULL or if PER_CU has
5305 dwarf2_per_cu_quick_data::MARK set (see
5306 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5307 EXPANSION_NOTIFY on it. */
5308
5309static void
5310dw2_expand_symtabs_matching_one
5311 (struct dwarf2_per_cu_data *per_cu,
5312 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5313 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5314{
5315 if (file_matcher == NULL || per_cu->v.quick->mark)
5316 {
5317 bool symtab_was_null
5318 = (per_cu->v.quick->compunit_symtab == NULL);
5319
5320 dw2_instantiate_symtab (per_cu);
5321
5322 if (expansion_notify != NULL
5323 && symtab_was_null
5324 && per_cu->v.quick->compunit_symtab != NULL)
5325 expansion_notify (per_cu->v.quick->compunit_symtab);
5326 }
5327}
5328
3f563c84
PA
5329/* Helper for dw2_expand_matching symtabs. Called on each symbol
5330 matched, to expand corresponding CUs that were marked. IDX is the
5331 index of the symbol name that matched. */
5332
5333static void
5334dw2_expand_marked_cus
ed2dc618 5335 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
5336 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5337 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5338 search_domain kind)
5339{
3f563c84
PA
5340 offset_type *vec, vec_len, vec_idx;
5341 bool global_seen = false;
ed2dc618 5342 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 5343
61920122 5344 vec = (offset_type *) (index.constant_pool
f00a2de2 5345 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5346 vec_len = MAYBE_SWAP (vec[0]);
5347 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5348 {
5349 struct dwarf2_per_cu_data *per_cu;
5350 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5351 /* This value is only valid for index versions >= 7. */
5352 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5353 gdb_index_symbol_kind symbol_kind =
5354 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5355 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5356 /* Only check the symbol attributes if they're present.
5357 Indices prior to version 7 don't record them,
5358 and indices >= 7 may elide them for certain symbols
5359 (gold does this). */
5360 int attrs_valid =
5361 (index.version >= 7
5362 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5363
5364 /* Work around gold/15646. */
5365 if (attrs_valid)
9291a0cd 5366 {
61920122
PA
5367 if (!is_static && global_seen)
5368 continue;
5369 if (!is_static)
5370 global_seen = true;
5371 }
3190f0c6 5372
61920122
PA
5373 /* Only check the symbol's kind if it has one. */
5374 if (attrs_valid)
5375 {
5376 switch (kind)
8943b874 5377 {
61920122
PA
5378 case VARIABLES_DOMAIN:
5379 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5380 continue;
5381 break;
5382 case FUNCTIONS_DOMAIN:
5383 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5384 continue;
61920122
PA
5385 break;
5386 case TYPES_DOMAIN:
5387 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5388 continue;
5389 break;
5390 default:
5391 break;
8943b874 5392 }
61920122 5393 }
8943b874 5394
61920122
PA
5395 /* Don't crash on bad data. */
5396 if (cu_index >= (dwarf2_per_objfile->n_comp_units
5397 + dwarf2_per_objfile->n_type_units))
5398 {
5399 complaint (&symfile_complaints,
5400 _(".gdb_index entry has bad CU index"
ed2dc618
SM
5401 " [in module %s]"),
5402 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
5403 continue;
5404 }
5405
ed2dc618 5406 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
4b514bc8
JK
5407 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5408 expansion_notify);
61920122
PA
5409 }
5410}
5411
4b514bc8
JK
5412/* If FILE_MATCHER is non-NULL, set all the
5413 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5414 that match FILE_MATCHER. */
5415
61920122 5416static void
4b514bc8 5417dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
5418 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5419 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5420{
4b514bc8 5421 if (file_matcher == NULL)
61920122
PA
5422 return;
5423
4b514bc8
JK
5424 objfile *const objfile = dwarf2_per_objfile->objfile;
5425
5426 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5427 htab_eq_pointer,
5428 NULL, xcalloc, xfree));
5429 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5430 htab_eq_pointer,
5431 NULL, xcalloc, xfree));
61920122 5432
4b514bc8
JK
5433 /* The rule is CUs specify all the files, including those used by
5434 any TU, so there's no need to scan TUs here. */
61920122 5435
927aa2e7
JK
5436 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5437 {
5438 int j;
ed2dc618 5439 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
927aa2e7
JK
5440 struct quick_file_names *file_data;
5441 void **slot;
5442
5443 QUIT;
5444
5445 per_cu->v.quick->mark = 0;
5446
5447 /* We only need to look at symtabs not already expanded. */
5448 if (per_cu->v.quick->compunit_symtab)
5449 continue;
5450
5451 file_data = dw2_get_file_names (per_cu);
5452 if (file_data == NULL)
5453 continue;
5454
5455 if (htab_find (visited_not_found.get (), file_data) != NULL)
5456 continue;
5457 else if (htab_find (visited_found.get (), file_data) != NULL)
5458 {
5459 per_cu->v.quick->mark = 1;
5460 continue;
5461 }
5462
5463 for (j = 0; j < file_data->num_file_names; ++j)
5464 {
5465 const char *this_real_name;
5466
5467 if (file_matcher (file_data->file_names[j], false))
5468 {
5469 per_cu->v.quick->mark = 1;
5470 break;
5471 }
5472
5473 /* Before we invoke realpath, which can get expensive when many
5474 files are involved, do a quick comparison of the basenames. */
5475 if (!basenames_may_differ
5476 && !file_matcher (lbasename (file_data->file_names[j]),
5477 true))
5478 continue;
5479
5480 this_real_name = dw2_get_real_path (objfile, file_data, j);
5481 if (file_matcher (this_real_name, false))
5482 {
5483 per_cu->v.quick->mark = 1;
5484 break;
5485 }
5486 }
5487
5488 slot = htab_find_slot (per_cu->v.quick->mark
5489 ? visited_found.get ()
5490 : visited_not_found.get (),
5491 file_data, INSERT);
5492 *slot = file_data;
5493 }
5494}
5495
5496static void
5497dw2_expand_symtabs_matching
5498 (struct objfile *objfile,
5499 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5500 const lookup_name_info &lookup_name,
5501 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5502 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5503 enum search_domain kind)
5504{
ed2dc618
SM
5505 struct dwarf2_per_objfile *dwarf2_per_objfile
5506 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5507
5508 /* index_table is NULL if OBJF_READNOW. */
5509 if (!dwarf2_per_objfile->index_table)
5510 return;
5511
ed2dc618 5512 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
5513
5514 mapped_index &index = *dwarf2_per_objfile->index_table;
5515
5516 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5517 symbol_matcher,
5518 kind, [&] (offset_type idx)
5519 {
ed2dc618 5520 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7
JK
5521 expansion_notify, kind);
5522 });
5523}
5524
5525/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5526 symtab. */
5527
5528static struct compunit_symtab *
5529recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5530 CORE_ADDR pc)
5531{
5532 int i;
5533
5534 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5535 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5536 return cust;
5537
5538 if (cust->includes == NULL)
5539 return NULL;
5540
5541 for (i = 0; cust->includes[i]; ++i)
5542 {
5543 struct compunit_symtab *s = cust->includes[i];
5544
5545 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5546 if (s != NULL)
5547 return s;
5548 }
5549
5550 return NULL;
5551}
5552
5553static struct compunit_symtab *
5554dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5555 struct bound_minimal_symbol msymbol,
5556 CORE_ADDR pc,
5557 struct obj_section *section,
5558 int warn_if_readin)
5559{
5560 struct dwarf2_per_cu_data *data;
5561 struct compunit_symtab *result;
5562
927aa2e7
JK
5563 if (!objfile->psymtabs_addrmap)
5564 return NULL;
5565
5566 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5567 pc);
5568 if (!data)
5569 return NULL;
5570
5571 if (warn_if_readin && data->v.quick->compunit_symtab)
5572 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5573 paddress (get_objfile_arch (objfile), pc));
5574
5575 result
5576 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5577 pc);
5578 gdb_assert (result != NULL);
5579 return result;
5580}
5581
5582static void
5583dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5584 void *data, int need_fullname)
5585{
ed2dc618
SM
5586 struct dwarf2_per_objfile *dwarf2_per_objfile
5587 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5588
5589 if (!dwarf2_per_objfile->filenames_cache)
5590 {
5591 dwarf2_per_objfile->filenames_cache.emplace ();
5592
5593 htab_up visited (htab_create_alloc (10,
5594 htab_hash_pointer, htab_eq_pointer,
5595 NULL, xcalloc, xfree));
5596
5597 /* The rule is CUs specify all the files, including those used
5598 by any TU, so there's no need to scan TUs here. We can
5599 ignore file names coming from already-expanded CUs. */
5600
5601 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5602 {
ed2dc618 5603 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
927aa2e7
JK
5604
5605 if (per_cu->v.quick->compunit_symtab)
5606 {
5607 void **slot = htab_find_slot (visited.get (),
5608 per_cu->v.quick->file_names,
5609 INSERT);
5610
5611 *slot = per_cu->v.quick->file_names;
5612 }
5613 }
5614
5615 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5616 {
ed2dc618 5617 dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
927aa2e7
JK
5618 struct quick_file_names *file_data;
5619 void **slot;
5620
5621 /* We only need to look at symtabs not already expanded. */
5622 if (per_cu->v.quick->compunit_symtab)
5623 continue;
5624
5625 file_data = dw2_get_file_names (per_cu);
5626 if (file_data == NULL)
5627 continue;
5628
5629 slot = htab_find_slot (visited.get (), file_data, INSERT);
5630 if (*slot)
5631 {
5632 /* Already visited. */
5633 continue;
5634 }
5635 *slot = file_data;
5636
5637 for (int j = 0; j < file_data->num_file_names; ++j)
5638 {
5639 const char *filename = file_data->file_names[j];
5640 dwarf2_per_objfile->filenames_cache->seen (filename);
5641 }
5642 }
5643 }
5644
5645 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5646 {
5647 gdb::unique_xmalloc_ptr<char> this_real_name;
5648
5649 if (need_fullname)
5650 this_real_name = gdb_realpath (filename);
5651 (*fun) (filename, this_real_name.get (), data);
5652 });
5653}
5654
5655static int
5656dw2_has_symbols (struct objfile *objfile)
5657{
5658 return 1;
5659}
5660
5661const struct quick_symbol_functions dwarf2_gdb_index_functions =
5662{
5663 dw2_has_symbols,
5664 dw2_find_last_source_symtab,
5665 dw2_forget_cached_source_info,
5666 dw2_map_symtabs_matching_filename,
5667 dw2_lookup_symbol,
5668 dw2_print_stats,
5669 dw2_dump,
5670 dw2_relocate,
5671 dw2_expand_symtabs_for_function,
5672 dw2_expand_all_symtabs,
5673 dw2_expand_symtabs_with_fullname,
5674 dw2_map_matching_symbols,
5675 dw2_expand_symtabs_matching,
5676 dw2_find_pc_sect_compunit_symtab,
5677 NULL,
5678 dw2_map_symbol_filenames
5679};
5680
5681/* DWARF-5 debug_names reader. */
5682
5683/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5684static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5685
5686/* A helper function that reads the .debug_names section in SECTION
5687 and fills in MAP. FILENAME is the name of the file containing the
5688 section; it is used for error reporting.
5689
5690 Returns true if all went well, false otherwise. */
5691
5692static bool
5693read_debug_names_from_section (struct objfile *objfile,
5694 const char *filename,
5695 struct dwarf2_section_info *section,
5696 mapped_debug_names &map)
5697{
5698 if (dwarf2_section_empty_p (section))
5699 return false;
5700
5701 /* Older elfutils strip versions could keep the section in the main
5702 executable while splitting it for the separate debug info file. */
5703 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5704 return false;
5705
5706 dwarf2_read_section (objfile, section);
5707
5708 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5709
5710 const gdb_byte *addr = section->buffer;
5711
5712 bfd *const abfd = get_section_bfd_owner (section);
5713
5714 unsigned int bytes_read;
5715 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5716 addr += bytes_read;
5717
5718 map.dwarf5_is_dwarf64 = bytes_read != 4;
5719 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5720 if (bytes_read + length != section->size)
5721 {
5722 /* There may be multiple per-CU indices. */
5723 warning (_("Section .debug_names in %s length %s does not match "
5724 "section length %s, ignoring .debug_names."),
5725 filename, plongest (bytes_read + length),
5726 pulongest (section->size));
5727 return false;
5728 }
5729
5730 /* The version number. */
5731 uint16_t version = read_2_bytes (abfd, addr);
5732 addr += 2;
5733 if (version != 5)
5734 {
5735 warning (_("Section .debug_names in %s has unsupported version %d, "
5736 "ignoring .debug_names."),
5737 filename, version);
5738 return false;
5739 }
5740
5741 /* Padding. */
5742 uint16_t padding = read_2_bytes (abfd, addr);
5743 addr += 2;
5744 if (padding != 0)
5745 {
5746 warning (_("Section .debug_names in %s has unsupported padding %d, "
5747 "ignoring .debug_names."),
5748 filename, padding);
5749 return false;
5750 }
5751
5752 /* comp_unit_count - The number of CUs in the CU list. */
5753 map.cu_count = read_4_bytes (abfd, addr);
5754 addr += 4;
5755
5756 /* local_type_unit_count - The number of TUs in the local TU
5757 list. */
5758 map.tu_count = read_4_bytes (abfd, addr);
5759 addr += 4;
5760
5761 /* foreign_type_unit_count - The number of TUs in the foreign TU
5762 list. */
5763 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5764 addr += 4;
5765 if (foreign_tu_count != 0)
5766 {
5767 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5768 "ignoring .debug_names."),
5769 filename, static_cast<unsigned long> (foreign_tu_count));
5770 return false;
5771 }
5772
5773 /* bucket_count - The number of hash buckets in the hash lookup
5774 table. */
5775 map.bucket_count = read_4_bytes (abfd, addr);
5776 addr += 4;
5777
5778 /* name_count - The number of unique names in the index. */
5779 map.name_count = read_4_bytes (abfd, addr);
5780 addr += 4;
5781
5782 /* abbrev_table_size - The size in bytes of the abbreviations
5783 table. */
5784 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5785 addr += 4;
5786
5787 /* augmentation_string_size - The size in bytes of the augmentation
5788 string. This value is rounded up to a multiple of 4. */
5789 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5790 addr += 4;
5791 map.augmentation_is_gdb = ((augmentation_string_size
5792 == sizeof (dwarf5_augmentation))
5793 && memcmp (addr, dwarf5_augmentation,
5794 sizeof (dwarf5_augmentation)) == 0);
5795 augmentation_string_size += (-augmentation_string_size) & 3;
5796 addr += augmentation_string_size;
5797
5798 /* List of CUs */
5799 map.cu_table_reordered = addr;
5800 addr += map.cu_count * map.offset_size;
5801
5802 /* List of Local TUs */
5803 map.tu_table_reordered = addr;
5804 addr += map.tu_count * map.offset_size;
5805
5806 /* Hash Lookup Table */
5807 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5808 addr += map.bucket_count * 4;
5809 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5810 addr += map.name_count * 4;
5811
5812 /* Name Table */
5813 map.name_table_string_offs_reordered = addr;
5814 addr += map.name_count * map.offset_size;
5815 map.name_table_entry_offs_reordered = addr;
5816 addr += map.name_count * map.offset_size;
5817
5818 const gdb_byte *abbrev_table_start = addr;
5819 for (;;)
5820 {
5821 unsigned int bytes_read;
5822 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5823 addr += bytes_read;
5824 if (index_num == 0)
5825 break;
5826
5827 const auto insertpair
5828 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5829 if (!insertpair.second)
5830 {
5831 warning (_("Section .debug_names in %s has duplicate index %s, "
5832 "ignoring .debug_names."),
5833 filename, pulongest (index_num));
5834 return false;
5835 }
5836 mapped_debug_names::index_val &indexval = insertpair.first->second;
5837 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5838 addr += bytes_read;
5839
5840 for (;;)
5841 {
5842 mapped_debug_names::index_val::attr attr;
5843 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5844 addr += bytes_read;
5845 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5846 addr += bytes_read;
5847 if (attr.form == DW_FORM_implicit_const)
5848 {
5849 attr.implicit_const = read_signed_leb128 (abfd, addr,
5850 &bytes_read);
5851 addr += bytes_read;
5852 }
5853 if (attr.dw_idx == 0 && attr.form == 0)
5854 break;
5855 indexval.attr_vec.push_back (std::move (attr));
5856 }
5857 }
5858 if (addr != abbrev_table_start + abbrev_table_size)
5859 {
5860 warning (_("Section .debug_names in %s has abbreviation_table "
5861 "of size %zu vs. written as %u, ignoring .debug_names."),
5862 filename, addr - abbrev_table_start, abbrev_table_size);
5863 return false;
5864 }
5865 map.entry_pool = addr;
5866
5867 return true;
5868}
5869
5870/* A helper for create_cus_from_debug_names that handles the MAP's CU
5871 list. */
5872
5873static void
ed2dc618 5874create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5875 const mapped_debug_names &map,
5876 dwarf2_section_info &section,
5877 bool is_dwz, int base_offset)
5878{
5879 sect_offset sect_off_prev;
5880 for (uint32_t i = 0; i <= map.cu_count; ++i)
5881 {
5882 sect_offset sect_off_next;
5883 if (i < map.cu_count)
5884 {
5885 sect_off_next
5886 = (sect_offset) (extract_unsigned_integer
5887 (map.cu_table_reordered + i * map.offset_size,
5888 map.offset_size,
5889 map.dwarf5_byte_order));
5890 }
5891 else
5892 sect_off_next = (sect_offset) section.size;
5893 if (i >= 1)
5894 {
5895 const ULONGEST length = sect_off_next - sect_off_prev;
5896 dwarf2_per_objfile->all_comp_units[base_offset + (i - 1)]
ed2dc618 5897 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7
JK
5898 sect_off_prev, length);
5899 }
5900 sect_off_prev = sect_off_next;
5901 }
5902}
5903
5904/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5905 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5906
5907static void
ed2dc618 5908create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5909 const mapped_debug_names &map,
5910 const mapped_debug_names &dwz_map)
5911{
ed2dc618 5912 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5913
5914 dwarf2_per_objfile->n_comp_units = map.cu_count + dwz_map.cu_count;
5915 dwarf2_per_objfile->all_comp_units
5916 = XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
5917 dwarf2_per_objfile->n_comp_units);
5918
ed2dc618
SM
5919 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5920 dwarf2_per_objfile->info,
927aa2e7
JK
5921 false /* is_dwz */,
5922 0 /* base_offset */);
5923
5924 if (dwz_map.cu_count == 0)
5925 return;
5926
ed2dc618
SM
5927 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5928 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
927aa2e7
JK
5929 true /* is_dwz */,
5930 map.cu_count /* base_offset */);
5931}
5932
5933/* Read .debug_names. If everything went ok, initialize the "quick"
5934 elements of all the CUs and return true. Otherwise, return false. */
5935
5936static bool
ed2dc618 5937dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5938{
ed2dc618
SM
5939 mapped_debug_names local_map (dwarf2_per_objfile);
5940 mapped_debug_names dwz_map (dwarf2_per_objfile);
5941 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5942
5943 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5944 &dwarf2_per_objfile->debug_names,
5945 local_map))
5946 return false;
5947
5948 /* Don't use the index if it's empty. */
5949 if (local_map.name_count == 0)
5950 return false;
5951
5952 /* If there is a .dwz file, read it so we can get its CU list as
5953 well. */
ed2dc618 5954 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5955 if (dwz != NULL)
5956 {
5957 if (!read_debug_names_from_section (objfile,
5958 bfd_get_filename (dwz->dwz_bfd),
5959 &dwz->debug_names, dwz_map))
5960 {
5961 warning (_("could not read '.debug_names' section from %s; skipping"),
5962 bfd_get_filename (dwz->dwz_bfd));
5963 return false;
5964 }
5965 }
5966
ed2dc618 5967 create_cus_from_debug_names (dwarf2_per_objfile, local_map, dwz_map);
927aa2e7
JK
5968
5969 if (local_map.tu_count != 0)
5970 {
5971 /* We can only handle a single .debug_types when we have an
5972 index. */
5973 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
5974 return false;
5975
5976 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
5977 dwarf2_per_objfile->types, 0);
5978
5979 create_signatured_type_table_from_debug_names
ed2dc618 5980 (dwarf2_per_objfile, local_map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5981 }
5982
ed2dc618
SM
5983 create_addrmap_from_aranges (dwarf2_per_objfile,
5984 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5985
ed2dc618
SM
5986 dwarf2_per_objfile->debug_names_table.reset
5987 (new mapped_debug_names (dwarf2_per_objfile));
927aa2e7
JK
5988 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
5989 dwarf2_per_objfile->using_index = 1;
5990 dwarf2_per_objfile->quick_file_names_table =
5991 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
5992
5993 return true;
5994}
5995
5996/* Symbol name hashing function as specified by DWARF-5. */
5997
5998static uint32_t
5999dwarf5_djb_hash (const char *str_)
6000{
6001 const unsigned char *str = (const unsigned char *) str_;
6002
6003 /* Note: tolower here ignores UTF-8, which isn't fully compliant.
6004 See http://dwarfstd.org/ShowIssue.php?issue=161027.1. */
6005
6006 uint32_t hash = 5381;
6007 while (int c = *str++)
6008 hash = hash * 33 + tolower (c);
6009 return hash;
6010}
6011
6012/* Type used to manage iterating over all CUs looking for a symbol for
6013 .debug_names. */
6014
6015class dw2_debug_names_iterator
6016{
6017public:
6018 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
6019 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
6020 dw2_debug_names_iterator (const mapped_debug_names &map,
6021 bool want_specific_block,
6022 block_enum block_index, domain_enum domain,
6023 const char *name)
6024 : m_map (map), m_want_specific_block (want_specific_block),
6025 m_block_index (block_index), m_domain (domain),
6026 m_addr (find_vec_in_debug_names (map, name))
6027 {}
6028
6029 dw2_debug_names_iterator (const mapped_debug_names &map,
6030 search_domain search, uint32_t namei)
6031 : m_map (map),
6032 m_search (search),
6033 m_addr (find_vec_in_debug_names (map, namei))
6034 {}
6035
6036 /* Return the next matching CU or NULL if there are no more. */
6037 dwarf2_per_cu_data *next ();
6038
6039private:
6040 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6041 const char *name);
6042 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6043 uint32_t namei);
6044
6045 /* The internalized form of .debug_names. */
6046 const mapped_debug_names &m_map;
6047
6048 /* If true, only look for symbols that match BLOCK_INDEX. */
6049 const bool m_want_specific_block = false;
6050
6051 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
6052 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
6053 value. */
6054 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
6055
6056 /* The kind of symbol we're looking for. */
6057 const domain_enum m_domain = UNDEF_DOMAIN;
6058 const search_domain m_search = ALL_DOMAIN;
6059
6060 /* The list of CUs from the index entry of the symbol, or NULL if
6061 not found. */
6062 const gdb_byte *m_addr;
6063};
6064
6065const char *
6066mapped_debug_names::namei_to_name (uint32_t namei) const
6067{
6068 const ULONGEST namei_string_offs
6069 = extract_unsigned_integer ((name_table_string_offs_reordered
6070 + namei * offset_size),
6071 offset_size,
6072 dwarf5_byte_order);
6073 return read_indirect_string_at_offset
ed2dc618 6074 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
6075}
6076
6077/* Find a slot in .debug_names for the object named NAME. If NAME is
6078 found, return pointer to its pool data. If NAME cannot be found,
6079 return NULL. */
6080
6081const gdb_byte *
6082dw2_debug_names_iterator::find_vec_in_debug_names
6083 (const mapped_debug_names &map, const char *name)
6084{
6085 int (*cmp) (const char *, const char *);
6086
6087 if (current_language->la_language == language_cplus
6088 || current_language->la_language == language_fortran
6089 || current_language->la_language == language_d)
6090 {
6091 /* NAME is already canonical. Drop any qualifiers as
6092 .debug_names does not contain any. */
6093
6094 if (strchr (name, '(') != NULL)
6095 {
6096 gdb::unique_xmalloc_ptr<char> without_params
6097 = cp_remove_params (name);
6098
6099 if (without_params != NULL)
6100 {
6101 name = without_params.get();
6102 }
6103 }
6104 }
6105
6106 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
6107
6108 const uint32_t full_hash = dwarf5_djb_hash (name);
6109 uint32_t namei
6110 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6111 (map.bucket_table_reordered
6112 + (full_hash % map.bucket_count)), 4,
6113 map.dwarf5_byte_order);
6114 if (namei == 0)
6115 return NULL;
6116 --namei;
6117 if (namei >= map.name_count)
6118 {
6119 complaint (&symfile_complaints,
6120 _("Wrong .debug_names with name index %u but name_count=%u "
6121 "[in module %s]"),
6122 namei, map.name_count,
ed2dc618 6123 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
6124 return NULL;
6125 }
6126
6127 for (;;)
6128 {
6129 const uint32_t namei_full_hash
6130 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6131 (map.hash_table_reordered + namei), 4,
6132 map.dwarf5_byte_order);
6133 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
6134 return NULL;
6135
6136 if (full_hash == namei_full_hash)
6137 {
6138 const char *const namei_string = map.namei_to_name (namei);
6139
6140#if 0 /* An expensive sanity check. */
6141 if (namei_full_hash != dwarf5_djb_hash (namei_string))
6142 {
6143 complaint (&symfile_complaints,
6144 _("Wrong .debug_names hash for string at index %u "
6145 "[in module %s]"),
6146 namei, objfile_name (dwarf2_per_objfile->objfile));
6147 return NULL;
6148 }
6149#endif
6150
6151 if (cmp (namei_string, name) == 0)
6152 {
6153 const ULONGEST namei_entry_offs
6154 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6155 + namei * map.offset_size),
6156 map.offset_size, map.dwarf5_byte_order);
6157 return map.entry_pool + namei_entry_offs;
6158 }
6159 }
6160
6161 ++namei;
6162 if (namei >= map.name_count)
6163 return NULL;
6164 }
6165}
6166
6167const gdb_byte *
6168dw2_debug_names_iterator::find_vec_in_debug_names
6169 (const mapped_debug_names &map, uint32_t namei)
6170{
6171 if (namei >= map.name_count)
6172 {
6173 complaint (&symfile_complaints,
6174 _("Wrong .debug_names with name index %u but name_count=%u "
6175 "[in module %s]"),
6176 namei, map.name_count,
ed2dc618 6177 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
6178 return NULL;
6179 }
6180
6181 const ULONGEST namei_entry_offs
6182 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6183 + namei * map.offset_size),
6184 map.offset_size, map.dwarf5_byte_order);
6185 return map.entry_pool + namei_entry_offs;
6186}
6187
6188/* See dw2_debug_names_iterator. */
6189
6190dwarf2_per_cu_data *
6191dw2_debug_names_iterator::next ()
6192{
6193 if (m_addr == NULL)
6194 return NULL;
6195
ed2dc618
SM
6196 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
6197 struct objfile *objfile = dwarf2_per_objfile->objfile;
6198 bfd *const abfd = objfile->obfd;
927aa2e7
JK
6199
6200 again:
6201
6202 unsigned int bytes_read;
6203 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6204 m_addr += bytes_read;
6205 if (abbrev == 0)
6206 return NULL;
6207
6208 const auto indexval_it = m_map.abbrev_map.find (abbrev);
6209 if (indexval_it == m_map.abbrev_map.cend ())
6210 {
6211 complaint (&symfile_complaints,
6212 _("Wrong .debug_names undefined abbrev code %s "
6213 "[in module %s]"),
ed2dc618 6214 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
6215 return NULL;
6216 }
6217 const mapped_debug_names::index_val &indexval = indexval_it->second;
6218 bool have_is_static = false;
6219 bool is_static;
6220 dwarf2_per_cu_data *per_cu = NULL;
6221 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
6222 {
6223 ULONGEST ull;
6224 switch (attr.form)
6225 {
6226 case DW_FORM_implicit_const:
6227 ull = attr.implicit_const;
6228 break;
6229 case DW_FORM_flag_present:
6230 ull = 1;
6231 break;
6232 case DW_FORM_udata:
6233 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6234 m_addr += bytes_read;
6235 break;
6236 default:
6237 complaint (&symfile_complaints,
6238 _("Unsupported .debug_names form %s [in module %s]"),
6239 dwarf_form_name (attr.form),
ed2dc618 6240 objfile_name (objfile));
927aa2e7
JK
6241 return NULL;
6242 }
6243 switch (attr.dw_idx)
6244 {
6245 case DW_IDX_compile_unit:
6246 /* Don't crash on bad data. */
8af5c486 6247 if (ull >= dwarf2_per_objfile->n_comp_units)
927aa2e7
JK
6248 {
6249 complaint (&symfile_complaints,
6250 _(".debug_names entry has bad CU index %s"
6251 " [in module %s]"),
6252 pulongest (ull),
6253 objfile_name (dwarf2_per_objfile->objfile));
6254 continue;
6255 }
ed2dc618 6256 per_cu = dw2_get_cutu (dwarf2_per_objfile, ull);
927aa2e7 6257 break;
8af5c486
JK
6258 case DW_IDX_type_unit:
6259 /* Don't crash on bad data. */
6260 if (ull >= dwarf2_per_objfile->n_type_units)
6261 {
6262 complaint (&symfile_complaints,
6263 _(".debug_names entry has bad TU index %s"
6264 " [in module %s]"),
6265 pulongest (ull),
6266 objfile_name (dwarf2_per_objfile->objfile));
6267 continue;
6268 }
ed2dc618
SM
6269 per_cu = dw2_get_cutu (dwarf2_per_objfile,
6270 dwarf2_per_objfile->n_comp_units + ull);
8af5c486 6271 break;
927aa2e7
JK
6272 case DW_IDX_GNU_internal:
6273 if (!m_map.augmentation_is_gdb)
6274 break;
6275 have_is_static = true;
6276 is_static = true;
6277 break;
6278 case DW_IDX_GNU_external:
6279 if (!m_map.augmentation_is_gdb)
6280 break;
6281 have_is_static = true;
6282 is_static = false;
6283 break;
6284 }
6285 }
6286
6287 /* Skip if already read in. */
6288 if (per_cu->v.quick->compunit_symtab)
6289 goto again;
6290
6291 /* Check static vs global. */
6292 if (have_is_static)
6293 {
6294 const bool want_static = m_block_index != GLOBAL_BLOCK;
6295 if (m_want_specific_block && want_static != is_static)
6296 goto again;
6297 }
6298
6299 /* Match dw2_symtab_iter_next, symbol_kind
6300 and debug_names::psymbol_tag. */
6301 switch (m_domain)
6302 {
6303 case VAR_DOMAIN:
6304 switch (indexval.dwarf_tag)
6305 {
6306 case DW_TAG_variable:
6307 case DW_TAG_subprogram:
6308 /* Some types are also in VAR_DOMAIN. */
6309 case DW_TAG_typedef:
6310 case DW_TAG_structure_type:
6311 break;
6312 default:
6313 goto again;
6314 }
6315 break;
6316 case STRUCT_DOMAIN:
6317 switch (indexval.dwarf_tag)
6318 {
6319 case DW_TAG_typedef:
6320 case DW_TAG_structure_type:
6321 break;
6322 default:
6323 goto again;
6324 }
6325 break;
6326 case LABEL_DOMAIN:
6327 switch (indexval.dwarf_tag)
6328 {
6329 case 0:
6330 case DW_TAG_variable:
6331 break;
6332 default:
6333 goto again;
6334 }
6335 break;
6336 default:
6337 break;
6338 }
6339
6340 /* Match dw2_expand_symtabs_matching, symbol_kind and
6341 debug_names::psymbol_tag. */
6342 switch (m_search)
4b514bc8 6343 {
927aa2e7
JK
6344 case VARIABLES_DOMAIN:
6345 switch (indexval.dwarf_tag)
4b514bc8 6346 {
927aa2e7
JK
6347 case DW_TAG_variable:
6348 break;
6349 default:
6350 goto again;
4b514bc8 6351 }
927aa2e7
JK
6352 break;
6353 case FUNCTIONS_DOMAIN:
6354 switch (indexval.dwarf_tag)
4b514bc8 6355 {
927aa2e7
JK
6356 case DW_TAG_subprogram:
6357 break;
6358 default:
6359 goto again;
4b514bc8 6360 }
927aa2e7
JK
6361 break;
6362 case TYPES_DOMAIN:
6363 switch (indexval.dwarf_tag)
6364 {
6365 case DW_TAG_typedef:
6366 case DW_TAG_structure_type:
6367 break;
6368 default:
6369 goto again;
6370 }
6371 break;
6372 default:
6373 break;
4b514bc8 6374 }
927aa2e7
JK
6375
6376 return per_cu;
4b514bc8 6377}
61920122 6378
927aa2e7
JK
6379static struct compunit_symtab *
6380dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6381 const char *name, domain_enum domain)
4b514bc8 6382{
927aa2e7 6383 const block_enum block_index = static_cast<block_enum> (block_index_int);
ed2dc618
SM
6384 struct dwarf2_per_objfile *dwarf2_per_objfile
6385 = get_dwarf2_per_objfile (objfile);
61920122 6386
927aa2e7
JK
6387 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6388 if (!mapp)
61920122 6389 {
927aa2e7
JK
6390 /* index is NULL if OBJF_READNOW. */
6391 return NULL;
6392 }
6393 const auto &map = *mapp;
9291a0cd 6394
927aa2e7
JK
6395 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6396 block_index, domain, name);
9703b513 6397
927aa2e7
JK
6398 struct compunit_symtab *stab_best = NULL;
6399 struct dwarf2_per_cu_data *per_cu;
6400 while ((per_cu = iter.next ()) != NULL)
6401 {
6402 struct symbol *sym, *with_opaque = NULL;
6403 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6404 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6405 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6406
927aa2e7
JK
6407 sym = block_find_symbol (block, name, domain,
6408 block_find_non_opaque_type_preferred,
6409 &with_opaque);
9703b513 6410
927aa2e7
JK
6411 /* Some caution must be observed with overloaded functions and
6412 methods, since the index will not contain any overload
6413 information (but NAME might contain it). */
a3ec0bb1 6414
927aa2e7
JK
6415 if (sym != NULL
6416 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6417 return stab;
6418 if (with_opaque != NULL
6419 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6420 stab_best = stab;
9703b513 6421
927aa2e7 6422 /* Keep looking through other CUs. */
9703b513
TT
6423 }
6424
927aa2e7 6425 return stab_best;
9703b513
TT
6426}
6427
927aa2e7
JK
6428/* This dumps minimal information about .debug_names. It is called
6429 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6430 uses this to verify that .debug_names has been loaded. */
9291a0cd 6431
927aa2e7
JK
6432static void
6433dw2_debug_names_dump (struct objfile *objfile)
6434{
ed2dc618
SM
6435 struct dwarf2_per_objfile *dwarf2_per_objfile
6436 = get_dwarf2_per_objfile (objfile);
6437
927aa2e7
JK
6438 gdb_assert (dwarf2_per_objfile->using_index);
6439 printf_filtered (".debug_names:");
6440 if (dwarf2_per_objfile->debug_names_table)
6441 printf_filtered (" exists\n");
6442 else
6443 printf_filtered (" faked for \"readnow\"\n");
6444 printf_filtered ("\n");
9291a0cd
TT
6445}
6446
9291a0cd 6447static void
927aa2e7
JK
6448dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6449 const char *func_name)
9291a0cd 6450{
ed2dc618
SM
6451 struct dwarf2_per_objfile *dwarf2_per_objfile
6452 = get_dwarf2_per_objfile (objfile);
ae2de4f8 6453
927aa2e7
JK
6454 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6455 if (dwarf2_per_objfile->debug_names_table)
24c79950 6456 {
927aa2e7 6457 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6458
927aa2e7
JK
6459 /* Note: It doesn't matter what we pass for block_index here. */
6460 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6461 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
24c79950 6462
927aa2e7
JK
6463 struct dwarf2_per_cu_data *per_cu;
6464 while ((per_cu = iter.next ()) != NULL)
6465 dw2_instantiate_symtab (per_cu);
6466 }
6467}
24c79950 6468
927aa2e7
JK
6469static void
6470dw2_debug_names_expand_symtabs_matching
6471 (struct objfile *objfile,
6472 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6473 const lookup_name_info &lookup_name,
6474 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6475 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6476 enum search_domain kind)
6477{
ed2dc618
SM
6478 struct dwarf2_per_objfile *dwarf2_per_objfile
6479 = get_dwarf2_per_objfile (objfile);
9291a0cd 6480
927aa2e7
JK
6481 /* debug_names_table is NULL if OBJF_READNOW. */
6482 if (!dwarf2_per_objfile->debug_names_table)
6483 return;
9291a0cd 6484
ed2dc618 6485 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 6486
44ed8f3e 6487 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6488
44ed8f3e
PA
6489 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6490 symbol_matcher,
6491 kind, [&] (offset_type namei)
927aa2e7 6492 {
927aa2e7
JK
6493 /* The name was matched, now expand corresponding CUs that were
6494 marked. */
6495 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6496
927aa2e7
JK
6497 struct dwarf2_per_cu_data *per_cu;
6498 while ((per_cu = iter.next ()) != NULL)
6499 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6500 expansion_notify);
44ed8f3e 6501 });
9291a0cd
TT
6502}
6503
927aa2e7 6504const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6505{
6506 dw2_has_symbols,
6507 dw2_find_last_source_symtab,
6508 dw2_forget_cached_source_info,
f8eba3c6 6509 dw2_map_symtabs_matching_filename,
927aa2e7 6510 dw2_debug_names_lookup_symbol,
9291a0cd 6511 dw2_print_stats,
927aa2e7 6512 dw2_debug_names_dump,
9291a0cd 6513 dw2_relocate,
927aa2e7 6514 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6515 dw2_expand_all_symtabs,
652a8996 6516 dw2_expand_symtabs_with_fullname,
40658b94 6517 dw2_map_matching_symbols,
927aa2e7 6518 dw2_debug_names_expand_symtabs_matching,
43f3e411 6519 dw2_find_pc_sect_compunit_symtab,
71a3c369 6520 NULL,
9291a0cd
TT
6521 dw2_map_symbol_filenames
6522};
6523
3c0aa29a 6524/* See symfile.h. */
9291a0cd 6525
3c0aa29a
PA
6526bool
6527dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6528{
ed2dc618
SM
6529 struct dwarf2_per_objfile *dwarf2_per_objfile
6530 = get_dwarf2_per_objfile (objfile);
6531
9291a0cd
TT
6532 /* If we're about to read full symbols, don't bother with the
6533 indices. In this case we also don't care if some other debug
6534 format is making psymtabs, because they are all about to be
6535 expanded anyway. */
6536 if ((objfile->flags & OBJF_READNOW))
6537 {
6538 int i;
6539
6540 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
6541 create_all_comp_units (dwarf2_per_objfile);
6542 create_all_type_units (dwarf2_per_objfile);
7b9f3c50
DE
6543 dwarf2_per_objfile->quick_file_names_table =
6544 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 6545
1fd400ff 6546 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 6547 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 6548 {
ed2dc618 6549 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
9291a0cd 6550
e254ef6a
DE
6551 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6552 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6553 }
6554
6555 /* Return 1 so that gdb sees the "quick" functions. However,
6556 these functions will be no-ops because we will have expanded
6557 all symtabs. */
3c0aa29a
PA
6558 *index_kind = dw_index_kind::GDB_INDEX;
6559 return true;
9291a0cd
TT
6560 }
6561
ed2dc618 6562 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6563 {
6564 *index_kind = dw_index_kind::DEBUG_NAMES;
6565 return true;
6566 }
927aa2e7 6567
9291a0cd 6568 if (dwarf2_read_index (objfile))
3c0aa29a
PA
6569 {
6570 *index_kind = dw_index_kind::GDB_INDEX;
6571 return true;
6572 }
9291a0cd 6573
3c0aa29a 6574 return false;
9291a0cd
TT
6575}
6576
6577\f
6578
dce234bc
PP
6579/* Build a partial symbol table. */
6580
6581void
f29dff0a 6582dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6583{
ed2dc618
SM
6584 struct dwarf2_per_objfile *dwarf2_per_objfile
6585 = get_dwarf2_per_objfile (objfile);
c9bf0622 6586
af5bf4ad
SM
6587 if (objfile->global_psymbols.capacity () == 0
6588 && objfile->static_psymbols.capacity () == 0)
6589 init_psymbol_list (objfile, 1024);
c906108c 6590
492d29ea 6591 TRY
c9bf0622
TT
6592 {
6593 /* This isn't really ideal: all the data we allocate on the
6594 objfile's obstack is still uselessly kept around. However,
6595 freeing it seems unsafe. */
906768f9 6596 psymtab_discarder psymtabs (objfile);
ed2dc618 6597 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6598 psymtabs.keep ();
c9bf0622 6599 }
492d29ea
PA
6600 CATCH (except, RETURN_MASK_ERROR)
6601 {
6602 exception_print (gdb_stderr, except);
6603 }
6604 END_CATCH
c906108c 6605}
c906108c 6606
1ce1cefd
DE
6607/* Return the total length of the CU described by HEADER. */
6608
6609static unsigned int
6610get_cu_length (const struct comp_unit_head *header)
6611{
6612 return header->initial_length_size + header->length;
6613}
6614
9c541725 6615/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6616
9c541725
PA
6617static inline bool
6618offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6619{
9c541725
PA
6620 sect_offset bottom = cu_header->sect_off;
6621 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6622
9c541725 6623 return sect_off >= bottom && sect_off < top;
45452591
DE
6624}
6625
3b80fe9b
DE
6626/* Find the base address of the compilation unit for range lists and
6627 location lists. It will normally be specified by DW_AT_low_pc.
6628 In DWARF-3 draft 4, the base address could be overridden by
6629 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6630 compilation units with discontinuous ranges. */
6631
6632static void
6633dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6634{
6635 struct attribute *attr;
6636
6637 cu->base_known = 0;
6638 cu->base_address = 0;
6639
6640 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6641 if (attr)
6642 {
31aa7e4e 6643 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6644 cu->base_known = 1;
6645 }
6646 else
6647 {
6648 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6649 if (attr)
6650 {
31aa7e4e 6651 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6652 cu->base_known = 1;
6653 }
6654 }
6655}
6656
93311388 6657/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6658 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6659 NOTE: This leaves members offset, first_die_offset to be filled in
6660 by the caller. */
107d2387 6661
d521ce57 6662static const gdb_byte *
107d2387 6663read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6664 const gdb_byte *info_ptr,
6665 struct dwarf2_section_info *section,
6666 rcuh_kind section_kind)
107d2387
AC
6667{
6668 int signed_addr;
891d2f0b 6669 unsigned int bytes_read;
43988095
JK
6670 const char *filename = get_section_file_name (section);
6671 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6672
6673 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6674 cu_header->initial_length_size = bytes_read;
6675 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6676 info_ptr += bytes_read;
107d2387
AC
6677 cu_header->version = read_2_bytes (abfd, info_ptr);
6678 info_ptr += 2;
43988095
JK
6679 if (cu_header->version < 5)
6680 switch (section_kind)
6681 {
6682 case rcuh_kind::COMPILE:
6683 cu_header->unit_type = DW_UT_compile;
6684 break;
6685 case rcuh_kind::TYPE:
6686 cu_header->unit_type = DW_UT_type;
6687 break;
6688 default:
6689 internal_error (__FILE__, __LINE__,
6690 _("read_comp_unit_head: invalid section_kind"));
6691 }
6692 else
6693 {
6694 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6695 (read_1_byte (abfd, info_ptr));
6696 info_ptr += 1;
6697 switch (cu_header->unit_type)
6698 {
6699 case DW_UT_compile:
6700 if (section_kind != rcuh_kind::COMPILE)
6701 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6702 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6703 filename);
6704 break;
6705 case DW_UT_type:
6706 section_kind = rcuh_kind::TYPE;
6707 break;
6708 default:
6709 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6710 "(is %d, should be %d or %d) [in module %s]"),
6711 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6712 }
6713
6714 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6715 info_ptr += 1;
6716 }
9c541725
PA
6717 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6718 cu_header,
6719 &bytes_read);
613e1657 6720 info_ptr += bytes_read;
43988095
JK
6721 if (cu_header->version < 5)
6722 {
6723 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6724 info_ptr += 1;
6725 }
107d2387
AC
6726 signed_addr = bfd_get_sign_extend_vma (abfd);
6727 if (signed_addr < 0)
8e65ff28 6728 internal_error (__FILE__, __LINE__,
e2e0b3e5 6729 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6730 cu_header->signed_addr_p = signed_addr;
c764a876 6731
43988095
JK
6732 if (section_kind == rcuh_kind::TYPE)
6733 {
6734 LONGEST type_offset;
6735
6736 cu_header->signature = read_8_bytes (abfd, info_ptr);
6737 info_ptr += 8;
6738
6739 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6740 info_ptr += bytes_read;
9c541725
PA
6741 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6742 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6743 error (_("Dwarf Error: Too big type_offset in compilation unit "
6744 "header (is %s) [in module %s]"), plongest (type_offset),
6745 filename);
6746 }
6747
107d2387
AC
6748 return info_ptr;
6749}
6750
36586728
TT
6751/* Helper function that returns the proper abbrev section for
6752 THIS_CU. */
6753
6754static struct dwarf2_section_info *
6755get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6756{
6757 struct dwarf2_section_info *abbrev;
ed2dc618 6758 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6759
6760 if (this_cu->is_dwz)
ed2dc618 6761 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6762 else
6763 abbrev = &dwarf2_per_objfile->abbrev;
6764
6765 return abbrev;
6766}
6767
9ff913ba
DE
6768/* Subroutine of read_and_check_comp_unit_head and
6769 read_and_check_type_unit_head to simplify them.
6770 Perform various error checking on the header. */
6771
6772static void
ed2dc618
SM
6773error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6774 struct comp_unit_head *header,
4bdcc0c1
DE
6775 struct dwarf2_section_info *section,
6776 struct dwarf2_section_info *abbrev_section)
9ff913ba 6777{
a32a8923 6778 const char *filename = get_section_file_name (section);
9ff913ba 6779
43988095 6780 if (header->version < 2 || header->version > 5)
9ff913ba 6781 error (_("Dwarf Error: wrong version in compilation unit header "
43988095 6782 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
9ff913ba
DE
6783 filename);
6784
9c541725 6785 if (to_underlying (header->abbrev_sect_off)
36586728 6786 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9c541725
PA
6787 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
6788 "(offset 0x%x + 6) [in module %s]"),
6789 to_underlying (header->abbrev_sect_off),
6790 to_underlying (header->sect_off),
9ff913ba
DE
6791 filename);
6792
9c541725 6793 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6794 avoid potential 32-bit overflow. */
9c541725 6795 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6796 > section->size)
9c541725
PA
6797 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6798 "(offset 0x%x + 0) [in module %s]"),
6799 header->length, to_underlying (header->sect_off),
9ff913ba
DE
6800 filename);
6801}
6802
6803/* Read in a CU/TU header and perform some basic error checking.
6804 The contents of the header are stored in HEADER.
6805 The result is a pointer to the start of the first DIE. */
adabb602 6806
d521ce57 6807static const gdb_byte *
ed2dc618
SM
6808read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6809 struct comp_unit_head *header,
9ff913ba 6810 struct dwarf2_section_info *section,
4bdcc0c1 6811 struct dwarf2_section_info *abbrev_section,
d521ce57 6812 const gdb_byte *info_ptr,
43988095 6813 rcuh_kind section_kind)
72bf9492 6814{
d521ce57 6815 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6816
9c541725 6817 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6818
43988095 6819 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6820
9c541725 6821 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6822
ed2dc618
SM
6823 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6824 abbrev_section);
9ff913ba
DE
6825
6826 return info_ptr;
348e048f
DE
6827}
6828
f4dc4d17
DE
6829/* Fetch the abbreviation table offset from a comp or type unit header. */
6830
6831static sect_offset
ed2dc618
SM
6832read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6833 struct dwarf2_section_info *section,
9c541725 6834 sect_offset sect_off)
f4dc4d17 6835{
a32a8923 6836 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6837 const gdb_byte *info_ptr;
ac298888 6838 unsigned int initial_length_size, offset_size;
43988095 6839 uint16_t version;
f4dc4d17
DE
6840
6841 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6842 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6843 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6844 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6845 info_ptr += initial_length_size;
6846
6847 version = read_2_bytes (abfd, info_ptr);
6848 info_ptr += 2;
6849 if (version >= 5)
6850 {
6851 /* Skip unit type and address size. */
6852 info_ptr += 2;
6853 }
6854
9c541725 6855 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6856}
6857
aaa75496
JB
6858/* Allocate a new partial symtab for file named NAME and mark this new
6859 partial symtab as being an include of PST. */
6860
6861static void
d521ce57 6862dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
6863 struct objfile *objfile)
6864{
6865 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6866
fbd9ab74
JK
6867 if (!IS_ABSOLUTE_PATH (subpst->filename))
6868 {
6869 /* It shares objfile->objfile_obstack. */
6870 subpst->dirname = pst->dirname;
6871 }
6872
aaa75496
JB
6873 subpst->textlow = 0;
6874 subpst->texthigh = 0;
6875
8d749320
SM
6876 subpst->dependencies
6877 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
6878 subpst->dependencies[0] = pst;
6879 subpst->number_of_dependencies = 1;
6880
6881 subpst->globals_offset = 0;
6882 subpst->n_global_syms = 0;
6883 subpst->statics_offset = 0;
6884 subpst->n_static_syms = 0;
43f3e411 6885 subpst->compunit_symtab = NULL;
aaa75496
JB
6886 subpst->read_symtab = pst->read_symtab;
6887 subpst->readin = 0;
6888
6889 /* No private part is necessary for include psymtabs. This property
6890 can be used to differentiate between such include psymtabs and
10b3939b 6891 the regular ones. */
58a9656e 6892 subpst->read_symtab_private = NULL;
aaa75496
JB
6893}
6894
6895/* Read the Line Number Program data and extract the list of files
6896 included by the source file represented by PST. Build an include
d85a05f0 6897 partial symtab for each of these included files. */
aaa75496
JB
6898
6899static void
6900dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
6901 struct die_info *die,
6902 struct partial_symtab *pst)
aaa75496 6903{
fff8551c 6904 line_header_up lh;
d85a05f0 6905 struct attribute *attr;
aaa75496 6906
d85a05f0
DJ
6907 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6908 if (attr)
9c541725 6909 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6910 if (lh == NULL)
6911 return; /* No linetable, so no includes. */
6912
c6da4cef 6913 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
fff8551c 6914 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
6915}
6916
348e048f 6917static hashval_t
52dc124a 6918hash_signatured_type (const void *item)
348e048f 6919{
9a3c8263
SM
6920 const struct signatured_type *sig_type
6921 = (const struct signatured_type *) item;
9a619af0 6922
348e048f 6923 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6924 return sig_type->signature;
348e048f
DE
6925}
6926
6927static int
52dc124a 6928eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6929{
9a3c8263
SM
6930 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6931 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6932
348e048f
DE
6933 return lhs->signature == rhs->signature;
6934}
6935
1fd400ff
TT
6936/* Allocate a hash table for signatured types. */
6937
6938static htab_t
673bfd45 6939allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6940{
6941 return htab_create_alloc_ex (41,
52dc124a
DE
6942 hash_signatured_type,
6943 eq_signatured_type,
1fd400ff
TT
6944 NULL,
6945 &objfile->objfile_obstack,
6946 hashtab_obstack_allocate,
6947 dummy_obstack_deallocate);
6948}
6949
d467dd73 6950/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6951
6952static int
d467dd73 6953add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6954{
9a3c8263
SM
6955 struct signatured_type *sigt = (struct signatured_type *) *slot;
6956 struct signatured_type ***datap = (struct signatured_type ***) datum;
1fd400ff 6957
b4dd5633 6958 **datap = sigt;
1fd400ff
TT
6959 ++*datap;
6960
6961 return 1;
6962}
6963
78d4d2c5 6964/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6965 and fill them into TYPES_HTAB. It will process only type units,
6966 therefore DW_UT_type. */
c88ee1f0 6967
78d4d2c5 6968static void
ed2dc618
SM
6969create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6970 struct dwo_file *dwo_file,
43988095
JK
6971 dwarf2_section_info *section, htab_t &types_htab,
6972 rcuh_kind section_kind)
348e048f 6973{
3019eac3 6974 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6975 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6976 bfd *abfd;
6977 const gdb_byte *info_ptr, *end_ptr;
348e048f 6978
4bdcc0c1
DE
6979 abbrev_section = (dwo_file != NULL
6980 ? &dwo_file->sections.abbrev
6981 : &dwarf2_per_objfile->abbrev);
6982
b4f54984 6983 if (dwarf_read_debug)
43988095
JK
6984 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6985 get_section_name (section),
a32a8923 6986 get_section_file_name (abbrev_section));
09406207 6987
78d4d2c5
JK
6988 dwarf2_read_section (objfile, section);
6989 info_ptr = section->buffer;
348e048f 6990
78d4d2c5
JK
6991 if (info_ptr == NULL)
6992 return;
348e048f 6993
78d4d2c5
JK
6994 /* We can't set abfd until now because the section may be empty or
6995 not present, in which case the bfd is unknown. */
6996 abfd = get_section_bfd_owner (section);
348e048f 6997
78d4d2c5
JK
6998 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6999 because we don't need to read any dies: the signature is in the
7000 header. */
3019eac3 7001
78d4d2c5
JK
7002 end_ptr = info_ptr + section->size;
7003 while (info_ptr < end_ptr)
7004 {
78d4d2c5
JK
7005 struct signatured_type *sig_type;
7006 struct dwo_unit *dwo_tu;
7007 void **slot;
7008 const gdb_byte *ptr = info_ptr;
7009 struct comp_unit_head header;
7010 unsigned int length;
8b70b953 7011
9c541725 7012 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 7013
a49dd8dd
JK
7014 /* Initialize it due to a false compiler warning. */
7015 header.signature = -1;
9c541725 7016 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 7017
78d4d2c5
JK
7018 /* We need to read the type's signature in order to build the hash
7019 table, but we don't need anything else just yet. */
348e048f 7020
ed2dc618 7021 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 7022 abbrev_section, ptr, section_kind);
348e048f 7023
78d4d2c5 7024 length = get_cu_length (&header);
6caca83c 7025
78d4d2c5
JK
7026 /* Skip dummy type units. */
7027 if (ptr >= info_ptr + length
43988095
JK
7028 || peek_abbrev_code (abfd, ptr) == 0
7029 || header.unit_type != DW_UT_type)
78d4d2c5
JK
7030 {
7031 info_ptr += length;
7032 continue;
7033 }
dee91e82 7034
78d4d2c5
JK
7035 if (types_htab == NULL)
7036 {
7037 if (dwo_file)
7038 types_htab = allocate_dwo_unit_table (objfile);
7039 else
7040 types_htab = allocate_signatured_type_table (objfile);
7041 }
8b70b953 7042
78d4d2c5
JK
7043 if (dwo_file)
7044 {
7045 sig_type = NULL;
7046 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7047 struct dwo_unit);
7048 dwo_tu->dwo_file = dwo_file;
43988095 7049 dwo_tu->signature = header.signature;
9c541725 7050 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 7051 dwo_tu->section = section;
9c541725 7052 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
7053 dwo_tu->length = length;
7054 }
7055 else
7056 {
7057 /* N.B.: type_offset is not usable if this type uses a DWO file.
7058 The real type_offset is in the DWO file. */
7059 dwo_tu = NULL;
7060 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7061 struct signatured_type);
43988095 7062 sig_type->signature = header.signature;
9c541725 7063 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 7064 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
7065 sig_type->per_cu.is_debug_types = 1;
7066 sig_type->per_cu.section = section;
9c541725 7067 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
7068 sig_type->per_cu.length = length;
7069 }
7070
7071 slot = htab_find_slot (types_htab,
7072 dwo_file ? (void*) dwo_tu : (void *) sig_type,
7073 INSERT);
7074 gdb_assert (slot != NULL);
7075 if (*slot != NULL)
7076 {
9c541725 7077 sect_offset dup_sect_off;
0349ea22 7078
3019eac3
DE
7079 if (dwo_file)
7080 {
78d4d2c5
JK
7081 const struct dwo_unit *dup_tu
7082 = (const struct dwo_unit *) *slot;
7083
9c541725 7084 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
7085 }
7086 else
7087 {
78d4d2c5
JK
7088 const struct signatured_type *dup_tu
7089 = (const struct signatured_type *) *slot;
7090
9c541725 7091 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 7092 }
8b70b953 7093
78d4d2c5
JK
7094 complaint (&symfile_complaints,
7095 _("debug type entry at offset 0x%x is duplicate to"
7096 " the entry at offset 0x%x, signature %s"),
9c541725 7097 to_underlying (sect_off), to_underlying (dup_sect_off),
43988095 7098 hex_string (header.signature));
78d4d2c5
JK
7099 }
7100 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 7101
78d4d2c5
JK
7102 if (dwarf_read_debug > 1)
7103 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
9c541725 7104 to_underlying (sect_off),
43988095 7105 hex_string (header.signature));
3019eac3 7106
78d4d2c5
JK
7107 info_ptr += length;
7108 }
7109}
3019eac3 7110
78d4d2c5
JK
7111/* Create the hash table of all entries in the .debug_types
7112 (or .debug_types.dwo) section(s).
7113 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
7114 otherwise it is NULL.
b3c8eb43 7115
78d4d2c5 7116 The result is a pointer to the hash table or NULL if there are no types.
348e048f 7117
78d4d2c5 7118 Note: This function processes DWO files only, not DWP files. */
348e048f 7119
78d4d2c5 7120static void
ed2dc618
SM
7121create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
7122 struct dwo_file *dwo_file,
78d4d2c5
JK
7123 VEC (dwarf2_section_info_def) *types,
7124 htab_t &types_htab)
7125{
7126 int ix;
7127 struct dwarf2_section_info *section;
7128
7129 if (VEC_empty (dwarf2_section_info_def, types))
7130 return;
348e048f 7131
78d4d2c5
JK
7132 for (ix = 0;
7133 VEC_iterate (dwarf2_section_info_def, types, ix, section);
7134 ++ix)
ed2dc618
SM
7135 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
7136 types_htab, rcuh_kind::TYPE);
3019eac3
DE
7137}
7138
7139/* Create the hash table of all entries in the .debug_types section,
7140 and initialize all_type_units.
7141 The result is zero if there is an error (e.g. missing .debug_types section),
7142 otherwise non-zero. */
7143
7144static int
ed2dc618 7145create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 7146{
78d4d2c5 7147 htab_t types_htab = NULL;
b4dd5633 7148 struct signatured_type **iter;
3019eac3 7149
ed2dc618
SM
7150 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
7151 &dwarf2_per_objfile->info, types_htab,
43988095 7152 rcuh_kind::COMPILE);
ed2dc618
SM
7153 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
7154 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
7155 if (types_htab == NULL)
7156 {
7157 dwarf2_per_objfile->signatured_types = NULL;
7158 return 0;
7159 }
7160
348e048f
DE
7161 dwarf2_per_objfile->signatured_types = types_htab;
7162
6aa5f3a6
DE
7163 dwarf2_per_objfile->n_type_units
7164 = dwarf2_per_objfile->n_allocated_type_units
7165 = htab_elements (types_htab);
8d749320
SM
7166 dwarf2_per_objfile->all_type_units =
7167 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
d467dd73
DE
7168 iter = &dwarf2_per_objfile->all_type_units[0];
7169 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
7170 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
7171 == dwarf2_per_objfile->n_type_units);
1fd400ff 7172
348e048f
DE
7173 return 1;
7174}
7175
6aa5f3a6
DE
7176/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
7177 If SLOT is non-NULL, it is the entry to use in the hash table.
7178 Otherwise we find one. */
7179
7180static struct signatured_type *
ed2dc618
SM
7181add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
7182 void **slot)
6aa5f3a6
DE
7183{
7184 struct objfile *objfile = dwarf2_per_objfile->objfile;
7185 int n_type_units = dwarf2_per_objfile->n_type_units;
7186 struct signatured_type *sig_type;
7187
7188 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
7189 ++n_type_units;
7190 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
7191 {
7192 if (dwarf2_per_objfile->n_allocated_type_units == 0)
7193 dwarf2_per_objfile->n_allocated_type_units = 1;
7194 dwarf2_per_objfile->n_allocated_type_units *= 2;
7195 dwarf2_per_objfile->all_type_units
224c3ddb
SM
7196 = XRESIZEVEC (struct signatured_type *,
7197 dwarf2_per_objfile->all_type_units,
7198 dwarf2_per_objfile->n_allocated_type_units);
6aa5f3a6
DE
7199 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
7200 }
7201 dwarf2_per_objfile->n_type_units = n_type_units;
7202
7203 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7204 struct signatured_type);
7205 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
7206 sig_type->signature = sig;
7207 sig_type->per_cu.is_debug_types = 1;
7208 if (dwarf2_per_objfile->using_index)
7209 {
7210 sig_type->per_cu.v.quick =
7211 OBSTACK_ZALLOC (&objfile->objfile_obstack,
7212 struct dwarf2_per_cu_quick_data);
7213 }
7214
7215 if (slot == NULL)
7216 {
7217 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7218 sig_type, INSERT);
7219 }
7220 gdb_assert (*slot == NULL);
7221 *slot = sig_type;
7222 /* The rest of sig_type must be filled in by the caller. */
7223 return sig_type;
7224}
7225
a2ce51a0
DE
7226/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7227 Fill in SIG_ENTRY with DWO_ENTRY. */
7228
7229static void
ed2dc618 7230fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
7231 struct signatured_type *sig_entry,
7232 struct dwo_unit *dwo_entry)
7233{
7ee85ab1 7234 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
7235 gdb_assert (! sig_entry->per_cu.queued);
7236 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
7237 if (dwarf2_per_objfile->using_index)
7238 {
7239 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 7240 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
7241 }
7242 else
7243 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 7244 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 7245 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 7246 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
7247 gdb_assert (sig_entry->dwo_unit == NULL);
7248
7249 sig_entry->per_cu.section = dwo_entry->section;
9c541725 7250 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
7251 sig_entry->per_cu.length = dwo_entry->length;
7252 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 7253 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
7254 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7255 sig_entry->dwo_unit = dwo_entry;
7256}
7257
7258/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
7259 If we haven't read the TU yet, create the signatured_type data structure
7260 for a TU to be read in directly from a DWO file, bypassing the stub.
7261 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7262 using .gdb_index, then when reading a CU we want to stay in the DWO file
7263 containing that CU. Otherwise we could end up reading several other DWO
7264 files (due to comdat folding) to process the transitive closure of all the
7265 mentioned TUs, and that can be slow. The current DWO file will have every
7266 type signature that it needs.
a2ce51a0
DE
7267 We only do this for .gdb_index because in the psymtab case we already have
7268 to read all the DWOs to build the type unit groups. */
7269
7270static struct signatured_type *
7271lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7272{
518817b3
SM
7273 struct dwarf2_per_objfile *dwarf2_per_objfile
7274 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
7275 struct objfile *objfile = dwarf2_per_objfile->objfile;
7276 struct dwo_file *dwo_file;
7277 struct dwo_unit find_dwo_entry, *dwo_entry;
7278 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7279 void **slot;
a2ce51a0
DE
7280
7281 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7282
6aa5f3a6
DE
7283 /* If TU skeletons have been removed then we may not have read in any
7284 TUs yet. */
7285 if (dwarf2_per_objfile->signatured_types == NULL)
7286 {
7287 dwarf2_per_objfile->signatured_types
7288 = allocate_signatured_type_table (objfile);
7289 }
a2ce51a0
DE
7290
7291 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
7292 Use the global signatured_types array to do our own comdat-folding
7293 of types. If this is the first time we're reading this TU, and
7294 the TU has an entry in .gdb_index, replace the recorded data from
7295 .gdb_index with this TU. */
a2ce51a0 7296
a2ce51a0 7297 find_sig_entry.signature = sig;
6aa5f3a6
DE
7298 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7299 &find_sig_entry, INSERT);
9a3c8263 7300 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
7301
7302 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
7303 read. Don't reassign the global entry to point to this DWO if that's
7304 the case. Also note that if the TU is already being read, it may not
7305 have come from a DWO, the program may be a mix of Fission-compiled
7306 code and non-Fission-compiled code. */
7307
7308 /* Have we already tried to read this TU?
7309 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7310 needn't exist in the global table yet). */
7311 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
7312 return sig_entry;
7313
6aa5f3a6
DE
7314 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7315 dwo_unit of the TU itself. */
7316 dwo_file = cu->dwo_unit->dwo_file;
7317
a2ce51a0
DE
7318 /* Ok, this is the first time we're reading this TU. */
7319 if (dwo_file->tus == NULL)
7320 return NULL;
7321 find_dwo_entry.signature = sig;
9a3c8263 7322 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
7323 if (dwo_entry == NULL)
7324 return NULL;
7325
6aa5f3a6
DE
7326 /* If the global table doesn't have an entry for this TU, add one. */
7327 if (sig_entry == NULL)
ed2dc618 7328 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 7329
ed2dc618 7330 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 7331 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
7332 return sig_entry;
7333}
7334
a2ce51a0
DE
7335/* Subroutine of lookup_signatured_type.
7336 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
7337 then try the DWP file. If the TU stub (skeleton) has been removed then
7338 it won't be in .gdb_index. */
a2ce51a0
DE
7339
7340static struct signatured_type *
7341lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7342{
518817b3
SM
7343 struct dwarf2_per_objfile *dwarf2_per_objfile
7344 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 7345 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 7346 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
7347 struct dwo_unit *dwo_entry;
7348 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7349 void **slot;
a2ce51a0
DE
7350
7351 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7352 gdb_assert (dwp_file != NULL);
7353
6aa5f3a6
DE
7354 /* If TU skeletons have been removed then we may not have read in any
7355 TUs yet. */
7356 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 7357 {
6aa5f3a6
DE
7358 dwarf2_per_objfile->signatured_types
7359 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
7360 }
7361
6aa5f3a6
DE
7362 find_sig_entry.signature = sig;
7363 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7364 &find_sig_entry, INSERT);
9a3c8263 7365 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
7366
7367 /* Have we already tried to read this TU?
7368 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7369 needn't exist in the global table yet). */
7370 if (sig_entry != NULL)
7371 return sig_entry;
7372
a2ce51a0
DE
7373 if (dwp_file->tus == NULL)
7374 return NULL;
ed2dc618 7375 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 7376 sig, 1 /* is_debug_types */);
a2ce51a0
DE
7377 if (dwo_entry == NULL)
7378 return NULL;
7379
ed2dc618
SM
7380 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7381 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 7382
a2ce51a0
DE
7383 return sig_entry;
7384}
7385
380bca97 7386/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7387 Returns NULL if signature SIG is not present in the table.
7388 It is up to the caller to complain about this. */
348e048f
DE
7389
7390static struct signatured_type *
a2ce51a0 7391lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7392{
518817b3
SM
7393 struct dwarf2_per_objfile *dwarf2_per_objfile
7394 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 7395
a2ce51a0
DE
7396 if (cu->dwo_unit
7397 && dwarf2_per_objfile->using_index)
7398 {
7399 /* We're in a DWO/DWP file, and we're using .gdb_index.
7400 These cases require special processing. */
ed2dc618 7401 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
7402 return lookup_dwo_signatured_type (cu, sig);
7403 else
7404 return lookup_dwp_signatured_type (cu, sig);
7405 }
7406 else
7407 {
7408 struct signatured_type find_entry, *entry;
348e048f 7409
a2ce51a0
DE
7410 if (dwarf2_per_objfile->signatured_types == NULL)
7411 return NULL;
7412 find_entry.signature = sig;
9a3c8263
SM
7413 entry = ((struct signatured_type *)
7414 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7415 return entry;
7416 }
348e048f 7417}
42e7ad6c
DE
7418\f
7419/* Low level DIE reading support. */
348e048f 7420
d85a05f0
DJ
7421/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7422
7423static void
7424init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7425 struct dwarf2_cu *cu,
3019eac3 7426 struct dwarf2_section_info *section,
685af9cd
TT
7427 struct dwo_file *dwo_file,
7428 struct abbrev_table *abbrev_table)
d85a05f0 7429{
fceca515 7430 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7431 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7432 reader->cu = cu;
3019eac3 7433 reader->dwo_file = dwo_file;
dee91e82
DE
7434 reader->die_section = section;
7435 reader->buffer = section->buffer;
f664829e 7436 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7437 reader->comp_dir = NULL;
685af9cd 7438 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
7439}
7440
b0c7bfa9
DE
7441/* Subroutine of init_cutu_and_read_dies to simplify it.
7442 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7443 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7444 already.
7445
7446 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7447 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7448 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7449 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7450 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7451 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7452 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7453 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
7454 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7455 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7456 kept around for at least as long as *RESULT_READER.
7457
b0c7bfa9
DE
7458 The result is non-zero if a valid (non-dummy) DIE was found. */
7459
7460static int
7461read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7462 struct dwo_unit *dwo_unit,
b0c7bfa9 7463 struct die_info *stub_comp_unit_die,
a2ce51a0 7464 const char *stub_comp_dir,
b0c7bfa9 7465 struct die_reader_specs *result_reader,
d521ce57 7466 const gdb_byte **result_info_ptr,
b0c7bfa9 7467 struct die_info **result_comp_unit_die,
685af9cd
TT
7468 int *result_has_children,
7469 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 7470{
ed2dc618 7471 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
7472 struct objfile *objfile = dwarf2_per_objfile->objfile;
7473 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7474 bfd *abfd;
d521ce57 7475 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7476 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7477 int i,num_extra_attrs;
7478 struct dwarf2_section_info *dwo_abbrev_section;
7479 struct attribute *attr;
7480 struct die_info *comp_unit_die;
7481
b0aeadb3
DE
7482 /* At most one of these may be provided. */
7483 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7484
b0c7bfa9
DE
7485 /* These attributes aren't processed until later:
7486 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7487 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7488 referenced later. However, these attributes are found in the stub
7489 which we won't have later. In order to not impose this complication
7490 on the rest of the code, we read them here and copy them to the
7491 DWO CU/TU die. */
b0c7bfa9
DE
7492
7493 stmt_list = NULL;
7494 low_pc = NULL;
7495 high_pc = NULL;
7496 ranges = NULL;
7497 comp_dir = NULL;
7498
7499 if (stub_comp_unit_die != NULL)
7500 {
7501 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7502 DWO file. */
7503 if (! this_cu->is_debug_types)
7504 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7505 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7506 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7507 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7508 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7509
7510 /* There should be a DW_AT_addr_base attribute here (if needed).
7511 We need the value before we can process DW_FORM_GNU_addr_index. */
7512 cu->addr_base = 0;
7513 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7514 if (attr)
7515 cu->addr_base = DW_UNSND (attr);
7516
7517 /* There should be a DW_AT_ranges_base attribute here (if needed).
7518 We need the value before we can process DW_AT_ranges. */
7519 cu->ranges_base = 0;
7520 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7521 if (attr)
7522 cu->ranges_base = DW_UNSND (attr);
7523 }
a2ce51a0
DE
7524 else if (stub_comp_dir != NULL)
7525 {
7526 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7527 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7528 comp_dir->name = DW_AT_comp_dir;
7529 comp_dir->form = DW_FORM_string;
7530 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7531 DW_STRING (comp_dir) = stub_comp_dir;
7532 }
b0c7bfa9
DE
7533
7534 /* Set up for reading the DWO CU/TU. */
7535 cu->dwo_unit = dwo_unit;
685af9cd 7536 dwarf2_section_info *section = dwo_unit->section;
b0c7bfa9 7537 dwarf2_read_section (objfile, section);
a32a8923 7538 abfd = get_section_bfd_owner (section);
9c541725
PA
7539 begin_info_ptr = info_ptr = (section->buffer
7540 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7541 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
7542
7543 if (this_cu->is_debug_types)
7544 {
b0c7bfa9
DE
7545 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7546
ed2dc618
SM
7547 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7548 &cu->header, section,
b0c7bfa9 7549 dwo_abbrev_section,
43988095 7550 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7551 /* This is not an assert because it can be caused by bad debug info. */
43988095 7552 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7553 {
7554 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7555 " TU at offset 0x%x [in module %s]"),
7556 hex_string (sig_type->signature),
43988095 7557 hex_string (cu->header.signature),
9c541725 7558 to_underlying (dwo_unit->sect_off),
a2ce51a0
DE
7559 bfd_get_filename (abfd));
7560 }
9c541725 7561 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7562 /* For DWOs coming from DWP files, we don't know the CU length
7563 nor the type's offset in the TU until now. */
7564 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7565 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7566
7567 /* Establish the type offset that can be used to lookup the type.
7568 For DWO files, we don't know it until now. */
9c541725
PA
7569 sig_type->type_offset_in_section
7570 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7571 }
7572 else
7573 {
ed2dc618
SM
7574 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7575 &cu->header, section,
b0c7bfa9 7576 dwo_abbrev_section,
43988095 7577 info_ptr, rcuh_kind::COMPILE);
9c541725 7578 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7579 /* For DWOs coming from DWP files, we don't know the CU length
7580 until now. */
7581 dwo_unit->length = get_cu_length (&cu->header);
7582 }
7583
685af9cd
TT
7584 *result_dwo_abbrev_table
7585 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7586 cu->header.abbrev_sect_off);
7587 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7588 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7589
7590 /* Read in the die, but leave space to copy over the attributes
7591 from the stub. This has the benefit of simplifying the rest of
7592 the code - all the work to maintain the illusion of a single
7593 DW_TAG_{compile,type}_unit DIE is done here. */
7594 num_extra_attrs = ((stmt_list != NULL)
7595 + (low_pc != NULL)
7596 + (high_pc != NULL)
7597 + (ranges != NULL)
7598 + (comp_dir != NULL));
7599 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7600 result_has_children, num_extra_attrs);
7601
7602 /* Copy over the attributes from the stub to the DIE we just read in. */
7603 comp_unit_die = *result_comp_unit_die;
7604 i = comp_unit_die->num_attrs;
7605 if (stmt_list != NULL)
7606 comp_unit_die->attrs[i++] = *stmt_list;
7607 if (low_pc != NULL)
7608 comp_unit_die->attrs[i++] = *low_pc;
7609 if (high_pc != NULL)
7610 comp_unit_die->attrs[i++] = *high_pc;
7611 if (ranges != NULL)
7612 comp_unit_die->attrs[i++] = *ranges;
7613 if (comp_dir != NULL)
7614 comp_unit_die->attrs[i++] = *comp_dir;
7615 comp_unit_die->num_attrs += num_extra_attrs;
7616
b4f54984 7617 if (dwarf_die_debug)
bf6af496
DE
7618 {
7619 fprintf_unfiltered (gdb_stdlog,
7620 "Read die from %s@0x%x of %s:\n",
a32a8923 7621 get_section_name (section),
bf6af496
DE
7622 (unsigned) (begin_info_ptr - section->buffer),
7623 bfd_get_filename (abfd));
b4f54984 7624 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7625 }
7626
a2ce51a0
DE
7627 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7628 TUs by skipping the stub and going directly to the entry in the DWO file.
7629 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7630 to get it via circuitous means. Blech. */
7631 if (comp_dir != NULL)
7632 result_reader->comp_dir = DW_STRING (comp_dir);
7633
b0c7bfa9
DE
7634 /* Skip dummy compilation units. */
7635 if (info_ptr >= begin_info_ptr + dwo_unit->length
7636 || peek_abbrev_code (abfd, info_ptr) == 0)
7637 return 0;
7638
7639 *result_info_ptr = info_ptr;
7640 return 1;
7641}
7642
7643/* Subroutine of init_cutu_and_read_dies to simplify it.
7644 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7645 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7646
7647static struct dwo_unit *
7648lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7649 struct die_info *comp_unit_die)
7650{
7651 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9
DE
7652 ULONGEST signature;
7653 struct dwo_unit *dwo_unit;
7654 const char *comp_dir, *dwo_name;
7655
a2ce51a0
DE
7656 gdb_assert (cu != NULL);
7657
b0c7bfa9 7658 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
7659 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7660 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7661
7662 if (this_cu->is_debug_types)
7663 {
7664 struct signatured_type *sig_type;
7665
7666 /* Since this_cu is the first member of struct signatured_type,
7667 we can go from a pointer to one to a pointer to the other. */
7668 sig_type = (struct signatured_type *) this_cu;
7669 signature = sig_type->signature;
7670 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7671 }
7672 else
7673 {
7674 struct attribute *attr;
7675
7676 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7677 if (! attr)
7678 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7679 " [in module %s]"),
e3b94546 7680 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9
DE
7681 signature = DW_UNSND (attr);
7682 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7683 signature);
7684 }
7685
b0c7bfa9
DE
7686 return dwo_unit;
7687}
7688
a2ce51a0 7689/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6 7690 See it for a description of the parameters.
fcd3b13d 7691 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0
DE
7692
7693static void
6aa5f3a6
DE
7694init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7695 int use_existing_cu, int keep,
a2ce51a0
DE
7696 die_reader_func_ftype *die_reader_func,
7697 void *data)
7698{
fcd3b13d 7699 std::unique_ptr<dwarf2_cu> new_cu;
a2ce51a0 7700 struct signatured_type *sig_type;
a2ce51a0
DE
7701 struct die_reader_specs reader;
7702 const gdb_byte *info_ptr;
7703 struct die_info *comp_unit_die;
7704 int has_children;
ed2dc618 7705 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
a2ce51a0
DE
7706
7707 /* Verify we can do the following downcast, and that we have the
7708 data we need. */
7709 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7710 sig_type = (struct signatured_type *) this_cu;
7711 gdb_assert (sig_type->dwo_unit != NULL);
7712
6aa5f3a6
DE
7713 if (use_existing_cu && this_cu->cu != NULL)
7714 {
7715 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6
DE
7716 /* There's no need to do the rereading_dwo_cu handling that
7717 init_cutu_and_read_dies does since we don't read the stub. */
7718 }
7719 else
7720 {
7721 /* If !use_existing_cu, this_cu->cu must be NULL. */
7722 gdb_assert (this_cu->cu == NULL);
fcd3b13d 7723 new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7724 }
7725
7726 /* A future optimization, if needed, would be to use an existing
7727 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7728 could share abbrev tables. */
a2ce51a0 7729
685af9cd
TT
7730 /* The abbreviation table used by READER, this must live at least as long as
7731 READER. */
7732 abbrev_table_up dwo_abbrev_table;
7733
a2ce51a0 7734 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7735 NULL /* stub_comp_unit_die */,
7736 sig_type->dwo_unit->dwo_file->comp_dir,
7737 &reader, &info_ptr,
685af9cd
TT
7738 &comp_unit_die, &has_children,
7739 &dwo_abbrev_table) == 0)
a2ce51a0
DE
7740 {
7741 /* Dummy die. */
a2ce51a0
DE
7742 return;
7743 }
7744
7745 /* All the "real" work is done here. */
7746 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7747
6aa5f3a6 7748 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
7749 but the alternative is making the latter more complex.
7750 This function is only for the special case of using DWO files directly:
7751 no point in overly complicating the general case just to handle this. */
fcd3b13d 7752 if (new_cu != NULL && keep)
a2ce51a0 7753 {
fcd3b13d
SM
7754 /* Link this CU into read_in_chain. */
7755 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7756 dwarf2_per_objfile->read_in_chain = this_cu;
7757 /* The chain owns it now. */
7758 new_cu.release ();
a2ce51a0 7759 }
a2ce51a0
DE
7760}
7761
fd820528 7762/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7763 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7764
f4dc4d17
DE
7765 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7766 Otherwise the table specified in the comp unit header is read in and used.
7767 This is an optimization for when we already have the abbrev table.
7768
dee91e82
DE
7769 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7770 Otherwise, a new CU is allocated with xmalloc.
7771
7772 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7773 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7774
7775 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 7776 linker) then DIE_READER_FUNC will not get called. */
aaa75496 7777
70221824 7778static void
fd820528 7779init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 7780 struct abbrev_table *abbrev_table,
fd820528
DE
7781 int use_existing_cu, int keep,
7782 die_reader_func_ftype *die_reader_func,
7783 void *data)
c906108c 7784{
ed2dc618 7785 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7786 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7787 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7788 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7789 struct dwarf2_cu *cu;
d521ce57 7790 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7791 struct die_reader_specs reader;
d85a05f0 7792 struct die_info *comp_unit_die;
dee91e82 7793 int has_children;
d85a05f0 7794 struct attribute *attr;
dee91e82 7795 struct signatured_type *sig_type = NULL;
4bdcc0c1 7796 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7797 /* Non-zero if CU currently points to a DWO file and we need to
7798 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7799 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7800 int rereading_dwo_cu = 0;
c906108c 7801
b4f54984 7802 if (dwarf_die_debug)
09406207
DE
7803 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
7804 this_cu->is_debug_types ? "type" : "comp",
9c541725 7805 to_underlying (this_cu->sect_off));
09406207 7806
dee91e82
DE
7807 if (use_existing_cu)
7808 gdb_assert (keep);
23745b47 7809
a2ce51a0
DE
7810 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7811 file (instead of going through the stub), short-circuit all of this. */
7812 if (this_cu->reading_dwo_directly)
7813 {
7814 /* Narrow down the scope of possibilities to have to understand. */
7815 gdb_assert (this_cu->is_debug_types);
7816 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
7817 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7818 die_reader_func, data);
a2ce51a0
DE
7819 return;
7820 }
7821
dee91e82
DE
7822 /* This is cheap if the section is already read in. */
7823 dwarf2_read_section (objfile, section);
7824
9c541725 7825 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7826
7827 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7828
fcd3b13d 7829 std::unique_ptr<dwarf2_cu> new_cu;
dee91e82
DE
7830 if (use_existing_cu && this_cu->cu != NULL)
7831 {
7832 cu = this_cu->cu;
42e7ad6c
DE
7833 /* If this CU is from a DWO file we need to start over, we need to
7834 refetch the attributes from the skeleton CU.
7835 This could be optimized by retrieving those attributes from when we
7836 were here the first time: the previous comp_unit_die was stored in
7837 comp_unit_obstack. But there's no data yet that we need this
7838 optimization. */
7839 if (cu->dwo_unit != NULL)
7840 rereading_dwo_cu = 1;
dee91e82
DE
7841 }
7842 else
7843 {
7844 /* If !use_existing_cu, this_cu->cu must be NULL. */
7845 gdb_assert (this_cu->cu == NULL);
fcd3b13d
SM
7846 new_cu.reset (new dwarf2_cu (this_cu));
7847 cu = new_cu.get ();
42e7ad6c 7848 }
dee91e82 7849
b0c7bfa9 7850 /* Get the header. */
9c541725 7851 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7852 {
7853 /* We already have the header, there's no need to read it in again. */
9c541725 7854 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7855 }
7856 else
7857 {
3019eac3 7858 if (this_cu->is_debug_types)
dee91e82 7859 {
ed2dc618
SM
7860 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7861 &cu->header, section,
4bdcc0c1 7862 abbrev_section, info_ptr,
43988095 7863 rcuh_kind::TYPE);
dee91e82 7864
42e7ad6c
DE
7865 /* Since per_cu is the first member of struct signatured_type,
7866 we can go from a pointer to one to a pointer to the other. */
7867 sig_type = (struct signatured_type *) this_cu;
43988095 7868 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7869 gdb_assert (sig_type->type_offset_in_tu
7870 == cu->header.type_cu_offset_in_tu);
7871 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7872
42e7ad6c
DE
7873 /* LENGTH has not been set yet for type units if we're
7874 using .gdb_index. */
1ce1cefd 7875 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7876
7877 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7878 sig_type->type_offset_in_section =
7879 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7880
7881 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7882 }
7883 else
7884 {
ed2dc618
SM
7885 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7886 &cu->header, section,
4bdcc0c1 7887 abbrev_section,
43988095
JK
7888 info_ptr,
7889 rcuh_kind::COMPILE);
dee91e82 7890
9c541725 7891 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7892 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7893 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7894 }
7895 }
10b3939b 7896
6caca83c 7897 /* Skip dummy compilation units. */
dee91e82 7898 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7899 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7900 return;
6caca83c 7901
433df2d4
DE
7902 /* If we don't have them yet, read the abbrevs for this compilation unit.
7903 And if we need to read them now, make sure they're freed when we're
685af9cd
TT
7904 done (own the table through ABBREV_TABLE_HOLDER). */
7905 abbrev_table_up abbrev_table_holder;
f4dc4d17 7906 if (abbrev_table != NULL)
685af9cd
TT
7907 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7908 else
f4dc4d17 7909 {
685af9cd
TT
7910 abbrev_table_holder
7911 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7912 cu->header.abbrev_sect_off);
7913 abbrev_table = abbrev_table_holder.get ();
42e7ad6c 7914 }
af703f96 7915
dee91e82 7916 /* Read the top level CU/TU die. */
685af9cd 7917 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
dee91e82 7918 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 7919
b0c7bfa9 7920 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7921 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7922 table from the DWO file and pass the ownership over to us. It will be
7923 referenced from READER, so we must make sure to free it after we're done
7924 with READER.
7925
b0c7bfa9
DE
7926 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7927 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3 7928 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
685af9cd 7929 abbrev_table_up dwo_abbrev_table;
3019eac3
DE
7930 if (attr)
7931 {
3019eac3 7932 struct dwo_unit *dwo_unit;
b0c7bfa9 7933 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7934
7935 if (has_children)
6a506a2d
DE
7936 {
7937 complaint (&symfile_complaints,
7938 _("compilation unit with DW_AT_GNU_dwo_name"
7939 " has children (offset 0x%x) [in module %s]"),
9c541725 7940 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
6a506a2d 7941 }
b0c7bfa9 7942 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 7943 if (dwo_unit != NULL)
3019eac3 7944 {
6a506a2d 7945 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7946 comp_unit_die, NULL,
6a506a2d 7947 &reader, &info_ptr,
685af9cd
TT
7948 &dwo_comp_unit_die, &has_children,
7949 &dwo_abbrev_table) == 0)
6a506a2d
DE
7950 {
7951 /* Dummy die. */
6a506a2d
DE
7952 return;
7953 }
7954 comp_unit_die = dwo_comp_unit_die;
7955 }
7956 else
7957 {
7958 /* Yikes, we couldn't find the rest of the DIE, we only have
7959 the stub. A complaint has already been logged. There's
7960 not much more we can do except pass on the stub DIE to
7961 die_reader_func. We don't want to throw an error on bad
7962 debug info. */
3019eac3
DE
7963 }
7964 }
7965
b0c7bfa9 7966 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
7967 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7968
b0c7bfa9 7969 /* Done, clean up. */
fcd3b13d 7970 if (new_cu != NULL && keep)
348e048f 7971 {
fcd3b13d
SM
7972 /* Link this CU into read_in_chain. */
7973 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7974 dwarf2_per_objfile->read_in_chain = this_cu;
7975 /* The chain owns it now. */
7976 new_cu.release ();
348e048f 7977 }
dee91e82
DE
7978}
7979
33e80786
DE
7980/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7981 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7982 to have already done the lookup to find the DWO file).
dee91e82
DE
7983
7984 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7985 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7986
7987 We fill in THIS_CU->length.
7988
7989 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7990 linker) then DIE_READER_FUNC will not get called.
7991
7992 THIS_CU->cu is always freed when done.
3019eac3
DE
7993 This is done in order to not leave THIS_CU->cu in a state where we have
7994 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
7995
7996static void
7997init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 7998 struct dwo_file *dwo_file,
dee91e82
DE
7999 die_reader_func_ftype *die_reader_func,
8000 void *data)
8001{
ed2dc618 8002 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 8003 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 8004 struct dwarf2_section_info *section = this_cu->section;
a32a8923 8005 bfd *abfd = get_section_bfd_owner (section);
33e80786 8006 struct dwarf2_section_info *abbrev_section;
d521ce57 8007 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 8008 struct die_reader_specs reader;
dee91e82
DE
8009 struct die_info *comp_unit_die;
8010 int has_children;
8011
b4f54984 8012 if (dwarf_die_debug)
09406207
DE
8013 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
8014 this_cu->is_debug_types ? "type" : "comp",
9c541725 8015 to_underlying (this_cu->sect_off));
09406207 8016
dee91e82
DE
8017 gdb_assert (this_cu->cu == NULL);
8018
33e80786
DE
8019 abbrev_section = (dwo_file != NULL
8020 ? &dwo_file->sections.abbrev
8021 : get_abbrev_section_for_cu (this_cu));
8022
dee91e82
DE
8023 /* This is cheap if the section is already read in. */
8024 dwarf2_read_section (objfile, section);
8025
fcd3b13d 8026 struct dwarf2_cu cu (this_cu);
dee91e82 8027
9c541725 8028 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618
SM
8029 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
8030 &cu.header, section,
4bdcc0c1 8031 abbrev_section, info_ptr,
43988095
JK
8032 (this_cu->is_debug_types
8033 ? rcuh_kind::TYPE
8034 : rcuh_kind::COMPILE));
dee91e82 8035
1ce1cefd 8036 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
8037
8038 /* Skip dummy compilation units. */
8039 if (info_ptr >= begin_info_ptr + this_cu->length
8040 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 8041 return;
72bf9492 8042
685af9cd
TT
8043 abbrev_table_up abbrev_table
8044 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
8045 cu.header.abbrev_sect_off);
dee91e82 8046
685af9cd 8047 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
dee91e82
DE
8048 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
8049
8050 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
dee91e82
DE
8051}
8052
3019eac3
DE
8053/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
8054 does not lookup the specified DWO file.
8055 This cannot be used to read DWO files.
dee91e82
DE
8056
8057 THIS_CU->cu is always freed when done.
3019eac3
DE
8058 This is done in order to not leave THIS_CU->cu in a state where we have
8059 to care whether it refers to the "main" CU or the DWO CU.
8060 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
8061
8062static void
8063init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
8064 die_reader_func_ftype *die_reader_func,
8065 void *data)
8066{
33e80786 8067 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 8068}
0018ea6f
DE
8069\f
8070/* Type Unit Groups.
dee91e82 8071
0018ea6f
DE
8072 Type Unit Groups are a way to collapse the set of all TUs (type units) into
8073 a more manageable set. The grouping is done by DW_AT_stmt_list entry
8074 so that all types coming from the same compilation (.o file) are grouped
8075 together. A future step could be to put the types in the same symtab as
8076 the CU the types ultimately came from. */
ff013f42 8077
f4dc4d17
DE
8078static hashval_t
8079hash_type_unit_group (const void *item)
8080{
9a3c8263
SM
8081 const struct type_unit_group *tu_group
8082 = (const struct type_unit_group *) item;
f4dc4d17 8083
094b34ac 8084 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 8085}
348e048f
DE
8086
8087static int
f4dc4d17 8088eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 8089{
9a3c8263
SM
8090 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
8091 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 8092
094b34ac 8093 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 8094}
348e048f 8095
f4dc4d17
DE
8096/* Allocate a hash table for type unit groups. */
8097
8098static htab_t
ed2dc618 8099allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
8100{
8101 return htab_create_alloc_ex (3,
8102 hash_type_unit_group,
8103 eq_type_unit_group,
8104 NULL,
ed2dc618 8105 &objfile->objfile_obstack,
f4dc4d17
DE
8106 hashtab_obstack_allocate,
8107 dummy_obstack_deallocate);
8108}
dee91e82 8109
f4dc4d17
DE
8110/* Type units that don't have DW_AT_stmt_list are grouped into their own
8111 partial symtabs. We combine several TUs per psymtab to not let the size
8112 of any one psymtab grow too big. */
8113#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
8114#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 8115
094b34ac 8116/* Helper routine for get_type_unit_group.
f4dc4d17
DE
8117 Create the type_unit_group object used to hold one or more TUs. */
8118
8119static struct type_unit_group *
094b34ac 8120create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 8121{
518817b3
SM
8122 struct dwarf2_per_objfile *dwarf2_per_objfile
8123 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 8124 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 8125 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 8126 struct type_unit_group *tu_group;
f4dc4d17
DE
8127
8128 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8129 struct type_unit_group);
094b34ac 8130 per_cu = &tu_group->per_cu;
518817b3 8131 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 8132
094b34ac
DE
8133 if (dwarf2_per_objfile->using_index)
8134 {
8135 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8136 struct dwarf2_per_cu_quick_data);
094b34ac
DE
8137 }
8138 else
8139 {
9c541725 8140 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac
DE
8141 struct partial_symtab *pst;
8142 char *name;
8143
8144 /* Give the symtab a useful name for debug purposes. */
8145 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
8146 name = xstrprintf ("<type_units_%d>",
8147 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
8148 else
8149 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
8150
8151 pst = create_partial_symtab (per_cu, name);
8152 pst->anonymous = 1;
f4dc4d17 8153
094b34ac
DE
8154 xfree (name);
8155 }
f4dc4d17 8156
094b34ac 8157 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 8158 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
8159
8160 return tu_group;
8161}
8162
094b34ac
DE
8163/* Look up the type_unit_group for type unit CU, and create it if necessary.
8164 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
8165
8166static struct type_unit_group *
ff39bb5e 8167get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 8168{
518817b3
SM
8169 struct dwarf2_per_objfile *dwarf2_per_objfile
8170 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8171 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8172 struct type_unit_group *tu_group;
8173 void **slot;
8174 unsigned int line_offset;
8175 struct type_unit_group type_unit_group_for_lookup;
8176
8177 if (dwarf2_per_objfile->type_unit_groups == NULL)
8178 {
8179 dwarf2_per_objfile->type_unit_groups =
ed2dc618 8180 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
8181 }
8182
8183 /* Do we need to create a new group, or can we use an existing one? */
8184
8185 if (stmt_list)
8186 {
8187 line_offset = DW_UNSND (stmt_list);
8188 ++tu_stats->nr_symtab_sharers;
8189 }
8190 else
8191 {
8192 /* Ugh, no stmt_list. Rare, but we have to handle it.
8193 We can do various things here like create one group per TU or
8194 spread them over multiple groups to split up the expansion work.
8195 To avoid worst case scenarios (too many groups or too large groups)
8196 we, umm, group them in bunches. */
8197 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
8198 | (tu_stats->nr_stmt_less_type_units
8199 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
8200 ++tu_stats->nr_stmt_less_type_units;
8201 }
8202
094b34ac 8203 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 8204 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
8205 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
8206 &type_unit_group_for_lookup, INSERT);
8207 if (*slot != NULL)
8208 {
9a3c8263 8209 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
8210 gdb_assert (tu_group != NULL);
8211 }
8212 else
8213 {
9c541725 8214 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 8215 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
8216 *slot = tu_group;
8217 ++tu_stats->nr_symtabs;
8218 }
8219
8220 return tu_group;
8221}
0018ea6f
DE
8222\f
8223/* Partial symbol tables. */
8224
8225/* Create a psymtab named NAME and assign it to PER_CU.
8226
8227 The caller must fill in the following details:
8228 dirname, textlow, texthigh. */
8229
8230static struct partial_symtab *
8231create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8232{
e3b94546 8233 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
0018ea6f
DE
8234 struct partial_symtab *pst;
8235
18a94d75 8236 pst = start_psymtab_common (objfile, name, 0,
af5bf4ad
SM
8237 objfile->global_psymbols,
8238 objfile->static_psymbols);
0018ea6f
DE
8239
8240 pst->psymtabs_addrmap_supported = 1;
8241
8242 /* This is the glue that links PST into GDB's symbol API. */
8243 pst->read_symtab_private = per_cu;
8244 pst->read_symtab = dwarf2_read_symtab;
8245 per_cu->v.psymtab = pst;
8246
8247 return pst;
8248}
8249
b93601f3
TT
8250/* The DATA object passed to process_psymtab_comp_unit_reader has this
8251 type. */
8252
8253struct process_psymtab_comp_unit_data
8254{
8255 /* True if we are reading a DW_TAG_partial_unit. */
8256
8257 int want_partial_unit;
8258
8259 /* The "pretend" language that is used if the CU doesn't declare a
8260 language. */
8261
8262 enum language pretend_language;
8263};
8264
0018ea6f
DE
8265/* die_reader_func for process_psymtab_comp_unit. */
8266
8267static void
8268process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8269 const gdb_byte *info_ptr,
0018ea6f
DE
8270 struct die_info *comp_unit_die,
8271 int has_children,
8272 void *data)
8273{
8274 struct dwarf2_cu *cu = reader->cu;
518817b3 8275 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 8276 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 8277 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
8278 CORE_ADDR baseaddr;
8279 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8280 struct partial_symtab *pst;
3a2b436a 8281 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 8282 const char *filename;
9a3c8263
SM
8283 struct process_psymtab_comp_unit_data *info
8284 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 8285
b93601f3 8286 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
8287 return;
8288
8289 gdb_assert (! per_cu->is_debug_types);
8290
b93601f3 8291 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
8292
8293 cu->list_in_scope = &file_symbols;
8294
8295 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
8296 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8297 if (filename == NULL)
0018ea6f 8298 filename = "";
0018ea6f
DE
8299
8300 pst = create_partial_symtab (per_cu, filename);
8301
8302 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 8303 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
8304
8305 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8306
8307 dwarf2_find_base_address (comp_unit_die, cu);
8308
8309 /* Possibly set the default values of LOWPC and HIGHPC from
8310 `DW_AT_ranges'. */
3a2b436a
JK
8311 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8312 &best_highpc, cu, pst);
8313 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
0018ea6f
DE
8314 /* Store the contiguous range if it is not empty; it can be empty for
8315 CUs with no code. */
8316 addrmap_set_empty (objfile->psymtabs_addrmap,
3e29f34a
MR
8317 gdbarch_adjust_dwarf2_addr (gdbarch,
8318 best_lowpc + baseaddr),
8319 gdbarch_adjust_dwarf2_addr (gdbarch,
8320 best_highpc + baseaddr) - 1,
8321 pst);
0018ea6f
DE
8322
8323 /* Check if comp unit has_children.
8324 If so, read the rest of the partial symbols from this comp unit.
8325 If not, there's no more debug_info for this comp unit. */
8326 if (has_children)
8327 {
8328 struct partial_die_info *first_die;
8329 CORE_ADDR lowpc, highpc;
8330
8331 lowpc = ((CORE_ADDR) -1);
8332 highpc = ((CORE_ADDR) 0);
8333
8334 first_die = load_partial_dies (reader, info_ptr, 1);
8335
8336 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 8337 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
8338
8339 /* If we didn't find a lowpc, set it to highpc to avoid
8340 complaints from `maint check'. */
8341 if (lowpc == ((CORE_ADDR) -1))
8342 lowpc = highpc;
8343
8344 /* If the compilation unit didn't have an explicit address range,
8345 then use the information extracted from its child dies. */
e385593e 8346 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
8347 {
8348 best_lowpc = lowpc;
8349 best_highpc = highpc;
8350 }
8351 }
3e29f34a
MR
8352 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
8353 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 8354
8763cede 8355 end_psymtab_common (objfile, pst);
0018ea6f
DE
8356
8357 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8358 {
8359 int i;
8360 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8361 struct dwarf2_per_cu_data *iter;
8362
8363 /* Fill in 'dependencies' here; we fill in 'users' in a
8364 post-pass. */
8365 pst->number_of_dependencies = len;
8d749320
SM
8366 pst->dependencies =
8367 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
8368 for (i = 0;
8369 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8370 i, iter);
8371 ++i)
8372 pst->dependencies[i] = iter->v.psymtab;
8373
8374 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8375 }
8376
8377 /* Get the list of files included in the current compilation unit,
8378 and build a psymtab for each of them. */
8379 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8380
b4f54984 8381 if (dwarf_read_debug)
0018ea6f
DE
8382 {
8383 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8384
8385 fprintf_unfiltered (gdb_stdlog,
8386 "Psymtab for %s unit @0x%x: %s - %s"
8387 ", %d global, %d static syms\n",
8388 per_cu->is_debug_types ? "type" : "comp",
9c541725 8389 to_underlying (per_cu->sect_off),
0018ea6f
DE
8390 paddress (gdbarch, pst->textlow),
8391 paddress (gdbarch, pst->texthigh),
8392 pst->n_global_syms, pst->n_static_syms);
8393 }
8394}
8395
8396/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8397 Process compilation unit THIS_CU for a psymtab. */
8398
8399static void
8400process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8401 int want_partial_unit,
8402 enum language pretend_language)
0018ea6f
DE
8403{
8404 /* If this compilation unit was already read in, free the
8405 cached copy in order to read it in again. This is
8406 necessary because we skipped some symbols when we first
8407 read in the compilation unit (see load_partial_dies).
8408 This problem could be avoided, but the benefit is unclear. */
8409 if (this_cu->cu != NULL)
8410 free_one_cached_comp_unit (this_cu);
8411
f1902523
JK
8412 if (this_cu->is_debug_types)
8413 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8414 NULL);
8415 else
8416 {
8417 process_psymtab_comp_unit_data info;
8418 info.want_partial_unit = want_partial_unit;
8419 info.pretend_language = pretend_language;
8420 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8421 process_psymtab_comp_unit_reader, &info);
8422 }
0018ea6f
DE
8423
8424 /* Age out any secondary CUs. */
ed2dc618 8425 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 8426}
f4dc4d17
DE
8427
8428/* Reader function for build_type_psymtabs. */
8429
8430static void
8431build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8432 const gdb_byte *info_ptr,
f4dc4d17
DE
8433 struct die_info *type_unit_die,
8434 int has_children,
8435 void *data)
8436{
ed2dc618 8437 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 8438 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8439 struct objfile *objfile = dwarf2_per_objfile->objfile;
8440 struct dwarf2_cu *cu = reader->cu;
8441 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8442 struct signatured_type *sig_type;
f4dc4d17
DE
8443 struct type_unit_group *tu_group;
8444 struct attribute *attr;
8445 struct partial_die_info *first_die;
8446 CORE_ADDR lowpc, highpc;
8447 struct partial_symtab *pst;
8448
8449 gdb_assert (data == NULL);
0186c6a7
DE
8450 gdb_assert (per_cu->is_debug_types);
8451 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8452
8453 if (! has_children)
8454 return;
8455
8456 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8457 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8458
0186c6a7 8459 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
8460
8461 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8462 cu->list_in_scope = &file_symbols;
8463 pst = create_partial_symtab (per_cu, "");
8464 pst->anonymous = 1;
8465
8466 first_die = load_partial_dies (reader, info_ptr, 1);
8467
8468 lowpc = (CORE_ADDR) -1;
8469 highpc = (CORE_ADDR) 0;
8470 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8471
8763cede 8472 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8473}
8474
73051182
DE
8475/* Struct used to sort TUs by their abbreviation table offset. */
8476
8477struct tu_abbrev_offset
8478{
8479 struct signatured_type *sig_type;
8480 sect_offset abbrev_offset;
8481};
8482
8483/* Helper routine for build_type_psymtabs_1, passed to qsort. */
8484
8485static int
8486sort_tu_by_abbrev_offset (const void *ap, const void *bp)
8487{
9a3c8263
SM
8488 const struct tu_abbrev_offset * const *a
8489 = (const struct tu_abbrev_offset * const*) ap;
8490 const struct tu_abbrev_offset * const *b
8491 = (const struct tu_abbrev_offset * const*) bp;
9c541725
PA
8492 sect_offset aoff = (*a)->abbrev_offset;
8493 sect_offset boff = (*b)->abbrev_offset;
73051182
DE
8494
8495 return (aoff > boff) - (aoff < boff);
8496}
8497
8498/* Efficiently read all the type units.
8499 This does the bulk of the work for build_type_psymtabs.
8500
8501 The efficiency is because we sort TUs by the abbrev table they use and
8502 only read each abbrev table once. In one program there are 200K TUs
8503 sharing 8K abbrev tables.
8504
8505 The main purpose of this function is to support building the
8506 dwarf2_per_objfile->type_unit_groups table.
8507 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8508 can collapse the search space by grouping them by stmt_list.
8509 The savings can be significant, in the same program from above the 200K TUs
8510 share 8K stmt_list tables.
8511
8512 FUNC is expected to call get_type_unit_group, which will create the
8513 struct type_unit_group if necessary and add it to
8514 dwarf2_per_objfile->type_unit_groups. */
8515
8516static void
ed2dc618 8517build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 8518{
73051182
DE
8519 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8520 struct cleanup *cleanups;
685af9cd 8521 abbrev_table_up abbrev_table;
73051182
DE
8522 sect_offset abbrev_offset;
8523 struct tu_abbrev_offset *sorted_by_abbrev;
73051182
DE
8524 int i;
8525
8526 /* It's up to the caller to not call us multiple times. */
8527 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8528
8529 if (dwarf2_per_objfile->n_type_units == 0)
8530 return;
8531
8532 /* TUs typically share abbrev tables, and there can be way more TUs than
8533 abbrev tables. Sort by abbrev table to reduce the number of times we
8534 read each abbrev table in.
8535 Alternatives are to punt or to maintain a cache of abbrev tables.
8536 This is simpler and efficient enough for now.
8537
8538 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8539 symtab to use). Typically TUs with the same abbrev offset have the same
8540 stmt_list value too so in practice this should work well.
8541
8542 The basic algorithm here is:
8543
8544 sort TUs by abbrev table
8545 for each TU with same abbrev table:
8546 read abbrev table if first user
8547 read TU top level DIE
8548 [IWBN if DWO skeletons had DW_AT_stmt_list]
8549 call FUNC */
8550
b4f54984 8551 if (dwarf_read_debug)
73051182
DE
8552 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8553
8554 /* Sort in a separate table to maintain the order of all_type_units
8555 for .gdb_index: TU indices directly index all_type_units. */
8556 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
8557 dwarf2_per_objfile->n_type_units);
8558 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8559 {
8560 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
8561
8562 sorted_by_abbrev[i].sig_type = sig_type;
8563 sorted_by_abbrev[i].abbrev_offset =
ed2dc618
SM
8564 read_abbrev_offset (dwarf2_per_objfile,
8565 sig_type->per_cu.section,
9c541725 8566 sig_type->per_cu.sect_off);
73051182
DE
8567 }
8568 cleanups = make_cleanup (xfree, sorted_by_abbrev);
8569 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
8570 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
8571
9c541725 8572 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182
DE
8573
8574 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8575 {
8576 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
8577
8578 /* Switch to the next abbrev table if necessary. */
8579 if (abbrev_table == NULL
9c541725 8580 || tu->abbrev_offset != abbrev_offset)
73051182 8581 {
73051182
DE
8582 abbrev_offset = tu->abbrev_offset;
8583 abbrev_table =
ed2dc618
SM
8584 abbrev_table_read_table (dwarf2_per_objfile,
8585 &dwarf2_per_objfile->abbrev,
73051182
DE
8586 abbrev_offset);
8587 ++tu_stats->nr_uniq_abbrev_tables;
8588 }
8589
685af9cd
TT
8590 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table.get (),
8591 0, 0, build_type_psymtabs_reader, NULL);
73051182
DE
8592 }
8593
73051182 8594 do_cleanups (cleanups);
6aa5f3a6 8595}
73051182 8596
6aa5f3a6
DE
8597/* Print collected type unit statistics. */
8598
8599static void
ed2dc618 8600print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8601{
8602 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8603
8604 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8605 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
8606 dwarf2_per_objfile->n_type_units);
8607 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8608 tu_stats->nr_uniq_abbrev_tables);
8609 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8610 tu_stats->nr_symtabs);
8611 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8612 tu_stats->nr_symtab_sharers);
8613 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8614 tu_stats->nr_stmt_less_type_units);
8615 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8616 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8617}
8618
f4dc4d17
DE
8619/* Traversal function for build_type_psymtabs. */
8620
8621static int
8622build_type_psymtab_dependencies (void **slot, void *info)
8623{
ed2dc618
SM
8624 struct dwarf2_per_objfile *dwarf2_per_objfile
8625 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8626 struct objfile *objfile = dwarf2_per_objfile->objfile;
8627 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8628 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 8629 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
8630 int len = VEC_length (sig_type_ptr, tu_group->tus);
8631 struct signatured_type *iter;
f4dc4d17
DE
8632 int i;
8633
8634 gdb_assert (len > 0);
0186c6a7 8635 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8636
8637 pst->number_of_dependencies = len;
8d749320
SM
8638 pst->dependencies =
8639 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 8640 for (i = 0;
0186c6a7 8641 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
8642 ++i)
8643 {
0186c6a7
DE
8644 gdb_assert (iter->per_cu.is_debug_types);
8645 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8646 iter->type_unit_group = tu_group;
f4dc4d17
DE
8647 }
8648
0186c6a7 8649 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
8650
8651 return 1;
8652}
8653
8654/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8655 Build partial symbol tables for the .debug_types comp-units. */
8656
8657static void
ed2dc618 8658build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8659{
ed2dc618 8660 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8661 return;
8662
ed2dc618 8663 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8664}
f4dc4d17 8665
6aa5f3a6
DE
8666/* Traversal function for process_skeletonless_type_unit.
8667 Read a TU in a DWO file and build partial symbols for it. */
8668
8669static int
8670process_skeletonless_type_unit (void **slot, void *info)
8671{
8672 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8673 struct dwarf2_per_objfile *dwarf2_per_objfile
8674 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8675 struct signatured_type find_entry, *entry;
8676
8677 /* If this TU doesn't exist in the global table, add it and read it in. */
8678
8679 if (dwarf2_per_objfile->signatured_types == NULL)
8680 {
8681 dwarf2_per_objfile->signatured_types
ed2dc618 8682 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8683 }
8684
8685 find_entry.signature = dwo_unit->signature;
8686 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8687 INSERT);
8688 /* If we've already seen this type there's nothing to do. What's happening
8689 is we're doing our own version of comdat-folding here. */
8690 if (*slot != NULL)
8691 return 1;
8692
8693 /* This does the job that create_all_type_units would have done for
8694 this TU. */
ed2dc618
SM
8695 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8696 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8697 *slot = entry;
8698
8699 /* This does the job that build_type_psymtabs_1 would have done. */
8700 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8701 build_type_psymtabs_reader, NULL);
8702
8703 return 1;
8704}
8705
8706/* Traversal function for process_skeletonless_type_units. */
8707
8708static int
8709process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8710{
8711 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8712
8713 if (dwo_file->tus != NULL)
8714 {
8715 htab_traverse_noresize (dwo_file->tus,
8716 process_skeletonless_type_unit, info);
8717 }
8718
8719 return 1;
8720}
8721
8722/* Scan all TUs of DWO files, verifying we've processed them.
8723 This is needed in case a TU was emitted without its skeleton.
8724 Note: This can't be done until we know what all the DWO files are. */
8725
8726static void
ed2dc618 8727process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8728{
8729 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8730 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8731 && dwarf2_per_objfile->dwo_files != NULL)
8732 {
8733 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8734 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8735 dwarf2_per_objfile);
6aa5f3a6 8736 }
348e048f
DE
8737}
8738
ed2dc618 8739/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8740
8741static void
ed2dc618 8742set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad
TT
8743{
8744 int i;
8745
8746 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8747 {
ed2dc618 8748 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
95554aad
TT
8749 struct partial_symtab *pst = per_cu->v.psymtab;
8750 int j;
8751
36586728
TT
8752 if (pst == NULL)
8753 continue;
8754
95554aad
TT
8755 for (j = 0; j < pst->number_of_dependencies; ++j)
8756 {
8757 /* Set the 'user' field only if it is not already set. */
8758 if (pst->dependencies[j]->user == NULL)
8759 pst->dependencies[j]->user = pst;
8760 }
8761 }
8762}
8763
93311388
DE
8764/* Build the partial symbol table by doing a quick pass through the
8765 .debug_info and .debug_abbrev sections. */
72bf9492 8766
93311388 8767static void
ed2dc618 8768dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8769{
791afaa2 8770 struct cleanup *back_to;
21b2bd31 8771 int i;
ed2dc618 8772 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8773
b4f54984 8774 if (dwarf_read_debug)
45cfd468
DE
8775 {
8776 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8777 objfile_name (objfile));
45cfd468
DE
8778 }
8779
98bfdba5
PA
8780 dwarf2_per_objfile->reading_partial_symbols = 1;
8781
be391dca 8782 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8783
93311388
DE
8784 /* Any cached compilation units will be linked by the per-objfile
8785 read_in_chain. Make sure to free them when we're done. */
ed2dc618 8786 back_to = make_cleanup (free_cached_comp_units, dwarf2_per_objfile);
72bf9492 8787
ed2dc618 8788 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8789
ed2dc618 8790 create_all_comp_units (dwarf2_per_objfile);
c906108c 8791
60606b2c
TT
8792 /* Create a temporary address map on a temporary obstack. We later
8793 copy this to the final obstack. */
8268c778 8794 auto_obstack temp_obstack;
791afaa2
TT
8795
8796 scoped_restore save_psymtabs_addrmap
8797 = make_scoped_restore (&objfile->psymtabs_addrmap,
8798 addrmap_create_mutable (&temp_obstack));
72bf9492 8799
21b2bd31 8800 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 8801 {
ed2dc618 8802 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
aaa75496 8803
b93601f3 8804 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 8805 }
ff013f42 8806
6aa5f3a6 8807 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8808 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8809
8810 /* Now that all TUs have been processed we can fill in the dependencies. */
8811 if (dwarf2_per_objfile->type_unit_groups != NULL)
8812 {
8813 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8814 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8815 }
8816
b4f54984 8817 if (dwarf_read_debug)
ed2dc618 8818 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8819
ed2dc618 8820 set_partial_user (dwarf2_per_objfile);
95554aad 8821
ff013f42
JK
8822 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8823 &objfile->objfile_obstack);
791afaa2
TT
8824 /* At this point we want to keep the address map. */
8825 save_psymtabs_addrmap.release ();
ff013f42 8826
ae038cb0 8827 do_cleanups (back_to);
45cfd468 8828
b4f54984 8829 if (dwarf_read_debug)
45cfd468 8830 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8831 objfile_name (objfile));
ae038cb0
DJ
8832}
8833
3019eac3 8834/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
8835
8836static void
dee91e82 8837load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8838 const gdb_byte *info_ptr,
dee91e82
DE
8839 struct die_info *comp_unit_die,
8840 int has_children,
8841 void *data)
ae038cb0 8842{
dee91e82 8843 struct dwarf2_cu *cu = reader->cu;
ae038cb0 8844
95554aad 8845 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 8846
ae038cb0
DJ
8847 /* Check if comp unit has_children.
8848 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 8849 If not, there's no more debug_info for this comp unit. */
d85a05f0 8850 if (has_children)
dee91e82
DE
8851 load_partial_dies (reader, info_ptr, 0);
8852}
98bfdba5 8853
dee91e82
DE
8854/* Load the partial DIEs for a secondary CU into memory.
8855 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8856
dee91e82
DE
8857static void
8858load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8859{
f4dc4d17
DE
8860 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8861 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
8862}
8863
ae038cb0 8864static void
ed2dc618 8865read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8866 struct dwarf2_section_info *section,
f1902523 8867 struct dwarf2_section_info *abbrev_section,
36586728
TT
8868 unsigned int is_dwz,
8869 int *n_allocated,
8870 int *n_comp_units,
8871 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 8872{
d521ce57 8873 const gdb_byte *info_ptr;
ed2dc618 8874 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8875
b4f54984 8876 if (dwarf_read_debug)
bf6af496 8877 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8878 get_section_name (section),
8879 get_section_file_name (section));
bf6af496 8880
36586728 8881 dwarf2_read_section (objfile, section);
ae038cb0 8882
36586728 8883 info_ptr = section->buffer;
6e70227d 8884
36586728 8885 while (info_ptr < section->buffer + section->size)
ae038cb0 8886 {
ae038cb0 8887 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8888
9c541725 8889 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8890
f1902523 8891 comp_unit_head cu_header;
ed2dc618
SM
8892 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8893 abbrev_section, info_ptr,
8894 rcuh_kind::COMPILE);
ae038cb0
DJ
8895
8896 /* Save the compilation unit for later lookup. */
f1902523
JK
8897 if (cu_header.unit_type != DW_UT_type)
8898 {
8899 this_cu = XOBNEW (&objfile->objfile_obstack,
8900 struct dwarf2_per_cu_data);
8901 memset (this_cu, 0, sizeof (*this_cu));
8902 }
8903 else
8904 {
8905 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8906 struct signatured_type);
8907 memset (sig_type, 0, sizeof (*sig_type));
8908 sig_type->signature = cu_header.signature;
8909 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8910 this_cu = &sig_type->per_cu;
8911 }
8912 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8913 this_cu->sect_off = sect_off;
f1902523 8914 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8915 this_cu->is_dwz = is_dwz;
e3b94546 8916 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8917 this_cu->section = section;
ae038cb0 8918
36586728 8919 if (*n_comp_units == *n_allocated)
ae038cb0 8920 {
36586728 8921 *n_allocated *= 2;
224c3ddb
SM
8922 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
8923 *all_comp_units, *n_allocated);
ae038cb0 8924 }
36586728
TT
8925 (*all_comp_units)[*n_comp_units] = this_cu;
8926 ++*n_comp_units;
ae038cb0
DJ
8927
8928 info_ptr = info_ptr + this_cu->length;
8929 }
36586728
TT
8930}
8931
8932/* Create a list of all compilation units in OBJFILE.
8933 This is only done for -readnow and building partial symtabs. */
8934
8935static void
ed2dc618 8936create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728
TT
8937{
8938 int n_allocated;
8939 int n_comp_units;
8940 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 8941 struct dwz_file *dwz;
ed2dc618 8942 struct objfile *objfile = dwarf2_per_objfile->objfile;
36586728
TT
8943
8944 n_comp_units = 0;
8945 n_allocated = 10;
8d749320 8946 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
36586728 8947
ed2dc618 8948 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
f1902523 8949 &dwarf2_per_objfile->abbrev, 0,
36586728
TT
8950 &n_allocated, &n_comp_units, &all_comp_units);
8951
ed2dc618 8952 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8953 if (dwz != NULL)
ed2dc618
SM
8954 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8955 1, &n_allocated, &n_comp_units,
4db1a1dc 8956 &all_comp_units);
ae038cb0 8957
8d749320
SM
8958 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
8959 struct dwarf2_per_cu_data *,
8960 n_comp_units);
ae038cb0
DJ
8961 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
8962 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
8963 xfree (all_comp_units);
8964 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
8965}
8966
5734ee8b 8967/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8968 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8969 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8970 DW_AT_ranges). See the comments of add_partial_subprogram on how
8971 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8972
72bf9492
DJ
8973static void
8974scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8975 CORE_ADDR *highpc, int set_addrmap,
8976 struct dwarf2_cu *cu)
c906108c 8977{
72bf9492 8978 struct partial_die_info *pdi;
c906108c 8979
91c24f0a
DC
8980 /* Now, march along the PDI's, descending into ones which have
8981 interesting children but skipping the children of the other ones,
8982 until we reach the end of the compilation unit. */
c906108c 8983
72bf9492 8984 pdi = first_die;
91c24f0a 8985
72bf9492
DJ
8986 while (pdi != NULL)
8987 {
8988 fixup_partial_die (pdi, cu);
c906108c 8989
f55ee35c 8990 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8991 children, so we need to look at them. Ditto for anonymous
8992 enums. */
933c6fe4 8993
72bf9492 8994 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8995 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8996 || pdi->tag == DW_TAG_imported_unit
8997 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8998 {
72bf9492 8999 switch (pdi->tag)
c906108c
SS
9000 {
9001 case DW_TAG_subprogram:
b1dc1806 9002 case DW_TAG_inlined_subroutine:
cdc07690 9003 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 9004 break;
72929c62 9005 case DW_TAG_constant:
c906108c
SS
9006 case DW_TAG_variable:
9007 case DW_TAG_typedef:
91c24f0a 9008 case DW_TAG_union_type:
72bf9492 9009 if (!pdi->is_declaration)
63d06c5c 9010 {
72bf9492 9011 add_partial_symbol (pdi, cu);
63d06c5c
DC
9012 }
9013 break;
c906108c 9014 case DW_TAG_class_type:
680b30c7 9015 case DW_TAG_interface_type:
c906108c 9016 case DW_TAG_structure_type:
72bf9492 9017 if (!pdi->is_declaration)
c906108c 9018 {
72bf9492 9019 add_partial_symbol (pdi, cu);
c906108c 9020 }
e98c9e7c
TT
9021 if (cu->language == language_rust && pdi->has_children)
9022 scan_partial_symbols (pdi->die_child, lowpc, highpc,
9023 set_addrmap, cu);
c906108c 9024 break;
91c24f0a 9025 case DW_TAG_enumeration_type:
72bf9492
DJ
9026 if (!pdi->is_declaration)
9027 add_partial_enumeration (pdi, cu);
c906108c
SS
9028 break;
9029 case DW_TAG_base_type:
a02abb62 9030 case DW_TAG_subrange_type:
c906108c 9031 /* File scope base type definitions are added to the partial
c5aa993b 9032 symbol table. */
72bf9492 9033 add_partial_symbol (pdi, cu);
c906108c 9034 break;
d9fa45fe 9035 case DW_TAG_namespace:
cdc07690 9036 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 9037 break;
5d7cb8df 9038 case DW_TAG_module:
cdc07690 9039 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 9040 break;
95554aad
TT
9041 case DW_TAG_imported_unit:
9042 {
9043 struct dwarf2_per_cu_data *per_cu;
9044
f4dc4d17
DE
9045 /* For now we don't handle imported units in type units. */
9046 if (cu->per_cu->is_debug_types)
9047 {
9048 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9049 " supported in type units [in module %s]"),
518817b3 9050 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
9051 }
9052
e3b94546
SM
9053 per_cu = dwarf2_find_containing_comp_unit
9054 (pdi->d.sect_off, pdi->is_dwz,
518817b3 9055 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
9056
9057 /* Go read the partial unit, if needed. */
9058 if (per_cu->v.psymtab == NULL)
b93601f3 9059 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 9060
f4dc4d17 9061 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 9062 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
9063 }
9064 break;
74921315
KS
9065 case DW_TAG_imported_declaration:
9066 add_partial_symbol (pdi, cu);
9067 break;
c906108c
SS
9068 default:
9069 break;
9070 }
9071 }
9072
72bf9492
DJ
9073 /* If the die has a sibling, skip to the sibling. */
9074
9075 pdi = pdi->die_sibling;
9076 }
9077}
9078
9079/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 9080
72bf9492 9081 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 9082 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
9083 Enumerators are an exception; they use the scope of their parent
9084 enumeration type, i.e. the name of the enumeration type is not
9085 prepended to the enumerator.
91c24f0a 9086
72bf9492
DJ
9087 There are two complexities. One is DW_AT_specification; in this
9088 case "parent" means the parent of the target of the specification,
9089 instead of the direct parent of the DIE. The other is compilers
9090 which do not emit DW_TAG_namespace; in this case we try to guess
9091 the fully qualified name of structure types from their members'
9092 linkage names. This must be done using the DIE's children rather
9093 than the children of any DW_AT_specification target. We only need
9094 to do this for structures at the top level, i.e. if the target of
9095 any DW_AT_specification (if any; otherwise the DIE itself) does not
9096 have a parent. */
9097
9098/* Compute the scope prefix associated with PDI's parent, in
9099 compilation unit CU. The result will be allocated on CU's
9100 comp_unit_obstack, or a copy of the already allocated PDI->NAME
9101 field. NULL is returned if no prefix is necessary. */
15d034d0 9102static const char *
72bf9492
DJ
9103partial_die_parent_scope (struct partial_die_info *pdi,
9104 struct dwarf2_cu *cu)
9105{
15d034d0 9106 const char *grandparent_scope;
72bf9492 9107 struct partial_die_info *parent, *real_pdi;
91c24f0a 9108
72bf9492
DJ
9109 /* We need to look at our parent DIE; if we have a DW_AT_specification,
9110 then this means the parent of the specification DIE. */
9111
9112 real_pdi = pdi;
72bf9492 9113 while (real_pdi->has_specification)
36586728
TT
9114 real_pdi = find_partial_die (real_pdi->spec_offset,
9115 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
9116
9117 parent = real_pdi->die_parent;
9118 if (parent == NULL)
9119 return NULL;
9120
9121 if (parent->scope_set)
9122 return parent->scope;
9123
9124 fixup_partial_die (parent, cu);
9125
10b3939b 9126 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 9127
acebe513
UW
9128 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
9129 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
9130 Work around this problem here. */
9131 if (cu->language == language_cplus
6e70227d 9132 && parent->tag == DW_TAG_namespace
acebe513
UW
9133 && strcmp (parent->name, "::") == 0
9134 && grandparent_scope == NULL)
9135 {
9136 parent->scope = NULL;
9137 parent->scope_set = 1;
9138 return NULL;
9139 }
9140
9c6c53f7
SA
9141 if (pdi->tag == DW_TAG_enumerator)
9142 /* Enumerators should not get the name of the enumeration as a prefix. */
9143 parent->scope = grandparent_scope;
9144 else if (parent->tag == DW_TAG_namespace
f55ee35c 9145 || parent->tag == DW_TAG_module
72bf9492
DJ
9146 || parent->tag == DW_TAG_structure_type
9147 || parent->tag == DW_TAG_class_type
680b30c7 9148 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
9149 || parent->tag == DW_TAG_union_type
9150 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
9151 {
9152 if (grandparent_scope == NULL)
9153 parent->scope = parent->name;
9154 else
3e43a32a
MS
9155 parent->scope = typename_concat (&cu->comp_unit_obstack,
9156 grandparent_scope,
f55ee35c 9157 parent->name, 0, cu);
72bf9492 9158 }
72bf9492
DJ
9159 else
9160 {
9161 /* FIXME drow/2004-04-01: What should we be doing with
9162 function-local names? For partial symbols, we should probably be
9163 ignoring them. */
9164 complaint (&symfile_complaints,
e2e0b3e5 9165 _("unhandled containing DIE tag %d for DIE at %d"),
9c541725 9166 parent->tag, to_underlying (pdi->sect_off));
72bf9492 9167 parent->scope = grandparent_scope;
c906108c
SS
9168 }
9169
72bf9492
DJ
9170 parent->scope_set = 1;
9171 return parent->scope;
9172}
9173
9174/* Return the fully scoped name associated with PDI, from compilation unit
9175 CU. The result will be allocated with malloc. */
4568ecf9 9176
72bf9492
DJ
9177static char *
9178partial_die_full_name (struct partial_die_info *pdi,
9179 struct dwarf2_cu *cu)
9180{
15d034d0 9181 const char *parent_scope;
72bf9492 9182
98bfdba5
PA
9183 /* If this is a template instantiation, we can not work out the
9184 template arguments from partial DIEs. So, unfortunately, we have
9185 to go through the full DIEs. At least any work we do building
9186 types here will be reused if full symbols are loaded later. */
9187 if (pdi->has_template_arguments)
9188 {
9189 fixup_partial_die (pdi, cu);
9190
9191 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
9192 {
9193 struct die_info *die;
9194 struct attribute attr;
9195 struct dwarf2_cu *ref_cu = cu;
9196
b64f50a1 9197 /* DW_FORM_ref_addr is using section offset. */
b4069958 9198 attr.name = (enum dwarf_attribute) 0;
98bfdba5 9199 attr.form = DW_FORM_ref_addr;
9c541725 9200 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
9201 die = follow_die_ref (NULL, &attr, &ref_cu);
9202
9203 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
9204 }
9205 }
9206
72bf9492
DJ
9207 parent_scope = partial_die_parent_scope (pdi, cu);
9208 if (parent_scope == NULL)
9209 return NULL;
9210 else
f55ee35c 9211 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
9212}
9213
9214static void
72bf9492 9215add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 9216{
518817b3
SM
9217 struct dwarf2_per_objfile *dwarf2_per_objfile
9218 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 9219 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9220 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 9221 CORE_ADDR addr = 0;
15d034d0 9222 const char *actual_name = NULL;
e142c38c 9223 CORE_ADDR baseaddr;
15d034d0 9224 char *built_actual_name;
e142c38c
DJ
9225
9226 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9227
15d034d0
TT
9228 built_actual_name = partial_die_full_name (pdi, cu);
9229 if (built_actual_name != NULL)
9230 actual_name = built_actual_name;
63d06c5c 9231
72bf9492
DJ
9232 if (actual_name == NULL)
9233 actual_name = pdi->name;
9234
c906108c
SS
9235 switch (pdi->tag)
9236 {
b1dc1806 9237 case DW_TAG_inlined_subroutine:
c906108c 9238 case DW_TAG_subprogram:
3e29f34a 9239 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 9240 if (pdi->is_external || cu->language == language_ada)
c906108c 9241 {
2cfa0c8d
JB
9242 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
9243 of the global scope. But in Ada, we want to be able to access
9244 nested procedures globally. So all Ada subprograms are stored
9245 in the global scope. */
f47fb265 9246 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9247 built_actual_name != NULL,
f47fb265
MS
9248 VAR_DOMAIN, LOC_BLOCK,
9249 &objfile->global_psymbols,
1762568f 9250 addr, cu->language, objfile);
c906108c
SS
9251 }
9252 else
9253 {
f47fb265 9254 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9255 built_actual_name != NULL,
f47fb265
MS
9256 VAR_DOMAIN, LOC_BLOCK,
9257 &objfile->static_psymbols,
1762568f 9258 addr, cu->language, objfile);
c906108c 9259 }
0c1b455e
TT
9260
9261 if (pdi->main_subprogram && actual_name != NULL)
9262 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 9263 break;
72929c62
JB
9264 case DW_TAG_constant:
9265 {
af5bf4ad 9266 std::vector<partial_symbol *> *list;
72929c62
JB
9267
9268 if (pdi->is_external)
9269 list = &objfile->global_psymbols;
9270 else
9271 list = &objfile->static_psymbols;
f47fb265 9272 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9273 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 9274 list, 0, cu->language, objfile);
72929c62
JB
9275 }
9276 break;
c906108c 9277 case DW_TAG_variable:
95554aad
TT
9278 if (pdi->d.locdesc)
9279 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 9280
95554aad 9281 if (pdi->d.locdesc
caac4577
JG
9282 && addr == 0
9283 && !dwarf2_per_objfile->has_section_at_zero)
9284 {
9285 /* A global or static variable may also have been stripped
9286 out by the linker if unused, in which case its address
9287 will be nullified; do not add such variables into partial
9288 symbol table then. */
9289 }
9290 else if (pdi->is_external)
c906108c
SS
9291 {
9292 /* Global Variable.
9293 Don't enter into the minimal symbol tables as there is
9294 a minimal symbol table entry from the ELF symbols already.
9295 Enter into partial symbol table if it has a location
9296 descriptor or a type.
9297 If the location descriptor is missing, new_symbol will create
9298 a LOC_UNRESOLVED symbol, the address of the variable will then
9299 be determined from the minimal symbol table whenever the variable
9300 is referenced.
9301 The address for the partial symbol table entry is not
9302 used by GDB, but it comes in handy for debugging partial symbol
9303 table building. */
9304
95554aad 9305 if (pdi->d.locdesc || pdi->has_type)
f47fb265 9306 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9307 built_actual_name != NULL,
f47fb265
MS
9308 VAR_DOMAIN, LOC_STATIC,
9309 &objfile->global_psymbols,
1762568f 9310 addr + baseaddr,
f47fb265 9311 cu->language, objfile);
c906108c
SS
9312 }
9313 else
9314 {
ff908ebf
AW
9315 int has_loc = pdi->d.locdesc != NULL;
9316
9317 /* Static Variable. Skip symbols whose value we cannot know (those
9318 without location descriptors or constant values). */
9319 if (!has_loc && !pdi->has_const_value)
decbce07 9320 {
15d034d0 9321 xfree (built_actual_name);
decbce07
MS
9322 return;
9323 }
ff908ebf 9324
f47fb265 9325 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9326 built_actual_name != NULL,
f47fb265
MS
9327 VAR_DOMAIN, LOC_STATIC,
9328 &objfile->static_psymbols,
ff908ebf 9329 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 9330 cu->language, objfile);
c906108c
SS
9331 }
9332 break;
9333 case DW_TAG_typedef:
9334 case DW_TAG_base_type:
a02abb62 9335 case DW_TAG_subrange_type:
38d518c9 9336 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9337 built_actual_name != NULL,
176620f1 9338 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 9339 &objfile->static_psymbols,
1762568f 9340 0, cu->language, objfile);
c906108c 9341 break;
74921315 9342 case DW_TAG_imported_declaration:
72bf9492
DJ
9343 case DW_TAG_namespace:
9344 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9345 built_actual_name != NULL,
72bf9492
DJ
9346 VAR_DOMAIN, LOC_TYPEDEF,
9347 &objfile->global_psymbols,
1762568f 9348 0, cu->language, objfile);
72bf9492 9349 break;
530e8392
KB
9350 case DW_TAG_module:
9351 add_psymbol_to_list (actual_name, strlen (actual_name),
9352 built_actual_name != NULL,
9353 MODULE_DOMAIN, LOC_TYPEDEF,
9354 &objfile->global_psymbols,
1762568f 9355 0, cu->language, objfile);
530e8392 9356 break;
c906108c 9357 case DW_TAG_class_type:
680b30c7 9358 case DW_TAG_interface_type:
c906108c
SS
9359 case DW_TAG_structure_type:
9360 case DW_TAG_union_type:
9361 case DW_TAG_enumeration_type:
fa4028e9
JB
9362 /* Skip external references. The DWARF standard says in the section
9363 about "Structure, Union, and Class Type Entries": "An incomplete
9364 structure, union or class type is represented by a structure,
9365 union or class entry that does not have a byte size attribute
9366 and that has a DW_AT_declaration attribute." */
9367 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 9368 {
15d034d0 9369 xfree (built_actual_name);
decbce07
MS
9370 return;
9371 }
fa4028e9 9372
63d06c5c
DC
9373 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9374 static vs. global. */
38d518c9 9375 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9376 built_actual_name != NULL,
176620f1 9377 STRUCT_DOMAIN, LOC_TYPEDEF,
9c37b5ae 9378 cu->language == language_cplus
63d06c5c
DC
9379 ? &objfile->global_psymbols
9380 : &objfile->static_psymbols,
1762568f 9381 0, cu->language, objfile);
c906108c 9382
c906108c
SS
9383 break;
9384 case DW_TAG_enumerator:
38d518c9 9385 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9386 built_actual_name != NULL,
176620f1 9387 VAR_DOMAIN, LOC_CONST,
9c37b5ae 9388 cu->language == language_cplus
f6fe98ef
DJ
9389 ? &objfile->global_psymbols
9390 : &objfile->static_psymbols,
1762568f 9391 0, cu->language, objfile);
c906108c
SS
9392 break;
9393 default:
9394 break;
9395 }
5c4e30ca 9396
15d034d0 9397 xfree (built_actual_name);
c906108c
SS
9398}
9399
5c4e30ca
DC
9400/* Read a partial die corresponding to a namespace; also, add a symbol
9401 corresponding to that namespace to the symbol table. NAMESPACE is
9402 the name of the enclosing namespace. */
91c24f0a 9403
72bf9492
DJ
9404static void
9405add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 9406 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9407 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 9408{
72bf9492 9409 /* Add a symbol for the namespace. */
e7c27a73 9410
72bf9492 9411 add_partial_symbol (pdi, cu);
5c4e30ca
DC
9412
9413 /* Now scan partial symbols in that namespace. */
9414
91c24f0a 9415 if (pdi->has_children)
cdc07690 9416 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
9417}
9418
5d7cb8df
JK
9419/* Read a partial die corresponding to a Fortran module. */
9420
9421static void
9422add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 9423 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 9424{
530e8392
KB
9425 /* Add a symbol for the namespace. */
9426
9427 add_partial_symbol (pdi, cu);
9428
f55ee35c 9429 /* Now scan partial symbols in that module. */
5d7cb8df
JK
9430
9431 if (pdi->has_children)
cdc07690 9432 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
9433}
9434
b1dc1806
XR
9435/* Read a partial die corresponding to a subprogram or an inlined
9436 subprogram and create a partial symbol for that subprogram.
9437 When the CU language allows it, this routine also defines a partial
9438 symbol for each nested subprogram that this subprogram contains.
9439 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9440 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 9441
cdc07690
YQ
9442 PDI may also be a lexical block, in which case we simply search
9443 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9444 Again, this is only performed when the CU language allows this
9445 type of definitions. */
9446
9447static void
9448add_partial_subprogram (struct partial_die_info *pdi,
9449 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9450 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 9451{
b1dc1806 9452 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
9453 {
9454 if (pdi->has_pc_info)
9455 {
9456 if (pdi->lowpc < *lowpc)
9457 *lowpc = pdi->lowpc;
9458 if (pdi->highpc > *highpc)
9459 *highpc = pdi->highpc;
cdc07690 9460 if (set_addrmap)
5734ee8b 9461 {
518817b3 9462 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
9463 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9464 CORE_ADDR baseaddr;
9465 CORE_ADDR highpc;
9466 CORE_ADDR lowpc;
5734ee8b
DJ
9467
9468 baseaddr = ANOFFSET (objfile->section_offsets,
9469 SECT_OFF_TEXT (objfile));
3e29f34a
MR
9470 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9471 pdi->lowpc + baseaddr);
9472 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9473 pdi->highpc + baseaddr);
9474 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 9475 cu->per_cu->v.psymtab);
5734ee8b 9476 }
481860b3
GB
9477 }
9478
9479 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9480 {
bc30ff58 9481 if (!pdi->is_declaration)
e8d05480
JB
9482 /* Ignore subprogram DIEs that do not have a name, they are
9483 illegal. Do not emit a complaint at this point, we will
9484 do so when we convert this psymtab into a symtab. */
9485 if (pdi->name)
9486 add_partial_symbol (pdi, cu);
bc30ff58
JB
9487 }
9488 }
6e70227d 9489
bc30ff58
JB
9490 if (! pdi->has_children)
9491 return;
9492
9493 if (cu->language == language_ada)
9494 {
9495 pdi = pdi->die_child;
9496 while (pdi != NULL)
9497 {
9498 fixup_partial_die (pdi, cu);
9499 if (pdi->tag == DW_TAG_subprogram
b1dc1806 9500 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 9501 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9502 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9503 pdi = pdi->die_sibling;
9504 }
9505 }
9506}
9507
91c24f0a
DC
9508/* Read a partial die corresponding to an enumeration type. */
9509
72bf9492
DJ
9510static void
9511add_partial_enumeration (struct partial_die_info *enum_pdi,
9512 struct dwarf2_cu *cu)
91c24f0a 9513{
72bf9492 9514 struct partial_die_info *pdi;
91c24f0a
DC
9515
9516 if (enum_pdi->name != NULL)
72bf9492
DJ
9517 add_partial_symbol (enum_pdi, cu);
9518
9519 pdi = enum_pdi->die_child;
9520 while (pdi)
91c24f0a 9521 {
72bf9492 9522 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 9523 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 9524 else
72bf9492
DJ
9525 add_partial_symbol (pdi, cu);
9526 pdi = pdi->die_sibling;
91c24f0a 9527 }
91c24f0a
DC
9528}
9529
6caca83c
CC
9530/* Return the initial uleb128 in the die at INFO_PTR. */
9531
9532static unsigned int
d521ce57 9533peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9534{
9535 unsigned int bytes_read;
9536
9537 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9538}
9539
685af9cd
TT
9540/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9541 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9542
4bb7a0a7
DJ
9543 Return the corresponding abbrev, or NULL if the number is zero (indicating
9544 an empty DIE). In either case *BYTES_READ will be set to the length of
9545 the initial number. */
9546
9547static struct abbrev_info *
685af9cd
TT
9548peek_die_abbrev (const die_reader_specs &reader,
9549 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 9550{
685af9cd 9551 dwarf2_cu *cu = reader.cu;
518817b3 9552 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
9553 unsigned int abbrev_number
9554 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
9555
9556 if (abbrev_number == 0)
9557 return NULL;
9558
685af9cd 9559 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
9560 if (!abbrev)
9561 {
422b9917
DE
9562 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9563 " at offset 0x%x [in module %s]"),
9564 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9c541725 9565 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9566 }
9567
9568 return abbrev;
9569}
9570
93311388
DE
9571/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9572 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9573 DIE. Any children of the skipped DIEs will also be skipped. */
9574
d521ce57
TT
9575static const gdb_byte *
9576skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9577{
4bb7a0a7
DJ
9578 while (1)
9579 {
685af9cd
TT
9580 unsigned int bytes_read;
9581 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9582
4bb7a0a7
DJ
9583 if (abbrev == NULL)
9584 return info_ptr + bytes_read;
9585 else
dee91e82 9586 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9587 }
9588}
9589
93311388
DE
9590/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9591 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9592 abbrev corresponding to that skipped uleb128 should be passed in
9593 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9594 children. */
9595
d521ce57
TT
9596static const gdb_byte *
9597skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9598 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9599{
9600 unsigned int bytes_read;
9601 struct attribute attr;
dee91e82
DE
9602 bfd *abfd = reader->abfd;
9603 struct dwarf2_cu *cu = reader->cu;
d521ce57 9604 const gdb_byte *buffer = reader->buffer;
f664829e 9605 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9606 unsigned int form, i;
9607
9608 for (i = 0; i < abbrev->num_attrs; i++)
9609 {
9610 /* The only abbrev we care about is DW_AT_sibling. */
9611 if (abbrev->attrs[i].name == DW_AT_sibling)
9612 {
dee91e82 9613 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9614 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9615 complaint (&symfile_complaints,
9616 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9617 else
b9502d3f 9618 {
9c541725
PA
9619 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9620 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9621
9622 if (sibling_ptr < info_ptr)
9623 complaint (&symfile_complaints,
9624 _("DW_AT_sibling points backwards"));
22869d73
KS
9625 else if (sibling_ptr > reader->buffer_end)
9626 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9627 else
9628 return sibling_ptr;
9629 }
4bb7a0a7
DJ
9630 }
9631
9632 /* If it isn't DW_AT_sibling, skip this attribute. */
9633 form = abbrev->attrs[i].form;
9634 skip_attribute:
9635 switch (form)
9636 {
4bb7a0a7 9637 case DW_FORM_ref_addr:
ae411497
TT
9638 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9639 and later it is offset sized. */
9640 if (cu->header.version == 2)
9641 info_ptr += cu->header.addr_size;
9642 else
9643 info_ptr += cu->header.offset_size;
9644 break;
36586728
TT
9645 case DW_FORM_GNU_ref_alt:
9646 info_ptr += cu->header.offset_size;
9647 break;
ae411497 9648 case DW_FORM_addr:
4bb7a0a7
DJ
9649 info_ptr += cu->header.addr_size;
9650 break;
9651 case DW_FORM_data1:
9652 case DW_FORM_ref1:
9653 case DW_FORM_flag:
9654 info_ptr += 1;
9655 break;
2dc7f7b3 9656 case DW_FORM_flag_present:
43988095 9657 case DW_FORM_implicit_const:
2dc7f7b3 9658 break;
4bb7a0a7
DJ
9659 case DW_FORM_data2:
9660 case DW_FORM_ref2:
9661 info_ptr += 2;
9662 break;
9663 case DW_FORM_data4:
9664 case DW_FORM_ref4:
9665 info_ptr += 4;
9666 break;
9667 case DW_FORM_data8:
9668 case DW_FORM_ref8:
55f1336d 9669 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9670 info_ptr += 8;
9671 break;
0224619f
JK
9672 case DW_FORM_data16:
9673 info_ptr += 16;
9674 break;
4bb7a0a7 9675 case DW_FORM_string:
9b1c24c8 9676 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9677 info_ptr += bytes_read;
9678 break;
2dc7f7b3 9679 case DW_FORM_sec_offset:
4bb7a0a7 9680 case DW_FORM_strp:
36586728 9681 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9682 info_ptr += cu->header.offset_size;
9683 break;
2dc7f7b3 9684 case DW_FORM_exprloc:
4bb7a0a7
DJ
9685 case DW_FORM_block:
9686 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9687 info_ptr += bytes_read;
9688 break;
9689 case DW_FORM_block1:
9690 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9691 break;
9692 case DW_FORM_block2:
9693 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9694 break;
9695 case DW_FORM_block4:
9696 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9697 break;
9698 case DW_FORM_sdata:
9699 case DW_FORM_udata:
9700 case DW_FORM_ref_udata:
3019eac3
DE
9701 case DW_FORM_GNU_addr_index:
9702 case DW_FORM_GNU_str_index:
d521ce57 9703 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9704 break;
9705 case DW_FORM_indirect:
9706 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9707 info_ptr += bytes_read;
9708 /* We need to continue parsing from here, so just go back to
9709 the top. */
9710 goto skip_attribute;
9711
9712 default:
3e43a32a
MS
9713 error (_("Dwarf Error: Cannot handle %s "
9714 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9715 dwarf_form_name (form),
9716 bfd_get_filename (abfd));
9717 }
9718 }
9719
9720 if (abbrev->has_children)
dee91e82 9721 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9722 else
9723 return info_ptr;
9724}
9725
93311388 9726/* Locate ORIG_PDI's sibling.
dee91e82 9727 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9728
d521ce57 9729static const gdb_byte *
dee91e82
DE
9730locate_pdi_sibling (const struct die_reader_specs *reader,
9731 struct partial_die_info *orig_pdi,
d521ce57 9732 const gdb_byte *info_ptr)
91c24f0a
DC
9733{
9734 /* Do we know the sibling already? */
72bf9492 9735
91c24f0a
DC
9736 if (orig_pdi->sibling)
9737 return orig_pdi->sibling;
9738
9739 /* Are there any children to deal with? */
9740
9741 if (!orig_pdi->has_children)
9742 return info_ptr;
9743
4bb7a0a7 9744 /* Skip the children the long way. */
91c24f0a 9745
dee91e82 9746 return skip_children (reader, info_ptr);
91c24f0a
DC
9747}
9748
257e7a09 9749/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9750 not NULL. */
c906108c
SS
9751
9752static void
257e7a09
YQ
9753dwarf2_read_symtab (struct partial_symtab *self,
9754 struct objfile *objfile)
c906108c 9755{
ed2dc618
SM
9756 struct dwarf2_per_objfile *dwarf2_per_objfile
9757 = get_dwarf2_per_objfile (objfile);
9758
257e7a09 9759 if (self->readin)
c906108c 9760 {
442e4d9c 9761 warning (_("bug: psymtab for %s is already read in."),
257e7a09 9762 self->filename);
442e4d9c
YQ
9763 }
9764 else
9765 {
9766 if (info_verbose)
c906108c 9767 {
442e4d9c 9768 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 9769 self->filename);
442e4d9c 9770 gdb_flush (gdb_stdout);
c906108c 9771 }
c906108c 9772
442e4d9c
YQ
9773 /* If this psymtab is constructed from a debug-only objfile, the
9774 has_section_at_zero flag will not necessarily be correct. We
9775 can get the correct value for this flag by looking at the data
9776 associated with the (presumably stripped) associated objfile. */
9777 if (objfile->separate_debug_objfile_backlink)
9778 {
9779 struct dwarf2_per_objfile *dpo_backlink
ed2dc618 9780 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9a619af0 9781
442e4d9c
YQ
9782 dwarf2_per_objfile->has_section_at_zero
9783 = dpo_backlink->has_section_at_zero;
9784 }
b2ab525c 9785
442e4d9c 9786 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 9787
257e7a09 9788 psymtab_to_symtab_1 (self);
c906108c 9789
442e4d9c
YQ
9790 /* Finish up the debug error message. */
9791 if (info_verbose)
9792 printf_filtered (_("done.\n"));
c906108c 9793 }
95554aad 9794
ed2dc618 9795 process_cu_includes (dwarf2_per_objfile);
c906108c 9796}
9cdd5dbd
DE
9797\f
9798/* Reading in full CUs. */
c906108c 9799
10b3939b
DJ
9800/* Add PER_CU to the queue. */
9801
9802static void
95554aad
TT
9803queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9804 enum language pretend_language)
10b3939b
DJ
9805{
9806 struct dwarf2_queue_item *item;
9807
9808 per_cu->queued = 1;
8d749320 9809 item = XNEW (struct dwarf2_queue_item);
10b3939b 9810 item->per_cu = per_cu;
95554aad 9811 item->pretend_language = pretend_language;
10b3939b
DJ
9812 item->next = NULL;
9813
9814 if (dwarf2_queue == NULL)
9815 dwarf2_queue = item;
9816 else
9817 dwarf2_queue_tail->next = item;
9818
9819 dwarf2_queue_tail = item;
9820}
9821
89e63ee4
DE
9822/* If PER_CU is not yet queued, add it to the queue.
9823 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9824 dependency.
0907af0c 9825 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9826 meaning either PER_CU is already queued or it is already loaded.
9827
9828 N.B. There is an invariant here that if a CU is queued then it is loaded.
9829 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9830
9831static int
89e63ee4 9832maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9833 struct dwarf2_per_cu_data *per_cu,
9834 enum language pretend_language)
9835{
9836 /* We may arrive here during partial symbol reading, if we need full
9837 DIEs to process an unusual case (e.g. template arguments). Do
9838 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9839 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9840 {
9841 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9842 return 1;
9843 return 0;
9844 }
9845
9846 /* Mark the dependence relation so that we don't flush PER_CU
9847 too early. */
89e63ee4
DE
9848 if (dependent_cu != NULL)
9849 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9850
9851 /* If it's already on the queue, we have nothing to do. */
9852 if (per_cu->queued)
9853 return 0;
9854
9855 /* If the compilation unit is already loaded, just mark it as
9856 used. */
9857 if (per_cu->cu != NULL)
9858 {
9859 per_cu->cu->last_used = 0;
9860 return 0;
9861 }
9862
9863 /* Add it to the queue. */
9864 queue_comp_unit (per_cu, pretend_language);
9865
9866 return 1;
9867}
9868
10b3939b
DJ
9869/* Process the queue. */
9870
9871static void
ed2dc618 9872process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
9873{
9874 struct dwarf2_queue_item *item, *next_item;
9875
b4f54984 9876 if (dwarf_read_debug)
45cfd468
DE
9877 {
9878 fprintf_unfiltered (gdb_stdlog,
9879 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9880 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9881 }
9882
03dd20cc
DJ
9883 /* The queue starts out with one item, but following a DIE reference
9884 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9885 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9886 {
cc12ce38
DE
9887 if ((dwarf2_per_objfile->using_index
9888 ? !item->per_cu->v.quick->compunit_symtab
9889 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9890 /* Skip dummy CUs. */
9891 && item->per_cu->cu != NULL)
f4dc4d17
DE
9892 {
9893 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9894 unsigned int debug_print_threshold;
247f5c4f 9895 char buf[100];
f4dc4d17 9896
247f5c4f 9897 if (per_cu->is_debug_types)
f4dc4d17 9898 {
247f5c4f
DE
9899 struct signatured_type *sig_type =
9900 (struct signatured_type *) per_cu;
9901
9902 sprintf (buf, "TU %s at offset 0x%x",
73be47f5 9903 hex_string (sig_type->signature),
9c541725 9904 to_underlying (per_cu->sect_off));
73be47f5
DE
9905 /* There can be 100s of TUs.
9906 Only print them in verbose mode. */
9907 debug_print_threshold = 2;
f4dc4d17 9908 }
247f5c4f 9909 else
73be47f5 9910 {
9c541725
PA
9911 sprintf (buf, "CU at offset 0x%x",
9912 to_underlying (per_cu->sect_off));
73be47f5
DE
9913 debug_print_threshold = 1;
9914 }
247f5c4f 9915
b4f54984 9916 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9917 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9918
9919 if (per_cu->is_debug_types)
9920 process_full_type_unit (per_cu, item->pretend_language);
9921 else
9922 process_full_comp_unit (per_cu, item->pretend_language);
9923
b4f54984 9924 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9925 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9926 }
10b3939b
DJ
9927
9928 item->per_cu->queued = 0;
9929 next_item = item->next;
9930 xfree (item);
9931 }
9932
9933 dwarf2_queue_tail = NULL;
45cfd468 9934
b4f54984 9935 if (dwarf_read_debug)
45cfd468
DE
9936 {
9937 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9938 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9939 }
10b3939b
DJ
9940}
9941
9942/* Free all allocated queue entries. This function only releases anything if
9943 an error was thrown; if the queue was processed then it would have been
9944 freed as we went along. */
9945
9946static void
9947dwarf2_release_queue (void *dummy)
9948{
9949 struct dwarf2_queue_item *item, *last;
9950
9951 item = dwarf2_queue;
9952 while (item)
9953 {
9954 /* Anything still marked queued is likely to be in an
9955 inconsistent state, so discard it. */
9956 if (item->per_cu->queued)
9957 {
9958 if (item->per_cu->cu != NULL)
dee91e82 9959 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
9960 item->per_cu->queued = 0;
9961 }
9962
9963 last = item;
9964 item = item->next;
9965 xfree (last);
9966 }
9967
9968 dwarf2_queue = dwarf2_queue_tail = NULL;
9969}
9970
9971/* Read in full symbols for PST, and anything it depends on. */
9972
c906108c 9973static void
fba45db2 9974psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 9975{
10b3939b 9976 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
9977 int i;
9978
95554aad
TT
9979 if (pst->readin)
9980 return;
9981
aaa75496 9982 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
9983 if (!pst->dependencies[i]->readin
9984 && pst->dependencies[i]->user == NULL)
aaa75496
JB
9985 {
9986 /* Inform about additional files that need to be read in. */
9987 if (info_verbose)
9988 {
a3f17187 9989 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
9990 fputs_filtered (" ", gdb_stdout);
9991 wrap_here ("");
9992 fputs_filtered ("and ", gdb_stdout);
9993 wrap_here ("");
9994 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 9995 wrap_here (""); /* Flush output. */
aaa75496
JB
9996 gdb_flush (gdb_stdout);
9997 }
9998 psymtab_to_symtab_1 (pst->dependencies[i]);
9999 }
10000
9a3c8263 10001 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
10002
10003 if (per_cu == NULL)
aaa75496
JB
10004 {
10005 /* It's an include file, no symbols to read for it.
10006 Everything is in the parent symtab. */
10007 pst->readin = 1;
10008 return;
10009 }
c906108c 10010
a0f42c21 10011 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
10012}
10013
dee91e82
DE
10014/* Trivial hash function for die_info: the hash value of a DIE
10015 is its offset in .debug_info for this objfile. */
10b3939b 10016
dee91e82
DE
10017static hashval_t
10018die_hash (const void *item)
10b3939b 10019{
9a3c8263 10020 const struct die_info *die = (const struct die_info *) item;
6502dd73 10021
9c541725 10022 return to_underlying (die->sect_off);
dee91e82 10023}
63d06c5c 10024
dee91e82
DE
10025/* Trivial comparison function for die_info structures: two DIEs
10026 are equal if they have the same offset. */
98bfdba5 10027
dee91e82
DE
10028static int
10029die_eq (const void *item_lhs, const void *item_rhs)
10030{
9a3c8263
SM
10031 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
10032 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 10033
9c541725 10034 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 10035}
c906108c 10036
dee91e82
DE
10037/* die_reader_func for load_full_comp_unit.
10038 This is identical to read_signatured_type_reader,
10039 but is kept separate for now. */
c906108c 10040
dee91e82
DE
10041static void
10042load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 10043 const gdb_byte *info_ptr,
dee91e82
DE
10044 struct die_info *comp_unit_die,
10045 int has_children,
10046 void *data)
10047{
10048 struct dwarf2_cu *cu = reader->cu;
9a3c8263 10049 enum language *language_ptr = (enum language *) data;
6caca83c 10050
dee91e82
DE
10051 gdb_assert (cu->die_hash == NULL);
10052 cu->die_hash =
10053 htab_create_alloc_ex (cu->header.length / 12,
10054 die_hash,
10055 die_eq,
10056 NULL,
10057 &cu->comp_unit_obstack,
10058 hashtab_obstack_allocate,
10059 dummy_obstack_deallocate);
e142c38c 10060
dee91e82
DE
10061 if (has_children)
10062 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
10063 &info_ptr, comp_unit_die);
10064 cu->dies = comp_unit_die;
10065 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
10066
10067 /* We try not to read any attributes in this function, because not
9cdd5dbd 10068 all CUs needed for references have been loaded yet, and symbol
10b3939b 10069 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
10070 or we won't be able to build types correctly.
10071 Similarly, if we do not read the producer, we can not apply
10072 producer-specific interpretation. */
95554aad 10073 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 10074}
10b3939b 10075
dee91e82 10076/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 10077
dee91e82 10078static void
95554aad
TT
10079load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
10080 enum language pretend_language)
dee91e82 10081{
3019eac3 10082 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 10083
f4dc4d17
DE
10084 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
10085 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
10086}
10087
3da10d80
KS
10088/* Add a DIE to the delayed physname list. */
10089
10090static void
10091add_to_method_list (struct type *type, int fnfield_index, int index,
10092 const char *name, struct die_info *die,
10093 struct dwarf2_cu *cu)
10094{
10095 struct delayed_method_info mi;
10096 mi.type = type;
10097 mi.fnfield_index = fnfield_index;
10098 mi.index = index;
10099 mi.name = name;
10100 mi.die = die;
c89b44cd 10101 cu->method_list.push_back (mi);
3da10d80
KS
10102}
10103
3693fdb3
PA
10104/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
10105 "const" / "volatile". If so, decrements LEN by the length of the
10106 modifier and return true. Otherwise return false. */
10107
10108template<size_t N>
10109static bool
10110check_modifier (const char *physname, size_t &len, const char (&mod)[N])
10111{
10112 size_t mod_len = sizeof (mod) - 1;
10113 if (len > mod_len && startswith (physname + (len - mod_len), mod))
10114 {
10115 len -= mod_len;
10116 return true;
10117 }
10118 return false;
10119}
10120
3da10d80
KS
10121/* Compute the physnames of any methods on the CU's method list.
10122
10123 The computation of method physnames is delayed in order to avoid the
10124 (bad) condition that one of the method's formal parameters is of an as yet
10125 incomplete type. */
10126
10127static void
10128compute_delayed_physnames (struct dwarf2_cu *cu)
10129{
3693fdb3 10130 /* Only C++ delays computing physnames. */
c89b44cd 10131 if (cu->method_list.empty ())
3693fdb3
PA
10132 return;
10133 gdb_assert (cu->language == language_cplus);
10134
c89b44cd 10135 for (struct delayed_method_info &mi : cu->method_list)
3da10d80 10136 {
1d06ead6 10137 const char *physname;
3da10d80 10138 struct fn_fieldlist *fn_flp
c89b44cd
TT
10139 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
10140 physname = dwarf2_physname (mi.name, mi.die, cu);
10141 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 10142 = physname ? physname : "";
3693fdb3
PA
10143
10144 /* Since there's no tag to indicate whether a method is a
10145 const/volatile overload, extract that information out of the
10146 demangled name. */
10147 if (physname != NULL)
10148 {
10149 size_t len = strlen (physname);
10150
10151 while (1)
10152 {
10153 if (physname[len] == ')') /* shortcut */
10154 break;
10155 else if (check_modifier (physname, len, " const"))
c89b44cd 10156 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 10157 else if (check_modifier (physname, len, " volatile"))
c89b44cd 10158 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
10159 else
10160 break;
10161 }
10162 }
3da10d80 10163 }
c89b44cd
TT
10164
10165 /* The list is no longer needed. */
10166 cu->method_list.clear ();
3da10d80
KS
10167}
10168
a766d390
DE
10169/* Go objects should be embedded in a DW_TAG_module DIE,
10170 and it's not clear if/how imported objects will appear.
10171 To keep Go support simple until that's worked out,
10172 go back through what we've read and create something usable.
10173 We could do this while processing each DIE, and feels kinda cleaner,
10174 but that way is more invasive.
10175 This is to, for example, allow the user to type "p var" or "b main"
10176 without having to specify the package name, and allow lookups
10177 of module.object to work in contexts that use the expression
10178 parser. */
10179
10180static void
10181fixup_go_packaging (struct dwarf2_cu *cu)
10182{
10183 char *package_name = NULL;
10184 struct pending *list;
10185 int i;
10186
10187 for (list = global_symbols; list != NULL; list = list->next)
10188 {
10189 for (i = 0; i < list->nsyms; ++i)
10190 {
10191 struct symbol *sym = list->symbol[i];
10192
10193 if (SYMBOL_LANGUAGE (sym) == language_go
10194 && SYMBOL_CLASS (sym) == LOC_BLOCK)
10195 {
10196 char *this_package_name = go_symbol_package_name (sym);
10197
10198 if (this_package_name == NULL)
10199 continue;
10200 if (package_name == NULL)
10201 package_name = this_package_name;
10202 else
10203 {
518817b3
SM
10204 struct objfile *objfile
10205 = cu->per_cu->dwarf2_per_objfile->objfile;
a766d390
DE
10206 if (strcmp (package_name, this_package_name) != 0)
10207 complaint (&symfile_complaints,
10208 _("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
10209 (symbol_symtab (sym) != NULL
10210 ? symtab_to_filename_for_display
10211 (symbol_symtab (sym))
e3b94546 10212 : objfile_name (objfile)),
a766d390
DE
10213 this_package_name, package_name);
10214 xfree (this_package_name);
10215 }
10216 }
10217 }
10218 }
10219
10220 if (package_name != NULL)
10221 {
518817b3 10222 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 10223 const char *saved_package_name
224c3ddb
SM
10224 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
10225 package_name,
10226 strlen (package_name));
19f392bc
UW
10227 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
10228 saved_package_name);
a766d390
DE
10229 struct symbol *sym;
10230
10231 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10232
e623cf5d 10233 sym = allocate_symbol (objfile);
f85f34ed 10234 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
10235 SYMBOL_SET_NAMES (sym, saved_package_name,
10236 strlen (saved_package_name), 0, objfile);
a766d390
DE
10237 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
10238 e.g., "main" finds the "main" module and not C's main(). */
10239 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 10240 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
10241 SYMBOL_TYPE (sym) = type;
10242
10243 add_symbol_to_list (sym, &global_symbols);
10244
10245 xfree (package_name);
10246 }
10247}
10248
95554aad
TT
10249/* Return the symtab for PER_CU. This works properly regardless of
10250 whether we're using the index or psymtabs. */
10251
43f3e411
DE
10252static struct compunit_symtab *
10253get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 10254{
ed2dc618 10255 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
10256 ? per_cu->v.quick->compunit_symtab
10257 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10258}
10259
10260/* A helper function for computing the list of all symbol tables
10261 included by PER_CU. */
10262
10263static void
43f3e411 10264recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 10265 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10266 struct dwarf2_per_cu_data *per_cu,
43f3e411 10267 struct compunit_symtab *immediate_parent)
95554aad
TT
10268{
10269 void **slot;
10270 int ix;
43f3e411 10271 struct compunit_symtab *cust;
95554aad
TT
10272 struct dwarf2_per_cu_data *iter;
10273
10274 slot = htab_find_slot (all_children, per_cu, INSERT);
10275 if (*slot != NULL)
10276 {
10277 /* This inclusion and its children have been processed. */
10278 return;
10279 }
10280
10281 *slot = per_cu;
10282 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10283 cust = get_compunit_symtab (per_cu);
10284 if (cust != NULL)
ec94af83
DE
10285 {
10286 /* If this is a type unit only add its symbol table if we haven't
10287 seen it yet (type unit per_cu's can share symtabs). */
10288 if (per_cu->is_debug_types)
10289 {
43f3e411 10290 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10291 if (*slot == NULL)
10292 {
43f3e411
DE
10293 *slot = cust;
10294 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10295 if (cust->user == NULL)
10296 cust->user = immediate_parent;
ec94af83
DE
10297 }
10298 }
10299 else
f9125b6c 10300 {
43f3e411
DE
10301 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10302 if (cust->user == NULL)
10303 cust->user = immediate_parent;
f9125b6c 10304 }
ec94af83 10305 }
95554aad
TT
10306
10307 for (ix = 0;
796a7ff8 10308 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 10309 ++ix)
ec94af83
DE
10310 {
10311 recursively_compute_inclusions (result, all_children,
43f3e411 10312 all_type_symtabs, iter, cust);
ec94af83 10313 }
95554aad
TT
10314}
10315
43f3e411 10316/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10317 PER_CU. */
10318
10319static void
43f3e411 10320compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10321{
f4dc4d17
DE
10322 gdb_assert (! per_cu->is_debug_types);
10323
796a7ff8 10324 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
10325 {
10326 int ix, len;
ec94af83 10327 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
10328 struct compunit_symtab *compunit_symtab_iter;
10329 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 10330 htab_t all_children, all_type_symtabs;
43f3e411 10331 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10332
10333 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10334 if (cust == NULL)
95554aad
TT
10335 return;
10336
10337 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10338 NULL, xcalloc, xfree);
ec94af83
DE
10339 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10340 NULL, xcalloc, xfree);
95554aad
TT
10341
10342 for (ix = 0;
796a7ff8 10343 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 10344 ix, per_cu_iter);
95554aad 10345 ++ix)
ec94af83
DE
10346 {
10347 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 10348 all_type_symtabs, per_cu_iter,
43f3e411 10349 cust);
ec94af83 10350 }
95554aad 10351
ec94af83 10352 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
10353 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10354 cust->includes
ed2dc618 10355 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 10356 struct compunit_symtab *, len + 1);
95554aad 10357 for (ix = 0;
43f3e411
DE
10358 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10359 compunit_symtab_iter);
95554aad 10360 ++ix)
43f3e411
DE
10361 cust->includes[ix] = compunit_symtab_iter;
10362 cust->includes[len] = NULL;
95554aad 10363
43f3e411 10364 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 10365 htab_delete (all_children);
ec94af83 10366 htab_delete (all_type_symtabs);
95554aad
TT
10367 }
10368}
10369
10370/* Compute the 'includes' field for the symtabs of all the CUs we just
10371 read. */
10372
10373static void
ed2dc618 10374process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad
TT
10375{
10376 int ix;
10377 struct dwarf2_per_cu_data *iter;
10378
10379 for (ix = 0;
10380 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10381 ix, iter);
10382 ++ix)
f4dc4d17
DE
10383 {
10384 if (! iter->is_debug_types)
43f3e411 10385 compute_compunit_symtab_includes (iter);
f4dc4d17 10386 }
95554aad
TT
10387
10388 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10389}
10390
9cdd5dbd 10391/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10392 already been loaded into memory. */
10393
10394static void
95554aad
TT
10395process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10396 enum language pretend_language)
10b3939b 10397{
10b3939b 10398 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10399 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10400 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10401 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10402 CORE_ADDR lowpc, highpc;
43f3e411 10403 struct compunit_symtab *cust;
10b3939b 10404 CORE_ADDR baseaddr;
4359dff1 10405 struct block *static_block;
3e29f34a 10406 CORE_ADDR addr;
10b3939b
DJ
10407
10408 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10409
10b3939b 10410 buildsym_init ();
33c7c59d 10411 scoped_free_pendings free_pending;
c89b44cd
TT
10412
10413 /* Clear the list here in case something was left over. */
10414 cu->method_list.clear ();
10b3939b
DJ
10415
10416 cu->list_in_scope = &file_symbols;
c906108c 10417
95554aad
TT
10418 cu->language = pretend_language;
10419 cu->language_defn = language_def (cu->language);
10420
c906108c 10421 /* Do line number decoding in read_file_scope () */
10b3939b 10422 process_die (cu->dies, cu);
c906108c 10423
a766d390
DE
10424 /* For now fudge the Go package. */
10425 if (cu->language == language_go)
10426 fixup_go_packaging (cu);
10427
3da10d80
KS
10428 /* Now that we have processed all the DIEs in the CU, all the types
10429 should be complete, and it should now be safe to compute all of the
10430 physnames. */
10431 compute_delayed_physnames (cu);
3da10d80 10432
fae299cd
DC
10433 /* Some compilers don't define a DW_AT_high_pc attribute for the
10434 compilation unit. If the DW_AT_high_pc is missing, synthesize
10435 it, by scanning the DIE's below the compilation unit. */
10b3939b 10436 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10437
3e29f34a
MR
10438 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10439 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10440
10441 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10442 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10443 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10444 addrmap to help ensure it has an accurate map of pc values belonging to
10445 this comp unit. */
10446 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10447
43f3e411
DE
10448 cust = end_symtab_from_static_block (static_block,
10449 SECT_OFF_TEXT (objfile), 0);
c906108c 10450
43f3e411 10451 if (cust != NULL)
c906108c 10452 {
df15bd07 10453 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10454
8be455d7
JK
10455 /* Set symtab language to language from DW_AT_language. If the
10456 compilation is from a C file generated by language preprocessors, do
10457 not set the language if it was already deduced by start_subfile. */
43f3e411 10458 if (!(cu->language == language_c
40e3ad0e 10459 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10460 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10461
10462 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10463 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10464 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10465 there were bugs in prologue debug info, fixed later in GCC-4.5
10466 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10467
10468 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10469 needed, it would be wrong due to missing DW_AT_producer there.
10470
10471 Still one can confuse GDB by using non-standard GCC compilation
10472 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10473 */
ab260dad 10474 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10475 cust->locations_valid = 1;
e0d00bc7
JK
10476
10477 if (gcc_4_minor >= 5)
43f3e411 10478 cust->epilogue_unwind_valid = 1;
96408a79 10479
43f3e411 10480 cust->call_site_htab = cu->call_site_htab;
c906108c 10481 }
9291a0cd
TT
10482
10483 if (dwarf2_per_objfile->using_index)
43f3e411 10484 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10485 else
10486 {
10487 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10488 pst->compunit_symtab = cust;
9291a0cd
TT
10489 pst->readin = 1;
10490 }
c906108c 10491
95554aad
TT
10492 /* Push it for inclusion processing later. */
10493 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
f4dc4d17 10494}
45cfd468 10495
f4dc4d17
DE
10496/* Generate full symbol information for type unit PER_CU, whose DIEs have
10497 already been loaded into memory. */
10498
10499static void
10500process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10501 enum language pretend_language)
10502{
10503 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10504 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10505 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10506 struct compunit_symtab *cust;
0186c6a7
DE
10507 struct signatured_type *sig_type;
10508
10509 gdb_assert (per_cu->is_debug_types);
10510 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
10511
10512 buildsym_init ();
33c7c59d 10513 scoped_free_pendings free_pending;
c89b44cd
TT
10514
10515 /* Clear the list here in case something was left over. */
10516 cu->method_list.clear ();
f4dc4d17
DE
10517
10518 cu->list_in_scope = &file_symbols;
10519
10520 cu->language = pretend_language;
10521 cu->language_defn = language_def (cu->language);
10522
10523 /* The symbol tables are set up in read_type_unit_scope. */
10524 process_die (cu->dies, cu);
10525
10526 /* For now fudge the Go package. */
10527 if (cu->language == language_go)
10528 fixup_go_packaging (cu);
10529
10530 /* Now that we have processed all the DIEs in the CU, all the types
10531 should be complete, and it should now be safe to compute all of the
10532 physnames. */
10533 compute_delayed_physnames (cu);
f4dc4d17
DE
10534
10535 /* TUs share symbol tables.
10536 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10537 of it with end_expandable_symtab. Otherwise, complete the addition of
10538 this TU's symbols to the existing symtab. */
43f3e411 10539 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10540 {
43f3e411
DE
10541 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10542 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10543
43f3e411 10544 if (cust != NULL)
f4dc4d17
DE
10545 {
10546 /* Set symtab language to language from DW_AT_language. If the
10547 compilation is from a C file generated by language preprocessors,
10548 do not set the language if it was already deduced by
10549 start_subfile. */
43f3e411
DE
10550 if (!(cu->language == language_c
10551 && COMPUNIT_FILETABS (cust)->language != language_c))
10552 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10553 }
10554 }
10555 else
10556 {
0ab9ce85 10557 augment_type_symtab ();
43f3e411 10558 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10559 }
10560
10561 if (dwarf2_per_objfile->using_index)
43f3e411 10562 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10563 else
10564 {
10565 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10566 pst->compunit_symtab = cust;
f4dc4d17 10567 pst->readin = 1;
45cfd468 10568 }
c906108c
SS
10569}
10570
95554aad
TT
10571/* Process an imported unit DIE. */
10572
10573static void
10574process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10575{
10576 struct attribute *attr;
10577
f4dc4d17
DE
10578 /* For now we don't handle imported units in type units. */
10579 if (cu->per_cu->is_debug_types)
10580 {
10581 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10582 " supported in type units [in module %s]"),
518817b3 10583 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10584 }
10585
95554aad
TT
10586 attr = dwarf2_attr (die, DW_AT_import, cu);
10587 if (attr != NULL)
10588 {
9c541725
PA
10589 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10590 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10591 dwarf2_per_cu_data *per_cu
e3b94546 10592 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10593 cu->per_cu->dwarf2_per_objfile);
95554aad 10594
69d751e3 10595 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
10596 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10597 load_full_comp_unit (per_cu, cu->language);
10598
796a7ff8 10599 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
10600 per_cu);
10601 }
10602}
10603
4c8aa72d
PA
10604/* RAII object that represents a process_die scope: i.e.,
10605 starts/finishes processing a DIE. */
10606class process_die_scope
adde2bff 10607{
4c8aa72d
PA
10608public:
10609 process_die_scope (die_info *die, dwarf2_cu *cu)
10610 : m_die (die), m_cu (cu)
10611 {
10612 /* We should only be processing DIEs not already in process. */
10613 gdb_assert (!m_die->in_process);
10614 m_die->in_process = true;
10615 }
8c3cb9fa 10616
4c8aa72d
PA
10617 ~process_die_scope ()
10618 {
10619 m_die->in_process = false;
10620
10621 /* If we're done processing the DIE for the CU that owns the line
10622 header, we don't need the line header anymore. */
10623 if (m_cu->line_header_die_owner == m_die)
10624 {
10625 delete m_cu->line_header;
10626 m_cu->line_header = NULL;
10627 m_cu->line_header_die_owner = NULL;
10628 }
10629 }
10630
10631private:
10632 die_info *m_die;
10633 dwarf2_cu *m_cu;
10634};
adde2bff 10635
c906108c
SS
10636/* Process a die and its children. */
10637
10638static void
e7c27a73 10639process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10640{
4c8aa72d 10641 process_die_scope scope (die, cu);
adde2bff 10642
c906108c
SS
10643 switch (die->tag)
10644 {
10645 case DW_TAG_padding:
10646 break;
10647 case DW_TAG_compile_unit:
95554aad 10648 case DW_TAG_partial_unit:
e7c27a73 10649 read_file_scope (die, cu);
c906108c 10650 break;
348e048f
DE
10651 case DW_TAG_type_unit:
10652 read_type_unit_scope (die, cu);
10653 break;
c906108c 10654 case DW_TAG_subprogram:
c906108c 10655 case DW_TAG_inlined_subroutine:
edb3359d 10656 read_func_scope (die, cu);
c906108c
SS
10657 break;
10658 case DW_TAG_lexical_block:
14898363
L
10659 case DW_TAG_try_block:
10660 case DW_TAG_catch_block:
e7c27a73 10661 read_lexical_block_scope (die, cu);
c906108c 10662 break;
216f72a1 10663 case DW_TAG_call_site:
96408a79
SA
10664 case DW_TAG_GNU_call_site:
10665 read_call_site_scope (die, cu);
10666 break;
c906108c 10667 case DW_TAG_class_type:
680b30c7 10668 case DW_TAG_interface_type:
c906108c
SS
10669 case DW_TAG_structure_type:
10670 case DW_TAG_union_type:
134d01f1 10671 process_structure_scope (die, cu);
c906108c
SS
10672 break;
10673 case DW_TAG_enumeration_type:
134d01f1 10674 process_enumeration_scope (die, cu);
c906108c 10675 break;
134d01f1 10676
f792889a
DJ
10677 /* These dies have a type, but processing them does not create
10678 a symbol or recurse to process the children. Therefore we can
10679 read them on-demand through read_type_die. */
c906108c 10680 case DW_TAG_subroutine_type:
72019c9c 10681 case DW_TAG_set_type:
c906108c 10682 case DW_TAG_array_type:
c906108c 10683 case DW_TAG_pointer_type:
c906108c 10684 case DW_TAG_ptr_to_member_type:
c906108c 10685 case DW_TAG_reference_type:
4297a3f0 10686 case DW_TAG_rvalue_reference_type:
c906108c 10687 case DW_TAG_string_type:
c906108c 10688 break;
134d01f1 10689
c906108c 10690 case DW_TAG_base_type:
a02abb62 10691 case DW_TAG_subrange_type:
cb249c71 10692 case DW_TAG_typedef:
134d01f1
DJ
10693 /* Add a typedef symbol for the type definition, if it has a
10694 DW_AT_name. */
f792889a 10695 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10696 break;
c906108c 10697 case DW_TAG_common_block:
e7c27a73 10698 read_common_block (die, cu);
c906108c
SS
10699 break;
10700 case DW_TAG_common_inclusion:
10701 break;
d9fa45fe 10702 case DW_TAG_namespace:
4d4ec4e5 10703 cu->processing_has_namespace_info = 1;
e7c27a73 10704 read_namespace (die, cu);
d9fa45fe 10705 break;
5d7cb8df 10706 case DW_TAG_module:
4d4ec4e5 10707 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
10708 read_module (die, cu);
10709 break;
d9fa45fe 10710 case DW_TAG_imported_declaration:
74921315
KS
10711 cu->processing_has_namespace_info = 1;
10712 if (read_namespace_alias (die, cu))
10713 break;
10714 /* The declaration is not a global namespace alias: fall through. */
d9fa45fe 10715 case DW_TAG_imported_module:
4d4ec4e5 10716 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
10717 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10718 || cu->language != language_fortran))
10719 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
10720 dwarf_tag_name (die->tag));
10721 read_import_statement (die, cu);
d9fa45fe 10722 break;
95554aad
TT
10723
10724 case DW_TAG_imported_unit:
10725 process_imported_unit_die (die, cu);
10726 break;
10727
71a3c369
TT
10728 case DW_TAG_variable:
10729 read_variable (die, cu);
10730 break;
10731
c906108c 10732 default:
e7c27a73 10733 new_symbol (die, NULL, cu);
c906108c
SS
10734 break;
10735 }
10736}
ca69b9e6
DE
10737\f
10738/* DWARF name computation. */
c906108c 10739
94af9270
KS
10740/* A helper function for dwarf2_compute_name which determines whether DIE
10741 needs to have the name of the scope prepended to the name listed in the
10742 die. */
10743
10744static int
10745die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10746{
1c809c68
TT
10747 struct attribute *attr;
10748
94af9270
KS
10749 switch (die->tag)
10750 {
10751 case DW_TAG_namespace:
10752 case DW_TAG_typedef:
10753 case DW_TAG_class_type:
10754 case DW_TAG_interface_type:
10755 case DW_TAG_structure_type:
10756 case DW_TAG_union_type:
10757 case DW_TAG_enumeration_type:
10758 case DW_TAG_enumerator:
10759 case DW_TAG_subprogram:
08a76f8a 10760 case DW_TAG_inlined_subroutine:
94af9270 10761 case DW_TAG_member:
74921315 10762 case DW_TAG_imported_declaration:
94af9270
KS
10763 return 1;
10764
10765 case DW_TAG_variable:
c2b0a229 10766 case DW_TAG_constant:
94af9270
KS
10767 /* We only need to prefix "globally" visible variables. These include
10768 any variable marked with DW_AT_external or any variable that
10769 lives in a namespace. [Variables in anonymous namespaces
10770 require prefixing, but they are not DW_AT_external.] */
10771
10772 if (dwarf2_attr (die, DW_AT_specification, cu))
10773 {
10774 struct dwarf2_cu *spec_cu = cu;
9a619af0 10775
94af9270
KS
10776 return die_needs_namespace (die_specification (die, &spec_cu),
10777 spec_cu);
10778 }
10779
1c809c68 10780 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10781 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10782 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10783 return 0;
10784 /* A variable in a lexical block of some kind does not need a
10785 namespace, even though in C++ such variables may be external
10786 and have a mangled name. */
10787 if (die->parent->tag == DW_TAG_lexical_block
10788 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10789 || die->parent->tag == DW_TAG_catch_block
10790 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10791 return 0;
10792 return 1;
94af9270
KS
10793
10794 default:
10795 return 0;
10796 }
10797}
10798
73b9be8b
KS
10799/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10800 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10801 defined for the given DIE. */
10802
10803static struct attribute *
10804dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10805{
10806 struct attribute *attr;
10807
10808 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10809 if (attr == NULL)
10810 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10811
10812 return attr;
10813}
10814
10815/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10816 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10817 defined for the given DIE. */
10818
10819static const char *
10820dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10821{
10822 const char *linkage_name;
10823
10824 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10825 if (linkage_name == NULL)
10826 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10827
10828 return linkage_name;
10829}
10830
94af9270 10831/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10832 compute the physname for the object, which include a method's:
9c37b5ae 10833 - formal parameters (C++),
a766d390 10834 - receiver type (Go),
a766d390
DE
10835
10836 The term "physname" is a bit confusing.
10837 For C++, for example, it is the demangled name.
10838 For Go, for example, it's the mangled name.
94af9270 10839
af6b7be1
JB
10840 For Ada, return the DIE's linkage name rather than the fully qualified
10841 name. PHYSNAME is ignored..
10842
94af9270
KS
10843 The result is allocated on the objfile_obstack and canonicalized. */
10844
10845static const char *
15d034d0
TT
10846dwarf2_compute_name (const char *name,
10847 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10848 int physname)
10849{
518817b3 10850 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10851
94af9270
KS
10852 if (name == NULL)
10853 name = dwarf2_name (die, cu);
10854
2ee7123e
DE
10855 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10856 but otherwise compute it by typename_concat inside GDB.
10857 FIXME: Actually this is not really true, or at least not always true.
10858 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
5e2db402 10859 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10860 will set the demangled name to the result of dwarf2_full_name, and it is
10861 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10862 if (cu->language == language_ada
10863 || (cu->language == language_fortran && physname))
10864 {
10865 /* For Ada unit, we prefer the linkage name over the name, as
10866 the former contains the exported name, which the user expects
10867 to be able to reference. Ideally, we want the user to be able
10868 to reference this entity using either natural or linkage name,
10869 but we haven't started looking at this enhancement yet. */
73b9be8b 10870 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10871
2ee7123e
DE
10872 if (linkage_name != NULL)
10873 return linkage_name;
f55ee35c
JK
10874 }
10875
94af9270
KS
10876 /* These are the only languages we know how to qualify names in. */
10877 if (name != NULL
9c37b5ae 10878 && (cu->language == language_cplus
c44af4eb
TT
10879 || cu->language == language_fortran || cu->language == language_d
10880 || cu->language == language_rust))
94af9270
KS
10881 {
10882 if (die_needs_namespace (die, cu))
10883 {
0d5cff50 10884 const char *prefix;
34a68019 10885 const char *canonical_name = NULL;
94af9270 10886
d7e74731
PA
10887 string_file buf;
10888
94af9270 10889 prefix = determine_prefix (die, cu);
94af9270
KS
10890 if (*prefix != '\0')
10891 {
f55ee35c
JK
10892 char *prefixed_name = typename_concat (NULL, prefix, name,
10893 physname, cu);
9a619af0 10894
d7e74731 10895 buf.puts (prefixed_name);
94af9270
KS
10896 xfree (prefixed_name);
10897 }
10898 else
d7e74731 10899 buf.puts (name);
94af9270 10900
98bfdba5
PA
10901 /* Template parameters may be specified in the DIE's DW_AT_name, or
10902 as children with DW_TAG_template_type_param or
10903 DW_TAG_value_type_param. If the latter, add them to the name
10904 here. If the name already has template parameters, then
10905 skip this step; some versions of GCC emit both, and
10906 it is more efficient to use the pre-computed name.
10907
10908 Something to keep in mind about this process: it is very
10909 unlikely, or in some cases downright impossible, to produce
10910 something that will match the mangled name of a function.
10911 If the definition of the function has the same debug info,
10912 we should be able to match up with it anyway. But fallbacks
10913 using the minimal symbol, for instance to find a method
10914 implemented in a stripped copy of libstdc++, will not work.
10915 If we do not have debug info for the definition, we will have to
10916 match them up some other way.
10917
10918 When we do name matching there is a related problem with function
10919 templates; two instantiated function templates are allowed to
10920 differ only by their return types, which we do not add here. */
10921
10922 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10923 {
10924 struct attribute *attr;
10925 struct die_info *child;
10926 int first = 1;
10927
10928 die->building_fullname = 1;
10929
10930 for (child = die->child; child != NULL; child = child->sibling)
10931 {
10932 struct type *type;
12df843f 10933 LONGEST value;
d521ce57 10934 const gdb_byte *bytes;
98bfdba5
PA
10935 struct dwarf2_locexpr_baton *baton;
10936 struct value *v;
10937
10938 if (child->tag != DW_TAG_template_type_param
10939 && child->tag != DW_TAG_template_value_param)
10940 continue;
10941
10942 if (first)
10943 {
d7e74731 10944 buf.puts ("<");
98bfdba5
PA
10945 first = 0;
10946 }
10947 else
d7e74731 10948 buf.puts (", ");
98bfdba5
PA
10949
10950 attr = dwarf2_attr (child, DW_AT_type, cu);
10951 if (attr == NULL)
10952 {
10953 complaint (&symfile_complaints,
10954 _("template parameter missing DW_AT_type"));
d7e74731 10955 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10956 continue;
10957 }
10958 type = die_type (child, cu);
10959
10960 if (child->tag == DW_TAG_template_type_param)
10961 {
d7e74731 10962 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
10963 continue;
10964 }
10965
10966 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10967 if (attr == NULL)
10968 {
10969 complaint (&symfile_complaints,
3e43a32a
MS
10970 _("template parameter missing "
10971 "DW_AT_const_value"));
d7e74731 10972 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10973 continue;
10974 }
10975
10976 dwarf2_const_value_attr (attr, type, name,
10977 &cu->comp_unit_obstack, cu,
10978 &value, &bytes, &baton);
10979
10980 if (TYPE_NOSIGN (type))
10981 /* GDB prints characters as NUMBER 'CHAR'. If that's
10982 changed, this can use value_print instead. */
d7e74731 10983 c_printchar (value, type, &buf);
98bfdba5
PA
10984 else
10985 {
10986 struct value_print_options opts;
10987
10988 if (baton != NULL)
10989 v = dwarf2_evaluate_loc_desc (type, NULL,
10990 baton->data,
10991 baton->size,
10992 baton->per_cu);
10993 else if (bytes != NULL)
10994 {
10995 v = allocate_value (type);
10996 memcpy (value_contents_writeable (v), bytes,
10997 TYPE_LENGTH (type));
10998 }
10999 else
11000 v = value_from_longest (type, value);
11001
3e43a32a
MS
11002 /* Specify decimal so that we do not depend on
11003 the radix. */
98bfdba5
PA
11004 get_formatted_print_options (&opts, 'd');
11005 opts.raw = 1;
d7e74731 11006 value_print (v, &buf, &opts);
98bfdba5
PA
11007 release_value (v);
11008 value_free (v);
11009 }
11010 }
11011
11012 die->building_fullname = 0;
11013
11014 if (!first)
11015 {
11016 /* Close the argument list, with a space if necessary
11017 (nested templates). */
d7e74731
PA
11018 if (!buf.empty () && buf.string ().back () == '>')
11019 buf.puts (" >");
98bfdba5 11020 else
d7e74731 11021 buf.puts (">");
98bfdba5
PA
11022 }
11023 }
11024
9c37b5ae 11025 /* For C++ methods, append formal parameter type
94af9270 11026 information, if PHYSNAME. */
6e70227d 11027
94af9270 11028 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 11029 && cu->language == language_cplus)
94af9270
KS
11030 {
11031 struct type *type = read_type_die (die, cu);
11032
d7e74731 11033 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 11034 &type_print_raw_options);
94af9270 11035
9c37b5ae 11036 if (cu->language == language_cplus)
94af9270 11037 {
60430eff
DJ
11038 /* Assume that an artificial first parameter is
11039 "this", but do not crash if it is not. RealView
11040 marks unnamed (and thus unused) parameters as
11041 artificial; there is no way to differentiate
11042 the two cases. */
94af9270
KS
11043 if (TYPE_NFIELDS (type) > 0
11044 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 11045 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
11046 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11047 0))))
d7e74731 11048 buf.puts (" const");
94af9270
KS
11049 }
11050 }
11051
d7e74731 11052 const std::string &intermediate_name = buf.string ();
94af9270
KS
11053
11054 if (cu->language == language_cplus)
34a68019 11055 canonical_name
322a8516 11056 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
11057 &objfile->per_bfd->storage_obstack);
11058
11059 /* If we only computed INTERMEDIATE_NAME, or if
11060 INTERMEDIATE_NAME is already canonical, then we need to
11061 copy it to the appropriate obstack. */
322a8516 11062 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
11063 name = ((const char *)
11064 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
11065 intermediate_name.c_str (),
11066 intermediate_name.length ()));
34a68019
TT
11067 else
11068 name = canonical_name;
94af9270
KS
11069 }
11070 }
11071
11072 return name;
11073}
11074
0114d602
DJ
11075/* Return the fully qualified name of DIE, based on its DW_AT_name.
11076 If scope qualifiers are appropriate they will be added. The result
34a68019 11077 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
11078 not have a name. NAME may either be from a previous call to
11079 dwarf2_name or NULL.
11080
9c37b5ae 11081 The output string will be canonicalized (if C++). */
0114d602
DJ
11082
11083static const char *
15d034d0 11084dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 11085{
94af9270
KS
11086 return dwarf2_compute_name (name, die, cu, 0);
11087}
0114d602 11088
94af9270
KS
11089/* Construct a physname for the given DIE in CU. NAME may either be
11090 from a previous call to dwarf2_name or NULL. The result will be
11091 allocated on the objfile_objstack or NULL if the DIE does not have a
11092 name.
0114d602 11093
9c37b5ae 11094 The output string will be canonicalized (if C++). */
0114d602 11095
94af9270 11096static const char *
15d034d0 11097dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 11098{
518817b3 11099 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 11100 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
11101 int need_copy = 1;
11102
11103 /* In this case dwarf2_compute_name is just a shortcut not building anything
11104 on its own. */
11105 if (!die_needs_namespace (die, cu))
11106 return dwarf2_compute_name (name, die, cu, 1);
11107
73b9be8b 11108 mangled = dw2_linkage_name (die, cu);
900e11f9 11109
e98c9e7c
TT
11110 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11111 See https://github.com/rust-lang/rust/issues/32925. */
11112 if (cu->language == language_rust && mangled != NULL
11113 && strchr (mangled, '{') != NULL)
11114 mangled = NULL;
11115
900e11f9
JK
11116 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11117 has computed. */
791afaa2 11118 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 11119 if (mangled != NULL)
900e11f9 11120 {
900e11f9
JK
11121 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
11122 type. It is easier for GDB users to search for such functions as
11123 `name(params)' than `long name(params)'. In such case the minimal
11124 symbol names do not match the full symbol names but for template
11125 functions there is never a need to look up their definition from their
11126 declaration so the only disadvantage remains the minimal symbol
11127 variant `long name(params)' does not have the proper inferior type.
11128 */
11129
a766d390
DE
11130 if (cu->language == language_go)
11131 {
5e2db402
TT
11132 /* This is a lie, but we already lie to the caller new_symbol.
11133 new_symbol assumes we return the mangled name.
a766d390 11134 This just undoes that lie until things are cleaned up. */
a766d390
DE
11135 }
11136 else
11137 {
791afaa2
TT
11138 demangled.reset (gdb_demangle (mangled,
11139 (DMGL_PARAMS | DMGL_ANSI
11140 | DMGL_RET_DROP)));
a766d390 11141 }
900e11f9 11142 if (demangled)
791afaa2 11143 canon = demangled.get ();
900e11f9
JK
11144 else
11145 {
11146 canon = mangled;
11147 need_copy = 0;
11148 }
11149 }
11150
11151 if (canon == NULL || check_physname)
11152 {
11153 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11154
11155 if (canon != NULL && strcmp (physname, canon) != 0)
11156 {
11157 /* It may not mean a bug in GDB. The compiler could also
11158 compute DW_AT_linkage_name incorrectly. But in such case
11159 GDB would need to be bug-to-bug compatible. */
11160
11161 complaint (&symfile_complaints,
11162 _("Computed physname <%s> does not match demangled <%s> "
11163 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
9c541725 11164 physname, canon, mangled, to_underlying (die->sect_off),
4262abfb 11165 objfile_name (objfile));
900e11f9
JK
11166
11167 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11168 is available here - over computed PHYSNAME. It is safer
11169 against both buggy GDB and buggy compilers. */
11170
11171 retval = canon;
11172 }
11173 else
11174 {
11175 retval = physname;
11176 need_copy = 0;
11177 }
11178 }
11179 else
11180 retval = canon;
11181
11182 if (need_copy)
224c3ddb
SM
11183 retval = ((const char *)
11184 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11185 retval, strlen (retval)));
900e11f9 11186
900e11f9 11187 return retval;
0114d602
DJ
11188}
11189
74921315
KS
11190/* Inspect DIE in CU for a namespace alias. If one exists, record
11191 a new symbol for it.
11192
11193 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11194
11195static int
11196read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11197{
11198 struct attribute *attr;
11199
11200 /* If the die does not have a name, this is not a namespace
11201 alias. */
11202 attr = dwarf2_attr (die, DW_AT_name, cu);
11203 if (attr != NULL)
11204 {
11205 int num;
11206 struct die_info *d = die;
11207 struct dwarf2_cu *imported_cu = cu;
11208
11209 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11210 keep inspecting DIEs until we hit the underlying import. */
11211#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11212 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11213 {
11214 attr = dwarf2_attr (d, DW_AT_import, cu);
11215 if (attr == NULL)
11216 break;
11217
11218 d = follow_die_ref (d, attr, &imported_cu);
11219 if (d->tag != DW_TAG_imported_declaration)
11220 break;
11221 }
11222
11223 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11224 {
11225 complaint (&symfile_complaints,
11226 _("DIE at 0x%x has too many recursively imported "
9c541725 11227 "declarations"), to_underlying (d->sect_off));
74921315
KS
11228 return 0;
11229 }
11230
11231 if (attr != NULL)
11232 {
11233 struct type *type;
9c541725 11234 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11235
9c541725 11236 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11237 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11238 {
11239 /* This declaration is a global namespace alias. Add
11240 a symbol for it whose type is the aliased namespace. */
11241 new_symbol (die, type, cu);
11242 return 1;
11243 }
11244 }
11245 }
11246
11247 return 0;
11248}
11249
22cee43f
PMR
11250/* Return the using directives repository (global or local?) to use in the
11251 current context for LANGUAGE.
11252
11253 For Ada, imported declarations can materialize renamings, which *may* be
11254 global. However it is impossible (for now?) in DWARF to distinguish
11255 "external" imported declarations and "static" ones. As all imported
11256 declarations seem to be static in all other languages, make them all CU-wide
11257 global only in Ada. */
11258
11259static struct using_direct **
11260using_directives (enum language language)
11261{
11262 if (language == language_ada && context_stack_depth == 0)
11263 return &global_using_directives;
11264 else
11265 return &local_using_directives;
11266}
11267
27aa8d6a
SW
11268/* Read the import statement specified by the given die and record it. */
11269
11270static void
11271read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11272{
518817b3 11273 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 11274 struct attribute *import_attr;
32019081 11275 struct die_info *imported_die, *child_die;
de4affc9 11276 struct dwarf2_cu *imported_cu;
27aa8d6a 11277 const char *imported_name;
794684b6 11278 const char *imported_name_prefix;
13387711
SW
11279 const char *canonical_name;
11280 const char *import_alias;
11281 const char *imported_declaration = NULL;
794684b6 11282 const char *import_prefix;
eb1e02fd 11283 std::vector<const char *> excludes;
13387711 11284
27aa8d6a
SW
11285 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11286 if (import_attr == NULL)
11287 {
11288 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11289 dwarf_tag_name (die->tag));
11290 return;
11291 }
11292
de4affc9
CC
11293 imported_cu = cu;
11294 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11295 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11296 if (imported_name == NULL)
11297 {
11298 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11299
11300 The import in the following code:
11301 namespace A
11302 {
11303 typedef int B;
11304 }
11305
11306 int main ()
11307 {
11308 using A::B;
11309 B b;
11310 return b;
11311 }
11312
11313 ...
11314 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11315 <52> DW_AT_decl_file : 1
11316 <53> DW_AT_decl_line : 6
11317 <54> DW_AT_import : <0x75>
11318 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11319 <59> DW_AT_name : B
11320 <5b> DW_AT_decl_file : 1
11321 <5c> DW_AT_decl_line : 2
11322 <5d> DW_AT_type : <0x6e>
11323 ...
11324 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11325 <76> DW_AT_byte_size : 4
11326 <77> DW_AT_encoding : 5 (signed)
11327
11328 imports the wrong die ( 0x75 instead of 0x58 ).
11329 This case will be ignored until the gcc bug is fixed. */
11330 return;
11331 }
11332
82856980
SW
11333 /* Figure out the local name after import. */
11334 import_alias = dwarf2_name (die, cu);
27aa8d6a 11335
794684b6
SW
11336 /* Figure out where the statement is being imported to. */
11337 import_prefix = determine_prefix (die, cu);
11338
11339 /* Figure out what the scope of the imported die is and prepend it
11340 to the name of the imported die. */
de4affc9 11341 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11342
f55ee35c
JK
11343 if (imported_die->tag != DW_TAG_namespace
11344 && imported_die->tag != DW_TAG_module)
794684b6 11345 {
13387711
SW
11346 imported_declaration = imported_name;
11347 canonical_name = imported_name_prefix;
794684b6 11348 }
13387711 11349 else if (strlen (imported_name_prefix) > 0)
12aaed36 11350 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11351 imported_name_prefix,
11352 (cu->language == language_d ? "." : "::"),
11353 imported_name, (char *) NULL);
13387711
SW
11354 else
11355 canonical_name = imported_name;
794684b6 11356
32019081
JK
11357 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11358 for (child_die = die->child; child_die && child_die->tag;
11359 child_die = sibling_die (child_die))
11360 {
11361 /* DWARF-4: A Fortran use statement with a “rename list” may be
11362 represented by an imported module entry with an import attribute
11363 referring to the module and owned entries corresponding to those
11364 entities that are renamed as part of being imported. */
11365
11366 if (child_die->tag != DW_TAG_imported_declaration)
11367 {
11368 complaint (&symfile_complaints,
11369 _("child DW_TAG_imported_declaration expected "
11370 "- DIE at 0x%x [in module %s]"),
9c541725 11371 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
11372 continue;
11373 }
11374
11375 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11376 if (import_attr == NULL)
11377 {
11378 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11379 dwarf_tag_name (child_die->tag));
11380 continue;
11381 }
11382
11383 imported_cu = cu;
11384 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11385 &imported_cu);
11386 imported_name = dwarf2_name (imported_die, imported_cu);
11387 if (imported_name == NULL)
11388 {
11389 complaint (&symfile_complaints,
11390 _("child DW_TAG_imported_declaration has unknown "
11391 "imported name - DIE at 0x%x [in module %s]"),
9c541725 11392 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
11393 continue;
11394 }
11395
eb1e02fd 11396 excludes.push_back (imported_name);
32019081
JK
11397
11398 process_die (child_die, cu);
11399 }
11400
22cee43f
PMR
11401 add_using_directive (using_directives (cu->language),
11402 import_prefix,
11403 canonical_name,
11404 import_alias,
11405 imported_declaration,
11406 excludes,
11407 0,
11408 &objfile->objfile_obstack);
27aa8d6a
SW
11409}
11410
5230b05a
WT
11411/* ICC<14 does not output the required DW_AT_declaration on incomplete
11412 types, but gives them a size of zero. Starting with version 14,
11413 ICC is compatible with GCC. */
11414
11415static int
11416producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11417{
11418 if (!cu->checked_producer)
11419 check_producer (cu);
11420
11421 return cu->producer_is_icc_lt_14;
11422}
11423
1b80a9fa
JK
11424/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11425 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11426 this, it was first present in GCC release 4.3.0. */
11427
11428static int
11429producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11430{
11431 if (!cu->checked_producer)
11432 check_producer (cu);
11433
11434 return cu->producer_is_gcc_lt_4_3;
11435}
11436
d721ba37
PA
11437static file_and_directory
11438find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11439{
d721ba37
PA
11440 file_and_directory res;
11441
9291a0cd
TT
11442 /* Find the filename. Do not use dwarf2_name here, since the filename
11443 is not a source language identifier. */
d721ba37
PA
11444 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11445 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11446
d721ba37
PA
11447 if (res.comp_dir == NULL
11448 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11449 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11450 {
d721ba37
PA
11451 res.comp_dir_storage = ldirname (res.name);
11452 if (!res.comp_dir_storage.empty ())
11453 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11454 }
d721ba37 11455 if (res.comp_dir != NULL)
9291a0cd
TT
11456 {
11457 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11458 directory, get rid of it. */
d721ba37 11459 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11460
d721ba37
PA
11461 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11462 res.comp_dir = cp + 1;
9291a0cd
TT
11463 }
11464
d721ba37
PA
11465 if (res.name == NULL)
11466 res.name = "<unknown>";
11467
11468 return res;
9291a0cd
TT
11469}
11470
f4dc4d17
DE
11471/* Handle DW_AT_stmt_list for a compilation unit.
11472 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11473 COMP_DIR is the compilation directory. LOWPC is passed to
11474 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11475
11476static void
11477handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11478 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11479{
518817b3
SM
11480 struct dwarf2_per_objfile *dwarf2_per_objfile
11481 = cu->per_cu->dwarf2_per_objfile;
527f3840 11482 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11483 struct attribute *attr;
527f3840
JK
11484 struct line_header line_header_local;
11485 hashval_t line_header_local_hash;
527f3840
JK
11486 void **slot;
11487 int decode_mapping;
2ab95328 11488
f4dc4d17
DE
11489 gdb_assert (! cu->per_cu->is_debug_types);
11490
2ab95328 11491 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11492 if (attr == NULL)
11493 return;
11494
9c541725 11495 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11496
11497 /* The line header hash table is only created if needed (it exists to
11498 prevent redundant reading of the line table for partial_units).
11499 If we're given a partial_unit, we'll need it. If we're given a
11500 compile_unit, then use the line header hash table if it's already
11501 created, but don't create one just yet. */
11502
11503 if (dwarf2_per_objfile->line_header_hash == NULL
11504 && die->tag == DW_TAG_partial_unit)
2ab95328 11505 {
527f3840
JK
11506 dwarf2_per_objfile->line_header_hash
11507 = htab_create_alloc_ex (127, line_header_hash_voidp,
11508 line_header_eq_voidp,
11509 free_line_header_voidp,
11510 &objfile->objfile_obstack,
11511 hashtab_obstack_allocate,
11512 dummy_obstack_deallocate);
11513 }
2ab95328 11514
9c541725 11515 line_header_local.sect_off = line_offset;
527f3840
JK
11516 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11517 line_header_local_hash = line_header_hash (&line_header_local);
11518 if (dwarf2_per_objfile->line_header_hash != NULL)
11519 {
11520 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11521 &line_header_local,
11522 line_header_local_hash, NO_INSERT);
11523
11524 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11525 is not present in *SLOT (since if there is something in *SLOT then
11526 it will be for a partial_unit). */
11527 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11528 {
527f3840 11529 gdb_assert (*slot != NULL);
9a3c8263 11530 cu->line_header = (struct line_header *) *slot;
527f3840 11531 return;
dee91e82 11532 }
2ab95328 11533 }
527f3840
JK
11534
11535 /* dwarf_decode_line_header does not yet provide sufficient information.
11536 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11537 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11538 if (lh == NULL)
527f3840 11539 return;
4c8aa72d
PA
11540
11541 cu->line_header = lh.release ();
11542 cu->line_header_die_owner = die;
527f3840
JK
11543
11544 if (dwarf2_per_objfile->line_header_hash == NULL)
11545 slot = NULL;
11546 else
11547 {
11548 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11549 &line_header_local,
11550 line_header_local_hash, INSERT);
11551 gdb_assert (slot != NULL);
11552 }
11553 if (slot != NULL && *slot == NULL)
11554 {
11555 /* This newly decoded line number information unit will be owned
11556 by line_header_hash hash table. */
11557 *slot = cu->line_header;
4c8aa72d 11558 cu->line_header_die_owner = NULL;
527f3840
JK
11559 }
11560 else
11561 {
11562 /* We cannot free any current entry in (*slot) as that struct line_header
11563 may be already used by multiple CUs. Create only temporary decoded
11564 line_header for this CU - it may happen at most once for each line
11565 number information unit. And if we're not using line_header_hash
11566 then this is what we want as well. */
11567 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11568 }
11569 decode_mapping = (die->tag != DW_TAG_partial_unit);
11570 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11571 decode_mapping);
fff8551c 11572
2ab95328
TT
11573}
11574
95554aad 11575/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11576
c906108c 11577static void
e7c27a73 11578read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11579{
518817b3
SM
11580 struct dwarf2_per_objfile *dwarf2_per_objfile
11581 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11582 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11583 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11584 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11585 CORE_ADDR highpc = ((CORE_ADDR) 0);
11586 struct attribute *attr;
c906108c 11587 struct die_info *child_die;
e142c38c 11588 CORE_ADDR baseaddr;
6e70227d 11589
e142c38c 11590 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11591
fae299cd 11592 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11593
11594 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11595 from finish_block. */
2acceee2 11596 if (lowpc == ((CORE_ADDR) -1))
c906108c 11597 lowpc = highpc;
3e29f34a 11598 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11599
d721ba37 11600 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11601
95554aad 11602 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 11603
f4b8a18d
KW
11604 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11605 standardised yet. As a workaround for the language detection we fall
11606 back to the DW_AT_producer string. */
11607 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11608 cu->language = language_opencl;
11609
3019eac3
DE
11610 /* Similar hack for Go. */
11611 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11612 set_cu_language (DW_LANG_Go, cu);
11613
d721ba37 11614 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11615
11616 /* Decode line number information if present. We do this before
11617 processing child DIEs, so that the line header table is available
11618 for DW_AT_decl_file. */
d721ba37 11619 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11620
11621 /* Process all dies in compilation unit. */
11622 if (die->child != NULL)
11623 {
11624 child_die = die->child;
11625 while (child_die && child_die->tag)
11626 {
11627 process_die (child_die, cu);
11628 child_die = sibling_die (child_die);
11629 }
11630 }
11631
11632 /* Decode macro information, if present. Dwarf 2 macro information
11633 refers to information in the line number info statement program
11634 header, so we can only read it if we've read the header
11635 successfully. */
0af92d60
JK
11636 attr = dwarf2_attr (die, DW_AT_macros, cu);
11637 if (attr == NULL)
11638 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11639 if (attr && cu->line_header)
11640 {
11641 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11642 complaint (&symfile_complaints,
0af92d60 11643 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11644
43f3e411 11645 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11646 }
11647 else
11648 {
11649 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11650 if (attr && cu->line_header)
11651 {
11652 unsigned int macro_offset = DW_UNSND (attr);
11653
43f3e411 11654 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11655 }
11656 }
3019eac3
DE
11657}
11658
f4dc4d17
DE
11659/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11660 Create the set of symtabs used by this TU, or if this TU is sharing
11661 symtabs with another TU and the symtabs have already been created
11662 then restore those symtabs in the line header.
11663 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
11664
11665static void
f4dc4d17 11666setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 11667{
f4dc4d17
DE
11668 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11669 struct type_unit_group *tu_group;
11670 int first_time;
3019eac3 11671 struct attribute *attr;
9c541725 11672 unsigned int i;
0186c6a7 11673 struct signatured_type *sig_type;
3019eac3 11674
f4dc4d17 11675 gdb_assert (per_cu->is_debug_types);
0186c6a7 11676 sig_type = (struct signatured_type *) per_cu;
3019eac3 11677
f4dc4d17 11678 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 11679
f4dc4d17 11680 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11681 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
11682 if (sig_type->type_unit_group == NULL)
11683 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11684 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11685
11686 /* If we've already processed this stmt_list there's no real need to
11687 do it again, we could fake it and just recreate the part we need
11688 (file name,index -> symtab mapping). If data shows this optimization
11689 is useful we can do it then. */
43f3e411 11690 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11691
11692 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11693 debug info. */
fff8551c 11694 line_header_up lh;
f4dc4d17 11695 if (attr != NULL)
3019eac3 11696 {
9c541725 11697 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
f4dc4d17
DE
11698 lh = dwarf_decode_line_header (line_offset, cu);
11699 }
11700 if (lh == NULL)
11701 {
11702 if (first_time)
11703 dwarf2_start_symtab (cu, "", NULL, 0);
11704 else
11705 {
11706 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 11707 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 11708 }
f4dc4d17 11709 return;
3019eac3
DE
11710 }
11711
4c8aa72d
PA
11712 cu->line_header = lh.release ();
11713 cu->line_header_die_owner = die;
3019eac3 11714
f4dc4d17
DE
11715 if (first_time)
11716 {
43f3e411 11717 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 11718
1fd60fc0
DE
11719 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11720 still initializing it, and our caller (a few levels up)
11721 process_full_type_unit still needs to know if this is the first
11722 time. */
11723
4c8aa72d
PA
11724 tu_group->num_symtabs = cu->line_header->file_names.size ();
11725 tu_group->symtabs = XNEWVEC (struct symtab *,
11726 cu->line_header->file_names.size ());
3019eac3 11727
4c8aa72d 11728 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 11729 {
4c8aa72d 11730 file_entry &fe = cu->line_header->file_names[i];
3019eac3 11731
4c8aa72d 11732 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
3019eac3 11733
f4dc4d17
DE
11734 if (current_subfile->symtab == NULL)
11735 {
4c8aa72d
PA
11736 /* NOTE: start_subfile will recognize when it's been
11737 passed a file it has already seen. So we can't
11738 assume there's a simple mapping from
11739 cu->line_header->file_names to subfiles, plus
11740 cu->line_header->file_names may contain dups. */
43f3e411
DE
11741 current_subfile->symtab
11742 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
11743 }
11744
8c43009f
PA
11745 fe.symtab = current_subfile->symtab;
11746 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11747 }
11748 }
11749 else
3019eac3 11750 {
0ab9ce85 11751 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 11752
4c8aa72d 11753 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 11754 {
4c8aa72d 11755 file_entry &fe = cu->line_header->file_names[i];
f4dc4d17 11756
4c8aa72d 11757 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11758 }
3019eac3
DE
11759 }
11760
f4dc4d17
DE
11761 /* The main symtab is allocated last. Type units don't have DW_AT_name
11762 so they don't have a "real" (so to speak) symtab anyway.
11763 There is later code that will assign the main symtab to all symbols
11764 that don't have one. We need to handle the case of a symbol with a
11765 missing symtab (DW_AT_decl_file) anyway. */
11766}
3019eac3 11767
f4dc4d17
DE
11768/* Process DW_TAG_type_unit.
11769 For TUs we want to skip the first top level sibling if it's not the
11770 actual type being defined by this TU. In this case the first top
11771 level sibling is there to provide context only. */
3019eac3 11772
f4dc4d17
DE
11773static void
11774read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11775{
11776 struct die_info *child_die;
3019eac3 11777
f4dc4d17
DE
11778 prepare_one_comp_unit (cu, die, language_minimal);
11779
11780 /* Initialize (or reinitialize) the machinery for building symtabs.
11781 We do this before processing child DIEs, so that the line header table
11782 is available for DW_AT_decl_file. */
11783 setup_type_unit_groups (die, cu);
11784
11785 if (die->child != NULL)
11786 {
11787 child_die = die->child;
11788 while (child_die && child_die->tag)
11789 {
11790 process_die (child_die, cu);
11791 child_die = sibling_die (child_die);
11792 }
11793 }
3019eac3
DE
11794}
11795\f
80626a55
DE
11796/* DWO/DWP files.
11797
11798 http://gcc.gnu.org/wiki/DebugFission
11799 http://gcc.gnu.org/wiki/DebugFissionDWP
11800
11801 To simplify handling of both DWO files ("object" files with the DWARF info)
11802 and DWP files (a file with the DWOs packaged up into one file), we treat
11803 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11804
11805static hashval_t
11806hash_dwo_file (const void *item)
11807{
9a3c8263 11808 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11809 hashval_t hash;
3019eac3 11810
a2ce51a0
DE
11811 hash = htab_hash_string (dwo_file->dwo_name);
11812 if (dwo_file->comp_dir != NULL)
11813 hash += htab_hash_string (dwo_file->comp_dir);
11814 return hash;
3019eac3
DE
11815}
11816
11817static int
11818eq_dwo_file (const void *item_lhs, const void *item_rhs)
11819{
9a3c8263
SM
11820 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11821 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11822
a2ce51a0
DE
11823 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11824 return 0;
11825 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11826 return lhs->comp_dir == rhs->comp_dir;
11827 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11828}
11829
11830/* Allocate a hash table for DWO files. */
11831
11832static htab_t
ed2dc618 11833allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 11834{
3019eac3
DE
11835 return htab_create_alloc_ex (41,
11836 hash_dwo_file,
11837 eq_dwo_file,
11838 NULL,
11839 &objfile->objfile_obstack,
11840 hashtab_obstack_allocate,
11841 dummy_obstack_deallocate);
11842}
11843
80626a55
DE
11844/* Lookup DWO file DWO_NAME. */
11845
11846static void **
ed2dc618
SM
11847lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11848 const char *dwo_name,
11849 const char *comp_dir)
80626a55
DE
11850{
11851 struct dwo_file find_entry;
11852 void **slot;
11853
11854 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11855 dwarf2_per_objfile->dwo_files
11856 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55
DE
11857
11858 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
11859 find_entry.dwo_name = dwo_name;
11860 find_entry.comp_dir = comp_dir;
80626a55
DE
11861 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11862
11863 return slot;
11864}
11865
3019eac3
DE
11866static hashval_t
11867hash_dwo_unit (const void *item)
11868{
9a3c8263 11869 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11870
11871 /* This drops the top 32 bits of the id, but is ok for a hash. */
11872 return dwo_unit->signature;
11873}
11874
11875static int
11876eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11877{
9a3c8263
SM
11878 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11879 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11880
11881 /* The signature is assumed to be unique within the DWO file.
11882 So while object file CU dwo_id's always have the value zero,
11883 that's OK, assuming each object file DWO file has only one CU,
11884 and that's the rule for now. */
11885 return lhs->signature == rhs->signature;
11886}
11887
11888/* Allocate a hash table for DWO CUs,TUs.
11889 There is one of these tables for each of CUs,TUs for each DWO file. */
11890
11891static htab_t
11892allocate_dwo_unit_table (struct objfile *objfile)
11893{
11894 /* Start out with a pretty small number.
11895 Generally DWO files contain only one CU and maybe some TUs. */
11896 return htab_create_alloc_ex (3,
11897 hash_dwo_unit,
11898 eq_dwo_unit,
11899 NULL,
11900 &objfile->objfile_obstack,
11901 hashtab_obstack_allocate,
11902 dummy_obstack_deallocate);
11903}
11904
80626a55 11905/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 11906
19c3d4c9 11907struct create_dwo_cu_data
3019eac3
DE
11908{
11909 struct dwo_file *dwo_file;
19c3d4c9 11910 struct dwo_unit dwo_unit;
3019eac3
DE
11911};
11912
19c3d4c9 11913/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11914
11915static void
19c3d4c9
DE
11916create_dwo_cu_reader (const struct die_reader_specs *reader,
11917 const gdb_byte *info_ptr,
11918 struct die_info *comp_unit_die,
11919 int has_children,
11920 void *datap)
3019eac3
DE
11921{
11922 struct dwarf2_cu *cu = reader->cu;
9c541725 11923 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11924 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 11925 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 11926 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 11927 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 11928 struct attribute *attr;
3019eac3
DE
11929
11930 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11931 if (attr == NULL)
11932 {
19c3d4c9
DE
11933 complaint (&symfile_complaints,
11934 _("Dwarf Error: debug entry at offset 0x%x is missing"
11935 " its dwo_id [in module %s]"),
9c541725 11936 to_underlying (sect_off), dwo_file->dwo_name);
3019eac3
DE
11937 return;
11938 }
11939
3019eac3
DE
11940 dwo_unit->dwo_file = dwo_file;
11941 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 11942 dwo_unit->section = section;
9c541725 11943 dwo_unit->sect_off = sect_off;
3019eac3
DE
11944 dwo_unit->length = cu->per_cu->length;
11945
b4f54984 11946 if (dwarf_read_debug)
4031ecc5 11947 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9c541725
PA
11948 to_underlying (sect_off),
11949 hex_string (dwo_unit->signature));
3019eac3
DE
11950}
11951
33c5cd75 11952/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11953 Note: This function processes DWO files only, not DWP files. */
3019eac3 11954
33c5cd75 11955static void
ed2dc618
SM
11956create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11957 struct dwo_file &dwo_file, dwarf2_section_info &section,
33c5cd75 11958 htab_t &cus_htab)
3019eac3
DE
11959{
11960 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11961 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11962
33c5cd75
DB
11963 dwarf2_read_section (objfile, &section);
11964 info_ptr = section.buffer;
3019eac3
DE
11965
11966 if (info_ptr == NULL)
33c5cd75 11967 return;
3019eac3 11968
b4f54984 11969 if (dwarf_read_debug)
19c3d4c9
DE
11970 {
11971 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
11972 get_section_name (&section),
11973 get_section_file_name (&section));
19c3d4c9 11974 }
3019eac3 11975
33c5cd75 11976 end_ptr = info_ptr + section.size;
3019eac3
DE
11977 while (info_ptr < end_ptr)
11978 {
11979 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
11980 struct create_dwo_cu_data create_dwo_cu_data;
11981 struct dwo_unit *dwo_unit;
11982 void **slot;
11983 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 11984
19c3d4c9
DE
11985 memset (&create_dwo_cu_data.dwo_unit, 0,
11986 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3 11987 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11988 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11989 per_cu.is_debug_types = 0;
33c5cd75
DB
11990 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11991 per_cu.section = &section;
c5ed0576 11992 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
11993
11994 init_cutu_and_read_dies_no_follow (
11995 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11996 info_ptr += per_cu.length;
11997
11998 // If the unit could not be parsed, skip it.
11999 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12000 continue;
3019eac3 12001
33c5cd75
DB
12002 if (cus_htab == NULL)
12003 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 12004
33c5cd75
DB
12005 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12006 *dwo_unit = create_dwo_cu_data.dwo_unit;
12007 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12008 gdb_assert (slot != NULL);
12009 if (*slot != NULL)
19c3d4c9 12010 {
33c5cd75
DB
12011 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12012 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 12013
33c5cd75
DB
12014 complaint (&symfile_complaints,
12015 _("debug cu entry at offset 0x%x is duplicate to"
12016 " the entry at offset 0x%x, signature %s"),
12017 to_underlying (sect_off), to_underlying (dup_sect_off),
12018 hex_string (dwo_unit->signature));
19c3d4c9 12019 }
33c5cd75 12020 *slot = (void *)dwo_unit;
3019eac3 12021 }
3019eac3
DE
12022}
12023
80626a55
DE
12024/* DWP file .debug_{cu,tu}_index section format:
12025 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12026
d2415c6c
DE
12027 DWP Version 1:
12028
80626a55
DE
12029 Both index sections have the same format, and serve to map a 64-bit
12030 signature to a set of section numbers. Each section begins with a header,
12031 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12032 indexes, and a pool of 32-bit section numbers. The index sections will be
12033 aligned at 8-byte boundaries in the file.
12034
d2415c6c
DE
12035 The index section header consists of:
12036
12037 V, 32 bit version number
12038 -, 32 bits unused
12039 N, 32 bit number of compilation units or type units in the index
12040 M, 32 bit number of slots in the hash table
80626a55 12041
d2415c6c 12042 Numbers are recorded using the byte order of the application binary.
80626a55 12043
d2415c6c
DE
12044 The hash table begins at offset 16 in the section, and consists of an array
12045 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12046 order of the application binary). Unused slots in the hash table are 0.
12047 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 12048
d2415c6c
DE
12049 The parallel table begins immediately after the hash table
12050 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12051 array of 32-bit indexes (using the byte order of the application binary),
12052 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12053 table contains a 32-bit index into the pool of section numbers. For unused
12054 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 12055
73869dc2
DE
12056 The pool of section numbers begins immediately following the hash table
12057 (at offset 16 + 12 * M from the beginning of the section). The pool of
12058 section numbers consists of an array of 32-bit words (using the byte order
12059 of the application binary). Each item in the array is indexed starting
12060 from 0. The hash table entry provides the index of the first section
12061 number in the set. Additional section numbers in the set follow, and the
12062 set is terminated by a 0 entry (section number 0 is not used in ELF).
12063
12064 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12065 section must be the first entry in the set, and the .debug_abbrev.dwo must
12066 be the second entry. Other members of the set may follow in any order.
12067
12068 ---
12069
12070 DWP Version 2:
12071
12072 DWP Version 2 combines all the .debug_info, etc. sections into one,
12073 and the entries in the index tables are now offsets into these sections.
12074 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12075 section.
12076
12077 Index Section Contents:
12078 Header
12079 Hash Table of Signatures dwp_hash_table.hash_table
12080 Parallel Table of Indices dwp_hash_table.unit_table
12081 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12082 Table of Section Sizes dwp_hash_table.v2.sizes
12083
12084 The index section header consists of:
12085
12086 V, 32 bit version number
12087 L, 32 bit number of columns in the table of section offsets
12088 N, 32 bit number of compilation units or type units in the index
12089 M, 32 bit number of slots in the hash table
12090
12091 Numbers are recorded using the byte order of the application binary.
12092
12093 The hash table has the same format as version 1.
12094 The parallel table of indices has the same format as version 1,
12095 except that the entries are origin-1 indices into the table of sections
12096 offsets and the table of section sizes.
12097
12098 The table of offsets begins immediately following the parallel table
12099 (at offset 16 + 12 * M from the beginning of the section). The table is
12100 a two-dimensional array of 32-bit words (using the byte order of the
12101 application binary), with L columns and N+1 rows, in row-major order.
12102 Each row in the array is indexed starting from 0. The first row provides
12103 a key to the remaining rows: each column in this row provides an identifier
12104 for a debug section, and the offsets in the same column of subsequent rows
12105 refer to that section. The section identifiers are:
12106
12107 DW_SECT_INFO 1 .debug_info.dwo
12108 DW_SECT_TYPES 2 .debug_types.dwo
12109 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12110 DW_SECT_LINE 4 .debug_line.dwo
12111 DW_SECT_LOC 5 .debug_loc.dwo
12112 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12113 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12114 DW_SECT_MACRO 8 .debug_macro.dwo
12115
12116 The offsets provided by the CU and TU index sections are the base offsets
12117 for the contributions made by each CU or TU to the corresponding section
12118 in the package file. Each CU and TU header contains an abbrev_offset
12119 field, used to find the abbreviations table for that CU or TU within the
12120 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12121 be interpreted as relative to the base offset given in the index section.
12122 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12123 should be interpreted as relative to the base offset for .debug_line.dwo,
12124 and offsets into other debug sections obtained from DWARF attributes should
12125 also be interpreted as relative to the corresponding base offset.
12126
12127 The table of sizes begins immediately following the table of offsets.
12128 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12129 with L columns and N rows, in row-major order. Each row in the array is
12130 indexed starting from 1 (row 0 is shared by the two tables).
12131
12132 ---
12133
12134 Hash table lookup is handled the same in version 1 and 2:
12135
12136 We assume that N and M will not exceed 2^32 - 1.
12137 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12138
d2415c6c
DE
12139 Given a 64-bit compilation unit signature or a type signature S, an entry
12140 in the hash table is located as follows:
80626a55 12141
d2415c6c
DE
12142 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12143 the low-order k bits all set to 1.
80626a55 12144
d2415c6c 12145 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12146
d2415c6c
DE
12147 3) If the hash table entry at index H matches the signature, use that
12148 entry. If the hash table entry at index H is unused (all zeroes),
12149 terminate the search: the signature is not present in the table.
80626a55 12150
d2415c6c 12151 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12152
d2415c6c 12153 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12154 to stop at an unused slot or find the match. */
80626a55
DE
12155
12156/* Create a hash table to map DWO IDs to their CU/TU entry in
12157 .debug_{info,types}.dwo in DWP_FILE.
12158 Returns NULL if there isn't one.
12159 Note: This function processes DWP files only, not DWO files. */
12160
12161static struct dwp_hash_table *
ed2dc618
SM
12162create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12163 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
12164{
12165 struct objfile *objfile = dwarf2_per_objfile->objfile;
12166 bfd *dbfd = dwp_file->dbfd;
948f8e3d 12167 const gdb_byte *index_ptr, *index_end;
80626a55 12168 struct dwarf2_section_info *index;
73869dc2 12169 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12170 struct dwp_hash_table *htab;
12171
12172 if (is_debug_types)
12173 index = &dwp_file->sections.tu_index;
12174 else
12175 index = &dwp_file->sections.cu_index;
12176
12177 if (dwarf2_section_empty_p (index))
12178 return NULL;
12179 dwarf2_read_section (objfile, index);
12180
12181 index_ptr = index->buffer;
12182 index_end = index_ptr + index->size;
12183
12184 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12185 index_ptr += 4;
12186 if (version == 2)
12187 nr_columns = read_4_bytes (dbfd, index_ptr);
12188 else
12189 nr_columns = 0;
12190 index_ptr += 4;
80626a55
DE
12191 nr_units = read_4_bytes (dbfd, index_ptr);
12192 index_ptr += 4;
12193 nr_slots = read_4_bytes (dbfd, index_ptr);
12194 index_ptr += 4;
12195
73869dc2 12196 if (version != 1 && version != 2)
80626a55 12197 {
21aa081e 12198 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12199 " [in module %s]"),
21aa081e 12200 pulongest (version), dwp_file->name);
80626a55
DE
12201 }
12202 if (nr_slots != (nr_slots & -nr_slots))
12203 {
21aa081e 12204 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12205 " is not power of 2 [in module %s]"),
21aa081e 12206 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12207 }
12208
12209 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12210 htab->version = version;
12211 htab->nr_columns = nr_columns;
80626a55
DE
12212 htab->nr_units = nr_units;
12213 htab->nr_slots = nr_slots;
12214 htab->hash_table = index_ptr;
12215 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12216
12217 /* Exit early if the table is empty. */
12218 if (nr_slots == 0 || nr_units == 0
12219 || (version == 2 && nr_columns == 0))
12220 {
12221 /* All must be zero. */
12222 if (nr_slots != 0 || nr_units != 0
12223 || (version == 2 && nr_columns != 0))
12224 {
12225 complaint (&symfile_complaints,
12226 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12227 " all zero [in modules %s]"),
12228 dwp_file->name);
12229 }
12230 return htab;
12231 }
12232
12233 if (version == 1)
12234 {
12235 htab->section_pool.v1.indices =
12236 htab->unit_table + sizeof (uint32_t) * nr_slots;
12237 /* It's harder to decide whether the section is too small in v1.
12238 V1 is deprecated anyway so we punt. */
12239 }
12240 else
12241 {
12242 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12243 int *ids = htab->section_pool.v2.section_ids;
12244 /* Reverse map for error checking. */
12245 int ids_seen[DW_SECT_MAX + 1];
12246 int i;
12247
12248 if (nr_columns < 2)
12249 {
12250 error (_("Dwarf Error: bad DWP hash table, too few columns"
12251 " in section table [in module %s]"),
12252 dwp_file->name);
12253 }
12254 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12255 {
12256 error (_("Dwarf Error: bad DWP hash table, too many columns"
12257 " in section table [in module %s]"),
12258 dwp_file->name);
12259 }
12260 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12261 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12262 for (i = 0; i < nr_columns; ++i)
12263 {
12264 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12265
12266 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12267 {
12268 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12269 " in section table [in module %s]"),
12270 id, dwp_file->name);
12271 }
12272 if (ids_seen[id] != -1)
12273 {
12274 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12275 " id %d in section table [in module %s]"),
12276 id, dwp_file->name);
12277 }
12278 ids_seen[id] = i;
12279 ids[i] = id;
12280 }
12281 /* Must have exactly one info or types section. */
12282 if (((ids_seen[DW_SECT_INFO] != -1)
12283 + (ids_seen[DW_SECT_TYPES] != -1))
12284 != 1)
12285 {
12286 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12287 " DWO info/types section [in module %s]"),
12288 dwp_file->name);
12289 }
12290 /* Must have an abbrev section. */
12291 if (ids_seen[DW_SECT_ABBREV] == -1)
12292 {
12293 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12294 " section [in module %s]"),
12295 dwp_file->name);
12296 }
12297 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12298 htab->section_pool.v2.sizes =
12299 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12300 * nr_units * nr_columns);
12301 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12302 * nr_units * nr_columns))
12303 > index_end)
12304 {
12305 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12306 " [in module %s]"),
12307 dwp_file->name);
12308 }
12309 }
80626a55
DE
12310
12311 return htab;
12312}
12313
12314/* Update SECTIONS with the data from SECTP.
12315
12316 This function is like the other "locate" section routines that are
12317 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12318 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12319
12320 The result is non-zero for success, or zero if an error was found. */
12321
12322static int
73869dc2
DE
12323locate_v1_virtual_dwo_sections (asection *sectp,
12324 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12325{
12326 const struct dwop_section_names *names = &dwop_section_names;
12327
12328 if (section_is_p (sectp->name, &names->abbrev_dwo))
12329 {
12330 /* There can be only one. */
049412e3 12331 if (sections->abbrev.s.section != NULL)
80626a55 12332 return 0;
049412e3 12333 sections->abbrev.s.section = sectp;
80626a55
DE
12334 sections->abbrev.size = bfd_get_section_size (sectp);
12335 }
12336 else if (section_is_p (sectp->name, &names->info_dwo)
12337 || section_is_p (sectp->name, &names->types_dwo))
12338 {
12339 /* There can be only one. */
049412e3 12340 if (sections->info_or_types.s.section != NULL)
80626a55 12341 return 0;
049412e3 12342 sections->info_or_types.s.section = sectp;
80626a55
DE
12343 sections->info_or_types.size = bfd_get_section_size (sectp);
12344 }
12345 else if (section_is_p (sectp->name, &names->line_dwo))
12346 {
12347 /* There can be only one. */
049412e3 12348 if (sections->line.s.section != NULL)
80626a55 12349 return 0;
049412e3 12350 sections->line.s.section = sectp;
80626a55
DE
12351 sections->line.size = bfd_get_section_size (sectp);
12352 }
12353 else if (section_is_p (sectp->name, &names->loc_dwo))
12354 {
12355 /* There can be only one. */
049412e3 12356 if (sections->loc.s.section != NULL)
80626a55 12357 return 0;
049412e3 12358 sections->loc.s.section = sectp;
80626a55
DE
12359 sections->loc.size = bfd_get_section_size (sectp);
12360 }
12361 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12362 {
12363 /* There can be only one. */
049412e3 12364 if (sections->macinfo.s.section != NULL)
80626a55 12365 return 0;
049412e3 12366 sections->macinfo.s.section = sectp;
80626a55
DE
12367 sections->macinfo.size = bfd_get_section_size (sectp);
12368 }
12369 else if (section_is_p (sectp->name, &names->macro_dwo))
12370 {
12371 /* There can be only one. */
049412e3 12372 if (sections->macro.s.section != NULL)
80626a55 12373 return 0;
049412e3 12374 sections->macro.s.section = sectp;
80626a55
DE
12375 sections->macro.size = bfd_get_section_size (sectp);
12376 }
12377 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12378 {
12379 /* There can be only one. */
049412e3 12380 if (sections->str_offsets.s.section != NULL)
80626a55 12381 return 0;
049412e3 12382 sections->str_offsets.s.section = sectp;
80626a55
DE
12383 sections->str_offsets.size = bfd_get_section_size (sectp);
12384 }
12385 else
12386 {
12387 /* No other kind of section is valid. */
12388 return 0;
12389 }
12390
12391 return 1;
12392}
12393
73869dc2
DE
12394/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12395 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12396 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12397 This is for DWP version 1 files. */
80626a55
DE
12398
12399static struct dwo_unit *
ed2dc618
SM
12400create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12401 struct dwp_file *dwp_file,
73869dc2
DE
12402 uint32_t unit_index,
12403 const char *comp_dir,
12404 ULONGEST signature, int is_debug_types)
80626a55
DE
12405{
12406 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12407 const struct dwp_hash_table *dwp_htab =
12408 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
12409 bfd *dbfd = dwp_file->dbfd;
12410 const char *kind = is_debug_types ? "TU" : "CU";
12411 struct dwo_file *dwo_file;
12412 struct dwo_unit *dwo_unit;
73869dc2 12413 struct virtual_v1_dwo_sections sections;
80626a55 12414 void **dwo_file_slot;
80626a55
DE
12415 int i;
12416
73869dc2
DE
12417 gdb_assert (dwp_file->version == 1);
12418
b4f54984 12419 if (dwarf_read_debug)
80626a55 12420 {
73869dc2 12421 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12422 kind,
73869dc2 12423 pulongest (unit_index), hex_string (signature),
80626a55
DE
12424 dwp_file->name);
12425 }
12426
19ac8c2e 12427 /* Fetch the sections of this DWO unit.
80626a55
DE
12428 Put a limit on the number of sections we look for so that bad data
12429 doesn't cause us to loop forever. */
12430
73869dc2 12431#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12432 (1 /* .debug_info or .debug_types */ \
12433 + 1 /* .debug_abbrev */ \
12434 + 1 /* .debug_line */ \
12435 + 1 /* .debug_loc */ \
12436 + 1 /* .debug_str_offsets */ \
19ac8c2e 12437 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12438 + 1 /* trailing zero */)
12439
12440 memset (&sections, 0, sizeof (sections));
80626a55 12441
73869dc2 12442 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12443 {
12444 asection *sectp;
12445 uint32_t section_nr =
12446 read_4_bytes (dbfd,
73869dc2
DE
12447 dwp_htab->section_pool.v1.indices
12448 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12449
12450 if (section_nr == 0)
12451 break;
12452 if (section_nr >= dwp_file->num_sections)
12453 {
12454 error (_("Dwarf Error: bad DWP hash table, section number too large"
12455 " [in module %s]"),
12456 dwp_file->name);
12457 }
12458
12459 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12460 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12461 {
12462 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12463 " [in module %s]"),
12464 dwp_file->name);
12465 }
12466 }
12467
12468 if (i < 2
a32a8923
DE
12469 || dwarf2_section_empty_p (&sections.info_or_types)
12470 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12471 {
12472 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12473 " [in module %s]"),
12474 dwp_file->name);
12475 }
73869dc2 12476 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12477 {
12478 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12479 " [in module %s]"),
12480 dwp_file->name);
12481 }
12482
12483 /* It's easier for the rest of the code if we fake a struct dwo_file and
12484 have dwo_unit "live" in that. At least for now.
12485
12486 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12487 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12488 file, we can combine them back into a virtual DWO file to save space
12489 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12490 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12491
791afaa2
TT
12492 std::string virtual_dwo_name =
12493 string_printf ("virtual-dwo/%d-%d-%d-%d",
12494 get_section_id (&sections.abbrev),
12495 get_section_id (&sections.line),
12496 get_section_id (&sections.loc),
12497 get_section_id (&sections.str_offsets));
80626a55 12498 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12499 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12500 virtual_dwo_name.c_str (),
12501 comp_dir);
80626a55
DE
12502 /* Create one if necessary. */
12503 if (*dwo_file_slot == NULL)
12504 {
b4f54984 12505 if (dwarf_read_debug)
80626a55
DE
12506 {
12507 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12508 virtual_dwo_name.c_str ());
80626a55
DE
12509 }
12510 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12511 dwo_file->dwo_name
12512 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12513 virtual_dwo_name.c_str (),
12514 virtual_dwo_name.size ());
0ac5b59e 12515 dwo_file->comp_dir = comp_dir;
80626a55
DE
12516 dwo_file->sections.abbrev = sections.abbrev;
12517 dwo_file->sections.line = sections.line;
12518 dwo_file->sections.loc = sections.loc;
12519 dwo_file->sections.macinfo = sections.macinfo;
12520 dwo_file->sections.macro = sections.macro;
12521 dwo_file->sections.str_offsets = sections.str_offsets;
12522 /* The "str" section is global to the entire DWP file. */
12523 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12524 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12525 there's no need to record it in dwo_file.
12526 Also, we can't simply record type sections in dwo_file because
12527 we record a pointer into the vector in dwo_unit. As we collect more
12528 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12529 for it, invalidating all copies of pointers into the previous
12530 contents. */
80626a55
DE
12531 *dwo_file_slot = dwo_file;
12532 }
12533 else
12534 {
b4f54984 12535 if (dwarf_read_debug)
80626a55
DE
12536 {
12537 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12538 virtual_dwo_name.c_str ());
80626a55 12539 }
9a3c8263 12540 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12541 }
80626a55
DE
12542
12543 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12544 dwo_unit->dwo_file = dwo_file;
12545 dwo_unit->signature = signature;
8d749320
SM
12546 dwo_unit->section =
12547 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12548 *dwo_unit->section = sections.info_or_types;
57d63ce2 12549 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12550
12551 return dwo_unit;
12552}
12553
73869dc2
DE
12554/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12555 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12556 piece within that section used by a TU/CU, return a virtual section
12557 of just that piece. */
12558
12559static struct dwarf2_section_info
ed2dc618
SM
12560create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12561 struct dwarf2_section_info *section,
73869dc2
DE
12562 bfd_size_type offset, bfd_size_type size)
12563{
12564 struct dwarf2_section_info result;
12565 asection *sectp;
12566
12567 gdb_assert (section != NULL);
12568 gdb_assert (!section->is_virtual);
12569
12570 memset (&result, 0, sizeof (result));
12571 result.s.containing_section = section;
12572 result.is_virtual = 1;
12573
12574 if (size == 0)
12575 return result;
12576
12577 sectp = get_section_bfd_section (section);
12578
12579 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12580 bounds of the real section. This is a pretty-rare event, so just
12581 flag an error (easier) instead of a warning and trying to cope. */
12582 if (sectp == NULL
12583 || offset + size > bfd_get_section_size (sectp))
12584 {
73869dc2
DE
12585 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12586 " in section %s [in module %s]"),
12587 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12588 objfile_name (dwarf2_per_objfile->objfile));
12589 }
12590
12591 result.virtual_offset = offset;
12592 result.size = size;
12593 return result;
12594}
12595
12596/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12597 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12598 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12599 This is for DWP version 2 files. */
12600
12601static struct dwo_unit *
ed2dc618
SM
12602create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12603 struct dwp_file *dwp_file,
73869dc2
DE
12604 uint32_t unit_index,
12605 const char *comp_dir,
12606 ULONGEST signature, int is_debug_types)
12607{
12608 struct objfile *objfile = dwarf2_per_objfile->objfile;
12609 const struct dwp_hash_table *dwp_htab =
12610 is_debug_types ? dwp_file->tus : dwp_file->cus;
12611 bfd *dbfd = dwp_file->dbfd;
12612 const char *kind = is_debug_types ? "TU" : "CU";
12613 struct dwo_file *dwo_file;
12614 struct dwo_unit *dwo_unit;
12615 struct virtual_v2_dwo_sections sections;
12616 void **dwo_file_slot;
73869dc2
DE
12617 int i;
12618
12619 gdb_assert (dwp_file->version == 2);
12620
b4f54984 12621 if (dwarf_read_debug)
73869dc2
DE
12622 {
12623 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12624 kind,
12625 pulongest (unit_index), hex_string (signature),
12626 dwp_file->name);
12627 }
12628
12629 /* Fetch the section offsets of this DWO unit. */
12630
12631 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12632
12633 for (i = 0; i < dwp_htab->nr_columns; ++i)
12634 {
12635 uint32_t offset = read_4_bytes (dbfd,
12636 dwp_htab->section_pool.v2.offsets
12637 + (((unit_index - 1) * dwp_htab->nr_columns
12638 + i)
12639 * sizeof (uint32_t)));
12640 uint32_t size = read_4_bytes (dbfd,
12641 dwp_htab->section_pool.v2.sizes
12642 + (((unit_index - 1) * dwp_htab->nr_columns
12643 + i)
12644 * sizeof (uint32_t)));
12645
12646 switch (dwp_htab->section_pool.v2.section_ids[i])
12647 {
12648 case DW_SECT_INFO:
12649 case DW_SECT_TYPES:
12650 sections.info_or_types_offset = offset;
12651 sections.info_or_types_size = size;
12652 break;
12653 case DW_SECT_ABBREV:
12654 sections.abbrev_offset = offset;
12655 sections.abbrev_size = size;
12656 break;
12657 case DW_SECT_LINE:
12658 sections.line_offset = offset;
12659 sections.line_size = size;
12660 break;
12661 case DW_SECT_LOC:
12662 sections.loc_offset = offset;
12663 sections.loc_size = size;
12664 break;
12665 case DW_SECT_STR_OFFSETS:
12666 sections.str_offsets_offset = offset;
12667 sections.str_offsets_size = size;
12668 break;
12669 case DW_SECT_MACINFO:
12670 sections.macinfo_offset = offset;
12671 sections.macinfo_size = size;
12672 break;
12673 case DW_SECT_MACRO:
12674 sections.macro_offset = offset;
12675 sections.macro_size = size;
12676 break;
12677 }
12678 }
12679
12680 /* It's easier for the rest of the code if we fake a struct dwo_file and
12681 have dwo_unit "live" in that. At least for now.
12682
12683 The DWP file can be made up of a random collection of CUs and TUs.
12684 However, for each CU + set of TUs that came from the same original DWO
12685 file, we can combine them back into a virtual DWO file to save space
12686 (fewer struct dwo_file objects to allocate). Remember that for really
12687 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12688
791afaa2
TT
12689 std::string virtual_dwo_name =
12690 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12691 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12692 (long) (sections.line_size ? sections.line_offset : 0),
12693 (long) (sections.loc_size ? sections.loc_offset : 0),
12694 (long) (sections.str_offsets_size
12695 ? sections.str_offsets_offset : 0));
73869dc2 12696 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12697 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12698 virtual_dwo_name.c_str (),
12699 comp_dir);
73869dc2
DE
12700 /* Create one if necessary. */
12701 if (*dwo_file_slot == NULL)
12702 {
b4f54984 12703 if (dwarf_read_debug)
73869dc2
DE
12704 {
12705 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12706 virtual_dwo_name.c_str ());
73869dc2
DE
12707 }
12708 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12709 dwo_file->dwo_name
12710 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12711 virtual_dwo_name.c_str (),
12712 virtual_dwo_name.size ());
73869dc2
DE
12713 dwo_file->comp_dir = comp_dir;
12714 dwo_file->sections.abbrev =
ed2dc618 12715 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12716 sections.abbrev_offset, sections.abbrev_size);
12717 dwo_file->sections.line =
ed2dc618 12718 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12719 sections.line_offset, sections.line_size);
12720 dwo_file->sections.loc =
ed2dc618 12721 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12722 sections.loc_offset, sections.loc_size);
12723 dwo_file->sections.macinfo =
ed2dc618 12724 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12725 sections.macinfo_offset, sections.macinfo_size);
12726 dwo_file->sections.macro =
ed2dc618 12727 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12728 sections.macro_offset, sections.macro_size);
12729 dwo_file->sections.str_offsets =
ed2dc618
SM
12730 create_dwp_v2_section (dwarf2_per_objfile,
12731 &dwp_file->sections.str_offsets,
73869dc2
DE
12732 sections.str_offsets_offset,
12733 sections.str_offsets_size);
12734 /* The "str" section is global to the entire DWP file. */
12735 dwo_file->sections.str = dwp_file->sections.str;
12736 /* The info or types section is assigned below to dwo_unit,
12737 there's no need to record it in dwo_file.
12738 Also, we can't simply record type sections in dwo_file because
12739 we record a pointer into the vector in dwo_unit. As we collect more
12740 types we'll grow the vector and eventually have to reallocate space
12741 for it, invalidating all copies of pointers into the previous
12742 contents. */
12743 *dwo_file_slot = dwo_file;
12744 }
12745 else
12746 {
b4f54984 12747 if (dwarf_read_debug)
73869dc2
DE
12748 {
12749 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12750 virtual_dwo_name.c_str ());
73869dc2 12751 }
9a3c8263 12752 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12753 }
73869dc2
DE
12754
12755 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12756 dwo_unit->dwo_file = dwo_file;
12757 dwo_unit->signature = signature;
8d749320
SM
12758 dwo_unit->section =
12759 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12760 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12761 is_debug_types
73869dc2
DE
12762 ? &dwp_file->sections.types
12763 : &dwp_file->sections.info,
12764 sections.info_or_types_offset,
12765 sections.info_or_types_size);
12766 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12767
12768 return dwo_unit;
12769}
12770
57d63ce2
DE
12771/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12772 Returns NULL if the signature isn't found. */
80626a55
DE
12773
12774static struct dwo_unit *
ed2dc618
SM
12775lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12776 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12777 ULONGEST signature, int is_debug_types)
80626a55 12778{
57d63ce2
DE
12779 const struct dwp_hash_table *dwp_htab =
12780 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 12781 bfd *dbfd = dwp_file->dbfd;
57d63ce2 12782 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12783 uint32_t hash = signature & mask;
12784 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12785 unsigned int i;
12786 void **slot;
870f88f7 12787 struct dwo_unit find_dwo_cu;
80626a55
DE
12788
12789 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12790 find_dwo_cu.signature = signature;
19ac8c2e
DE
12791 slot = htab_find_slot (is_debug_types
12792 ? dwp_file->loaded_tus
12793 : dwp_file->loaded_cus,
12794 &find_dwo_cu, INSERT);
80626a55
DE
12795
12796 if (*slot != NULL)
9a3c8263 12797 return (struct dwo_unit *) *slot;
80626a55
DE
12798
12799 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12800 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12801 {
12802 ULONGEST signature_in_table;
12803
12804 signature_in_table =
57d63ce2 12805 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12806 if (signature_in_table == signature)
12807 {
57d63ce2
DE
12808 uint32_t unit_index =
12809 read_4_bytes (dbfd,
12810 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12811
73869dc2
DE
12812 if (dwp_file->version == 1)
12813 {
ed2dc618
SM
12814 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12815 dwp_file, unit_index,
73869dc2
DE
12816 comp_dir, signature,
12817 is_debug_types);
12818 }
12819 else
12820 {
ed2dc618
SM
12821 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12822 dwp_file, unit_index,
73869dc2
DE
12823 comp_dir, signature,
12824 is_debug_types);
12825 }
9a3c8263 12826 return (struct dwo_unit *) *slot;
80626a55
DE
12827 }
12828 if (signature_in_table == 0)
12829 return NULL;
12830 hash = (hash + hash2) & mask;
12831 }
12832
12833 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12834 " [in module %s]"),
12835 dwp_file->name);
12836}
12837
ab5088bf 12838/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12839 Open the file specified by FILE_NAME and hand it off to BFD for
12840 preliminary analysis. Return a newly initialized bfd *, which
12841 includes a canonicalized copy of FILE_NAME.
80626a55 12842 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12843 SEARCH_CWD is true if the current directory is to be searched.
12844 It will be searched before debug-file-directory.
13aaf454
DE
12845 If successful, the file is added to the bfd include table of the
12846 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12847 If unable to find/open the file, return NULL.
3019eac3
DE
12848 NOTE: This function is derived from symfile_bfd_open. */
12849
192b62ce 12850static gdb_bfd_ref_ptr
ed2dc618
SM
12851try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12852 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12853{
80626a55 12854 int desc, flags;
3019eac3 12855 char *absolute_name;
9c02c129
DE
12856 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12857 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12858 to debug_file_directory. */
12859 char *search_path;
12860 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12861
6ac97d4c
DE
12862 if (search_cwd)
12863 {
12864 if (*debug_file_directory != '\0')
12865 search_path = concat (".", dirname_separator_string,
b36cec19 12866 debug_file_directory, (char *) NULL);
6ac97d4c
DE
12867 else
12868 search_path = xstrdup (".");
12869 }
9c02c129 12870 else
6ac97d4c 12871 search_path = xstrdup (debug_file_directory);
3019eac3 12872
492c0ab7 12873 flags = OPF_RETURN_REALPATH;
80626a55
DE
12874 if (is_dwp)
12875 flags |= OPF_SEARCH_IN_PATH;
9c02c129 12876 desc = openp (search_path, flags, file_name,
3019eac3 12877 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 12878 xfree (search_path);
3019eac3
DE
12879 if (desc < 0)
12880 return NULL;
12881
192b62ce 12882 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
a4453b7e 12883 xfree (absolute_name);
9c02c129
DE
12884 if (sym_bfd == NULL)
12885 return NULL;
192b62ce 12886 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12887
192b62ce
TT
12888 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12889 return NULL;
3019eac3 12890
13aaf454
DE
12891 /* Success. Record the bfd as having been included by the objfile's bfd.
12892 This is important because things like demangled_names_hash lives in the
12893 objfile's per_bfd space and may have references to things like symbol
12894 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12895 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12896
3019eac3
DE
12897 return sym_bfd;
12898}
12899
ab5088bf 12900/* Try to open DWO file FILE_NAME.
3019eac3
DE
12901 COMP_DIR is the DW_AT_comp_dir attribute.
12902 The result is the bfd handle of the file.
12903 If there is a problem finding or opening the file, return NULL.
12904 Upon success, the canonicalized path of the file is stored in the bfd,
12905 same as symfile_bfd_open. */
12906
192b62ce 12907static gdb_bfd_ref_ptr
ed2dc618
SM
12908open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12909 const char *file_name, const char *comp_dir)
3019eac3 12910{
80626a55 12911 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12912 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12913 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12914
12915 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12916
12917 if (comp_dir != NULL)
12918 {
b36cec19
PA
12919 char *path_to_try = concat (comp_dir, SLASH_STRING,
12920 file_name, (char *) NULL);
3019eac3
DE
12921
12922 /* NOTE: If comp_dir is a relative path, this will also try the
12923 search path, which seems useful. */
ed2dc618
SM
12924 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12925 path_to_try,
12926 0 /*is_dwp*/,
192b62ce 12927 1 /*search_cwd*/));
3019eac3
DE
12928 xfree (path_to_try);
12929 if (abfd != NULL)
12930 return abfd;
12931 }
12932
12933 /* That didn't work, try debug-file-directory, which, despite its name,
12934 is a list of paths. */
12935
12936 if (*debug_file_directory == '\0')
12937 return NULL;
12938
ed2dc618
SM
12939 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12940 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12941}
12942
80626a55
DE
12943/* This function is mapped across the sections and remembers the offset and
12944 size of each of the DWO debugging sections we are interested in. */
12945
12946static void
12947dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12948{
9a3c8263 12949 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12950 const struct dwop_section_names *names = &dwop_section_names;
12951
12952 if (section_is_p (sectp->name, &names->abbrev_dwo))
12953 {
049412e3 12954 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
12955 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12956 }
12957 else if (section_is_p (sectp->name, &names->info_dwo))
12958 {
049412e3 12959 dwo_sections->info.s.section = sectp;
80626a55
DE
12960 dwo_sections->info.size = bfd_get_section_size (sectp);
12961 }
12962 else if (section_is_p (sectp->name, &names->line_dwo))
12963 {
049412e3 12964 dwo_sections->line.s.section = sectp;
80626a55
DE
12965 dwo_sections->line.size = bfd_get_section_size (sectp);
12966 }
12967 else if (section_is_p (sectp->name, &names->loc_dwo))
12968 {
049412e3 12969 dwo_sections->loc.s.section = sectp;
80626a55
DE
12970 dwo_sections->loc.size = bfd_get_section_size (sectp);
12971 }
12972 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12973 {
049412e3 12974 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
12975 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12976 }
12977 else if (section_is_p (sectp->name, &names->macro_dwo))
12978 {
049412e3 12979 dwo_sections->macro.s.section = sectp;
80626a55
DE
12980 dwo_sections->macro.size = bfd_get_section_size (sectp);
12981 }
12982 else if (section_is_p (sectp->name, &names->str_dwo))
12983 {
049412e3 12984 dwo_sections->str.s.section = sectp;
80626a55
DE
12985 dwo_sections->str.size = bfd_get_section_size (sectp);
12986 }
12987 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12988 {
049412e3 12989 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
12990 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12991 }
12992 else if (section_is_p (sectp->name, &names->types_dwo))
12993 {
12994 struct dwarf2_section_info type_section;
12995
12996 memset (&type_section, 0, sizeof (type_section));
049412e3 12997 type_section.s.section = sectp;
80626a55
DE
12998 type_section.size = bfd_get_section_size (sectp);
12999 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
13000 &type_section);
13001 }
13002}
13003
ab5088bf 13004/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 13005 by PER_CU. This is for the non-DWP case.
80626a55 13006 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
13007
13008static struct dwo_file *
0ac5b59e
DE
13009open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13010 const char *dwo_name, const char *comp_dir)
3019eac3 13011{
ed2dc618 13012 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 13013 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 13014 struct dwo_file *dwo_file;
3019eac3
DE
13015 struct cleanup *cleanups;
13016
ed2dc618 13017 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
80626a55
DE
13018 if (dbfd == NULL)
13019 {
b4f54984 13020 if (dwarf_read_debug)
80626a55
DE
13021 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13022 return NULL;
13023 }
13024 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
13025 dwo_file->dwo_name = dwo_name;
13026 dwo_file->comp_dir = comp_dir;
192b62ce 13027 dwo_file->dbfd = dbfd.release ();
3019eac3 13028
ed2dc618
SM
13029 free_dwo_file_cleanup_data *cleanup_data = XNEW (free_dwo_file_cleanup_data);
13030 cleanup_data->dwo_file = dwo_file;
13031 cleanup_data->dwarf2_per_objfile = dwarf2_per_objfile;
13032
13033 cleanups = make_cleanup (free_dwo_file_cleanup, cleanup_data);
3019eac3 13034
192b62ce
TT
13035 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
13036 &dwo_file->sections);
3019eac3 13037
ed2dc618
SM
13038 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13039 dwo_file->cus);
3019eac3 13040
ed2dc618
SM
13041 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file,
13042 dwo_file->sections.types, dwo_file->tus);
3019eac3
DE
13043
13044 discard_cleanups (cleanups);
13045
b4f54984 13046 if (dwarf_read_debug)
80626a55
DE
13047 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13048
3019eac3
DE
13049 return dwo_file;
13050}
13051
80626a55 13052/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
13053 size of each of the DWP debugging sections common to version 1 and 2 that
13054 we are interested in. */
3019eac3 13055
80626a55 13056static void
73869dc2
DE
13057dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13058 void *dwp_file_ptr)
3019eac3 13059{
9a3c8263 13060 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
13061 const struct dwop_section_names *names = &dwop_section_names;
13062 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 13063
80626a55 13064 /* Record the ELF section number for later lookup: this is what the
73869dc2 13065 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
13066 gdb_assert (elf_section_nr < dwp_file->num_sections);
13067 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 13068
80626a55
DE
13069 /* Look for specific sections that we need. */
13070 if (section_is_p (sectp->name, &names->str_dwo))
13071 {
049412e3 13072 dwp_file->sections.str.s.section = sectp;
80626a55
DE
13073 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13074 }
13075 else if (section_is_p (sectp->name, &names->cu_index))
13076 {
049412e3 13077 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
13078 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13079 }
13080 else if (section_is_p (sectp->name, &names->tu_index))
13081 {
049412e3 13082 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
13083 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13084 }
13085}
3019eac3 13086
73869dc2
DE
13087/* This function is mapped across the sections and remembers the offset and
13088 size of each of the DWP version 2 debugging sections that we are interested
13089 in. This is split into a separate function because we don't know if we
13090 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13091
13092static void
13093dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13094{
9a3c8263 13095 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
13096 const struct dwop_section_names *names = &dwop_section_names;
13097 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13098
13099 /* Record the ELF section number for later lookup: this is what the
13100 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13101 gdb_assert (elf_section_nr < dwp_file->num_sections);
13102 dwp_file->elf_sections[elf_section_nr] = sectp;
13103
13104 /* Look for specific sections that we need. */
13105 if (section_is_p (sectp->name, &names->abbrev_dwo))
13106 {
049412e3 13107 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
13108 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13109 }
13110 else if (section_is_p (sectp->name, &names->info_dwo))
13111 {
049412e3 13112 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
13113 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13114 }
13115 else if (section_is_p (sectp->name, &names->line_dwo))
13116 {
049412e3 13117 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
13118 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13119 }
13120 else if (section_is_p (sectp->name, &names->loc_dwo))
13121 {
049412e3 13122 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
13123 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13124 }
13125 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13126 {
049412e3 13127 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
13128 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13129 }
13130 else if (section_is_p (sectp->name, &names->macro_dwo))
13131 {
049412e3 13132 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
13133 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13134 }
13135 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13136 {
049412e3 13137 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
13138 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13139 }
13140 else if (section_is_p (sectp->name, &names->types_dwo))
13141 {
049412e3 13142 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
13143 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13144 }
13145}
13146
80626a55 13147/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13148
80626a55
DE
13149static hashval_t
13150hash_dwp_loaded_cutus (const void *item)
13151{
9a3c8263 13152 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13153
80626a55
DE
13154 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13155 return dwo_unit->signature;
3019eac3
DE
13156}
13157
80626a55 13158/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13159
80626a55
DE
13160static int
13161eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13162{
9a3c8263
SM
13163 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13164 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13165
80626a55
DE
13166 return dua->signature == dub->signature;
13167}
3019eac3 13168
80626a55 13169/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13170
80626a55
DE
13171static htab_t
13172allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13173{
13174 return htab_create_alloc_ex (3,
13175 hash_dwp_loaded_cutus,
13176 eq_dwp_loaded_cutus,
13177 NULL,
13178 &objfile->objfile_obstack,
13179 hashtab_obstack_allocate,
13180 dummy_obstack_deallocate);
13181}
3019eac3 13182
ab5088bf
DE
13183/* Try to open DWP file FILE_NAME.
13184 The result is the bfd handle of the file.
13185 If there is a problem finding or opening the file, return NULL.
13186 Upon success, the canonicalized path of the file is stored in the bfd,
13187 same as symfile_bfd_open. */
13188
192b62ce 13189static gdb_bfd_ref_ptr
ed2dc618
SM
13190open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13191 const char *file_name)
ab5088bf 13192{
ed2dc618
SM
13193 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13194 1 /*is_dwp*/,
192b62ce 13195 1 /*search_cwd*/));
6ac97d4c
DE
13196 if (abfd != NULL)
13197 return abfd;
13198
13199 /* Work around upstream bug 15652.
13200 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13201 [Whether that's a "bug" is debatable, but it is getting in our way.]
13202 We have no real idea where the dwp file is, because gdb's realpath-ing
13203 of the executable's path may have discarded the needed info.
13204 [IWBN if the dwp file name was recorded in the executable, akin to
13205 .gnu_debuglink, but that doesn't exist yet.]
13206 Strip the directory from FILE_NAME and search again. */
13207 if (*debug_file_directory != '\0')
13208 {
13209 /* Don't implicitly search the current directory here.
13210 If the user wants to search "." to handle this case,
13211 it must be added to debug-file-directory. */
ed2dc618
SM
13212 return try_open_dwop_file (dwarf2_per_objfile,
13213 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
13214 0 /*search_cwd*/);
13215 }
13216
13217 return NULL;
ab5088bf
DE
13218}
13219
80626a55
DE
13220/* Initialize the use of the DWP file for the current objfile.
13221 By convention the name of the DWP file is ${objfile}.dwp.
13222 The result is NULL if it can't be found. */
a766d390 13223
80626a55 13224static struct dwp_file *
ed2dc618 13225open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
13226{
13227 struct objfile *objfile = dwarf2_per_objfile->objfile;
13228 struct dwp_file *dwp_file;
80626a55 13229
82bf32bc
JK
13230 /* Try to find first .dwp for the binary file before any symbolic links
13231 resolving. */
6c447423
DE
13232
13233 /* If the objfile is a debug file, find the name of the real binary
13234 file and get the name of dwp file from there. */
d721ba37 13235 std::string dwp_name;
6c447423
DE
13236 if (objfile->separate_debug_objfile_backlink != NULL)
13237 {
13238 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13239 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13240
d721ba37 13241 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13242 }
13243 else
d721ba37
PA
13244 dwp_name = objfile->original_name;
13245
13246 dwp_name += ".dwp";
80626a55 13247
ed2dc618 13248 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13249 if (dbfd == NULL
13250 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13251 {
13252 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13253 dwp_name = objfile_name (objfile);
13254 dwp_name += ".dwp";
ed2dc618 13255 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
13256 }
13257
80626a55
DE
13258 if (dbfd == NULL)
13259 {
b4f54984 13260 if (dwarf_read_debug)
d721ba37 13261 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
80626a55 13262 return NULL;
3019eac3 13263 }
80626a55 13264 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
192b62ce
TT
13265 dwp_file->name = bfd_get_filename (dbfd.get ());
13266 dwp_file->dbfd = dbfd.release ();
c906108c 13267
80626a55 13268 /* +1: section 0 is unused */
192b62ce 13269 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
13270 dwp_file->elf_sections =
13271 OBSTACK_CALLOC (&objfile->objfile_obstack,
13272 dwp_file->num_sections, asection *);
13273
192b62ce
TT
13274 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13275 dwp_file);
80626a55 13276
ed2dc618 13277 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 0);
80626a55 13278
ed2dc618 13279 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 1);
80626a55 13280
73869dc2 13281 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13282 if (dwp_file->cus && dwp_file->tus
13283 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13284 {
13285 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13286 pretty bizarre. We use pulongest here because that's the established
4d65956b 13287 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13288 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13289 " TU version %s [in DWP file %s]"),
13290 pulongest (dwp_file->cus->version),
d721ba37 13291 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13292 }
08302ed2
DE
13293
13294 if (dwp_file->cus)
13295 dwp_file->version = dwp_file->cus->version;
13296 else if (dwp_file->tus)
13297 dwp_file->version = dwp_file->tus->version;
13298 else
13299 dwp_file->version = 2;
73869dc2
DE
13300
13301 if (dwp_file->version == 2)
192b62ce
TT
13302 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13303 dwp_file);
73869dc2 13304
19ac8c2e
DE
13305 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13306 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13307
b4f54984 13308 if (dwarf_read_debug)
80626a55
DE
13309 {
13310 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13311 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13312 " %s CUs, %s TUs\n",
13313 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13314 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13315 }
13316
13317 return dwp_file;
3019eac3 13318}
c906108c 13319
ab5088bf
DE
13320/* Wrapper around open_and_init_dwp_file, only open it once. */
13321
13322static struct dwp_file *
ed2dc618 13323get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
13324{
13325 if (! dwarf2_per_objfile->dwp_checked)
13326 {
ed2dc618
SM
13327 dwarf2_per_objfile->dwp_file
13328 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
13329 dwarf2_per_objfile->dwp_checked = 1;
13330 }
13331 return dwarf2_per_objfile->dwp_file;
13332}
13333
80626a55
DE
13334/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13335 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13336 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13337 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13338 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13339
13340 This is called, for example, when wanting to read a variable with a
13341 complex location. Therefore we don't want to do file i/o for every call.
13342 Therefore we don't want to look for a DWO file on every call.
13343 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13344 then we check if we've already seen DWO_NAME, and only THEN do we check
13345 for a DWO file.
13346
1c658ad5 13347 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13348 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13349
3019eac3 13350static struct dwo_unit *
80626a55
DE
13351lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13352 const char *dwo_name, const char *comp_dir,
13353 ULONGEST signature, int is_debug_types)
3019eac3 13354{
ed2dc618 13355 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 13356 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13357 const char *kind = is_debug_types ? "TU" : "CU";
13358 void **dwo_file_slot;
3019eac3 13359 struct dwo_file *dwo_file;
80626a55 13360 struct dwp_file *dwp_file;
cb1df416 13361
6a506a2d
DE
13362 /* First see if there's a DWP file.
13363 If we have a DWP file but didn't find the DWO inside it, don't
13364 look for the original DWO file. It makes gdb behave differently
13365 depending on whether one is debugging in the build tree. */
cf2c3c16 13366
ed2dc618 13367 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 13368 if (dwp_file != NULL)
cf2c3c16 13369 {
80626a55
DE
13370 const struct dwp_hash_table *dwp_htab =
13371 is_debug_types ? dwp_file->tus : dwp_file->cus;
13372
13373 if (dwp_htab != NULL)
13374 {
13375 struct dwo_unit *dwo_cutu =
ed2dc618 13376 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 13377 signature, is_debug_types);
80626a55
DE
13378
13379 if (dwo_cutu != NULL)
13380 {
b4f54984 13381 if (dwarf_read_debug)
80626a55
DE
13382 {
13383 fprintf_unfiltered (gdb_stdlog,
13384 "Virtual DWO %s %s found: @%s\n",
13385 kind, hex_string (signature),
13386 host_address_to_string (dwo_cutu));
13387 }
13388 return dwo_cutu;
13389 }
13390 }
13391 }
6a506a2d 13392 else
80626a55 13393 {
6a506a2d 13394 /* No DWP file, look for the DWO file. */
80626a55 13395
ed2dc618
SM
13396 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13397 dwo_name, comp_dir);
6a506a2d 13398 if (*dwo_file_slot == NULL)
80626a55 13399 {
6a506a2d
DE
13400 /* Read in the file and build a table of the CUs/TUs it contains. */
13401 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13402 }
6a506a2d 13403 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13404 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13405
6a506a2d 13406 if (dwo_file != NULL)
19c3d4c9 13407 {
6a506a2d
DE
13408 struct dwo_unit *dwo_cutu = NULL;
13409
13410 if (is_debug_types && dwo_file->tus)
13411 {
13412 struct dwo_unit find_dwo_cutu;
13413
13414 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13415 find_dwo_cutu.signature = signature;
9a3c8263
SM
13416 dwo_cutu
13417 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13418 }
33c5cd75 13419 else if (!is_debug_types && dwo_file->cus)
80626a55 13420 {
33c5cd75
DB
13421 struct dwo_unit find_dwo_cutu;
13422
13423 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13424 find_dwo_cutu.signature = signature;
13425 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13426 &find_dwo_cutu);
6a506a2d
DE
13427 }
13428
13429 if (dwo_cutu != NULL)
13430 {
b4f54984 13431 if (dwarf_read_debug)
6a506a2d
DE
13432 {
13433 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13434 kind, dwo_name, hex_string (signature),
13435 host_address_to_string (dwo_cutu));
13436 }
13437 return dwo_cutu;
80626a55
DE
13438 }
13439 }
2e276125 13440 }
9cdd5dbd 13441
80626a55
DE
13442 /* We didn't find it. This could mean a dwo_id mismatch, or
13443 someone deleted the DWO/DWP file, or the search path isn't set up
13444 correctly to find the file. */
13445
b4f54984 13446 if (dwarf_read_debug)
80626a55
DE
13447 {
13448 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13449 kind, dwo_name, hex_string (signature));
13450 }
3019eac3 13451
6656a72d
DE
13452 /* This is a warning and not a complaint because it can be caused by
13453 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13454 {
13455 /* Print the name of the DWP file if we looked there, helps the user
13456 better diagnose the problem. */
791afaa2 13457 std::string dwp_text;
43942612
DE
13458
13459 if (dwp_file != NULL)
791afaa2
TT
13460 dwp_text = string_printf (" [in DWP file %s]",
13461 lbasename (dwp_file->name));
43942612
DE
13462
13463 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
13464 " [in module %s]"),
13465 kind, dwo_name, hex_string (signature),
791afaa2 13466 dwp_text.c_str (),
43942612 13467 this_unit->is_debug_types ? "TU" : "CU",
9c541725 13468 to_underlying (this_unit->sect_off), objfile_name (objfile));
43942612 13469 }
3019eac3 13470 return NULL;
5fb290d7
DJ
13471}
13472
80626a55
DE
13473/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13474 See lookup_dwo_cutu_unit for details. */
13475
13476static struct dwo_unit *
13477lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13478 const char *dwo_name, const char *comp_dir,
13479 ULONGEST signature)
13480{
13481 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13482}
13483
13484/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13485 See lookup_dwo_cutu_unit for details. */
13486
13487static struct dwo_unit *
13488lookup_dwo_type_unit (struct signatured_type *this_tu,
13489 const char *dwo_name, const char *comp_dir)
13490{
13491 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13492}
13493
89e63ee4
DE
13494/* Traversal function for queue_and_load_all_dwo_tus. */
13495
13496static int
13497queue_and_load_dwo_tu (void **slot, void *info)
13498{
13499 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13500 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13501 ULONGEST signature = dwo_unit->signature;
13502 struct signatured_type *sig_type =
13503 lookup_dwo_signatured_type (per_cu->cu, signature);
13504
13505 if (sig_type != NULL)
13506 {
13507 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13508
13509 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13510 a real dependency of PER_CU on SIG_TYPE. That is detected later
13511 while processing PER_CU. */
13512 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13513 load_full_type_unit (sig_cu);
13514 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13515 }
13516
13517 return 1;
13518}
13519
13520/* Queue all TUs contained in the DWO of PER_CU to be read in.
13521 The DWO may have the only definition of the type, though it may not be
13522 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13523 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13524
13525static void
13526queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13527{
13528 struct dwo_unit *dwo_unit;
13529 struct dwo_file *dwo_file;
13530
13531 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13532 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13533 gdb_assert (per_cu->cu != NULL);
13534
13535 dwo_unit = per_cu->cu->dwo_unit;
13536 gdb_assert (dwo_unit != NULL);
13537
13538 dwo_file = dwo_unit->dwo_file;
13539 if (dwo_file->tus != NULL)
13540 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13541}
13542
3019eac3
DE
13543/* Free all resources associated with DWO_FILE.
13544 Close the DWO file and munmap the sections.
13545 All memory should be on the objfile obstack. */
348e048f
DE
13546
13547static void
3019eac3 13548free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 13549{
348e048f 13550
5c6fa7ab 13551 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 13552 gdb_bfd_unref (dwo_file->dbfd);
348e048f 13553
3019eac3
DE
13554 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13555}
348e048f 13556
3019eac3 13557/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 13558
3019eac3
DE
13559static void
13560free_dwo_file_cleanup (void *arg)
13561{
ed2dc618
SM
13562 struct free_dwo_file_cleanup_data *data
13563 = (struct free_dwo_file_cleanup_data *) arg;
13564 struct objfile *objfile = data->dwarf2_per_objfile->objfile;
348e048f 13565
ed2dc618
SM
13566 free_dwo_file (data->dwo_file, objfile);
13567
13568 xfree (data);
3019eac3 13569}
348e048f 13570
3019eac3 13571/* Traversal function for free_dwo_files. */
2ab95328 13572
3019eac3
DE
13573static int
13574free_dwo_file_from_slot (void **slot, void *info)
13575{
13576 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13577 struct objfile *objfile = (struct objfile *) info;
348e048f 13578
3019eac3 13579 free_dwo_file (dwo_file, objfile);
348e048f 13580
3019eac3
DE
13581 return 1;
13582}
348e048f 13583
3019eac3 13584/* Free all resources associated with DWO_FILES. */
348e048f 13585
3019eac3
DE
13586static void
13587free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13588{
13589 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 13590}
3019eac3
DE
13591\f
13592/* Read in various DIEs. */
348e048f 13593
d389af10 13594/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13595 Inherit only the children of the DW_AT_abstract_origin DIE not being
13596 already referenced by DW_AT_abstract_origin from the children of the
13597 current DIE. */
d389af10
JK
13598
13599static void
13600inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13601{
13602 struct die_info *child_die;
791afaa2 13603 sect_offset *offsetp;
d389af10
JK
13604 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13605 struct die_info *origin_die;
13606 /* Iterator of the ORIGIN_DIE children. */
13607 struct die_info *origin_child_die;
d389af10 13608 struct attribute *attr;
cd02d79d
PA
13609 struct dwarf2_cu *origin_cu;
13610 struct pending **origin_previous_list_in_scope;
d389af10
JK
13611
13612 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13613 if (!attr)
13614 return;
13615
cd02d79d
PA
13616 /* Note that following die references may follow to a die in a
13617 different cu. */
13618
13619 origin_cu = cu;
13620 origin_die = follow_die_ref (die, attr, &origin_cu);
13621
13622 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13623 symbols in. */
13624 origin_previous_list_in_scope = origin_cu->list_in_scope;
13625 origin_cu->list_in_scope = cu->list_in_scope;
13626
edb3359d
DJ
13627 if (die->tag != origin_die->tag
13628 && !(die->tag == DW_TAG_inlined_subroutine
13629 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
13630 complaint (&symfile_complaints,
13631 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9c541725
PA
13632 to_underlying (die->sect_off),
13633 to_underlying (origin_die->sect_off));
d389af10 13634
791afaa2 13635 std::vector<sect_offset> offsets;
d389af10 13636
3ea89b92
PMR
13637 for (child_die = die->child;
13638 child_die && child_die->tag;
13639 child_die = sibling_die (child_die))
13640 {
13641 struct die_info *child_origin_die;
13642 struct dwarf2_cu *child_origin_cu;
13643
13644 /* We are trying to process concrete instance entries:
216f72a1 13645 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13646 it's not relevant to our analysis here. i.e. detecting DIEs that are
13647 present in the abstract instance but not referenced in the concrete
13648 one. */
216f72a1
JK
13649 if (child_die->tag == DW_TAG_call_site
13650 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13651 continue;
13652
c38f313d
DJ
13653 /* For each CHILD_DIE, find the corresponding child of
13654 ORIGIN_DIE. If there is more than one layer of
13655 DW_AT_abstract_origin, follow them all; there shouldn't be,
13656 but GCC versions at least through 4.4 generate this (GCC PR
13657 40573). */
3ea89b92
PMR
13658 child_origin_die = child_die;
13659 child_origin_cu = cu;
c38f313d
DJ
13660 while (1)
13661 {
cd02d79d
PA
13662 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13663 child_origin_cu);
c38f313d
DJ
13664 if (attr == NULL)
13665 break;
cd02d79d
PA
13666 child_origin_die = follow_die_ref (child_origin_die, attr,
13667 &child_origin_cu);
c38f313d
DJ
13668 }
13669
d389af10
JK
13670 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13671 counterpart may exist. */
c38f313d 13672 if (child_origin_die != child_die)
d389af10 13673 {
edb3359d
DJ
13674 if (child_die->tag != child_origin_die->tag
13675 && !(child_die->tag == DW_TAG_inlined_subroutine
13676 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
13677 complaint (&symfile_complaints,
13678 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
13679 "different tags"),
13680 to_underlying (child_die->sect_off),
13681 to_underlying (child_origin_die->sect_off));
c38f313d
DJ
13682 if (child_origin_die->parent != origin_die)
13683 complaint (&symfile_complaints,
13684 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
13685 "different parents"),
13686 to_underlying (child_die->sect_off),
13687 to_underlying (child_origin_die->sect_off));
c38f313d 13688 else
791afaa2 13689 offsets.push_back (child_origin_die->sect_off);
d389af10 13690 }
d389af10 13691 }
791afaa2
TT
13692 std::sort (offsets.begin (), offsets.end ());
13693 sect_offset *offsets_end = offsets.data () + offsets.size ();
13694 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13695 if (offsetp[-1] == *offsetp)
3e43a32a
MS
13696 complaint (&symfile_complaints,
13697 _("Multiple children of DIE 0x%x refer "
13698 "to DIE 0x%x as their abstract origin"),
9c541725 13699 to_underlying (die->sect_off), to_underlying (*offsetp));
d389af10 13700
791afaa2 13701 offsetp = offsets.data ();
d389af10
JK
13702 origin_child_die = origin_die->child;
13703 while (origin_child_die && origin_child_die->tag)
13704 {
13705 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13706 while (offsetp < offsets_end
9c541725 13707 && *offsetp < origin_child_die->sect_off)
d389af10 13708 offsetp++;
b64f50a1 13709 if (offsetp >= offsets_end
9c541725 13710 || *offsetp > origin_child_die->sect_off)
d389af10 13711 {
adde2bff
DE
13712 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13713 Check whether we're already processing ORIGIN_CHILD_DIE.
13714 This can happen with mutually referenced abstract_origins.
13715 PR 16581. */
13716 if (!origin_child_die->in_process)
13717 process_die (origin_child_die, origin_cu);
d389af10
JK
13718 }
13719 origin_child_die = sibling_die (origin_child_die);
13720 }
cd02d79d 13721 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
13722}
13723
c906108c 13724static void
e7c27a73 13725read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13726{
518817b3 13727 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13728 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13729 struct context_stack *newobj;
c906108c
SS
13730 CORE_ADDR lowpc;
13731 CORE_ADDR highpc;
13732 struct die_info *child_die;
edb3359d 13733 struct attribute *attr, *call_line, *call_file;
15d034d0 13734 const char *name;
e142c38c 13735 CORE_ADDR baseaddr;
801e3a5b 13736 struct block *block;
edb3359d 13737 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13738 std::vector<struct symbol *> template_args;
34eaf542 13739 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13740
13741 if (inlined_func)
13742 {
13743 /* If we do not have call site information, we can't show the
13744 caller of this inlined function. That's too confusing, so
13745 only use the scope for local variables. */
13746 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13747 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13748 if (call_line == NULL || call_file == NULL)
13749 {
13750 read_lexical_block_scope (die, cu);
13751 return;
13752 }
13753 }
c906108c 13754
e142c38c
DJ
13755 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13756
94af9270 13757 name = dwarf2_name (die, cu);
c906108c 13758
e8d05480
JB
13759 /* Ignore functions with missing or empty names. These are actually
13760 illegal according to the DWARF standard. */
13761 if (name == NULL)
13762 {
13763 complaint (&symfile_complaints,
b64f50a1 13764 _("missing name for subprogram DIE at %d"),
9c541725 13765 to_underlying (die->sect_off));
e8d05480
JB
13766 return;
13767 }
13768
13769 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13770 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13771 <= PC_BOUNDS_INVALID)
e8d05480 13772 {
ae4d0c03
PM
13773 attr = dwarf2_attr (die, DW_AT_external, cu);
13774 if (!attr || !DW_UNSND (attr))
13775 complaint (&symfile_complaints,
3e43a32a
MS
13776 _("cannot get low and high bounds "
13777 "for subprogram DIE at %d"),
9c541725 13778 to_underlying (die->sect_off));
e8d05480
JB
13779 return;
13780 }
c906108c 13781
3e29f34a
MR
13782 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13783 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13784
34eaf542
TT
13785 /* If we have any template arguments, then we must allocate a
13786 different sort of symbol. */
13787 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13788 {
13789 if (child_die->tag == DW_TAG_template_type_param
13790 || child_die->tag == DW_TAG_template_value_param)
13791 {
e623cf5d 13792 templ_func = allocate_template_symbol (objfile);
cf724bc9 13793 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13794 break;
13795 }
13796 }
13797
fe978cb0 13798 newobj = push_context (0, lowpc);
5e2db402
TT
13799 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13800 (struct symbol *) templ_func);
4c2df51b 13801
4cecd739
DJ
13802 /* If there is a location expression for DW_AT_frame_base, record
13803 it. */
e142c38c 13804 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 13805 if (attr)
fe978cb0 13806 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13807
63e43d3a
PMR
13808 /* If there is a location for the static link, record it. */
13809 newobj->static_link = NULL;
13810 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13811 if (attr)
13812 {
224c3ddb
SM
13813 newobj->static_link
13814 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
13815 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13816 }
13817
e142c38c 13818 cu->list_in_scope = &local_symbols;
c906108c 13819
639d11d3 13820 if (die->child != NULL)
c906108c 13821 {
639d11d3 13822 child_die = die->child;
c906108c
SS
13823 while (child_die && child_die->tag)
13824 {
34eaf542
TT
13825 if (child_die->tag == DW_TAG_template_type_param
13826 || child_die->tag == DW_TAG_template_value_param)
13827 {
13828 struct symbol *arg = new_symbol (child_die, NULL, cu);
13829
f1078f66 13830 if (arg != NULL)
2f4732b0 13831 template_args.push_back (arg);
34eaf542
TT
13832 }
13833 else
13834 process_die (child_die, cu);
c906108c
SS
13835 child_die = sibling_die (child_die);
13836 }
13837 }
13838
d389af10
JK
13839 inherit_abstract_dies (die, cu);
13840
4a811a97
UW
13841 /* If we have a DW_AT_specification, we might need to import using
13842 directives from the context of the specification DIE. See the
13843 comment in determine_prefix. */
13844 if (cu->language == language_cplus
13845 && dwarf2_attr (die, DW_AT_specification, cu))
13846 {
13847 struct dwarf2_cu *spec_cu = cu;
13848 struct die_info *spec_die = die_specification (die, &spec_cu);
13849
13850 while (spec_die)
13851 {
13852 child_die = spec_die->child;
13853 while (child_die && child_die->tag)
13854 {
13855 if (child_die->tag == DW_TAG_imported_module)
13856 process_die (child_die, spec_cu);
13857 child_die = sibling_die (child_die);
13858 }
13859
13860 /* In some cases, GCC generates specification DIEs that
13861 themselves contain DW_AT_specification attributes. */
13862 spec_die = die_specification (spec_die, &spec_cu);
13863 }
13864 }
13865
fe978cb0 13866 newobj = pop_context ();
c906108c 13867 /* Make a block for the local symbols within. */
fe978cb0 13868 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
63e43d3a 13869 newobj->static_link, lowpc, highpc);
801e3a5b 13870
df8a16a1 13871 /* For C++, set the block's scope. */
45280282
IB
13872 if ((cu->language == language_cplus
13873 || cu->language == language_fortran
c44af4eb
TT
13874 || cu->language == language_d
13875 || cu->language == language_rust)
4d4ec4e5 13876 && cu->processing_has_namespace_info)
195a3f6c
TT
13877 block_set_scope (block, determine_prefix (die, cu),
13878 &objfile->objfile_obstack);
df8a16a1 13879
801e3a5b
JB
13880 /* If we have address ranges, record them. */
13881 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13882
fe978cb0 13883 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 13884
34eaf542 13885 /* Attach template arguments to function. */
2f4732b0 13886 if (!template_args.empty ())
34eaf542
TT
13887 {
13888 gdb_assert (templ_func != NULL);
13889
2f4732b0 13890 templ_func->n_template_arguments = template_args.size ();
34eaf542 13891 templ_func->template_arguments
8d749320
SM
13892 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13893 templ_func->n_template_arguments);
34eaf542 13894 memcpy (templ_func->template_arguments,
2f4732b0 13895 template_args.data (),
34eaf542 13896 (templ_func->n_template_arguments * sizeof (struct symbol *)));
34eaf542
TT
13897 }
13898
208d8187
JB
13899 /* In C++, we can have functions nested inside functions (e.g., when
13900 a function declares a class that has methods). This means that
13901 when we finish processing a function scope, we may need to go
13902 back to building a containing block's symbol lists. */
fe978cb0 13903 local_symbols = newobj->locals;
22cee43f 13904 local_using_directives = newobj->local_using_directives;
208d8187 13905
921e78cf
JB
13906 /* If we've finished processing a top-level function, subsequent
13907 symbols go in the file symbol list. */
13908 if (outermost_context_p ())
e142c38c 13909 cu->list_in_scope = &file_symbols;
c906108c
SS
13910}
13911
13912/* Process all the DIES contained within a lexical block scope. Start
13913 a new scope, process the dies, and then close the scope. */
13914
13915static void
e7c27a73 13916read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13917{
518817b3 13918 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13919 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13920 struct context_stack *newobj;
c906108c
SS
13921 CORE_ADDR lowpc, highpc;
13922 struct die_info *child_die;
e142c38c
DJ
13923 CORE_ADDR baseaddr;
13924
13925 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
13926
13927 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13928 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13929 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13930 be nasty. Might be easier to properly extend generic blocks to
af34e669 13931 describe ranges. */
e385593e
JK
13932 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13933 {
13934 case PC_BOUNDS_NOT_PRESENT:
13935 /* DW_TAG_lexical_block has no attributes, process its children as if
13936 there was no wrapping by that DW_TAG_lexical_block.
13937 GCC does no longer produces such DWARF since GCC r224161. */
13938 for (child_die = die->child;
13939 child_die != NULL && child_die->tag;
13940 child_die = sibling_die (child_die))
13941 process_die (child_die, cu);
13942 return;
13943 case PC_BOUNDS_INVALID:
13944 return;
13945 }
3e29f34a
MR
13946 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13947 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
13948
13949 push_context (0, lowpc);
639d11d3 13950 if (die->child != NULL)
c906108c 13951 {
639d11d3 13952 child_die = die->child;
c906108c
SS
13953 while (child_die && child_die->tag)
13954 {
e7c27a73 13955 process_die (child_die, cu);
c906108c
SS
13956 child_die = sibling_die (child_die);
13957 }
13958 }
3ea89b92 13959 inherit_abstract_dies (die, cu);
fe978cb0 13960 newobj = pop_context ();
c906108c 13961
22cee43f 13962 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 13963 {
801e3a5b 13964 struct block *block
63e43d3a 13965 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
fe978cb0 13966 newobj->start_addr, highpc);
801e3a5b
JB
13967
13968 /* Note that recording ranges after traversing children, as we
13969 do here, means that recording a parent's ranges entails
13970 walking across all its children's ranges as they appear in
13971 the address map, which is quadratic behavior.
13972
13973 It would be nicer to record the parent's ranges before
13974 traversing its children, simply overriding whatever you find
13975 there. But since we don't even decide whether to create a
13976 block until after we've traversed its children, that's hard
13977 to do. */
13978 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13979 }
fe978cb0 13980 local_symbols = newobj->locals;
22cee43f 13981 local_using_directives = newobj->local_using_directives;
c906108c
SS
13982}
13983
216f72a1 13984/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13985
13986static void
13987read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13988{
518817b3 13989 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13990 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13991 CORE_ADDR pc, baseaddr;
13992 struct attribute *attr;
13993 struct call_site *call_site, call_site_local;
13994 void **slot;
13995 int nparams;
13996 struct die_info *child_die;
13997
13998 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13999
216f72a1
JK
14000 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
14001 if (attr == NULL)
14002 {
14003 /* This was a pre-DWARF-5 GNU extension alias
14004 for DW_AT_call_return_pc. */
14005 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14006 }
96408a79
SA
14007 if (!attr)
14008 {
14009 complaint (&symfile_complaints,
216f72a1 14010 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
96408a79 14011 "DIE 0x%x [in module %s]"),
9c541725 14012 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
14013 return;
14014 }
31aa7e4e 14015 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 14016 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
14017
14018 if (cu->call_site_htab == NULL)
14019 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
14020 NULL, &objfile->objfile_obstack,
14021 hashtab_obstack_allocate, NULL);
14022 call_site_local.pc = pc;
14023 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
14024 if (*slot != NULL)
14025 {
14026 complaint (&symfile_complaints,
216f72a1 14027 _("Duplicate PC %s for DW_TAG_call_site "
96408a79 14028 "DIE 0x%x [in module %s]"),
9c541725 14029 paddress (gdbarch, pc), to_underlying (die->sect_off),
4262abfb 14030 objfile_name (objfile));
96408a79
SA
14031 return;
14032 }
14033
14034 /* Count parameters at the caller. */
14035
14036 nparams = 0;
14037 for (child_die = die->child; child_die && child_die->tag;
14038 child_die = sibling_die (child_die))
14039 {
216f72a1
JK
14040 if (child_die->tag != DW_TAG_call_site_parameter
14041 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14042 {
14043 complaint (&symfile_complaints,
216f72a1
JK
14044 _("Tag %d is not DW_TAG_call_site_parameter in "
14045 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 14046 child_die->tag, to_underlying (child_die->sect_off),
4262abfb 14047 objfile_name (objfile));
96408a79
SA
14048 continue;
14049 }
14050
14051 nparams++;
14052 }
14053
224c3ddb
SM
14054 call_site
14055 = ((struct call_site *)
14056 obstack_alloc (&objfile->objfile_obstack,
14057 sizeof (*call_site)
14058 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
14059 *slot = call_site;
14060 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14061 call_site->pc = pc;
14062
216f72a1
JK
14063 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14064 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
14065 {
14066 struct die_info *func_die;
14067
14068 /* Skip also over DW_TAG_inlined_subroutine. */
14069 for (func_die = die->parent;
14070 func_die && func_die->tag != DW_TAG_subprogram
14071 && func_die->tag != DW_TAG_subroutine_type;
14072 func_die = func_die->parent);
14073
216f72a1
JK
14074 /* DW_AT_call_all_calls is a superset
14075 of DW_AT_call_all_tail_calls. */
96408a79 14076 if (func_die
216f72a1 14077 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 14078 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 14079 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
14080 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14081 {
14082 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14083 not complete. But keep CALL_SITE for look ups via call_site_htab,
14084 both the initial caller containing the real return address PC and
14085 the final callee containing the current PC of a chain of tail
14086 calls do not need to have the tail call list complete. But any
14087 function candidate for a virtual tail call frame searched via
14088 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14089 determined unambiguously. */
14090 }
14091 else
14092 {
14093 struct type *func_type = NULL;
14094
14095 if (func_die)
14096 func_type = get_die_type (func_die, cu);
14097 if (func_type != NULL)
14098 {
14099 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14100
14101 /* Enlist this call site to the function. */
14102 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14103 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14104 }
14105 else
14106 complaint (&symfile_complaints,
216f72a1 14107 _("Cannot find function owning DW_TAG_call_site "
96408a79 14108 "DIE 0x%x [in module %s]"),
9c541725 14109 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
14110 }
14111 }
14112
216f72a1
JK
14113 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14114 if (attr == NULL)
14115 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14116 if (attr == NULL)
14117 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14118 if (attr == NULL)
216f72a1
JK
14119 {
14120 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14121 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14122 }
96408a79
SA
14123 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14124 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14125 /* Keep NULL DWARF_BLOCK. */;
14126 else if (attr_form_is_block (attr))
14127 {
14128 struct dwarf2_locexpr_baton *dlbaton;
14129
8d749320 14130 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
14131 dlbaton->data = DW_BLOCK (attr)->data;
14132 dlbaton->size = DW_BLOCK (attr)->size;
14133 dlbaton->per_cu = cu->per_cu;
14134
14135 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14136 }
7771576e 14137 else if (attr_form_is_ref (attr))
96408a79 14138 {
96408a79
SA
14139 struct dwarf2_cu *target_cu = cu;
14140 struct die_info *target_die;
14141
ac9ec31b 14142 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 14143 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
14144 if (die_is_declaration (target_die, target_cu))
14145 {
7d45c7c3 14146 const char *target_physname;
9112db09
JK
14147
14148 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14149 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14150 if (target_physname == NULL)
9112db09 14151 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
14152 if (target_physname == NULL)
14153 complaint (&symfile_complaints,
216f72a1 14154 _("DW_AT_call_target target DIE has invalid "
96408a79 14155 "physname, for referencing DIE 0x%x [in module %s]"),
9c541725 14156 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 14157 else
7d455152 14158 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14159 }
14160 else
14161 {
14162 CORE_ADDR lowpc;
14163
14164 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14165 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14166 <= PC_BOUNDS_INVALID)
96408a79 14167 complaint (&symfile_complaints,
216f72a1 14168 _("DW_AT_call_target target DIE has invalid "
96408a79 14169 "low pc, for referencing DIE 0x%x [in module %s]"),
9c541725 14170 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 14171 else
3e29f34a
MR
14172 {
14173 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14174 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14175 }
96408a79
SA
14176 }
14177 }
14178 else
14179 complaint (&symfile_complaints,
216f72a1 14180 _("DW_TAG_call_site DW_AT_call_target is neither "
96408a79 14181 "block nor reference, for DIE 0x%x [in module %s]"),
9c541725 14182 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
14183
14184 call_site->per_cu = cu->per_cu;
14185
14186 for (child_die = die->child;
14187 child_die && child_die->tag;
14188 child_die = sibling_die (child_die))
14189 {
96408a79 14190 struct call_site_parameter *parameter;
1788b2d3 14191 struct attribute *loc, *origin;
96408a79 14192
216f72a1
JK
14193 if (child_die->tag != DW_TAG_call_site_parameter
14194 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14195 {
14196 /* Already printed the complaint above. */
14197 continue;
14198 }
14199
14200 gdb_assert (call_site->parameter_count < nparams);
14201 parameter = &call_site->parameter[call_site->parameter_count];
14202
1788b2d3
JK
14203 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14204 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14205 register is contained in DW_AT_call_value. */
96408a79 14206
24c5c679 14207 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14208 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14209 if (origin == NULL)
14210 {
14211 /* This was a pre-DWARF-5 GNU extension alias
14212 for DW_AT_call_parameter. */
14213 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14214 }
7771576e 14215 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14216 {
1788b2d3 14217 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14218
14219 sect_offset sect_off
14220 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14221 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14222 {
14223 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14224 binding can be done only inside one CU. Such referenced DIE
14225 therefore cannot be even moved to DW_TAG_partial_unit. */
14226 complaint (&symfile_complaints,
216f72a1
JK
14227 _("DW_AT_call_parameter offset is not in CU for "
14228 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
14229 to_underlying (child_die->sect_off),
14230 objfile_name (objfile));
d76b7dbc
JK
14231 continue;
14232 }
9c541725
PA
14233 parameter->u.param_cu_off
14234 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14235 }
14236 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
14237 {
14238 complaint (&symfile_complaints,
14239 _("No DW_FORM_block* DW_AT_location for "
216f72a1 14240 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 14241 to_underlying (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14242 continue;
14243 }
24c5c679 14244 else
96408a79 14245 {
24c5c679
JK
14246 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14247 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14248 if (parameter->u.dwarf_reg != -1)
14249 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14250 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14251 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14252 &parameter->u.fb_offset))
14253 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14254 else
14255 {
14256 complaint (&symfile_complaints,
14257 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14258 "for DW_FORM_block* DW_AT_location is supported for "
216f72a1 14259 "DW_TAG_call_site child DIE 0x%x "
24c5c679 14260 "[in module %s]"),
9c541725
PA
14261 to_underlying (child_die->sect_off),
14262 objfile_name (objfile));
24c5c679
JK
14263 continue;
14264 }
96408a79
SA
14265 }
14266
216f72a1
JK
14267 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14268 if (attr == NULL)
14269 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14270 if (!attr_form_is_block (attr))
14271 {
14272 complaint (&symfile_complaints,
216f72a1
JK
14273 _("No DW_FORM_block* DW_AT_call_value for "
14274 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
14275 to_underlying (child_die->sect_off),
14276 objfile_name (objfile));
96408a79
SA
14277 continue;
14278 }
14279 parameter->value = DW_BLOCK (attr)->data;
14280 parameter->value_size = DW_BLOCK (attr)->size;
14281
14282 /* Parameters are not pre-cleared by memset above. */
14283 parameter->data_value = NULL;
14284 parameter->data_value_size = 0;
14285 call_site->parameter_count++;
14286
216f72a1
JK
14287 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14288 if (attr == NULL)
14289 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
14290 if (attr)
14291 {
14292 if (!attr_form_is_block (attr))
14293 complaint (&symfile_complaints,
216f72a1
JK
14294 _("No DW_FORM_block* DW_AT_call_data_value for "
14295 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
14296 to_underlying (child_die->sect_off),
14297 objfile_name (objfile));
96408a79
SA
14298 else
14299 {
14300 parameter->data_value = DW_BLOCK (attr)->data;
14301 parameter->data_value_size = DW_BLOCK (attr)->size;
14302 }
14303 }
14304 }
14305}
14306
71a3c369
TT
14307/* Helper function for read_variable. If DIE represents a virtual
14308 table, then return the type of the concrete object that is
14309 associated with the virtual table. Otherwise, return NULL. */
14310
14311static struct type *
14312rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14313{
14314 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14315 if (attr == NULL)
14316 return NULL;
14317
14318 /* Find the type DIE. */
14319 struct die_info *type_die = NULL;
14320 struct dwarf2_cu *type_cu = cu;
14321
14322 if (attr_form_is_ref (attr))
14323 type_die = follow_die_ref (die, attr, &type_cu);
14324 if (type_die == NULL)
14325 return NULL;
14326
14327 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14328 return NULL;
14329 return die_containing_type (type_die, type_cu);
14330}
14331
14332/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14333
14334static void
14335read_variable (struct die_info *die, struct dwarf2_cu *cu)
14336{
14337 struct rust_vtable_symbol *storage = NULL;
14338
14339 if (cu->language == language_rust)
14340 {
14341 struct type *containing_type = rust_containing_type (die, cu);
14342
14343 if (containing_type != NULL)
14344 {
518817b3 14345 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369
TT
14346
14347 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14348 struct rust_vtable_symbol);
14349 initialize_objfile_symbol (storage);
14350 storage->concrete_type = containing_type;
cf724bc9 14351 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14352 }
14353 }
14354
5e2db402 14355 new_symbol (die, NULL, cu, storage);
71a3c369
TT
14356}
14357
43988095
JK
14358/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14359 reading .debug_rnglists.
14360 Callback's type should be:
14361 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14362 Return true if the attributes are present and valid, otherwise,
14363 return false. */
14364
14365template <typename Callback>
14366static bool
14367dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14368 Callback &&callback)
14369{
ed2dc618 14370 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14371 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14372 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 14373 bfd *obfd = objfile->obfd;
43988095
JK
14374 /* Base address selection entry. */
14375 CORE_ADDR base;
14376 int found_base;
43988095 14377 const gdb_byte *buffer;
43988095
JK
14378 CORE_ADDR baseaddr;
14379 bool overflow = false;
14380
14381 found_base = cu->base_known;
14382 base = cu->base_address;
14383
14384 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14385 if (offset >= dwarf2_per_objfile->rnglists.size)
14386 {
14387 complaint (&symfile_complaints,
14388 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14389 offset);
14390 return false;
14391 }
14392 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14393
14394 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14395
14396 while (1)
14397 {
7814882a
JK
14398 /* Initialize it due to a false compiler warning. */
14399 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14400 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14401 + dwarf2_per_objfile->rnglists.size);
14402 unsigned int bytes_read;
14403
14404 if (buffer == buf_end)
14405 {
14406 overflow = true;
14407 break;
14408 }
14409 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14410 switch (rlet)
14411 {
14412 case DW_RLE_end_of_list:
14413 break;
14414 case DW_RLE_base_address:
14415 if (buffer + cu->header.addr_size > buf_end)
14416 {
14417 overflow = true;
14418 break;
14419 }
14420 base = read_address (obfd, buffer, cu, &bytes_read);
14421 found_base = 1;
14422 buffer += bytes_read;
14423 break;
14424 case DW_RLE_start_length:
14425 if (buffer + cu->header.addr_size > buf_end)
14426 {
14427 overflow = true;
14428 break;
14429 }
14430 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14431 buffer += bytes_read;
14432 range_end = (range_beginning
14433 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14434 buffer += bytes_read;
14435 if (buffer > buf_end)
14436 {
14437 overflow = true;
14438 break;
14439 }
14440 break;
14441 case DW_RLE_offset_pair:
14442 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14443 buffer += bytes_read;
14444 if (buffer > buf_end)
14445 {
14446 overflow = true;
14447 break;
14448 }
14449 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14450 buffer += bytes_read;
14451 if (buffer > buf_end)
14452 {
14453 overflow = true;
14454 break;
14455 }
14456 break;
14457 case DW_RLE_start_end:
14458 if (buffer + 2 * cu->header.addr_size > buf_end)
14459 {
14460 overflow = true;
14461 break;
14462 }
14463 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14464 buffer += bytes_read;
14465 range_end = read_address (obfd, buffer, cu, &bytes_read);
14466 buffer += bytes_read;
14467 break;
14468 default:
14469 complaint (&symfile_complaints,
14470 _("Invalid .debug_rnglists data (no base address)"));
14471 return false;
14472 }
14473 if (rlet == DW_RLE_end_of_list || overflow)
14474 break;
14475 if (rlet == DW_RLE_base_address)
14476 continue;
14477
14478 if (!found_base)
14479 {
14480 /* We have no valid base address for the ranges
14481 data. */
14482 complaint (&symfile_complaints,
14483 _("Invalid .debug_rnglists data (no base address)"));
14484 return false;
14485 }
14486
14487 if (range_beginning > range_end)
14488 {
14489 /* Inverted range entries are invalid. */
14490 complaint (&symfile_complaints,
14491 _("Invalid .debug_rnglists data (inverted range)"));
14492 return false;
14493 }
14494
14495 /* Empty range entries have no effect. */
14496 if (range_beginning == range_end)
14497 continue;
14498
14499 range_beginning += base;
14500 range_end += base;
14501
14502 /* A not-uncommon case of bad debug info.
14503 Don't pollute the addrmap with bad data. */
14504 if (range_beginning + baseaddr == 0
14505 && !dwarf2_per_objfile->has_section_at_zero)
14506 {
14507 complaint (&symfile_complaints,
14508 _(".debug_rnglists entry has start address of zero"
14509 " [in module %s]"), objfile_name (objfile));
14510 continue;
14511 }
14512
14513 callback (range_beginning, range_end);
14514 }
14515
14516 if (overflow)
14517 {
14518 complaint (&symfile_complaints,
14519 _("Offset %d is not terminated "
14520 "for DW_AT_ranges attribute"),
14521 offset);
14522 return false;
14523 }
14524
14525 return true;
14526}
14527
14528/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14529 Callback's type should be:
14530 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14531 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14532
43988095 14533template <typename Callback>
43039443 14534static int
5f46c5a5 14535dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14536 Callback &&callback)
43039443 14537{
ed2dc618 14538 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14539 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14540 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14541 struct comp_unit_head *cu_header = &cu->header;
14542 bfd *obfd = objfile->obfd;
14543 unsigned int addr_size = cu_header->addr_size;
14544 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14545 /* Base address selection entry. */
14546 CORE_ADDR base;
14547 int found_base;
14548 unsigned int dummy;
d521ce57 14549 const gdb_byte *buffer;
ff013f42 14550 CORE_ADDR baseaddr;
43039443 14551
43988095
JK
14552 if (cu_header->version >= 5)
14553 return dwarf2_rnglists_process (offset, cu, callback);
14554
d00adf39
DE
14555 found_base = cu->base_known;
14556 base = cu->base_address;
43039443 14557
be391dca 14558 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14559 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
14560 {
14561 complaint (&symfile_complaints,
14562 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14563 offset);
14564 return 0;
14565 }
dce234bc 14566 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14567
e7030f15 14568 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 14569
43039443
JK
14570 while (1)
14571 {
14572 CORE_ADDR range_beginning, range_end;
14573
14574 range_beginning = read_address (obfd, buffer, cu, &dummy);
14575 buffer += addr_size;
14576 range_end = read_address (obfd, buffer, cu, &dummy);
14577 buffer += addr_size;
14578 offset += 2 * addr_size;
14579
14580 /* An end of list marker is a pair of zero addresses. */
14581 if (range_beginning == 0 && range_end == 0)
14582 /* Found the end of list entry. */
14583 break;
14584
14585 /* Each base address selection entry is a pair of 2 values.
14586 The first is the largest possible address, the second is
14587 the base address. Check for a base address here. */
14588 if ((range_beginning & mask) == mask)
14589 {
28d2bfb9
AB
14590 /* If we found the largest possible address, then we already
14591 have the base address in range_end. */
14592 base = range_end;
43039443
JK
14593 found_base = 1;
14594 continue;
14595 }
14596
14597 if (!found_base)
14598 {
14599 /* We have no valid base address for the ranges
14600 data. */
14601 complaint (&symfile_complaints,
14602 _("Invalid .debug_ranges data (no base address)"));
14603 return 0;
14604 }
14605
9277c30c
UW
14606 if (range_beginning > range_end)
14607 {
14608 /* Inverted range entries are invalid. */
14609 complaint (&symfile_complaints,
14610 _("Invalid .debug_ranges data (inverted range)"));
14611 return 0;
14612 }
14613
14614 /* Empty range entries have no effect. */
14615 if (range_beginning == range_end)
14616 continue;
14617
43039443
JK
14618 range_beginning += base;
14619 range_end += base;
14620
01093045
DE
14621 /* A not-uncommon case of bad debug info.
14622 Don't pollute the addrmap with bad data. */
14623 if (range_beginning + baseaddr == 0
14624 && !dwarf2_per_objfile->has_section_at_zero)
14625 {
14626 complaint (&symfile_complaints,
14627 _(".debug_ranges entry has start address of zero"
4262abfb 14628 " [in module %s]"), objfile_name (objfile));
01093045
DE
14629 continue;
14630 }
14631
5f46c5a5
JK
14632 callback (range_beginning, range_end);
14633 }
14634
14635 return 1;
14636}
14637
14638/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14639 Return 1 if the attributes are present and valid, otherwise, return 0.
14640 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14641
14642static int
14643dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14644 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14645 struct partial_symtab *ranges_pst)
14646{
518817b3 14647 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5
JK
14648 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14649 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14650 SECT_OFF_TEXT (objfile));
14651 int low_set = 0;
14652 CORE_ADDR low = 0;
14653 CORE_ADDR high = 0;
14654 int retval;
14655
14656 retval = dwarf2_ranges_process (offset, cu,
14657 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14658 {
9277c30c 14659 if (ranges_pst != NULL)
3e29f34a
MR
14660 {
14661 CORE_ADDR lowpc;
14662 CORE_ADDR highpc;
14663
14664 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14665 range_beginning + baseaddr);
14666 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14667 range_end + baseaddr);
14668 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14669 ranges_pst);
14670 }
ff013f42 14671
43039443
JK
14672 /* FIXME: This is recording everything as a low-high
14673 segment of consecutive addresses. We should have a
14674 data structure for discontiguous block ranges
14675 instead. */
14676 if (! low_set)
14677 {
14678 low = range_beginning;
14679 high = range_end;
14680 low_set = 1;
14681 }
14682 else
14683 {
14684 if (range_beginning < low)
14685 low = range_beginning;
14686 if (range_end > high)
14687 high = range_end;
14688 }
5f46c5a5
JK
14689 });
14690 if (!retval)
14691 return 0;
43039443
JK
14692
14693 if (! low_set)
14694 /* If the first entry is an end-of-list marker, the range
14695 describes an empty scope, i.e. no instructions. */
14696 return 0;
14697
14698 if (low_return)
14699 *low_return = low;
14700 if (high_return)
14701 *high_return = high;
14702 return 1;
14703}
14704
3a2b436a
JK
14705/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14706 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14707 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14708
3a2b436a 14709static enum pc_bounds_kind
af34e669 14710dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
14711 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14712 struct partial_symtab *pst)
c906108c 14713{
518817b3
SM
14714 struct dwarf2_per_objfile *dwarf2_per_objfile
14715 = cu->per_cu->dwarf2_per_objfile;
c906108c 14716 struct attribute *attr;
91da1414 14717 struct attribute *attr_high;
af34e669
DJ
14718 CORE_ADDR low = 0;
14719 CORE_ADDR high = 0;
e385593e 14720 enum pc_bounds_kind ret;
c906108c 14721
91da1414
MW
14722 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14723 if (attr_high)
af34e669 14724 {
e142c38c 14725 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 14726 if (attr)
91da1414 14727 {
31aa7e4e
JB
14728 low = attr_value_as_address (attr);
14729 high = attr_value_as_address (attr_high);
14730 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14731 high += low;
91da1414 14732 }
af34e669
DJ
14733 else
14734 /* Found high w/o low attribute. */
e385593e 14735 return PC_BOUNDS_INVALID;
af34e669
DJ
14736
14737 /* Found consecutive range of addresses. */
3a2b436a 14738 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14739 }
c906108c 14740 else
af34e669 14741 {
e142c38c 14742 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14743 if (attr != NULL)
14744 {
ab435259
DE
14745 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14746 We take advantage of the fact that DW_AT_ranges does not appear
14747 in DW_TAG_compile_unit of DWO files. */
14748 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14749 unsigned int ranges_offset = (DW_UNSND (attr)
14750 + (need_ranges_base
14751 ? cu->ranges_base
14752 : 0));
2e3cf129 14753
af34e669 14754 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14755 .debug_ranges section. */
2e3cf129 14756 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14757 return PC_BOUNDS_INVALID;
43039443 14758 /* Found discontinuous range of addresses. */
3a2b436a 14759 ret = PC_BOUNDS_RANGES;
af34e669 14760 }
e385593e
JK
14761 else
14762 return PC_BOUNDS_NOT_PRESENT;
af34e669 14763 }
c906108c 14764
9373cf26
JK
14765 /* read_partial_die has also the strict LOW < HIGH requirement. */
14766 if (high <= low)
e385593e 14767 return PC_BOUNDS_INVALID;
c906108c
SS
14768
14769 /* When using the GNU linker, .gnu.linkonce. sections are used to
14770 eliminate duplicate copies of functions and vtables and such.
14771 The linker will arbitrarily choose one and discard the others.
14772 The AT_*_pc values for such functions refer to local labels in
14773 these sections. If the section from that file was discarded, the
14774 labels are not in the output, so the relocs get a value of 0.
14775 If this is a discarded function, mark the pc bounds as invalid,
14776 so that GDB will ignore it. */
72dca2f5 14777 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14778 return PC_BOUNDS_INVALID;
c906108c
SS
14779
14780 *lowpc = low;
96408a79
SA
14781 if (highpc)
14782 *highpc = high;
af34e669 14783 return ret;
c906108c
SS
14784}
14785
b084d499
JB
14786/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14787 its low and high PC addresses. Do nothing if these addresses could not
14788 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14789 and HIGHPC to the high address if greater than HIGHPC. */
14790
14791static void
14792dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14793 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14794 struct dwarf2_cu *cu)
14795{
14796 CORE_ADDR low, high;
14797 struct die_info *child = die->child;
14798
e385593e 14799 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14800 {
325fac50
PA
14801 *lowpc = std::min (*lowpc, low);
14802 *highpc = std::max (*highpc, high);
b084d499
JB
14803 }
14804
14805 /* If the language does not allow nested subprograms (either inside
14806 subprograms or lexical blocks), we're done. */
14807 if (cu->language != language_ada)
14808 return;
6e70227d 14809
b084d499
JB
14810 /* Check all the children of the given DIE. If it contains nested
14811 subprograms, then check their pc bounds. Likewise, we need to
14812 check lexical blocks as well, as they may also contain subprogram
14813 definitions. */
14814 while (child && child->tag)
14815 {
14816 if (child->tag == DW_TAG_subprogram
14817 || child->tag == DW_TAG_lexical_block)
14818 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14819 child = sibling_die (child);
14820 }
14821}
14822
fae299cd
DC
14823/* Get the low and high pc's represented by the scope DIE, and store
14824 them in *LOWPC and *HIGHPC. If the correct values can't be
14825 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14826
14827static void
14828get_scope_pc_bounds (struct die_info *die,
14829 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14830 struct dwarf2_cu *cu)
14831{
14832 CORE_ADDR best_low = (CORE_ADDR) -1;
14833 CORE_ADDR best_high = (CORE_ADDR) 0;
14834 CORE_ADDR current_low, current_high;
14835
3a2b436a 14836 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14837 >= PC_BOUNDS_RANGES)
fae299cd
DC
14838 {
14839 best_low = current_low;
14840 best_high = current_high;
14841 }
14842 else
14843 {
14844 struct die_info *child = die->child;
14845
14846 while (child && child->tag)
14847 {
14848 switch (child->tag) {
14849 case DW_TAG_subprogram:
b084d499 14850 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14851 break;
14852 case DW_TAG_namespace:
f55ee35c 14853 case DW_TAG_module:
fae299cd
DC
14854 /* FIXME: carlton/2004-01-16: Should we do this for
14855 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14856 that current GCC's always emit the DIEs corresponding
14857 to definitions of methods of classes as children of a
14858 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14859 the DIEs giving the declarations, which could be
14860 anywhere). But I don't see any reason why the
14861 standards says that they have to be there. */
14862 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14863
14864 if (current_low != ((CORE_ADDR) -1))
14865 {
325fac50
PA
14866 best_low = std::min (best_low, current_low);
14867 best_high = std::max (best_high, current_high);
fae299cd
DC
14868 }
14869 break;
14870 default:
0963b4bd 14871 /* Ignore. */
fae299cd
DC
14872 break;
14873 }
14874
14875 child = sibling_die (child);
14876 }
14877 }
14878
14879 *lowpc = best_low;
14880 *highpc = best_high;
14881}
14882
801e3a5b
JB
14883/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14884 in DIE. */
380bca97 14885
801e3a5b
JB
14886static void
14887dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14888 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14889{
518817b3 14890 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14891 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14892 struct attribute *attr;
91da1414 14893 struct attribute *attr_high;
801e3a5b 14894
91da1414
MW
14895 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14896 if (attr_high)
801e3a5b 14897 {
801e3a5b
JB
14898 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14899 if (attr)
14900 {
31aa7e4e
JB
14901 CORE_ADDR low = attr_value_as_address (attr);
14902 CORE_ADDR high = attr_value_as_address (attr_high);
14903
14904 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14905 high += low;
9a619af0 14906
3e29f34a
MR
14907 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14908 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14909 record_block_range (block, low, high - 1);
801e3a5b
JB
14910 }
14911 }
14912
14913 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14914 if (attr)
14915 {
ab435259
DE
14916 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14917 We take advantage of the fact that DW_AT_ranges does not appear
14918 in DW_TAG_compile_unit of DWO files. */
14919 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14920
14921 /* The value of the DW_AT_ranges attribute is the offset of the
14922 address range list in the .debug_ranges section. */
ab435259
DE
14923 unsigned long offset = (DW_UNSND (attr)
14924 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 14925 const gdb_byte *buffer;
801e3a5b
JB
14926
14927 /* For some target architectures, but not others, the
14928 read_address function sign-extends the addresses it returns.
14929 To recognize base address selection entries, we need a
14930 mask. */
14931 unsigned int addr_size = cu->header.addr_size;
14932 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14933
14934 /* The base address, to which the next pair is relative. Note
14935 that this 'base' is a DWARF concept: most entries in a range
14936 list are relative, to reduce the number of relocs against the
14937 debugging information. This is separate from this function's
14938 'baseaddr' argument, which GDB uses to relocate debugging
14939 information from a shared library based on the address at
14940 which the library was loaded. */
d00adf39
DE
14941 CORE_ADDR base = cu->base_address;
14942 int base_known = cu->base_known;
801e3a5b 14943
5f46c5a5
JK
14944 dwarf2_ranges_process (offset, cu,
14945 [&] (CORE_ADDR start, CORE_ADDR end)
14946 {
58fdfd2c
JK
14947 start += baseaddr;
14948 end += baseaddr;
5f46c5a5
JK
14949 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14950 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14951 record_block_range (block, start, end - 1);
14952 });
801e3a5b
JB
14953 }
14954}
14955
685b1105
JK
14956/* Check whether the producer field indicates either of GCC < 4.6, or the
14957 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14958
685b1105
JK
14959static void
14960check_producer (struct dwarf2_cu *cu)
60d5a603 14961{
38360086 14962 int major, minor;
60d5a603
JK
14963
14964 if (cu->producer == NULL)
14965 {
14966 /* For unknown compilers expect their behavior is DWARF version
14967 compliant.
14968
14969 GCC started to support .debug_types sections by -gdwarf-4 since
14970 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14971 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14972 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14973 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14974 }
b1ffba5a 14975 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14976 {
38360086
MW
14977 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14978 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14979 }
5230b05a
WT
14980 else if (producer_is_icc (cu->producer, &major, &minor))
14981 cu->producer_is_icc_lt_14 = major < 14;
685b1105
JK
14982 else
14983 {
14984 /* For other non-GCC compilers, expect their behavior is DWARF version
14985 compliant. */
60d5a603
JK
14986 }
14987
ba919b58 14988 cu->checked_producer = 1;
685b1105 14989}
ba919b58 14990
685b1105
JK
14991/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14992 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14993 during 4.6.0 experimental. */
14994
14995static int
14996producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14997{
14998 if (!cu->checked_producer)
14999 check_producer (cu);
15000
15001 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
15002}
15003
15004/* Return the default accessibility type if it is not overriden by
15005 DW_AT_accessibility. */
15006
15007static enum dwarf_access_attribute
15008dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
15009{
15010 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15011 {
15012 /* The default DWARF 2 accessibility for members is public, the default
15013 accessibility for inheritance is private. */
15014
15015 if (die->tag != DW_TAG_inheritance)
15016 return DW_ACCESS_public;
15017 else
15018 return DW_ACCESS_private;
15019 }
15020 else
15021 {
15022 /* DWARF 3+ defines the default accessibility a different way. The same
15023 rules apply now for DW_TAG_inheritance as for the members and it only
15024 depends on the container kind. */
15025
15026 if (die->parent->tag == DW_TAG_class_type)
15027 return DW_ACCESS_private;
15028 else
15029 return DW_ACCESS_public;
15030 }
15031}
15032
74ac6d43
TT
15033/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15034 offset. If the attribute was not found return 0, otherwise return
15035 1. If it was found but could not properly be handled, set *OFFSET
15036 to 0. */
15037
15038static int
15039handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15040 LONGEST *offset)
15041{
15042 struct attribute *attr;
15043
15044 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15045 if (attr != NULL)
15046 {
15047 *offset = 0;
15048
15049 /* Note that we do not check for a section offset first here.
15050 This is because DW_AT_data_member_location is new in DWARF 4,
15051 so if we see it, we can assume that a constant form is really
15052 a constant and not a section offset. */
15053 if (attr_form_is_constant (attr))
15054 *offset = dwarf2_get_attr_constant_value (attr, 0);
15055 else if (attr_form_is_section_offset (attr))
15056 dwarf2_complex_location_expr_complaint ();
15057 else if (attr_form_is_block (attr))
15058 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15059 else
15060 dwarf2_complex_location_expr_complaint ();
15061
15062 return 1;
15063 }
15064
15065 return 0;
15066}
15067
c906108c
SS
15068/* Add an aggregate field to the field list. */
15069
15070static void
107d2387 15071dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 15072 struct dwarf2_cu *cu)
6e70227d 15073{
518817b3 15074 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 15075 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
15076 struct nextfield *new_field;
15077 struct attribute *attr;
15078 struct field *fp;
15d034d0 15079 const char *fieldname = "";
c906108c
SS
15080
15081 /* Allocate a new field list entry and link it in. */
8d749320 15082 new_field = XNEW (struct nextfield);
b8c9b27d 15083 make_cleanup (xfree, new_field);
c906108c 15084 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
15085
15086 if (die->tag == DW_TAG_inheritance)
15087 {
15088 new_field->next = fip->baseclasses;
15089 fip->baseclasses = new_field;
15090 }
15091 else
15092 {
15093 new_field->next = fip->fields;
15094 fip->fields = new_field;
15095 }
c906108c
SS
15096 fip->nfields++;
15097
e142c38c 15098 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
15099 if (attr)
15100 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
15101 else
15102 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
15103 if (new_field->accessibility != DW_ACCESS_public)
15104 fip->non_public_fields = 1;
60d5a603 15105
e142c38c 15106 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
15107 if (attr)
15108 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
15109 else
15110 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15111
15112 fp = &new_field->field;
a9a9bd0f 15113
e142c38c 15114 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15115 {
74ac6d43
TT
15116 LONGEST offset;
15117
a9a9bd0f 15118 /* Data member other than a C++ static data member. */
6e70227d 15119
c906108c 15120 /* Get type of field. */
e7c27a73 15121 fp->type = die_type (die, cu);
c906108c 15122
d6a843b5 15123 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15124
c906108c 15125 /* Get bit size of field (zero if none). */
e142c38c 15126 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
15127 if (attr)
15128 {
15129 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15130 }
15131 else
15132 {
15133 FIELD_BITSIZE (*fp) = 0;
15134 }
15135
15136 /* Get bit offset of field. */
74ac6d43
TT
15137 if (handle_data_member_location (die, cu, &offset))
15138 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 15139 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
15140 if (attr)
15141 {
5e2b427d 15142 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
15143 {
15144 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
15145 additional bit offset from the MSB of the containing
15146 anonymous object to the MSB of the field. We don't
15147 have to do anything special since we don't need to
15148 know the size of the anonymous object. */
f41f5e61 15149 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
15150 }
15151 else
15152 {
15153 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
15154 MSB of the anonymous object, subtract off the number of
15155 bits from the MSB of the field to the MSB of the
15156 object, and then subtract off the number of bits of
15157 the field itself. The result is the bit offset of
15158 the LSB of the field. */
c906108c
SS
15159 int anonymous_size;
15160 int bit_offset = DW_UNSND (attr);
15161
e142c38c 15162 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15163 if (attr)
15164 {
15165 /* The size of the anonymous object containing
15166 the bit field is explicit, so use the
15167 indicated size (in bytes). */
15168 anonymous_size = DW_UNSND (attr);
15169 }
15170 else
15171 {
15172 /* The size of the anonymous object containing
15173 the bit field must be inferred from the type
15174 attribute of the data member containing the
15175 bit field. */
15176 anonymous_size = TYPE_LENGTH (fp->type);
15177 }
f41f5e61
PA
15178 SET_FIELD_BITPOS (*fp,
15179 (FIELD_BITPOS (*fp)
15180 + anonymous_size * bits_per_byte
15181 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15182 }
15183 }
da5b30da
AA
15184 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15185 if (attr != NULL)
15186 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15187 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
15188
15189 /* Get name of field. */
39cbfefa
DJ
15190 fieldname = dwarf2_name (die, cu);
15191 if (fieldname == NULL)
15192 fieldname = "";
d8151005
DJ
15193
15194 /* The name is already allocated along with this objfile, so we don't
15195 need to duplicate it for the type. */
15196 fp->name = fieldname;
c906108c
SS
15197
15198 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15199 pointer or virtual base class pointer) to private. */
e142c38c 15200 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15201 {
d48cc9dd 15202 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
15203 new_field->accessibility = DW_ACCESS_private;
15204 fip->non_public_fields = 1;
15205 }
15206 }
a9a9bd0f 15207 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15208 {
a9a9bd0f
DC
15209 /* C++ static member. */
15210
15211 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15212 is a declaration, but all versions of G++ as of this writing
15213 (so through at least 3.2.1) incorrectly generate
15214 DW_TAG_variable tags. */
6e70227d 15215
ff355380 15216 const char *physname;
c906108c 15217
a9a9bd0f 15218 /* Get name of field. */
39cbfefa
DJ
15219 fieldname = dwarf2_name (die, cu);
15220 if (fieldname == NULL)
c906108c
SS
15221 return;
15222
254e6b9e 15223 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15224 if (attr
15225 /* Only create a symbol if this is an external value.
15226 new_symbol checks this and puts the value in the global symbol
15227 table, which we want. If it is not external, new_symbol
15228 will try to put the value in cu->list_in_scope which is wrong. */
15229 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15230 {
15231 /* A static const member, not much different than an enum as far as
15232 we're concerned, except that we can support more types. */
15233 new_symbol (die, NULL, cu);
15234 }
15235
2df3850c 15236 /* Get physical name. */
ff355380 15237 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15238
d8151005
DJ
15239 /* The name is already allocated along with this objfile, so we don't
15240 need to duplicate it for the type. */
15241 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15242 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15243 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15244 }
15245 else if (die->tag == DW_TAG_inheritance)
15246 {
74ac6d43 15247 LONGEST offset;
d4b96c9a 15248
74ac6d43
TT
15249 /* C++ base class field. */
15250 if (handle_data_member_location (die, cu, &offset))
15251 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15252 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15253 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
15254 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
15255 fip->nbaseclasses++;
15256 }
15257}
15258
883fd55a
KS
15259/* Can the type given by DIE define another type? */
15260
15261static bool
15262type_can_define_types (const struct die_info *die)
15263{
15264 switch (die->tag)
15265 {
15266 case DW_TAG_typedef:
15267 case DW_TAG_class_type:
15268 case DW_TAG_structure_type:
15269 case DW_TAG_union_type:
15270 case DW_TAG_enumeration_type:
15271 return true;
15272
15273 default:
15274 return false;
15275 }
15276}
15277
15278/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15279
15280static void
883fd55a
KS
15281dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15282 struct dwarf2_cu *cu)
6e70227d 15283{
883fd55a
KS
15284 struct decl_field_list *new_field;
15285 struct decl_field *fp;
98751a41
JK
15286
15287 /* Allocate a new field list entry and link it in. */
883fd55a 15288 new_field = XCNEW (struct decl_field_list);
98751a41
JK
15289 make_cleanup (xfree, new_field);
15290
883fd55a 15291 gdb_assert (type_can_define_types (die));
98751a41
JK
15292
15293 fp = &new_field->field;
15294
883fd55a 15295 /* Get name of field. NULL is okay here, meaning an anonymous type. */
98751a41 15296 fp->name = dwarf2_name (die, cu);
98751a41
JK
15297 fp->type = read_type_die (die, cu);
15298
c191a687
KS
15299 /* Save accessibility. */
15300 enum dwarf_access_attribute accessibility;
15301 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15302 if (attr != NULL)
15303 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15304 else
15305 accessibility = dwarf2_default_access_attribute (die, cu);
15306 switch (accessibility)
15307 {
15308 case DW_ACCESS_public:
15309 /* The assumed value if neither private nor protected. */
15310 break;
15311 case DW_ACCESS_private:
15312 fp->is_private = 1;
15313 break;
15314 case DW_ACCESS_protected:
15315 fp->is_protected = 1;
15316 break;
15317 default:
37534686
KS
15318 complaint (&symfile_complaints,
15319 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15320 }
15321
883fd55a
KS
15322 if (die->tag == DW_TAG_typedef)
15323 {
15324 new_field->next = fip->typedef_field_list;
15325 fip->typedef_field_list = new_field;
15326 fip->typedef_field_list_count++;
15327 }
15328 else
15329 {
15330 new_field->next = fip->nested_types_list;
15331 fip->nested_types_list = new_field;
15332 fip->nested_types_list_count++;
15333 }
98751a41
JK
15334}
15335
c906108c
SS
15336/* Create the vector of fields, and attach it to the type. */
15337
15338static void
fba45db2 15339dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15340 struct dwarf2_cu *cu)
c906108c
SS
15341{
15342 int nfields = fip->nfields;
15343
15344 /* Record the field count, allocate space for the array of fields,
15345 and create blank accessibility bitfields if necessary. */
15346 TYPE_NFIELDS (type) = nfields;
15347 TYPE_FIELDS (type) = (struct field *)
15348 TYPE_ALLOC (type, sizeof (struct field) * nfields);
15349 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
15350
b4ba55a1 15351 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15352 {
15353 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15354
15355 TYPE_FIELD_PRIVATE_BITS (type) =
15356 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15357 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15358
15359 TYPE_FIELD_PROTECTED_BITS (type) =
15360 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15361 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15362
774b6a14
TT
15363 TYPE_FIELD_IGNORE_BITS (type) =
15364 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15365 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15366 }
15367
15368 /* If the type has baseclasses, allocate and clear a bit vector for
15369 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 15370 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
15371 {
15372 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 15373 unsigned char *pointer;
c906108c
SS
15374
15375 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15376 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15377 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
15378 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
15379 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
15380 }
15381
3e43a32a
MS
15382 /* Copy the saved-up fields into the field vector. Start from the head of
15383 the list, adding to the tail of the field array, so that they end up in
15384 the same order in the array in which they were added to the list. */
c906108c
SS
15385 while (nfields-- > 0)
15386 {
7d0ccb61
DJ
15387 struct nextfield *fieldp;
15388
15389 if (fip->fields)
15390 {
15391 fieldp = fip->fields;
15392 fip->fields = fieldp->next;
15393 }
15394 else
15395 {
15396 fieldp = fip->baseclasses;
15397 fip->baseclasses = fieldp->next;
15398 }
15399
15400 TYPE_FIELD (type, nfields) = fieldp->field;
15401 switch (fieldp->accessibility)
c906108c 15402 {
c5aa993b 15403 case DW_ACCESS_private:
b4ba55a1
JB
15404 if (cu->language != language_ada)
15405 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 15406 break;
c906108c 15407
c5aa993b 15408 case DW_ACCESS_protected:
b4ba55a1
JB
15409 if (cu->language != language_ada)
15410 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 15411 break;
c906108c 15412
c5aa993b
JM
15413 case DW_ACCESS_public:
15414 break;
c906108c 15415
c5aa993b
JM
15416 default:
15417 /* Unknown accessibility. Complain and treat it as public. */
15418 {
e2e0b3e5 15419 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 15420 fieldp->accessibility);
c5aa993b
JM
15421 }
15422 break;
c906108c
SS
15423 }
15424 if (nfields < fip->nbaseclasses)
15425 {
7d0ccb61 15426 switch (fieldp->virtuality)
c906108c 15427 {
c5aa993b
JM
15428 case DW_VIRTUALITY_virtual:
15429 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15430 if (cu->language == language_ada)
a73c6dcd 15431 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
15432 SET_TYPE_FIELD_VIRTUAL (type, nfields);
15433 break;
c906108c
SS
15434 }
15435 }
c906108c
SS
15436 }
15437}
15438
7d27a96d
TT
15439/* Return true if this member function is a constructor, false
15440 otherwise. */
15441
15442static int
15443dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15444{
15445 const char *fieldname;
fe978cb0 15446 const char *type_name;
7d27a96d
TT
15447 int len;
15448
15449 if (die->parent == NULL)
15450 return 0;
15451
15452 if (die->parent->tag != DW_TAG_structure_type
15453 && die->parent->tag != DW_TAG_union_type
15454 && die->parent->tag != DW_TAG_class_type)
15455 return 0;
15456
15457 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15458 type_name = dwarf2_name (die->parent, cu);
15459 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15460 return 0;
15461
15462 len = strlen (fieldname);
fe978cb0
PA
15463 return (strncmp (fieldname, type_name, len) == 0
15464 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15465}
15466
c906108c
SS
15467/* Add a member function to the proper fieldlist. */
15468
15469static void
107d2387 15470dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15471 struct type *type, struct dwarf2_cu *cu)
c906108c 15472{
518817b3 15473 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15474 struct attribute *attr;
15475 struct fnfieldlist *flp;
15476 int i;
15477 struct fn_field *fnp;
15d034d0 15478 const char *fieldname;
c906108c 15479 struct nextfnfield *new_fnfield;
f792889a 15480 struct type *this_type;
60d5a603 15481 enum dwarf_access_attribute accessibility;
c906108c 15482
b4ba55a1 15483 if (cu->language == language_ada)
a73c6dcd 15484 error (_("unexpected member function in Ada type"));
b4ba55a1 15485
2df3850c 15486 /* Get name of member function. */
39cbfefa
DJ
15487 fieldname = dwarf2_name (die, cu);
15488 if (fieldname == NULL)
2df3850c 15489 return;
c906108c 15490
c906108c
SS
15491 /* Look up member function name in fieldlist. */
15492 for (i = 0; i < fip->nfnfields; i++)
15493 {
27bfe10e 15494 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
15495 break;
15496 }
15497
15498 /* Create new list element if necessary. */
15499 if (i < fip->nfnfields)
15500 flp = &fip->fnfieldlists[i];
15501 else
15502 {
15503 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
15504 {
15505 fip->fnfieldlists = (struct fnfieldlist *)
15506 xrealloc (fip->fnfieldlists,
15507 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 15508 * sizeof (struct fnfieldlist));
c906108c 15509 if (fip->nfnfields == 0)
c13c43fd 15510 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
15511 }
15512 flp = &fip->fnfieldlists[fip->nfnfields];
15513 flp->name = fieldname;
15514 flp->length = 0;
15515 flp->head = NULL;
3da10d80 15516 i = fip->nfnfields++;
c906108c
SS
15517 }
15518
15519 /* Create a new member function field and chain it to the field list
0963b4bd 15520 entry. */
8d749320 15521 new_fnfield = XNEW (struct nextfnfield);
b8c9b27d 15522 make_cleanup (xfree, new_fnfield);
c906108c
SS
15523 memset (new_fnfield, 0, sizeof (struct nextfnfield));
15524 new_fnfield->next = flp->head;
15525 flp->head = new_fnfield;
15526 flp->length++;
15527
15528 /* Fill in the member function field info. */
15529 fnp = &new_fnfield->fnfield;
3da10d80
KS
15530
15531 /* Delay processing of the physname until later. */
9c37b5ae 15532 if (cu->language == language_cplus)
3da10d80
KS
15533 {
15534 add_to_method_list (type, i, flp->length - 1, fieldname,
15535 die, cu);
15536 }
15537 else
15538 {
1d06ead6 15539 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15540 fnp->physname = physname ? physname : "";
15541 }
15542
c906108c 15543 fnp->type = alloc_type (objfile);
f792889a
DJ
15544 this_type = read_type_die (die, cu);
15545 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15546 {
f792889a 15547 int nparams = TYPE_NFIELDS (this_type);
c906108c 15548
f792889a 15549 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15550 of the method itself (TYPE_CODE_METHOD). */
15551 smash_to_method_type (fnp->type, type,
f792889a
DJ
15552 TYPE_TARGET_TYPE (this_type),
15553 TYPE_FIELDS (this_type),
15554 TYPE_NFIELDS (this_type),
15555 TYPE_VARARGS (this_type));
c906108c
SS
15556
15557 /* Handle static member functions.
c5aa993b 15558 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15559 member functions. G++ helps GDB by marking the first
15560 parameter for non-static member functions (which is the this
15561 pointer) as artificial. We obtain this information from
15562 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15563 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15564 fnp->voffset = VOFFSET_STATIC;
15565 }
15566 else
e2e0b3e5 15567 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 15568 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15569
15570 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15571 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15572 fnp->fcontext = die_containing_type (die, cu);
c906108c 15573
3e43a32a
MS
15574 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15575 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15576
15577 /* Get accessibility. */
e142c38c 15578 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 15579 if (attr)
aead7601 15580 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15581 else
15582 accessibility = dwarf2_default_access_attribute (die, cu);
15583 switch (accessibility)
c906108c 15584 {
60d5a603
JK
15585 case DW_ACCESS_private:
15586 fnp->is_private = 1;
15587 break;
15588 case DW_ACCESS_protected:
15589 fnp->is_protected = 1;
15590 break;
c906108c
SS
15591 }
15592
b02dede2 15593 /* Check for artificial methods. */
e142c38c 15594 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15595 if (attr && DW_UNSND (attr) != 0)
15596 fnp->is_artificial = 1;
15597
7d27a96d
TT
15598 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15599
0d564a31 15600 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15601 function. For older versions of GCC, this is an offset in the
15602 appropriate virtual table, as specified by DW_AT_containing_type.
15603 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15604 to the object address. */
15605
e142c38c 15606 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 15607 if (attr)
8e19ed76 15608 {
aec5aa8b 15609 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15610 {
aec5aa8b
TT
15611 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15612 {
15613 /* Old-style GCC. */
15614 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15615 }
15616 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15617 || (DW_BLOCK (attr)->size > 1
15618 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15619 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15620 {
aec5aa8b
TT
15621 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15622 if ((fnp->voffset % cu->header.addr_size) != 0)
15623 dwarf2_complex_location_expr_complaint ();
15624 else
15625 fnp->voffset /= cu->header.addr_size;
15626 fnp->voffset += 2;
15627 }
15628 else
15629 dwarf2_complex_location_expr_complaint ();
15630
15631 if (!fnp->fcontext)
7e993ebf
KS
15632 {
15633 /* If there is no `this' field and no DW_AT_containing_type,
15634 we cannot actually find a base class context for the
15635 vtable! */
15636 if (TYPE_NFIELDS (this_type) == 0
15637 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15638 {
15639 complaint (&symfile_complaints,
15640 _("cannot determine context for virtual member "
15641 "function \"%s\" (offset %d)"),
9c541725 15642 fieldname, to_underlying (die->sect_off));
7e993ebf
KS
15643 }
15644 else
15645 {
15646 fnp->fcontext
15647 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15648 }
15649 }
aec5aa8b 15650 }
3690dd37 15651 else if (attr_form_is_section_offset (attr))
8e19ed76 15652 {
4d3c2250 15653 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15654 }
15655 else
15656 {
4d3c2250
KB
15657 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15658 fieldname);
8e19ed76 15659 }
0d564a31 15660 }
d48cc9dd
DJ
15661 else
15662 {
15663 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15664 if (attr && DW_UNSND (attr))
15665 {
15666 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15667 complaint (&symfile_complaints,
3e43a32a
MS
15668 _("Member function \"%s\" (offset %d) is virtual "
15669 "but the vtable offset is not specified"),
9c541725 15670 fieldname, to_underlying (die->sect_off));
9655fd1a 15671 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15672 TYPE_CPLUS_DYNAMIC (type) = 1;
15673 }
15674 }
c906108c
SS
15675}
15676
15677/* Create the vector of member function fields, and attach it to the type. */
15678
15679static void
fba45db2 15680dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15681 struct dwarf2_cu *cu)
c906108c
SS
15682{
15683 struct fnfieldlist *flp;
c906108c
SS
15684 int i;
15685
b4ba55a1 15686 if (cu->language == language_ada)
a73c6dcd 15687 error (_("unexpected member functions in Ada type"));
b4ba55a1 15688
c906108c
SS
15689 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15690 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15691 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
15692
15693 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
15694 {
15695 struct nextfnfield *nfp = flp->head;
15696 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15697 int k;
15698
15699 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
15700 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
15701 fn_flp->fn_fields = (struct fn_field *)
15702 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
15703 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 15704 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
15705 }
15706
15707 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
15708}
15709
1168df01
JB
15710/* Returns non-zero if NAME is the name of a vtable member in CU's
15711 language, zero otherwise. */
15712static int
15713is_vtable_name (const char *name, struct dwarf2_cu *cu)
15714{
15715 static const char vptr[] = "_vptr";
15716
9c37b5ae
TT
15717 /* Look for the C++ form of the vtable. */
15718 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15719 return 1;
15720
15721 return 0;
15722}
15723
c0dd20ea 15724/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15725 functions, with the ABI-specified layout. If TYPE describes
15726 such a structure, smash it into a member function type.
61049d3b
DJ
15727
15728 GCC shouldn't do this; it should just output pointer to member DIEs.
15729 This is GCC PR debug/28767. */
c0dd20ea 15730
0b92b5bb
TT
15731static void
15732quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15733{
09e2d7c7 15734 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15735
15736 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15737 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15738 return;
c0dd20ea
DJ
15739
15740 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15741 if (TYPE_FIELD_NAME (type, 0) == NULL
15742 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15743 || TYPE_FIELD_NAME (type, 1) == NULL
15744 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15745 return;
c0dd20ea
DJ
15746
15747 /* Find the type of the method. */
0b92b5bb 15748 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15749 if (pfn_type == NULL
15750 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15751 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15752 return;
c0dd20ea
DJ
15753
15754 /* Look for the "this" argument. */
15755 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15756 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15757 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15758 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15759 return;
c0dd20ea 15760
09e2d7c7 15761 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15762 new_type = alloc_type (objfile);
09e2d7c7 15763 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15764 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15765 TYPE_VARARGS (pfn_type));
0b92b5bb 15766 smash_to_methodptr_type (type, new_type);
c0dd20ea 15767}
1168df01 15768
685b1105 15769
c906108c 15770/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15771 (definition) to create a type for the structure or union. Fill in
15772 the type's name and general properties; the members will not be
83655187
DE
15773 processed until process_structure_scope. A symbol table entry for
15774 the type will also not be done until process_structure_scope (assuming
15775 the type has a name).
c906108c 15776
c767944b
DJ
15777 NOTE: we need to call these functions regardless of whether or not the
15778 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15779 structure or union. This gets the type entered into our set of
83655187 15780 user defined types. */
c906108c 15781
f792889a 15782static struct type *
134d01f1 15783read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15784{
518817b3 15785 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15786 struct type *type;
15787 struct attribute *attr;
15d034d0 15788 const char *name;
c906108c 15789
348e048f
DE
15790 /* If the definition of this type lives in .debug_types, read that type.
15791 Don't follow DW_AT_specification though, that will take us back up
15792 the chain and we want to go down. */
45e58e77 15793 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
15794 if (attr)
15795 {
ac9ec31b 15796 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15797
ac9ec31b 15798 /* The type's CU may not be the same as CU.
02142a6c 15799 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15800 return set_die_type (die, type, cu);
15801 }
15802
c0dd20ea 15803 type = alloc_type (objfile);
c906108c 15804 INIT_CPLUS_SPECIFIC (type);
93311388 15805
39cbfefa
DJ
15806 name = dwarf2_name (die, cu);
15807 if (name != NULL)
c906108c 15808 {
987504bb 15809 if (cu->language == language_cplus
c44af4eb
TT
15810 || cu->language == language_d
15811 || cu->language == language_rust)
63d06c5c 15812 {
15d034d0 15813 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15814
15815 /* dwarf2_full_name might have already finished building the DIE's
15816 type. If so, there is no need to continue. */
15817 if (get_die_type (die, cu) != NULL)
15818 return get_die_type (die, cu);
15819
15820 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
15821 if (die->tag == DW_TAG_structure_type
15822 || die->tag == DW_TAG_class_type)
15823 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
15824 }
15825 else
15826 {
d8151005
DJ
15827 /* The name is already allocated along with this objfile, so
15828 we don't need to duplicate it for the type. */
7d455152 15829 TYPE_TAG_NAME (type) = name;
94af9270
KS
15830 if (die->tag == DW_TAG_class_type)
15831 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 15832 }
c906108c
SS
15833 }
15834
15835 if (die->tag == DW_TAG_structure_type)
15836 {
15837 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15838 }
15839 else if (die->tag == DW_TAG_union_type)
15840 {
15841 TYPE_CODE (type) = TYPE_CODE_UNION;
15842 }
15843 else
15844 {
4753d33b 15845 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15846 }
15847
0cc2414c
TT
15848 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15849 TYPE_DECLARED_CLASS (type) = 1;
15850
e142c38c 15851 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15852 if (attr)
15853 {
155bfbd3
JB
15854 if (attr_form_is_constant (attr))
15855 TYPE_LENGTH (type) = DW_UNSND (attr);
15856 else
15857 {
15858 /* For the moment, dynamic type sizes are not supported
15859 by GDB's struct type. The actual size is determined
15860 on-demand when resolving the type of a given object,
15861 so set the type's length to zero for now. Otherwise,
15862 we record an expression as the length, and that expression
15863 could lead to a very large value, which could eventually
15864 lead to us trying to allocate that much memory when creating
15865 a value of that type. */
15866 TYPE_LENGTH (type) = 0;
15867 }
c906108c
SS
15868 }
15869 else
15870 {
15871 TYPE_LENGTH (type) = 0;
15872 }
15873
5230b05a 15874 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15875 {
5230b05a
WT
15876 /* ICC<14 does not output the required DW_AT_declaration on
15877 incomplete types, but gives them a size of zero. */
422b1cb0 15878 TYPE_STUB (type) = 1;
685b1105
JK
15879 }
15880 else
15881 TYPE_STUB_SUPPORTED (type) = 1;
15882
dc718098 15883 if (die_is_declaration (die, cu))
876cecd0 15884 TYPE_STUB (type) = 1;
a6c727b2
DJ
15885 else if (attr == NULL && die->child == NULL
15886 && producer_is_realview (cu->producer))
15887 /* RealView does not output the required DW_AT_declaration
15888 on incomplete types. */
15889 TYPE_STUB (type) = 1;
dc718098 15890
c906108c
SS
15891 /* We need to add the type field to the die immediately so we don't
15892 infinitely recurse when dealing with pointers to the structure
0963b4bd 15893 type within the structure itself. */
1c379e20 15894 set_die_type (die, type, cu);
c906108c 15895
7e314c57
JK
15896 /* set_die_type should be already done. */
15897 set_descriptive_type (type, die, cu);
15898
c767944b
DJ
15899 return type;
15900}
15901
15902/* Finish creating a structure or union type, including filling in
15903 its members and creating a symbol for it. */
15904
15905static void
15906process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15907{
518817b3 15908 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15909 struct die_info *child_die;
c767944b
DJ
15910 struct type *type;
15911
15912 type = get_die_type (die, cu);
15913 if (type == NULL)
15914 type = read_structure_type (die, cu);
15915
e142c38c 15916 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15917 {
15918 struct field_info fi;
2f4732b0 15919 std::vector<struct symbol *> template_args;
c767944b 15920 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
15921
15922 memset (&fi, 0, sizeof (struct field_info));
15923
639d11d3 15924 child_die = die->child;
c906108c
SS
15925
15926 while (child_die && child_die->tag)
15927 {
a9a9bd0f
DC
15928 if (child_die->tag == DW_TAG_member
15929 || child_die->tag == DW_TAG_variable)
c906108c 15930 {
a9a9bd0f
DC
15931 /* NOTE: carlton/2002-11-05: A C++ static data member
15932 should be a DW_TAG_member that is a declaration, but
15933 all versions of G++ as of this writing (so through at
15934 least 3.2.1) incorrectly generate DW_TAG_variable
15935 tags for them instead. */
e7c27a73 15936 dwarf2_add_field (&fi, child_die, cu);
c906108c 15937 }
8713b1b1 15938 else if (child_die->tag == DW_TAG_subprogram)
c906108c 15939 {
e98c9e7c
TT
15940 /* Rust doesn't have member functions in the C++ sense.
15941 However, it does emit ordinary functions as children
15942 of a struct DIE. */
15943 if (cu->language == language_rust)
15944 read_func_scope (child_die, cu);
15945 else
15946 {
15947 /* C++ member function. */
15948 dwarf2_add_member_fn (&fi, child_die, type, cu);
15949 }
c906108c
SS
15950 }
15951 else if (child_die->tag == DW_TAG_inheritance)
15952 {
15953 /* C++ base class field. */
e7c27a73 15954 dwarf2_add_field (&fi, child_die, cu);
c906108c 15955 }
883fd55a
KS
15956 else if (type_can_define_types (child_die))
15957 dwarf2_add_type_defn (&fi, child_die, cu);
34eaf542
TT
15958 else if (child_die->tag == DW_TAG_template_type_param
15959 || child_die->tag == DW_TAG_template_value_param)
15960 {
15961 struct symbol *arg = new_symbol (child_die, NULL, cu);
15962
f1078f66 15963 if (arg != NULL)
2f4732b0 15964 template_args.push_back (arg);
34eaf542
TT
15965 }
15966
c906108c
SS
15967 child_die = sibling_die (child_die);
15968 }
15969
34eaf542 15970 /* Attach template arguments to type. */
2f4732b0 15971 if (!template_args.empty ())
34eaf542
TT
15972 {
15973 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15974 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15975 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15976 = XOBNEWVEC (&objfile->objfile_obstack,
15977 struct symbol *,
15978 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15979 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15980 template_args.data (),
34eaf542
TT
15981 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15982 * sizeof (struct symbol *)));
34eaf542
TT
15983 }
15984
c906108c
SS
15985 /* Attach fields and member functions to the type. */
15986 if (fi.nfields)
e7c27a73 15987 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
15988 if (fi.nfnfields)
15989 {
e7c27a73 15990 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15991
c5aa993b 15992 /* Get the type which refers to the base class (possibly this
c906108c 15993 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15994 class from the DW_AT_containing_type attribute. This use of
15995 DW_AT_containing_type is a GNU extension. */
c906108c 15996
e142c38c 15997 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15998 {
e7c27a73 15999 struct type *t = die_containing_type (die, cu);
c906108c 16000
ae6ae975 16001 set_type_vptr_basetype (type, t);
c906108c
SS
16002 if (type == t)
16003 {
c906108c
SS
16004 int i;
16005
16006 /* Our own class provides vtbl ptr. */
16007 for (i = TYPE_NFIELDS (t) - 1;
16008 i >= TYPE_N_BASECLASSES (t);
16009 --i)
16010 {
0d5cff50 16011 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16012
1168df01 16013 if (is_vtable_name (fieldname, cu))
c906108c 16014 {
ae6ae975 16015 set_type_vptr_fieldno (type, i);
c906108c
SS
16016 break;
16017 }
16018 }
16019
16020 /* Complain if virtual function table field not found. */
16021 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 16022 complaint (&symfile_complaints,
3e43a32a
MS
16023 _("virtual function table pointer "
16024 "not found when defining class '%s'"),
4d3c2250
KB
16025 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
16026 "");
c906108c
SS
16027 }
16028 else
16029 {
ae6ae975 16030 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16031 }
16032 }
f6235d4c 16033 else if (cu->producer
61012eef 16034 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16035 {
16036 /* The IBM XLC compiler does not provide direct indication
16037 of the containing type, but the vtable pointer is
16038 always named __vfp. */
16039
16040 int i;
16041
16042 for (i = TYPE_NFIELDS (type) - 1;
16043 i >= TYPE_N_BASECLASSES (type);
16044 --i)
16045 {
16046 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16047 {
ae6ae975
DE
16048 set_type_vptr_fieldno (type, i);
16049 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16050 break;
16051 }
16052 }
16053 }
c906108c 16054 }
98751a41
JK
16055
16056 /* Copy fi.typedef_field_list linked list elements content into the
16057 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16058 if (fi.typedef_field_list)
16059 {
16060 int i = fi.typedef_field_list_count;
16061
a0d7a4ff 16062 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16063 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16064 = ((struct decl_field *)
224c3ddb 16065 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
98751a41
JK
16066 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
16067
16068 /* Reverse the list order to keep the debug info elements order. */
16069 while (--i >= 0)
16070 {
883fd55a 16071 struct decl_field *dest, *src;
6e70227d 16072
98751a41
JK
16073 dest = &TYPE_TYPEDEF_FIELD (type, i);
16074 src = &fi.typedef_field_list->field;
16075 fi.typedef_field_list = fi.typedef_field_list->next;
16076 *dest = *src;
16077 }
16078 }
c767944b 16079
883fd55a
KS
16080 /* Copy fi.nested_types_list linked list elements content into the
16081 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16082 if (fi.nested_types_list != NULL && cu->language != language_ada)
16083 {
16084 int i = fi.nested_types_list_count;
16085
16086 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16087 TYPE_NESTED_TYPES_ARRAY (type)
16088 = ((struct decl_field *)
16089 TYPE_ALLOC (type, sizeof (struct decl_field) * i));
16090 TYPE_NESTED_TYPES_COUNT (type) = i;
16091
16092 /* Reverse the list order to keep the debug info elements order. */
16093 while (--i >= 0)
16094 {
16095 struct decl_field *dest, *src;
16096
16097 dest = &TYPE_NESTED_TYPES_FIELD (type, i);
16098 src = &fi.nested_types_list->field;
16099 fi.nested_types_list = fi.nested_types_list->next;
16100 *dest = *src;
16101 }
16102 }
16103
c767944b 16104 do_cleanups (back_to);
c906108c 16105 }
63d06c5c 16106
bb5ed363 16107 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 16108
90aeadfc
DC
16109 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16110 snapshots) has been known to create a die giving a declaration
16111 for a class that has, as a child, a die giving a definition for a
16112 nested class. So we have to process our children even if the
16113 current die is a declaration. Normally, of course, a declaration
16114 won't have any children at all. */
134d01f1 16115
ca040673
DE
16116 child_die = die->child;
16117
90aeadfc
DC
16118 while (child_die != NULL && child_die->tag)
16119 {
16120 if (child_die->tag == DW_TAG_member
16121 || child_die->tag == DW_TAG_variable
34eaf542
TT
16122 || child_die->tag == DW_TAG_inheritance
16123 || child_die->tag == DW_TAG_template_value_param
16124 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16125 {
90aeadfc 16126 /* Do nothing. */
134d01f1 16127 }
90aeadfc
DC
16128 else
16129 process_die (child_die, cu);
134d01f1 16130
90aeadfc 16131 child_die = sibling_die (child_die);
134d01f1
DJ
16132 }
16133
fa4028e9
JB
16134 /* Do not consider external references. According to the DWARF standard,
16135 these DIEs are identified by the fact that they have no byte_size
16136 attribute, and a declaration attribute. */
16137 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16138 || !die_is_declaration (die, cu))
c767944b 16139 new_symbol (die, type, cu);
134d01f1
DJ
16140}
16141
55426c9d
JB
16142/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16143 update TYPE using some information only available in DIE's children. */
16144
16145static void
16146update_enumeration_type_from_children (struct die_info *die,
16147 struct type *type,
16148 struct dwarf2_cu *cu)
16149{
60f7655a 16150 struct die_info *child_die;
55426c9d
JB
16151 int unsigned_enum = 1;
16152 int flag_enum = 1;
16153 ULONGEST mask = 0;
55426c9d 16154
8268c778 16155 auto_obstack obstack;
55426c9d 16156
60f7655a
DE
16157 for (child_die = die->child;
16158 child_die != NULL && child_die->tag;
16159 child_die = sibling_die (child_die))
55426c9d
JB
16160 {
16161 struct attribute *attr;
16162 LONGEST value;
16163 const gdb_byte *bytes;
16164 struct dwarf2_locexpr_baton *baton;
16165 const char *name;
60f7655a 16166
55426c9d
JB
16167 if (child_die->tag != DW_TAG_enumerator)
16168 continue;
16169
16170 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16171 if (attr == NULL)
16172 continue;
16173
16174 name = dwarf2_name (child_die, cu);
16175 if (name == NULL)
16176 name = "<anonymous enumerator>";
16177
16178 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16179 &value, &bytes, &baton);
16180 if (value < 0)
16181 {
16182 unsigned_enum = 0;
16183 flag_enum = 0;
16184 }
16185 else if ((mask & value) != 0)
16186 flag_enum = 0;
16187 else
16188 mask |= value;
16189
16190 /* If we already know that the enum type is neither unsigned, nor
16191 a flag type, no need to look at the rest of the enumerates. */
16192 if (!unsigned_enum && !flag_enum)
16193 break;
55426c9d
JB
16194 }
16195
16196 if (unsigned_enum)
16197 TYPE_UNSIGNED (type) = 1;
16198 if (flag_enum)
16199 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16200}
16201
134d01f1
DJ
16202/* Given a DW_AT_enumeration_type die, set its type. We do not
16203 complete the type's fields yet, or create any symbols. */
c906108c 16204
f792889a 16205static struct type *
134d01f1 16206read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16207{
518817b3 16208 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16209 struct type *type;
c906108c 16210 struct attribute *attr;
0114d602 16211 const char *name;
134d01f1 16212
348e048f
DE
16213 /* If the definition of this type lives in .debug_types, read that type.
16214 Don't follow DW_AT_specification though, that will take us back up
16215 the chain and we want to go down. */
45e58e77 16216 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
16217 if (attr)
16218 {
ac9ec31b 16219 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16220
ac9ec31b 16221 /* The type's CU may not be the same as CU.
02142a6c 16222 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16223 return set_die_type (die, type, cu);
16224 }
16225
c906108c
SS
16226 type = alloc_type (objfile);
16227
16228 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16229 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16230 if (name != NULL)
7d455152 16231 TYPE_TAG_NAME (type) = name;
c906108c 16232
0626fc76
TT
16233 attr = dwarf2_attr (die, DW_AT_type, cu);
16234 if (attr != NULL)
16235 {
16236 struct type *underlying_type = die_type (die, cu);
16237
16238 TYPE_TARGET_TYPE (type) = underlying_type;
16239 }
16240
e142c38c 16241 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16242 if (attr)
16243 {
16244 TYPE_LENGTH (type) = DW_UNSND (attr);
16245 }
16246 else
16247 {
16248 TYPE_LENGTH (type) = 0;
16249 }
16250
137033e9
JB
16251 /* The enumeration DIE can be incomplete. In Ada, any type can be
16252 declared as private in the package spec, and then defined only
16253 inside the package body. Such types are known as Taft Amendment
16254 Types. When another package uses such a type, an incomplete DIE
16255 may be generated by the compiler. */
02eb380e 16256 if (die_is_declaration (die, cu))
876cecd0 16257 TYPE_STUB (type) = 1;
02eb380e 16258
0626fc76
TT
16259 /* Finish the creation of this type by using the enum's children.
16260 We must call this even when the underlying type has been provided
16261 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16262 update_enumeration_type_from_children (die, type, cu);
16263
0626fc76
TT
16264 /* If this type has an underlying type that is not a stub, then we
16265 may use its attributes. We always use the "unsigned" attribute
16266 in this situation, because ordinarily we guess whether the type
16267 is unsigned -- but the guess can be wrong and the underlying type
16268 can tell us the reality. However, we defer to a local size
16269 attribute if one exists, because this lets the compiler override
16270 the underlying type if needed. */
16271 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16272 {
16273 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16274 if (TYPE_LENGTH (type) == 0)
16275 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16276 }
16277
3d567982
TT
16278 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16279
f792889a 16280 return set_die_type (die, type, cu);
134d01f1
DJ
16281}
16282
16283/* Given a pointer to a die which begins an enumeration, process all
16284 the dies that define the members of the enumeration, and create the
16285 symbol for the enumeration type.
16286
16287 NOTE: We reverse the order of the element list. */
16288
16289static void
16290process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16291{
f792889a 16292 struct type *this_type;
134d01f1 16293
f792889a
DJ
16294 this_type = get_die_type (die, cu);
16295 if (this_type == NULL)
16296 this_type = read_enumeration_type (die, cu);
9dc481d3 16297
639d11d3 16298 if (die->child != NULL)
c906108c 16299 {
9dc481d3
DE
16300 struct die_info *child_die;
16301 struct symbol *sym;
16302 struct field *fields = NULL;
16303 int num_fields = 0;
15d034d0 16304 const char *name;
9dc481d3 16305
639d11d3 16306 child_die = die->child;
c906108c
SS
16307 while (child_die && child_die->tag)
16308 {
16309 if (child_die->tag != DW_TAG_enumerator)
16310 {
e7c27a73 16311 process_die (child_die, cu);
c906108c
SS
16312 }
16313 else
16314 {
39cbfefa
DJ
16315 name = dwarf2_name (child_die, cu);
16316 if (name)
c906108c 16317 {
f792889a 16318 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
16319
16320 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16321 {
16322 fields = (struct field *)
16323 xrealloc (fields,
16324 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 16325 * sizeof (struct field));
c906108c
SS
16326 }
16327
3567439c 16328 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 16329 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 16330 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
16331 FIELD_BITSIZE (fields[num_fields]) = 0;
16332
16333 num_fields++;
16334 }
16335 }
16336
16337 child_die = sibling_die (child_die);
16338 }
16339
16340 if (num_fields)
16341 {
f792889a
DJ
16342 TYPE_NFIELDS (this_type) = num_fields;
16343 TYPE_FIELDS (this_type) = (struct field *)
16344 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16345 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 16346 sizeof (struct field) * num_fields);
b8c9b27d 16347 xfree (fields);
c906108c 16348 }
c906108c 16349 }
134d01f1 16350
6c83ed52
TT
16351 /* If we are reading an enum from a .debug_types unit, and the enum
16352 is a declaration, and the enum is not the signatured type in the
16353 unit, then we do not want to add a symbol for it. Adding a
16354 symbol would in some cases obscure the true definition of the
16355 enum, giving users an incomplete type when the definition is
16356 actually available. Note that we do not want to do this for all
16357 enums which are just declarations, because C++0x allows forward
16358 enum declarations. */
3019eac3 16359 if (cu->per_cu->is_debug_types
6c83ed52
TT
16360 && die_is_declaration (die, cu))
16361 {
52dc124a 16362 struct signatured_type *sig_type;
6c83ed52 16363
c0f78cd4 16364 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16365 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16366 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16367 return;
16368 }
16369
f792889a 16370 new_symbol (die, this_type, cu);
c906108c
SS
16371}
16372
16373/* Extract all information from a DW_TAG_array_type DIE and put it in
16374 the DIE's type field. For now, this only handles one dimensional
16375 arrays. */
16376
f792889a 16377static struct type *
e7c27a73 16378read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16379{
518817b3 16380 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16381 struct die_info *child_die;
7e314c57 16382 struct type *type;
c906108c 16383 struct type *element_type, *range_type, *index_type;
c906108c 16384 struct attribute *attr;
15d034d0 16385 const char *name;
a405673c 16386 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16387 unsigned int bit_stride = 0;
c906108c 16388
e7c27a73 16389 element_type = die_type (die, cu);
c906108c 16390
7e314c57
JK
16391 /* The die_type call above may have already set the type for this DIE. */
16392 type = get_die_type (die, cu);
16393 if (type)
16394 return type;
16395
dc53a7ad
JB
16396 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16397 if (attr != NULL)
a405673c
JB
16398 {
16399 int stride_ok;
16400
16401 byte_stride_prop
16402 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16403 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16404 if (!stride_ok)
16405 {
16406 complaint (&symfile_complaints,
16407 _("unable to read array DW_AT_byte_stride "
16408 " - DIE at 0x%x [in module %s]"),
16409 to_underlying (die->sect_off),
518817b3 16410 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16411 /* Ignore this attribute. We will likely not be able to print
16412 arrays of this type correctly, but there is little we can do
16413 to help if we cannot read the attribute's value. */
16414 byte_stride_prop = NULL;
16415 }
16416 }
dc53a7ad
JB
16417
16418 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16419 if (attr != NULL)
16420 bit_stride = DW_UNSND (attr);
16421
c906108c
SS
16422 /* Irix 6.2 native cc creates array types without children for
16423 arrays with unspecified length. */
639d11d3 16424 if (die->child == NULL)
c906108c 16425 {
46bf5051 16426 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16427 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16428 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16429 byte_stride_prop, bit_stride);
f792889a 16430 return set_die_type (die, type, cu);
c906108c
SS
16431 }
16432
791afaa2 16433 std::vector<struct type *> range_types;
639d11d3 16434 child_die = die->child;
c906108c
SS
16435 while (child_die && child_die->tag)
16436 {
16437 if (child_die->tag == DW_TAG_subrange_type)
16438 {
f792889a 16439 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16440
f792889a 16441 if (child_type != NULL)
a02abb62 16442 {
0963b4bd
MS
16443 /* The range type was succesfully read. Save it for the
16444 array type creation. */
791afaa2 16445 range_types.push_back (child_type);
a02abb62 16446 }
c906108c
SS
16447 }
16448 child_die = sibling_die (child_die);
16449 }
16450
16451 /* Dwarf2 dimensions are output from left to right, create the
16452 necessary array types in backwards order. */
7ca2d3a3 16453
c906108c 16454 type = element_type;
7ca2d3a3
DL
16455
16456 if (read_array_order (die, cu) == DW_ORD_col_major)
16457 {
16458 int i = 0;
9a619af0 16459
791afaa2 16460 while (i < range_types.size ())
dc53a7ad 16461 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16462 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16463 }
16464 else
16465 {
791afaa2 16466 size_t ndim = range_types.size ();
7ca2d3a3 16467 while (ndim-- > 0)
dc53a7ad 16468 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16469 byte_stride_prop, bit_stride);
7ca2d3a3 16470 }
c906108c 16471
f5f8a009
EZ
16472 /* Understand Dwarf2 support for vector types (like they occur on
16473 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16474 array type. This is not part of the Dwarf2/3 standard yet, but a
16475 custom vendor extension. The main difference between a regular
16476 array and the vector variant is that vectors are passed by value
16477 to functions. */
e142c38c 16478 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 16479 if (attr)
ea37ba09 16480 make_vector_type (type);
f5f8a009 16481
dbc98a8b
KW
16482 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16483 implementation may choose to implement triple vectors using this
16484 attribute. */
16485 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16486 if (attr)
16487 {
16488 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16489 TYPE_LENGTH (type) = DW_UNSND (attr);
16490 else
3e43a32a
MS
16491 complaint (&symfile_complaints,
16492 _("DW_AT_byte_size for array type smaller "
16493 "than the total size of elements"));
dbc98a8b
KW
16494 }
16495
39cbfefa
DJ
16496 name = dwarf2_name (die, cu);
16497 if (name)
16498 TYPE_NAME (type) = name;
6e70227d 16499
0963b4bd 16500 /* Install the type in the die. */
7e314c57
JK
16501 set_die_type (die, type, cu);
16502
16503 /* set_die_type should be already done. */
b4ba55a1
JB
16504 set_descriptive_type (type, die, cu);
16505
7e314c57 16506 return type;
c906108c
SS
16507}
16508
7ca2d3a3 16509static enum dwarf_array_dim_ordering
6e70227d 16510read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16511{
16512 struct attribute *attr;
16513
16514 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16515
aead7601
SM
16516 if (attr)
16517 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16518
0963b4bd
MS
16519 /* GNU F77 is a special case, as at 08/2004 array type info is the
16520 opposite order to the dwarf2 specification, but data is still
16521 laid out as per normal fortran.
7ca2d3a3 16522
0963b4bd
MS
16523 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16524 version checking. */
7ca2d3a3 16525
905e0470
PM
16526 if (cu->language == language_fortran
16527 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16528 {
16529 return DW_ORD_row_major;
16530 }
16531
6e70227d 16532 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16533 {
16534 case array_column_major:
16535 return DW_ORD_col_major;
16536 case array_row_major:
16537 default:
16538 return DW_ORD_row_major;
16539 };
16540}
16541
72019c9c 16542/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16543 the DIE's type field. */
72019c9c 16544
f792889a 16545static struct type *
72019c9c
GM
16546read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16547{
7e314c57
JK
16548 struct type *domain_type, *set_type;
16549 struct attribute *attr;
f792889a 16550
7e314c57
JK
16551 domain_type = die_type (die, cu);
16552
16553 /* The die_type call above may have already set the type for this DIE. */
16554 set_type = get_die_type (die, cu);
16555 if (set_type)
16556 return set_type;
16557
16558 set_type = create_set_type (NULL, domain_type);
16559
16560 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
16561 if (attr)
16562 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16563
f792889a 16564 return set_die_type (die, set_type, cu);
72019c9c 16565}
7ca2d3a3 16566
0971de02
TT
16567/* A helper for read_common_block that creates a locexpr baton.
16568 SYM is the symbol which we are marking as computed.
16569 COMMON_DIE is the DIE for the common block.
16570 COMMON_LOC is the location expression attribute for the common
16571 block itself.
16572 MEMBER_LOC is the location expression attribute for the particular
16573 member of the common block that we are processing.
16574 CU is the CU from which the above come. */
16575
16576static void
16577mark_common_block_symbol_computed (struct symbol *sym,
16578 struct die_info *common_die,
16579 struct attribute *common_loc,
16580 struct attribute *member_loc,
16581 struct dwarf2_cu *cu)
16582{
518817b3
SM
16583 struct dwarf2_per_objfile *dwarf2_per_objfile
16584 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16585 struct objfile *objfile = dwarf2_per_objfile->objfile;
16586 struct dwarf2_locexpr_baton *baton;
16587 gdb_byte *ptr;
16588 unsigned int cu_off;
16589 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16590 LONGEST offset = 0;
16591
16592 gdb_assert (common_loc && member_loc);
16593 gdb_assert (attr_form_is_block (common_loc));
16594 gdb_assert (attr_form_is_block (member_loc)
16595 || attr_form_is_constant (member_loc));
16596
8d749320 16597 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16598 baton->per_cu = cu->per_cu;
16599 gdb_assert (baton->per_cu);
16600
16601 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16602
16603 if (attr_form_is_constant (member_loc))
16604 {
16605 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16606 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16607 }
16608 else
16609 baton->size += DW_BLOCK (member_loc)->size;
16610
224c3ddb 16611 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16612 baton->data = ptr;
16613
16614 *ptr++ = DW_OP_call4;
9c541725 16615 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16616 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16617 ptr += 4;
16618
16619 if (attr_form_is_constant (member_loc))
16620 {
16621 *ptr++ = DW_OP_addr;
16622 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16623 ptr += cu->header.addr_size;
16624 }
16625 else
16626 {
16627 /* We have to copy the data here, because DW_OP_call4 will only
16628 use a DW_AT_location attribute. */
16629 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16630 ptr += DW_BLOCK (member_loc)->size;
16631 }
16632
16633 *ptr++ = DW_OP_plus;
16634 gdb_assert (ptr - baton->data == baton->size);
16635
0971de02 16636 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16637 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16638}
16639
4357ac6c
TT
16640/* Create appropriate locally-scoped variables for all the
16641 DW_TAG_common_block entries. Also create a struct common_block
16642 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16643 is used to sepate the common blocks name namespace from regular
16644 variable names. */
c906108c
SS
16645
16646static void
e7c27a73 16647read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16648{
0971de02
TT
16649 struct attribute *attr;
16650
16651 attr = dwarf2_attr (die, DW_AT_location, cu);
16652 if (attr)
16653 {
16654 /* Support the .debug_loc offsets. */
16655 if (attr_form_is_block (attr))
16656 {
16657 /* Ok. */
16658 }
16659 else if (attr_form_is_section_offset (attr))
16660 {
16661 dwarf2_complex_location_expr_complaint ();
16662 attr = NULL;
16663 }
16664 else
16665 {
16666 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16667 "common block member");
16668 attr = NULL;
16669 }
16670 }
16671
639d11d3 16672 if (die->child != NULL)
c906108c 16673 {
518817b3 16674 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16675 struct die_info *child_die;
16676 size_t n_entries = 0, size;
16677 struct common_block *common_block;
16678 struct symbol *sym;
74ac6d43 16679
4357ac6c
TT
16680 for (child_die = die->child;
16681 child_die && child_die->tag;
16682 child_die = sibling_die (child_die))
16683 ++n_entries;
16684
16685 size = (sizeof (struct common_block)
16686 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16687 common_block
16688 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16689 size);
4357ac6c
TT
16690 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16691 common_block->n_entries = 0;
16692
16693 for (child_die = die->child;
16694 child_die && child_die->tag;
16695 child_die = sibling_die (child_die))
16696 {
16697 /* Create the symbol in the DW_TAG_common_block block in the current
16698 symbol scope. */
e7c27a73 16699 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16700 if (sym != NULL)
16701 {
16702 struct attribute *member_loc;
16703
16704 common_block->contents[common_block->n_entries++] = sym;
16705
16706 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16707 cu);
16708 if (member_loc)
16709 {
16710 /* GDB has handled this for a long time, but it is
16711 not specified by DWARF. It seems to have been
16712 emitted by gfortran at least as recently as:
16713 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16714 complaint (&symfile_complaints,
16715 _("Variable in common block has "
16716 "DW_AT_data_member_location "
16717 "- DIE at 0x%x [in module %s]"),
9c541725 16718 to_underlying (child_die->sect_off),
518817b3 16719 objfile_name (objfile));
0971de02
TT
16720
16721 if (attr_form_is_section_offset (member_loc))
16722 dwarf2_complex_location_expr_complaint ();
16723 else if (attr_form_is_constant (member_loc)
16724 || attr_form_is_block (member_loc))
16725 {
16726 if (attr)
16727 mark_common_block_symbol_computed (sym, die, attr,
16728 member_loc, cu);
16729 }
16730 else
16731 dwarf2_complex_location_expr_complaint ();
16732 }
16733 }
c906108c 16734 }
4357ac6c
TT
16735
16736 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16737 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16738 }
16739}
16740
0114d602 16741/* Create a type for a C++ namespace. */
d9fa45fe 16742
0114d602
DJ
16743static struct type *
16744read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16745{
518817b3 16746 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16747 const char *previous_prefix, *name;
9219021c 16748 int is_anonymous;
0114d602
DJ
16749 struct type *type;
16750
16751 /* For extensions, reuse the type of the original namespace. */
16752 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16753 {
16754 struct die_info *ext_die;
16755 struct dwarf2_cu *ext_cu = cu;
9a619af0 16756
0114d602
DJ
16757 ext_die = dwarf2_extension (die, &ext_cu);
16758 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16759
16760 /* EXT_CU may not be the same as CU.
02142a6c 16761 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16762 return set_die_type (die, type, cu);
16763 }
9219021c 16764
e142c38c 16765 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16766
16767 /* Now build the name of the current namespace. */
16768
0114d602
DJ
16769 previous_prefix = determine_prefix (die, cu);
16770 if (previous_prefix[0] != '\0')
16771 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16772 previous_prefix, name, 0, cu);
0114d602
DJ
16773
16774 /* Create the type. */
19f392bc 16775 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602
DJ
16776 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16777
60531b24 16778 return set_die_type (die, type, cu);
0114d602
DJ
16779}
16780
22cee43f 16781/* Read a namespace scope. */
0114d602
DJ
16782
16783static void
16784read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16785{
518817b3 16786 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16787 int is_anonymous;
9219021c 16788
5c4e30ca
DC
16789 /* Add a symbol associated to this if we haven't seen the namespace
16790 before. Also, add a using directive if it's an anonymous
16791 namespace. */
9219021c 16792
f2f0e013 16793 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16794 {
16795 struct type *type;
16796
0114d602 16797 type = read_type_die (die, cu);
e7c27a73 16798 new_symbol (die, type, cu);
5c4e30ca 16799
e8e80198 16800 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16801 if (is_anonymous)
0114d602
DJ
16802 {
16803 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16804
eb1e02fd 16805 std::vector<const char *> excludes;
22cee43f
PMR
16806 add_using_directive (using_directives (cu->language),
16807 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16808 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16809 }
5c4e30ca 16810 }
9219021c 16811
639d11d3 16812 if (die->child != NULL)
d9fa45fe 16813 {
639d11d3 16814 struct die_info *child_die = die->child;
6e70227d 16815
d9fa45fe
DC
16816 while (child_die && child_die->tag)
16817 {
e7c27a73 16818 process_die (child_die, cu);
d9fa45fe
DC
16819 child_die = sibling_die (child_die);
16820 }
16821 }
38d518c9
EZ
16822}
16823
f55ee35c
JK
16824/* Read a Fortran module as type. This DIE can be only a declaration used for
16825 imported module. Still we need that type as local Fortran "use ... only"
16826 declaration imports depend on the created type in determine_prefix. */
16827
16828static struct type *
16829read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16830{
518817b3 16831 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16832 const char *module_name;
f55ee35c
JK
16833 struct type *type;
16834
16835 module_name = dwarf2_name (die, cu);
16836 if (!module_name)
3e43a32a
MS
16837 complaint (&symfile_complaints,
16838 _("DW_TAG_module has no name, offset 0x%x"),
9c541725 16839 to_underlying (die->sect_off));
19f392bc 16840 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c
JK
16841
16842 /* determine_prefix uses TYPE_TAG_NAME. */
16843 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16844
16845 return set_die_type (die, type, cu);
16846}
16847
5d7cb8df
JK
16848/* Read a Fortran module. */
16849
16850static void
16851read_module (struct die_info *die, struct dwarf2_cu *cu)
16852{
16853 struct die_info *child_die = die->child;
530e8392
KB
16854 struct type *type;
16855
16856 type = read_type_die (die, cu);
16857 new_symbol (die, type, cu);
5d7cb8df 16858
5d7cb8df
JK
16859 while (child_die && child_die->tag)
16860 {
16861 process_die (child_die, cu);
16862 child_die = sibling_die (child_die);
16863 }
16864}
16865
38d518c9
EZ
16866/* Return the name of the namespace represented by DIE. Set
16867 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16868 namespace. */
16869
16870static const char *
e142c38c 16871namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16872{
16873 struct die_info *current_die;
16874 const char *name = NULL;
16875
16876 /* Loop through the extensions until we find a name. */
16877
16878 for (current_die = die;
16879 current_die != NULL;
f2f0e013 16880 current_die = dwarf2_extension (die, &cu))
38d518c9 16881 {
96553a0c
DE
16882 /* We don't use dwarf2_name here so that we can detect the absence
16883 of a name -> anonymous namespace. */
7d45c7c3 16884 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16885
38d518c9
EZ
16886 if (name != NULL)
16887 break;
16888 }
16889
16890 /* Is it an anonymous namespace? */
16891
16892 *is_anonymous = (name == NULL);
16893 if (*is_anonymous)
2b1dbab0 16894 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16895
16896 return name;
d9fa45fe
DC
16897}
16898
c906108c
SS
16899/* Extract all information from a DW_TAG_pointer_type DIE and add to
16900 the user defined type vector. */
16901
f792889a 16902static struct type *
e7c27a73 16903read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16904{
518817b3
SM
16905 struct gdbarch *gdbarch
16906 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 16907 struct comp_unit_head *cu_header = &cu->header;
c906108c 16908 struct type *type;
8b2dbe47
KB
16909 struct attribute *attr_byte_size;
16910 struct attribute *attr_address_class;
16911 int byte_size, addr_class;
7e314c57
JK
16912 struct type *target_type;
16913
16914 target_type = die_type (die, cu);
c906108c 16915
7e314c57
JK
16916 /* The die_type call above may have already set the type for this DIE. */
16917 type = get_die_type (die, cu);
16918 if (type)
16919 return type;
16920
16921 type = lookup_pointer_type (target_type);
8b2dbe47 16922
e142c38c 16923 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16924 if (attr_byte_size)
16925 byte_size = DW_UNSND (attr_byte_size);
c906108c 16926 else
8b2dbe47
KB
16927 byte_size = cu_header->addr_size;
16928
e142c38c 16929 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16930 if (attr_address_class)
16931 addr_class = DW_UNSND (attr_address_class);
16932 else
16933 addr_class = DW_ADDR_none;
16934
16935 /* If the pointer size or address class is different than the
16936 default, create a type variant marked as such and set the
16937 length accordingly. */
16938 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 16939 {
5e2b427d 16940 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16941 {
16942 int type_flags;
16943
849957d9 16944 type_flags = gdbarch_address_class_type_flags
5e2b427d 16945 (gdbarch, byte_size, addr_class);
876cecd0
TT
16946 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16947 == 0);
8b2dbe47
KB
16948 type = make_type_with_address_space (type, type_flags);
16949 }
16950 else if (TYPE_LENGTH (type) != byte_size)
16951 {
3e43a32a
MS
16952 complaint (&symfile_complaints,
16953 _("invalid pointer size %d"), byte_size);
8b2dbe47 16954 }
6e70227d 16955 else
9a619af0
MS
16956 {
16957 /* Should we also complain about unhandled address classes? */
16958 }
c906108c 16959 }
8b2dbe47
KB
16960
16961 TYPE_LENGTH (type) = byte_size;
f792889a 16962 return set_die_type (die, type, cu);
c906108c
SS
16963}
16964
16965/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16966 the user defined type vector. */
16967
f792889a 16968static struct type *
e7c27a73 16969read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16970{
16971 struct type *type;
16972 struct type *to_type;
16973 struct type *domain;
16974
e7c27a73
DJ
16975 to_type = die_type (die, cu);
16976 domain = die_containing_type (die, cu);
0d5de010 16977
7e314c57
JK
16978 /* The calls above may have already set the type for this DIE. */
16979 type = get_die_type (die, cu);
16980 if (type)
16981 return type;
16982
0d5de010
DJ
16983 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16984 type = lookup_methodptr_type (to_type);
7078baeb
TT
16985 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16986 {
518817b3
SM
16987 struct type *new_type
16988 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
16989
16990 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16991 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16992 TYPE_VARARGS (to_type));
16993 type = lookup_methodptr_type (new_type);
16994 }
0d5de010
DJ
16995 else
16996 type = lookup_memberptr_type (to_type, domain);
c906108c 16997
f792889a 16998 return set_die_type (die, type, cu);
c906108c
SS
16999}
17000
4297a3f0 17001/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17002 the user defined type vector. */
17003
f792889a 17004static struct type *
4297a3f0
AV
17005read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17006 enum type_code refcode)
c906108c 17007{
e7c27a73 17008 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17009 struct type *type, *target_type;
c906108c
SS
17010 struct attribute *attr;
17011
4297a3f0
AV
17012 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17013
7e314c57
JK
17014 target_type = die_type (die, cu);
17015
17016 /* The die_type call above may have already set the type for this DIE. */
17017 type = get_die_type (die, cu);
17018 if (type)
17019 return type;
17020
4297a3f0 17021 type = lookup_reference_type (target_type, refcode);
e142c38c 17022 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17023 if (attr)
17024 {
17025 TYPE_LENGTH (type) = DW_UNSND (attr);
17026 }
17027 else
17028 {
107d2387 17029 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17030 }
f792889a 17031 return set_die_type (die, type, cu);
c906108c
SS
17032}
17033
cf363f18
MW
17034/* Add the given cv-qualifiers to the element type of the array. GCC
17035 outputs DWARF type qualifiers that apply to an array, not the
17036 element type. But GDB relies on the array element type to carry
17037 the cv-qualifiers. This mimics section 6.7.3 of the C99
17038 specification. */
17039
17040static struct type *
17041add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17042 struct type *base_type, int cnst, int voltl)
17043{
17044 struct type *el_type, *inner_array;
17045
17046 base_type = copy_type (base_type);
17047 inner_array = base_type;
17048
17049 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17050 {
17051 TYPE_TARGET_TYPE (inner_array) =
17052 copy_type (TYPE_TARGET_TYPE (inner_array));
17053 inner_array = TYPE_TARGET_TYPE (inner_array);
17054 }
17055
17056 el_type = TYPE_TARGET_TYPE (inner_array);
17057 cnst |= TYPE_CONST (el_type);
17058 voltl |= TYPE_VOLATILE (el_type);
17059 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17060
17061 return set_die_type (die, base_type, cu);
17062}
17063
f792889a 17064static struct type *
e7c27a73 17065read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17066{
f792889a 17067 struct type *base_type, *cv_type;
c906108c 17068
e7c27a73 17069 base_type = die_type (die, cu);
7e314c57
JK
17070
17071 /* The die_type call above may have already set the type for this DIE. */
17072 cv_type = get_die_type (die, cu);
17073 if (cv_type)
17074 return cv_type;
17075
2f608a3a
KW
17076 /* In case the const qualifier is applied to an array type, the element type
17077 is so qualified, not the array type (section 6.7.3 of C99). */
17078 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 17079 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17080
f792889a
DJ
17081 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17082 return set_die_type (die, cv_type, cu);
c906108c
SS
17083}
17084
f792889a 17085static struct type *
e7c27a73 17086read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17087{
f792889a 17088 struct type *base_type, *cv_type;
c906108c 17089
e7c27a73 17090 base_type = die_type (die, cu);
7e314c57
JK
17091
17092 /* The die_type call above may have already set the type for this DIE. */
17093 cv_type = get_die_type (die, cu);
17094 if (cv_type)
17095 return cv_type;
17096
cf363f18
MW
17097 /* In case the volatile qualifier is applied to an array type, the
17098 element type is so qualified, not the array type (section 6.7.3
17099 of C99). */
17100 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17101 return add_array_cv_type (die, cu, base_type, 0, 1);
17102
f792889a
DJ
17103 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17104 return set_die_type (die, cv_type, cu);
c906108c
SS
17105}
17106
06d66ee9
TT
17107/* Handle DW_TAG_restrict_type. */
17108
17109static struct type *
17110read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17111{
17112 struct type *base_type, *cv_type;
17113
17114 base_type = die_type (die, cu);
17115
17116 /* The die_type call above may have already set the type for this DIE. */
17117 cv_type = get_die_type (die, cu);
17118 if (cv_type)
17119 return cv_type;
17120
17121 cv_type = make_restrict_type (base_type);
17122 return set_die_type (die, cv_type, cu);
17123}
17124
a2c2acaf
MW
17125/* Handle DW_TAG_atomic_type. */
17126
17127static struct type *
17128read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17129{
17130 struct type *base_type, *cv_type;
17131
17132 base_type = die_type (die, cu);
17133
17134 /* The die_type call above may have already set the type for this DIE. */
17135 cv_type = get_die_type (die, cu);
17136 if (cv_type)
17137 return cv_type;
17138
17139 cv_type = make_atomic_type (base_type);
17140 return set_die_type (die, cv_type, cu);
17141}
17142
c906108c
SS
17143/* Extract all information from a DW_TAG_string_type DIE and add to
17144 the user defined type vector. It isn't really a user defined type,
17145 but it behaves like one, with other DIE's using an AT_user_def_type
17146 attribute to reference it. */
17147
f792889a 17148static struct type *
e7c27a73 17149read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17150{
518817b3 17151 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 17152 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17153 struct type *type, *range_type, *index_type, *char_type;
17154 struct attribute *attr;
17155 unsigned int length;
17156
e142c38c 17157 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
17158 if (attr)
17159 {
17160 length = DW_UNSND (attr);
17161 }
17162 else
17163 {
0963b4bd 17164 /* Check for the DW_AT_byte_size attribute. */
e142c38c 17165 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
17166 if (attr)
17167 {
17168 length = DW_UNSND (attr);
17169 }
17170 else
17171 {
17172 length = 1;
17173 }
c906108c 17174 }
6ccb9162 17175
46bf5051 17176 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17177 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
17178 char_type = language_string_char_type (cu->language_defn, gdbarch);
17179 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17180
f792889a 17181 return set_die_type (die, type, cu);
c906108c
SS
17182}
17183
4d804846
JB
17184/* Assuming that DIE corresponds to a function, returns nonzero
17185 if the function is prototyped. */
17186
17187static int
17188prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17189{
17190 struct attribute *attr;
17191
17192 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17193 if (attr && (DW_UNSND (attr) != 0))
17194 return 1;
17195
17196 /* The DWARF standard implies that the DW_AT_prototyped attribute
17197 is only meaninful for C, but the concept also extends to other
17198 languages that allow unprototyped functions (Eg: Objective C).
17199 For all other languages, assume that functions are always
17200 prototyped. */
17201 if (cu->language != language_c
17202 && cu->language != language_objc
17203 && cu->language != language_opencl)
17204 return 1;
17205
17206 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17207 prototyped and unprototyped functions; default to prototyped,
17208 since that is more common in modern code (and RealView warns
17209 about unprototyped functions). */
17210 if (producer_is_realview (cu->producer))
17211 return 1;
17212
17213 return 0;
17214}
17215
c906108c
SS
17216/* Handle DIES due to C code like:
17217
17218 struct foo
c5aa993b
JM
17219 {
17220 int (*funcp)(int a, long l);
17221 int b;
17222 };
c906108c 17223
0963b4bd 17224 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17225
f792889a 17226static struct type *
e7c27a73 17227read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17228{
518817b3 17229 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17230 struct type *type; /* Type that this function returns. */
17231 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17232 struct attribute *attr;
17233
e7c27a73 17234 type = die_type (die, cu);
7e314c57
JK
17235
17236 /* The die_type call above may have already set the type for this DIE. */
17237 ftype = get_die_type (die, cu);
17238 if (ftype)
17239 return ftype;
17240
0c8b41f1 17241 ftype = lookup_function_type (type);
c906108c 17242
4d804846 17243 if (prototyped_function_p (die, cu))
a6c727b2 17244 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17245
c055b101
CV
17246 /* Store the calling convention in the type if it's available in
17247 the subroutine die. Otherwise set the calling convention to
17248 the default value DW_CC_normal. */
17249 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
17250 if (attr)
17251 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17252 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17253 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17254 else
17255 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17256
743649fd
MW
17257 /* Record whether the function returns normally to its caller or not
17258 if the DWARF producer set that information. */
17259 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17260 if (attr && (DW_UNSND (attr) != 0))
17261 TYPE_NO_RETURN (ftype) = 1;
17262
76c10ea2
GM
17263 /* We need to add the subroutine type to the die immediately so
17264 we don't infinitely recurse when dealing with parameters
0963b4bd 17265 declared as the same subroutine type. */
76c10ea2 17266 set_die_type (die, ftype, cu);
6e70227d 17267
639d11d3 17268 if (die->child != NULL)
c906108c 17269 {
bb5ed363 17270 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17271 struct die_info *child_die;
8072405b 17272 int nparams, iparams;
c906108c
SS
17273
17274 /* Count the number of parameters.
17275 FIXME: GDB currently ignores vararg functions, but knows about
17276 vararg member functions. */
8072405b 17277 nparams = 0;
639d11d3 17278 child_die = die->child;
c906108c
SS
17279 while (child_die && child_die->tag)
17280 {
17281 if (child_die->tag == DW_TAG_formal_parameter)
17282 nparams++;
17283 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17284 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17285 child_die = sibling_die (child_die);
17286 }
17287
17288 /* Allocate storage for parameters and fill them in. */
17289 TYPE_NFIELDS (ftype) = nparams;
17290 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17291 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17292
8072405b
JK
17293 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17294 even if we error out during the parameters reading below. */
17295 for (iparams = 0; iparams < nparams; iparams++)
17296 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17297
17298 iparams = 0;
639d11d3 17299 child_die = die->child;
c906108c
SS
17300 while (child_die && child_die->tag)
17301 {
17302 if (child_die->tag == DW_TAG_formal_parameter)
17303 {
3ce3b1ba
PA
17304 struct type *arg_type;
17305
17306 /* DWARF version 2 has no clean way to discern C++
17307 static and non-static member functions. G++ helps
17308 GDB by marking the first parameter for non-static
17309 member functions (which is the this pointer) as
17310 artificial. We pass this information to
17311 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17312
17313 DWARF version 3 added DW_AT_object_pointer, which GCC
17314 4.5 does not yet generate. */
e142c38c 17315 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
17316 if (attr)
17317 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17318 else
9c37b5ae 17319 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17320 arg_type = die_type (child_die, cu);
17321
17322 /* RealView does not mark THIS as const, which the testsuite
17323 expects. GCC marks THIS as const in method definitions,
17324 but not in the class specifications (GCC PR 43053). */
17325 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17326 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17327 {
17328 int is_this = 0;
17329 struct dwarf2_cu *arg_cu = cu;
17330 const char *name = dwarf2_name (child_die, cu);
17331
17332 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17333 if (attr)
17334 {
17335 /* If the compiler emits this, use it. */
17336 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17337 is_this = 1;
17338 }
17339 else if (name && strcmp (name, "this") == 0)
17340 /* Function definitions will have the argument names. */
17341 is_this = 1;
17342 else if (name == NULL && iparams == 0)
17343 /* Declarations may not have the names, so like
17344 elsewhere in GDB, assume an artificial first
17345 argument is "this". */
17346 is_this = 1;
17347
17348 if (is_this)
17349 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17350 arg_type, 0);
17351 }
17352
17353 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17354 iparams++;
17355 }
17356 child_die = sibling_die (child_die);
17357 }
17358 }
17359
76c10ea2 17360 return ftype;
c906108c
SS
17361}
17362
f792889a 17363static struct type *
e7c27a73 17364read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17365{
518817b3 17366 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17367 const char *name = NULL;
3c8e0968 17368 struct type *this_type, *target_type;
c906108c 17369
94af9270 17370 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17371 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17372 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17373 set_die_type (die, this_type, cu);
3c8e0968
DE
17374 target_type = die_type (die, cu);
17375 if (target_type != this_type)
17376 TYPE_TARGET_TYPE (this_type) = target_type;
17377 else
17378 {
17379 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17380 spec and cause infinite loops in GDB. */
17381 complaint (&symfile_complaints,
17382 _("Self-referential DW_TAG_typedef "
17383 "- DIE at 0x%x [in module %s]"),
9c541725 17384 to_underlying (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17385 TYPE_TARGET_TYPE (this_type) = NULL;
17386 }
f792889a 17387 return this_type;
c906108c
SS
17388}
17389
9b790ce7
UW
17390/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17391 (which may be different from NAME) to the architecture back-end to allow
17392 it to guess the correct format if necessary. */
17393
17394static struct type *
17395dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17396 const char *name_hint)
17397{
17398 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17399 const struct floatformat **format;
17400 struct type *type;
17401
17402 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17403 if (format)
17404 type = init_float_type (objfile, bits, name, format);
17405 else
77b7c781 17406 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17407
17408 return type;
17409}
17410
c906108c
SS
17411/* Find a representation of a given base type and install
17412 it in the TYPE field of the die. */
17413
f792889a 17414static struct type *
e7c27a73 17415read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17416{
518817b3 17417 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17418 struct type *type;
17419 struct attribute *attr;
19f392bc 17420 int encoding = 0, bits = 0;
15d034d0 17421 const char *name;
c906108c 17422
e142c38c 17423 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
17424 if (attr)
17425 {
17426 encoding = DW_UNSND (attr);
17427 }
e142c38c 17428 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17429 if (attr)
17430 {
19f392bc 17431 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 17432 }
39cbfefa 17433 name = dwarf2_name (die, cu);
6ccb9162 17434 if (!name)
c906108c 17435 {
6ccb9162
UW
17436 complaint (&symfile_complaints,
17437 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 17438 }
6ccb9162
UW
17439
17440 switch (encoding)
c906108c 17441 {
6ccb9162
UW
17442 case DW_ATE_address:
17443 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17444 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17445 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17446 break;
17447 case DW_ATE_boolean:
19f392bc 17448 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17449 break;
17450 case DW_ATE_complex_float:
9b790ce7 17451 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 17452 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17453 break;
17454 case DW_ATE_decimal_float:
19f392bc 17455 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17456 break;
17457 case DW_ATE_float:
9b790ce7 17458 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
17459 break;
17460 case DW_ATE_signed:
19f392bc 17461 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
17462 break;
17463 case DW_ATE_unsigned:
3b2b8fea
TT
17464 if (cu->language == language_fortran
17465 && name
61012eef 17466 && startswith (name, "character("))
19f392bc
UW
17467 type = init_character_type (objfile, bits, 1, name);
17468 else
17469 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
17470 break;
17471 case DW_ATE_signed_char:
6e70227d 17472 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17473 || cu->language == language_pascal
17474 || cu->language == language_fortran)
19f392bc
UW
17475 type = init_character_type (objfile, bits, 0, name);
17476 else
17477 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
17478 break;
17479 case DW_ATE_unsigned_char:
868a0084 17480 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17481 || cu->language == language_pascal
c44af4eb
TT
17482 || cu->language == language_fortran
17483 || cu->language == language_rust)
19f392bc
UW
17484 type = init_character_type (objfile, bits, 1, name);
17485 else
17486 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 17487 break;
75079b2b 17488 case DW_ATE_UTF:
53e710ac
PA
17489 {
17490 gdbarch *arch = get_objfile_arch (objfile);
17491
17492 if (bits == 16)
17493 type = builtin_type (arch)->builtin_char16;
17494 else if (bits == 32)
17495 type = builtin_type (arch)->builtin_char32;
17496 else
17497 {
17498 complaint (&symfile_complaints,
17499 _("unsupported DW_ATE_UTF bit size: '%d'"),
17500 bits);
17501 type = init_integer_type (objfile, bits, 1, name);
17502 }
17503 return set_die_type (die, type, cu);
17504 }
75079b2b
TT
17505 break;
17506
6ccb9162
UW
17507 default:
17508 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17509 dwarf_type_encoding_name (encoding));
77b7c781 17510 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17511 break;
c906108c 17512 }
6ccb9162 17513
0114d602 17514 if (name && strcmp (name, "char") == 0)
876cecd0 17515 TYPE_NOSIGN (type) = 1;
0114d602 17516
f792889a 17517 return set_die_type (die, type, cu);
c906108c
SS
17518}
17519
80180f79
SA
17520/* Parse dwarf attribute if it's a block, reference or constant and put the
17521 resulting value of the attribute into struct bound_prop.
17522 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17523
17524static int
17525attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17526 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17527{
17528 struct dwarf2_property_baton *baton;
518817b3
SM
17529 struct obstack *obstack
17530 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79
SA
17531
17532 if (attr == NULL || prop == NULL)
17533 return 0;
17534
17535 if (attr_form_is_block (attr))
17536 {
8d749320 17537 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
17538 baton->referenced_type = NULL;
17539 baton->locexpr.per_cu = cu->per_cu;
17540 baton->locexpr.size = DW_BLOCK (attr)->size;
17541 baton->locexpr.data = DW_BLOCK (attr)->data;
17542 prop->data.baton = baton;
17543 prop->kind = PROP_LOCEXPR;
17544 gdb_assert (prop->data.baton != NULL);
17545 }
17546 else if (attr_form_is_ref (attr))
17547 {
17548 struct dwarf2_cu *target_cu = cu;
17549 struct die_info *target_die;
17550 struct attribute *target_attr;
17551
17552 target_die = follow_die_ref (die, attr, &target_cu);
17553 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17554 if (target_attr == NULL)
17555 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17556 target_cu);
80180f79
SA
17557 if (target_attr == NULL)
17558 return 0;
17559
df25ebbd 17560 switch (target_attr->name)
80180f79 17561 {
df25ebbd
JB
17562 case DW_AT_location:
17563 if (attr_form_is_section_offset (target_attr))
17564 {
8d749320 17565 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17566 baton->referenced_type = die_type (target_die, target_cu);
17567 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17568 prop->data.baton = baton;
17569 prop->kind = PROP_LOCLIST;
17570 gdb_assert (prop->data.baton != NULL);
17571 }
17572 else if (attr_form_is_block (target_attr))
17573 {
8d749320 17574 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17575 baton->referenced_type = die_type (target_die, target_cu);
17576 baton->locexpr.per_cu = cu->per_cu;
17577 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17578 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17579 prop->data.baton = baton;
17580 prop->kind = PROP_LOCEXPR;
17581 gdb_assert (prop->data.baton != NULL);
17582 }
17583 else
17584 {
17585 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17586 "dynamic property");
17587 return 0;
17588 }
17589 break;
17590 case DW_AT_data_member_location:
17591 {
17592 LONGEST offset;
17593
17594 if (!handle_data_member_location (target_die, target_cu,
17595 &offset))
17596 return 0;
17597
8d749320 17598 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
17599 baton->referenced_type = read_type_die (target_die->parent,
17600 target_cu);
df25ebbd
JB
17601 baton->offset_info.offset = offset;
17602 baton->offset_info.type = die_type (target_die, target_cu);
17603 prop->data.baton = baton;
17604 prop->kind = PROP_ADDR_OFFSET;
17605 break;
17606 }
80180f79
SA
17607 }
17608 }
17609 else if (attr_form_is_constant (attr))
17610 {
17611 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17612 prop->kind = PROP_CONST;
17613 }
17614 else
17615 {
17616 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17617 dwarf2_name (die, cu));
17618 return 0;
17619 }
17620
17621 return 1;
17622}
17623
a02abb62
JB
17624/* Read the given DW_AT_subrange DIE. */
17625
f792889a 17626static struct type *
a02abb62
JB
17627read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17628{
4c9ad8c2 17629 struct type *base_type, *orig_base_type;
a02abb62
JB
17630 struct type *range_type;
17631 struct attribute *attr;
729efb13 17632 struct dynamic_prop low, high;
4fae6e18 17633 int low_default_is_valid;
c451ebe5 17634 int high_bound_is_count = 0;
15d034d0 17635 const char *name;
43bbcdc2 17636 LONGEST negative_mask;
e77813c8 17637
4c9ad8c2
TT
17638 orig_base_type = die_type (die, cu);
17639 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17640 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17641 creating the range type, but we use the result of check_typedef
17642 when examining properties of the type. */
17643 base_type = check_typedef (orig_base_type);
a02abb62 17644
7e314c57
JK
17645 /* The die_type call above may have already set the type for this DIE. */
17646 range_type = get_die_type (die, cu);
17647 if (range_type)
17648 return range_type;
17649
729efb13
SA
17650 low.kind = PROP_CONST;
17651 high.kind = PROP_CONST;
17652 high.data.const_val = 0;
17653
4fae6e18
JK
17654 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17655 omitting DW_AT_lower_bound. */
17656 switch (cu->language)
6e70227d 17657 {
4fae6e18
JK
17658 case language_c:
17659 case language_cplus:
729efb13 17660 low.data.const_val = 0;
4fae6e18
JK
17661 low_default_is_valid = 1;
17662 break;
17663 case language_fortran:
729efb13 17664 low.data.const_val = 1;
4fae6e18
JK
17665 low_default_is_valid = 1;
17666 break;
17667 case language_d:
4fae6e18 17668 case language_objc:
c44af4eb 17669 case language_rust:
729efb13 17670 low.data.const_val = 0;
4fae6e18
JK
17671 low_default_is_valid = (cu->header.version >= 4);
17672 break;
17673 case language_ada:
17674 case language_m2:
17675 case language_pascal:
729efb13 17676 low.data.const_val = 1;
4fae6e18
JK
17677 low_default_is_valid = (cu->header.version >= 4);
17678 break;
17679 default:
729efb13 17680 low.data.const_val = 0;
4fae6e18
JK
17681 low_default_is_valid = 0;
17682 break;
a02abb62
JB
17683 }
17684
e142c38c 17685 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 17686 if (attr)
11c1ba78 17687 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
17688 else if (!low_default_is_valid)
17689 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
17690 "- DIE at 0x%x [in module %s]"),
e3b94546 17691 to_underlying (die->sect_off),
518817b3 17692 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17693
e142c38c 17694 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 17695 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
17696 {
17697 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 17698 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 17699 {
c451ebe5
SA
17700 /* If bounds are constant do the final calculation here. */
17701 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17702 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17703 else
17704 high_bound_is_count = 1;
c2ff108b 17705 }
e77813c8
PM
17706 }
17707
17708 /* Dwarf-2 specifications explicitly allows to create subrange types
17709 without specifying a base type.
17710 In that case, the base type must be set to the type of
17711 the lower bound, upper bound or count, in that order, if any of these
17712 three attributes references an object that has a type.
17713 If no base type is found, the Dwarf-2 specifications say that
17714 a signed integer type of size equal to the size of an address should
17715 be used.
17716 For the following C code: `extern char gdb_int [];'
17717 GCC produces an empty range DIE.
17718 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 17719 high bound or count are not yet handled by this code. */
e77813c8
PM
17720 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17721 {
518817b3 17722 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e77813c8
PM
17723 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17724 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17725 struct type *int_type = objfile_type (objfile)->builtin_int;
17726
17727 /* Test "int", "long int", and "long long int" objfile types,
17728 and select the first one having a size above or equal to the
17729 architecture address size. */
17730 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17731 base_type = int_type;
17732 else
17733 {
17734 int_type = objfile_type (objfile)->builtin_long;
17735 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17736 base_type = int_type;
17737 else
17738 {
17739 int_type = objfile_type (objfile)->builtin_long_long;
17740 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17741 base_type = int_type;
17742 }
17743 }
17744 }
a02abb62 17745
dbb9c2b1
JB
17746 /* Normally, the DWARF producers are expected to use a signed
17747 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17748 But this is unfortunately not always the case, as witnessed
17749 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17750 is used instead. To work around that ambiguity, we treat
17751 the bounds as signed, and thus sign-extend their values, when
17752 the base type is signed. */
6e70227d 17753 negative_mask =
66c6502d 17754 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17755 if (low.kind == PROP_CONST
17756 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17757 low.data.const_val |= negative_mask;
17758 if (high.kind == PROP_CONST
17759 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17760 high.data.const_val |= negative_mask;
43bbcdc2 17761
729efb13 17762 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 17763
c451ebe5
SA
17764 if (high_bound_is_count)
17765 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17766
c2ff108b
JK
17767 /* Ada expects an empty array on no boundary attributes. */
17768 if (attr == NULL && cu->language != language_ada)
729efb13 17769 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17770
39cbfefa
DJ
17771 name = dwarf2_name (die, cu);
17772 if (name)
17773 TYPE_NAME (range_type) = name;
6e70227d 17774
e142c38c 17775 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
17776 if (attr)
17777 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17778
7e314c57
JK
17779 set_die_type (die, range_type, cu);
17780
17781 /* set_die_type should be already done. */
b4ba55a1
JB
17782 set_descriptive_type (range_type, die, cu);
17783
7e314c57 17784 return range_type;
a02abb62 17785}
6e70227d 17786
f792889a 17787static struct type *
81a17f79
JB
17788read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17789{
17790 struct type *type;
81a17f79 17791
518817b3
SM
17792 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17793 NULL);
0114d602 17794 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17795
74a2f8ff
JB
17796 /* In Ada, an unspecified type is typically used when the description
17797 of the type is defered to a different unit. When encountering
17798 such a type, we treat it as a stub, and try to resolve it later on,
17799 when needed. */
17800 if (cu->language == language_ada)
17801 TYPE_STUB (type) = 1;
17802
f792889a 17803 return set_die_type (die, type, cu);
81a17f79 17804}
a02abb62 17805
639d11d3
DC
17806/* Read a single die and all its descendents. Set the die's sibling
17807 field to NULL; set other fields in the die correctly, and set all
17808 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17809 location of the info_ptr after reading all of those dies. PARENT
17810 is the parent of the die in question. */
17811
17812static struct die_info *
dee91e82 17813read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17814 const gdb_byte *info_ptr,
17815 const gdb_byte **new_info_ptr,
dee91e82 17816 struct die_info *parent)
639d11d3
DC
17817{
17818 struct die_info *die;
d521ce57 17819 const gdb_byte *cur_ptr;
639d11d3
DC
17820 int has_children;
17821
bf6af496 17822 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
17823 if (die == NULL)
17824 {
17825 *new_info_ptr = cur_ptr;
17826 return NULL;
17827 }
93311388 17828 store_in_ref_table (die, reader->cu);
639d11d3
DC
17829
17830 if (has_children)
bf6af496 17831 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17832 else
17833 {
17834 die->child = NULL;
17835 *new_info_ptr = cur_ptr;
17836 }
17837
17838 die->sibling = NULL;
17839 die->parent = parent;
17840 return die;
17841}
17842
17843/* Read a die, all of its descendents, and all of its siblings; set
17844 all of the fields of all of the dies correctly. Arguments are as
17845 in read_die_and_children. */
17846
17847static struct die_info *
bf6af496 17848read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17849 const gdb_byte *info_ptr,
17850 const gdb_byte **new_info_ptr,
bf6af496 17851 struct die_info *parent)
639d11d3
DC
17852{
17853 struct die_info *first_die, *last_sibling;
d521ce57 17854 const gdb_byte *cur_ptr;
639d11d3 17855
c906108c 17856 cur_ptr = info_ptr;
639d11d3
DC
17857 first_die = last_sibling = NULL;
17858
17859 while (1)
c906108c 17860 {
639d11d3 17861 struct die_info *die
dee91e82 17862 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17863
1d325ec1 17864 if (die == NULL)
c906108c 17865 {
639d11d3
DC
17866 *new_info_ptr = cur_ptr;
17867 return first_die;
c906108c 17868 }
1d325ec1
DJ
17869
17870 if (!first_die)
17871 first_die = die;
c906108c 17872 else
1d325ec1
DJ
17873 last_sibling->sibling = die;
17874
17875 last_sibling = die;
c906108c 17876 }
c906108c
SS
17877}
17878
bf6af496
DE
17879/* Read a die, all of its descendents, and all of its siblings; set
17880 all of the fields of all of the dies correctly. Arguments are as
17881 in read_die_and_children.
17882 This the main entry point for reading a DIE and all its children. */
17883
17884static struct die_info *
17885read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17886 const gdb_byte *info_ptr,
17887 const gdb_byte **new_info_ptr,
bf6af496
DE
17888 struct die_info *parent)
17889{
17890 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17891 new_info_ptr, parent);
17892
b4f54984 17893 if (dwarf_die_debug)
bf6af496
DE
17894 {
17895 fprintf_unfiltered (gdb_stdlog,
17896 "Read die from %s@0x%x of %s:\n",
a32a8923 17897 get_section_name (reader->die_section),
bf6af496
DE
17898 (unsigned) (info_ptr - reader->die_section->buffer),
17899 bfd_get_filename (reader->abfd));
b4f54984 17900 dump_die (die, dwarf_die_debug);
bf6af496
DE
17901 }
17902
17903 return die;
17904}
17905
3019eac3
DE
17906/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17907 attributes.
17908 The caller is responsible for filling in the extra attributes
17909 and updating (*DIEP)->num_attrs.
17910 Set DIEP to point to a newly allocated die with its information,
17911 except for its child, sibling, and parent fields.
17912 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 17913
d521ce57 17914static const gdb_byte *
3019eac3 17915read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17916 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 17917 int *has_children, int num_extra_attrs)
93311388 17918{
b64f50a1 17919 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17920 struct abbrev_info *abbrev;
17921 struct die_info *die;
17922 struct dwarf2_cu *cu = reader->cu;
17923 bfd *abfd = reader->abfd;
17924
9c541725 17925 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17926 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17927 info_ptr += bytes_read;
17928 if (!abbrev_number)
17929 {
17930 *diep = NULL;
17931 *has_children = 0;
17932 return info_ptr;
17933 }
17934
685af9cd 17935 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 17936 if (!abbrev)
348e048f
DE
17937 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17938 abbrev_number,
17939 bfd_get_filename (abfd));
17940
3019eac3 17941 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 17942 die->sect_off = sect_off;
93311388
DE
17943 die->tag = abbrev->tag;
17944 die->abbrev = abbrev_number;
17945
3019eac3
DE
17946 /* Make the result usable.
17947 The caller needs to update num_attrs after adding the extra
17948 attributes. */
93311388
DE
17949 die->num_attrs = abbrev->num_attrs;
17950
17951 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
17952 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17953 info_ptr);
93311388
DE
17954
17955 *diep = die;
17956 *has_children = abbrev->has_children;
17957 return info_ptr;
17958}
17959
3019eac3
DE
17960/* Read a die and all its attributes.
17961 Set DIEP to point to a newly allocated die with its information,
17962 except for its child, sibling, and parent fields.
17963 Set HAS_CHILDREN to tell whether the die has children or not. */
17964
d521ce57 17965static const gdb_byte *
3019eac3 17966read_full_die (const struct die_reader_specs *reader,
d521ce57 17967 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
17968 int *has_children)
17969{
d521ce57 17970 const gdb_byte *result;
bf6af496
DE
17971
17972 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
17973
b4f54984 17974 if (dwarf_die_debug)
bf6af496
DE
17975 {
17976 fprintf_unfiltered (gdb_stdlog,
17977 "Read die from %s@0x%x of %s:\n",
a32a8923 17978 get_section_name (reader->die_section),
bf6af496
DE
17979 (unsigned) (info_ptr - reader->die_section->buffer),
17980 bfd_get_filename (reader->abfd));
b4f54984 17981 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
17982 }
17983
17984 return result;
3019eac3 17985}
433df2d4
DE
17986\f
17987/* Abbreviation tables.
3019eac3 17988
433df2d4 17989 In DWARF version 2, the description of the debugging information is
c906108c
SS
17990 stored in a separate .debug_abbrev section. Before we read any
17991 dies from a section we read in all abbreviations and install them
433df2d4
DE
17992 in a hash table. */
17993
17994/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
17995
685af9cd
TT
17996struct abbrev_info *
17997abbrev_table::alloc_abbrev ()
433df2d4
DE
17998{
17999 struct abbrev_info *abbrev;
18000
685af9cd 18001 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
433df2d4 18002 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 18003
433df2d4
DE
18004 return abbrev;
18005}
18006
18007/* Add an abbreviation to the table. */
c906108c 18008
685af9cd
TT
18009void
18010abbrev_table::add_abbrev (unsigned int abbrev_number,
18011 struct abbrev_info *abbrev)
433df2d4
DE
18012{
18013 unsigned int hash_number;
18014
18015 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768
YQ
18016 abbrev->next = m_abbrevs[hash_number];
18017 m_abbrevs[hash_number] = abbrev;
433df2d4 18018}
dee91e82 18019
433df2d4
DE
18020/* Look up an abbrev in the table.
18021 Returns NULL if the abbrev is not found. */
18022
685af9cd
TT
18023struct abbrev_info *
18024abbrev_table::lookup_abbrev (unsigned int abbrev_number)
c906108c 18025{
433df2d4
DE
18026 unsigned int hash_number;
18027 struct abbrev_info *abbrev;
18028
18029 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768 18030 abbrev = m_abbrevs[hash_number];
433df2d4
DE
18031
18032 while (abbrev)
18033 {
18034 if (abbrev->number == abbrev_number)
18035 return abbrev;
18036 abbrev = abbrev->next;
18037 }
18038 return NULL;
18039}
18040
18041/* Read in an abbrev table. */
18042
685af9cd 18043static abbrev_table_up
ed2dc618
SM
18044abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18045 struct dwarf2_section_info *section,
9c541725 18046 sect_offset sect_off)
433df2d4
DE
18047{
18048 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 18049 bfd *abfd = get_section_bfd_owner (section);
d521ce57 18050 const gdb_byte *abbrev_ptr;
c906108c
SS
18051 struct abbrev_info *cur_abbrev;
18052 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 18053 unsigned int abbrev_form;
f3dd6933
DJ
18054 struct attr_abbrev *cur_attrs;
18055 unsigned int allocated_attrs;
c906108c 18056
685af9cd 18057 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
c906108c 18058
433df2d4 18059 dwarf2_read_section (objfile, section);
9c541725 18060 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
18061 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18062 abbrev_ptr += bytes_read;
18063
f3dd6933 18064 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 18065 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 18066
0963b4bd 18067 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
18068 while (abbrev_number)
18069 {
685af9cd 18070 cur_abbrev = abbrev_table->alloc_abbrev ();
c906108c
SS
18071
18072 /* read in abbrev header */
18073 cur_abbrev->number = abbrev_number;
aead7601
SM
18074 cur_abbrev->tag
18075 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
18076 abbrev_ptr += bytes_read;
18077 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18078 abbrev_ptr += 1;
18079
18080 /* now read in declarations */
22d2f3ab 18081 for (;;)
c906108c 18082 {
43988095
JK
18083 LONGEST implicit_const;
18084
22d2f3ab
JK
18085 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18086 abbrev_ptr += bytes_read;
18087 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18088 abbrev_ptr += bytes_read;
43988095
JK
18089 if (abbrev_form == DW_FORM_implicit_const)
18090 {
18091 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18092 &bytes_read);
18093 abbrev_ptr += bytes_read;
18094 }
18095 else
18096 {
18097 /* Initialize it due to a false compiler warning. */
18098 implicit_const = -1;
18099 }
22d2f3ab
JK
18100
18101 if (abbrev_name == 0)
18102 break;
18103
f3dd6933 18104 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 18105 {
f3dd6933
DJ
18106 allocated_attrs += ATTR_ALLOC_CHUNK;
18107 cur_attrs
224c3ddb 18108 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 18109 }
ae038cb0 18110
aead7601
SM
18111 cur_attrs[cur_abbrev->num_attrs].name
18112 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 18113 cur_attrs[cur_abbrev->num_attrs].form
aead7601 18114 = (enum dwarf_form) abbrev_form;
43988095 18115 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 18116 ++cur_abbrev->num_attrs;
c906108c
SS
18117 }
18118
8d749320
SM
18119 cur_abbrev->attrs =
18120 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18121 cur_abbrev->num_attrs);
f3dd6933
DJ
18122 memcpy (cur_abbrev->attrs, cur_attrs,
18123 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18124
685af9cd 18125 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
c906108c
SS
18126
18127 /* Get next abbreviation.
18128 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18129 always properly terminated with an abbrev number of 0.
18130 Exit loop if we encounter an abbreviation which we have
18131 already read (which means we are about to read the abbreviations
18132 for the next compile unit) or if the end of the abbreviation
18133 table is reached. */
433df2d4 18134 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18135 break;
18136 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18137 abbrev_ptr += bytes_read;
685af9cd 18138 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
c906108c
SS
18139 break;
18140 }
f3dd6933
DJ
18141
18142 xfree (cur_attrs);
433df2d4 18143 return abbrev_table;
c906108c
SS
18144}
18145
72bf9492
DJ
18146/* Returns nonzero if TAG represents a type that we might generate a partial
18147 symbol for. */
18148
18149static int
18150is_type_tag_for_partial (int tag)
18151{
18152 switch (tag)
18153 {
18154#if 0
18155 /* Some types that would be reasonable to generate partial symbols for,
18156 that we don't at present. */
18157 case DW_TAG_array_type:
18158 case DW_TAG_file_type:
18159 case DW_TAG_ptr_to_member_type:
18160 case DW_TAG_set_type:
18161 case DW_TAG_string_type:
18162 case DW_TAG_subroutine_type:
18163#endif
18164 case DW_TAG_base_type:
18165 case DW_TAG_class_type:
680b30c7 18166 case DW_TAG_interface_type:
72bf9492
DJ
18167 case DW_TAG_enumeration_type:
18168 case DW_TAG_structure_type:
18169 case DW_TAG_subrange_type:
18170 case DW_TAG_typedef:
18171 case DW_TAG_union_type:
18172 return 1;
18173 default:
18174 return 0;
18175 }
18176}
18177
18178/* Load all DIEs that are interesting for partial symbols into memory. */
18179
18180static struct partial_die_info *
dee91e82 18181load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18182 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18183{
dee91e82 18184 struct dwarf2_cu *cu = reader->cu;
518817b3 18185 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492
DJ
18186 struct partial_die_info *part_die;
18187 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18188 unsigned int bytes_read;
5afb4e99 18189 unsigned int load_all = 0;
72bf9492
DJ
18190 int nesting_level = 1;
18191
18192 parent_die = NULL;
18193 last_die = NULL;
18194
7adf1e79
DE
18195 gdb_assert (cu->per_cu != NULL);
18196 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18197 load_all = 1;
18198
72bf9492
DJ
18199 cu->partial_dies
18200 = htab_create_alloc_ex (cu->header.length / 12,
18201 partial_die_hash,
18202 partial_die_eq,
18203 NULL,
18204 &cu->comp_unit_obstack,
18205 hashtab_obstack_allocate,
18206 dummy_obstack_deallocate);
18207
8d749320 18208 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
18209
18210 while (1)
18211 {
685af9cd 18212 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18213
18214 /* A NULL abbrev means the end of a series of children. */
18215 if (abbrev == NULL)
18216 {
18217 if (--nesting_level == 0)
18218 {
18219 /* PART_DIE was probably the last thing allocated on the
18220 comp_unit_obstack, so we could call obstack_free
18221 here. We don't do that because the waste is small,
18222 and will be cleaned up when we're done with this
18223 compilation unit. This way, we're also more robust
18224 against other users of the comp_unit_obstack. */
18225 return first_die;
18226 }
18227 info_ptr += bytes_read;
18228 last_die = parent_die;
18229 parent_die = parent_die->die_parent;
18230 continue;
18231 }
18232
98bfdba5
PA
18233 /* Check for template arguments. We never save these; if
18234 they're seen, we just mark the parent, and go on our way. */
18235 if (parent_die != NULL
18236 && cu->language == language_cplus
18237 && (abbrev->tag == DW_TAG_template_type_param
18238 || abbrev->tag == DW_TAG_template_value_param))
18239 {
18240 parent_die->has_template_arguments = 1;
18241
18242 if (!load_all)
18243 {
18244 /* We don't need a partial DIE for the template argument. */
dee91e82 18245 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18246 continue;
18247 }
18248 }
18249
0d99eb77 18250 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18251 Skip their other children. */
18252 if (!load_all
18253 && cu->language == language_cplus
18254 && parent_die != NULL
18255 && parent_die->tag == DW_TAG_subprogram)
18256 {
dee91e82 18257 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18258 continue;
18259 }
18260
5afb4e99
DJ
18261 /* Check whether this DIE is interesting enough to save. Normally
18262 we would not be interested in members here, but there may be
18263 later variables referencing them via DW_AT_specification (for
18264 static members). */
18265 if (!load_all
18266 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18267 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18268 && abbrev->tag != DW_TAG_enumerator
18269 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18270 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18271 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18272 && abbrev->tag != DW_TAG_variable
5afb4e99 18273 && abbrev->tag != DW_TAG_namespace
f55ee35c 18274 && abbrev->tag != DW_TAG_module
95554aad 18275 && abbrev->tag != DW_TAG_member
74921315
KS
18276 && abbrev->tag != DW_TAG_imported_unit
18277 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18278 {
18279 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18280 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18281 continue;
18282 }
18283
dee91e82
DE
18284 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
18285 info_ptr);
72bf9492
DJ
18286
18287 /* This two-pass algorithm for processing partial symbols has a
18288 high cost in cache pressure. Thus, handle some simple cases
18289 here which cover the majority of C partial symbols. DIEs
18290 which neither have specification tags in them, nor could have
18291 specification tags elsewhere pointing at them, can simply be
18292 processed and discarded.
18293
18294 This segment is also optional; scan_partial_symbols and
18295 add_partial_symbol will handle these DIEs if we chain
18296 them in normally. When compilers which do not emit large
18297 quantities of duplicate debug information are more common,
18298 this code can probably be removed. */
18299
18300 /* Any complete simple types at the top level (pretty much all
18301 of them, for a language without namespaces), can be processed
18302 directly. */
18303 if (parent_die == NULL
18304 && part_die->has_specification == 0
18305 && part_die->is_declaration == 0
d8228535 18306 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
18307 || part_die->tag == DW_TAG_base_type
18308 || part_die->tag == DW_TAG_subrange_type))
18309 {
18310 if (building_psymtab && part_die->name != NULL)
04a679b8 18311 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 18312 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 18313 &objfile->static_psymbols,
1762568f 18314 0, cu->language, objfile);
dee91e82 18315 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
18316 continue;
18317 }
18318
d8228535
JK
18319 /* The exception for DW_TAG_typedef with has_children above is
18320 a workaround of GCC PR debug/47510. In the case of this complaint
18321 type_name_no_tag_or_error will error on such types later.
18322
18323 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18324 it could not find the child DIEs referenced later, this is checked
18325 above. In correct DWARF DW_TAG_typedef should have no children. */
18326
18327 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
18328 complaint (&symfile_complaints,
18329 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18330 "- DIE at 0x%x [in module %s]"),
9c541725 18331 to_underlying (part_die->sect_off), objfile_name (objfile));
d8228535 18332
72bf9492
DJ
18333 /* If we're at the second level, and we're an enumerator, and
18334 our parent has no specification (meaning possibly lives in a
18335 namespace elsewhere), then we can add the partial symbol now
18336 instead of queueing it. */
18337 if (part_die->tag == DW_TAG_enumerator
18338 && parent_die != NULL
18339 && parent_die->die_parent == NULL
18340 && parent_die->tag == DW_TAG_enumeration_type
18341 && parent_die->has_specification == 0)
18342 {
18343 if (part_die->name == NULL)
3e43a32a
MS
18344 complaint (&symfile_complaints,
18345 _("malformed enumerator DIE ignored"));
72bf9492 18346 else if (building_psymtab)
04a679b8 18347 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 18348 VAR_DOMAIN, LOC_CONST,
9c37b5ae 18349 cu->language == language_cplus
bb5ed363
DE
18350 ? &objfile->global_psymbols
18351 : &objfile->static_psymbols,
1762568f 18352 0, cu->language, objfile);
72bf9492 18353
dee91e82 18354 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
18355 continue;
18356 }
18357
18358 /* We'll save this DIE so link it in. */
18359 part_die->die_parent = parent_die;
18360 part_die->die_sibling = NULL;
18361 part_die->die_child = NULL;
18362
18363 if (last_die && last_die == parent_die)
18364 last_die->die_child = part_die;
18365 else if (last_die)
18366 last_die->die_sibling = part_die;
18367
18368 last_die = part_die;
18369
18370 if (first_die == NULL)
18371 first_die = part_die;
18372
18373 /* Maybe add the DIE to the hash table. Not all DIEs that we
18374 find interesting need to be in the hash table, because we
18375 also have the parent/sibling/child chains; only those that we
18376 might refer to by offset later during partial symbol reading.
18377
18378 For now this means things that might have be the target of a
18379 DW_AT_specification, DW_AT_abstract_origin, or
18380 DW_AT_extension. DW_AT_extension will refer only to
18381 namespaces; DW_AT_abstract_origin refers to functions (and
18382 many things under the function DIE, but we do not recurse
18383 into function DIEs during partial symbol reading) and
18384 possibly variables as well; DW_AT_specification refers to
18385 declarations. Declarations ought to have the DW_AT_declaration
18386 flag. It happens that GCC forgets to put it in sometimes, but
18387 only for functions, not for types.
18388
18389 Adding more things than necessary to the hash table is harmless
18390 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18391 wasted time in find_partial_die, when we reread the compilation
18392 unit with load_all_dies set. */
72bf9492 18393
5afb4e99 18394 if (load_all
72929c62 18395 || abbrev->tag == DW_TAG_constant
5afb4e99 18396 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18397 || abbrev->tag == DW_TAG_variable
18398 || abbrev->tag == DW_TAG_namespace
18399 || part_die->is_declaration)
18400 {
18401 void **slot;
18402
18403 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18404 to_underlying (part_die->sect_off),
18405 INSERT);
72bf9492
DJ
18406 *slot = part_die;
18407 }
18408
8d749320 18409 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
18410
18411 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18412 we have no reason to follow the children of structures; for other
98bfdba5
PA
18413 languages we have to, so that we can get at method physnames
18414 to infer fully qualified class names, for DW_AT_specification,
18415 and for C++ template arguments. For C++, we also look one level
18416 inside functions to find template arguments (if the name of the
18417 function does not already contain the template arguments).
bc30ff58
JB
18418
18419 For Ada, we need to scan the children of subprograms and lexical
18420 blocks as well because Ada allows the definition of nested
18421 entities that could be interesting for the debugger, such as
18422 nested subprograms for instance. */
72bf9492 18423 if (last_die->has_children
5afb4e99
DJ
18424 && (load_all
18425 || last_die->tag == DW_TAG_namespace
f55ee35c 18426 || last_die->tag == DW_TAG_module
72bf9492 18427 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18428 || (cu->language == language_cplus
18429 && last_die->tag == DW_TAG_subprogram
18430 && (last_die->name == NULL
18431 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18432 || (cu->language != language_c
18433 && (last_die->tag == DW_TAG_class_type
680b30c7 18434 || last_die->tag == DW_TAG_interface_type
72bf9492 18435 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
18436 || last_die->tag == DW_TAG_union_type))
18437 || (cu->language == language_ada
18438 && (last_die->tag == DW_TAG_subprogram
18439 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18440 {
18441 nesting_level++;
18442 parent_die = last_die;
18443 continue;
18444 }
18445
18446 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18447 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18448
18449 /* Back to the top, do it again. */
18450 }
18451}
18452
c906108c
SS
18453/* Read a minimal amount of information into the minimal die structure. */
18454
d521ce57 18455static const gdb_byte *
dee91e82
DE
18456read_partial_die (const struct die_reader_specs *reader,
18457 struct partial_die_info *part_die,
18458 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 18459 const gdb_byte *info_ptr)
c906108c 18460{
dee91e82 18461 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18462 struct dwarf2_per_objfile *dwarf2_per_objfile
18463 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18464 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 18465 const gdb_byte *buffer = reader->buffer;
fa238c03 18466 unsigned int i;
c906108c 18467 struct attribute attr;
c5aa993b 18468 int has_low_pc_attr = 0;
c906108c 18469 int has_high_pc_attr = 0;
91da1414 18470 int high_pc_relative = 0;
c906108c 18471
72bf9492 18472 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 18473
9c541725 18474 part_die->sect_off = (sect_offset) (info_ptr - buffer);
72bf9492
DJ
18475
18476 info_ptr += abbrev_len;
18477
18478 if (abbrev == NULL)
18479 return info_ptr;
18480
c906108c
SS
18481 part_die->tag = abbrev->tag;
18482 part_die->has_children = abbrev->has_children;
c906108c
SS
18483
18484 for (i = 0; i < abbrev->num_attrs; ++i)
18485 {
dee91e82 18486 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
18487
18488 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18489 partial symbol table. */
c906108c
SS
18490 switch (attr.name)
18491 {
18492 case DW_AT_name:
71c25dea
TT
18493 switch (part_die->tag)
18494 {
18495 case DW_TAG_compile_unit:
95554aad 18496 case DW_TAG_partial_unit:
348e048f 18497 case DW_TAG_type_unit:
71c25dea
TT
18498 /* Compilation units have a DW_AT_name that is a filename, not
18499 a source language identifier. */
18500 case DW_TAG_enumeration_type:
18501 case DW_TAG_enumerator:
18502 /* These tags always have simple identifiers already; no need
18503 to canonicalize them. */
18504 part_die->name = DW_STRING (&attr);
18505 break;
18506 default:
18507 part_die->name
18508 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
34a68019 18509 &objfile->per_bfd->storage_obstack);
71c25dea
TT
18510 break;
18511 }
c906108c 18512 break;
31ef98ae 18513 case DW_AT_linkage_name:
c906108c 18514 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18515 /* Note that both forms of linkage name might appear. We
18516 assume they will be the same, and we only store the last
18517 one we see. */
94af9270
KS
18518 if (cu->language == language_ada)
18519 part_die->name = DW_STRING (&attr);
abc72ce4 18520 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
18521 break;
18522 case DW_AT_low_pc:
18523 has_low_pc_attr = 1;
31aa7e4e 18524 part_die->lowpc = attr_value_as_address (&attr);
c906108c
SS
18525 break;
18526 case DW_AT_high_pc:
18527 has_high_pc_attr = 1;
31aa7e4e
JB
18528 part_die->highpc = attr_value_as_address (&attr);
18529 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18530 high_pc_relative = 1;
c906108c
SS
18531 break;
18532 case DW_AT_location:
0963b4bd 18533 /* Support the .debug_loc offsets. */
8e19ed76
PS
18534 if (attr_form_is_block (&attr))
18535 {
95554aad 18536 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 18537 }
3690dd37 18538 else if (attr_form_is_section_offset (&attr))
8e19ed76 18539 {
4d3c2250 18540 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18541 }
18542 else
18543 {
4d3c2250
KB
18544 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18545 "partial symbol information");
8e19ed76 18546 }
c906108c 18547 break;
c906108c
SS
18548 case DW_AT_external:
18549 part_die->is_external = DW_UNSND (&attr);
18550 break;
18551 case DW_AT_declaration:
18552 part_die->is_declaration = DW_UNSND (&attr);
18553 break;
18554 case DW_AT_type:
18555 part_die->has_type = 1;
18556 break;
18557 case DW_AT_abstract_origin:
18558 case DW_AT_specification:
72bf9492
DJ
18559 case DW_AT_extension:
18560 part_die->has_specification = 1;
c764a876 18561 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
18562 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18563 || cu->per_cu->is_dwz);
c906108c
SS
18564 break;
18565 case DW_AT_sibling:
18566 /* Ignore absolute siblings, they might point outside of
18567 the current compile unit. */
18568 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
18569 complaint (&symfile_complaints,
18570 _("ignoring absolute DW_AT_sibling"));
c906108c 18571 else
b9502d3f 18572 {
9c541725
PA
18573 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18574 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18575
18576 if (sibling_ptr < info_ptr)
18577 complaint (&symfile_complaints,
18578 _("DW_AT_sibling points backwards"));
22869d73
KS
18579 else if (sibling_ptr > reader->buffer_end)
18580 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
18581 else
18582 part_die->sibling = sibling_ptr;
18583 }
c906108c 18584 break;
fa4028e9
JB
18585 case DW_AT_byte_size:
18586 part_die->has_byte_size = 1;
18587 break;
ff908ebf
AW
18588 case DW_AT_const_value:
18589 part_die->has_const_value = 1;
18590 break;
68511cec
CES
18591 case DW_AT_calling_convention:
18592 /* DWARF doesn't provide a way to identify a program's source-level
18593 entry point. DW_AT_calling_convention attributes are only meant
18594 to describe functions' calling conventions.
18595
18596 However, because it's a necessary piece of information in
0c1b455e
TT
18597 Fortran, and before DWARF 4 DW_CC_program was the only
18598 piece of debugging information whose definition refers to
18599 a 'main program' at all, several compilers marked Fortran
18600 main programs with DW_CC_program --- even when those
18601 functions use the standard calling conventions.
18602
18603 Although DWARF now specifies a way to provide this
18604 information, we support this practice for backward
18605 compatibility. */
68511cec 18606 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e
TT
18607 && cu->language == language_fortran)
18608 part_die->main_subprogram = 1;
68511cec 18609 break;
481860b3
GB
18610 case DW_AT_inline:
18611 if (DW_UNSND (&attr) == DW_INL_inlined
18612 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18613 part_die->may_be_inlined = 1;
18614 break;
95554aad
TT
18615
18616 case DW_AT_import:
18617 if (part_die->tag == DW_TAG_imported_unit)
36586728 18618 {
9c541725 18619 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
36586728
TT
18620 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18621 || cu->per_cu->is_dwz);
18622 }
95554aad
TT
18623 break;
18624
0c1b455e
TT
18625 case DW_AT_main_subprogram:
18626 part_die->main_subprogram = DW_UNSND (&attr);
18627 break;
18628
c906108c
SS
18629 default:
18630 break;
18631 }
18632 }
18633
91da1414
MW
18634 if (high_pc_relative)
18635 part_die->highpc += part_die->lowpc;
18636
9373cf26
JK
18637 if (has_low_pc_attr && has_high_pc_attr)
18638 {
18639 /* When using the GNU linker, .gnu.linkonce. sections are used to
18640 eliminate duplicate copies of functions and vtables and such.
18641 The linker will arbitrarily choose one and discard the others.
18642 The AT_*_pc values for such functions refer to local labels in
18643 these sections. If the section from that file was discarded, the
18644 labels are not in the output, so the relocs get a value of 0.
18645 If this is a discarded function, mark the pc bounds as invalid,
18646 so that GDB will ignore it. */
18647 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18648 {
bb5ed363 18649 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
18650
18651 complaint (&symfile_complaints,
18652 _("DW_AT_low_pc %s is zero "
18653 "for DIE at 0x%x [in module %s]"),
18654 paddress (gdbarch, part_die->lowpc),
9c541725 18655 to_underlying (part_die->sect_off), objfile_name (objfile));
9373cf26
JK
18656 }
18657 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18658 else if (part_die->lowpc >= part_die->highpc)
18659 {
bb5ed363 18660 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
18661
18662 complaint (&symfile_complaints,
18663 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18664 "for DIE at 0x%x [in module %s]"),
18665 paddress (gdbarch, part_die->lowpc),
18666 paddress (gdbarch, part_die->highpc),
9c541725
PA
18667 to_underlying (part_die->sect_off),
18668 objfile_name (objfile));
9373cf26
JK
18669 }
18670 else
18671 part_die->has_pc_info = 1;
18672 }
85cbf3d3 18673
c906108c
SS
18674 return info_ptr;
18675}
18676
72bf9492
DJ
18677/* Find a cached partial DIE at OFFSET in CU. */
18678
18679static struct partial_die_info *
9c541725 18680find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
72bf9492
DJ
18681{
18682 struct partial_die_info *lookup_die = NULL;
18683 struct partial_die_info part_die;
18684
9c541725 18685 part_die.sect_off = sect_off;
9a3c8263
SM
18686 lookup_die = ((struct partial_die_info *)
18687 htab_find_with_hash (cu->partial_dies, &part_die,
9c541725 18688 to_underlying (sect_off)));
72bf9492 18689
72bf9492
DJ
18690 return lookup_die;
18691}
18692
348e048f
DE
18693/* Find a partial DIE at OFFSET, which may or may not be in CU,
18694 except in the case of .debug_types DIEs which do not reference
18695 outside their CU (they do however referencing other types via
55f1336d 18696 DW_FORM_ref_sig8). */
72bf9492
DJ
18697
18698static struct partial_die_info *
9c541725 18699find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18700{
518817b3
SM
18701 struct dwarf2_per_objfile *dwarf2_per_objfile
18702 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18703 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18704 struct dwarf2_per_cu_data *per_cu = NULL;
18705 struct partial_die_info *pd = NULL;
72bf9492 18706
36586728 18707 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 18708 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 18709 {
9c541725 18710 pd = find_partial_die_in_comp_unit (sect_off, cu);
5afb4e99
DJ
18711 if (pd != NULL)
18712 return pd;
0d99eb77
DE
18713 /* We missed recording what we needed.
18714 Load all dies and try again. */
18715 per_cu = cu->per_cu;
5afb4e99 18716 }
0d99eb77
DE
18717 else
18718 {
18719 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18720 if (cu->per_cu->is_debug_types)
0d99eb77 18721 {
9c541725
PA
18722 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
18723 " external reference to offset 0x%x [in module %s].\n"),
18724 to_underlying (cu->header.sect_off), to_underlying (sect_off),
0d99eb77
DE
18725 bfd_get_filename (objfile->obfd));
18726 }
9c541725 18727 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18728 dwarf2_per_objfile);
72bf9492 18729
0d99eb77
DE
18730 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18731 load_partial_comp_unit (per_cu);
ae038cb0 18732
0d99eb77 18733 per_cu->cu->last_used = 0;
9c541725 18734 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
0d99eb77 18735 }
5afb4e99 18736
dee91e82
DE
18737 /* If we didn't find it, and not all dies have been loaded,
18738 load them all and try again. */
18739
5afb4e99
DJ
18740 if (pd == NULL && per_cu->load_all_dies == 0)
18741 {
5afb4e99 18742 per_cu->load_all_dies = 1;
fd820528
DE
18743
18744 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18745 THIS_CU->cu may already be in use. So we can't just free it and
18746 replace its DIEs with the ones we read in. Instead, we leave those
18747 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18748 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18749 set. */
dee91e82 18750 load_partial_comp_unit (per_cu);
5afb4e99 18751
9c541725 18752 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
5afb4e99
DJ
18753 }
18754
18755 if (pd == NULL)
18756 internal_error (__FILE__, __LINE__,
3e43a32a
MS
18757 _("could not find partial DIE 0x%x "
18758 "in cache [from module %s]\n"),
9c541725 18759 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
5afb4e99 18760 return pd;
72bf9492
DJ
18761}
18762
abc72ce4
DE
18763/* See if we can figure out if the class lives in a namespace. We do
18764 this by looking for a member function; its demangled name will
18765 contain namespace info, if there is any. */
18766
18767static void
18768guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18769 struct dwarf2_cu *cu)
18770{
18771 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18772 what template types look like, because the demangler
18773 frequently doesn't give the same name as the debug info. We
18774 could fix this by only using the demangled name to get the
18775 prefix (but see comment in read_structure_type). */
18776
18777 struct partial_die_info *real_pdi;
18778 struct partial_die_info *child_pdi;
18779
18780 /* If this DIE (this DIE's specification, if any) has a parent, then
18781 we should not do this. We'll prepend the parent's fully qualified
18782 name when we create the partial symbol. */
18783
18784 real_pdi = struct_pdi;
18785 while (real_pdi->has_specification)
36586728
TT
18786 real_pdi = find_partial_die (real_pdi->spec_offset,
18787 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
18788
18789 if (real_pdi->die_parent != NULL)
18790 return;
18791
18792 for (child_pdi = struct_pdi->die_child;
18793 child_pdi != NULL;
18794 child_pdi = child_pdi->die_sibling)
18795 {
18796 if (child_pdi->tag == DW_TAG_subprogram
18797 && child_pdi->linkage_name != NULL)
18798 {
18799 char *actual_class_name
18800 = language_class_name_from_physname (cu->language_defn,
18801 child_pdi->linkage_name);
18802 if (actual_class_name != NULL)
18803 {
518817b3 18804 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 18805 struct_pdi->name
224c3ddb 18806 = ((const char *)
e3b94546 18807 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb
SM
18808 actual_class_name,
18809 strlen (actual_class_name)));
abc72ce4
DE
18810 xfree (actual_class_name);
18811 }
18812 break;
18813 }
18814 }
18815}
18816
72bf9492
DJ
18817/* Adjust PART_DIE before generating a symbol for it. This function
18818 may set the is_external flag or change the DIE's name. */
18819
18820static void
18821fixup_partial_die (struct partial_die_info *part_die,
18822 struct dwarf2_cu *cu)
18823{
abc72ce4
DE
18824 /* Once we've fixed up a die, there's no point in doing so again.
18825 This also avoids a memory leak if we were to call
18826 guess_partial_die_structure_name multiple times. */
18827 if (part_die->fixup_called)
18828 return;
18829
72bf9492
DJ
18830 /* If we found a reference attribute and the DIE has no name, try
18831 to find a name in the referred to DIE. */
18832
18833 if (part_die->name == NULL && part_die->has_specification)
18834 {
18835 struct partial_die_info *spec_die;
72bf9492 18836
36586728
TT
18837 spec_die = find_partial_die (part_die->spec_offset,
18838 part_die->spec_is_dwz, cu);
72bf9492 18839
10b3939b 18840 fixup_partial_die (spec_die, cu);
72bf9492
DJ
18841
18842 if (spec_die->name)
18843 {
18844 part_die->name = spec_die->name;
18845
18846 /* Copy DW_AT_external attribute if it is set. */
18847 if (spec_die->is_external)
18848 part_die->is_external = spec_die->is_external;
18849 }
18850 }
18851
18852 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
18853
18854 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 18855 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18856
abc72ce4
DE
18857 /* If there is no parent die to provide a namespace, and there are
18858 children, see if we can determine the namespace from their linkage
122d1940 18859 name. */
abc72ce4 18860 if (cu->language == language_cplus
518817b3
SM
18861 && !VEC_empty (dwarf2_section_info_def,
18862 cu->per_cu->dwarf2_per_objfile->types)
abc72ce4
DE
18863 && part_die->die_parent == NULL
18864 && part_die->has_children
18865 && (part_die->tag == DW_TAG_class_type
18866 || part_die->tag == DW_TAG_structure_type
18867 || part_die->tag == DW_TAG_union_type))
18868 guess_partial_die_structure_name (part_die, cu);
18869
53832f31
TT
18870 /* GCC might emit a nameless struct or union that has a linkage
18871 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18872 if (part_die->name == NULL
96408a79
SA
18873 && (part_die->tag == DW_TAG_class_type
18874 || part_die->tag == DW_TAG_interface_type
18875 || part_die->tag == DW_TAG_structure_type
18876 || part_die->tag == DW_TAG_union_type)
53832f31
TT
18877 && part_die->linkage_name != NULL)
18878 {
18879 char *demangled;
18880
8de20a37 18881 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
18882 if (demangled)
18883 {
96408a79
SA
18884 const char *base;
18885
18886 /* Strip any leading namespaces/classes, keep only the base name.
18887 DW_AT_name for named DIEs does not contain the prefixes. */
18888 base = strrchr (demangled, ':');
18889 if (base && base > demangled && base[-1] == ':')
18890 base++;
18891 else
18892 base = demangled;
18893
518817b3 18894 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 18895 part_die->name
224c3ddb 18896 = ((const char *)
e3b94546 18897 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb 18898 base, strlen (base)));
53832f31
TT
18899 xfree (demangled);
18900 }
18901 }
18902
abc72ce4 18903 part_die->fixup_called = 1;
72bf9492
DJ
18904}
18905
a8329558 18906/* Read an attribute value described by an attribute form. */
c906108c 18907
d521ce57 18908static const gdb_byte *
dee91e82
DE
18909read_attribute_value (const struct die_reader_specs *reader,
18910 struct attribute *attr, unsigned form,
43988095 18911 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 18912{
dee91e82 18913 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18914 struct dwarf2_per_objfile *dwarf2_per_objfile
18915 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18916 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 18917 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 18918 bfd *abfd = reader->abfd;
e7c27a73 18919 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18920 unsigned int bytes_read;
18921 struct dwarf_block *blk;
18922
aead7601 18923 attr->form = (enum dwarf_form) form;
a8329558 18924 switch (form)
c906108c 18925 {
c906108c 18926 case DW_FORM_ref_addr:
ae411497 18927 if (cu->header.version == 2)
4568ecf9 18928 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 18929 else
4568ecf9
DE
18930 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18931 &cu->header, &bytes_read);
ae411497
TT
18932 info_ptr += bytes_read;
18933 break;
36586728
TT
18934 case DW_FORM_GNU_ref_alt:
18935 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18936 info_ptr += bytes_read;
18937 break;
ae411497 18938 case DW_FORM_addr:
e7c27a73 18939 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 18940 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 18941 info_ptr += bytes_read;
c906108c
SS
18942 break;
18943 case DW_FORM_block2:
7b5a2f43 18944 blk = dwarf_alloc_block (cu);
c906108c
SS
18945 blk->size = read_2_bytes (abfd, info_ptr);
18946 info_ptr += 2;
18947 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18948 info_ptr += blk->size;
18949 DW_BLOCK (attr) = blk;
18950 break;
18951 case DW_FORM_block4:
7b5a2f43 18952 blk = dwarf_alloc_block (cu);
c906108c
SS
18953 blk->size = read_4_bytes (abfd, info_ptr);
18954 info_ptr += 4;
18955 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18956 info_ptr += blk->size;
18957 DW_BLOCK (attr) = blk;
18958 break;
18959 case DW_FORM_data2:
18960 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18961 info_ptr += 2;
18962 break;
18963 case DW_FORM_data4:
18964 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18965 info_ptr += 4;
18966 break;
18967 case DW_FORM_data8:
18968 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18969 info_ptr += 8;
18970 break;
0224619f
JK
18971 case DW_FORM_data16:
18972 blk = dwarf_alloc_block (cu);
18973 blk->size = 16;
18974 blk->data = read_n_bytes (abfd, info_ptr, 16);
18975 info_ptr += 16;
18976 DW_BLOCK (attr) = blk;
18977 break;
2dc7f7b3
TT
18978 case DW_FORM_sec_offset:
18979 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18980 info_ptr += bytes_read;
18981 break;
c906108c 18982 case DW_FORM_string:
9b1c24c8 18983 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 18984 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
18985 info_ptr += bytes_read;
18986 break;
4bdf3d34 18987 case DW_FORM_strp:
36586728
TT
18988 if (!cu->per_cu->is_dwz)
18989 {
ed2dc618
SM
18990 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18991 abfd, info_ptr, cu_header,
36586728
TT
18992 &bytes_read);
18993 DW_STRING_IS_CANONICAL (attr) = 0;
18994 info_ptr += bytes_read;
18995 break;
18996 }
18997 /* FALLTHROUGH */
43988095
JK
18998 case DW_FORM_line_strp:
18999 if (!cu->per_cu->is_dwz)
19000 {
ed2dc618
SM
19001 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19002 abfd, info_ptr,
43988095
JK
19003 cu_header, &bytes_read);
19004 DW_STRING_IS_CANONICAL (attr) = 0;
19005 info_ptr += bytes_read;
19006 break;
19007 }
19008 /* FALLTHROUGH */
36586728
TT
19009 case DW_FORM_GNU_strp_alt:
19010 {
ed2dc618 19011 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19012 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19013 &bytes_read);
19014
ed2dc618
SM
19015 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19016 dwz, str_offset);
36586728
TT
19017 DW_STRING_IS_CANONICAL (attr) = 0;
19018 info_ptr += bytes_read;
19019 }
4bdf3d34 19020 break;
2dc7f7b3 19021 case DW_FORM_exprloc:
c906108c 19022 case DW_FORM_block:
7b5a2f43 19023 blk = dwarf_alloc_block (cu);
c906108c
SS
19024 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19025 info_ptr += bytes_read;
19026 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19027 info_ptr += blk->size;
19028 DW_BLOCK (attr) = blk;
19029 break;
19030 case DW_FORM_block1:
7b5a2f43 19031 blk = dwarf_alloc_block (cu);
c906108c
SS
19032 blk->size = read_1_byte (abfd, info_ptr);
19033 info_ptr += 1;
19034 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19035 info_ptr += blk->size;
19036 DW_BLOCK (attr) = blk;
19037 break;
19038 case DW_FORM_data1:
19039 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19040 info_ptr += 1;
19041 break;
19042 case DW_FORM_flag:
19043 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19044 info_ptr += 1;
19045 break;
2dc7f7b3
TT
19046 case DW_FORM_flag_present:
19047 DW_UNSND (attr) = 1;
19048 break;
c906108c
SS
19049 case DW_FORM_sdata:
19050 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19051 info_ptr += bytes_read;
19052 break;
19053 case DW_FORM_udata:
19054 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19055 info_ptr += bytes_read;
19056 break;
19057 case DW_FORM_ref1:
9c541725 19058 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19059 + read_1_byte (abfd, info_ptr));
c906108c
SS
19060 info_ptr += 1;
19061 break;
19062 case DW_FORM_ref2:
9c541725 19063 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19064 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19065 info_ptr += 2;
19066 break;
19067 case DW_FORM_ref4:
9c541725 19068 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19069 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19070 info_ptr += 4;
19071 break;
613e1657 19072 case DW_FORM_ref8:
9c541725 19073 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19074 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19075 info_ptr += 8;
19076 break;
55f1336d 19077 case DW_FORM_ref_sig8:
ac9ec31b 19078 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19079 info_ptr += 8;
19080 break;
c906108c 19081 case DW_FORM_ref_udata:
9c541725 19082 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19083 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19084 info_ptr += bytes_read;
19085 break;
c906108c 19086 case DW_FORM_indirect:
a8329558
KW
19087 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19088 info_ptr += bytes_read;
43988095
JK
19089 if (form == DW_FORM_implicit_const)
19090 {
19091 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19092 info_ptr += bytes_read;
19093 }
19094 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19095 info_ptr);
19096 break;
19097 case DW_FORM_implicit_const:
19098 DW_SND (attr) = implicit_const;
a8329558 19099 break;
3019eac3
DE
19100 case DW_FORM_GNU_addr_index:
19101 if (reader->dwo_file == NULL)
19102 {
19103 /* For now flag a hard error.
19104 Later we can turn this into a complaint. */
19105 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19106 dwarf_form_name (form),
19107 bfd_get_filename (abfd));
19108 }
19109 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19110 info_ptr += bytes_read;
19111 break;
19112 case DW_FORM_GNU_str_index:
19113 if (reader->dwo_file == NULL)
19114 {
19115 /* For now flag a hard error.
19116 Later we can turn this into a complaint if warranted. */
19117 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19118 dwarf_form_name (form),
19119 bfd_get_filename (abfd));
19120 }
19121 {
19122 ULONGEST str_index =
19123 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19124
342587c4 19125 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
19126 DW_STRING_IS_CANONICAL (attr) = 0;
19127 info_ptr += bytes_read;
19128 }
19129 break;
c906108c 19130 default:
8a3fe4f8 19131 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19132 dwarf_form_name (form),
19133 bfd_get_filename (abfd));
c906108c 19134 }
28e94949 19135
36586728 19136 /* Super hack. */
7771576e 19137 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19138 attr->form = DW_FORM_GNU_ref_alt;
19139
28e94949
JB
19140 /* We have seen instances where the compiler tried to emit a byte
19141 size attribute of -1 which ended up being encoded as an unsigned
19142 0xffffffff. Although 0xffffffff is technically a valid size value,
19143 an object of this size seems pretty unlikely so we can relatively
19144 safely treat these cases as if the size attribute was invalid and
19145 treat them as zero by default. */
19146 if (attr->name == DW_AT_byte_size
19147 && form == DW_FORM_data4
19148 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19149 {
19150 complaint
19151 (&symfile_complaints,
43bbcdc2
PH
19152 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19153 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19154 DW_UNSND (attr) = 0;
19155 }
28e94949 19156
c906108c
SS
19157 return info_ptr;
19158}
19159
a8329558
KW
19160/* Read an attribute described by an abbreviated attribute. */
19161
d521ce57 19162static const gdb_byte *
dee91e82
DE
19163read_attribute (const struct die_reader_specs *reader,
19164 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 19165 const gdb_byte *info_ptr)
a8329558
KW
19166{
19167 attr->name = abbrev->name;
43988095
JK
19168 return read_attribute_value (reader, attr, abbrev->form,
19169 abbrev->implicit_const, info_ptr);
a8329558
KW
19170}
19171
0963b4bd 19172/* Read dwarf information from a buffer. */
c906108c
SS
19173
19174static unsigned int
a1855c1d 19175read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19176{
fe1b8b76 19177 return bfd_get_8 (abfd, buf);
c906108c
SS
19178}
19179
19180static int
a1855c1d 19181read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19182{
fe1b8b76 19183 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19184}
19185
19186static unsigned int
a1855c1d 19187read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19188{
fe1b8b76 19189 return bfd_get_16 (abfd, buf);
c906108c
SS
19190}
19191
21ae7a4d 19192static int
a1855c1d 19193read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19194{
19195 return bfd_get_signed_16 (abfd, buf);
19196}
19197
c906108c 19198static unsigned int
a1855c1d 19199read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19200{
fe1b8b76 19201 return bfd_get_32 (abfd, buf);
c906108c
SS
19202}
19203
21ae7a4d 19204static int
a1855c1d 19205read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19206{
19207 return bfd_get_signed_32 (abfd, buf);
19208}
19209
93311388 19210static ULONGEST
a1855c1d 19211read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19212{
fe1b8b76 19213 return bfd_get_64 (abfd, buf);
c906108c
SS
19214}
19215
19216static CORE_ADDR
d521ce57 19217read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19218 unsigned int *bytes_read)
c906108c 19219{
e7c27a73 19220 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19221 CORE_ADDR retval = 0;
19222
107d2387 19223 if (cu_header->signed_addr_p)
c906108c 19224 {
107d2387
AC
19225 switch (cu_header->addr_size)
19226 {
19227 case 2:
fe1b8b76 19228 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19229 break;
19230 case 4:
fe1b8b76 19231 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19232 break;
19233 case 8:
fe1b8b76 19234 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19235 break;
19236 default:
8e65ff28 19237 internal_error (__FILE__, __LINE__,
e2e0b3e5 19238 _("read_address: bad switch, signed [in module %s]"),
659b0389 19239 bfd_get_filename (abfd));
107d2387
AC
19240 }
19241 }
19242 else
19243 {
19244 switch (cu_header->addr_size)
19245 {
19246 case 2:
fe1b8b76 19247 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19248 break;
19249 case 4:
fe1b8b76 19250 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19251 break;
19252 case 8:
fe1b8b76 19253 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19254 break;
19255 default:
8e65ff28 19256 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19257 _("read_address: bad switch, "
19258 "unsigned [in module %s]"),
659b0389 19259 bfd_get_filename (abfd));
107d2387 19260 }
c906108c 19261 }
64367e0a 19262
107d2387
AC
19263 *bytes_read = cu_header->addr_size;
19264 return retval;
c906108c
SS
19265}
19266
f7ef9339 19267/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19268 specification allows the initial length to take up either 4 bytes
19269 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19270 bytes describe the length and all offsets will be 8 bytes in length
19271 instead of 4.
19272
f7ef9339
KB
19273 An older, non-standard 64-bit format is also handled by this
19274 function. The older format in question stores the initial length
19275 as an 8-byte quantity without an escape value. Lengths greater
19276 than 2^32 aren't very common which means that the initial 4 bytes
19277 is almost always zero. Since a length value of zero doesn't make
19278 sense for the 32-bit format, this initial zero can be considered to
19279 be an escape value which indicates the presence of the older 64-bit
19280 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19281 greater than 4GB. If it becomes necessary to handle lengths
19282 somewhat larger than 4GB, we could allow other small values (such
19283 as the non-sensical values of 1, 2, and 3) to also be used as
19284 escape values indicating the presence of the old format.
f7ef9339 19285
917c78fc
MK
19286 The value returned via bytes_read should be used to increment the
19287 relevant pointer after calling read_initial_length().
c764a876 19288
613e1657
KB
19289 [ Note: read_initial_length() and read_offset() are based on the
19290 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19291 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19292 from:
19293
f7ef9339 19294 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19295
613e1657
KB
19296 This document is only a draft and is subject to change. (So beware.)
19297
f7ef9339 19298 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19299 determined empirically by examining 64-bit ELF files produced by
19300 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19301
19302 - Kevin, July 16, 2002
613e1657
KB
19303 ] */
19304
19305static LONGEST
d521ce57 19306read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19307{
fe1b8b76 19308 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19309
dd373385 19310 if (length == 0xffffffff)
613e1657 19311 {
fe1b8b76 19312 length = bfd_get_64 (abfd, buf + 4);
613e1657 19313 *bytes_read = 12;
613e1657 19314 }
dd373385 19315 else if (length == 0)
f7ef9339 19316 {
dd373385 19317 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19318 length = bfd_get_64 (abfd, buf);
f7ef9339 19319 *bytes_read = 8;
f7ef9339 19320 }
613e1657
KB
19321 else
19322 {
19323 *bytes_read = 4;
613e1657
KB
19324 }
19325
c764a876
DE
19326 return length;
19327}
dd373385 19328
c764a876
DE
19329/* Cover function for read_initial_length.
19330 Returns the length of the object at BUF, and stores the size of the
19331 initial length in *BYTES_READ and stores the size that offsets will be in
19332 *OFFSET_SIZE.
19333 If the initial length size is not equivalent to that specified in
19334 CU_HEADER then issue a complaint.
19335 This is useful when reading non-comp-unit headers. */
dd373385 19336
c764a876 19337static LONGEST
d521ce57 19338read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19339 const struct comp_unit_head *cu_header,
19340 unsigned int *bytes_read,
19341 unsigned int *offset_size)
19342{
19343 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19344
19345 gdb_assert (cu_header->initial_length_size == 4
19346 || cu_header->initial_length_size == 8
19347 || cu_header->initial_length_size == 12);
19348
19349 if (cu_header->initial_length_size != *bytes_read)
19350 complaint (&symfile_complaints,
19351 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19352
c764a876 19353 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19354 return length;
613e1657
KB
19355}
19356
19357/* Read an offset from the data stream. The size of the offset is
917c78fc 19358 given by cu_header->offset_size. */
613e1657
KB
19359
19360static LONGEST
d521ce57
TT
19361read_offset (bfd *abfd, const gdb_byte *buf,
19362 const struct comp_unit_head *cu_header,
891d2f0b 19363 unsigned int *bytes_read)
c764a876
DE
19364{
19365 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19366
c764a876
DE
19367 *bytes_read = cu_header->offset_size;
19368 return offset;
19369}
19370
19371/* Read an offset from the data stream. */
19372
19373static LONGEST
d521ce57 19374read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19375{
19376 LONGEST retval = 0;
19377
c764a876 19378 switch (offset_size)
613e1657
KB
19379 {
19380 case 4:
fe1b8b76 19381 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19382 break;
19383 case 8:
fe1b8b76 19384 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19385 break;
19386 default:
8e65ff28 19387 internal_error (__FILE__, __LINE__,
c764a876 19388 _("read_offset_1: bad switch [in module %s]"),
659b0389 19389 bfd_get_filename (abfd));
613e1657
KB
19390 }
19391
917c78fc 19392 return retval;
613e1657
KB
19393}
19394
d521ce57
TT
19395static const gdb_byte *
19396read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19397{
19398 /* If the size of a host char is 8 bits, we can return a pointer
19399 to the buffer, otherwise we have to copy the data to a buffer
19400 allocated on the temporary obstack. */
4bdf3d34 19401 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19402 return buf;
c906108c
SS
19403}
19404
d521ce57
TT
19405static const char *
19406read_direct_string (bfd *abfd, const gdb_byte *buf,
19407 unsigned int *bytes_read_ptr)
c906108c
SS
19408{
19409 /* If the size of a host char is 8 bits, we can return a pointer
19410 to the string, otherwise we have to copy the string to a buffer
19411 allocated on the temporary obstack. */
4bdf3d34 19412 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19413 if (*buf == '\0')
19414 {
19415 *bytes_read_ptr = 1;
19416 return NULL;
19417 }
d521ce57
TT
19418 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19419 return (const char *) buf;
4bdf3d34
JJ
19420}
19421
43988095
JK
19422/* Return pointer to string at section SECT offset STR_OFFSET with error
19423 reporting strings FORM_NAME and SECT_NAME. */
19424
d521ce57 19425static const char *
ed2dc618
SM
19426read_indirect_string_at_offset_from (struct objfile *objfile,
19427 bfd *abfd, LONGEST str_offset,
43988095
JK
19428 struct dwarf2_section_info *sect,
19429 const char *form_name,
19430 const char *sect_name)
19431{
ed2dc618 19432 dwarf2_read_section (objfile, sect);
43988095
JK
19433 if (sect->buffer == NULL)
19434 error (_("%s used without %s section [in module %s]"),
19435 form_name, sect_name, bfd_get_filename (abfd));
19436 if (str_offset >= sect->size)
19437 error (_("%s pointing outside of %s section [in module %s]"),
19438 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19439 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19440 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19441 return NULL;
43988095
JK
19442 return (const char *) (sect->buffer + str_offset);
19443}
19444
19445/* Return pointer to string at .debug_str offset STR_OFFSET. */
19446
19447static const char *
ed2dc618
SM
19448read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19449 bfd *abfd, LONGEST str_offset)
43988095 19450{
ed2dc618
SM
19451 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19452 abfd, str_offset,
43988095
JK
19453 &dwarf2_per_objfile->str,
19454 "DW_FORM_strp", ".debug_str");
19455}
19456
19457/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19458
19459static const char *
ed2dc618
SM
19460read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19461 bfd *abfd, LONGEST str_offset)
43988095 19462{
ed2dc618
SM
19463 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19464 abfd, str_offset,
43988095
JK
19465 &dwarf2_per_objfile->line_str,
19466 "DW_FORM_line_strp",
19467 ".debug_line_str");
c906108c
SS
19468}
19469
36586728
TT
19470/* Read a string at offset STR_OFFSET in the .debug_str section from
19471 the .dwz file DWZ. Throw an error if the offset is too large. If
19472 the string consists of a single NUL byte, return NULL; otherwise
19473 return a pointer to the string. */
19474
d521ce57 19475static const char *
ed2dc618
SM
19476read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19477 LONGEST str_offset)
36586728 19478{
ed2dc618 19479 dwarf2_read_section (objfile, &dwz->str);
36586728
TT
19480
19481 if (dwz->str.buffer == NULL)
19482 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19483 "section [in module %s]"),
19484 bfd_get_filename (dwz->dwz_bfd));
19485 if (str_offset >= dwz->str.size)
19486 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19487 ".debug_str section [in module %s]"),
19488 bfd_get_filename (dwz->dwz_bfd));
19489 gdb_assert (HOST_CHAR_BIT == 8);
19490 if (dwz->str.buffer[str_offset] == '\0')
19491 return NULL;
d521ce57 19492 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
19493}
19494
43988095
JK
19495/* Return pointer to string at .debug_str offset as read from BUF.
19496 BUF is assumed to be in a compilation unit described by CU_HEADER.
19497 Return *BYTES_READ_PTR count of bytes read from BUF. */
19498
d521ce57 19499static const char *
ed2dc618
SM
19500read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19501 const gdb_byte *buf,
cf2c3c16
TT
19502 const struct comp_unit_head *cu_header,
19503 unsigned int *bytes_read_ptr)
19504{
19505 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19506
ed2dc618 19507 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
19508}
19509
43988095
JK
19510/* Return pointer to string at .debug_line_str offset as read from BUF.
19511 BUF is assumed to be in a compilation unit described by CU_HEADER.
19512 Return *BYTES_READ_PTR count of bytes read from BUF. */
19513
19514static const char *
ed2dc618
SM
19515read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19516 bfd *abfd, const gdb_byte *buf,
43988095
JK
19517 const struct comp_unit_head *cu_header,
19518 unsigned int *bytes_read_ptr)
19519{
19520 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19521
ed2dc618
SM
19522 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19523 str_offset);
43988095
JK
19524}
19525
19526ULONGEST
d521ce57 19527read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 19528 unsigned int *bytes_read_ptr)
c906108c 19529{
12df843f 19530 ULONGEST result;
ce5d95e1 19531 unsigned int num_read;
870f88f7 19532 int shift;
c906108c
SS
19533 unsigned char byte;
19534
19535 result = 0;
19536 shift = 0;
19537 num_read = 0;
c906108c
SS
19538 while (1)
19539 {
fe1b8b76 19540 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19541 buf++;
19542 num_read++;
12df843f 19543 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19544 if ((byte & 128) == 0)
19545 {
19546 break;
19547 }
19548 shift += 7;
19549 }
19550 *bytes_read_ptr = num_read;
19551 return result;
19552}
19553
12df843f 19554static LONGEST
d521ce57
TT
19555read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19556 unsigned int *bytes_read_ptr)
c906108c 19557{
12df843f 19558 LONGEST result;
870f88f7 19559 int shift, num_read;
c906108c
SS
19560 unsigned char byte;
19561
19562 result = 0;
19563 shift = 0;
c906108c 19564 num_read = 0;
c906108c
SS
19565 while (1)
19566 {
fe1b8b76 19567 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19568 buf++;
19569 num_read++;
12df843f 19570 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
19571 shift += 7;
19572 if ((byte & 128) == 0)
19573 {
19574 break;
19575 }
19576 }
77e0b926 19577 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 19578 result |= -(((LONGEST) 1) << shift);
c906108c
SS
19579 *bytes_read_ptr = num_read;
19580 return result;
19581}
19582
3019eac3
DE
19583/* Given index ADDR_INDEX in .debug_addr, fetch the value.
19584 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19585 ADDR_SIZE is the size of addresses from the CU header. */
19586
19587static CORE_ADDR
ed2dc618
SM
19588read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19589 unsigned int addr_index, ULONGEST addr_base, int addr_size)
3019eac3
DE
19590{
19591 struct objfile *objfile = dwarf2_per_objfile->objfile;
19592 bfd *abfd = objfile->obfd;
19593 const gdb_byte *info_ptr;
19594
19595 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19596 if (dwarf2_per_objfile->addr.buffer == NULL)
19597 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19598 objfile_name (objfile));
3019eac3
DE
19599 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19600 error (_("DW_FORM_addr_index pointing outside of "
19601 ".debug_addr section [in module %s]"),
4262abfb 19602 objfile_name (objfile));
3019eac3
DE
19603 info_ptr = (dwarf2_per_objfile->addr.buffer
19604 + addr_base + addr_index * addr_size);
19605 if (addr_size == 4)
19606 return bfd_get_32 (abfd, info_ptr);
19607 else
19608 return bfd_get_64 (abfd, info_ptr);
19609}
19610
19611/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19612
19613static CORE_ADDR
19614read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19615{
518817b3
SM
19616 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19617 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19618}
19619
19620/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19621
19622static CORE_ADDR
d521ce57 19623read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19624 unsigned int *bytes_read)
19625{
518817b3 19626 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
19627 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19628
19629 return read_addr_index (cu, addr_index);
19630}
19631
19632/* Data structure to pass results from dwarf2_read_addr_index_reader
19633 back to dwarf2_read_addr_index. */
19634
19635struct dwarf2_read_addr_index_data
19636{
19637 ULONGEST addr_base;
19638 int addr_size;
19639};
19640
19641/* die_reader_func for dwarf2_read_addr_index. */
19642
19643static void
19644dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 19645 const gdb_byte *info_ptr,
3019eac3
DE
19646 struct die_info *comp_unit_die,
19647 int has_children,
19648 void *data)
19649{
19650 struct dwarf2_cu *cu = reader->cu;
19651 struct dwarf2_read_addr_index_data *aidata =
19652 (struct dwarf2_read_addr_index_data *) data;
19653
19654 aidata->addr_base = cu->addr_base;
19655 aidata->addr_size = cu->header.addr_size;
19656}
19657
19658/* Given an index in .debug_addr, fetch the value.
19659 NOTE: This can be called during dwarf expression evaluation,
19660 long after the debug information has been read, and thus per_cu->cu
19661 may no longer exist. */
19662
19663CORE_ADDR
19664dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19665 unsigned int addr_index)
19666{
ed2dc618
SM
19667 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19668 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
19669 struct dwarf2_cu *cu = per_cu->cu;
19670 ULONGEST addr_base;
19671 int addr_size;
19672
3019eac3
DE
19673 /* We need addr_base and addr_size.
19674 If we don't have PER_CU->cu, we have to get it.
19675 Nasty, but the alternative is storing the needed info in PER_CU,
19676 which at this point doesn't seem justified: it's not clear how frequently
19677 it would get used and it would increase the size of every PER_CU.
19678 Entry points like dwarf2_per_cu_addr_size do a similar thing
19679 so we're not in uncharted territory here.
19680 Alas we need to be a bit more complicated as addr_base is contained
19681 in the DIE.
19682
19683 We don't need to read the entire CU(/TU).
19684 We just need the header and top level die.
a1b64ce1 19685
3019eac3 19686 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19687 For now we skip this optimization. */
3019eac3
DE
19688
19689 if (cu != NULL)
19690 {
19691 addr_base = cu->addr_base;
19692 addr_size = cu->header.addr_size;
19693 }
19694 else
19695 {
19696 struct dwarf2_read_addr_index_data aidata;
19697
a1b64ce1
DE
19698 /* Note: We can't use init_cutu_and_read_dies_simple here,
19699 we need addr_base. */
19700 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
19701 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
19702 addr_base = aidata.addr_base;
19703 addr_size = aidata.addr_size;
19704 }
19705
ed2dc618
SM
19706 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19707 addr_size);
3019eac3
DE
19708}
19709
57d63ce2
DE
19710/* Given a DW_FORM_GNU_str_index, fetch the string.
19711 This is only used by the Fission support. */
3019eac3 19712
d521ce57 19713static const char *
342587c4 19714read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3 19715{
ed2dc618 19716 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19717 struct dwarf2_per_objfile *dwarf2_per_objfile
19718 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19719 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19720 const char *objf_name = objfile_name (objfile);
3019eac3 19721 bfd *abfd = objfile->obfd;
73869dc2
DE
19722 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19723 struct dwarf2_section_info *str_offsets_section =
19724 &reader->dwo_file->sections.str_offsets;
d521ce57 19725 const gdb_byte *info_ptr;
3019eac3 19726 ULONGEST str_offset;
57d63ce2 19727 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 19728
73869dc2
DE
19729 dwarf2_read_section (objfile, str_section);
19730 dwarf2_read_section (objfile, str_offsets_section);
19731 if (str_section->buffer == NULL)
57d63ce2 19732 error (_("%s used without .debug_str.dwo section"
9c541725
PA
19733 " in CU at offset 0x%x [in module %s]"),
19734 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 19735 if (str_offsets_section->buffer == NULL)
57d63ce2 19736 error (_("%s used without .debug_str_offsets.dwo section"
9c541725
PA
19737 " in CU at offset 0x%x [in module %s]"),
19738 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 19739 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 19740 error (_("%s pointing outside of .debug_str_offsets.dwo"
9c541725
PA
19741 " section in CU at offset 0x%x [in module %s]"),
19742 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 19743 info_ptr = (str_offsets_section->buffer
3019eac3
DE
19744 + str_index * cu->header.offset_size);
19745 if (cu->header.offset_size == 4)
19746 str_offset = bfd_get_32 (abfd, info_ptr);
19747 else
19748 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19749 if (str_offset >= str_section->size)
57d63ce2 19750 error (_("Offset from %s pointing outside of"
9c541725
PA
19751 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
19752 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 19753 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19754}
19755
3019eac3
DE
19756/* Return the length of an LEB128 number in BUF. */
19757
19758static int
19759leb128_size (const gdb_byte *buf)
19760{
19761 const gdb_byte *begin = buf;
19762 gdb_byte byte;
19763
19764 while (1)
19765 {
19766 byte = *buf++;
19767 if ((byte & 128) == 0)
19768 return buf - begin;
19769 }
19770}
19771
c906108c 19772static void
e142c38c 19773set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19774{
19775 switch (lang)
19776 {
19777 case DW_LANG_C89:
76bee0cc 19778 case DW_LANG_C99:
0cfd832f 19779 case DW_LANG_C11:
c906108c 19780 case DW_LANG_C:
d1be3247 19781 case DW_LANG_UPC:
e142c38c 19782 cu->language = language_c;
c906108c 19783 break;
9c37b5ae 19784 case DW_LANG_Java:
c906108c 19785 case DW_LANG_C_plus_plus:
0cfd832f
MW
19786 case DW_LANG_C_plus_plus_11:
19787 case DW_LANG_C_plus_plus_14:
e142c38c 19788 cu->language = language_cplus;
c906108c 19789 break;
6aecb9c2
JB
19790 case DW_LANG_D:
19791 cu->language = language_d;
19792 break;
c906108c
SS
19793 case DW_LANG_Fortran77:
19794 case DW_LANG_Fortran90:
b21b22e0 19795 case DW_LANG_Fortran95:
f7de9aab
MW
19796 case DW_LANG_Fortran03:
19797 case DW_LANG_Fortran08:
e142c38c 19798 cu->language = language_fortran;
c906108c 19799 break;
a766d390
DE
19800 case DW_LANG_Go:
19801 cu->language = language_go;
19802 break;
c906108c 19803 case DW_LANG_Mips_Assembler:
e142c38c 19804 cu->language = language_asm;
c906108c
SS
19805 break;
19806 case DW_LANG_Ada83:
8aaf0b47 19807 case DW_LANG_Ada95:
bc5f45f8
JB
19808 cu->language = language_ada;
19809 break;
72019c9c
GM
19810 case DW_LANG_Modula2:
19811 cu->language = language_m2;
19812 break;
fe8e67fd
PM
19813 case DW_LANG_Pascal83:
19814 cu->language = language_pascal;
19815 break;
22566fbd
DJ
19816 case DW_LANG_ObjC:
19817 cu->language = language_objc;
19818 break;
c44af4eb
TT
19819 case DW_LANG_Rust:
19820 case DW_LANG_Rust_old:
19821 cu->language = language_rust;
19822 break;
c906108c
SS
19823 case DW_LANG_Cobol74:
19824 case DW_LANG_Cobol85:
c906108c 19825 default:
e142c38c 19826 cu->language = language_minimal;
c906108c
SS
19827 break;
19828 }
e142c38c 19829 cu->language_defn = language_def (cu->language);
c906108c
SS
19830}
19831
19832/* Return the named attribute or NULL if not there. */
19833
19834static struct attribute *
e142c38c 19835dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19836{
a48e046c 19837 for (;;)
c906108c 19838 {
a48e046c
TT
19839 unsigned int i;
19840 struct attribute *spec = NULL;
19841
19842 for (i = 0; i < die->num_attrs; ++i)
19843 {
19844 if (die->attrs[i].name == name)
19845 return &die->attrs[i];
19846 if (die->attrs[i].name == DW_AT_specification
19847 || die->attrs[i].name == DW_AT_abstract_origin)
19848 spec = &die->attrs[i];
19849 }
19850
19851 if (!spec)
19852 break;
c906108c 19853
f2f0e013 19854 die = follow_die_ref (die, spec, &cu);
f2f0e013 19855 }
c5aa993b 19856
c906108c
SS
19857 return NULL;
19858}
19859
348e048f
DE
19860/* Return the named attribute or NULL if not there,
19861 but do not follow DW_AT_specification, etc.
19862 This is for use in contexts where we're reading .debug_types dies.
19863 Following DW_AT_specification, DW_AT_abstract_origin will take us
19864 back up the chain, and we want to go down. */
19865
19866static struct attribute *
45e58e77 19867dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
19868{
19869 unsigned int i;
19870
19871 for (i = 0; i < die->num_attrs; ++i)
19872 if (die->attrs[i].name == name)
19873 return &die->attrs[i];
19874
19875 return NULL;
19876}
19877
7d45c7c3
KB
19878/* Return the string associated with a string-typed attribute, or NULL if it
19879 is either not found or is of an incorrect type. */
19880
19881static const char *
19882dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19883{
19884 struct attribute *attr;
19885 const char *str = NULL;
19886
19887 attr = dwarf2_attr (die, name, cu);
19888
19889 if (attr != NULL)
19890 {
43988095 19891 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438
L
19892 || attr->form == DW_FORM_string
19893 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 19894 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
19895 str = DW_STRING (attr);
19896 else
19897 complaint (&symfile_complaints,
19898 _("string type expected for attribute %s for "
19899 "DIE at 0x%x in module %s"),
9c541725 19900 dwarf_attr_name (name), to_underlying (die->sect_off),
518817b3 19901 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
19902 }
19903
19904 return str;
19905}
19906
05cf31d1
JB
19907/* Return non-zero iff the attribute NAME is defined for the given DIE,
19908 and holds a non-zero value. This function should only be used for
2dc7f7b3 19909 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19910
19911static int
19912dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19913{
19914 struct attribute *attr = dwarf2_attr (die, name, cu);
19915
19916 return (attr && DW_UNSND (attr));
19917}
19918
3ca72b44 19919static int
e142c38c 19920die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19921{
05cf31d1
JB
19922 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19923 which value is non-zero. However, we have to be careful with
19924 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19925 (via dwarf2_flag_true_p) follows this attribute. So we may
19926 end up accidently finding a declaration attribute that belongs
19927 to a different DIE referenced by the specification attribute,
19928 even though the given DIE does not have a declaration attribute. */
19929 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19930 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19931}
19932
63d06c5c 19933/* Return the die giving the specification for DIE, if there is
f2f0e013 19934 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19935 containing the return value on output. If there is no
19936 specification, but there is an abstract origin, that is
19937 returned. */
63d06c5c
DC
19938
19939static struct die_info *
f2f0e013 19940die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19941{
f2f0e013
DJ
19942 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19943 *spec_cu);
63d06c5c 19944
edb3359d
DJ
19945 if (spec_attr == NULL)
19946 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19947
63d06c5c
DC
19948 if (spec_attr == NULL)
19949 return NULL;
19950 else
f2f0e013 19951 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19952}
c906108c 19953
527f3840
JK
19954/* Stub for free_line_header to match void * callback types. */
19955
19956static void
19957free_line_header_voidp (void *arg)
19958{
9a3c8263 19959 struct line_header *lh = (struct line_header *) arg;
527f3840 19960
fff8551c 19961 delete lh;
527f3840
JK
19962}
19963
fff8551c
PA
19964void
19965line_header::add_include_dir (const char *include_dir)
c906108c 19966{
27e0867f 19967 if (dwarf_line_debug >= 2)
fff8551c
PA
19968 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19969 include_dirs.size () + 1, include_dir);
27e0867f 19970
fff8551c 19971 include_dirs.push_back (include_dir);
debd256d 19972}
6e70227d 19973
fff8551c
PA
19974void
19975line_header::add_file_name (const char *name,
ecfb656c 19976 dir_index d_index,
fff8551c
PA
19977 unsigned int mod_time,
19978 unsigned int length)
debd256d 19979{
27e0867f
DE
19980 if (dwarf_line_debug >= 2)
19981 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 19982 (unsigned) file_names.size () + 1, name);
27e0867f 19983
ecfb656c 19984 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 19985}
6e70227d 19986
83769d0b 19987/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19988
19989static struct dwarf2_section_info *
19990get_debug_line_section (struct dwarf2_cu *cu)
19991{
19992 struct dwarf2_section_info *section;
518817b3
SM
19993 struct dwarf2_per_objfile *dwarf2_per_objfile
19994 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
19995
19996 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19997 DWO file. */
19998 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19999 section = &cu->dwo_unit->dwo_file->sections.line;
20000 else if (cu->per_cu->is_dwz)
20001 {
ed2dc618 20002 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
20003
20004 section = &dwz->line;
20005 }
20006 else
20007 section = &dwarf2_per_objfile->line;
20008
20009 return section;
20010}
20011
43988095
JK
20012/* Read directory or file name entry format, starting with byte of
20013 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20014 entries count and the entries themselves in the described entry
20015 format. */
20016
20017static void
ed2dc618
SM
20018read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20019 bfd *abfd, const gdb_byte **bufp,
43988095
JK
20020 struct line_header *lh,
20021 const struct comp_unit_head *cu_header,
20022 void (*callback) (struct line_header *lh,
20023 const char *name,
ecfb656c 20024 dir_index d_index,
43988095
JK
20025 unsigned int mod_time,
20026 unsigned int length))
20027{
20028 gdb_byte format_count, formati;
20029 ULONGEST data_count, datai;
20030 const gdb_byte *buf = *bufp;
20031 const gdb_byte *format_header_data;
43988095
JK
20032 unsigned int bytes_read;
20033
20034 format_count = read_1_byte (abfd, buf);
20035 buf += 1;
20036 format_header_data = buf;
20037 for (formati = 0; formati < format_count; formati++)
20038 {
20039 read_unsigned_leb128 (abfd, buf, &bytes_read);
20040 buf += bytes_read;
20041 read_unsigned_leb128 (abfd, buf, &bytes_read);
20042 buf += bytes_read;
20043 }
20044
20045 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20046 buf += bytes_read;
20047 for (datai = 0; datai < data_count; datai++)
20048 {
20049 const gdb_byte *format = format_header_data;
20050 struct file_entry fe;
20051
43988095
JK
20052 for (formati = 0; formati < format_count; formati++)
20053 {
ecfb656c 20054 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20055 format += bytes_read;
43988095 20056
ecfb656c 20057 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20058 format += bytes_read;
ecfb656c
PA
20059
20060 gdb::optional<const char *> string;
20061 gdb::optional<unsigned int> uint;
20062
43988095
JK
20063 switch (form)
20064 {
20065 case DW_FORM_string:
ecfb656c 20066 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
20067 buf += bytes_read;
20068 break;
20069
20070 case DW_FORM_line_strp:
ed2dc618
SM
20071 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20072 abfd, buf,
ecfb656c
PA
20073 cu_header,
20074 &bytes_read));
43988095
JK
20075 buf += bytes_read;
20076 break;
20077
20078 case DW_FORM_data1:
ecfb656c 20079 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
20080 buf += 1;
20081 break;
20082
20083 case DW_FORM_data2:
ecfb656c 20084 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
20085 buf += 2;
20086 break;
20087
20088 case DW_FORM_data4:
ecfb656c 20089 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
20090 buf += 4;
20091 break;
20092
20093 case DW_FORM_data8:
ecfb656c 20094 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
20095 buf += 8;
20096 break;
20097
20098 case DW_FORM_udata:
ecfb656c 20099 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
20100 buf += bytes_read;
20101 break;
20102
20103 case DW_FORM_block:
20104 /* It is valid only for DW_LNCT_timestamp which is ignored by
20105 current GDB. */
20106 break;
20107 }
ecfb656c
PA
20108
20109 switch (content_type)
20110 {
20111 case DW_LNCT_path:
20112 if (string.has_value ())
20113 fe.name = *string;
20114 break;
20115 case DW_LNCT_directory_index:
20116 if (uint.has_value ())
20117 fe.d_index = (dir_index) *uint;
20118 break;
20119 case DW_LNCT_timestamp:
20120 if (uint.has_value ())
20121 fe.mod_time = *uint;
20122 break;
20123 case DW_LNCT_size:
20124 if (uint.has_value ())
20125 fe.length = *uint;
20126 break;
20127 case DW_LNCT_MD5:
20128 break;
20129 default:
20130 complaint (&symfile_complaints,
20131 _("Unknown format content type %s"),
20132 pulongest (content_type));
20133 }
43988095
JK
20134 }
20135
ecfb656c 20136 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20137 }
20138
20139 *bufp = buf;
20140}
20141
debd256d 20142/* Read the statement program header starting at OFFSET in
3019eac3 20143 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20144 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20145 Returns NULL if there is a problem reading the header, e.g., if it
20146 has a version we don't understand.
debd256d
JB
20147
20148 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20149 the returned object point into the dwarf line section buffer,
20150 and must not be freed. */
ae2de4f8 20151
fff8551c 20152static line_header_up
9c541725 20153dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20154{
d521ce57 20155 const gdb_byte *line_ptr;
c764a876 20156 unsigned int bytes_read, offset_size;
debd256d 20157 int i;
d521ce57 20158 const char *cur_dir, *cur_file;
3019eac3
DE
20159 struct dwarf2_section_info *section;
20160 bfd *abfd;
518817b3
SM
20161 struct dwarf2_per_objfile *dwarf2_per_objfile
20162 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20163
36586728 20164 section = get_debug_line_section (cu);
3019eac3
DE
20165 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20166 if (section->buffer == NULL)
debd256d 20167 {
3019eac3
DE
20168 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20169 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20170 else
20171 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
20172 return 0;
20173 }
20174
fceca515
DE
20175 /* We can't do this until we know the section is non-empty.
20176 Only then do we know we have such a section. */
a32a8923 20177 abfd = get_section_bfd_owner (section);
fceca515 20178
a738430d
MK
20179 /* Make sure that at least there's room for the total_length field.
20180 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20181 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20182 {
4d3c2250 20183 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20184 return 0;
20185 }
20186
fff8551c 20187 line_header_up lh (new line_header ());
debd256d 20188
9c541725 20189 lh->sect_off = sect_off;
527f3840
JK
20190 lh->offset_in_dwz = cu->per_cu->is_dwz;
20191
9c541725 20192 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20193
a738430d 20194 /* Read in the header. */
6e70227d 20195 lh->total_length =
c764a876
DE
20196 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20197 &bytes_read, &offset_size);
debd256d 20198 line_ptr += bytes_read;
3019eac3 20199 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20200 {
4d3c2250 20201 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20202 return 0;
20203 }
20204 lh->statement_program_end = line_ptr + lh->total_length;
20205 lh->version = read_2_bytes (abfd, line_ptr);
20206 line_ptr += 2;
43988095 20207 if (lh->version > 5)
cd366ee8
DE
20208 {
20209 /* This is a version we don't understand. The format could have
20210 changed in ways we don't handle properly so just punt. */
20211 complaint (&symfile_complaints,
20212 _("unsupported version in .debug_line section"));
20213 return NULL;
20214 }
43988095
JK
20215 if (lh->version >= 5)
20216 {
20217 gdb_byte segment_selector_size;
20218
20219 /* Skip address size. */
20220 read_1_byte (abfd, line_ptr);
20221 line_ptr += 1;
20222
20223 segment_selector_size = read_1_byte (abfd, line_ptr);
20224 line_ptr += 1;
20225 if (segment_selector_size != 0)
20226 {
20227 complaint (&symfile_complaints,
20228 _("unsupported segment selector size %u "
20229 "in .debug_line section"),
20230 segment_selector_size);
20231 return NULL;
20232 }
20233 }
c764a876
DE
20234 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20235 line_ptr += offset_size;
debd256d
JB
20236 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20237 line_ptr += 1;
2dc7f7b3
TT
20238 if (lh->version >= 4)
20239 {
20240 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20241 line_ptr += 1;
20242 }
20243 else
20244 lh->maximum_ops_per_instruction = 1;
20245
20246 if (lh->maximum_ops_per_instruction == 0)
20247 {
20248 lh->maximum_ops_per_instruction = 1;
20249 complaint (&symfile_complaints,
3e43a32a
MS
20250 _("invalid maximum_ops_per_instruction "
20251 "in `.debug_line' section"));
2dc7f7b3
TT
20252 }
20253
debd256d
JB
20254 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20255 line_ptr += 1;
20256 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20257 line_ptr += 1;
20258 lh->line_range = read_1_byte (abfd, line_ptr);
20259 line_ptr += 1;
20260 lh->opcode_base = read_1_byte (abfd, line_ptr);
20261 line_ptr += 1;
fff8551c 20262 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20263
20264 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20265 for (i = 1; i < lh->opcode_base; ++i)
20266 {
20267 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20268 line_ptr += 1;
20269 }
20270
43988095 20271 if (lh->version >= 5)
debd256d 20272 {
43988095 20273 /* Read directory table. */
ed2dc618
SM
20274 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20275 &cu->header,
fff8551c 20276 [] (struct line_header *lh, const char *name,
ecfb656c 20277 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20278 unsigned int length)
20279 {
20280 lh->add_include_dir (name);
20281 });
debd256d 20282
43988095 20283 /* Read file name table. */
ed2dc618
SM
20284 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20285 &cu->header,
fff8551c 20286 [] (struct line_header *lh, const char *name,
ecfb656c 20287 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20288 unsigned int length)
20289 {
ecfb656c 20290 lh->add_file_name (name, d_index, mod_time, length);
fff8551c 20291 });
43988095
JK
20292 }
20293 else
debd256d 20294 {
43988095
JK
20295 /* Read directory table. */
20296 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20297 {
20298 line_ptr += bytes_read;
fff8551c 20299 lh->add_include_dir (cur_dir);
43988095 20300 }
debd256d
JB
20301 line_ptr += bytes_read;
20302
43988095
JK
20303 /* Read file name table. */
20304 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20305 {
ecfb656c
PA
20306 unsigned int mod_time, length;
20307 dir_index d_index;
43988095
JK
20308
20309 line_ptr += bytes_read;
ecfb656c 20310 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20311 line_ptr += bytes_read;
20312 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20313 line_ptr += bytes_read;
20314 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20315 line_ptr += bytes_read;
20316
ecfb656c 20317 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20318 }
20319 line_ptr += bytes_read;
debd256d 20320 }
6e70227d 20321 lh->statement_program_start = line_ptr;
debd256d 20322
3019eac3 20323 if (line_ptr > (section->buffer + section->size))
4d3c2250 20324 complaint (&symfile_complaints,
3e43a32a
MS
20325 _("line number info header doesn't "
20326 "fit in `.debug_line' section"));
debd256d 20327
debd256d
JB
20328 return lh;
20329}
c906108c 20330
c6da4cef
DE
20331/* Subroutine of dwarf_decode_lines to simplify it.
20332 Return the file name of the psymtab for included file FILE_INDEX
20333 in line header LH of PST.
20334 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20335 If space for the result is malloc'd, *NAME_HOLDER will be set.
20336 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20337
d521ce57 20338static const char *
c6da4cef
DE
20339psymtab_include_file_name (const struct line_header *lh, int file_index,
20340 const struct partial_symtab *pst,
c89b44cd
TT
20341 const char *comp_dir,
20342 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20343{
8c43009f 20344 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
20345 const char *include_name = fe.name;
20346 const char *include_name_to_compare = include_name;
72b9f47f 20347 const char *pst_filename;
c6da4cef
DE
20348 int file_is_pst;
20349
8c43009f 20350 const char *dir_name = fe.include_dir (lh);
c6da4cef 20351
c89b44cd 20352 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20353 if (!IS_ABSOLUTE_PATH (include_name)
20354 && (dir_name != NULL || comp_dir != NULL))
20355 {
20356 /* Avoid creating a duplicate psymtab for PST.
20357 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20358 Before we do the comparison, however, we need to account
20359 for DIR_NAME and COMP_DIR.
20360 First prepend dir_name (if non-NULL). If we still don't
20361 have an absolute path prepend comp_dir (if non-NULL).
20362 However, the directory we record in the include-file's
20363 psymtab does not contain COMP_DIR (to match the
20364 corresponding symtab(s)).
20365
20366 Example:
20367
20368 bash$ cd /tmp
20369 bash$ gcc -g ./hello.c
20370 include_name = "hello.c"
20371 dir_name = "."
20372 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20373 DW_AT_name = "./hello.c"
20374
20375 */
c6da4cef
DE
20376
20377 if (dir_name != NULL)
20378 {
c89b44cd
TT
20379 name_holder->reset (concat (dir_name, SLASH_STRING,
20380 include_name, (char *) NULL));
20381 include_name = name_holder->get ();
c6da4cef 20382 include_name_to_compare = include_name;
c6da4cef
DE
20383 }
20384 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20385 {
c89b44cd
TT
20386 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20387 include_name, (char *) NULL));
20388 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20389 }
20390 }
20391
20392 pst_filename = pst->filename;
c89b44cd 20393 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20394 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20395 {
c89b44cd
TT
20396 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20397 pst_filename, (char *) NULL));
20398 pst_filename = copied_name.get ();
c6da4cef
DE
20399 }
20400
1e3fad37 20401 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20402
c6da4cef
DE
20403 if (file_is_pst)
20404 return NULL;
20405 return include_name;
20406}
20407
d9b3de22
DE
20408/* State machine to track the state of the line number program. */
20409
6f77053d 20410class lnp_state_machine
d9b3de22 20411{
6f77053d
PA
20412public:
20413 /* Initialize a machine state for the start of a line number
20414 program. */
20415 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20416
8c43009f
PA
20417 file_entry *current_file ()
20418 {
20419 /* lh->file_names is 0-based, but the file name numbers in the
20420 statement program are 1-based. */
6f77053d
PA
20421 return m_line_header->file_name_at (m_file);
20422 }
20423
20424 /* Record the line in the state machine. END_SEQUENCE is true if
20425 we're processing the end of a sequence. */
20426 void record_line (bool end_sequence);
20427
20428 /* Check address and if invalid nop-out the rest of the lines in this
20429 sequence. */
20430 void check_line_address (struct dwarf2_cu *cu,
20431 const gdb_byte *line_ptr,
20432 CORE_ADDR lowpc, CORE_ADDR address);
20433
20434 void handle_set_discriminator (unsigned int discriminator)
20435 {
20436 m_discriminator = discriminator;
20437 m_line_has_non_zero_discriminator |= discriminator != 0;
20438 }
20439
20440 /* Handle DW_LNE_set_address. */
20441 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20442 {
20443 m_op_index = 0;
20444 address += baseaddr;
20445 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20446 }
20447
20448 /* Handle DW_LNS_advance_pc. */
20449 void handle_advance_pc (CORE_ADDR adjust);
20450
20451 /* Handle a special opcode. */
20452 void handle_special_opcode (unsigned char op_code);
20453
20454 /* Handle DW_LNS_advance_line. */
20455 void handle_advance_line (int line_delta)
20456 {
20457 advance_line (line_delta);
20458 }
20459
20460 /* Handle DW_LNS_set_file. */
20461 void handle_set_file (file_name_index file);
20462
20463 /* Handle DW_LNS_negate_stmt. */
20464 void handle_negate_stmt ()
20465 {
20466 m_is_stmt = !m_is_stmt;
20467 }
20468
20469 /* Handle DW_LNS_const_add_pc. */
20470 void handle_const_add_pc ();
20471
20472 /* Handle DW_LNS_fixed_advance_pc. */
20473 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20474 {
20475 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20476 m_op_index = 0;
20477 }
20478
20479 /* Handle DW_LNS_copy. */
20480 void handle_copy ()
20481 {
20482 record_line (false);
20483 m_discriminator = 0;
20484 }
20485
20486 /* Handle DW_LNE_end_sequence. */
20487 void handle_end_sequence ()
20488 {
20489 m_record_line_callback = ::record_line;
20490 }
20491
20492private:
20493 /* Advance the line by LINE_DELTA. */
20494 void advance_line (int line_delta)
20495 {
20496 m_line += line_delta;
20497
20498 if (line_delta != 0)
20499 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20500 }
20501
6f77053d
PA
20502 gdbarch *m_gdbarch;
20503
20504 /* True if we're recording lines.
20505 Otherwise we're building partial symtabs and are just interested in
20506 finding include files mentioned by the line number program. */
20507 bool m_record_lines_p;
20508
8c43009f 20509 /* The line number header. */
6f77053d 20510 line_header *m_line_header;
8c43009f 20511
6f77053d
PA
20512 /* These are part of the standard DWARF line number state machine,
20513 and initialized according to the DWARF spec. */
d9b3de22 20514
6f77053d 20515 unsigned char m_op_index = 0;
8c43009f 20516 /* The line table index (1-based) of the current file. */
6f77053d
PA
20517 file_name_index m_file = (file_name_index) 1;
20518 unsigned int m_line = 1;
20519
20520 /* These are initialized in the constructor. */
20521
20522 CORE_ADDR m_address;
20523 bool m_is_stmt;
20524 unsigned int m_discriminator;
d9b3de22
DE
20525
20526 /* Additional bits of state we need to track. */
20527
20528 /* The last file that we called dwarf2_start_subfile for.
20529 This is only used for TLLs. */
6f77053d 20530 unsigned int m_last_file = 0;
d9b3de22 20531 /* The last file a line number was recorded for. */
6f77053d 20532 struct subfile *m_last_subfile = NULL;
d9b3de22
DE
20533
20534 /* The function to call to record a line. */
6f77053d 20535 record_line_ftype *m_record_line_callback = NULL;
d9b3de22
DE
20536
20537 /* The last line number that was recorded, used to coalesce
20538 consecutive entries for the same line. This can happen, for
20539 example, when discriminators are present. PR 17276. */
6f77053d
PA
20540 unsigned int m_last_line = 0;
20541 bool m_line_has_non_zero_discriminator = false;
8c43009f 20542};
d9b3de22 20543
6f77053d
PA
20544void
20545lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20546{
20547 CORE_ADDR addr_adj = (((m_op_index + adjust)
20548 / m_line_header->maximum_ops_per_instruction)
20549 * m_line_header->minimum_instruction_length);
20550 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20551 m_op_index = ((m_op_index + adjust)
20552 % m_line_header->maximum_ops_per_instruction);
20553}
d9b3de22 20554
6f77053d
PA
20555void
20556lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20557{
6f77053d
PA
20558 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20559 CORE_ADDR addr_adj = (((m_op_index
20560 + (adj_opcode / m_line_header->line_range))
20561 / m_line_header->maximum_ops_per_instruction)
20562 * m_line_header->minimum_instruction_length);
20563 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20564 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20565 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20566
6f77053d
PA
20567 int line_delta = (m_line_header->line_base
20568 + (adj_opcode % m_line_header->line_range));
20569 advance_line (line_delta);
20570 record_line (false);
20571 m_discriminator = 0;
20572}
d9b3de22 20573
6f77053d
PA
20574void
20575lnp_state_machine::handle_set_file (file_name_index file)
20576{
20577 m_file = file;
20578
20579 const file_entry *fe = current_file ();
20580 if (fe == NULL)
20581 dwarf2_debug_line_missing_file_complaint ();
20582 else if (m_record_lines_p)
20583 {
20584 const char *dir = fe->include_dir (m_line_header);
20585
20586 m_last_subfile = current_subfile;
20587 m_line_has_non_zero_discriminator = m_discriminator != 0;
20588 dwarf2_start_subfile (fe->name, dir);
20589 }
20590}
20591
20592void
20593lnp_state_machine::handle_const_add_pc ()
20594{
20595 CORE_ADDR adjust
20596 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20597
20598 CORE_ADDR addr_adj
20599 = (((m_op_index + adjust)
20600 / m_line_header->maximum_ops_per_instruction)
20601 * m_line_header->minimum_instruction_length);
20602
20603 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20604 m_op_index = ((m_op_index + adjust)
20605 % m_line_header->maximum_ops_per_instruction);
20606}
d9b3de22 20607
c91513d8
PP
20608/* Ignore this record_line request. */
20609
20610static void
20611noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
20612{
20613 return;
20614}
20615
a05a36a5
DE
20616/* Return non-zero if we should add LINE to the line number table.
20617 LINE is the line to add, LAST_LINE is the last line that was added,
20618 LAST_SUBFILE is the subfile for LAST_LINE.
20619 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20620 had a non-zero discriminator.
20621
20622 We have to be careful in the presence of discriminators.
20623 E.g., for this line:
20624
20625 for (i = 0; i < 100000; i++);
20626
20627 clang can emit four line number entries for that one line,
20628 each with a different discriminator.
20629 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20630
20631 However, we want gdb to coalesce all four entries into one.
20632 Otherwise the user could stepi into the middle of the line and
20633 gdb would get confused about whether the pc really was in the
20634 middle of the line.
20635
20636 Things are further complicated by the fact that two consecutive
20637 line number entries for the same line is a heuristic used by gcc
20638 to denote the end of the prologue. So we can't just discard duplicate
20639 entries, we have to be selective about it. The heuristic we use is
20640 that we only collapse consecutive entries for the same line if at least
20641 one of those entries has a non-zero discriminator. PR 17276.
20642
20643 Note: Addresses in the line number state machine can never go backwards
20644 within one sequence, thus this coalescing is ok. */
20645
20646static int
20647dwarf_record_line_p (unsigned int line, unsigned int last_line,
20648 int line_has_non_zero_discriminator,
20649 struct subfile *last_subfile)
20650{
20651 if (current_subfile != last_subfile)
20652 return 1;
20653 if (line != last_line)
20654 return 1;
20655 /* Same line for the same file that we've seen already.
20656 As a last check, for pr 17276, only record the line if the line
20657 has never had a non-zero discriminator. */
20658 if (!line_has_non_zero_discriminator)
20659 return 1;
20660 return 0;
20661}
20662
252a6764
DE
20663/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
20664 in the line table of subfile SUBFILE. */
20665
20666static void
d9b3de22
DE
20667dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20668 unsigned int line, CORE_ADDR address,
20669 record_line_ftype p_record_line)
252a6764
DE
20670{
20671 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20672
27e0867f
DE
20673 if (dwarf_line_debug)
20674 {
20675 fprintf_unfiltered (gdb_stdlog,
20676 "Recording line %u, file %s, address %s\n",
20677 line, lbasename (subfile->name),
20678 paddress (gdbarch, address));
20679 }
20680
d5962de5 20681 (*p_record_line) (subfile, line, addr);
252a6764
DE
20682}
20683
20684/* Subroutine of dwarf_decode_lines_1 to simplify it.
20685 Mark the end of a set of line number records.
d9b3de22 20686 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20687 If SUBFILE is NULL the request is ignored. */
20688
20689static void
20690dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20691 CORE_ADDR address, record_line_ftype p_record_line)
20692{
27e0867f
DE
20693 if (subfile == NULL)
20694 return;
20695
20696 if (dwarf_line_debug)
20697 {
20698 fprintf_unfiltered (gdb_stdlog,
20699 "Finishing current line, file %s, address %s\n",
20700 lbasename (subfile->name),
20701 paddress (gdbarch, address));
20702 }
20703
d9b3de22
DE
20704 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
20705}
20706
6f77053d
PA
20707void
20708lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20709{
d9b3de22
DE
20710 if (dwarf_line_debug)
20711 {
20712 fprintf_unfiltered (gdb_stdlog,
20713 "Processing actual line %u: file %u,"
20714 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
20715 m_line, to_underlying (m_file),
20716 paddress (m_gdbarch, m_address),
20717 m_is_stmt, m_discriminator);
d9b3de22
DE
20718 }
20719
6f77053d 20720 file_entry *fe = current_file ();
8c43009f
PA
20721
20722 if (fe == NULL)
d9b3de22
DE
20723 dwarf2_debug_line_missing_file_complaint ();
20724 /* For now we ignore lines not starting on an instruction boundary.
20725 But not when processing end_sequence for compatibility with the
20726 previous version of the code. */
6f77053d 20727 else if (m_op_index == 0 || end_sequence)
d9b3de22 20728 {
8c43009f 20729 fe->included_p = 1;
6f77053d 20730 if (m_record_lines_p && m_is_stmt)
d9b3de22 20731 {
6f77053d 20732 if (m_last_subfile != current_subfile || end_sequence)
d9b3de22 20733 {
6f77053d
PA
20734 dwarf_finish_line (m_gdbarch, m_last_subfile,
20735 m_address, m_record_line_callback);
d9b3de22
DE
20736 }
20737
20738 if (!end_sequence)
20739 {
6f77053d
PA
20740 if (dwarf_record_line_p (m_line, m_last_line,
20741 m_line_has_non_zero_discriminator,
20742 m_last_subfile))
d9b3de22 20743 {
6f77053d
PA
20744 dwarf_record_line_1 (m_gdbarch, current_subfile,
20745 m_line, m_address,
20746 m_record_line_callback);
d9b3de22 20747 }
6f77053d
PA
20748 m_last_subfile = current_subfile;
20749 m_last_line = m_line;
d9b3de22
DE
20750 }
20751 }
20752 }
20753}
20754
6f77053d
PA
20755lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
20756 bool record_lines_p)
d9b3de22 20757{
6f77053d
PA
20758 m_gdbarch = arch;
20759 m_record_lines_p = record_lines_p;
20760 m_line_header = lh;
d9b3de22 20761
6f77053d 20762 m_record_line_callback = ::record_line;
d9b3de22 20763
d9b3de22
DE
20764 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20765 was a line entry for it so that the backend has a chance to adjust it
20766 and also record it in case it needs it. This is currently used by MIPS
20767 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20768 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20769 m_is_stmt = lh->default_is_stmt;
20770 m_discriminator = 0;
252a6764
DE
20771}
20772
6f77053d
PA
20773void
20774lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20775 const gdb_byte *line_ptr,
20776 CORE_ADDR lowpc, CORE_ADDR address)
924c2928
DE
20777{
20778 /* If address < lowpc then it's not a usable value, it's outside the
20779 pc range of the CU. However, we restrict the test to only address
20780 values of zero to preserve GDB's previous behaviour which is to
20781 handle the specific case of a function being GC'd by the linker. */
20782
20783 if (address == 0 && address < lowpc)
20784 {
20785 /* This line table is for a function which has been
20786 GCd by the linker. Ignore it. PR gdb/12528 */
20787
518817b3 20788 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
20789 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20790
20791 complaint (&symfile_complaints,
20792 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20793 line_offset, objfile_name (objfile));
6f77053d
PA
20794 m_record_line_callback = noop_record_line;
20795 /* Note: record_line_callback is left as noop_record_line until
20796 we see DW_LNE_end_sequence. */
924c2928
DE
20797 }
20798}
20799
f3f5162e 20800/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20801 Process the line number information in LH.
20802 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20803 program in order to set included_p for every referenced header. */
debd256d 20804
c906108c 20805static void
43f3e411
DE
20806dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20807 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20808{
d521ce57
TT
20809 const gdb_byte *line_ptr, *extended_end;
20810 const gdb_byte *line_end;
a8c50c1f 20811 unsigned int bytes_read, extended_len;
699ca60a 20812 unsigned char op_code, extended_op;
e142c38c 20813 CORE_ADDR baseaddr;
518817b3 20814 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20815 bfd *abfd = objfile->obfd;
fbf65064 20816 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
20817 /* True if we're recording line info (as opposed to building partial
20818 symtabs and just interested in finding include files mentioned by
20819 the line number program). */
20820 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
20821
20822 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 20823
debd256d
JB
20824 line_ptr = lh->statement_program_start;
20825 line_end = lh->statement_program_end;
c906108c
SS
20826
20827 /* Read the statement sequences until there's nothing left. */
20828 while (line_ptr < line_end)
20829 {
6f77053d
PA
20830 /* The DWARF line number program state machine. Reset the state
20831 machine at the start of each sequence. */
20832 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
20833 bool end_sequence = false;
d9b3de22 20834
8c43009f 20835 if (record_lines_p)
c906108c 20836 {
8c43009f
PA
20837 /* Start a subfile for the current file of the state
20838 machine. */
20839 const file_entry *fe = state_machine.current_file ();
20840
20841 if (fe != NULL)
20842 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
c906108c
SS
20843 }
20844
a738430d 20845 /* Decode the table. */
d9b3de22 20846 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20847 {
20848 op_code = read_1_byte (abfd, line_ptr);
20849 line_ptr += 1;
9aa1fe7e 20850
debd256d 20851 if (op_code >= lh->opcode_base)
6e70227d 20852 {
8e07a239 20853 /* Special opcode. */
6f77053d 20854 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20855 }
20856 else switch (op_code)
c906108c
SS
20857 {
20858 case DW_LNS_extended_op:
3e43a32a
MS
20859 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20860 &bytes_read);
473b7be6 20861 line_ptr += bytes_read;
a8c50c1f 20862 extended_end = line_ptr + extended_len;
c906108c
SS
20863 extended_op = read_1_byte (abfd, line_ptr);
20864 line_ptr += 1;
20865 switch (extended_op)
20866 {
20867 case DW_LNE_end_sequence:
6f77053d
PA
20868 state_machine.handle_end_sequence ();
20869 end_sequence = true;
c906108c
SS
20870 break;
20871 case DW_LNE_set_address:
d9b3de22
DE
20872 {
20873 CORE_ADDR address
20874 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 20875 line_ptr += bytes_read;
6f77053d
PA
20876
20877 state_machine.check_line_address (cu, line_ptr,
20878 lowpc, address);
20879 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20880 }
c906108c
SS
20881 break;
20882 case DW_LNE_define_file:
debd256d 20883 {
d521ce57 20884 const char *cur_file;
ecfb656c
PA
20885 unsigned int mod_time, length;
20886 dir_index dindex;
6e70227d 20887
3e43a32a
MS
20888 cur_file = read_direct_string (abfd, line_ptr,
20889 &bytes_read);
debd256d 20890 line_ptr += bytes_read;
ecfb656c 20891 dindex = (dir_index)
debd256d
JB
20892 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20893 line_ptr += bytes_read;
20894 mod_time =
20895 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20896 line_ptr += bytes_read;
20897 length =
20898 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20899 line_ptr += bytes_read;
ecfb656c 20900 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20901 }
c906108c 20902 break;
d0c6ba3d 20903 case DW_LNE_set_discriminator:
6f77053d
PA
20904 {
20905 /* The discriminator is not interesting to the
20906 debugger; just ignore it. We still need to
20907 check its value though:
20908 if there are consecutive entries for the same
20909 (non-prologue) line we want to coalesce them.
20910 PR 17276. */
20911 unsigned int discr
20912 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20913 line_ptr += bytes_read;
20914
20915 state_machine.handle_set_discriminator (discr);
20916 }
d0c6ba3d 20917 break;
c906108c 20918 default:
4d3c2250 20919 complaint (&symfile_complaints,
e2e0b3e5 20920 _("mangled .debug_line section"));
debd256d 20921 return;
c906108c 20922 }
a8c50c1f
DJ
20923 /* Make sure that we parsed the extended op correctly. If e.g.
20924 we expected a different address size than the producer used,
20925 we may have read the wrong number of bytes. */
20926 if (line_ptr != extended_end)
20927 {
20928 complaint (&symfile_complaints,
20929 _("mangled .debug_line section"));
20930 return;
20931 }
c906108c
SS
20932 break;
20933 case DW_LNS_copy:
6f77053d 20934 state_machine.handle_copy ();
c906108c
SS
20935 break;
20936 case DW_LNS_advance_pc:
2dc7f7b3
TT
20937 {
20938 CORE_ADDR adjust
20939 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20940 line_ptr += bytes_read;
6f77053d
PA
20941
20942 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20943 }
c906108c
SS
20944 break;
20945 case DW_LNS_advance_line:
a05a36a5
DE
20946 {
20947 int line_delta
20948 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20949 line_ptr += bytes_read;
6f77053d
PA
20950
20951 state_machine.handle_advance_line (line_delta);
a05a36a5 20952 }
c906108c
SS
20953 break;
20954 case DW_LNS_set_file:
d9b3de22 20955 {
6f77053d 20956 file_name_index file
ecfb656c
PA
20957 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20958 &bytes_read);
d9b3de22 20959 line_ptr += bytes_read;
8c43009f 20960
6f77053d 20961 state_machine.handle_set_file (file);
d9b3de22 20962 }
c906108c
SS
20963 break;
20964 case DW_LNS_set_column:
0ad93d4f 20965 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20966 line_ptr += bytes_read;
20967 break;
20968 case DW_LNS_negate_stmt:
6f77053d 20969 state_machine.handle_negate_stmt ();
c906108c
SS
20970 break;
20971 case DW_LNS_set_basic_block:
c906108c 20972 break;
c2c6d25f
JM
20973 /* Add to the address register of the state machine the
20974 address increment value corresponding to special opcode
a738430d
MK
20975 255. I.e., this value is scaled by the minimum
20976 instruction length since special opcode 255 would have
b021a221 20977 scaled the increment. */
c906108c 20978 case DW_LNS_const_add_pc:
6f77053d 20979 state_machine.handle_const_add_pc ();
c906108c
SS
20980 break;
20981 case DW_LNS_fixed_advance_pc:
3e29f34a 20982 {
6f77053d 20983 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20984 line_ptr += 2;
6f77053d
PA
20985
20986 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20987 }
c906108c 20988 break;
9aa1fe7e 20989 default:
a738430d
MK
20990 {
20991 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20992 int i;
a738430d 20993
debd256d 20994 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20995 {
20996 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20997 line_ptr += bytes_read;
20998 }
20999 }
c906108c
SS
21000 }
21001 }
d9b3de22
DE
21002
21003 if (!end_sequence)
21004 dwarf2_debug_line_missing_end_sequence_complaint ();
21005
21006 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21007 in which case we still finish recording the last line). */
6f77053d 21008 state_machine.record_line (true);
c906108c 21009 }
f3f5162e
DE
21010}
21011
21012/* Decode the Line Number Program (LNP) for the given line_header
21013 structure and CU. The actual information extracted and the type
21014 of structures created from the LNP depends on the value of PST.
21015
21016 1. If PST is NULL, then this procedure uses the data from the program
21017 to create all necessary symbol tables, and their linetables.
21018
21019 2. If PST is not NULL, this procedure reads the program to determine
21020 the list of files included by the unit represented by PST, and
21021 builds all the associated partial symbol tables.
21022
21023 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21024 It is used for relative paths in the line table.
21025 NOTE: When processing partial symtabs (pst != NULL),
21026 comp_dir == pst->dirname.
21027
21028 NOTE: It is important that psymtabs have the same file name (via strcmp)
21029 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21030 symtab we don't use it in the name of the psymtabs we create.
21031 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21032 A good testcase for this is mb-inline.exp.
21033
527f3840
JK
21034 LOWPC is the lowest address in CU (or 0 if not known).
21035
21036 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21037 for its PC<->lines mapping information. Otherwise only the filename
21038 table is read in. */
f3f5162e
DE
21039
21040static void
21041dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 21042 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 21043 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21044{
518817b3 21045 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21046 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21047
527f3840
JK
21048 if (decode_mapping)
21049 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21050
21051 if (decode_for_pst_p)
21052 {
21053 int file_index;
21054
21055 /* Now that we're done scanning the Line Header Program, we can
21056 create the psymtab of each included file. */
fff8551c 21057 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
21058 if (lh->file_names[file_index].included_p == 1)
21059 {
c89b44cd 21060 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21061 const char *include_name =
c89b44cd
TT
21062 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21063 &name_holder);
c6da4cef 21064 if (include_name != NULL)
aaa75496
JB
21065 dwarf2_create_include_psymtab (include_name, pst, objfile);
21066 }
21067 }
cb1df416
DJ
21068 else
21069 {
21070 /* Make sure a symtab is created for every file, even files
21071 which contain only variables (i.e. no code with associated
21072 line numbers). */
43f3e411 21073 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 21074 int i;
cb1df416 21075
fff8551c 21076 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 21077 {
8c43009f 21078 file_entry &fe = lh->file_names[i];
9a619af0 21079
8c43009f 21080 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
cb1df416 21081
cb1df416 21082 if (current_subfile->symtab == NULL)
43f3e411
DE
21083 {
21084 current_subfile->symtab
21085 = allocate_symtab (cust, current_subfile->name);
21086 }
8c43009f 21087 fe.symtab = current_subfile->symtab;
cb1df416
DJ
21088 }
21089 }
c906108c
SS
21090}
21091
21092/* Start a subfile for DWARF. FILENAME is the name of the file and
21093 DIRNAME the name of the source directory which contains FILENAME
4d663531 21094 or NULL if not known.
c906108c
SS
21095 This routine tries to keep line numbers from identical absolute and
21096 relative file names in a common subfile.
21097
21098 Using the `list' example from the GDB testsuite, which resides in
21099 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21100 of /srcdir/list0.c yields the following debugging information for list0.c:
21101
c5aa993b 21102 DW_AT_name: /srcdir/list0.c
4d663531 21103 DW_AT_comp_dir: /compdir
357e46e7 21104 files.files[0].name: list0.h
c5aa993b 21105 files.files[0].dir: /srcdir
357e46e7 21106 files.files[1].name: list0.c
c5aa993b 21107 files.files[1].dir: /srcdir
c906108c
SS
21108
21109 The line number information for list0.c has to end up in a single
4f1520fb
FR
21110 subfile, so that `break /srcdir/list0.c:1' works as expected.
21111 start_subfile will ensure that this happens provided that we pass the
21112 concatenation of files.files[1].dir and files.files[1].name as the
21113 subfile's name. */
c906108c
SS
21114
21115static void
4d663531 21116dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 21117{
d521ce57 21118 char *copy = NULL;
4f1520fb 21119
4d663531 21120 /* In order not to lose the line information directory,
4f1520fb
FR
21121 we concatenate it to the filename when it makes sense.
21122 Note that the Dwarf3 standard says (speaking of filenames in line
21123 information): ``The directory index is ignored for file names
21124 that represent full path names''. Thus ignoring dirname in the
21125 `else' branch below isn't an issue. */
c906108c 21126
d5166ae1 21127 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
21128 {
21129 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21130 filename = copy;
21131 }
c906108c 21132
4d663531 21133 start_subfile (filename);
4f1520fb 21134
d521ce57
TT
21135 if (copy != NULL)
21136 xfree (copy);
c906108c
SS
21137}
21138
f4dc4d17
DE
21139/* Start a symtab for DWARF.
21140 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21141
43f3e411 21142static struct compunit_symtab *
f4dc4d17 21143dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 21144 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 21145{
43f3e411 21146 struct compunit_symtab *cust
518817b3
SM
21147 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21148 low_pc, cu->language);
43f3e411 21149
f4dc4d17
DE
21150 record_debugformat ("DWARF 2");
21151 record_producer (cu->producer);
21152
21153 /* We assume that we're processing GCC output. */
21154 processing_gcc_compilation = 2;
21155
4d4ec4e5 21156 cu->processing_has_namespace_info = 0;
43f3e411
DE
21157
21158 return cust;
f4dc4d17
DE
21159}
21160
4c2df51b
DJ
21161static void
21162var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21163 struct dwarf2_cu *cu)
4c2df51b 21164{
518817b3 21165 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21166 struct comp_unit_head *cu_header = &cu->header;
21167
4c2df51b
DJ
21168 /* NOTE drow/2003-01-30: There used to be a comment and some special
21169 code here to turn a symbol with DW_AT_external and a
21170 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21171 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21172 with some versions of binutils) where shared libraries could have
21173 relocations against symbols in their debug information - the
21174 minimal symbol would have the right address, but the debug info
21175 would not. It's no longer necessary, because we will explicitly
21176 apply relocations when we read in the debug information now. */
21177
21178 /* A DW_AT_location attribute with no contents indicates that a
21179 variable has been optimized away. */
21180 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21181 {
f1e6e072 21182 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21183 return;
21184 }
21185
21186 /* Handle one degenerate form of location expression specially, to
21187 preserve GDB's previous behavior when section offsets are
3019eac3
DE
21188 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21189 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21190
21191 if (attr_form_is_block (attr)
3019eac3
DE
21192 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21193 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21194 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21195 && (DW_BLOCK (attr)->size
21196 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21197 {
891d2f0b 21198 unsigned int dummy;
4c2df51b 21199
3019eac3
DE
21200 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21201 SYMBOL_VALUE_ADDRESS (sym) =
21202 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21203 else
21204 SYMBOL_VALUE_ADDRESS (sym) =
21205 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 21206 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
21207 fixup_symbol_section (sym, objfile);
21208 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21209 SYMBOL_SECTION (sym));
4c2df51b
DJ
21210 return;
21211 }
21212
21213 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21214 expression evaluator, and use LOC_COMPUTED only when necessary
21215 (i.e. when the value of a register or memory location is
21216 referenced, or a thread-local block, etc.). Then again, it might
21217 not be worthwhile. I'm assuming that it isn't unless performance
21218 or memory numbers show me otherwise. */
21219
f1e6e072 21220 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21221
f1e6e072 21222 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 21223 cu->has_loclist = 1;
4c2df51b
DJ
21224}
21225
c906108c
SS
21226/* Given a pointer to a DWARF information entry, figure out if we need
21227 to make a symbol table entry for it, and if so, create a new entry
21228 and return a pointer to it.
21229 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21230 used the passed type.
21231 If SPACE is not NULL, use it to hold the new symbol. If it is
21232 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21233
21234static struct symbol *
5e2db402
TT
21235new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21236 struct symbol *space)
c906108c 21237{
518817b3
SM
21238 struct dwarf2_per_objfile *dwarf2_per_objfile
21239 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21240 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21241 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21242 struct symbol *sym = NULL;
15d034d0 21243 const char *name;
c906108c
SS
21244 struct attribute *attr = NULL;
21245 struct attribute *attr2 = NULL;
e142c38c 21246 CORE_ADDR baseaddr;
e37fd15a
SW
21247 struct pending **list_to_add = NULL;
21248
edb3359d 21249 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
21250
21251 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21252
94af9270 21253 name = dwarf2_name (die, cu);
c906108c
SS
21254 if (name)
21255 {
94af9270 21256 const char *linkagename;
34eaf542 21257 int suppress_add = 0;
94af9270 21258
34eaf542
TT
21259 if (space)
21260 sym = space;
21261 else
e623cf5d 21262 sym = allocate_symbol (objfile);
c906108c 21263 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21264
21265 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 21266 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
21267 linkagename = dwarf2_physname (name, die, cu);
21268 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 21269
f55ee35c
JK
21270 /* Fortran does not have mangling standard and the mangling does differ
21271 between gfortran, iFort etc. */
21272 if (cu->language == language_fortran
b250c185 21273 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 21274 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 21275 dwarf2_full_name (name, die, cu),
29df156d 21276 NULL);
f55ee35c 21277
c906108c 21278 /* Default assumptions.
c5aa993b 21279 Use the passed type or decode it from the die. */
176620f1 21280 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21281 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21282 if (type != NULL)
21283 SYMBOL_TYPE (sym) = type;
21284 else
e7c27a73 21285 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21286 attr = dwarf2_attr (die,
21287 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21288 cu);
c906108c
SS
21289 if (attr)
21290 {
21291 SYMBOL_LINE (sym) = DW_UNSND (attr);
21292 }
cb1df416 21293
edb3359d
DJ
21294 attr = dwarf2_attr (die,
21295 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21296 cu);
cb1df416
DJ
21297 if (attr)
21298 {
ecfb656c 21299 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21300 struct file_entry *fe;
9a619af0 21301
ecfb656c
PA
21302 if (cu->line_header != NULL)
21303 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21304 else
21305 fe = NULL;
21306
21307 if (fe == NULL)
cb1df416
DJ
21308 complaint (&symfile_complaints,
21309 _("file index out of range"));
8c43009f
PA
21310 else
21311 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21312 }
21313
c906108c
SS
21314 switch (die->tag)
21315 {
21316 case DW_TAG_label:
e142c38c 21317 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 21318 if (attr)
3e29f34a
MR
21319 {
21320 CORE_ADDR addr;
21321
21322 addr = attr_value_as_address (attr);
21323 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21324 SYMBOL_VALUE_ADDRESS (sym) = addr;
21325 }
0f5238ed
TT
21326 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21327 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21328 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 21329 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21330 break;
21331 case DW_TAG_subprogram:
21332 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21333 finish_block. */
f1e6e072 21334 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21335 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
21336 if ((attr2 && (DW_UNSND (attr2) != 0))
21337 || cu->language == language_ada)
c906108c 21338 {
2cfa0c8d
JB
21339 /* Subprograms marked external are stored as a global symbol.
21340 Ada subprograms, whether marked external or not, are always
21341 stored as a global symbol, because we want to be able to
21342 access them globally. For instance, we want to be able
21343 to break on a nested subprogram without having to
21344 specify the context. */
e37fd15a 21345 list_to_add = &global_symbols;
c906108c
SS
21346 }
21347 else
21348 {
e37fd15a 21349 list_to_add = cu->list_in_scope;
c906108c
SS
21350 }
21351 break;
edb3359d
DJ
21352 case DW_TAG_inlined_subroutine:
21353 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21354 finish_block. */
f1e6e072 21355 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21356 SYMBOL_INLINED (sym) = 1;
481860b3 21357 list_to_add = cu->list_in_scope;
edb3359d 21358 break;
34eaf542
TT
21359 case DW_TAG_template_value_param:
21360 suppress_add = 1;
21361 /* Fall through. */
72929c62 21362 case DW_TAG_constant:
c906108c 21363 case DW_TAG_variable:
254e6b9e 21364 case DW_TAG_member:
0963b4bd
MS
21365 /* Compilation with minimal debug info may result in
21366 variables with missing type entries. Change the
21367 misleading `void' type to something sensible. */
c906108c 21368 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21369 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21370
e142c38c 21371 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21372 /* In the case of DW_TAG_member, we should only be called for
21373 static const members. */
21374 if (die->tag == DW_TAG_member)
21375 {
3863f96c
DE
21376 /* dwarf2_add_field uses die_is_declaration,
21377 so we do the same. */
254e6b9e
DE
21378 gdb_assert (die_is_declaration (die, cu));
21379 gdb_assert (attr);
21380 }
c906108c
SS
21381 if (attr)
21382 {
e7c27a73 21383 dwarf2_const_value (attr, sym, cu);
e142c38c 21384 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21385 if (!suppress_add)
34eaf542
TT
21386 {
21387 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 21388 list_to_add = &global_symbols;
34eaf542 21389 else
e37fd15a 21390 list_to_add = cu->list_in_scope;
34eaf542 21391 }
c906108c
SS
21392 break;
21393 }
e142c38c 21394 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21395 if (attr)
21396 {
e7c27a73 21397 var_decode_location (attr, sym, cu);
e142c38c 21398 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21399
21400 /* Fortran explicitly imports any global symbols to the local
21401 scope by DW_TAG_common_block. */
21402 if (cu->language == language_fortran && die->parent
21403 && die->parent->tag == DW_TAG_common_block)
21404 attr2 = NULL;
21405
caac4577
JG
21406 if (SYMBOL_CLASS (sym) == LOC_STATIC
21407 && SYMBOL_VALUE_ADDRESS (sym) == 0
21408 && !dwarf2_per_objfile->has_section_at_zero)
21409 {
21410 /* When a static variable is eliminated by the linker,
21411 the corresponding debug information is not stripped
21412 out, but the variable address is set to null;
21413 do not add such variables into symbol table. */
21414 }
21415 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21416 {
f55ee35c
JK
21417 /* Workaround gfortran PR debug/40040 - it uses
21418 DW_AT_location for variables in -fPIC libraries which may
21419 get overriden by other libraries/executable and get
21420 a different address. Resolve it by the minimal symbol
21421 which may come from inferior's executable using copy
21422 relocation. Make this workaround only for gfortran as for
21423 other compilers GDB cannot guess the minimal symbol
21424 Fortran mangling kind. */
21425 if (cu->language == language_fortran && die->parent
21426 && die->parent->tag == DW_TAG_module
21427 && cu->producer
28586665 21428 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 21429 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 21430
1c809c68
TT
21431 /* A variable with DW_AT_external is never static,
21432 but it may be block-scoped. */
21433 list_to_add = (cu->list_in_scope == &file_symbols
21434 ? &global_symbols : cu->list_in_scope);
1c809c68 21435 }
c906108c 21436 else
e37fd15a 21437 list_to_add = cu->list_in_scope;
c906108c
SS
21438 }
21439 else
21440 {
21441 /* We do not know the address of this symbol.
c5aa993b
JM
21442 If it is an external symbol and we have type information
21443 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21444 The address of the variable will then be determined from
21445 the minimal symbol table whenever the variable is
21446 referenced. */
e142c38c 21447 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21448
21449 /* Fortran explicitly imports any global symbols to the local
21450 scope by DW_TAG_common_block. */
21451 if (cu->language == language_fortran && die->parent
21452 && die->parent->tag == DW_TAG_common_block)
21453 {
21454 /* SYMBOL_CLASS doesn't matter here because
21455 read_common_block is going to reset it. */
21456 if (!suppress_add)
21457 list_to_add = cu->list_in_scope;
21458 }
21459 else if (attr2 && (DW_UNSND (attr2) != 0)
21460 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21461 {
0fe7935b
DJ
21462 /* A variable with DW_AT_external is never static, but it
21463 may be block-scoped. */
21464 list_to_add = (cu->list_in_scope == &file_symbols
21465 ? &global_symbols : cu->list_in_scope);
21466
f1e6e072 21467 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21468 }
442ddf59
JK
21469 else if (!die_is_declaration (die, cu))
21470 {
21471 /* Use the default LOC_OPTIMIZED_OUT class. */
21472 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21473 if (!suppress_add)
21474 list_to_add = cu->list_in_scope;
442ddf59 21475 }
c906108c
SS
21476 }
21477 break;
21478 case DW_TAG_formal_parameter:
edb3359d
DJ
21479 /* If we are inside a function, mark this as an argument. If
21480 not, we might be looking at an argument to an inlined function
21481 when we do not have enough information to show inlined frames;
21482 pretend it's a local variable in that case so that the user can
21483 still see it. */
21484 if (context_stack_depth > 0
21485 && context_stack[context_stack_depth - 1].name != NULL)
21486 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 21487 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21488 if (attr)
21489 {
e7c27a73 21490 var_decode_location (attr, sym, cu);
c906108c 21491 }
e142c38c 21492 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21493 if (attr)
21494 {
e7c27a73 21495 dwarf2_const_value (attr, sym, cu);
c906108c 21496 }
f346a30d 21497
e37fd15a 21498 list_to_add = cu->list_in_scope;
c906108c
SS
21499 break;
21500 case DW_TAG_unspecified_parameters:
21501 /* From varargs functions; gdb doesn't seem to have any
21502 interest in this information, so just ignore it for now.
21503 (FIXME?) */
21504 break;
34eaf542
TT
21505 case DW_TAG_template_type_param:
21506 suppress_add = 1;
21507 /* Fall through. */
c906108c 21508 case DW_TAG_class_type:
680b30c7 21509 case DW_TAG_interface_type:
c906108c
SS
21510 case DW_TAG_structure_type:
21511 case DW_TAG_union_type:
72019c9c 21512 case DW_TAG_set_type:
c906108c 21513 case DW_TAG_enumeration_type:
f1e6e072 21514 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21515 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21516
63d06c5c 21517 {
9c37b5ae 21518 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21519 really ever be static objects: otherwise, if you try
21520 to, say, break of a class's method and you're in a file
21521 which doesn't mention that class, it won't work unless
21522 the check for all static symbols in lookup_symbol_aux
21523 saves you. See the OtherFileClass tests in
21524 gdb.c++/namespace.exp. */
21525
e37fd15a 21526 if (!suppress_add)
34eaf542 21527 {
34eaf542 21528 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 21529 && cu->language == language_cplus
34eaf542 21530 ? &global_symbols : cu->list_in_scope);
63d06c5c 21531
64382290 21532 /* The semantics of C++ state that "struct foo {
9c37b5ae 21533 ... }" also defines a typedef for "foo". */
64382290 21534 if (cu->language == language_cplus
45280282 21535 || cu->language == language_ada
c44af4eb
TT
21536 || cu->language == language_d
21537 || cu->language == language_rust)
64382290
TT
21538 {
21539 /* The symbol's name is already allocated along
21540 with this objfile, so we don't need to
21541 duplicate it for the type. */
21542 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21543 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21544 }
63d06c5c
DC
21545 }
21546 }
c906108c
SS
21547 break;
21548 case DW_TAG_typedef:
f1e6e072 21549 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21550 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21551 list_to_add = cu->list_in_scope;
63d06c5c 21552 break;
c906108c 21553 case DW_TAG_base_type:
a02abb62 21554 case DW_TAG_subrange_type:
f1e6e072 21555 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21556 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21557 list_to_add = cu->list_in_scope;
c906108c
SS
21558 break;
21559 case DW_TAG_enumerator:
e142c38c 21560 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21561 if (attr)
21562 {
e7c27a73 21563 dwarf2_const_value (attr, sym, cu);
c906108c 21564 }
63d06c5c
DC
21565 {
21566 /* NOTE: carlton/2003-11-10: See comment above in the
21567 DW_TAG_class_type, etc. block. */
21568
e142c38c 21569 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 21570 && cu->language == language_cplus
e142c38c 21571 ? &global_symbols : cu->list_in_scope);
63d06c5c 21572 }
c906108c 21573 break;
74921315 21574 case DW_TAG_imported_declaration:
5c4e30ca 21575 case DW_TAG_namespace:
f1e6e072 21576 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 21577 list_to_add = &global_symbols;
5c4e30ca 21578 break;
530e8392
KB
21579 case DW_TAG_module:
21580 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21581 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21582 list_to_add = &global_symbols;
21583 break;
4357ac6c 21584 case DW_TAG_common_block:
f1e6e072 21585 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
21586 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21587 add_symbol_to_list (sym, cu->list_in_scope);
21588 break;
c906108c
SS
21589 default:
21590 /* Not a tag we recognize. Hopefully we aren't processing
21591 trash data, but since we must specifically ignore things
21592 we don't recognize, there is nothing else we should do at
0963b4bd 21593 this point. */
e2e0b3e5 21594 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 21595 dwarf_tag_name (die->tag));
c906108c
SS
21596 break;
21597 }
df8a16a1 21598
e37fd15a
SW
21599 if (suppress_add)
21600 {
21601 sym->hash_next = objfile->template_symbols;
21602 objfile->template_symbols = sym;
21603 list_to_add = NULL;
21604 }
21605
21606 if (list_to_add != NULL)
21607 add_symbol_to_list (sym, list_to_add);
21608
df8a16a1
DJ
21609 /* For the benefit of old versions of GCC, check for anonymous
21610 namespaces based on the demangled name. */
4d4ec4e5 21611 if (!cu->processing_has_namespace_info
94af9270 21612 && cu->language == language_cplus)
a10964d1 21613 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
21614 }
21615 return (sym);
21616}
21617
98bfdba5
PA
21618/* Given an attr with a DW_FORM_dataN value in host byte order,
21619 zero-extend it as appropriate for the symbol's type. The DWARF
21620 standard (v4) is not entirely clear about the meaning of using
21621 DW_FORM_dataN for a constant with a signed type, where the type is
21622 wider than the data. The conclusion of a discussion on the DWARF
21623 list was that this is unspecified. We choose to always zero-extend
21624 because that is the interpretation long in use by GCC. */
c906108c 21625
98bfdba5 21626static gdb_byte *
ff39bb5e 21627dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21628 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21629{
518817b3 21630 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
21631 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21632 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21633 LONGEST l = DW_UNSND (attr);
21634
21635 if (bits < sizeof (*value) * 8)
21636 {
21637 l &= ((LONGEST) 1 << bits) - 1;
21638 *value = l;
21639 }
21640 else if (bits == sizeof (*value) * 8)
21641 *value = l;
21642 else
21643 {
224c3ddb 21644 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21645 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21646 return bytes;
21647 }
21648
21649 return NULL;
21650}
21651
21652/* Read a constant value from an attribute. Either set *VALUE, or if
21653 the value does not fit in *VALUE, set *BYTES - either already
21654 allocated on the objfile obstack, or newly allocated on OBSTACK,
21655 or, set *BATON, if we translated the constant to a location
21656 expression. */
21657
21658static void
ff39bb5e 21659dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21660 const char *name, struct obstack *obstack,
21661 struct dwarf2_cu *cu,
d521ce57 21662 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21663 struct dwarf2_locexpr_baton **baton)
21664{
518817b3 21665 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 21666 struct comp_unit_head *cu_header = &cu->header;
c906108c 21667 struct dwarf_block *blk;
98bfdba5
PA
21668 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21669 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21670
21671 *value = 0;
21672 *bytes = NULL;
21673 *baton = NULL;
c906108c
SS
21674
21675 switch (attr->form)
21676 {
21677 case DW_FORM_addr:
3019eac3 21678 case DW_FORM_GNU_addr_index:
ac56253d 21679 {
ac56253d
TT
21680 gdb_byte *data;
21681
98bfdba5
PA
21682 if (TYPE_LENGTH (type) != cu_header->addr_size)
21683 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21684 cu_header->addr_size,
98bfdba5 21685 TYPE_LENGTH (type));
ac56253d
TT
21686 /* Symbols of this form are reasonably rare, so we just
21687 piggyback on the existing location code rather than writing
21688 a new implementation of symbol_computed_ops. */
8d749320 21689 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
21690 (*baton)->per_cu = cu->per_cu;
21691 gdb_assert ((*baton)->per_cu);
ac56253d 21692
98bfdba5 21693 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21694 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21695 (*baton)->data = data;
ac56253d
TT
21696
21697 data[0] = DW_OP_addr;
21698 store_unsigned_integer (&data[1], cu_header->addr_size,
21699 byte_order, DW_ADDR (attr));
21700 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21701 }
c906108c 21702 break;
4ac36638 21703 case DW_FORM_string:
93b5768b 21704 case DW_FORM_strp:
3019eac3 21705 case DW_FORM_GNU_str_index:
36586728 21706 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21707 /* DW_STRING is already allocated on the objfile obstack, point
21708 directly to it. */
d521ce57 21709 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21710 break;
c906108c
SS
21711 case DW_FORM_block1:
21712 case DW_FORM_block2:
21713 case DW_FORM_block4:
21714 case DW_FORM_block:
2dc7f7b3 21715 case DW_FORM_exprloc:
0224619f 21716 case DW_FORM_data16:
c906108c 21717 blk = DW_BLOCK (attr);
98bfdba5
PA
21718 if (TYPE_LENGTH (type) != blk->size)
21719 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21720 TYPE_LENGTH (type));
21721 *bytes = blk->data;
c906108c 21722 break;
2df3850c
JM
21723
21724 /* The DW_AT_const_value attributes are supposed to carry the
21725 symbol's value "represented as it would be on the target
21726 architecture." By the time we get here, it's already been
21727 converted to host endianness, so we just need to sign- or
21728 zero-extend it as appropriate. */
21729 case DW_FORM_data1:
3aef2284 21730 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21731 break;
c906108c 21732 case DW_FORM_data2:
3aef2284 21733 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21734 break;
c906108c 21735 case DW_FORM_data4:
3aef2284 21736 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21737 break;
c906108c 21738 case DW_FORM_data8:
3aef2284 21739 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21740 break;
21741
c906108c 21742 case DW_FORM_sdata:
663c44ac 21743 case DW_FORM_implicit_const:
98bfdba5 21744 *value = DW_SND (attr);
2df3850c
JM
21745 break;
21746
c906108c 21747 case DW_FORM_udata:
98bfdba5 21748 *value = DW_UNSND (attr);
c906108c 21749 break;
2df3850c 21750
c906108c 21751 default:
4d3c2250 21752 complaint (&symfile_complaints,
e2e0b3e5 21753 _("unsupported const value attribute form: '%s'"),
4d3c2250 21754 dwarf_form_name (attr->form));
98bfdba5 21755 *value = 0;
c906108c
SS
21756 break;
21757 }
21758}
21759
2df3850c 21760
98bfdba5
PA
21761/* Copy constant value from an attribute to a symbol. */
21762
2df3850c 21763static void
ff39bb5e 21764dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21765 struct dwarf2_cu *cu)
2df3850c 21766{
518817b3 21767 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 21768 LONGEST value;
d521ce57 21769 const gdb_byte *bytes;
98bfdba5 21770 struct dwarf2_locexpr_baton *baton;
2df3850c 21771
98bfdba5
PA
21772 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21773 SYMBOL_PRINT_NAME (sym),
21774 &objfile->objfile_obstack, cu,
21775 &value, &bytes, &baton);
2df3850c 21776
98bfdba5
PA
21777 if (baton != NULL)
21778 {
98bfdba5 21779 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21780 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21781 }
21782 else if (bytes != NULL)
21783 {
21784 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21785 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21786 }
21787 else
21788 {
21789 SYMBOL_VALUE (sym) = value;
f1e6e072 21790 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21791 }
2df3850c
JM
21792}
21793
c906108c
SS
21794/* Return the type of the die in question using its DW_AT_type attribute. */
21795
21796static struct type *
e7c27a73 21797die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21798{
c906108c 21799 struct attribute *type_attr;
c906108c 21800
e142c38c 21801 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21802 if (!type_attr)
21803 {
518817b3 21804 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21805 /* A missing DW_AT_type represents a void type. */
518817b3 21806 return objfile_type (objfile)->builtin_void;
c906108c 21807 }
348e048f 21808
673bfd45 21809 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21810}
21811
b4ba55a1
JB
21812/* True iff CU's producer generates GNAT Ada auxiliary information
21813 that allows to find parallel types through that information instead
21814 of having to do expensive parallel lookups by type name. */
21815
21816static int
21817need_gnat_info (struct dwarf2_cu *cu)
21818{
de4cb04a
JB
21819 /* Assume that the Ada compiler was GNAT, which always produces
21820 the auxiliary information. */
21821 return (cu->language == language_ada);
b4ba55a1
JB
21822}
21823
b4ba55a1
JB
21824/* Return the auxiliary type of the die in question using its
21825 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21826 attribute is not present. */
21827
21828static struct type *
21829die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21830{
b4ba55a1 21831 struct attribute *type_attr;
b4ba55a1
JB
21832
21833 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21834 if (!type_attr)
21835 return NULL;
21836
673bfd45 21837 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21838}
21839
21840/* If DIE has a descriptive_type attribute, then set the TYPE's
21841 descriptive type accordingly. */
21842
21843static void
21844set_descriptive_type (struct type *type, struct die_info *die,
21845 struct dwarf2_cu *cu)
21846{
21847 struct type *descriptive_type = die_descriptive_type (die, cu);
21848
21849 if (descriptive_type)
21850 {
21851 ALLOCATE_GNAT_AUX_TYPE (type);
21852 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21853 }
21854}
21855
c906108c
SS
21856/* Return the containing type of the die in question using its
21857 DW_AT_containing_type attribute. */
21858
21859static struct type *
e7c27a73 21860die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21861{
c906108c 21862 struct attribute *type_attr;
518817b3 21863 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21864
e142c38c 21865 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21866 if (!type_attr)
21867 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21868 "[in module %s]"), objfile_name (objfile));
33ac96f0 21869
673bfd45 21870 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21871}
21872
ac9ec31b
DE
21873/* Return an error marker type to use for the ill formed type in DIE/CU. */
21874
21875static struct type *
21876build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21877{
518817b3
SM
21878 struct dwarf2_per_objfile *dwarf2_per_objfile
21879 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
21880 struct objfile *objfile = dwarf2_per_objfile->objfile;
21881 char *message, *saved;
21882
21883 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
4262abfb 21884 objfile_name (objfile),
9c541725
PA
21885 to_underlying (cu->header.sect_off),
21886 to_underlying (die->sect_off));
224c3ddb
SM
21887 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
21888 message, strlen (message));
ac9ec31b
DE
21889 xfree (message);
21890
19f392bc 21891 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21892}
21893
673bfd45 21894/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21895 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21896 DW_AT_containing_type.
673bfd45
DE
21897 If there is no type substitute an error marker. */
21898
c906108c 21899static struct type *
ff39bb5e 21900lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21901 struct dwarf2_cu *cu)
c906108c 21902{
518817b3
SM
21903 struct dwarf2_per_objfile *dwarf2_per_objfile
21904 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21905 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21906 struct type *this_type;
21907
ac9ec31b
DE
21908 gdb_assert (attr->name == DW_AT_type
21909 || attr->name == DW_AT_GNAT_descriptive_type
21910 || attr->name == DW_AT_containing_type);
21911
673bfd45
DE
21912 /* First see if we have it cached. */
21913
36586728
TT
21914 if (attr->form == DW_FORM_GNU_ref_alt)
21915 {
21916 struct dwarf2_per_cu_data *per_cu;
9c541725 21917 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 21918
ed2dc618
SM
21919 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21920 dwarf2_per_objfile);
9c541725 21921 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21922 }
7771576e 21923 else if (attr_form_is_ref (attr))
673bfd45 21924 {
9c541725 21925 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 21926
9c541725 21927 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21928 }
55f1336d 21929 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21930 {
ac9ec31b 21931 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21932
ac9ec31b 21933 return get_signatured_type (die, signature, cu);
673bfd45
DE
21934 }
21935 else
21936 {
ac9ec31b
DE
21937 complaint (&symfile_complaints,
21938 _("Dwarf Error: Bad type attribute %s in DIE"
21939 " at 0x%x [in module %s]"),
9c541725 21940 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
4262abfb 21941 objfile_name (objfile));
ac9ec31b 21942 return build_error_marker_type (cu, die);
673bfd45
DE
21943 }
21944
21945 /* If not cached we need to read it in. */
21946
21947 if (this_type == NULL)
21948 {
ac9ec31b 21949 struct die_info *type_die = NULL;
673bfd45
DE
21950 struct dwarf2_cu *type_cu = cu;
21951
7771576e 21952 if (attr_form_is_ref (attr))
ac9ec31b
DE
21953 type_die = follow_die_ref (die, attr, &type_cu);
21954 if (type_die == NULL)
21955 return build_error_marker_type (cu, die);
21956 /* If we find the type now, it's probably because the type came
3019eac3
DE
21957 from an inter-CU reference and the type's CU got expanded before
21958 ours. */
ac9ec31b 21959 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21960 }
21961
21962 /* If we still don't have a type use an error marker. */
21963
21964 if (this_type == NULL)
ac9ec31b 21965 return build_error_marker_type (cu, die);
673bfd45 21966
f792889a 21967 return this_type;
c906108c
SS
21968}
21969
673bfd45
DE
21970/* Return the type in DIE, CU.
21971 Returns NULL for invalid types.
21972
02142a6c 21973 This first does a lookup in die_type_hash,
673bfd45
DE
21974 and only reads the die in if necessary.
21975
21976 NOTE: This can be called when reading in partial or full symbols. */
21977
f792889a 21978static struct type *
e7c27a73 21979read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21980{
f792889a
DJ
21981 struct type *this_type;
21982
21983 this_type = get_die_type (die, cu);
21984 if (this_type)
21985 return this_type;
21986
673bfd45
DE
21987 return read_type_die_1 (die, cu);
21988}
21989
21990/* Read the type in DIE, CU.
21991 Returns NULL for invalid types. */
21992
21993static struct type *
21994read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21995{
21996 struct type *this_type = NULL;
21997
c906108c
SS
21998 switch (die->tag)
21999 {
22000 case DW_TAG_class_type:
680b30c7 22001 case DW_TAG_interface_type:
c906108c
SS
22002 case DW_TAG_structure_type:
22003 case DW_TAG_union_type:
f792889a 22004 this_type = read_structure_type (die, cu);
c906108c
SS
22005 break;
22006 case DW_TAG_enumeration_type:
f792889a 22007 this_type = read_enumeration_type (die, cu);
c906108c
SS
22008 break;
22009 case DW_TAG_subprogram:
22010 case DW_TAG_subroutine_type:
edb3359d 22011 case DW_TAG_inlined_subroutine:
f792889a 22012 this_type = read_subroutine_type (die, cu);
c906108c
SS
22013 break;
22014 case DW_TAG_array_type:
f792889a 22015 this_type = read_array_type (die, cu);
c906108c 22016 break;
72019c9c 22017 case DW_TAG_set_type:
f792889a 22018 this_type = read_set_type (die, cu);
72019c9c 22019 break;
c906108c 22020 case DW_TAG_pointer_type:
f792889a 22021 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22022 break;
22023 case DW_TAG_ptr_to_member_type:
f792889a 22024 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22025 break;
22026 case DW_TAG_reference_type:
4297a3f0
AV
22027 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22028 break;
22029 case DW_TAG_rvalue_reference_type:
22030 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22031 break;
22032 case DW_TAG_const_type:
f792889a 22033 this_type = read_tag_const_type (die, cu);
c906108c
SS
22034 break;
22035 case DW_TAG_volatile_type:
f792889a 22036 this_type = read_tag_volatile_type (die, cu);
c906108c 22037 break;
06d66ee9
TT
22038 case DW_TAG_restrict_type:
22039 this_type = read_tag_restrict_type (die, cu);
22040 break;
c906108c 22041 case DW_TAG_string_type:
f792889a 22042 this_type = read_tag_string_type (die, cu);
c906108c
SS
22043 break;
22044 case DW_TAG_typedef:
f792889a 22045 this_type = read_typedef (die, cu);
c906108c 22046 break;
a02abb62 22047 case DW_TAG_subrange_type:
f792889a 22048 this_type = read_subrange_type (die, cu);
a02abb62 22049 break;
c906108c 22050 case DW_TAG_base_type:
f792889a 22051 this_type = read_base_type (die, cu);
c906108c 22052 break;
81a17f79 22053 case DW_TAG_unspecified_type:
f792889a 22054 this_type = read_unspecified_type (die, cu);
81a17f79 22055 break;
0114d602
DJ
22056 case DW_TAG_namespace:
22057 this_type = read_namespace_type (die, cu);
22058 break;
f55ee35c
JK
22059 case DW_TAG_module:
22060 this_type = read_module_type (die, cu);
22061 break;
a2c2acaf
MW
22062 case DW_TAG_atomic_type:
22063 this_type = read_tag_atomic_type (die, cu);
22064 break;
c906108c 22065 default:
3e43a32a
MS
22066 complaint (&symfile_complaints,
22067 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 22068 dwarf_tag_name (die->tag));
c906108c
SS
22069 break;
22070 }
63d06c5c 22071
f792889a 22072 return this_type;
63d06c5c
DC
22073}
22074
abc72ce4
DE
22075/* See if we can figure out if the class lives in a namespace. We do
22076 this by looking for a member function; its demangled name will
22077 contain namespace info, if there is any.
22078 Return the computed name or NULL.
22079 Space for the result is allocated on the objfile's obstack.
22080 This is the full-die version of guess_partial_die_structure_name.
22081 In this case we know DIE has no useful parent. */
22082
22083static char *
22084guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22085{
22086 struct die_info *spec_die;
22087 struct dwarf2_cu *spec_cu;
22088 struct die_info *child;
518817b3 22089 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
22090
22091 spec_cu = cu;
22092 spec_die = die_specification (die, &spec_cu);
22093 if (spec_die != NULL)
22094 {
22095 die = spec_die;
22096 cu = spec_cu;
22097 }
22098
22099 for (child = die->child;
22100 child != NULL;
22101 child = child->sibling)
22102 {
22103 if (child->tag == DW_TAG_subprogram)
22104 {
73b9be8b 22105 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22106
7d45c7c3 22107 if (linkage_name != NULL)
abc72ce4
DE
22108 {
22109 char *actual_name
22110 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 22111 linkage_name);
abc72ce4
DE
22112 char *name = NULL;
22113
22114 if (actual_name != NULL)
22115 {
15d034d0 22116 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22117
22118 if (die_name != NULL
22119 && strcmp (die_name, actual_name) != 0)
22120 {
22121 /* Strip off the class name from the full name.
22122 We want the prefix. */
22123 int die_name_len = strlen (die_name);
22124 int actual_name_len = strlen (actual_name);
22125
22126 /* Test for '::' as a sanity check. */
22127 if (actual_name_len > die_name_len + 2
3e43a32a
MS
22128 && actual_name[actual_name_len
22129 - die_name_len - 1] == ':')
224c3ddb 22130 name = (char *) obstack_copy0 (
e3b94546 22131 &objfile->per_bfd->storage_obstack,
224c3ddb 22132 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
22133 }
22134 }
22135 xfree (actual_name);
22136 return name;
22137 }
22138 }
22139 }
22140
22141 return NULL;
22142}
22143
96408a79
SA
22144/* GCC might emit a nameless typedef that has a linkage name. Determine the
22145 prefix part in such case. See
22146 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22147
a121b7c1 22148static const char *
96408a79
SA
22149anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22150{
22151 struct attribute *attr;
e6a959d6 22152 const char *base;
96408a79
SA
22153
22154 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22155 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22156 return NULL;
22157
7d45c7c3 22158 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22159 return NULL;
22160
73b9be8b 22161 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22162 if (attr == NULL || DW_STRING (attr) == NULL)
22163 return NULL;
22164
22165 /* dwarf2_name had to be already called. */
22166 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22167
22168 /* Strip the base name, keep any leading namespaces/classes. */
22169 base = strrchr (DW_STRING (attr), ':');
22170 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22171 return "";
22172
518817b3 22173 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e3b94546 22174 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb
SM
22175 DW_STRING (attr),
22176 &base[-1] - DW_STRING (attr));
96408a79
SA
22177}
22178
fdde2d81 22179/* Return the name of the namespace/class that DIE is defined within,
0114d602 22180 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22181
0114d602
DJ
22182 For example, if we're within the method foo() in the following
22183 code:
22184
22185 namespace N {
22186 class C {
22187 void foo () {
22188 }
22189 };
22190 }
22191
22192 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22193
0d5cff50 22194static const char *
e142c38c 22195determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22196{
518817b3
SM
22197 struct dwarf2_per_objfile *dwarf2_per_objfile
22198 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22199 struct die_info *parent, *spec_die;
22200 struct dwarf2_cu *spec_cu;
22201 struct type *parent_type;
a121b7c1 22202 const char *retval;
63d06c5c 22203
9c37b5ae 22204 if (cu->language != language_cplus
c44af4eb
TT
22205 && cu->language != language_fortran && cu->language != language_d
22206 && cu->language != language_rust)
0114d602
DJ
22207 return "";
22208
96408a79
SA
22209 retval = anonymous_struct_prefix (die, cu);
22210 if (retval)
22211 return retval;
22212
0114d602
DJ
22213 /* We have to be careful in the presence of DW_AT_specification.
22214 For example, with GCC 3.4, given the code
22215
22216 namespace N {
22217 void foo() {
22218 // Definition of N::foo.
22219 }
22220 }
22221
22222 then we'll have a tree of DIEs like this:
22223
22224 1: DW_TAG_compile_unit
22225 2: DW_TAG_namespace // N
22226 3: DW_TAG_subprogram // declaration of N::foo
22227 4: DW_TAG_subprogram // definition of N::foo
22228 DW_AT_specification // refers to die #3
22229
22230 Thus, when processing die #4, we have to pretend that we're in
22231 the context of its DW_AT_specification, namely the contex of die
22232 #3. */
22233 spec_cu = cu;
22234 spec_die = die_specification (die, &spec_cu);
22235 if (spec_die == NULL)
22236 parent = die->parent;
22237 else
63d06c5c 22238 {
0114d602
DJ
22239 parent = spec_die->parent;
22240 cu = spec_cu;
63d06c5c 22241 }
0114d602
DJ
22242
22243 if (parent == NULL)
22244 return "";
98bfdba5
PA
22245 else if (parent->building_fullname)
22246 {
22247 const char *name;
22248 const char *parent_name;
22249
22250 /* It has been seen on RealView 2.2 built binaries,
22251 DW_TAG_template_type_param types actually _defined_ as
22252 children of the parent class:
22253
22254 enum E {};
22255 template class <class Enum> Class{};
22256 Class<enum E> class_e;
22257
22258 1: DW_TAG_class_type (Class)
22259 2: DW_TAG_enumeration_type (E)
22260 3: DW_TAG_enumerator (enum1:0)
22261 3: DW_TAG_enumerator (enum2:1)
22262 ...
22263 2: DW_TAG_template_type_param
22264 DW_AT_type DW_FORM_ref_udata (E)
22265
22266 Besides being broken debug info, it can put GDB into an
22267 infinite loop. Consider:
22268
22269 When we're building the full name for Class<E>, we'll start
22270 at Class, and go look over its template type parameters,
22271 finding E. We'll then try to build the full name of E, and
22272 reach here. We're now trying to build the full name of E,
22273 and look over the parent DIE for containing scope. In the
22274 broken case, if we followed the parent DIE of E, we'd again
22275 find Class, and once again go look at its template type
22276 arguments, etc., etc. Simply don't consider such parent die
22277 as source-level parent of this die (it can't be, the language
22278 doesn't allow it), and break the loop here. */
22279 name = dwarf2_name (die, cu);
22280 parent_name = dwarf2_name (parent, cu);
22281 complaint (&symfile_complaints,
22282 _("template param type '%s' defined within parent '%s'"),
22283 name ? name : "<unknown>",
22284 parent_name ? parent_name : "<unknown>");
22285 return "";
22286 }
63d06c5c 22287 else
0114d602
DJ
22288 switch (parent->tag)
22289 {
63d06c5c 22290 case DW_TAG_namespace:
0114d602 22291 parent_type = read_type_die (parent, cu);
acebe513
UW
22292 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22293 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22294 Work around this problem here. */
22295 if (cu->language == language_cplus
22296 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22297 return "";
0114d602
DJ
22298 /* We give a name to even anonymous namespaces. */
22299 return TYPE_TAG_NAME (parent_type);
63d06c5c 22300 case DW_TAG_class_type:
680b30c7 22301 case DW_TAG_interface_type:
63d06c5c 22302 case DW_TAG_structure_type:
0114d602 22303 case DW_TAG_union_type:
f55ee35c 22304 case DW_TAG_module:
0114d602
DJ
22305 parent_type = read_type_die (parent, cu);
22306 if (TYPE_TAG_NAME (parent_type) != NULL)
22307 return TYPE_TAG_NAME (parent_type);
22308 else
22309 /* An anonymous structure is only allowed non-static data
22310 members; no typedefs, no member functions, et cetera.
22311 So it does not need a prefix. */
22312 return "";
abc72ce4 22313 case DW_TAG_compile_unit:
95554aad 22314 case DW_TAG_partial_unit:
abc72ce4
DE
22315 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22316 if (cu->language == language_cplus
8b70b953 22317 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
22318 && die->child != NULL
22319 && (die->tag == DW_TAG_class_type
22320 || die->tag == DW_TAG_structure_type
22321 || die->tag == DW_TAG_union_type))
22322 {
22323 char *name = guess_full_die_structure_name (die, cu);
22324 if (name != NULL)
22325 return name;
22326 }
22327 return "";
3d567982
TT
22328 case DW_TAG_enumeration_type:
22329 parent_type = read_type_die (parent, cu);
22330 if (TYPE_DECLARED_CLASS (parent_type))
22331 {
22332 if (TYPE_TAG_NAME (parent_type) != NULL)
22333 return TYPE_TAG_NAME (parent_type);
22334 return "";
22335 }
22336 /* Fall through. */
63d06c5c 22337 default:
8176b9b8 22338 return determine_prefix (parent, cu);
63d06c5c 22339 }
63d06c5c
DC
22340}
22341
3e43a32a
MS
22342/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22343 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22344 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22345 an obconcat, otherwise allocate storage for the result. The CU argument is
22346 used to determine the language and hence, the appropriate separator. */
987504bb 22347
f55ee35c 22348#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22349
22350static char *
f55ee35c
JK
22351typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22352 int physname, struct dwarf2_cu *cu)
63d06c5c 22353{
f55ee35c 22354 const char *lead = "";
5c315b68 22355 const char *sep;
63d06c5c 22356
3e43a32a
MS
22357 if (suffix == NULL || suffix[0] == '\0'
22358 || prefix == NULL || prefix[0] == '\0')
987504bb 22359 sep = "";
45280282
IB
22360 else if (cu->language == language_d)
22361 {
22362 /* For D, the 'main' function could be defined in any module, but it
22363 should never be prefixed. */
22364 if (strcmp (suffix, "D main") == 0)
22365 {
22366 prefix = "";
22367 sep = "";
22368 }
22369 else
22370 sep = ".";
22371 }
f55ee35c
JK
22372 else if (cu->language == language_fortran && physname)
22373 {
22374 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22375 DW_AT_MIPS_linkage_name is preferred and used instead. */
22376
22377 lead = "__";
22378 sep = "_MOD_";
22379 }
987504bb
JJ
22380 else
22381 sep = "::";
63d06c5c 22382
6dd47d34
DE
22383 if (prefix == NULL)
22384 prefix = "";
22385 if (suffix == NULL)
22386 suffix = "";
22387
987504bb
JJ
22388 if (obs == NULL)
22389 {
3e43a32a 22390 char *retval
224c3ddb
SM
22391 = ((char *)
22392 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22393
f55ee35c
JK
22394 strcpy (retval, lead);
22395 strcat (retval, prefix);
6dd47d34
DE
22396 strcat (retval, sep);
22397 strcat (retval, suffix);
63d06c5c
DC
22398 return retval;
22399 }
987504bb
JJ
22400 else
22401 {
22402 /* We have an obstack. */
f55ee35c 22403 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22404 }
63d06c5c
DC
22405}
22406
c906108c
SS
22407/* Return sibling of die, NULL if no sibling. */
22408
f9aca02d 22409static struct die_info *
fba45db2 22410sibling_die (struct die_info *die)
c906108c 22411{
639d11d3 22412 return die->sibling;
c906108c
SS
22413}
22414
71c25dea
TT
22415/* Get name of a die, return NULL if not found. */
22416
15d034d0
TT
22417static const char *
22418dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
22419 struct obstack *obstack)
22420{
22421 if (name && cu->language == language_cplus)
22422 {
2f408ecb 22423 std::string canon_name = cp_canonicalize_string (name);
71c25dea 22424
2f408ecb 22425 if (!canon_name.empty ())
71c25dea 22426 {
2f408ecb
PA
22427 if (canon_name != name)
22428 name = (const char *) obstack_copy0 (obstack,
22429 canon_name.c_str (),
22430 canon_name.length ());
71c25dea
TT
22431 }
22432 }
22433
22434 return name;
c906108c
SS
22435}
22436
96553a0c
DE
22437/* Get name of a die, return NULL if not found.
22438 Anonymous namespaces are converted to their magic string. */
9219021c 22439
15d034d0 22440static const char *
e142c38c 22441dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22442{
22443 struct attribute *attr;
518817b3 22444 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 22445
e142c38c 22446 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22447 if ((!attr || !DW_STRING (attr))
96553a0c 22448 && die->tag != DW_TAG_namespace
53832f31
TT
22449 && die->tag != DW_TAG_class_type
22450 && die->tag != DW_TAG_interface_type
22451 && die->tag != DW_TAG_structure_type
22452 && die->tag != DW_TAG_union_type)
71c25dea
TT
22453 return NULL;
22454
22455 switch (die->tag)
22456 {
22457 case DW_TAG_compile_unit:
95554aad 22458 case DW_TAG_partial_unit:
71c25dea
TT
22459 /* Compilation units have a DW_AT_name that is a filename, not
22460 a source language identifier. */
22461 case DW_TAG_enumeration_type:
22462 case DW_TAG_enumerator:
22463 /* These tags always have simple identifiers already; no need
22464 to canonicalize them. */
22465 return DW_STRING (attr);
907af001 22466
96553a0c
DE
22467 case DW_TAG_namespace:
22468 if (attr != NULL && DW_STRING (attr) != NULL)
22469 return DW_STRING (attr);
22470 return CP_ANONYMOUS_NAMESPACE_STR;
22471
907af001
UW
22472 case DW_TAG_class_type:
22473 case DW_TAG_interface_type:
22474 case DW_TAG_structure_type:
22475 case DW_TAG_union_type:
22476 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22477 structures or unions. These were of the form "._%d" in GCC 4.1,
22478 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22479 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22480 if (attr && DW_STRING (attr)
61012eef
GB
22481 && (startswith (DW_STRING (attr), "._")
22482 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22483 return NULL;
53832f31
TT
22484
22485 /* GCC might emit a nameless typedef that has a linkage name. See
22486 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22487 if (!attr || DW_STRING (attr) == NULL)
22488 {
df5c6c50 22489 char *demangled = NULL;
53832f31 22490
73b9be8b 22491 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22492 if (attr == NULL || DW_STRING (attr) == NULL)
22493 return NULL;
22494
df5c6c50
JK
22495 /* Avoid demangling DW_STRING (attr) the second time on a second
22496 call for the same DIE. */
22497 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 22498 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
22499
22500 if (demangled)
22501 {
e6a959d6 22502 const char *base;
96408a79 22503
53832f31 22504 /* FIXME: we already did this for the partial symbol... */
34a68019 22505 DW_STRING (attr)
224c3ddb 22506 = ((const char *)
e3b94546 22507 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb 22508 demangled, strlen (demangled)));
53832f31
TT
22509 DW_STRING_IS_CANONICAL (attr) = 1;
22510 xfree (demangled);
96408a79
SA
22511
22512 /* Strip any leading namespaces/classes, keep only the base name.
22513 DW_AT_name for named DIEs does not contain the prefixes. */
22514 base = strrchr (DW_STRING (attr), ':');
22515 if (base && base > DW_STRING (attr) && base[-1] == ':')
22516 return &base[1];
22517 else
22518 return DW_STRING (attr);
53832f31
TT
22519 }
22520 }
907af001
UW
22521 break;
22522
71c25dea 22523 default:
907af001
UW
22524 break;
22525 }
22526
22527 if (!DW_STRING_IS_CANONICAL (attr))
22528 {
22529 DW_STRING (attr)
22530 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 22531 &objfile->per_bfd->storage_obstack);
907af001 22532 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22533 }
907af001 22534 return DW_STRING (attr);
9219021c
DC
22535}
22536
22537/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22538 is none. *EXT_CU is the CU containing DIE on input, and the CU
22539 containing the return value on output. */
9219021c
DC
22540
22541static struct die_info *
f2f0e013 22542dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22543{
22544 struct attribute *attr;
9219021c 22545
f2f0e013 22546 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22547 if (attr == NULL)
22548 return NULL;
22549
f2f0e013 22550 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22551}
22552
c906108c
SS
22553/* Convert a DIE tag into its string name. */
22554
f39c6ffd 22555static const char *
aa1ee363 22556dwarf_tag_name (unsigned tag)
c906108c 22557{
f39c6ffd
TT
22558 const char *name = get_DW_TAG_name (tag);
22559
22560 if (name == NULL)
22561 return "DW_TAG_<unknown>";
22562
22563 return name;
c906108c
SS
22564}
22565
22566/* Convert a DWARF attribute code into its string name. */
22567
f39c6ffd 22568static const char *
aa1ee363 22569dwarf_attr_name (unsigned attr)
c906108c 22570{
f39c6ffd
TT
22571 const char *name;
22572
c764a876 22573#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
22574 if (attr == DW_AT_MIPS_fde)
22575 return "DW_AT_MIPS_fde";
22576#else
22577 if (attr == DW_AT_HP_block_index)
22578 return "DW_AT_HP_block_index";
c764a876 22579#endif
f39c6ffd
TT
22580
22581 name = get_DW_AT_name (attr);
22582
22583 if (name == NULL)
22584 return "DW_AT_<unknown>";
22585
22586 return name;
c906108c
SS
22587}
22588
22589/* Convert a DWARF value form code into its string name. */
22590
f39c6ffd 22591static const char *
aa1ee363 22592dwarf_form_name (unsigned form)
c906108c 22593{
f39c6ffd
TT
22594 const char *name = get_DW_FORM_name (form);
22595
22596 if (name == NULL)
22597 return "DW_FORM_<unknown>";
22598
22599 return name;
c906108c
SS
22600}
22601
a121b7c1 22602static const char *
fba45db2 22603dwarf_bool_name (unsigned mybool)
c906108c
SS
22604{
22605 if (mybool)
22606 return "TRUE";
22607 else
22608 return "FALSE";
22609}
22610
22611/* Convert a DWARF type code into its string name. */
22612
f39c6ffd 22613static const char *
aa1ee363 22614dwarf_type_encoding_name (unsigned enc)
c906108c 22615{
f39c6ffd 22616 const char *name = get_DW_ATE_name (enc);
c906108c 22617
f39c6ffd
TT
22618 if (name == NULL)
22619 return "DW_ATE_<unknown>";
c906108c 22620
f39c6ffd 22621 return name;
c906108c 22622}
c906108c 22623
f9aca02d 22624static void
d97bc12b 22625dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22626{
22627 unsigned int i;
22628
d97bc12b
DE
22629 print_spaces (indent, f);
22630 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
9c541725
PA
22631 dwarf_tag_name (die->tag), die->abbrev,
22632 to_underlying (die->sect_off));
d97bc12b
DE
22633
22634 if (die->parent != NULL)
22635 {
22636 print_spaces (indent, f);
22637 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
9c541725 22638 to_underlying (die->parent->sect_off));
d97bc12b
DE
22639 }
22640
22641 print_spaces (indent, f);
22642 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22643 dwarf_bool_name (die->child != NULL));
c906108c 22644
d97bc12b
DE
22645 print_spaces (indent, f);
22646 fprintf_unfiltered (f, " attributes:\n");
22647
c906108c
SS
22648 for (i = 0; i < die->num_attrs; ++i)
22649 {
d97bc12b
DE
22650 print_spaces (indent, f);
22651 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22652 dwarf_attr_name (die->attrs[i].name),
22653 dwarf_form_name (die->attrs[i].form));
d97bc12b 22654
c906108c
SS
22655 switch (die->attrs[i].form)
22656 {
c906108c 22657 case DW_FORM_addr:
3019eac3 22658 case DW_FORM_GNU_addr_index:
d97bc12b 22659 fprintf_unfiltered (f, "address: ");
5af949e3 22660 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22661 break;
22662 case DW_FORM_block2:
22663 case DW_FORM_block4:
22664 case DW_FORM_block:
22665 case DW_FORM_block1:
56eb65bd
SP
22666 fprintf_unfiltered (f, "block: size %s",
22667 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22668 break;
2dc7f7b3 22669 case DW_FORM_exprloc:
56eb65bd
SP
22670 fprintf_unfiltered (f, "expression: size %s",
22671 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22672 break;
0224619f
JK
22673 case DW_FORM_data16:
22674 fprintf_unfiltered (f, "constant of 16 bytes");
22675 break;
4568ecf9
DE
22676 case DW_FORM_ref_addr:
22677 fprintf_unfiltered (f, "ref address: ");
22678 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22679 break;
36586728
TT
22680 case DW_FORM_GNU_ref_alt:
22681 fprintf_unfiltered (f, "alt ref address: ");
22682 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22683 break;
10b3939b
DJ
22684 case DW_FORM_ref1:
22685 case DW_FORM_ref2:
22686 case DW_FORM_ref4:
4568ecf9
DE
22687 case DW_FORM_ref8:
22688 case DW_FORM_ref_udata:
d97bc12b 22689 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22690 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22691 break;
c906108c
SS
22692 case DW_FORM_data1:
22693 case DW_FORM_data2:
22694 case DW_FORM_data4:
ce5d95e1 22695 case DW_FORM_data8:
c906108c
SS
22696 case DW_FORM_udata:
22697 case DW_FORM_sdata:
43bbcdc2
PH
22698 fprintf_unfiltered (f, "constant: %s",
22699 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22700 break;
2dc7f7b3
TT
22701 case DW_FORM_sec_offset:
22702 fprintf_unfiltered (f, "section offset: %s",
22703 pulongest (DW_UNSND (&die->attrs[i])));
22704 break;
55f1336d 22705 case DW_FORM_ref_sig8:
ac9ec31b
DE
22706 fprintf_unfiltered (f, "signature: %s",
22707 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22708 break;
c906108c 22709 case DW_FORM_string:
4bdf3d34 22710 case DW_FORM_strp:
43988095 22711 case DW_FORM_line_strp:
3019eac3 22712 case DW_FORM_GNU_str_index:
36586728 22713 case DW_FORM_GNU_strp_alt:
8285870a 22714 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22715 DW_STRING (&die->attrs[i])
8285870a
JK
22716 ? DW_STRING (&die->attrs[i]) : "",
22717 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22718 break;
22719 case DW_FORM_flag:
22720 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22721 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22722 else
d97bc12b 22723 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22724 break;
2dc7f7b3
TT
22725 case DW_FORM_flag_present:
22726 fprintf_unfiltered (f, "flag: TRUE");
22727 break;
a8329558 22728 case DW_FORM_indirect:
0963b4bd
MS
22729 /* The reader will have reduced the indirect form to
22730 the "base form" so this form should not occur. */
3e43a32a
MS
22731 fprintf_unfiltered (f,
22732 "unexpected attribute form: DW_FORM_indirect");
a8329558 22733 break;
663c44ac
JK
22734 case DW_FORM_implicit_const:
22735 fprintf_unfiltered (f, "constant: %s",
22736 plongest (DW_SND (&die->attrs[i])));
22737 break;
c906108c 22738 default:
d97bc12b 22739 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22740 die->attrs[i].form);
d97bc12b 22741 break;
c906108c 22742 }
d97bc12b 22743 fprintf_unfiltered (f, "\n");
c906108c
SS
22744 }
22745}
22746
f9aca02d 22747static void
d97bc12b 22748dump_die_for_error (struct die_info *die)
c906108c 22749{
d97bc12b
DE
22750 dump_die_shallow (gdb_stderr, 0, die);
22751}
22752
22753static void
22754dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22755{
22756 int indent = level * 4;
22757
22758 gdb_assert (die != NULL);
22759
22760 if (level >= max_level)
22761 return;
22762
22763 dump_die_shallow (f, indent, die);
22764
22765 if (die->child != NULL)
c906108c 22766 {
d97bc12b
DE
22767 print_spaces (indent, f);
22768 fprintf_unfiltered (f, " Children:");
22769 if (level + 1 < max_level)
22770 {
22771 fprintf_unfiltered (f, "\n");
22772 dump_die_1 (f, level + 1, max_level, die->child);
22773 }
22774 else
22775 {
3e43a32a
MS
22776 fprintf_unfiltered (f,
22777 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22778 }
22779 }
22780
22781 if (die->sibling != NULL && level > 0)
22782 {
22783 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22784 }
22785}
22786
d97bc12b
DE
22787/* This is called from the pdie macro in gdbinit.in.
22788 It's not static so gcc will keep a copy callable from gdb. */
22789
22790void
22791dump_die (struct die_info *die, int max_level)
22792{
22793 dump_die_1 (gdb_stdlog, 0, max_level, die);
22794}
22795
f9aca02d 22796static void
51545339 22797store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22798{
51545339 22799 void **slot;
c906108c 22800
9c541725
PA
22801 slot = htab_find_slot_with_hash (cu->die_hash, die,
22802 to_underlying (die->sect_off),
b64f50a1 22803 INSERT);
51545339
DJ
22804
22805 *slot = die;
c906108c
SS
22806}
22807
b64f50a1
JK
22808/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22809 required kind. */
22810
22811static sect_offset
ff39bb5e 22812dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 22813{
7771576e 22814 if (attr_form_is_ref (attr))
9c541725 22815 return (sect_offset) DW_UNSND (attr);
93311388
DE
22816
22817 complaint (&symfile_complaints,
22818 _("unsupported die ref attribute form: '%s'"),
22819 dwarf_form_name (attr->form));
9c541725 22820 return {};
c906108c
SS
22821}
22822
43bbcdc2
PH
22823/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22824 * the value held by the attribute is not constant. */
a02abb62 22825
43bbcdc2 22826static LONGEST
ff39bb5e 22827dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 22828{
663c44ac 22829 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
22830 return DW_SND (attr);
22831 else if (attr->form == DW_FORM_udata
22832 || attr->form == DW_FORM_data1
22833 || attr->form == DW_FORM_data2
22834 || attr->form == DW_FORM_data4
22835 || attr->form == DW_FORM_data8)
22836 return DW_UNSND (attr);
22837 else
22838 {
0224619f 22839 /* For DW_FORM_data16 see attr_form_is_constant. */
3e43a32a
MS
22840 complaint (&symfile_complaints,
22841 _("Attribute value is not a constant (%s)"),
a02abb62
JB
22842 dwarf_form_name (attr->form));
22843 return default_value;
22844 }
22845}
22846
348e048f
DE
22847/* Follow reference or signature attribute ATTR of SRC_DIE.
22848 On entry *REF_CU is the CU of SRC_DIE.
22849 On exit *REF_CU is the CU of the result. */
22850
22851static struct die_info *
ff39bb5e 22852follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22853 struct dwarf2_cu **ref_cu)
22854{
22855 struct die_info *die;
22856
7771576e 22857 if (attr_form_is_ref (attr))
348e048f 22858 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22859 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22860 die = follow_die_sig (src_die, attr, ref_cu);
22861 else
22862 {
22863 dump_die_for_error (src_die);
22864 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 22865 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
22866 }
22867
22868 return die;
03dd20cc
DJ
22869}
22870
5c631832 22871/* Follow reference OFFSET.
673bfd45
DE
22872 On entry *REF_CU is the CU of the source die referencing OFFSET.
22873 On exit *REF_CU is the CU of the result.
22874 Returns NULL if OFFSET is invalid. */
f504f079 22875
f9aca02d 22876static struct die_info *
9c541725 22877follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22878 struct dwarf2_cu **ref_cu)
c906108c 22879{
10b3939b 22880 struct die_info temp_die;
f2f0e013 22881 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
22882 struct dwarf2_per_objfile *dwarf2_per_objfile
22883 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22884 struct objfile *objfile = dwarf2_per_objfile->objfile;
10b3939b 22885
348e048f
DE
22886 gdb_assert (cu->per_cu != NULL);
22887
98bfdba5
PA
22888 target_cu = cu;
22889
3019eac3 22890 if (cu->per_cu->is_debug_types)
348e048f
DE
22891 {
22892 /* .debug_types CUs cannot reference anything outside their CU.
22893 If they need to, they have to reference a signatured type via
55f1336d 22894 DW_FORM_ref_sig8. */
9c541725 22895 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 22896 return NULL;
348e048f 22897 }
36586728 22898 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 22899 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
22900 {
22901 struct dwarf2_per_cu_data *per_cu;
9a619af0 22902
9c541725 22903 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22904 dwarf2_per_objfile);
03dd20cc
DJ
22905
22906 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
22907 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22908 load_full_comp_unit (per_cu, cu->language);
03dd20cc 22909
10b3939b
DJ
22910 target_cu = per_cu->cu;
22911 }
98bfdba5
PA
22912 else if (cu->dies == NULL)
22913 {
22914 /* We're loading full DIEs during partial symbol reading. */
22915 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 22916 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 22917 }
c906108c 22918
f2f0e013 22919 *ref_cu = target_cu;
9c541725 22920 temp_die.sect_off = sect_off;
9a3c8263 22921 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22922 &temp_die,
22923 to_underlying (sect_off));
5c631832 22924}
10b3939b 22925
5c631832
JK
22926/* Follow reference attribute ATTR of SRC_DIE.
22927 On entry *REF_CU is the CU of SRC_DIE.
22928 On exit *REF_CU is the CU of the result. */
22929
22930static struct die_info *
ff39bb5e 22931follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22932 struct dwarf2_cu **ref_cu)
22933{
9c541725 22934 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
22935 struct dwarf2_cu *cu = *ref_cu;
22936 struct die_info *die;
22937
9c541725 22938 die = follow_die_offset (sect_off,
36586728
TT
22939 (attr->form == DW_FORM_GNU_ref_alt
22940 || cu->per_cu->is_dwz),
22941 ref_cu);
5c631832
JK
22942 if (!die)
22943 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
22944 "at 0x%x [in module %s]"),
9c541725 22945 to_underlying (sect_off), to_underlying (src_die->sect_off),
518817b3 22946 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 22947
5c631832
JK
22948 return die;
22949}
22950
9c541725 22951/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 22952 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
22953 dwarf2_locexpr_baton->data has lifetime of
22954 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
22955
22956struct dwarf2_locexpr_baton
9c541725 22957dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
22958 struct dwarf2_per_cu_data *per_cu,
22959 CORE_ADDR (*get_frame_pc) (void *baton),
22960 void *baton)
5c631832 22961{
918dd910 22962 struct dwarf2_cu *cu;
5c631832
JK
22963 struct die_info *die;
22964 struct attribute *attr;
22965 struct dwarf2_locexpr_baton retval;
e3b94546 22966 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ed2dc618
SM
22967 struct dwarf2_per_objfile *dwarf2_per_objfile
22968 = get_dwarf2_per_objfile (objfile);
8cf6f0b1 22969
918dd910
JK
22970 if (per_cu->cu == NULL)
22971 load_cu (per_cu);
22972 cu = per_cu->cu;
cc12ce38
DE
22973 if (cu == NULL)
22974 {
22975 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22976 Instead just throw an error, not much else we can do. */
22977 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
e3b94546 22978 to_underlying (sect_off), objfile_name (objfile));
cc12ce38 22979 }
918dd910 22980
9c541725 22981 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832
JK
22982 if (!die)
22983 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
e3b94546 22984 to_underlying (sect_off), objfile_name (objfile));
5c631832
JK
22985
22986 attr = dwarf2_attr (die, DW_AT_location, cu);
22987 if (!attr)
22988 {
e103e986
JK
22989 /* DWARF: "If there is no such attribute, then there is no effect.".
22990 DATA is ignored if SIZE is 0. */
5c631832 22991
e103e986 22992 retval.data = NULL;
5c631832
JK
22993 retval.size = 0;
22994 }
8cf6f0b1
TT
22995 else if (attr_form_is_section_offset (attr))
22996 {
22997 struct dwarf2_loclist_baton loclist_baton;
22998 CORE_ADDR pc = (*get_frame_pc) (baton);
22999 size_t size;
23000
23001 fill_in_loclist_baton (cu, &loclist_baton, attr);
23002
23003 retval.data = dwarf2_find_location_expression (&loclist_baton,
23004 &size, pc);
23005 retval.size = size;
23006 }
5c631832
JK
23007 else
23008 {
23009 if (!attr_form_is_block (attr))
23010 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
23011 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
e3b94546 23012 to_underlying (sect_off), objfile_name (objfile));
5c631832
JK
23013
23014 retval.data = DW_BLOCK (attr)->data;
23015 retval.size = DW_BLOCK (attr)->size;
23016 }
23017 retval.per_cu = cu->per_cu;
918dd910 23018
ed2dc618 23019 age_cached_comp_units (dwarf2_per_objfile);
918dd910 23020
5c631832 23021 return retval;
348e048f
DE
23022}
23023
8b9737bf
TT
23024/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23025 offset. */
23026
23027struct dwarf2_locexpr_baton
23028dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23029 struct dwarf2_per_cu_data *per_cu,
23030 CORE_ADDR (*get_frame_pc) (void *baton),
23031 void *baton)
23032{
9c541725 23033 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23034
9c541725 23035 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
23036}
23037
b6807d98
TT
23038/* Write a constant of a given type as target-ordered bytes into
23039 OBSTACK. */
23040
23041static const gdb_byte *
23042write_constant_as_bytes (struct obstack *obstack,
23043 enum bfd_endian byte_order,
23044 struct type *type,
23045 ULONGEST value,
23046 LONGEST *len)
23047{
23048 gdb_byte *result;
23049
23050 *len = TYPE_LENGTH (type);
224c3ddb 23051 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23052 store_unsigned_integer (result, *len, byte_order, value);
23053
23054 return result;
23055}
23056
23057/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23058 pointer to the constant bytes and set LEN to the length of the
23059 data. If memory is needed, allocate it on OBSTACK. If the DIE
23060 does not have a DW_AT_const_value, return NULL. */
23061
23062const gdb_byte *
9c541725 23063dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
23064 struct dwarf2_per_cu_data *per_cu,
23065 struct obstack *obstack,
23066 LONGEST *len)
23067{
23068 struct dwarf2_cu *cu;
23069 struct die_info *die;
23070 struct attribute *attr;
23071 const gdb_byte *result = NULL;
23072 struct type *type;
23073 LONGEST value;
23074 enum bfd_endian byte_order;
e3b94546 23075 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 23076
b6807d98
TT
23077 if (per_cu->cu == NULL)
23078 load_cu (per_cu);
23079 cu = per_cu->cu;
cc12ce38
DE
23080 if (cu == NULL)
23081 {
23082 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23083 Instead just throw an error, not much else we can do. */
23084 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
e3b94546 23085 to_underlying (sect_off), objfile_name (objfile));
cc12ce38 23086 }
b6807d98 23087
9c541725 23088 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98
TT
23089 if (!die)
23090 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
e3b94546 23091 to_underlying (sect_off), objfile_name (objfile));
b6807d98
TT
23092
23093
23094 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23095 if (attr == NULL)
23096 return NULL;
23097
e3b94546 23098 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23099 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23100
23101 switch (attr->form)
23102 {
23103 case DW_FORM_addr:
23104 case DW_FORM_GNU_addr_index:
23105 {
23106 gdb_byte *tem;
23107
23108 *len = cu->header.addr_size;
224c3ddb 23109 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23110 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23111 result = tem;
23112 }
23113 break;
23114 case DW_FORM_string:
23115 case DW_FORM_strp:
23116 case DW_FORM_GNU_str_index:
23117 case DW_FORM_GNU_strp_alt:
23118 /* DW_STRING is already allocated on the objfile obstack, point
23119 directly to it. */
23120 result = (const gdb_byte *) DW_STRING (attr);
23121 *len = strlen (DW_STRING (attr));
23122 break;
23123 case DW_FORM_block1:
23124 case DW_FORM_block2:
23125 case DW_FORM_block4:
23126 case DW_FORM_block:
23127 case DW_FORM_exprloc:
0224619f 23128 case DW_FORM_data16:
b6807d98
TT
23129 result = DW_BLOCK (attr)->data;
23130 *len = DW_BLOCK (attr)->size;
23131 break;
23132
23133 /* The DW_AT_const_value attributes are supposed to carry the
23134 symbol's value "represented as it would be on the target
23135 architecture." By the time we get here, it's already been
23136 converted to host endianness, so we just need to sign- or
23137 zero-extend it as appropriate. */
23138 case DW_FORM_data1:
23139 type = die_type (die, cu);
23140 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23141 if (result == NULL)
23142 result = write_constant_as_bytes (obstack, byte_order,
23143 type, value, len);
23144 break;
23145 case DW_FORM_data2:
23146 type = die_type (die, cu);
23147 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23148 if (result == NULL)
23149 result = write_constant_as_bytes (obstack, byte_order,
23150 type, value, len);
23151 break;
23152 case DW_FORM_data4:
23153 type = die_type (die, cu);
23154 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23155 if (result == NULL)
23156 result = write_constant_as_bytes (obstack, byte_order,
23157 type, value, len);
23158 break;
23159 case DW_FORM_data8:
23160 type = die_type (die, cu);
23161 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23162 if (result == NULL)
23163 result = write_constant_as_bytes (obstack, byte_order,
23164 type, value, len);
23165 break;
23166
23167 case DW_FORM_sdata:
663c44ac 23168 case DW_FORM_implicit_const:
b6807d98
TT
23169 type = die_type (die, cu);
23170 result = write_constant_as_bytes (obstack, byte_order,
23171 type, DW_SND (attr), len);
23172 break;
23173
23174 case DW_FORM_udata:
23175 type = die_type (die, cu);
23176 result = write_constant_as_bytes (obstack, byte_order,
23177 type, DW_UNSND (attr), len);
23178 break;
23179
23180 default:
23181 complaint (&symfile_complaints,
23182 _("unsupported const value attribute form: '%s'"),
23183 dwarf_form_name (attr->form));
23184 break;
23185 }
23186
23187 return result;
23188}
23189
7942e96e
AA
23190/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23191 valid type for this die is found. */
23192
23193struct type *
9c541725 23194dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23195 struct dwarf2_per_cu_data *per_cu)
23196{
23197 struct dwarf2_cu *cu;
23198 struct die_info *die;
23199
7942e96e
AA
23200 if (per_cu->cu == NULL)
23201 load_cu (per_cu);
23202 cu = per_cu->cu;
23203 if (!cu)
23204 return NULL;
23205
9c541725 23206 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23207 if (!die)
23208 return NULL;
23209
23210 return die_type (die, cu);
23211}
23212
8a9b8146
TT
23213/* Return the type of the DIE at DIE_OFFSET in the CU named by
23214 PER_CU. */
23215
23216struct type *
b64f50a1 23217dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23218 struct dwarf2_per_cu_data *per_cu)
23219{
9c541725 23220 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23221 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23222}
23223
ac9ec31b 23224/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23225 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23226 On exit *REF_CU is the CU of the result.
23227 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23228
23229static struct die_info *
ac9ec31b
DE
23230follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23231 struct dwarf2_cu **ref_cu)
348e048f 23232{
348e048f 23233 struct die_info temp_die;
348e048f
DE
23234 struct dwarf2_cu *sig_cu;
23235 struct die_info *die;
23236
ac9ec31b
DE
23237 /* While it might be nice to assert sig_type->type == NULL here,
23238 we can get here for DW_AT_imported_declaration where we need
23239 the DIE not the type. */
348e048f
DE
23240
23241 /* If necessary, add it to the queue and load its DIEs. */
23242
95554aad 23243 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23244 read_signatured_type (sig_type);
348e048f 23245
348e048f 23246 sig_cu = sig_type->per_cu.cu;
69d751e3 23247 gdb_assert (sig_cu != NULL);
9c541725
PA
23248 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23249 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23250 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23251 to_underlying (temp_die.sect_off));
348e048f
DE
23252 if (die)
23253 {
ed2dc618 23254 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23255 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23256
796a7ff8
DE
23257 /* For .gdb_index version 7 keep track of included TUs.
23258 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23259 if (dwarf2_per_objfile->index_table != NULL
23260 && dwarf2_per_objfile->index_table->version <= 7)
23261 {
23262 VEC_safe_push (dwarf2_per_cu_ptr,
23263 (*ref_cu)->per_cu->imported_symtabs,
23264 sig_cu->per_cu);
23265 }
23266
348e048f
DE
23267 *ref_cu = sig_cu;
23268 return die;
23269 }
23270
ac9ec31b
DE
23271 return NULL;
23272}
23273
23274/* Follow signatured type referenced by ATTR in SRC_DIE.
23275 On entry *REF_CU is the CU of SRC_DIE.
23276 On exit *REF_CU is the CU of the result.
23277 The result is the DIE of the type.
23278 If the referenced type cannot be found an error is thrown. */
23279
23280static struct die_info *
ff39bb5e 23281follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23282 struct dwarf2_cu **ref_cu)
23283{
23284 ULONGEST signature = DW_SIGNATURE (attr);
23285 struct signatured_type *sig_type;
23286 struct die_info *die;
23287
23288 gdb_assert (attr->form == DW_FORM_ref_sig8);
23289
a2ce51a0 23290 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23291 /* sig_type will be NULL if the signatured type is missing from
23292 the debug info. */
23293 if (sig_type == NULL)
23294 {
23295 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23296 " from DIE at 0x%x [in module %s]"),
9c541725 23297 hex_string (signature), to_underlying (src_die->sect_off),
518817b3 23298 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23299 }
23300
23301 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23302 if (die == NULL)
23303 {
23304 dump_die_for_error (src_die);
23305 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23306 " from DIE at 0x%x [in module %s]"),
9c541725 23307 hex_string (signature), to_underlying (src_die->sect_off),
518817b3 23308 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23309 }
23310
23311 return die;
23312}
23313
23314/* Get the type specified by SIGNATURE referenced in DIE/CU,
23315 reading in and processing the type unit if necessary. */
23316
23317static struct type *
23318get_signatured_type (struct die_info *die, ULONGEST signature,
23319 struct dwarf2_cu *cu)
23320{
518817b3
SM
23321 struct dwarf2_per_objfile *dwarf2_per_objfile
23322 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23323 struct signatured_type *sig_type;
23324 struct dwarf2_cu *type_cu;
23325 struct die_info *type_die;
23326 struct type *type;
23327
a2ce51a0 23328 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23329 /* sig_type will be NULL if the signatured type is missing from
23330 the debug info. */
23331 if (sig_type == NULL)
23332 {
23333 complaint (&symfile_complaints,
23334 _("Dwarf Error: Cannot find signatured DIE %s referenced"
23335 " from DIE at 0x%x [in module %s]"),
9c541725 23336 hex_string (signature), to_underlying (die->sect_off),
4262abfb 23337 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23338 return build_error_marker_type (cu, die);
23339 }
23340
23341 /* If we already know the type we're done. */
23342 if (sig_type->type != NULL)
23343 return sig_type->type;
23344
23345 type_cu = cu;
23346 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23347 if (type_die != NULL)
23348 {
23349 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23350 is created. This is important, for example, because for c++ classes
23351 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23352 type = read_type_die (type_die, type_cu);
23353 if (type == NULL)
23354 {
23355 complaint (&symfile_complaints,
23356 _("Dwarf Error: Cannot build signatured type %s"
23357 " referenced from DIE at 0x%x [in module %s]"),
9c541725 23358 hex_string (signature), to_underlying (die->sect_off),
4262abfb 23359 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23360 type = build_error_marker_type (cu, die);
23361 }
23362 }
23363 else
23364 {
23365 complaint (&symfile_complaints,
23366 _("Dwarf Error: Problem reading signatured DIE %s referenced"
23367 " from DIE at 0x%x [in module %s]"),
9c541725 23368 hex_string (signature), to_underlying (die->sect_off),
4262abfb 23369 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23370 type = build_error_marker_type (cu, die);
23371 }
23372 sig_type->type = type;
23373
23374 return type;
23375}
23376
23377/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23378 reading in and processing the type unit if necessary. */
23379
23380static struct type *
ff39bb5e 23381get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23382 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23383{
23384 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 23385 if (attr_form_is_ref (attr))
ac9ec31b
DE
23386 {
23387 struct dwarf2_cu *type_cu = cu;
23388 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23389
23390 return read_type_die (type_die, type_cu);
23391 }
23392 else if (attr->form == DW_FORM_ref_sig8)
23393 {
23394 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23395 }
23396 else
23397 {
518817b3
SM
23398 struct dwarf2_per_objfile *dwarf2_per_objfile
23399 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23400
ac9ec31b
DE
23401 complaint (&symfile_complaints,
23402 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23403 " at 0x%x [in module %s]"),
9c541725 23404 dwarf_form_name (attr->form), to_underlying (die->sect_off),
4262abfb 23405 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23406 return build_error_marker_type (cu, die);
23407 }
348e048f
DE
23408}
23409
e5fe5e75 23410/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23411
23412static void
e5fe5e75 23413load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 23414{
52dc124a 23415 struct signatured_type *sig_type;
348e048f 23416
f4dc4d17
DE
23417 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23418 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23419
6721b2ec
DE
23420 /* We have the per_cu, but we need the signatured_type.
23421 Fortunately this is an easy translation. */
23422 gdb_assert (per_cu->is_debug_types);
23423 sig_type = (struct signatured_type *) per_cu;
348e048f 23424
6721b2ec 23425 gdb_assert (per_cu->cu == NULL);
348e048f 23426
52dc124a 23427 read_signatured_type (sig_type);
348e048f 23428
6721b2ec 23429 gdb_assert (per_cu->cu != NULL);
348e048f
DE
23430}
23431
dee91e82
DE
23432/* die_reader_func for read_signatured_type.
23433 This is identical to load_full_comp_unit_reader,
23434 but is kept separate for now. */
348e048f
DE
23435
23436static void
dee91e82 23437read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 23438 const gdb_byte *info_ptr,
dee91e82
DE
23439 struct die_info *comp_unit_die,
23440 int has_children,
23441 void *data)
348e048f 23442{
dee91e82 23443 struct dwarf2_cu *cu = reader->cu;
348e048f 23444
dee91e82
DE
23445 gdb_assert (cu->die_hash == NULL);
23446 cu->die_hash =
23447 htab_create_alloc_ex (cu->header.length / 12,
23448 die_hash,
23449 die_eq,
23450 NULL,
23451 &cu->comp_unit_obstack,
23452 hashtab_obstack_allocate,
23453 dummy_obstack_deallocate);
348e048f 23454
dee91e82
DE
23455 if (has_children)
23456 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23457 &info_ptr, comp_unit_die);
23458 cu->dies = comp_unit_die;
23459 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
23460
23461 /* We try not to read any attributes in this function, because not
9cdd5dbd 23462 all CUs needed for references have been loaded yet, and symbol
348e048f 23463 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
23464 or we won't be able to build types correctly.
23465 Similarly, if we do not read the producer, we can not apply
23466 producer-specific interpretation. */
95554aad 23467 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 23468}
348e048f 23469
3019eac3
DE
23470/* Read in a signatured type and build its CU and DIEs.
23471 If the type is a stub for the real type in a DWO file,
23472 read in the real type from the DWO file as well. */
dee91e82
DE
23473
23474static void
23475read_signatured_type (struct signatured_type *sig_type)
23476{
23477 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 23478
3019eac3 23479 gdb_assert (per_cu->is_debug_types);
dee91e82 23480 gdb_assert (per_cu->cu == NULL);
348e048f 23481
f4dc4d17
DE
23482 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
23483 read_signatured_type_reader, NULL);
7ee85ab1 23484 sig_type->per_cu.tu_read = 1;
c906108c
SS
23485}
23486
c906108c
SS
23487/* Decode simple location descriptions.
23488 Given a pointer to a dwarf block that defines a location, compute
23489 the location and return the value.
23490
4cecd739
DJ
23491 NOTE drow/2003-11-18: This function is called in two situations
23492 now: for the address of static or global variables (partial symbols
23493 only) and for offsets into structures which are expected to be
23494 (more or less) constant. The partial symbol case should go away,
23495 and only the constant case should remain. That will let this
23496 function complain more accurately. A few special modes are allowed
23497 without complaint for global variables (for instance, global
23498 register values and thread-local values).
c906108c
SS
23499
23500 A location description containing no operations indicates that the
4cecd739 23501 object is optimized out. The return value is 0 for that case.
6b992462
DJ
23502 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23503 callers will only want a very basic result and this can become a
21ae7a4d
JK
23504 complaint.
23505
23506 Note that stack[0] is unused except as a default error return. */
c906108c
SS
23507
23508static CORE_ADDR
e7c27a73 23509decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 23510{
518817b3 23511 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
23512 size_t i;
23513 size_t size = blk->size;
d521ce57 23514 const gdb_byte *data = blk->data;
21ae7a4d
JK
23515 CORE_ADDR stack[64];
23516 int stacki;
23517 unsigned int bytes_read, unsnd;
23518 gdb_byte op;
c906108c 23519
21ae7a4d
JK
23520 i = 0;
23521 stacki = 0;
23522 stack[stacki] = 0;
23523 stack[++stacki] = 0;
23524
23525 while (i < size)
23526 {
23527 op = data[i++];
23528 switch (op)
23529 {
23530 case DW_OP_lit0:
23531 case DW_OP_lit1:
23532 case DW_OP_lit2:
23533 case DW_OP_lit3:
23534 case DW_OP_lit4:
23535 case DW_OP_lit5:
23536 case DW_OP_lit6:
23537 case DW_OP_lit7:
23538 case DW_OP_lit8:
23539 case DW_OP_lit9:
23540 case DW_OP_lit10:
23541 case DW_OP_lit11:
23542 case DW_OP_lit12:
23543 case DW_OP_lit13:
23544 case DW_OP_lit14:
23545 case DW_OP_lit15:
23546 case DW_OP_lit16:
23547 case DW_OP_lit17:
23548 case DW_OP_lit18:
23549 case DW_OP_lit19:
23550 case DW_OP_lit20:
23551 case DW_OP_lit21:
23552 case DW_OP_lit22:
23553 case DW_OP_lit23:
23554 case DW_OP_lit24:
23555 case DW_OP_lit25:
23556 case DW_OP_lit26:
23557 case DW_OP_lit27:
23558 case DW_OP_lit28:
23559 case DW_OP_lit29:
23560 case DW_OP_lit30:
23561 case DW_OP_lit31:
23562 stack[++stacki] = op - DW_OP_lit0;
23563 break;
f1bea926 23564
21ae7a4d
JK
23565 case DW_OP_reg0:
23566 case DW_OP_reg1:
23567 case DW_OP_reg2:
23568 case DW_OP_reg3:
23569 case DW_OP_reg4:
23570 case DW_OP_reg5:
23571 case DW_OP_reg6:
23572 case DW_OP_reg7:
23573 case DW_OP_reg8:
23574 case DW_OP_reg9:
23575 case DW_OP_reg10:
23576 case DW_OP_reg11:
23577 case DW_OP_reg12:
23578 case DW_OP_reg13:
23579 case DW_OP_reg14:
23580 case DW_OP_reg15:
23581 case DW_OP_reg16:
23582 case DW_OP_reg17:
23583 case DW_OP_reg18:
23584 case DW_OP_reg19:
23585 case DW_OP_reg20:
23586 case DW_OP_reg21:
23587 case DW_OP_reg22:
23588 case DW_OP_reg23:
23589 case DW_OP_reg24:
23590 case DW_OP_reg25:
23591 case DW_OP_reg26:
23592 case DW_OP_reg27:
23593 case DW_OP_reg28:
23594 case DW_OP_reg29:
23595 case DW_OP_reg30:
23596 case DW_OP_reg31:
23597 stack[++stacki] = op - DW_OP_reg0;
23598 if (i < size)
23599 dwarf2_complex_location_expr_complaint ();
23600 break;
c906108c 23601
21ae7a4d
JK
23602 case DW_OP_regx:
23603 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23604 i += bytes_read;
23605 stack[++stacki] = unsnd;
23606 if (i < size)
23607 dwarf2_complex_location_expr_complaint ();
23608 break;
c906108c 23609
21ae7a4d
JK
23610 case DW_OP_addr:
23611 stack[++stacki] = read_address (objfile->obfd, &data[i],
23612 cu, &bytes_read);
23613 i += bytes_read;
23614 break;
d53d4ac5 23615
21ae7a4d
JK
23616 case DW_OP_const1u:
23617 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23618 i += 1;
23619 break;
23620
23621 case DW_OP_const1s:
23622 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23623 i += 1;
23624 break;
23625
23626 case DW_OP_const2u:
23627 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23628 i += 2;
23629 break;
23630
23631 case DW_OP_const2s:
23632 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23633 i += 2;
23634 break;
d53d4ac5 23635
21ae7a4d
JK
23636 case DW_OP_const4u:
23637 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23638 i += 4;
23639 break;
23640
23641 case DW_OP_const4s:
23642 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23643 i += 4;
23644 break;
23645
585861ea
JK
23646 case DW_OP_const8u:
23647 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23648 i += 8;
23649 break;
23650
21ae7a4d
JK
23651 case DW_OP_constu:
23652 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23653 &bytes_read);
23654 i += bytes_read;
23655 break;
23656
23657 case DW_OP_consts:
23658 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23659 i += bytes_read;
23660 break;
23661
23662 case DW_OP_dup:
23663 stack[stacki + 1] = stack[stacki];
23664 stacki++;
23665 break;
23666
23667 case DW_OP_plus:
23668 stack[stacki - 1] += stack[stacki];
23669 stacki--;
23670 break;
23671
23672 case DW_OP_plus_uconst:
23673 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23674 &bytes_read);
23675 i += bytes_read;
23676 break;
23677
23678 case DW_OP_minus:
23679 stack[stacki - 1] -= stack[stacki];
23680 stacki--;
23681 break;
23682
23683 case DW_OP_deref:
23684 /* If we're not the last op, then we definitely can't encode
23685 this using GDB's address_class enum. This is valid for partial
23686 global symbols, although the variable's address will be bogus
23687 in the psymtab. */
23688 if (i < size)
23689 dwarf2_complex_location_expr_complaint ();
23690 break;
23691
23692 case DW_OP_GNU_push_tls_address:
4aa4e28b 23693 case DW_OP_form_tls_address:
21ae7a4d
JK
23694 /* The top of the stack has the offset from the beginning
23695 of the thread control block at which the variable is located. */
23696 /* Nothing should follow this operator, so the top of stack would
23697 be returned. */
23698 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23699 address will be bogus in the psymtab. Make it always at least
23700 non-zero to not look as a variable garbage collected by linker
23701 which have DW_OP_addr 0. */
21ae7a4d
JK
23702 if (i < size)
23703 dwarf2_complex_location_expr_complaint ();
585861ea 23704 stack[stacki]++;
21ae7a4d
JK
23705 break;
23706
23707 case DW_OP_GNU_uninit:
23708 break;
23709
3019eac3 23710 case DW_OP_GNU_addr_index:
49f6c839 23711 case DW_OP_GNU_const_index:
3019eac3
DE
23712 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23713 &bytes_read);
23714 i += bytes_read;
23715 break;
23716
21ae7a4d
JK
23717 default:
23718 {
f39c6ffd 23719 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
23720
23721 if (name)
23722 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
23723 name);
23724 else
23725 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
23726 op);
23727 }
23728
23729 return (stack[stacki]);
d53d4ac5 23730 }
3c6e0cb3 23731
21ae7a4d
JK
23732 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23733 outside of the allocated space. Also enforce minimum>0. */
23734 if (stacki >= ARRAY_SIZE (stack) - 1)
23735 {
23736 complaint (&symfile_complaints,
23737 _("location description stack overflow"));
23738 return 0;
23739 }
23740
23741 if (stacki <= 0)
23742 {
23743 complaint (&symfile_complaints,
23744 _("location description stack underflow"));
23745 return 0;
23746 }
23747 }
23748 return (stack[stacki]);
c906108c
SS
23749}
23750
23751/* memory allocation interface */
23752
c906108c 23753static struct dwarf_block *
7b5a2f43 23754dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23755{
8d749320 23756 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23757}
23758
c906108c 23759static struct die_info *
b60c80d6 23760dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23761{
23762 struct die_info *die;
b60c80d6
DJ
23763 size_t size = sizeof (struct die_info);
23764
23765 if (num_attrs > 1)
23766 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23767
b60c80d6 23768 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23769 memset (die, 0, sizeof (struct die_info));
23770 return (die);
23771}
2e276125
JB
23772
23773\f
23774/* Macro support. */
23775
233d95b5
JK
23776/* Return file name relative to the compilation directory of file number I in
23777 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 23778 responsible for freeing it. */
233d95b5 23779
2e276125 23780static char *
233d95b5 23781file_file_name (int file, struct line_header *lh)
2e276125 23782{
6a83a1e6
EZ
23783 /* Is the file number a valid index into the line header's file name
23784 table? Remember that file numbers start with one, not zero. */
fff8551c 23785 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 23786 {
8c43009f 23787 const file_entry &fe = lh->file_names[file - 1];
6e70227d 23788
8c43009f
PA
23789 if (!IS_ABSOLUTE_PATH (fe.name))
23790 {
23791 const char *dir = fe.include_dir (lh);
23792 if (dir != NULL)
23793 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23794 }
23795 return xstrdup (fe.name);
6a83a1e6 23796 }
2e276125
JB
23797 else
23798 {
6a83a1e6
EZ
23799 /* The compiler produced a bogus file number. We can at least
23800 record the macro definitions made in the file, even if we
23801 won't be able to find the file by name. */
23802 char fake_name[80];
9a619af0 23803
8c042590
PM
23804 xsnprintf (fake_name, sizeof (fake_name),
23805 "<bad macro file number %d>", file);
2e276125 23806
6e70227d 23807 complaint (&symfile_complaints,
6a83a1e6
EZ
23808 _("bad file number in macro information (%d)"),
23809 file);
2e276125 23810
6a83a1e6 23811 return xstrdup (fake_name);
2e276125
JB
23812 }
23813}
23814
233d95b5
JK
23815/* Return the full name of file number I in *LH's file name table.
23816 Use COMP_DIR as the name of the current directory of the
23817 compilation. The result is allocated using xmalloc; the caller is
23818 responsible for freeing it. */
23819static char *
23820file_full_name (int file, struct line_header *lh, const char *comp_dir)
23821{
23822 /* Is the file number a valid index into the line header's file name
23823 table? Remember that file numbers start with one, not zero. */
fff8551c 23824 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
23825 {
23826 char *relative = file_file_name (file, lh);
23827
23828 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23829 return relative;
b36cec19
PA
23830 return reconcat (relative, comp_dir, SLASH_STRING,
23831 relative, (char *) NULL);
233d95b5
JK
23832 }
23833 else
23834 return file_file_name (file, lh);
23835}
23836
2e276125
JB
23837
23838static struct macro_source_file *
23839macro_start_file (int file, int line,
23840 struct macro_source_file *current_file,
43f3e411 23841 struct line_header *lh)
2e276125 23842{
233d95b5
JK
23843 /* File name relative to the compilation directory of this source file. */
23844 char *file_name = file_file_name (file, lh);
2e276125 23845
2e276125 23846 if (! current_file)
abc9d0dc 23847 {
fc474241
DE
23848 /* Note: We don't create a macro table for this compilation unit
23849 at all until we actually get a filename. */
43f3e411 23850 struct macro_table *macro_table = get_macro_table ();
fc474241 23851
abc9d0dc
TT
23852 /* If we have no current file, then this must be the start_file
23853 directive for the compilation unit's main source file. */
fc474241
DE
23854 current_file = macro_set_main (macro_table, file_name);
23855 macro_define_special (macro_table);
abc9d0dc 23856 }
2e276125 23857 else
233d95b5 23858 current_file = macro_include (current_file, line, file_name);
2e276125 23859
233d95b5 23860 xfree (file_name);
6e70227d 23861
2e276125
JB
23862 return current_file;
23863}
23864
2e276125
JB
23865static const char *
23866consume_improper_spaces (const char *p, const char *body)
23867{
23868 if (*p == ' ')
23869 {
4d3c2250 23870 complaint (&symfile_complaints,
3e43a32a
MS
23871 _("macro definition contains spaces "
23872 "in formal argument list:\n`%s'"),
4d3c2250 23873 body);
2e276125
JB
23874
23875 while (*p == ' ')
23876 p++;
23877 }
23878
23879 return p;
23880}
23881
23882
23883static void
23884parse_macro_definition (struct macro_source_file *file, int line,
23885 const char *body)
23886{
23887 const char *p;
23888
23889 /* The body string takes one of two forms. For object-like macro
23890 definitions, it should be:
23891
23892 <macro name> " " <definition>
23893
23894 For function-like macro definitions, it should be:
23895
23896 <macro name> "() " <definition>
23897 or
23898 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23899
23900 Spaces may appear only where explicitly indicated, and in the
23901 <definition>.
23902
23903 The Dwarf 2 spec says that an object-like macro's name is always
23904 followed by a space, but versions of GCC around March 2002 omit
6e70227d 23905 the space when the macro's definition is the empty string.
2e276125
JB
23906
23907 The Dwarf 2 spec says that there should be no spaces between the
23908 formal arguments in a function-like macro's formal argument list,
23909 but versions of GCC around March 2002 include spaces after the
23910 commas. */
23911
23912
23913 /* Find the extent of the macro name. The macro name is terminated
23914 by either a space or null character (for an object-like macro) or
23915 an opening paren (for a function-like macro). */
23916 for (p = body; *p; p++)
23917 if (*p == ' ' || *p == '(')
23918 break;
23919
23920 if (*p == ' ' || *p == '\0')
23921 {
23922 /* It's an object-like macro. */
23923 int name_len = p - body;
3f8a7804 23924 char *name = savestring (body, name_len);
2e276125
JB
23925 const char *replacement;
23926
23927 if (*p == ' ')
23928 replacement = body + name_len + 1;
23929 else
23930 {
4d3c2250 23931 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23932 replacement = body + name_len;
23933 }
6e70227d 23934
2e276125
JB
23935 macro_define_object (file, line, name, replacement);
23936
23937 xfree (name);
23938 }
23939 else if (*p == '(')
23940 {
23941 /* It's a function-like macro. */
3f8a7804 23942 char *name = savestring (body, p - body);
2e276125
JB
23943 int argc = 0;
23944 int argv_size = 1;
8d749320 23945 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
23946
23947 p++;
23948
23949 p = consume_improper_spaces (p, body);
23950
23951 /* Parse the formal argument list. */
23952 while (*p && *p != ')')
23953 {
23954 /* Find the extent of the current argument name. */
23955 const char *arg_start = p;
23956
23957 while (*p && *p != ',' && *p != ')' && *p != ' ')
23958 p++;
23959
23960 if (! *p || p == arg_start)
4d3c2250 23961 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23962 else
23963 {
23964 /* Make sure argv has room for the new argument. */
23965 if (argc >= argv_size)
23966 {
23967 argv_size *= 2;
224c3ddb 23968 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
23969 }
23970
3f8a7804 23971 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
23972 }
23973
23974 p = consume_improper_spaces (p, body);
23975
23976 /* Consume the comma, if present. */
23977 if (*p == ',')
23978 {
23979 p++;
23980
23981 p = consume_improper_spaces (p, body);
23982 }
23983 }
23984
23985 if (*p == ')')
23986 {
23987 p++;
23988
23989 if (*p == ' ')
23990 /* Perfectly formed definition, no complaints. */
23991 macro_define_function (file, line, name,
6e70227d 23992 argc, (const char **) argv,
2e276125
JB
23993 p + 1);
23994 else if (*p == '\0')
23995 {
23996 /* Complain, but do define it. */
4d3c2250 23997 dwarf2_macro_malformed_definition_complaint (body);
2e276125 23998 macro_define_function (file, line, name,
6e70227d 23999 argc, (const char **) argv,
2e276125
JB
24000 p);
24001 }
24002 else
24003 /* Just complain. */
4d3c2250 24004 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24005 }
24006 else
24007 /* Just complain. */
4d3c2250 24008 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24009
24010 xfree (name);
24011 {
24012 int i;
24013
24014 for (i = 0; i < argc; i++)
24015 xfree (argv[i]);
24016 }
24017 xfree (argv);
24018 }
24019 else
4d3c2250 24020 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24021}
24022
cf2c3c16
TT
24023/* Skip some bytes from BYTES according to the form given in FORM.
24024 Returns the new pointer. */
2e276125 24025
d521ce57
TT
24026static const gdb_byte *
24027skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
24028 enum dwarf_form form,
24029 unsigned int offset_size,
24030 struct dwarf2_section_info *section)
2e276125 24031{
cf2c3c16 24032 unsigned int bytes_read;
2e276125 24033
cf2c3c16 24034 switch (form)
2e276125 24035 {
cf2c3c16
TT
24036 case DW_FORM_data1:
24037 case DW_FORM_flag:
24038 ++bytes;
24039 break;
24040
24041 case DW_FORM_data2:
24042 bytes += 2;
24043 break;
24044
24045 case DW_FORM_data4:
24046 bytes += 4;
24047 break;
24048
24049 case DW_FORM_data8:
24050 bytes += 8;
24051 break;
24052
0224619f
JK
24053 case DW_FORM_data16:
24054 bytes += 16;
24055 break;
24056
cf2c3c16
TT
24057 case DW_FORM_string:
24058 read_direct_string (abfd, bytes, &bytes_read);
24059 bytes += bytes_read;
24060 break;
24061
24062 case DW_FORM_sec_offset:
24063 case DW_FORM_strp:
36586728 24064 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
24065 bytes += offset_size;
24066 break;
24067
24068 case DW_FORM_block:
24069 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24070 bytes += bytes_read;
24071 break;
24072
24073 case DW_FORM_block1:
24074 bytes += 1 + read_1_byte (abfd, bytes);
24075 break;
24076 case DW_FORM_block2:
24077 bytes += 2 + read_2_bytes (abfd, bytes);
24078 break;
24079 case DW_FORM_block4:
24080 bytes += 4 + read_4_bytes (abfd, bytes);
24081 break;
24082
24083 case DW_FORM_sdata:
24084 case DW_FORM_udata:
3019eac3
DE
24085 case DW_FORM_GNU_addr_index:
24086 case DW_FORM_GNU_str_index:
d521ce57 24087 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
24088 if (bytes == NULL)
24089 {
24090 dwarf2_section_buffer_overflow_complaint (section);
24091 return NULL;
24092 }
cf2c3c16
TT
24093 break;
24094
663c44ac
JK
24095 case DW_FORM_implicit_const:
24096 break;
24097
cf2c3c16
TT
24098 default:
24099 {
cf2c3c16
TT
24100 complaint (&symfile_complaints,
24101 _("invalid form 0x%x in `%s'"),
a32a8923 24102 form, get_section_name (section));
cf2c3c16
TT
24103 return NULL;
24104 }
2e276125
JB
24105 }
24106
cf2c3c16
TT
24107 return bytes;
24108}
757a13d0 24109
cf2c3c16
TT
24110/* A helper for dwarf_decode_macros that handles skipping an unknown
24111 opcode. Returns an updated pointer to the macro data buffer; or,
24112 on error, issues a complaint and returns NULL. */
757a13d0 24113
d521ce57 24114static const gdb_byte *
cf2c3c16 24115skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24116 const gdb_byte **opcode_definitions,
24117 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24118 bfd *abfd,
24119 unsigned int offset_size,
24120 struct dwarf2_section_info *section)
24121{
24122 unsigned int bytes_read, i;
24123 unsigned long arg;
d521ce57 24124 const gdb_byte *defn;
2e276125 24125
cf2c3c16 24126 if (opcode_definitions[opcode] == NULL)
2e276125 24127 {
cf2c3c16
TT
24128 complaint (&symfile_complaints,
24129 _("unrecognized DW_MACFINO opcode 0x%x"),
24130 opcode);
24131 return NULL;
24132 }
2e276125 24133
cf2c3c16
TT
24134 defn = opcode_definitions[opcode];
24135 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24136 defn += bytes_read;
2e276125 24137
cf2c3c16
TT
24138 for (i = 0; i < arg; ++i)
24139 {
aead7601
SM
24140 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24141 (enum dwarf_form) defn[i], offset_size,
f664829e 24142 section);
cf2c3c16
TT
24143 if (mac_ptr == NULL)
24144 {
24145 /* skip_form_bytes already issued the complaint. */
24146 return NULL;
24147 }
24148 }
757a13d0 24149
cf2c3c16
TT
24150 return mac_ptr;
24151}
757a13d0 24152
cf2c3c16
TT
24153/* A helper function which parses the header of a macro section.
24154 If the macro section is the extended (for now called "GNU") type,
24155 then this updates *OFFSET_SIZE. Returns a pointer to just after
24156 the header, or issues a complaint and returns NULL on error. */
757a13d0 24157
d521ce57
TT
24158static const gdb_byte *
24159dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24160 bfd *abfd,
d521ce57 24161 const gdb_byte *mac_ptr,
cf2c3c16
TT
24162 unsigned int *offset_size,
24163 int section_is_gnu)
24164{
24165 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24166
cf2c3c16
TT
24167 if (section_is_gnu)
24168 {
24169 unsigned int version, flags;
757a13d0 24170
cf2c3c16 24171 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24172 if (version != 4 && version != 5)
cf2c3c16
TT
24173 {
24174 complaint (&symfile_complaints,
24175 _("unrecognized version `%d' in .debug_macro section"),
24176 version);
24177 return NULL;
24178 }
24179 mac_ptr += 2;
757a13d0 24180
cf2c3c16
TT
24181 flags = read_1_byte (abfd, mac_ptr);
24182 ++mac_ptr;
24183 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24184
cf2c3c16
TT
24185 if ((flags & 2) != 0)
24186 /* We don't need the line table offset. */
24187 mac_ptr += *offset_size;
757a13d0 24188
cf2c3c16
TT
24189 /* Vendor opcode descriptions. */
24190 if ((flags & 4) != 0)
24191 {
24192 unsigned int i, count;
757a13d0 24193
cf2c3c16
TT
24194 count = read_1_byte (abfd, mac_ptr);
24195 ++mac_ptr;
24196 for (i = 0; i < count; ++i)
24197 {
24198 unsigned int opcode, bytes_read;
24199 unsigned long arg;
24200
24201 opcode = read_1_byte (abfd, mac_ptr);
24202 ++mac_ptr;
24203 opcode_definitions[opcode] = mac_ptr;
24204 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24205 mac_ptr += bytes_read;
24206 mac_ptr += arg;
24207 }
757a13d0 24208 }
cf2c3c16 24209 }
757a13d0 24210
cf2c3c16
TT
24211 return mac_ptr;
24212}
757a13d0 24213
cf2c3c16 24214/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24215 including DW_MACRO_import. */
cf2c3c16
TT
24216
24217static void
ed2dc618
SM
24218dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24219 bfd *abfd,
d521ce57 24220 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24221 struct macro_source_file *current_file,
43f3e411 24222 struct line_header *lh,
cf2c3c16 24223 struct dwarf2_section_info *section,
36586728 24224 int section_is_gnu, int section_is_dwz,
cf2c3c16 24225 unsigned int offset_size,
8fc3fc34 24226 htab_t include_hash)
cf2c3c16 24227{
4d663531 24228 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24229 enum dwarf_macro_record_type macinfo_type;
24230 int at_commandline;
d521ce57 24231 const gdb_byte *opcode_definitions[256];
757a13d0 24232
cf2c3c16
TT
24233 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24234 &offset_size, section_is_gnu);
24235 if (mac_ptr == NULL)
24236 {
24237 /* We already issued a complaint. */
24238 return;
24239 }
757a13d0
JK
24240
24241 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24242 GDB is still reading the definitions from command line. First
24243 DW_MACINFO_start_file will need to be ignored as it was already executed
24244 to create CURRENT_FILE for the main source holding also the command line
24245 definitions. On first met DW_MACINFO_start_file this flag is reset to
24246 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24247
24248 at_commandline = 1;
24249
24250 do
24251 {
24252 /* Do we at least have room for a macinfo type byte? */
24253 if (mac_ptr >= mac_end)
24254 {
f664829e 24255 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24256 break;
24257 }
24258
aead7601 24259 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24260 mac_ptr++;
24261
cf2c3c16
TT
24262 /* Note that we rely on the fact that the corresponding GNU and
24263 DWARF constants are the same. */
132448f8
SM
24264 DIAGNOSTIC_PUSH
24265 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24266 switch (macinfo_type)
24267 {
24268 /* A zero macinfo type indicates the end of the macro
24269 information. */
24270 case 0:
24271 break;
2e276125 24272
0af92d60
JK
24273 case DW_MACRO_define:
24274 case DW_MACRO_undef:
24275 case DW_MACRO_define_strp:
24276 case DW_MACRO_undef_strp:
24277 case DW_MACRO_define_sup:
24278 case DW_MACRO_undef_sup:
2e276125 24279 {
891d2f0b 24280 unsigned int bytes_read;
2e276125 24281 int line;
d521ce57 24282 const char *body;
cf2c3c16 24283 int is_define;
2e276125 24284
cf2c3c16
TT
24285 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24286 mac_ptr += bytes_read;
24287
0af92d60
JK
24288 if (macinfo_type == DW_MACRO_define
24289 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24290 {
24291 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24292 mac_ptr += bytes_read;
24293 }
24294 else
24295 {
24296 LONGEST str_offset;
24297
24298 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24299 mac_ptr += offset_size;
2e276125 24300
0af92d60
JK
24301 if (macinfo_type == DW_MACRO_define_sup
24302 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24303 || section_is_dwz)
36586728 24304 {
ed2dc618
SM
24305 struct dwz_file *dwz
24306 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24307
ed2dc618
SM
24308 body = read_indirect_string_from_dwz (objfile,
24309 dwz, str_offset);
36586728
TT
24310 }
24311 else
ed2dc618
SM
24312 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24313 abfd, str_offset);
cf2c3c16
TT
24314 }
24315
0af92d60
JK
24316 is_define = (macinfo_type == DW_MACRO_define
24317 || macinfo_type == DW_MACRO_define_strp
24318 || macinfo_type == DW_MACRO_define_sup);
2e276125 24319 if (! current_file)
757a13d0
JK
24320 {
24321 /* DWARF violation as no main source is present. */
24322 complaint (&symfile_complaints,
24323 _("debug info with no main source gives macro %s "
24324 "on line %d: %s"),
cf2c3c16
TT
24325 is_define ? _("definition") : _("undefinition"),
24326 line, body);
757a13d0
JK
24327 break;
24328 }
3e43a32a
MS
24329 if ((line == 0 && !at_commandline)
24330 || (line != 0 && at_commandline))
4d3c2250 24331 complaint (&symfile_complaints,
757a13d0
JK
24332 _("debug info gives %s macro %s with %s line %d: %s"),
24333 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24334 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24335 line == 0 ? _("zero") : _("non-zero"), line, body);
24336
cf2c3c16 24337 if (is_define)
757a13d0 24338 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24339 else
24340 {
0af92d60
JK
24341 gdb_assert (macinfo_type == DW_MACRO_undef
24342 || macinfo_type == DW_MACRO_undef_strp
24343 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24344 macro_undef (current_file, line, body);
24345 }
2e276125
JB
24346 }
24347 break;
24348
0af92d60 24349 case DW_MACRO_start_file:
2e276125 24350 {
891d2f0b 24351 unsigned int bytes_read;
2e276125
JB
24352 int line, file;
24353
24354 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24355 mac_ptr += bytes_read;
24356 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24357 mac_ptr += bytes_read;
24358
3e43a32a
MS
24359 if ((line == 0 && !at_commandline)
24360 || (line != 0 && at_commandline))
757a13d0
JK
24361 complaint (&symfile_complaints,
24362 _("debug info gives source %d included "
24363 "from %s at %s line %d"),
24364 file, at_commandline ? _("command-line") : _("file"),
24365 line == 0 ? _("zero") : _("non-zero"), line);
24366
24367 if (at_commandline)
24368 {
0af92d60 24369 /* This DW_MACRO_start_file was executed in the
cf2c3c16 24370 pass one. */
757a13d0
JK
24371 at_commandline = 0;
24372 }
24373 else
43f3e411 24374 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
24375 }
24376 break;
24377
0af92d60 24378 case DW_MACRO_end_file:
2e276125 24379 if (! current_file)
4d3c2250 24380 complaint (&symfile_complaints,
3e43a32a
MS
24381 _("macro debug info has an unmatched "
24382 "`close_file' directive"));
2e276125
JB
24383 else
24384 {
24385 current_file = current_file->included_by;
24386 if (! current_file)
24387 {
cf2c3c16 24388 enum dwarf_macro_record_type next_type;
2e276125
JB
24389
24390 /* GCC circa March 2002 doesn't produce the zero
24391 type byte marking the end of the compilation
24392 unit. Complain if it's not there, but exit no
24393 matter what. */
24394
24395 /* Do we at least have room for a macinfo type byte? */
24396 if (mac_ptr >= mac_end)
24397 {
f664829e 24398 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
24399 return;
24400 }
24401
24402 /* We don't increment mac_ptr here, so this is just
24403 a look-ahead. */
aead7601
SM
24404 next_type
24405 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24406 mac_ptr);
2e276125 24407 if (next_type != 0)
4d3c2250 24408 complaint (&symfile_complaints,
3e43a32a
MS
24409 _("no terminating 0-type entry for "
24410 "macros in `.debug_macinfo' section"));
2e276125
JB
24411
24412 return;
24413 }
24414 }
24415 break;
24416
0af92d60
JK
24417 case DW_MACRO_import:
24418 case DW_MACRO_import_sup:
cf2c3c16
TT
24419 {
24420 LONGEST offset;
8fc3fc34 24421 void **slot;
a036ba48
TT
24422 bfd *include_bfd = abfd;
24423 struct dwarf2_section_info *include_section = section;
d521ce57 24424 const gdb_byte *include_mac_end = mac_end;
a036ba48 24425 int is_dwz = section_is_dwz;
d521ce57 24426 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
24427
24428 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24429 mac_ptr += offset_size;
24430
0af92d60 24431 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 24432 {
ed2dc618 24433 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 24434
4d663531 24435 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 24436
a036ba48 24437 include_section = &dwz->macro;
a32a8923 24438 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
24439 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24440 is_dwz = 1;
24441 }
24442
24443 new_mac_ptr = include_section->buffer + offset;
24444 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24445
8fc3fc34
TT
24446 if (*slot != NULL)
24447 {
24448 /* This has actually happened; see
24449 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24450 complaint (&symfile_complaints,
0af92d60 24451 _("recursive DW_MACRO_import in "
8fc3fc34
TT
24452 ".debug_macro section"));
24453 }
24454 else
24455 {
d521ce57 24456 *slot = (void *) new_mac_ptr;
36586728 24457
ed2dc618
SM
24458 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24459 include_bfd, new_mac_ptr,
43f3e411 24460 include_mac_end, current_file, lh,
36586728 24461 section, section_is_gnu, is_dwz,
4d663531 24462 offset_size, include_hash);
8fc3fc34 24463
d521ce57 24464 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 24465 }
cf2c3c16
TT
24466 }
24467 break;
24468
2e276125 24469 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
24470 if (!section_is_gnu)
24471 {
24472 unsigned int bytes_read;
2e276125 24473
ac298888
TT
24474 /* This reads the constant, but since we don't recognize
24475 any vendor extensions, we ignore it. */
24476 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
24477 mac_ptr += bytes_read;
24478 read_direct_string (abfd, mac_ptr, &bytes_read);
24479 mac_ptr += bytes_read;
2e276125 24480
cf2c3c16
TT
24481 /* We don't recognize any vendor extensions. */
24482 break;
24483 }
24484 /* FALLTHROUGH */
24485
24486 default:
24487 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24488 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24489 section);
24490 if (mac_ptr == NULL)
24491 return;
24492 break;
2e276125 24493 }
132448f8 24494 DIAGNOSTIC_POP
757a13d0 24495 } while (macinfo_type != 0);
2e276125 24496}
8e19ed76 24497
cf2c3c16 24498static void
09262596 24499dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 24500 int section_is_gnu)
cf2c3c16 24501{
518817b3
SM
24502 struct dwarf2_per_objfile *dwarf2_per_objfile
24503 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24504 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
24505 struct line_header *lh = cu->line_header;
24506 bfd *abfd;
d521ce57 24507 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
24508 struct macro_source_file *current_file = 0;
24509 enum dwarf_macro_record_type macinfo_type;
24510 unsigned int offset_size = cu->header.offset_size;
d521ce57 24511 const gdb_byte *opcode_definitions[256];
8fc3fc34 24512 void **slot;
09262596
DE
24513 struct dwarf2_section_info *section;
24514 const char *section_name;
24515
24516 if (cu->dwo_unit != NULL)
24517 {
24518 if (section_is_gnu)
24519 {
24520 section = &cu->dwo_unit->dwo_file->sections.macro;
24521 section_name = ".debug_macro.dwo";
24522 }
24523 else
24524 {
24525 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24526 section_name = ".debug_macinfo.dwo";
24527 }
24528 }
24529 else
24530 {
24531 if (section_is_gnu)
24532 {
24533 section = &dwarf2_per_objfile->macro;
24534 section_name = ".debug_macro";
24535 }
24536 else
24537 {
24538 section = &dwarf2_per_objfile->macinfo;
24539 section_name = ".debug_macinfo";
24540 }
24541 }
cf2c3c16 24542
bb5ed363 24543 dwarf2_read_section (objfile, section);
cf2c3c16
TT
24544 if (section->buffer == NULL)
24545 {
fceca515 24546 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
24547 return;
24548 }
a32a8923 24549 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
24550
24551 /* First pass: Find the name of the base filename.
24552 This filename is needed in order to process all macros whose definition
24553 (or undefinition) comes from the command line. These macros are defined
24554 before the first DW_MACINFO_start_file entry, and yet still need to be
24555 associated to the base file.
24556
24557 To determine the base file name, we scan the macro definitions until we
24558 reach the first DW_MACINFO_start_file entry. We then initialize
24559 CURRENT_FILE accordingly so that any macro definition found before the
24560 first DW_MACINFO_start_file can still be associated to the base file. */
24561
24562 mac_ptr = section->buffer + offset;
24563 mac_end = section->buffer + section->size;
24564
24565 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24566 &offset_size, section_is_gnu);
24567 if (mac_ptr == NULL)
24568 {
24569 /* We already issued a complaint. */
24570 return;
24571 }
24572
24573 do
24574 {
24575 /* Do we at least have room for a macinfo type byte? */
24576 if (mac_ptr >= mac_end)
24577 {
24578 /* Complaint is printed during the second pass as GDB will probably
24579 stop the first pass earlier upon finding
24580 DW_MACINFO_start_file. */
24581 break;
24582 }
24583
aead7601 24584 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
24585 mac_ptr++;
24586
24587 /* Note that we rely on the fact that the corresponding GNU and
24588 DWARF constants are the same. */
132448f8
SM
24589 DIAGNOSTIC_PUSH
24590 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
24591 switch (macinfo_type)
24592 {
24593 /* A zero macinfo type indicates the end of the macro
24594 information. */
24595 case 0:
24596 break;
24597
0af92d60
JK
24598 case DW_MACRO_define:
24599 case DW_MACRO_undef:
cf2c3c16
TT
24600 /* Only skip the data by MAC_PTR. */
24601 {
24602 unsigned int bytes_read;
24603
24604 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24605 mac_ptr += bytes_read;
24606 read_direct_string (abfd, mac_ptr, &bytes_read);
24607 mac_ptr += bytes_read;
24608 }
24609 break;
24610
0af92d60 24611 case DW_MACRO_start_file:
cf2c3c16
TT
24612 {
24613 unsigned int bytes_read;
24614 int line, file;
24615
24616 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24617 mac_ptr += bytes_read;
24618 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24619 mac_ptr += bytes_read;
24620
43f3e411 24621 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
24622 }
24623 break;
24624
0af92d60 24625 case DW_MACRO_end_file:
cf2c3c16
TT
24626 /* No data to skip by MAC_PTR. */
24627 break;
24628
0af92d60
JK
24629 case DW_MACRO_define_strp:
24630 case DW_MACRO_undef_strp:
24631 case DW_MACRO_define_sup:
24632 case DW_MACRO_undef_sup:
cf2c3c16
TT
24633 {
24634 unsigned int bytes_read;
24635
24636 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24637 mac_ptr += bytes_read;
24638 mac_ptr += offset_size;
24639 }
24640 break;
24641
0af92d60
JK
24642 case DW_MACRO_import:
24643 case DW_MACRO_import_sup:
cf2c3c16 24644 /* Note that, according to the spec, a transparent include
0af92d60 24645 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
24646 skip this opcode. */
24647 mac_ptr += offset_size;
24648 break;
24649
24650 case DW_MACINFO_vendor_ext:
24651 /* Only skip the data by MAC_PTR. */
24652 if (!section_is_gnu)
24653 {
24654 unsigned int bytes_read;
24655
24656 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24657 mac_ptr += bytes_read;
24658 read_direct_string (abfd, mac_ptr, &bytes_read);
24659 mac_ptr += bytes_read;
24660 }
24661 /* FALLTHROUGH */
24662
24663 default:
24664 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24665 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24666 section);
24667 if (mac_ptr == NULL)
24668 return;
24669 break;
24670 }
132448f8 24671 DIAGNOSTIC_POP
cf2c3c16
TT
24672 } while (macinfo_type != 0 && current_file == NULL);
24673
24674 /* Second pass: Process all entries.
24675
24676 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24677 command-line macro definitions/undefinitions. This flag is unset when we
24678 reach the first DW_MACINFO_start_file entry. */
24679
fc4007c9
TT
24680 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24681 htab_eq_pointer,
24682 NULL, xcalloc, xfree));
8fc3fc34 24683 mac_ptr = section->buffer + offset;
fc4007c9 24684 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 24685 *slot = (void *) mac_ptr;
ed2dc618
SM
24686 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24687 abfd, mac_ptr, mac_end,
43f3e411 24688 current_file, lh, section,
fc4007c9
TT
24689 section_is_gnu, 0, offset_size,
24690 include_hash.get ());
cf2c3c16
TT
24691}
24692
8e19ed76 24693/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 24694 if so return true else false. */
380bca97 24695
8e19ed76 24696static int
6e5a29e1 24697attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
24698{
24699 return (attr == NULL ? 0 :
24700 attr->form == DW_FORM_block1
24701 || attr->form == DW_FORM_block2
24702 || attr->form == DW_FORM_block4
2dc7f7b3
TT
24703 || attr->form == DW_FORM_block
24704 || attr->form == DW_FORM_exprloc);
8e19ed76 24705}
4c2df51b 24706
c6a0999f
JB
24707/* Return non-zero if ATTR's value is a section offset --- classes
24708 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24709 You may use DW_UNSND (attr) to retrieve such offsets.
24710
24711 Section 7.5.4, "Attribute Encodings", explains that no attribute
24712 may have a value that belongs to more than one of these classes; it
24713 would be ambiguous if we did, because we use the same forms for all
24714 of them. */
380bca97 24715
3690dd37 24716static int
6e5a29e1 24717attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
24718{
24719 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
24720 || attr->form == DW_FORM_data8
24721 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
24722}
24723
3690dd37
JB
24724/* Return non-zero if ATTR's value falls in the 'constant' class, or
24725 zero otherwise. When this function returns true, you can apply
24726 dwarf2_get_attr_constant_value to it.
24727
24728 However, note that for some attributes you must check
24729 attr_form_is_section_offset before using this test. DW_FORM_data4
24730 and DW_FORM_data8 are members of both the constant class, and of
24731 the classes that contain offsets into other debug sections
24732 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24733 that, if an attribute's can be either a constant or one of the
24734 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
24735 taken as section offsets, not constants.
24736
24737 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24738 cannot handle that. */
380bca97 24739
3690dd37 24740static int
6e5a29e1 24741attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
24742{
24743 switch (attr->form)
24744 {
24745 case DW_FORM_sdata:
24746 case DW_FORM_udata:
24747 case DW_FORM_data1:
24748 case DW_FORM_data2:
24749 case DW_FORM_data4:
24750 case DW_FORM_data8:
663c44ac 24751 case DW_FORM_implicit_const:
3690dd37
JB
24752 return 1;
24753 default:
24754 return 0;
24755 }
24756}
24757
7771576e
SA
24758
24759/* DW_ADDR is always stored already as sect_offset; despite for the forms
24760 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24761
24762static int
6e5a29e1 24763attr_form_is_ref (const struct attribute *attr)
7771576e
SA
24764{
24765 switch (attr->form)
24766 {
24767 case DW_FORM_ref_addr:
24768 case DW_FORM_ref1:
24769 case DW_FORM_ref2:
24770 case DW_FORM_ref4:
24771 case DW_FORM_ref8:
24772 case DW_FORM_ref_udata:
24773 case DW_FORM_GNU_ref_alt:
24774 return 1;
24775 default:
24776 return 0;
24777 }
24778}
24779
3019eac3
DE
24780/* Return the .debug_loc section to use for CU.
24781 For DWO files use .debug_loc.dwo. */
24782
24783static struct dwarf2_section_info *
24784cu_debug_loc_section (struct dwarf2_cu *cu)
24785{
518817b3
SM
24786 struct dwarf2_per_objfile *dwarf2_per_objfile
24787 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 24788
3019eac3 24789 if (cu->dwo_unit)
43988095
JK
24790 {
24791 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24792
24793 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24794 }
24795 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24796 : &dwarf2_per_objfile->loc);
3019eac3
DE
24797}
24798
8cf6f0b1
TT
24799/* A helper function that fills in a dwarf2_loclist_baton. */
24800
24801static void
24802fill_in_loclist_baton (struct dwarf2_cu *cu,
24803 struct dwarf2_loclist_baton *baton,
ff39bb5e 24804 const struct attribute *attr)
8cf6f0b1 24805{
518817b3
SM
24806 struct dwarf2_per_objfile *dwarf2_per_objfile
24807 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
24808 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24809
24810 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
24811
24812 baton->per_cu = cu->per_cu;
24813 gdb_assert (baton->per_cu);
24814 /* We don't know how long the location list is, but make sure we
24815 don't run off the edge of the section. */
3019eac3
DE
24816 baton->size = section->size - DW_UNSND (attr);
24817 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 24818 baton->base_address = cu->base_address;
f664829e 24819 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24820}
24821
4c2df51b 24822static void
ff39bb5e 24823dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24824 struct dwarf2_cu *cu, int is_block)
4c2df51b 24825{
518817b3
SM
24826 struct dwarf2_per_objfile *dwarf2_per_objfile
24827 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24828 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 24829 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24830
3690dd37 24831 if (attr_form_is_section_offset (attr)
3019eac3 24832 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24833 the section. If so, fall through to the complaint in the
24834 other branch. */
3019eac3 24835 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 24836 {
0d53c4c4 24837 struct dwarf2_loclist_baton *baton;
4c2df51b 24838
8d749320 24839 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24840
8cf6f0b1 24841 fill_in_loclist_baton (cu, baton, attr);
be391dca 24842
d00adf39 24843 if (cu->base_known == 0)
0d53c4c4 24844 complaint (&symfile_complaints,
3e43a32a
MS
24845 _("Location list used without "
24846 "specifying the CU base address."));
4c2df51b 24847
f1e6e072
TT
24848 SYMBOL_ACLASS_INDEX (sym) = (is_block
24849 ? dwarf2_loclist_block_index
24850 : dwarf2_loclist_index);
0d53c4c4
DJ
24851 SYMBOL_LOCATION_BATON (sym) = baton;
24852 }
24853 else
24854 {
24855 struct dwarf2_locexpr_baton *baton;
24856
8d749320 24857 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
24858 baton->per_cu = cu->per_cu;
24859 gdb_assert (baton->per_cu);
0d53c4c4
DJ
24860
24861 if (attr_form_is_block (attr))
24862 {
24863 /* Note that we're just copying the block's data pointer
24864 here, not the actual data. We're still pointing into the
6502dd73
DJ
24865 info_buffer for SYM's objfile; right now we never release
24866 that buffer, but when we do clean up properly this may
24867 need to change. */
0d53c4c4
DJ
24868 baton->size = DW_BLOCK (attr)->size;
24869 baton->data = DW_BLOCK (attr)->data;
24870 }
24871 else
24872 {
24873 dwarf2_invalid_attrib_class_complaint ("location description",
24874 SYMBOL_NATURAL_NAME (sym));
24875 baton->size = 0;
0d53c4c4 24876 }
6e70227d 24877
f1e6e072
TT
24878 SYMBOL_ACLASS_INDEX (sym) = (is_block
24879 ? dwarf2_locexpr_block_index
24880 : dwarf2_locexpr_index);
0d53c4c4
DJ
24881 SYMBOL_LOCATION_BATON (sym) = baton;
24882 }
4c2df51b 24883}
6502dd73 24884
9aa1f1e3
TT
24885/* Return the OBJFILE associated with the compilation unit CU. If CU
24886 came from a separate debuginfo file, then the master objfile is
24887 returned. */
ae0d2f24
UW
24888
24889struct objfile *
24890dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24891{
e3b94546 24892 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
24893
24894 /* Return the master objfile, so that we can report and look up the
24895 correct file containing this variable. */
24896 if (objfile->separate_debug_objfile_backlink)
24897 objfile = objfile->separate_debug_objfile_backlink;
24898
24899 return objfile;
24900}
24901
96408a79
SA
24902/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24903 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24904 CU_HEADERP first. */
24905
24906static const struct comp_unit_head *
24907per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24908 struct dwarf2_per_cu_data *per_cu)
24909{
d521ce57 24910 const gdb_byte *info_ptr;
96408a79
SA
24911
24912 if (per_cu->cu)
24913 return &per_cu->cu->header;
24914
9c541725 24915 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
24916
24917 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
24918 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24919 rcuh_kind::COMPILE);
96408a79
SA
24920
24921 return cu_headerp;
24922}
24923
ae0d2f24
UW
24924/* Return the address size given in the compilation unit header for CU. */
24925
98714339 24926int
ae0d2f24
UW
24927dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24928{
96408a79
SA
24929 struct comp_unit_head cu_header_local;
24930 const struct comp_unit_head *cu_headerp;
c471e790 24931
96408a79
SA
24932 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24933
24934 return cu_headerp->addr_size;
ae0d2f24
UW
24935}
24936
9eae7c52
TT
24937/* Return the offset size given in the compilation unit header for CU. */
24938
24939int
24940dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24941{
96408a79
SA
24942 struct comp_unit_head cu_header_local;
24943 const struct comp_unit_head *cu_headerp;
9c6c53f7 24944
96408a79
SA
24945 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24946
24947 return cu_headerp->offset_size;
24948}
24949
24950/* See its dwarf2loc.h declaration. */
24951
24952int
24953dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24954{
24955 struct comp_unit_head cu_header_local;
24956 const struct comp_unit_head *cu_headerp;
24957
24958 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24959
24960 if (cu_headerp->version == 2)
24961 return cu_headerp->addr_size;
24962 else
24963 return cu_headerp->offset_size;
181cebd4
JK
24964}
24965
9aa1f1e3
TT
24966/* Return the text offset of the CU. The returned offset comes from
24967 this CU's objfile. If this objfile came from a separate debuginfo
24968 file, then the offset may be different from the corresponding
24969 offset in the parent objfile. */
24970
24971CORE_ADDR
24972dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24973{
e3b94546 24974 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9aa1f1e3
TT
24975
24976 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24977}
24978
43988095
JK
24979/* Return DWARF version number of PER_CU. */
24980
24981short
24982dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24983{
24984 return per_cu->dwarf_version;
24985}
24986
348e048f
DE
24987/* Locate the .debug_info compilation unit from CU's objfile which contains
24988 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
24989
24990static struct dwarf2_per_cu_data *
9c541725 24991dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 24992 unsigned int offset_in_dwz,
ed2dc618 24993 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
24994{
24995 struct dwarf2_per_cu_data *this_cu;
24996 int low, high;
36586728 24997 const sect_offset *cu_off;
ae038cb0 24998
ae038cb0
DJ
24999 low = 0;
25000 high = dwarf2_per_objfile->n_comp_units - 1;
25001 while (high > low)
25002 {
36586728 25003 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 25004 int mid = low + (high - low) / 2;
9a619af0 25005
36586728 25006 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
9c541725 25007 cu_off = &mid_cu->sect_off;
36586728 25008 if (mid_cu->is_dwz > offset_in_dwz
9c541725 25009 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
ae038cb0
DJ
25010 high = mid;
25011 else
25012 low = mid + 1;
25013 }
25014 gdb_assert (low == high);
36586728 25015 this_cu = dwarf2_per_objfile->all_comp_units[low];
9c541725
PA
25016 cu_off = &this_cu->sect_off;
25017 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
ae038cb0 25018 {
36586728 25019 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 25020 error (_("Dwarf Error: could not find partial DIE containing "
9c541725 25021 "offset 0x%x [in module %s]"),
ed2dc618
SM
25022 to_underlying (sect_off),
25023 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 25024
9c541725
PA
25025 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25026 <= sect_off);
ae038cb0
DJ
25027 return dwarf2_per_objfile->all_comp_units[low-1];
25028 }
25029 else
25030 {
25031 this_cu = dwarf2_per_objfile->all_comp_units[low];
25032 if (low == dwarf2_per_objfile->n_comp_units - 1
9c541725
PA
25033 && sect_off >= this_cu->sect_off + this_cu->length)
25034 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
25035 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
25036 return this_cu;
25037 }
25038}
25039
23745b47 25040/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 25041
fcd3b13d
SM
25042dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25043 : per_cu (per_cu_),
25044 mark (0),
25045 has_loclist (0),
25046 checked_producer (0),
25047 producer_is_gxx_lt_4_6 (0),
25048 producer_is_gcc_lt_4_3 (0),
25049 producer_is_icc_lt_14 (0),
25050 processing_has_namespace_info (0)
93311388 25051{
fcd3b13d
SM
25052 per_cu->cu = this;
25053}
25054
25055/* Destroy a dwarf2_cu. */
25056
25057dwarf2_cu::~dwarf2_cu ()
25058{
25059 per_cu->cu = NULL;
9816fde3
JK
25060}
25061
25062/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25063
25064static void
95554aad
TT
25065prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25066 enum language pretend_language)
9816fde3
JK
25067{
25068 struct attribute *attr;
25069
25070 /* Set the language we're debugging. */
25071 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25072 if (attr)
25073 set_cu_language (DW_UNSND (attr), cu);
25074 else
9cded63f 25075 {
95554aad 25076 cu->language = pretend_language;
9cded63f
TT
25077 cu->language_defn = language_def (cu->language);
25078 }
dee91e82 25079
7d45c7c3 25080 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25081}
25082
ae038cb0
DJ
25083/* Free all cached compilation units. */
25084
25085static void
25086free_cached_comp_units (void *data)
25087{
ed2dc618
SM
25088 struct dwarf2_per_objfile *dwarf2_per_objfile
25089 = (struct dwarf2_per_objfile *) data;
25090
330cdd98 25091 dwarf2_per_objfile->free_cached_comp_units ();
ae038cb0
DJ
25092}
25093
25094/* Increase the age counter on each cached compilation unit, and free
25095 any that are too old. */
25096
25097static void
ed2dc618 25098age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25099{
25100 struct dwarf2_per_cu_data *per_cu, **last_chain;
25101
25102 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25103 per_cu = dwarf2_per_objfile->read_in_chain;
25104 while (per_cu != NULL)
25105 {
25106 per_cu->cu->last_used ++;
b4f54984 25107 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
25108 dwarf2_mark (per_cu->cu);
25109 per_cu = per_cu->cu->read_in_chain;
25110 }
25111
25112 per_cu = dwarf2_per_objfile->read_in_chain;
25113 last_chain = &dwarf2_per_objfile->read_in_chain;
25114 while (per_cu != NULL)
25115 {
25116 struct dwarf2_per_cu_data *next_cu;
25117
25118 next_cu = per_cu->cu->read_in_chain;
25119
25120 if (!per_cu->cu->mark)
25121 {
fcd3b13d 25122 delete per_cu->cu;
ae038cb0
DJ
25123 *last_chain = next_cu;
25124 }
25125 else
25126 last_chain = &per_cu->cu->read_in_chain;
25127
25128 per_cu = next_cu;
25129 }
25130}
25131
25132/* Remove a single compilation unit from the cache. */
25133
25134static void
dee91e82 25135free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25136{
25137 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
25138 struct dwarf2_per_objfile *dwarf2_per_objfile
25139 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
25140
25141 per_cu = dwarf2_per_objfile->read_in_chain;
25142 last_chain = &dwarf2_per_objfile->read_in_chain;
25143 while (per_cu != NULL)
25144 {
25145 struct dwarf2_per_cu_data *next_cu;
25146
25147 next_cu = per_cu->cu->read_in_chain;
25148
dee91e82 25149 if (per_cu == target_per_cu)
ae038cb0 25150 {
fcd3b13d 25151 delete per_cu->cu;
dee91e82 25152 per_cu->cu = NULL;
ae038cb0
DJ
25153 *last_chain = next_cu;
25154 break;
25155 }
25156 else
25157 last_chain = &per_cu->cu->read_in_chain;
25158
25159 per_cu = next_cu;
25160 }
25161}
25162
fe3e1990
DJ
25163/* Release all extra memory associated with OBJFILE. */
25164
25165void
25166dwarf2_free_objfile (struct objfile *objfile)
25167{
ed2dc618
SM
25168 struct dwarf2_per_objfile *dwarf2_per_objfile
25169 = get_dwarf2_per_objfile (objfile);
fe3e1990
DJ
25170
25171 if (dwarf2_per_objfile == NULL)
25172 return;
25173
330cdd98 25174 dwarf2_per_objfile->~dwarf2_per_objfile ();
fe3e1990
DJ
25175}
25176
dee91e82
DE
25177/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25178 We store these in a hash table separate from the DIEs, and preserve them
25179 when the DIEs are flushed out of cache.
25180
25181 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25182 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25183 or the type may come from a DWO file. Furthermore, while it's more logical
25184 to use per_cu->section+offset, with Fission the section with the data is in
25185 the DWO file but we don't know that section at the point we need it.
25186 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25187 because we can enter the lookup routine, get_die_type_at_offset, from
25188 outside this file, and thus won't necessarily have PER_CU->cu.
25189 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25190
dee91e82 25191struct dwarf2_per_cu_offset_and_type
1c379e20 25192{
dee91e82 25193 const struct dwarf2_per_cu_data *per_cu;
9c541725 25194 sect_offset sect_off;
1c379e20
DJ
25195 struct type *type;
25196};
25197
dee91e82 25198/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25199
25200static hashval_t
dee91e82 25201per_cu_offset_and_type_hash (const void *item)
1c379e20 25202{
9a3c8263
SM
25203 const struct dwarf2_per_cu_offset_and_type *ofs
25204 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25205
9c541725 25206 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25207}
25208
dee91e82 25209/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25210
25211static int
dee91e82 25212per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25213{
9a3c8263
SM
25214 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25215 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25216 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25217 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25218
dee91e82 25219 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25220 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25221}
25222
25223/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25224 table if necessary. For convenience, return TYPE.
25225
25226 The DIEs reading must have careful ordering to:
25227 * Not cause infite loops trying to read in DIEs as a prerequisite for
25228 reading current DIE.
25229 * Not trying to dereference contents of still incompletely read in types
25230 while reading in other DIEs.
25231 * Enable referencing still incompletely read in types just by a pointer to
25232 the type without accessing its fields.
25233
25234 Therefore caller should follow these rules:
25235 * Try to fetch any prerequisite types we may need to build this DIE type
25236 before building the type and calling set_die_type.
e71ec853 25237 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25238 possible before fetching more types to complete the current type.
25239 * Make the type as complete as possible before fetching more types. */
1c379e20 25240
f792889a 25241static struct type *
1c379e20
DJ
25242set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25243{
518817b3
SM
25244 struct dwarf2_per_objfile *dwarf2_per_objfile
25245 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25246 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25247 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25248 struct attribute *attr;
25249 struct dynamic_prop prop;
1c379e20 25250
b4ba55a1
JB
25251 /* For Ada types, make sure that the gnat-specific data is always
25252 initialized (if not already set). There are a few types where
25253 we should not be doing so, because the type-specific area is
25254 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25255 where the type-specific area is used to store the floatformat).
25256 But this is not a problem, because the gnat-specific information
25257 is actually not needed for these types. */
25258 if (need_gnat_info (cu)
25259 && TYPE_CODE (type) != TYPE_CODE_FUNC
25260 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25261 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25262 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25263 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25264 && !HAVE_GNAT_AUX_INFO (type))
25265 INIT_GNAT_SPECIFIC (type);
25266
3f2f83dd
KB
25267 /* Read DW_AT_allocated and set in type. */
25268 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25269 if (attr_form_is_block (attr))
25270 {
25271 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25272 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25273 }
25274 else if (attr != NULL)
25275 {
25276 complaint (&symfile_complaints,
9c541725
PA
25277 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
25278 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25279 to_underlying (die->sect_off));
3f2f83dd
KB
25280 }
25281
25282 /* Read DW_AT_associated and set in type. */
25283 attr = dwarf2_attr (die, DW_AT_associated, cu);
25284 if (attr_form_is_block (attr))
25285 {
25286 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25287 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25288 }
25289 else if (attr != NULL)
25290 {
25291 complaint (&symfile_complaints,
9c541725
PA
25292 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
25293 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25294 to_underlying (die->sect_off));
3f2f83dd
KB
25295 }
25296
3cdcd0ce
JB
25297 /* Read DW_AT_data_location and set in type. */
25298 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25299 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25300 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25301
dee91e82 25302 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25303 {
dee91e82
DE
25304 dwarf2_per_objfile->die_type_hash =
25305 htab_create_alloc_ex (127,
25306 per_cu_offset_and_type_hash,
25307 per_cu_offset_and_type_eq,
25308 NULL,
25309 &objfile->objfile_obstack,
25310 hashtab_obstack_allocate,
25311 dummy_obstack_deallocate);
f792889a 25312 }
1c379e20 25313
dee91e82 25314 ofs.per_cu = cu->per_cu;
9c541725 25315 ofs.sect_off = die->sect_off;
1c379e20 25316 ofs.type = type;
dee91e82
DE
25317 slot = (struct dwarf2_per_cu_offset_and_type **)
25318 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
25319 if (*slot)
25320 complaint (&symfile_complaints,
25321 _("A problem internal to GDB: DIE 0x%x has type already set"),
9c541725 25322 to_underlying (die->sect_off));
8d749320
SM
25323 *slot = XOBNEW (&objfile->objfile_obstack,
25324 struct dwarf2_per_cu_offset_and_type);
1c379e20 25325 **slot = ofs;
f792889a 25326 return type;
1c379e20
DJ
25327}
25328
9c541725 25329/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25330 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25331
25332static struct type *
9c541725 25333get_die_type_at_offset (sect_offset sect_off,
673bfd45 25334 struct dwarf2_per_cu_data *per_cu)
1c379e20 25335{
dee91e82 25336 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 25337 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 25338
dee91e82 25339 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25340 return NULL;
1c379e20 25341
dee91e82 25342 ofs.per_cu = per_cu;
9c541725 25343 ofs.sect_off = sect_off;
9a3c8263
SM
25344 slot = ((struct dwarf2_per_cu_offset_and_type *)
25345 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25346 if (slot)
25347 return slot->type;
25348 else
25349 return NULL;
25350}
25351
02142a6c 25352/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25353 or return NULL if DIE does not have a saved type. */
25354
25355static struct type *
25356get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25357{
9c541725 25358 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
25359}
25360
10b3939b
DJ
25361/* Add a dependence relationship from CU to REF_PER_CU. */
25362
25363static void
25364dwarf2_add_dependence (struct dwarf2_cu *cu,
25365 struct dwarf2_per_cu_data *ref_per_cu)
25366{
25367 void **slot;
25368
25369 if (cu->dependencies == NULL)
25370 cu->dependencies
25371 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25372 NULL, &cu->comp_unit_obstack,
25373 hashtab_obstack_allocate,
25374 dummy_obstack_deallocate);
25375
25376 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25377 if (*slot == NULL)
25378 *slot = ref_per_cu;
25379}
1c379e20 25380
f504f079
DE
25381/* Subroutine of dwarf2_mark to pass to htab_traverse.
25382 Set the mark field in every compilation unit in the
ae038cb0
DJ
25383 cache that we must keep because we are keeping CU. */
25384
10b3939b
DJ
25385static int
25386dwarf2_mark_helper (void **slot, void *data)
25387{
25388 struct dwarf2_per_cu_data *per_cu;
25389
25390 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
25391
25392 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25393 reading of the chain. As such dependencies remain valid it is not much
25394 useful to track and undo them during QUIT cleanups. */
25395 if (per_cu->cu == NULL)
25396 return 1;
25397
10b3939b
DJ
25398 if (per_cu->cu->mark)
25399 return 1;
25400 per_cu->cu->mark = 1;
25401
25402 if (per_cu->cu->dependencies != NULL)
25403 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25404
25405 return 1;
25406}
25407
f504f079
DE
25408/* Set the mark field in CU and in every other compilation unit in the
25409 cache that we must keep because we are keeping CU. */
25410
ae038cb0
DJ
25411static void
25412dwarf2_mark (struct dwarf2_cu *cu)
25413{
25414 if (cu->mark)
25415 return;
25416 cu->mark = 1;
10b3939b
DJ
25417 if (cu->dependencies != NULL)
25418 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
25419}
25420
25421static void
25422dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25423{
25424 while (per_cu)
25425 {
25426 per_cu->cu->mark = 0;
25427 per_cu = per_cu->cu->read_in_chain;
25428 }
72bf9492
DJ
25429}
25430
72bf9492
DJ
25431/* Trivial hash function for partial_die_info: the hash value of a DIE
25432 is its offset in .debug_info for this objfile. */
25433
25434static hashval_t
25435partial_die_hash (const void *item)
25436{
9a3c8263
SM
25437 const struct partial_die_info *part_die
25438 = (const struct partial_die_info *) item;
9a619af0 25439
9c541725 25440 return to_underlying (part_die->sect_off);
72bf9492
DJ
25441}
25442
25443/* Trivial comparison function for partial_die_info structures: two DIEs
25444 are equal if they have the same offset. */
25445
25446static int
25447partial_die_eq (const void *item_lhs, const void *item_rhs)
25448{
9a3c8263
SM
25449 const struct partial_die_info *part_die_lhs
25450 = (const struct partial_die_info *) item_lhs;
25451 const struct partial_die_info *part_die_rhs
25452 = (const struct partial_die_info *) item_rhs;
9a619af0 25453
9c541725 25454 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25455}
25456
b4f54984
DE
25457static struct cmd_list_element *set_dwarf_cmdlist;
25458static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
25459
25460static void
981a3fb3 25461set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 25462{
b4f54984 25463 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 25464 gdb_stdout);
ae038cb0
DJ
25465}
25466
25467static void
981a3fb3 25468show_dwarf_cmd (const char *args, int from_tty)
6e70227d 25469{
b4f54984 25470 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
25471}
25472
4bf44c1c 25473/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
25474
25475static void
c1bd65d0 25476dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc 25477{
9a3c8263 25478 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
8b70b953 25479 int ix;
8b70b953 25480
59b0c7c1
JB
25481 for (ix = 0; ix < data->n_comp_units; ++ix)
25482 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 25483
59b0c7c1 25484 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 25485 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
25486 data->all_type_units[ix]->per_cu.imported_symtabs);
25487 xfree (data->all_type_units);
95554aad 25488
8b70b953 25489 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
25490
25491 if (data->dwo_files)
25492 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
25493 if (data->dwp_file)
25494 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
25495
25496 if (data->dwz_file && data->dwz_file->dwz_bfd)
25497 gdb_bfd_unref (data->dwz_file->dwz_bfd);
3f563c84
PA
25498
25499 if (data->index_table != NULL)
25500 data->index_table->~mapped_index ();
9291a0cd
TT
25501}
25502
25503\f
ae2de4f8 25504/* The "save gdb-index" command. */
9291a0cd 25505
437afbb8
JK
25506/* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
25507 error checking. */
25508
25509static void
25510file_write (FILE *file, const void *data, size_t size)
25511{
25512 if (fwrite (data, 1, size, file) != size)
25513 error (_("couldn't data write to file"));
25514}
25515
25516/* Write the contents of VEC to FILE, with error checking. */
25517
25518template<typename Elem, typename Alloc>
25519static void
25520file_write (FILE *file, const std::vector<Elem, Alloc> &vec)
25521{
25522 file_write (file, vec.data (), vec.size () * sizeof (vec[0]));
25523}
25524
bc8f2430
JK
25525/* In-memory buffer to prepare data to be written later to a file. */
25526class data_buf
9291a0cd 25527{
bc8f2430 25528public:
bc8f2430
JK
25529 /* Copy DATA to the end of the buffer. */
25530 template<typename T>
25531 void append_data (const T &data)
25532 {
25533 std::copy (reinterpret_cast<const gdb_byte *> (&data),
25534 reinterpret_cast<const gdb_byte *> (&data + 1),
c2f134ac 25535 grow (sizeof (data)));
bc8f2430 25536 }
b89be57b 25537
c2f134ac
PA
25538 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
25539 terminating zero is appended too. */
bc8f2430
JK
25540 void append_cstr0 (const char *cstr)
25541 {
25542 const size_t size = strlen (cstr) + 1;
c2f134ac
PA
25543 std::copy (cstr, cstr + size, grow (size));
25544 }
25545
437afbb8
JK
25546 /* Store INPUT as ULEB128 to the end of buffer. */
25547 void append_unsigned_leb128 (ULONGEST input)
25548 {
25549 for (;;)
25550 {
25551 gdb_byte output = input & 0x7f;
25552 input >>= 7;
25553 if (input)
25554 output |= 0x80;
25555 append_data (output);
25556 if (input == 0)
25557 break;
25558 }
25559 }
25560
c2f134ac
PA
25561 /* Accept a host-format integer in VAL and append it to the buffer
25562 as a target-format integer which is LEN bytes long. */
25563 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
25564 {
25565 ::store_unsigned_integer (grow (len), len, byte_order, val);
bc8f2430 25566 }
9291a0cd 25567
bc8f2430
JK
25568 /* Return the size of the buffer. */
25569 size_t size () const
25570 {
25571 return m_vec.size ();
25572 }
25573
437afbb8
JK
25574 /* Return true iff the buffer is empty. */
25575 bool empty () const
25576 {
25577 return m_vec.empty ();
25578 }
25579
bc8f2430
JK
25580 /* Write the buffer to FILE. */
25581 void file_write (FILE *file) const
25582 {
437afbb8 25583 ::file_write (file, m_vec);
bc8f2430
JK
25584 }
25585
25586private:
c2f134ac
PA
25587 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
25588 the start of the new block. */
25589 gdb_byte *grow (size_t size)
25590 {
25591 m_vec.resize (m_vec.size () + size);
25592 return &*m_vec.end () - size;
25593 }
25594
d5722aa2 25595 gdb::byte_vector m_vec;
bc8f2430 25596};
9291a0cd
TT
25597
25598/* An entry in the symbol table. */
25599struct symtab_index_entry
25600{
25601 /* The name of the symbol. */
25602 const char *name;
25603 /* The offset of the name in the constant pool. */
25604 offset_type index_offset;
25605 /* A sorted vector of the indices of all the CUs that hold an object
25606 of this name. */
bc8f2430 25607 std::vector<offset_type> cu_indices;
9291a0cd
TT
25608};
25609
25610/* The symbol table. This is a power-of-2-sized hash table. */
25611struct mapped_symtab
25612{
bc8f2430
JK
25613 mapped_symtab ()
25614 {
25615 data.resize (1024);
25616 }
b89be57b 25617
bc8f2430 25618 offset_type n_elements = 0;
4b76cda9 25619 std::vector<symtab_index_entry> data;
bc8f2430 25620};
9291a0cd 25621
bc8f2430 25622/* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
559a7a62
JK
25623 the slot.
25624
25625 Function is used only during write_hash_table so no index format backward
25626 compatibility is needed. */
b89be57b 25627
4b76cda9 25628static symtab_index_entry &
9291a0cd
TT
25629find_slot (struct mapped_symtab *symtab, const char *name)
25630{
559a7a62 25631 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd 25632
bc8f2430
JK
25633 index = hash & (symtab->data.size () - 1);
25634 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
9291a0cd
TT
25635
25636 for (;;)
25637 {
4b76cda9
PA
25638 if (symtab->data[index].name == NULL
25639 || strcmp (name, symtab->data[index].name) == 0)
bc8f2430
JK
25640 return symtab->data[index];
25641 index = (index + step) & (symtab->data.size () - 1);
9291a0cd
TT
25642 }
25643}
25644
25645/* Expand SYMTAB's hash table. */
b89be57b 25646
9291a0cd
TT
25647static void
25648hash_expand (struct mapped_symtab *symtab)
25649{
bc8f2430 25650 auto old_entries = std::move (symtab->data);
9291a0cd 25651
bc8f2430
JK
25652 symtab->data.clear ();
25653 symtab->data.resize (old_entries.size () * 2);
9291a0cd 25654
bc8f2430 25655 for (auto &it : old_entries)
4b76cda9 25656 if (it.name != NULL)
bc8f2430 25657 {
4b76cda9 25658 auto &ref = find_slot (symtab, it.name);
bc8f2430
JK
25659 ref = std::move (it);
25660 }
9291a0cd
TT
25661}
25662
156942c7
DE
25663/* Add an entry to SYMTAB. NAME is the name of the symbol.
25664 CU_INDEX is the index of the CU in which the symbol appears.
25665 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 25666
9291a0cd
TT
25667static void
25668add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 25669 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
25670 offset_type cu_index)
25671{
156942c7 25672 offset_type cu_index_and_attrs;
9291a0cd
TT
25673
25674 ++symtab->n_elements;
bc8f2430 25675 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
9291a0cd
TT
25676 hash_expand (symtab);
25677
4b76cda9
PA
25678 symtab_index_entry &slot = find_slot (symtab, name);
25679 if (slot.name == NULL)
9291a0cd 25680 {
4b76cda9 25681 slot.name = name;
156942c7 25682 /* index_offset is set later. */
9291a0cd 25683 }
156942c7
DE
25684
25685 cu_index_and_attrs = 0;
25686 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
25687 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
25688 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
25689
25690 /* We don't want to record an index value twice as we want to avoid the
25691 duplication.
25692 We process all global symbols and then all static symbols
25693 (which would allow us to avoid the duplication by only having to check
25694 the last entry pushed), but a symbol could have multiple kinds in one CU.
25695 To keep things simple we don't worry about the duplication here and
25696 sort and uniqufy the list after we've processed all symbols. */
4b76cda9 25697 slot.cu_indices.push_back (cu_index_and_attrs);
156942c7
DE
25698}
25699
25700/* Sort and remove duplicates of all symbols' cu_indices lists. */
25701
25702static void
25703uniquify_cu_indices (struct mapped_symtab *symtab)
25704{
4b76cda9 25705 for (auto &entry : symtab->data)
156942c7 25706 {
4b76cda9 25707 if (entry.name != NULL && !entry.cu_indices.empty ())
156942c7 25708 {
4b76cda9 25709 auto &cu_indices = entry.cu_indices;
6fd931f2
PA
25710 std::sort (cu_indices.begin (), cu_indices.end ());
25711 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
25712 cu_indices.erase (from, cu_indices.end ());
156942c7
DE
25713 }
25714 }
9291a0cd
TT
25715}
25716
bc8f2430
JK
25717/* A form of 'const char *' suitable for container keys. Only the
25718 pointer is stored. The strings themselves are compared, not the
25719 pointers. */
25720class c_str_view
9291a0cd 25721{
bc8f2430
JK
25722public:
25723 c_str_view (const char *cstr)
25724 : m_cstr (cstr)
25725 {}
9291a0cd 25726
bc8f2430
JK
25727 bool operator== (const c_str_view &other) const
25728 {
25729 return strcmp (m_cstr, other.m_cstr) == 0;
25730 }
9291a0cd 25731
437afbb8
JK
25732 /* Return the underlying C string. Note, the returned string is
25733 only a reference with lifetime of this object. */
25734 const char *c_str () const
25735 {
25736 return m_cstr;
25737 }
25738
bc8f2430
JK
25739private:
25740 friend class c_str_view_hasher;
25741 const char *const m_cstr;
25742};
9291a0cd 25743
bc8f2430
JK
25744/* A std::unordered_map::hasher for c_str_view that uses the right
25745 hash function for strings in a mapped index. */
25746class c_str_view_hasher
25747{
25748public:
25749 size_t operator () (const c_str_view &x) const
25750 {
25751 return mapped_index_string_hash (INT_MAX, x.m_cstr);
25752 }
25753};
b89be57b 25754
bc8f2430
JK
25755/* A std::unordered_map::hasher for std::vector<>. */
25756template<typename T>
25757class vector_hasher
9291a0cd 25758{
bc8f2430
JK
25759public:
25760 size_t operator () (const std::vector<T> &key) const
25761 {
25762 return iterative_hash (key.data (),
25763 sizeof (key.front ()) * key.size (), 0);
25764 }
25765};
9291a0cd 25766
bc8f2430
JK
25767/* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
25768 constant pool entries going into the data buffer CPOOL. */
3876f04e 25769
bc8f2430
JK
25770static void
25771write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
25772{
25773 {
25774 /* Elements are sorted vectors of the indices of all the CUs that
25775 hold an object of this name. */
25776 std::unordered_map<std::vector<offset_type>, offset_type,
25777 vector_hasher<offset_type>>
25778 symbol_hash_table;
25779
25780 /* We add all the index vectors to the constant pool first, to
25781 ensure alignment is ok. */
4b76cda9 25782 for (symtab_index_entry &entry : symtab->data)
bc8f2430 25783 {
4b76cda9 25784 if (entry.name == NULL)
bc8f2430 25785 continue;
4b76cda9 25786 gdb_assert (entry.index_offset == 0);
70a1152b
PA
25787
25788 /* Finding before inserting is faster than always trying to
25789 insert, because inserting always allocates a node, does the
25790 lookup, and then destroys the new node if another node
25791 already had the same key. C++17 try_emplace will avoid
25792 this. */
25793 const auto found
4b76cda9 25794 = symbol_hash_table.find (entry.cu_indices);
70a1152b
PA
25795 if (found != symbol_hash_table.end ())
25796 {
4b76cda9 25797 entry.index_offset = found->second;
70a1152b
PA
25798 continue;
25799 }
25800
4b76cda9
PA
25801 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
25802 entry.index_offset = cpool.size ();
25803 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
25804 for (const auto index : entry.cu_indices)
25805 cpool.append_data (MAYBE_SWAP (index));
bc8f2430
JK
25806 }
25807 }
9291a0cd
TT
25808
25809 /* Now write out the hash table. */
bc8f2430 25810 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
4b76cda9 25811 for (const auto &entry : symtab->data)
9291a0cd
TT
25812 {
25813 offset_type str_off, vec_off;
25814
4b76cda9 25815 if (entry.name != NULL)
9291a0cd 25816 {
4b76cda9 25817 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
bc8f2430 25818 if (insertpair.second)
4b76cda9 25819 cpool.append_cstr0 (entry.name);
bc8f2430 25820 str_off = insertpair.first->second;
4b76cda9 25821 vec_off = entry.index_offset;
9291a0cd
TT
25822 }
25823 else
25824 {
25825 /* While 0 is a valid constant pool index, it is not valid
25826 to have 0 for both offsets. */
25827 str_off = 0;
25828 vec_off = 0;
25829 }
25830
bc8f2430
JK
25831 output.append_data (MAYBE_SWAP (str_off));
25832 output.append_data (MAYBE_SWAP (vec_off));
9291a0cd 25833 }
9291a0cd
TT
25834}
25835
bc8f2430 25836typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
0a5429f6
DE
25837
25838/* Helper struct for building the address table. */
25839struct addrmap_index_data
25840{
bc8f2430
JK
25841 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
25842 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
25843 {}
25844
0a5429f6 25845 struct objfile *objfile;
bc8f2430
JK
25846 data_buf &addr_vec;
25847 psym_index_map &cu_index_htab;
0a5429f6
DE
25848
25849 /* Non-zero if the previous_* fields are valid.
25850 We can't write an entry until we see the next entry (since it is only then
25851 that we know the end of the entry). */
25852 int previous_valid;
25853 /* Index of the CU in the table of all CUs in the index file. */
25854 unsigned int previous_cu_index;
0963b4bd 25855 /* Start address of the CU. */
0a5429f6
DE
25856 CORE_ADDR previous_cu_start;
25857};
25858
bc8f2430 25859/* Write an address entry to ADDR_VEC. */
b89be57b 25860
9291a0cd 25861static void
bc8f2430 25862add_address_entry (struct objfile *objfile, data_buf &addr_vec,
0a5429f6 25863 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 25864{
9291a0cd
TT
25865 CORE_ADDR baseaddr;
25866
25867 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25868
c2f134ac
PA
25869 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
25870 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
bc8f2430 25871 addr_vec.append_data (MAYBE_SWAP (cu_index));
0a5429f6
DE
25872}
25873
25874/* Worker function for traversing an addrmap to build the address table. */
25875
25876static int
25877add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
25878{
9a3c8263
SM
25879 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
25880 struct partial_symtab *pst = (struct partial_symtab *) obj;
0a5429f6
DE
25881
25882 if (data->previous_valid)
bc8f2430 25883 add_address_entry (data->objfile, data->addr_vec,
0a5429f6
DE
25884 data->previous_cu_start, start_addr,
25885 data->previous_cu_index);
25886
25887 data->previous_cu_start = start_addr;
25888 if (pst != NULL)
25889 {
bc8f2430
JK
25890 const auto it = data->cu_index_htab.find (pst);
25891 gdb_assert (it != data->cu_index_htab.cend ());
25892 data->previous_cu_index = it->second;
0a5429f6
DE
25893 data->previous_valid = 1;
25894 }
25895 else
bc8f2430 25896 data->previous_valid = 0;
0a5429f6
DE
25897
25898 return 0;
25899}
25900
bc8f2430 25901/* Write OBJFILE's address map to ADDR_VEC.
0a5429f6
DE
25902 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
25903 in the index file. */
25904
25905static void
bc8f2430
JK
25906write_address_map (struct objfile *objfile, data_buf &addr_vec,
25907 psym_index_map &cu_index_htab)
0a5429f6 25908{
bc8f2430 25909 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
0a5429f6
DE
25910
25911 /* When writing the address table, we have to cope with the fact that
25912 the addrmap iterator only provides the start of a region; we have to
25913 wait until the next invocation to get the start of the next region. */
25914
25915 addrmap_index_data.objfile = objfile;
0a5429f6
DE
25916 addrmap_index_data.previous_valid = 0;
25917
25918 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
25919 &addrmap_index_data);
25920
25921 /* It's highly unlikely the last entry (end address = 0xff...ff)
25922 is valid, but we should still handle it.
25923 The end address is recorded as the start of the next region, but that
25924 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
25925 anyway. */
25926 if (addrmap_index_data.previous_valid)
bc8f2430 25927 add_address_entry (objfile, addr_vec,
0a5429f6
DE
25928 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
25929 addrmap_index_data.previous_cu_index);
9291a0cd
TT
25930}
25931
156942c7
DE
25932/* Return the symbol kind of PSYM. */
25933
25934static gdb_index_symbol_kind
25935symbol_kind (struct partial_symbol *psym)
25936{
25937 domain_enum domain = PSYMBOL_DOMAIN (psym);
25938 enum address_class aclass = PSYMBOL_CLASS (psym);
25939
25940 switch (domain)
25941 {
25942 case VAR_DOMAIN:
25943 switch (aclass)
25944 {
25945 case LOC_BLOCK:
25946 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
25947 case LOC_TYPEDEF:
25948 return GDB_INDEX_SYMBOL_KIND_TYPE;
25949 case LOC_COMPUTED:
25950 case LOC_CONST_BYTES:
25951 case LOC_OPTIMIZED_OUT:
25952 case LOC_STATIC:
25953 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
25954 case LOC_CONST:
25955 /* Note: It's currently impossible to recognize psyms as enum values
25956 short of reading the type info. For now punt. */
25957 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
25958 default:
25959 /* There are other LOC_FOO values that one might want to classify
25960 as variables, but dwarf2read.c doesn't currently use them. */
25961 return GDB_INDEX_SYMBOL_KIND_OTHER;
25962 }
25963 case STRUCT_DOMAIN:
25964 return GDB_INDEX_SYMBOL_KIND_TYPE;
25965 default:
25966 return GDB_INDEX_SYMBOL_KIND_OTHER;
25967 }
25968}
25969
9291a0cd 25970/* Add a list of partial symbols to SYMTAB. */
b89be57b 25971
9291a0cd
TT
25972static void
25973write_psymbols (struct mapped_symtab *symtab,
bc8f2430 25974 std::unordered_set<partial_symbol *> &psyms_seen,
9291a0cd
TT
25975 struct partial_symbol **psymp,
25976 int count,
987d643c
TT
25977 offset_type cu_index,
25978 int is_static)
9291a0cd
TT
25979{
25980 for (; count-- > 0; ++psymp)
25981 {
156942c7 25982 struct partial_symbol *psym = *psymp;
987d643c 25983
156942c7 25984 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 25985 error (_("Ada is not currently supported by the index"));
987d643c 25986
987d643c 25987 /* Only add a given psymbol once. */
bc8f2430 25988 if (psyms_seen.insert (psym).second)
987d643c 25989 {
156942c7
DE
25990 gdb_index_symbol_kind kind = symbol_kind (psym);
25991
156942c7
DE
25992 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
25993 is_static, kind, cu_index);
987d643c 25994 }
9291a0cd
TT
25995 }
25996}
25997
1fd400ff
TT
25998/* A helper struct used when iterating over debug_types. */
25999struct signatured_type_index_data
26000{
bc8f2430
JK
26001 signatured_type_index_data (data_buf &types_list_,
26002 std::unordered_set<partial_symbol *> &psyms_seen_)
26003 : types_list (types_list_), psyms_seen (psyms_seen_)
26004 {}
26005
1fd400ff
TT
26006 struct objfile *objfile;
26007 struct mapped_symtab *symtab;
bc8f2430
JK
26008 data_buf &types_list;
26009 std::unordered_set<partial_symbol *> &psyms_seen;
1fd400ff
TT
26010 int cu_index;
26011};
26012
26013/* A helper function that writes a single signatured_type to an
26014 obstack. */
b89be57b 26015
1fd400ff
TT
26016static int
26017write_one_signatured_type (void **slot, void *d)
26018{
9a3c8263
SM
26019 struct signatured_type_index_data *info
26020 = (struct signatured_type_index_data *) d;
1fd400ff 26021 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 26022 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
26023
26024 write_psymbols (info->symtab,
987d643c 26025 info->psyms_seen,
af5bf4ad 26026 &info->objfile->global_psymbols[psymtab->globals_offset],
987d643c
TT
26027 psymtab->n_global_syms, info->cu_index,
26028 0);
1fd400ff 26029 write_psymbols (info->symtab,
987d643c 26030 info->psyms_seen,
af5bf4ad 26031 &info->objfile->static_psymbols[psymtab->statics_offset],
987d643c
TT
26032 psymtab->n_static_syms, info->cu_index,
26033 1);
1fd400ff 26034
c2f134ac
PA
26035 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26036 to_underlying (entry->per_cu.sect_off));
26037 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26038 to_underlying (entry->type_offset_in_tu));
26039 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
1fd400ff
TT
26040
26041 ++info->cu_index;
26042
26043 return 1;
26044}
26045
e8f8bcb3
PA
26046/* Recurse into all "included" dependencies and count their symbols as
26047 if they appeared in this psymtab. */
26048
26049static void
26050recursively_count_psymbols (struct partial_symtab *psymtab,
26051 size_t &psyms_seen)
26052{
26053 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26054 if (psymtab->dependencies[i]->user != NULL)
26055 recursively_count_psymbols (psymtab->dependencies[i],
26056 psyms_seen);
26057
26058 psyms_seen += psymtab->n_global_syms;
26059 psyms_seen += psymtab->n_static_syms;
26060}
26061
95554aad
TT
26062/* Recurse into all "included" dependencies and write their symbols as
26063 if they appeared in this psymtab. */
26064
26065static void
26066recursively_write_psymbols (struct objfile *objfile,
26067 struct partial_symtab *psymtab,
26068 struct mapped_symtab *symtab,
bc8f2430 26069 std::unordered_set<partial_symbol *> &psyms_seen,
95554aad
TT
26070 offset_type cu_index)
26071{
26072 int i;
26073
26074 for (i = 0; i < psymtab->number_of_dependencies; ++i)
26075 if (psymtab->dependencies[i]->user != NULL)
26076 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26077 symtab, psyms_seen, cu_index);
26078
26079 write_psymbols (symtab,
26080 psyms_seen,
af5bf4ad 26081 &objfile->global_psymbols[psymtab->globals_offset],
95554aad
TT
26082 psymtab->n_global_syms, cu_index,
26083 0);
26084 write_psymbols (symtab,
26085 psyms_seen,
af5bf4ad 26086 &objfile->static_psymbols[psymtab->statics_offset],
95554aad
TT
26087 psymtab->n_static_syms, cu_index,
26088 1);
26089}
26090
437afbb8
JK
26091/* DWARF-5 .debug_names builder. */
26092class debug_names
26093{
26094public:
ed2dc618
SM
26095 debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile, bool is_dwarf64,
26096 bfd_endian dwarf5_byte_order)
437afbb8
JK
26097 : m_dwarf5_byte_order (dwarf5_byte_order),
26098 m_dwarf32 (dwarf5_byte_order),
26099 m_dwarf64 (dwarf5_byte_order),
26100 m_dwarf (is_dwarf64
26101 ? static_cast<dwarf &> (m_dwarf64)
26102 : static_cast<dwarf &> (m_dwarf32)),
26103 m_name_table_string_offs (m_dwarf.name_table_string_offs),
ed2dc618
SM
26104 m_name_table_entry_offs (m_dwarf.name_table_entry_offs),
26105 m_debugstrlookup (dwarf2_per_objfile)
437afbb8 26106 {}
9291a0cd 26107
8af5c486
JK
26108 int dwarf5_offset_size () const
26109 {
26110 const bool dwarf5_is_dwarf64 = &m_dwarf == &m_dwarf64;
26111 return dwarf5_is_dwarf64 ? 8 : 4;
26112 }
26113
26114 /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit? */
26115 enum class unit_kind { cu, tu };
26116
437afbb8 26117 /* Insert one symbol. */
8af5c486
JK
26118 void insert (const partial_symbol *psym, int cu_index, bool is_static,
26119 unit_kind kind)
437afbb8
JK
26120 {
26121 const int dwarf_tag = psymbol_tag (psym);
26122 if (dwarf_tag == 0)
26123 return;
26124 const char *const name = SYMBOL_SEARCH_NAME (psym);
26125 const auto insertpair
26126 = m_name_to_value_set.emplace (c_str_view (name),
26127 std::set<symbol_value> ());
26128 std::set<symbol_value> &value_set = insertpair.first->second;
8af5c486 26129 value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static, kind));
437afbb8 26130 }
9291a0cd 26131
437afbb8
JK
26132 /* Build all the tables. All symbols must be already inserted.
26133 This function does not call file_write, caller has to do it
26134 afterwards. */
26135 void build ()
26136 {
26137 /* Verify the build method has not be called twice. */
26138 gdb_assert (m_abbrev_table.empty ());
26139 const size_t name_count = m_name_to_value_set.size ();
26140 m_bucket_table.resize
26141 (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3))));
26142 m_hash_table.reserve (name_count);
26143 m_name_table_string_offs.reserve (name_count);
26144 m_name_table_entry_offs.reserve (name_count);
26145
26146 /* Map each hash of symbol to its name and value. */
26147 struct hash_it_pair
26148 {
26149 uint32_t hash;
26150 decltype (m_name_to_value_set)::const_iterator it;
26151 };
26152 std::vector<std::forward_list<hash_it_pair>> bucket_hash;
26153 bucket_hash.resize (m_bucket_table.size ());
26154 for (decltype (m_name_to_value_set)::const_iterator it
26155 = m_name_to_value_set.cbegin ();
26156 it != m_name_to_value_set.cend ();
26157 ++it)
26158 {
26159 const char *const name = it->first.c_str ();
26160 const uint32_t hash = dwarf5_djb_hash (name);
26161 hash_it_pair hashitpair;
26162 hashitpair.hash = hash;
26163 hashitpair.it = it;
26164 auto &slot = bucket_hash[hash % bucket_hash.size()];
26165 slot.push_front (std::move (hashitpair));
26166 }
26167 for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix)
26168 {
26169 const std::forward_list<hash_it_pair> &hashitlist
26170 = bucket_hash[bucket_ix];
26171 if (hashitlist.empty ())
26172 continue;
26173 uint32_t &bucket_slot = m_bucket_table[bucket_ix];
26174 /* The hashes array is indexed starting at 1. */
26175 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot),
26176 sizeof (bucket_slot), m_dwarf5_byte_order,
26177 m_hash_table.size () + 1);
26178 for (const hash_it_pair &hashitpair : hashitlist)
26179 {
26180 m_hash_table.push_back (0);
26181 store_unsigned_integer (reinterpret_cast<gdb_byte *>
26182 (&m_hash_table.back ()),
26183 sizeof (m_hash_table.back ()),
26184 m_dwarf5_byte_order, hashitpair.hash);
26185 const c_str_view &name = hashitpair.it->first;
26186 const std::set<symbol_value> &value_set = hashitpair.it->second;
26187 m_name_table_string_offs.push_back_reorder
26188 (m_debugstrlookup.lookup (name.c_str ()));
26189 m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
26190 gdb_assert (!value_set.empty ());
26191 for (const symbol_value &value : value_set)
26192 {
26193 int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag,
8af5c486
JK
26194 value.is_static,
26195 value.kind)];
437afbb8
JK
26196 if (idx == 0)
26197 {
26198 idx = m_idx_next++;
26199 m_abbrev_table.append_unsigned_leb128 (idx);
26200 m_abbrev_table.append_unsigned_leb128 (value.dwarf_tag);
8af5c486
JK
26201 m_abbrev_table.append_unsigned_leb128
26202 (value.kind == unit_kind::cu ? DW_IDX_compile_unit
26203 : DW_IDX_type_unit);
437afbb8
JK
26204 m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
26205 m_abbrev_table.append_unsigned_leb128 (value.is_static
26206 ? DW_IDX_GNU_internal
26207 : DW_IDX_GNU_external);
26208 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
26209
26210 /* Terminate attributes list. */
26211 m_abbrev_table.append_unsigned_leb128 (0);
26212 m_abbrev_table.append_unsigned_leb128 (0);
26213 }
9291a0cd 26214
437afbb8
JK
26215 m_entry_pool.append_unsigned_leb128 (idx);
26216 m_entry_pool.append_unsigned_leb128 (value.cu_index);
26217 }
9291a0cd 26218
437afbb8
JK
26219 /* Terminate the list of CUs. */
26220 m_entry_pool.append_unsigned_leb128 (0);
26221 }
26222 }
26223 gdb_assert (m_hash_table.size () == name_count);
987d643c 26224
437afbb8
JK
26225 /* Terminate tags list. */
26226 m_abbrev_table.append_unsigned_leb128 (0);
26227 }
0a5429f6 26228
437afbb8
JK
26229 /* Return .debug_names bucket count. This must be called only after
26230 calling the build method. */
26231 uint32_t bucket_count () const
26232 {
26233 /* Verify the build method has been already called. */
26234 gdb_assert (!m_abbrev_table.empty ());
26235 const uint32_t retval = m_bucket_table.size ();
26236
26237 /* Check for overflow. */
26238 gdb_assert (retval == m_bucket_table.size ());
26239 return retval;
26240 }
26241
26242 /* Return .debug_names names count. This must be called only after
26243 calling the build method. */
26244 uint32_t name_count () const
26245 {
26246 /* Verify the build method has been already called. */
26247 gdb_assert (!m_abbrev_table.empty ());
26248 const uint32_t retval = m_hash_table.size ();
26249
26250 /* Check for overflow. */
26251 gdb_assert (retval == m_hash_table.size ());
26252 return retval;
26253 }
26254
26255 /* Return number of bytes of .debug_names abbreviation table. This
26256 must be called only after calling the build method. */
26257 uint32_t abbrev_table_bytes () const
26258 {
26259 gdb_assert (!m_abbrev_table.empty ());
26260 return m_abbrev_table.size ();
26261 }
26262
26263 /* Recurse into all "included" dependencies and store their symbols
26264 as if they appeared in this psymtab. */
26265 void recursively_write_psymbols
26266 (struct objfile *objfile,
26267 struct partial_symtab *psymtab,
26268 std::unordered_set<partial_symbol *> &psyms_seen,
26269 int cu_index)
26270 {
26271 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26272 if (psymtab->dependencies[i]->user != NULL)
26273 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26274 psyms_seen, cu_index);
26275
26276 write_psymbols (psyms_seen,
26277 &objfile->global_psymbols[psymtab->globals_offset],
8af5c486 26278 psymtab->n_global_syms, cu_index, false, unit_kind::cu);
437afbb8
JK
26279 write_psymbols (psyms_seen,
26280 &objfile->static_psymbols[psymtab->statics_offset],
8af5c486 26281 psymtab->n_static_syms, cu_index, true, unit_kind::cu);
437afbb8
JK
26282 }
26283
26284 /* Return number of bytes the .debug_names section will have. This
26285 must be called only after calling the build method. */
26286 size_t bytes () const
26287 {
26288 /* Verify the build method has been already called. */
26289 gdb_assert (!m_abbrev_table.empty ());
26290 size_t expected_bytes = 0;
26291 expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]);
26292 expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]);
26293 expected_bytes += m_name_table_string_offs.bytes ();
26294 expected_bytes += m_name_table_entry_offs.bytes ();
26295 expected_bytes += m_abbrev_table.size ();
26296 expected_bytes += m_entry_pool.size ();
26297 return expected_bytes;
26298 }
26299
26300 /* Write .debug_names to FILE_NAMES and .debug_str addition to
26301 FILE_STR. This must be called only after calling the build
26302 method. */
26303 void file_write (FILE *file_names, FILE *file_str) const
26304 {
26305 /* Verify the build method has been already called. */
26306 gdb_assert (!m_abbrev_table.empty ());
26307 ::file_write (file_names, m_bucket_table);
26308 ::file_write (file_names, m_hash_table);
26309 m_name_table_string_offs.file_write (file_names);
26310 m_name_table_entry_offs.file_write (file_names);
26311 m_abbrev_table.file_write (file_names);
26312 m_entry_pool.file_write (file_names);
26313 m_debugstrlookup.file_write (file_str);
26314 }
26315
8af5c486
JK
26316 /* A helper user data for write_one_signatured_type. */
26317 class write_one_signatured_type_data
26318 {
26319 public:
26320 write_one_signatured_type_data (debug_names &nametable_,
26321 signatured_type_index_data &&info_)
26322 : nametable (nametable_), info (std::move (info_))
26323 {}
26324 debug_names &nametable;
26325 struct signatured_type_index_data info;
26326 };
26327
26328 /* A helper function to pass write_one_signatured_type to
26329 htab_traverse_noresize. */
26330 static int
26331 write_one_signatured_type (void **slot, void *d)
26332 {
26333 write_one_signatured_type_data *data = (write_one_signatured_type_data *) d;
26334 struct signatured_type_index_data *info = &data->info;
26335 struct signatured_type *entry = (struct signatured_type *) *slot;
26336
26337 data->nametable.write_one_signatured_type (entry, info);
26338
26339 return 1;
26340 }
26341
437afbb8
JK
26342private:
26343
26344 /* Storage for symbol names mapping them to their .debug_str section
26345 offsets. */
26346 class debug_str_lookup
26347 {
26348 public:
26349
26350 /* Object costructor to be called for current DWARF2_PER_OBJFILE.
26351 All .debug_str section strings are automatically stored. */
ed2dc618
SM
26352 debug_str_lookup (struct dwarf2_per_objfile *dwarf2_per_objfile)
26353 : m_abfd (dwarf2_per_objfile->objfile->obfd),
26354 m_dwarf2_per_objfile (dwarf2_per_objfile)
437afbb8
JK
26355 {
26356 dwarf2_read_section (dwarf2_per_objfile->objfile,
26357 &dwarf2_per_objfile->str);
26358 if (dwarf2_per_objfile->str.buffer == NULL)
26359 return;
26360 for (const gdb_byte *data = dwarf2_per_objfile->str.buffer;
26361 data < (dwarf2_per_objfile->str.buffer
26362 + dwarf2_per_objfile->str.size);)
26363 {
26364 const char *const s = reinterpret_cast<const char *> (data);
26365 const auto insertpair
26366 = m_str_table.emplace (c_str_view (s),
26367 data - dwarf2_per_objfile->str.buffer);
26368 if (!insertpair.second)
26369 complaint (&symfile_complaints,
26370 _("Duplicate string \"%s\" in "
26371 ".debug_str section [in module %s]"),
26372 s, bfd_get_filename (m_abfd));
26373 data += strlen (s) + 1;
26374 }
26375 }
26376
26377 /* Return offset of symbol name S in the .debug_str section. Add
26378 such symbol to the section's end if it does not exist there
26379 yet. */
26380 size_t lookup (const char *s)
26381 {
26382 const auto it = m_str_table.find (c_str_view (s));
26383 if (it != m_str_table.end ())
26384 return it->second;
ed2dc618 26385 const size_t offset = (m_dwarf2_per_objfile->str.size
437afbb8
JK
26386 + m_str_add_buf.size ());
26387 m_str_table.emplace (c_str_view (s), offset);
26388 m_str_add_buf.append_cstr0 (s);
26389 return offset;
26390 }
26391
26392 /* Append the end of the .debug_str section to FILE. */
26393 void file_write (FILE *file) const
26394 {
26395 m_str_add_buf.file_write (file);
26396 }
26397
26398 private:
26399 std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
26400 bfd *const m_abfd;
ed2dc618 26401 struct dwarf2_per_objfile *m_dwarf2_per_objfile;
437afbb8
JK
26402
26403 /* Data to add at the end of .debug_str for new needed symbol names. */
26404 data_buf m_str_add_buf;
26405 };
26406
26407 /* Container to map used DWARF tags to their .debug_names abbreviation
26408 tags. */
26409 class index_key
26410 {
26411 public:
8af5c486
JK
26412 index_key (int dwarf_tag_, bool is_static_, unit_kind kind_)
26413 : dwarf_tag (dwarf_tag_), is_static (is_static_), kind (kind_)
437afbb8
JK
26414 {
26415 }
26416
26417 bool
26418 operator== (const index_key &other) const
26419 {
8af5c486
JK
26420 return (dwarf_tag == other.dwarf_tag && is_static == other.is_static
26421 && kind == other.kind);
437afbb8
JK
26422 }
26423
26424 const int dwarf_tag;
26425 const bool is_static;
8af5c486 26426 const unit_kind kind;
437afbb8
JK
26427 };
26428
26429 /* Provide std::unordered_map::hasher for index_key. */
26430 class index_key_hasher
26431 {
26432 public:
26433 size_t
26434 operator () (const index_key &key) const
26435 {
26436 return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static;
26437 }
26438 };
26439
26440 /* Parameters of one symbol entry. */
26441 class symbol_value
26442 {
26443 public:
26444 const int dwarf_tag, cu_index;
26445 const bool is_static;
8af5c486 26446 const unit_kind kind;
437afbb8 26447
8af5c486
JK
26448 symbol_value (int dwarf_tag_, int cu_index_, bool is_static_,
26449 unit_kind kind_)
26450 : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_),
26451 kind (kind_)
437afbb8
JK
26452 {}
26453
26454 bool
26455 operator< (const symbol_value &other) const
26456 {
26457#define X(n) \
26458 do \
26459 { \
26460 if (n < other.n) \
26461 return true; \
26462 if (n > other.n) \
26463 return false; \
26464 } \
26465 while (0)
26466 X (dwarf_tag);
26467 X (is_static);
8af5c486 26468 X (kind);
437afbb8
JK
26469 X (cu_index);
26470#undef X
26471 return false;
26472 }
26473 };
26474
26475 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
26476 output. */
26477 class offset_vec
26478 {
26479 protected:
26480 const bfd_endian dwarf5_byte_order;
26481 public:
26482 explicit offset_vec (bfd_endian dwarf5_byte_order_)
26483 : dwarf5_byte_order (dwarf5_byte_order_)
26484 {}
26485
26486 /* Call std::vector::reserve for NELEM elements. */
26487 virtual void reserve (size_t nelem) = 0;
26488
26489 /* Call std::vector::push_back with store_unsigned_integer byte
26490 reordering for ELEM. */
26491 virtual void push_back_reorder (size_t elem) = 0;
26492
26493 /* Return expected output size in bytes. */
26494 virtual size_t bytes () const = 0;
26495
26496 /* Write name table to FILE. */
26497 virtual void file_write (FILE *file) const = 0;
26498 };
26499
26500 /* Template to unify DWARF-32 and DWARF-64 output. */
26501 template<typename OffsetSize>
26502 class offset_vec_tmpl : public offset_vec
26503 {
26504 public:
26505 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
26506 : offset_vec (dwarf5_byte_order_)
26507 {}
26508
26509 /* Implement offset_vec::reserve. */
26510 void reserve (size_t nelem) override
26511 {
26512 m_vec.reserve (nelem);
26513 }
26514
26515 /* Implement offset_vec::push_back_reorder. */
26516 void push_back_reorder (size_t elem) override
26517 {
26518 m_vec.push_back (elem);
26519 /* Check for overflow. */
26520 gdb_assert (m_vec.back () == elem);
26521 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
26522 sizeof (m_vec.back ()), dwarf5_byte_order, elem);
26523 }
26524
26525 /* Implement offset_vec::bytes. */
26526 size_t bytes () const override
26527 {
26528 return m_vec.size () * sizeof (m_vec[0]);
26529 }
26530
26531 /* Implement offset_vec::file_write. */
26532 void file_write (FILE *file) const override
26533 {
26534 ::file_write (file, m_vec);
26535 }
26536
26537 private:
26538 std::vector<OffsetSize> m_vec;
26539 };
26540
26541 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
26542 respecting name table width. */
26543 class dwarf
26544 {
26545 public:
26546 offset_vec &name_table_string_offs, &name_table_entry_offs;
26547
26548 dwarf (offset_vec &name_table_string_offs_,
26549 offset_vec &name_table_entry_offs_)
26550 : name_table_string_offs (name_table_string_offs_),
26551 name_table_entry_offs (name_table_entry_offs_)
26552 {
26553 }
26554 };
e8f8bcb3 26555
437afbb8
JK
26556 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
26557 respecting name table width. */
26558 template<typename OffsetSize>
26559 class dwarf_tmpl : public dwarf
26560 {
26561 public:
26562 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
26563 : dwarf (m_name_table_string_offs, m_name_table_entry_offs),
26564 m_name_table_string_offs (dwarf5_byte_order_),
26565 m_name_table_entry_offs (dwarf5_byte_order_)
26566 {}
26567
26568 private:
26569 offset_vec_tmpl<OffsetSize> m_name_table_string_offs;
26570 offset_vec_tmpl<OffsetSize> m_name_table_entry_offs;
26571 };
26572
26573 /* Try to reconstruct original DWARF tag for given partial_symbol.
26574 This function is not DWARF-5 compliant but it is sufficient for
26575 GDB as a DWARF-5 index consumer. */
26576 static int psymbol_tag (const struct partial_symbol *psym)
26577 {
26578 domain_enum domain = PSYMBOL_DOMAIN (psym);
26579 enum address_class aclass = PSYMBOL_CLASS (psym);
26580
26581 switch (domain)
26582 {
26583 case VAR_DOMAIN:
26584 switch (aclass)
26585 {
26586 case LOC_BLOCK:
26587 return DW_TAG_subprogram;
26588 case LOC_TYPEDEF:
26589 return DW_TAG_typedef;
26590 case LOC_COMPUTED:
26591 case LOC_CONST_BYTES:
26592 case LOC_OPTIMIZED_OUT:
26593 case LOC_STATIC:
26594 return DW_TAG_variable;
26595 case LOC_CONST:
26596 /* Note: It's currently impossible to recognize psyms as enum values
26597 short of reading the type info. For now punt. */
26598 return DW_TAG_variable;
26599 default:
26600 /* There are other LOC_FOO values that one might want to classify
26601 as variables, but dwarf2read.c doesn't currently use them. */
26602 return DW_TAG_variable;
26603 }
26604 case STRUCT_DOMAIN:
26605 return DW_TAG_structure_type;
26606 default:
26607 return 0;
26608 }
26609 }
26610
26611 /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */
26612 void write_psymbols (std::unordered_set<partial_symbol *> &psyms_seen,
26613 struct partial_symbol **psymp, int count, int cu_index,
8af5c486 26614 bool is_static, unit_kind kind)
437afbb8
JK
26615 {
26616 for (; count-- > 0; ++psymp)
26617 {
26618 struct partial_symbol *psym = *psymp;
26619
26620 if (SYMBOL_LANGUAGE (psym) == language_ada)
26621 error (_("Ada is not currently supported by the index"));
26622
26623 /* Only add a given psymbol once. */
26624 if (psyms_seen.insert (psym).second)
8af5c486 26625 insert (psym, cu_index, is_static, kind);
437afbb8
JK
26626 }
26627 }
26628
8af5c486
JK
26629 /* A helper function that writes a single signatured_type
26630 to a debug_names. */
26631 void
26632 write_one_signatured_type (struct signatured_type *entry,
26633 struct signatured_type_index_data *info)
26634 {
26635 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
26636
26637 write_psymbols (info->psyms_seen,
26638 &info->objfile->global_psymbols[psymtab->globals_offset],
26639 psymtab->n_global_syms, info->cu_index, false,
26640 unit_kind::tu);
26641 write_psymbols (info->psyms_seen,
26642 &info->objfile->static_psymbols[psymtab->statics_offset],
26643 psymtab->n_static_syms, info->cu_index, true,
26644 unit_kind::tu);
26645
26646 info->types_list.append_uint (dwarf5_offset_size (), m_dwarf5_byte_order,
26647 to_underlying (entry->per_cu.sect_off));
26648
26649 ++info->cu_index;
26650 }
26651
437afbb8
JK
26652 /* Store value of each symbol. */
26653 std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher>
26654 m_name_to_value_set;
26655
26656 /* Tables of DWARF-5 .debug_names. They are in object file byte
26657 order. */
26658 std::vector<uint32_t> m_bucket_table;
26659 std::vector<uint32_t> m_hash_table;
26660
26661 const bfd_endian m_dwarf5_byte_order;
26662 dwarf_tmpl<uint32_t> m_dwarf32;
26663 dwarf_tmpl<uint64_t> m_dwarf64;
26664 dwarf &m_dwarf;
26665 offset_vec &m_name_table_string_offs, &m_name_table_entry_offs;
26666 debug_str_lookup m_debugstrlookup;
26667
26668 /* Map each used .debug_names abbreviation tag parameter to its
26669 index value. */
26670 std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
26671
26672 /* Next unused .debug_names abbreviation tag for
26673 m_indexkey_to_idx. */
26674 int m_idx_next = 1;
26675
26676 /* .debug_names abbreviation table. */
26677 data_buf m_abbrev_table;
26678
26679 /* .debug_names entry pool. */
26680 data_buf m_entry_pool;
26681};
26682
26683/* Return iff any of the needed offsets does not fit into 32-bit
26684 .debug_names section. */
26685
26686static bool
ed2dc618 26687check_dwarf64_offsets (struct dwarf2_per_objfile *dwarf2_per_objfile)
437afbb8
JK
26688{
26689 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26690 {
26691 const dwarf2_per_cu_data &per_cu = *dwarf2_per_objfile->all_comp_units[i];
26692
26693 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26694 return true;
26695 }
26696 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
26697 {
26698 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
26699 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
26700
26701 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26702 return true;
26703 }
26704 return false;
26705}
26706
26707/* The psyms_seen set is potentially going to be largish (~40k
26708 elements when indexing a -g3 build of GDB itself). Estimate the
26709 number of elements in order to avoid too many rehashes, which
26710 require rebuilding buckets and thus many trips to
26711 malloc/free. */
26712
26713static size_t
ed2dc618 26714psyms_seen_size (struct dwarf2_per_objfile *dwarf2_per_objfile)
437afbb8 26715{
e8f8bcb3
PA
26716 size_t psyms_count = 0;
26717 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26718 {
26719 struct dwarf2_per_cu_data *per_cu
26720 = dwarf2_per_objfile->all_comp_units[i];
26721 struct partial_symtab *psymtab = per_cu->v.psymtab;
26722
26723 if (psymtab != NULL && psymtab->user == NULL)
26724 recursively_count_psymbols (psymtab, psyms_count);
26725 }
26726 /* Generating an index for gdb itself shows a ratio of
26727 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
437afbb8
JK
26728 return psyms_count / 4;
26729}
26730
26731/* Write new .gdb_index section for OBJFILE into OUT_FILE.
26732 Return how many bytes were expected to be written into OUT_FILE. */
26733
26734static size_t
ed2dc618 26735write_gdbindex (struct dwarf2_per_objfile *dwarf2_per_objfile, FILE *out_file)
437afbb8 26736{
ed2dc618 26737 struct objfile *objfile = dwarf2_per_objfile->objfile;
437afbb8
JK
26738 mapped_symtab symtab;
26739 data_buf cu_list;
26740
26741 /* While we're scanning CU's create a table that maps a psymtab pointer
26742 (which is what addrmap records) to its index (which is what is recorded
26743 in the index file). This will later be needed to write the address
26744 table. */
26745 psym_index_map cu_index_htab;
26746 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
26747
26748 /* The CU list is already sorted, so we don't need to do additional
26749 work here. Also, the debug_types entries do not appear in
26750 all_comp_units, but only in their own hash table. */
26751
ed2dc618
SM
26752 std::unordered_set<partial_symbol *> psyms_seen
26753 (psyms_seen_size (dwarf2_per_objfile));
bc8f2430 26754 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd 26755 {
3e43a32a
MS
26756 struct dwarf2_per_cu_data *per_cu
26757 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 26758 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 26759
92fac807
JK
26760 /* CU of a shared file from 'dwz -m' may be unused by this main file.
26761 It may be referenced from a local scope but in such case it does not
26762 need to be present in .gdb_index. */
26763 if (psymtab == NULL)
26764 continue;
26765
95554aad 26766 if (psymtab->user == NULL)
bc8f2430
JK
26767 recursively_write_psymbols (objfile, psymtab, &symtab,
26768 psyms_seen, i);
9291a0cd 26769
bc8f2430
JK
26770 const auto insertpair = cu_index_htab.emplace (psymtab, i);
26771 gdb_assert (insertpair.second);
9291a0cd 26772
c2f134ac
PA
26773 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
26774 to_underlying (per_cu->sect_off));
26775 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
26776 }
26777
0a5429f6 26778 /* Dump the address map. */
bc8f2430
JK
26779 data_buf addr_vec;
26780 write_address_map (objfile, addr_vec, cu_index_htab);
0a5429f6 26781
1fd400ff 26782 /* Write out the .debug_type entries, if any. */
bc8f2430 26783 data_buf types_cu_list;
1fd400ff
TT
26784 if (dwarf2_per_objfile->signatured_types)
26785 {
bc8f2430
JK
26786 signatured_type_index_data sig_data (types_cu_list,
26787 psyms_seen);
1fd400ff
TT
26788
26789 sig_data.objfile = objfile;
bc8f2430 26790 sig_data.symtab = &symtab;
1fd400ff
TT
26791 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
26792 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
26793 write_one_signatured_type, &sig_data);
26794 }
26795
156942c7
DE
26796 /* Now that we've processed all symbols we can shrink their cu_indices
26797 lists. */
bc8f2430 26798 uniquify_cu_indices (&symtab);
156942c7 26799
bc8f2430
JK
26800 data_buf symtab_vec, constant_pool;
26801 write_hash_table (&symtab, symtab_vec, constant_pool);
9291a0cd 26802
bc8f2430
JK
26803 data_buf contents;
26804 const offset_type size_of_contents = 6 * sizeof (offset_type);
26805 offset_type total_len = size_of_contents;
9291a0cd
TT
26806
26807 /* The version number. */
bc8f2430 26808 contents.append_data (MAYBE_SWAP (8));
9291a0cd
TT
26809
26810 /* The offset of the CU list from the start of the file. */
bc8f2430
JK
26811 contents.append_data (MAYBE_SWAP (total_len));
26812 total_len += cu_list.size ();
9291a0cd 26813
1fd400ff 26814 /* The offset of the types CU list from the start of the file. */
bc8f2430
JK
26815 contents.append_data (MAYBE_SWAP (total_len));
26816 total_len += types_cu_list.size ();
1fd400ff 26817
9291a0cd 26818 /* The offset of the address table from the start of the file. */
bc8f2430
JK
26819 contents.append_data (MAYBE_SWAP (total_len));
26820 total_len += addr_vec.size ();
9291a0cd
TT
26821
26822 /* The offset of the symbol table from the start of the file. */
bc8f2430
JK
26823 contents.append_data (MAYBE_SWAP (total_len));
26824 total_len += symtab_vec.size ();
9291a0cd
TT
26825
26826 /* The offset of the constant pool from the start of the file. */
bc8f2430
JK
26827 contents.append_data (MAYBE_SWAP (total_len));
26828 total_len += constant_pool.size ();
9291a0cd 26829
bc8f2430 26830 gdb_assert (contents.size () == size_of_contents);
9291a0cd 26831
bc8f2430
JK
26832 contents.file_write (out_file);
26833 cu_list.file_write (out_file);
26834 types_cu_list.file_write (out_file);
26835 addr_vec.file_write (out_file);
26836 symtab_vec.file_write (out_file);
26837 constant_pool.file_write (out_file);
9291a0cd 26838
437afbb8
JK
26839 return total_len;
26840}
26841
26842/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
26843static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
26844
26845/* Write a new .debug_names section for OBJFILE into OUT_FILE, write
26846 needed addition to .debug_str section to OUT_FILE_STR. Return how
26847 many bytes were expected to be written into OUT_FILE. */
26848
26849static size_t
ed2dc618
SM
26850write_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
26851 FILE *out_file, FILE *out_file_str)
437afbb8 26852{
ed2dc618
SM
26853 const bool dwarf5_is_dwarf64 = check_dwarf64_offsets (dwarf2_per_objfile);
26854 struct objfile *objfile = dwarf2_per_objfile->objfile;
437afbb8
JK
26855 const enum bfd_endian dwarf5_byte_order
26856 = gdbarch_byte_order (get_objfile_arch (objfile));
26857
26858 /* The CU list is already sorted, so we don't need to do additional
26859 work here. Also, the debug_types entries do not appear in
26860 all_comp_units, but only in their own hash table. */
26861 data_buf cu_list;
ed2dc618
SM
26862 debug_names nametable (dwarf2_per_objfile, dwarf5_is_dwarf64,
26863 dwarf5_byte_order);
26864 std::unordered_set<partial_symbol *>
26865 psyms_seen (psyms_seen_size (dwarf2_per_objfile));
437afbb8
JK
26866 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26867 {
26868 const dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
26869 partial_symtab *psymtab = per_cu->v.psymtab;
26870
26871 /* CU of a shared file from 'dwz -m' may be unused by this main
26872 file. It may be referenced from a local scope but in such
26873 case it does not need to be present in .debug_names. */
26874 if (psymtab == NULL)
26875 continue;
26876
26877 if (psymtab->user == NULL)
26878 nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i);
26879
8af5c486 26880 cu_list.append_uint (nametable.dwarf5_offset_size (), dwarf5_byte_order,
437afbb8
JK
26881 to_underlying (per_cu->sect_off));
26882 }
437afbb8 26883
8af5c486 26884 /* Write out the .debug_type entries, if any. */
437afbb8 26885 data_buf types_cu_list;
8af5c486 26886 if (dwarf2_per_objfile->signatured_types)
437afbb8 26887 {
8af5c486
JK
26888 debug_names::write_one_signatured_type_data sig_data (nametable,
26889 signatured_type_index_data (types_cu_list, psyms_seen));
437afbb8 26890
8af5c486
JK
26891 sig_data.info.objfile = objfile;
26892 /* It is used only for gdb_index. */
26893 sig_data.info.symtab = nullptr;
26894 sig_data.info.cu_index = 0;
26895 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
26896 debug_names::write_one_signatured_type,
26897 &sig_data);
437afbb8
JK
26898 }
26899
8af5c486
JK
26900 nametable.build ();
26901
26902 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
26903
437afbb8
JK
26904 const offset_type bytes_of_header
26905 = ((dwarf5_is_dwarf64 ? 12 : 4)
26906 + 2 + 2 + 7 * 4
26907 + sizeof (dwarf5_gdb_augmentation));
26908 size_t expected_bytes = 0;
26909 expected_bytes += bytes_of_header;
26910 expected_bytes += cu_list.size ();
26911 expected_bytes += types_cu_list.size ();
26912 expected_bytes += nametable.bytes ();
26913 data_buf header;
26914
26915 if (!dwarf5_is_dwarf64)
26916 {
26917 const uint64_t size64 = expected_bytes - 4;
26918 gdb_assert (size64 < 0xfffffff0);
26919 header.append_uint (4, dwarf5_byte_order, size64);
26920 }
26921 else
26922 {
26923 header.append_uint (4, dwarf5_byte_order, 0xffffffff);
26924 header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
26925 }
26926
26927 /* The version number. */
26928 header.append_uint (2, dwarf5_byte_order, 5);
26929
26930 /* Padding. */
26931 header.append_uint (2, dwarf5_byte_order, 0);
26932
26933 /* comp_unit_count - The number of CUs in the CU list. */
26934 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_comp_units);
26935
26936 /* local_type_unit_count - The number of TUs in the local TU
26937 list. */
26938 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_type_units);
26939
26940 /* foreign_type_unit_count - The number of TUs in the foreign TU
26941 list. */
26942 header.append_uint (4, dwarf5_byte_order, 0);
26943
26944 /* bucket_count - The number of hash buckets in the hash lookup
26945 table. */
26946 header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ());
26947
26948 /* name_count - The number of unique names in the index. */
26949 header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
26950
26951 /* abbrev_table_size - The size in bytes of the abbreviations
26952 table. */
26953 header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
26954
26955 /* augmentation_string_size - The size in bytes of the augmentation
26956 string. This value is rounded up to a multiple of 4. */
26957 static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, "");
26958 header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation));
26959 header.append_data (dwarf5_gdb_augmentation);
26960
26961 gdb_assert (header.size () == bytes_of_header);
26962
26963 header.file_write (out_file);
26964 cu_list.file_write (out_file);
26965 types_cu_list.file_write (out_file);
26966 nametable.file_write (out_file, out_file_str);
26967
26968 return expected_bytes;
26969}
26970
26971/* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
26972 position is at the end of the file. */
26973
26974static void
26975assert_file_size (FILE *file, const char *filename, size_t expected_size)
26976{
26977 const auto file_size = ftell (file);
26978 if (file_size == -1)
26979 error (_("Can't get `%s' size"), filename);
26980 gdb_assert (file_size == expected_size);
26981}
26982
437afbb8
JK
26983/* Create an index file for OBJFILE in the directory DIR. */
26984
26985static void
ed2dc618
SM
26986write_psymtabs_to_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
26987 const char *dir,
437afbb8
JK
26988 dw_index_kind index_kind)
26989{
ed2dc618
SM
26990 struct objfile *objfile = dwarf2_per_objfile->objfile;
26991
437afbb8
JK
26992 if (dwarf2_per_objfile->using_index)
26993 error (_("Cannot use an index to create the index"));
26994
26995 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
26996 error (_("Cannot make an index when the file has multiple .debug_types sections"));
26997
26998 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
26999 return;
27000
27001 struct stat st;
27002 if (stat (objfile_name (objfile), &st) < 0)
27003 perror_with_name (objfile_name (objfile));
27004
27005 std::string filename (std::string (dir) + SLASH_STRING
27006 + lbasename (objfile_name (objfile))
27007 + (index_kind == dw_index_kind::DEBUG_NAMES
27008 ? INDEX5_SUFFIX : INDEX4_SUFFIX));
27009
27010 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
27011 if (!out_file)
27012 error (_("Can't open `%s' for writing"), filename.c_str ());
27013
27014 /* Order matters here; we want FILE to be closed before FILENAME is
27015 unlinked, because on MS-Windows one cannot delete a file that is
27016 still open. (Don't call anything here that might throw until
27017 file_closer is created.) */
27018 gdb::unlinker unlink_file (filename.c_str ());
27019 gdb_file_up close_out_file (out_file);
27020
27021 if (index_kind == dw_index_kind::DEBUG_NAMES)
27022 {
27023 std::string filename_str (std::string (dir) + SLASH_STRING
27024 + lbasename (objfile_name (objfile))
27025 + DEBUG_STR_SUFFIX);
27026 FILE *out_file_str
27027 = gdb_fopen_cloexec (filename_str.c_str (), "wb").release ();
27028 if (!out_file_str)
27029 error (_("Can't open `%s' for writing"), filename_str.c_str ());
27030 gdb::unlinker unlink_file_str (filename_str.c_str ());
27031 gdb_file_up close_out_file_str (out_file_str);
27032
27033 const size_t total_len
ed2dc618 27034 = write_debug_names (dwarf2_per_objfile, out_file, out_file_str);
437afbb8
JK
27035 assert_file_size (out_file, filename.c_str (), total_len);
27036
27037 /* We want to keep the file .debug_str file too. */
27038 unlink_file_str.keep ();
27039 }
27040 else
27041 {
27042 const size_t total_len
ed2dc618 27043 = write_gdbindex (dwarf2_per_objfile, out_file);
437afbb8
JK
27044 assert_file_size (out_file, filename.c_str (), total_len);
27045 }
27046
bef155c3
TT
27047 /* We want to keep the file. */
27048 unlink_file.keep ();
9291a0cd
TT
27049}
27050
90476074
TT
27051/* Implementation of the `save gdb-index' command.
27052
437afbb8
JK
27053 Note that the .gdb_index file format used by this command is
27054 documented in the GDB manual. Any changes here must be documented
27055 there. */
11570e71 27056
9291a0cd 27057static void
8384c356 27058save_gdb_index_command (const char *arg, int from_tty)
9291a0cd
TT
27059{
27060 struct objfile *objfile;
437afbb8
JK
27061 const char dwarf5space[] = "-dwarf-5 ";
27062 dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
27063
27064 if (!arg)
27065 arg = "";
27066
27067 arg = skip_spaces (arg);
27068 if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
27069 {
27070 index_kind = dw_index_kind::DEBUG_NAMES;
27071 arg += strlen (dwarf5space);
27072 arg = skip_spaces (arg);
27073 }
9291a0cd 27074
437afbb8
JK
27075 if (!*arg)
27076 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
9291a0cd
TT
27077
27078 ALL_OBJFILES (objfile)
27079 {
27080 struct stat st;
27081
27082 /* If the objfile does not correspond to an actual file, skip it. */
4262abfb 27083 if (stat (objfile_name (objfile), &st) < 0)
9291a0cd
TT
27084 continue;
27085
ed2dc618
SM
27086 struct dwarf2_per_objfile *dwarf2_per_objfile
27087 = get_dwarf2_per_objfile (objfile);
9291a0cd 27088
ed2dc618
SM
27089 if (dwarf2_per_objfile != NULL)
27090 {
492d29ea 27091 TRY
9291a0cd 27092 {
ed2dc618 27093 write_psymtabs_to_index (dwarf2_per_objfile, arg, index_kind);
9291a0cd 27094 }
492d29ea
PA
27095 CATCH (except, RETURN_MASK_ERROR)
27096 {
27097 exception_fprintf (gdb_stderr, except,
27098 _("Error while writing index for `%s': "),
27099 objfile_name (objfile));
27100 }
27101 END_CATCH
9291a0cd 27102 }
ed2dc618 27103
9291a0cd 27104 }
dce234bc
PP
27105}
27106
9291a0cd
TT
27107\f
27108
b4f54984 27109int dwarf_always_disassemble;
9eae7c52
TT
27110
27111static void
b4f54984
DE
27112show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
27113 struct cmd_list_element *c, const char *value)
9eae7c52 27114{
3e43a32a
MS
27115 fprintf_filtered (file,
27116 _("Whether to always disassemble "
27117 "DWARF expressions is %s.\n"),
9eae7c52
TT
27118 value);
27119}
27120
900e11f9
JK
27121static void
27122show_check_physname (struct ui_file *file, int from_tty,
27123 struct cmd_list_element *c, const char *value)
27124{
27125 fprintf_filtered (file,
27126 _("Whether to check \"physname\" is %s.\n"),
27127 value);
27128}
27129
6502dd73
DJ
27130void
27131_initialize_dwarf2_read (void)
27132{
96d19272
JK
27133 struct cmd_list_element *c;
27134
dce234bc 27135 dwarf2_objfile_data_key
c1bd65d0 27136 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 27137
b4f54984
DE
27138 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
27139Set DWARF specific variables.\n\
27140Configure DWARF variables such as the cache size"),
27141 &set_dwarf_cmdlist, "maintenance set dwarf ",
ae038cb0
DJ
27142 0/*allow-unknown*/, &maintenance_set_cmdlist);
27143
b4f54984
DE
27144 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
27145Show DWARF specific variables\n\
27146Show DWARF variables such as the cache size"),
27147 &show_dwarf_cmdlist, "maintenance show dwarf ",
ae038cb0
DJ
27148 0/*allow-unknown*/, &maintenance_show_cmdlist);
27149
27150 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
b4f54984
DE
27151 &dwarf_max_cache_age, _("\
27152Set the upper bound on the age of cached DWARF compilation units."), _("\
27153Show the upper bound on the age of cached DWARF compilation units."), _("\
7915a72c
AC
27154A higher limit means that cached compilation units will be stored\n\
27155in memory longer, and more total memory will be used. Zero disables\n\
27156caching, which can slow down startup."),
2c5b56ce 27157 NULL,
b4f54984
DE
27158 show_dwarf_max_cache_age,
27159 &set_dwarf_cmdlist,
27160 &show_dwarf_cmdlist);
d97bc12b 27161
9eae7c52 27162 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
b4f54984 27163 &dwarf_always_disassemble, _("\
9eae7c52
TT
27164Set whether `info address' always disassembles DWARF expressions."), _("\
27165Show whether `info address' always disassembles DWARF expressions."), _("\
27166When enabled, DWARF expressions are always printed in an assembly-like\n\
27167syntax. When disabled, expressions will be printed in a more\n\
27168conversational style, when possible."),
27169 NULL,
b4f54984
DE
27170 show_dwarf_always_disassemble,
27171 &set_dwarf_cmdlist,
27172 &show_dwarf_cmdlist);
27173
27174 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
27175Set debugging of the DWARF reader."), _("\
27176Show debugging of the DWARF reader."), _("\
27177When enabled (non-zero), debugging messages are printed during DWARF\n\
73be47f5
DE
27178reading and symtab expansion. A value of 1 (one) provides basic\n\
27179information. A value greater than 1 provides more verbose information."),
45cfd468
DE
27180 NULL,
27181 NULL,
27182 &setdebuglist, &showdebuglist);
27183
b4f54984
DE
27184 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
27185Set debugging of the DWARF DIE reader."), _("\
27186Show debugging of the DWARF DIE reader."), _("\
d97bc12b
DE
27187When enabled (non-zero), DIEs are dumped after they are read in.\n\
27188The value is the maximum depth to print."),
ccce17b0
YQ
27189 NULL,
27190 NULL,
27191 &setdebuglist, &showdebuglist);
9291a0cd 27192
27e0867f
DE
27193 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
27194Set debugging of the dwarf line reader."), _("\
27195Show debugging of the dwarf line reader."), _("\
27196When enabled (non-zero), line number entries are dumped as they are read in.\n\
27197A value of 1 (one) provides basic information.\n\
27198A value greater than 1 provides more verbose information."),
27199 NULL,
27200 NULL,
27201 &setdebuglist, &showdebuglist);
27202
900e11f9
JK
27203 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
27204Set cross-checking of \"physname\" code against demangler."), _("\
27205Show cross-checking of \"physname\" code against demangler."), _("\
27206When enabled, GDB's internal \"physname\" code is checked against\n\
27207the demangler."),
27208 NULL, show_check_physname,
27209 &setdebuglist, &showdebuglist);
27210
e615022a
DE
27211 add_setshow_boolean_cmd ("use-deprecated-index-sections",
27212 no_class, &use_deprecated_index_sections, _("\
27213Set whether to use deprecated gdb_index sections."), _("\
27214Show whether to use deprecated gdb_index sections."), _("\
27215When enabled, deprecated .gdb_index sections are used anyway.\n\
27216Normally they are ignored either because of a missing feature or\n\
27217performance issue.\n\
27218Warning: This option must be enabled before gdb reads the file."),
27219 NULL,
27220 NULL,
27221 &setlist, &showlist);
27222
96d19272 27223 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 27224 _("\
fc1a9d6e 27225Save a gdb-index file.\n\
437afbb8
JK
27226Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
27227\n\
27228No options create one file with .gdb-index extension for pre-DWARF-5\n\
27229compatible .gdb_index section. With -dwarf-5 creates two files with\n\
27230extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
96d19272
JK
27231 &save_cmdlist);
27232 set_cmd_completer (c, filename_completer);
f1e6e072
TT
27233
27234 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
27235 &dwarf2_locexpr_funcs);
27236 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
27237 &dwarf2_loclist_funcs);
27238
27239 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
27240 &dwarf2_block_frame_base_locexpr_funcs);
27241 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
27242 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
27243
27244#if GDB_SELF_TEST
27245 selftests::register_test ("dw2_expand_symtabs_matching",
27246 selftests::dw2_expand_symtabs_matching::run_test);
27247#endif
6502dd73 27248}
This page took 4.301838 seconds and 4 git commands to generate.